2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
8 // TODO : THIS FILE IS LAME, MUST BE MADE MUCH BETTER
10 #include "libs/Module.h"
11 #include "libs/Kernel.h"
13 #include "TemperatureControl.h"
14 #include "TemperatureControlPool.h"
16 #include "libs/Median.h"
17 #include "modules/robot/Conveyor.h"
18 #include "PublicDataRequest.h"
20 #include "MRI_Hooks.h"
22 TemperatureControl::TemperatureControl(uint16_t name
) :
23 name_checksum(name
), waiting(false), min_temp_violated(false) {}
25 void TemperatureControl::on_module_loaded(){
27 // We start not desiring any temp
28 this->target_temperature
= UNDEFINED
;
31 this->on_config_reload(this);
33 this->acceleration_factor
= 10;
35 // Register for events
36 register_for_event(ON_CONFIG_RELOAD
);
37 this->register_for_event(ON_GCODE_EXECUTE
);
38 this->register_for_event(ON_GCODE_RECEIVED
);
39 this->register_for_event(ON_MAIN_LOOP
);
40 this->register_for_event(ON_SECOND_TICK
);
41 this->register_for_event(ON_GET_PUBLIC_DATA
);
42 this->register_for_event(ON_SET_PUBLIC_DATA
);
45 void TemperatureControl::on_main_loop(void* argument
){
46 if (this->min_temp_violated
) {
47 THEKERNEL
->streams
->printf("Error: MINTEMP triggered on P%d.%d! check your thermistors!\n", this->thermistor_pin
.port_number
, this->thermistor_pin
.pin
);
48 this->min_temp_violated
= false;
52 // Get configuration from the config file
53 void TemperatureControl::on_config_reload(void* argument
){
56 this->set_m_code
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, set_m_code_checksum
)->by_default(104)->as_number();
57 this->set_and_wait_m_code
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, set_and_wait_m_code_checksum
)->by_default(109)->as_number();
58 this->get_m_code
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, get_m_code_checksum
)->by_default(105)->as_number();
59 this->readings_per_second
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, readings_per_second_checksum
)->by_default(20)->as_number();
61 this->designator
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, designator_checksum
)->by_default(string("T"))->as_string();
63 // Values are here : http://reprap.org/wiki/Thermistor
70 // Preset values for various common types of thermistors
71 ConfigValue
* thermistor
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, thermistor_checksum
);
72 if( thermistor
->value
.compare("EPCOS100K" ) == 0 ){ // Default
73 }else if( thermistor
->value
.compare("RRRF100K" ) == 0 ){ this->beta
= 3960;
74 }else if( thermistor
->value
.compare("RRRF10K" ) == 0 ){ this->beta
= 3964; this->r0
= 10000; this->r1
= 680; this->r2
= 1600;
75 }else if( thermistor
->value
.compare("Honeywell100K") == 0 ){ this->beta
= 3974;
76 }else if( thermistor
->value
.compare("Semitec" ) == 0 ){ this->beta
= 4267;
77 }else if( thermistor
->value
.compare("HT100K" ) == 0 ){ this->beta
= 3990; }
79 // Preset values are overriden by specified values
80 this->r0
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, r0_checksum
)->by_default(this->r0
)->as_number(); // Stated resistance eg. 100K
81 this->t0
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, t0_checksum
)->by_default(this->t0
)->as_number(); // Temperature at stated resistance, eg. 25C
82 this->beta
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, beta_checksum
)->by_default(this->beta
)->as_number(); // Thermistor beta rating. See http://reprap.org/bin/view/Main/MeasuringThermistorBeta
83 this->r1
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, r1_checksum
)->by_default(this->r1
)->as_number();
84 this->r2
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, r2_checksum
)->by_default(this->r2
)->as_number();
86 this->preset1
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, preset1_checksum
)->by_default(0)->as_number();
87 this->preset2
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, preset2_checksum
)->by_default(0)->as_number();
92 k
= (1.0 / (t0
+ 273.15));
94 // sigma-delta output modulation
97 // Thermistor pin for ADC readings
98 this->thermistor_pin
.from_string(THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, thermistor_pin_checksum
)->required()->as_string());
99 THEKERNEL
->adc
->enable_pin(&thermistor_pin
);
102 this->heater_pin
.from_string( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, heater_pin_checksum
)->required()->as_string())->as_output();
103 this->heater_pin
.max_pwm( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, max_pwm_checksum
)->by_default(255)->as_number() );
104 this->heater_pin
.set(0);
106 set_low_on_debug(heater_pin
.port_number
, heater_pin
.pin
);
108 // activate SD-DAC timer
109 THEKERNEL
->slow_ticker
->attach( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, pwm_frequency_checksum
)->by_default(2000)->as_number() , &heater_pin
, &Pwm::on_tick
);
112 THEKERNEL
->slow_ticker
->attach( this->readings_per_second
, this, &TemperatureControl::thermistor_read_tick
);
113 this->PIDdt
= 1.0 / this->readings_per_second
;
116 setPIDp( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, p_factor_checksum
)->by_default(10 )->as_number() );
117 setPIDi( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, i_factor_checksum
)->by_default(0.3)->as_number() );
118 setPIDd( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, d_factor_checksum
)->by_default(200)->as_number() );
119 // set to the same as max_pwm by default
120 this->i_max
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, i_max_checksum
)->by_default(this->heater_pin
.max_pwm())->as_number();
122 this->lastInput
= -1.0;
123 this->last_reading
= 0.0;
126 void TemperatureControl::on_gcode_received(void* argument
){
127 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
130 if( gcode
->m
== this->get_m_code
){
131 char buf
[32]; // should be big enough for any status
132 int n
= snprintf(buf
, sizeof(buf
), "%s:%3.1f /%3.1f @%d ", this->designator
.c_str(), this->get_temperature(), ((target_temperature
== UNDEFINED
)?0.0:target_temperature
), this->o
);
133 gcode
->txt_after_ok
.append(buf
, n
);
134 gcode
->mark_as_taken();
136 } else if (gcode
->m
== 301) {
137 gcode
->mark_as_taken();
138 if (gcode
->has_letter('S') && (gcode
->get_value('S') == this->pool_index
))
140 if (gcode
->has_letter('P'))
141 setPIDp( gcode
->get_value('P') );
142 if (gcode
->has_letter('I'))
143 setPIDi( gcode
->get_value('I') );
144 if (gcode
->has_letter('D'))
145 setPIDd( gcode
->get_value('D') );
146 if (gcode
->has_letter('X'))
147 this->i_max
= gcode
->get_value('X');
149 //gcode->stream->printf("%s(S%d): Pf:%g If:%g Df:%g X(I_max):%g Pv:%g Iv:%g Dv:%g O:%d\n", this->designator.c_str(), this->pool_index, this->p_factor, this->i_factor/this->PIDdt, this->d_factor*this->PIDdt, this->i_max, this->p, this->i, this->d, o);
150 gcode
->stream
->printf("%s(S%d): Pf:%g If:%g Df:%g X(I_max):%g O:%d\n", this->designator
.c_str(), this->pool_index
, this->p_factor
, this->i_factor
/this->PIDdt
, this->d_factor
*this->PIDdt
, this->i_max
, o
);
152 } else if (gcode
->m
== 303) {
153 if (gcode
->has_letter('E') && (gcode
->get_value('E') == this->pool_index
)) {
154 gcode
->mark_as_taken();
155 double target
= 150.0;
156 if (gcode
->has_letter('S')) {
157 target
= gcode
->get_value('S');
158 gcode
->stream
->printf("Target: %5.1f\n", target
);
161 if (gcode
->has_letter('C')) {
162 ncycles
= gcode
->get_value('C');
164 gcode
->stream
->printf("Start PID tune, command is %s\n", gcode
->command
.c_str());
165 this->pool
->PIDtuner
->begin(this, target
, gcode
->stream
, ncycles
);
168 } else if (gcode
->m
== 500 || gcode
->m
== 503){// M500 saves some volatile settings to config override file, M503 just prints the settings
169 gcode
->stream
->printf(";PID settings:\nM301 P%1.4f I%1.4f D%1.4f\n", this->p_factor
, this->i_factor
/this->PIDdt
, this->d_factor
*this->PIDdt
);
170 gcode
->mark_as_taken();
172 } else if( ( gcode
->m
== this->set_m_code
|| gcode
->m
== this->set_and_wait_m_code
) && gcode
->has_letter('S') ) {
173 gcode
->mark_as_taken();
175 // Attach gcodes to the last block for on_gcode_execute
176 if( THEKERNEL
->conveyor
->queue
.size() == 0 ){
177 THEKERNEL
->call_event(ON_GCODE_EXECUTE
, gcode
);
179 Block
* block
= THEKERNEL
->conveyor
->queue
.get_ref( THEKERNEL
->conveyor
->queue
.size() - 1 );
180 block
->append_gcode(gcode
);
186 void TemperatureControl::on_gcode_execute(void* argument
){
187 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
189 if (((gcode
->m
== this->set_m_code
) || (gcode
->m
== this->set_and_wait_m_code
))
190 && gcode
->has_letter('S'))
192 double v
= gcode
->get_value('S');
196 this->target_temperature
= UNDEFINED
;
197 this->heater_pin
.set(0);
201 this->set_desired_temperature(v
);
203 if( gcode
->m
== this->set_and_wait_m_code
)
205 THEKERNEL
->pauser
->take();
206 this->waiting
= true;
213 void TemperatureControl::on_get_public_data(void* argument
){
214 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
216 if(!pdr
->starts_with(temperature_control_checksum
)) return;
218 if(!pdr
->second_element_is(this->name_checksum
)) return; // will be bed or hotend
220 // ok this is targeted at us, so send back the requested data
221 if(pdr
->third_element_is(current_temperature_checksum
)) {
222 // this must be static as it will be accessed long after we have returned
223 static struct pad_temperature temp_return
;
224 temp_return
.current_temperature
= this->get_temperature();
225 temp_return
.target_temperature
= (target_temperature
== UNDEFINED
) ? 0 : this->target_temperature
;
226 temp_return
.pwm
= this->o
;
228 pdr
->set_data_ptr(&temp_return
);
233 void TemperatureControl::on_set_public_data(void* argument
){
234 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
236 if(!pdr
->starts_with(temperature_control_checksum
)) return;
238 if(!pdr
->second_element_is(this->name_checksum
)) return; // will be bed or hotend
240 // ok this is targeted at us, so set the temp
241 double t
= *static_cast<double*>(pdr
->get_data_ptr());
242 this->set_desired_temperature(t
);
246 void TemperatureControl::set_desired_temperature(double desired_temperature
)
248 if (desired_temperature
== 1.0)
249 desired_temperature
= preset1
;
250 else if (desired_temperature
== 2.0)
251 desired_temperature
= preset2
;
253 target_temperature
= desired_temperature
;
254 if (desired_temperature
== 0.0)
255 heater_pin
.set((o
= 0));
258 double TemperatureControl::get_temperature(){
262 double TemperatureControl::adc_value_to_temperature(int adc_value
)
264 if ((adc_value
== 4095) || (adc_value
== 0))
266 double r
= r2
/ ((4095.0 / adc_value
) - 1.0);
268 r
= (r1
* r
) / (r1
- r
);
269 return (1.0 / (k
+ (j
* log(r
/ r0
)))) - 273.15;
272 uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy
){
273 int r
= new_thermistor_reading();
275 double temperature
= adc_value_to_temperature(r
);
277 if (target_temperature
> 0)
279 if ((r
<= 1) || (r
>= 4094))
281 this->min_temp_violated
= true;
282 target_temperature
= UNDEFINED
;
287 pid_process(temperature
);
288 if ((temperature
> target_temperature
) && waiting
)
290 THEKERNEL
->pauser
->release();
297 heater_pin
.set((o
= 0));
299 last_reading
= temperature
;
304 * Based on https://github.com/br3ttb/Arduino-PID-Library
306 void TemperatureControl::pid_process(double temperature
)
308 double error
= target_temperature
- temperature
;
310 this->iTerm
+= (error
* this->i_factor
);
311 if (this->iTerm
> this->i_max
) this->iTerm
= this->i_max
;
312 else if (this->iTerm
< 0.0) this->iTerm
= 0.0;
314 if(this->lastInput
< 0.0) this->lastInput
= temperature
; // set first time
315 double d
= (temperature
- this->lastInput
);
317 // calculate the PID output
318 // TODO does this need to be scaled by max_pwm/256? I think not as p_factor already does that
319 this->o
= (this->p_factor
*error
) + this->iTerm
- (this->d_factor
*d
);
321 if (this->o
>= heater_pin
.max_pwm())
322 this->o
= heater_pin
.max_pwm();
323 else if (this->o
< 0)
326 this->heater_pin
.pwm(this->o
);
327 this->lastInput
= temperature
;
330 int TemperatureControl::new_thermistor_reading()
332 int last_raw
= THEKERNEL
->adc
->read(&thermistor_pin
);
333 if (queue
.size() >= queue
.capacity()) {
337 uint16_t r
= last_raw
;
339 for (int i
=0; i
<queue
.size(); i
++)
340 median_buffer
[i
] = *queue
.get_ref(i
);
341 uint16_t m
= median_buffer
[quick_median(median_buffer
, queue
.size())];
345 void TemperatureControl::on_second_tick(void* argument
)
348 THEKERNEL
->streams
->printf("%s:%3.1f /%3.1f @%d\n", designator
.c_str(), get_temperature(), ((target_temperature
== UNDEFINED
)?0.0:target_temperature
), o
);
351 void TemperatureControl::setPIDp(double p
) {
355 void TemperatureControl::setPIDi(double i
) {
356 this->i_factor
= i
*this->PIDdt
;
359 void TemperatureControl::setPIDd(double d
) {
360 this->d_factor
= d
/this->PIDdt
;