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 #include "libs/Module.h"
9 #include "libs/Kernel.h"
11 #include "TemperatureControl.h"
12 #include "TemperatureControlPool.h"
14 #include "modules/robot/Conveyor.h"
15 #include "PublicDataRequest.h"
17 #include "PublicData.h"
18 #include "ToolManagerPublicAccess.h"
19 #include "StreamOutputPool.h"
21 #include "checksumm.h"
23 #include "SlowTicker.h"
24 #include "ConfigValue.h"
25 #include "PID_Autotuner.h"
26 #include "SerialMessage.h"
29 // Temp sensor implementations:
30 #include "Thermistor.h"
33 #include "PT100_E3D.h"
35 #include "MRI_Hooks.h"
39 #define sensor_checksum CHECKSUM("sensor")
41 #define readings_per_second_checksum CHECKSUM("readings_per_second")
42 #define max_pwm_checksum CHECKSUM("max_pwm")
43 #define pwm_frequency_checksum CHECKSUM("pwm_frequency")
44 #define bang_bang_checksum CHECKSUM("bang_bang")
45 #define hysteresis_checksum CHECKSUM("hysteresis")
46 #define heater_pin_checksum CHECKSUM("heater_pin")
47 #define max_temp_checksum CHECKSUM("max_temp")
48 #define min_temp_checksum CHECKSUM("min_temp")
50 #define get_m_code_checksum CHECKSUM("get_m_code")
51 #define set_m_code_checksum CHECKSUM("set_m_code")
52 #define set_and_wait_m_code_checksum CHECKSUM("set_and_wait_m_code")
54 #define designator_checksum CHECKSUM("designator")
56 #define p_factor_checksum CHECKSUM("p_factor")
57 #define i_factor_checksum CHECKSUM("i_factor")
58 #define d_factor_checksum CHECKSUM("d_factor")
60 #define i_max_checksum CHECKSUM("i_max")
61 #define windup_checksum CHECKSUM("windup")
63 #define preset1_checksum CHECKSUM("preset1")
64 #define preset2_checksum CHECKSUM("preset2")
66 #define runaway_range_checksum CHECKSUM("runaway_range")
67 #define runaway_heating_timeout_checksum CHECKSUM("runaway_heating_timeout")
68 #define runaway_cooling_timeout_checksum CHECKSUM("runaway_cooling_timeout")
69 #define runaway_error_range_checksum CHECKSUM("runaway_error_range")
71 TemperatureControl::TemperatureControl(uint16_t name
, int index
)
82 TemperatureControl::~TemperatureControl()
87 void TemperatureControl::on_module_loaded()
90 // We start not desiring any temp
91 this->target_temperature
= UNDEFINED
;
92 this->sensor_settings
= false; // set to true if sensor settings have been overriden
97 // Register for events
98 this->register_for_event(ON_GCODE_RECEIVED
);
99 this->register_for_event(ON_GET_PUBLIC_DATA
);
100 this->register_for_event(ON_IDLE
);
102 if(!this->readonly
) {
103 this->register_for_event(ON_SECOND_TICK
);
104 this->register_for_event(ON_MAIN_LOOP
);
105 this->register_for_event(ON_SET_PUBLIC_DATA
);
106 this->register_for_event(ON_HALT
);
110 void TemperatureControl::on_halt(void *arg
)
115 this->heater_pin
.set(0);
116 this->target_temperature
= UNDEFINED
;
120 void TemperatureControl::on_idle(void *arg
)
126 void TemperatureControl::on_main_loop(void *argument
)
128 if (this->temp_violated
) {
129 this->temp_violated
= false;
130 THEKERNEL
->streams
->printf("ERROR: MINTEMP or MAXTEMP triggered on %s. Check your temperature sensors!\n", designator
.c_str());
131 THEKERNEL
->streams
->printf("HALT asserted - reset or M999 required\n");
132 THEKERNEL
->call_event(ON_HALT
, nullptr);
136 // Get configuration from the config file
137 void TemperatureControl::load_config()
141 this->set_m_code
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, set_m_code_checksum
)->by_default(104)->as_number();
142 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();
143 this->get_m_code
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, get_m_code_checksum
)->by_default(105)->as_number();
144 this->readings_per_second
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, readings_per_second_checksum
)->by_default(20)->as_number();
146 this->designator
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, designator_checksum
)->by_default(string("T"))->as_string();
148 // Runaway parameters
149 uint32_t n
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, runaway_range_checksum
)->by_default(20)->as_number();
151 this->runaway_range
= n
;
153 // these need to fit in 9 bits after dividing by 8 so max is 4088 secs or 68 minutes
154 n
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, runaway_heating_timeout_checksum
)->by_default(900)->as_number();
155 if(n
> 4088) n
= 4088;
156 this->runaway_heating_timeout
= n
/8; // we have 8 second ticks
157 n
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, runaway_cooling_timeout_checksum
)->by_default(0)->as_number(); // disable by default
158 if(n
> 4088) n
= 4088;
159 this->runaway_cooling_timeout
= n
/8;
161 this->runaway_error_range
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, runaway_error_range_checksum
)->by_default(1.0F
)->as_number();
163 // Max and min temperatures we are not allowed to get over (Safety)
164 this->max_temp
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, max_temp_checksum
)->by_default(300)->as_number();
165 this->min_temp
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, min_temp_checksum
)->by_default(0)->as_number();
168 this->heater_pin
.from_string( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, heater_pin_checksum
)->by_default("nc")->as_string());
169 if(this->heater_pin
.connected()){
170 this->readonly
= false;
171 this->heater_pin
.as_output();
174 this->readonly
= true;
177 // For backward compatibility, default to a thermistor sensor.
178 std::string sensor_type
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, sensor_checksum
)->by_default("thermistor")->as_string();
180 // Instantiate correct sensor (TBD: TempSensor factory?)
182 sensor
= nullptr; // In case we fail to create a new sensor.
183 if(sensor_type
.compare("thermistor") == 0) {
184 sensor
= new Thermistor();
185 } else if(sensor_type
.compare("max31855") == 0) {
186 sensor
= new Max31855();
187 } else if(sensor_type
.compare("ad8495") == 0) {
188 sensor
= new AD8495();
189 } else if(sensor_type
.compare("pt100_e3d") == 0) {
190 sensor
= new PT100_E3D();
192 sensor
= new TempSensor(); // A dummy implementation
194 sensor
->UpdateConfig(temperature_control_checksum
, this->name_checksum
);
196 this->preset1
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, preset1_checksum
)->by_default(0)->as_number();
197 this->preset2
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, preset2_checksum
)->by_default(0)->as_number();
200 // sigma-delta output modulation
203 if(!this->readonly
) {
204 // used to enable bang bang control of heater
205 this->use_bangbang
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, bang_bang_checksum
)->by_default(false)->as_bool();
206 this->hysteresis
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, hysteresis_checksum
)->by_default(2)->as_number();
207 this->windup
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, windup_checksum
)->by_default(false)->as_bool();
208 this->heater_pin
.max_pwm( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, max_pwm_checksum
)->by_default(255)->as_number() );
209 this->heater_pin
.set(0);
210 set_low_on_debug(heater_pin
.port_number
, heater_pin
.pin
);
211 // activate SD-DAC timer
212 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
);
217 THEKERNEL
->slow_ticker
->attach( this->readings_per_second
, this, &TemperatureControl::thermistor_read_tick
);
218 this->PIDdt
= 1.0 / this->readings_per_second
;
221 setPIDp( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, p_factor_checksum
)->by_default(10 )->as_number() );
222 setPIDi( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, i_factor_checksum
)->by_default(0.3f
)->as_number() );
223 setPIDd( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, d_factor_checksum
)->by_default(200)->as_number() );
225 if(!this->readonly
) {
226 // set to the same as max_pwm by default
227 this->i_max
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, i_max_checksum
)->by_default(this->heater_pin
.max_pwm())->as_number();
231 this->lastInput
= -1.0;
232 this->last_reading
= 0.0;
235 void TemperatureControl::on_gcode_received(void *argument
)
237 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
240 if( gcode
->m
== this->get_m_code
) {
241 char buf
[32]; // should be big enough for any status
242 int n
= snprintf(buf
, sizeof(buf
), "%s:%3.1f /%3.1f @%d ", this->designator
.c_str(), this->get_temperature(), ((target_temperature
<= 0) ? 0.0 : target_temperature
), this->o
);
243 gcode
->txt_after_ok
.append(buf
, n
);
247 if (gcode
->m
== 305) { // set or get sensor settings
248 if (gcode
->has_letter('S') && (gcode
->get_value('S') == this->pool_index
)) {
249 TempSensor::sensor_options_t args
= gcode
->get_args();
250 args
.erase('S'); // don't include the S
251 if(args
.size() > 0) {
252 // set the new options
253 if(sensor
->set_optional(args
)) {
254 this->sensor_settings
= true;
256 gcode
->stream
->printf("Unable to properly set sensor settings, make sure you specify all required values\n");
260 this->sensor_settings
= false;
263 }else if(!gcode
->has_letter('S')) {
264 gcode
->stream
->printf("%s(S%d): using %s\n", this->designator
.c_str(), this->pool_index
, this->readonly
?"Readonly" : this->use_bangbang
?"Bangbang":"PID");
266 TempSensor::sensor_options_t options
;
267 if(sensor
->get_optional(options
)) {
268 for(auto &i
: options
) {
269 // foreach optional value
270 gcode
->stream
->printf("%s(S%d): %c %1.18f\n", this->designator
.c_str(), this->pool_index
, i
.first
, i
.second
);
278 // readonly sensors don't handle the rest
279 if(this->readonly
) return;
281 if (gcode
->m
== 143) {
282 if (gcode
->has_letter('S') && (gcode
->get_value('S') == this->pool_index
)) {
283 if(gcode
->has_letter('P')) {
284 max_temp
= gcode
->get_value('P');
287 gcode
->stream
->printf("Nothing set NOTE Usage is M143 S0 P300 where <S> is the hotend index and <P> is the maximum temp to set\n");
290 }else if(gcode
->get_num_args() == 0) {
291 gcode
->stream
->printf("Maximum temperature for %s(%d) is %f°C\n", this->designator
.c_str(), this->pool_index
, max_temp
);
294 } else if (gcode
->m
== 301) {
295 if (gcode
->has_letter('S') && (gcode
->get_value('S') == this->pool_index
)) {
296 if (gcode
->has_letter('P'))
297 setPIDp( gcode
->get_value('P') );
298 if (gcode
->has_letter('I'))
299 setPIDi( gcode
->get_value('I') );
300 if (gcode
->has_letter('D'))
301 setPIDd( gcode
->get_value('D') );
302 if (gcode
->has_letter('X'))
303 this->i_max
= gcode
->get_value('X');
304 if (gcode
->has_letter('Y'))
305 this->heater_pin
.max_pwm(gcode
->get_value('Y'));
307 }else if(!gcode
->has_letter('S')) {
308 gcode
->stream
->printf("%s(S%d): Pf:%g If:%g Df:%g X(I_max):%g Y(max pwm):%d 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->heater_pin
.max_pwm(), o
);
311 } else if (gcode
->m
== 500 || gcode
->m
== 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
312 gcode
->stream
->printf(";PID settings, i_max, max_pwm:\nM301 S%d P%1.4f I%1.4f D%1.4f X%1.4f Y%d\n", this->pool_index
, this->p_factor
, this->i_factor
/ this->PIDdt
, this->d_factor
* this->PIDdt
, this->i_max
, this->heater_pin
.max_pwm());
314 gcode
->stream
->printf(";Max temperature setting:\nM143 S%d P%1.4f\n", this->pool_index
, this->max_temp
);
316 if(this->sensor_settings
) {
317 // get or save any sensor specific optional values
318 TempSensor::sensor_options_t options
;
319 if(sensor
->get_optional(options
) && !options
.empty()) {
320 gcode
->stream
->printf(";Optional temp sensor specific settings:\nM305 S%d", this->pool_index
);
321 for(auto &i
: options
) {
322 gcode
->stream
->printf(" %c%1.18f", i
.first
, i
.second
);
324 gcode
->stream
->printf("\n");
328 } else if( ( gcode
->m
== this->set_m_code
|| gcode
->m
== this->set_and_wait_m_code
) && gcode
->has_letter('S')) {
329 int tool
= gcode
->has_letter('T') ? gcode
->get_value('T') : -1;
330 // default on when no T, for unmanaged tools (e.g. heatbed)
331 bool am_tool
= (tool
== -1) ? true : false;
334 // this is safe as old configs as well as single extruder configs the toolmanager will not be running so will return false
335 // this will also ignore anything that the tool manager is not controlling and return false, otherwise it returns the active tool
337 bool ok
= PublicData::get_value( tool_manager_checksum
, is_active_tool_checksum
, this->name_checksum
, &returned_data
);
339 uint16_t active_tool_name
= *static_cast<uint16_t *>(returned_data
);
340 am_tool
= (active_tool_name
== this->name_checksum
);
344 // this is wrong because we can't get the names of any
345 // inactive tools to check that we are associated with
346 // one. Just assume one temperature control per
348 am_tool
= (tool
== this->pool_index
);
352 // required so temp change happens in order
353 // Allow set without wait to be processed immediately
354 if( gcode
->m
== this->set_and_wait_m_code
) {
355 THEKERNEL
->conveyor
->wait_for_idle();
358 float v
= gcode
->get_value('S');
361 this->target_temperature
= UNDEFINED
;
362 this->heater_pin
.set((this->o
= 0));
364 this->set_desired_temperature(v
);
365 // wait for temp to be reached, no more gcodes will be fetched until this is complete
366 if( gcode
->m
== this->set_and_wait_m_code
) {
367 if(isinf(get_temperature()) && isinf(sensor
->get_temperature())) {
368 THEKERNEL
->streams
->printf("Temperature reading is unreliable on %s HALT asserted - reset or M999 required\n", designator
.c_str());
369 THEKERNEL
->call_event(ON_HALT
, nullptr);
373 this->waiting
= true; // on_second_tick will announce temps
374 while ( get_temperature() < target_temperature
) {
375 THEKERNEL
->call_event(ON_IDLE
, this);
376 // check if ON_HALT was called (usually by kill button)
377 if(THEKERNEL
->is_halted() || this->target_temperature
== UNDEFINED
) {
378 THEKERNEL
->streams
->printf("Wait on temperature aborted by kill\n");
382 this->waiting
= false;
390 void TemperatureControl::on_get_public_data(void *argument
)
392 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
394 if(!pdr
->starts_with(temperature_control_checksum
)) return;
396 if(pdr
->second_element_is(pool_index_checksum
)) {
397 // asking for our instance pointer if we have this pool_index
398 if(pdr
->third_element_is(this->pool_index
)) {
399 static void *return_data
;
401 pdr
->set_data_ptr(&return_data
);
405 }else if(pdr
->second_element_is(poll_controls_checksum
)) {
406 // polling for all temperature controls
407 // add our data to the list which is passed in via the data_ptr
409 std::vector
<struct pad_temperature
> *v
= static_cast<std::vector
<pad_temperature
>*>(pdr
->get_data_ptr());
411 struct pad_temperature t
;
413 t
.current_temperature
= this->get_temperature();
414 t
.target_temperature
= (target_temperature
<= 0) ? 0 : this->target_temperature
;
416 t
.designator
= this->designator
;
417 t
.id
= this->name_checksum
;
421 }else if(pdr
->second_element_is(current_temperature_checksum
)) {
423 if(pdr
->third_element_is(this->name_checksum
)) {
424 // ok this is targeted at us, so set the requ3sted data in the pointer passed into us
425 struct pad_temperature
*t
= static_cast<pad_temperature
*>(pdr
->get_data_ptr());
426 t
->current_temperature
= this->get_temperature();
427 t
->target_temperature
= (target_temperature
<= 0) ? 0 : this->target_temperature
;
429 t
->designator
= this->designator
;
430 t
->id
= this->name_checksum
;
437 void TemperatureControl::on_set_public_data(void *argument
)
439 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
441 if(!pdr
->starts_with(temperature_control_checksum
)) return;
443 if(!pdr
->second_element_is(this->name_checksum
)) return;
445 // ok this is targeted at us, so set the temp
446 // NOTE unlike the M code this will set the temp now not when the queue is empty
447 float t
= *static_cast<float *>(pdr
->get_data_ptr());
448 this->set_desired_temperature(t
);
452 void TemperatureControl::set_desired_temperature(float desired_temperature
)
454 // Never go over the configured max temperature
455 if( desired_temperature
> this->max_temp
){
456 desired_temperature
= this->max_temp
;
459 if (desired_temperature
== 1.0F
)
460 desired_temperature
= preset1
;
461 else if (desired_temperature
== 2.0F
)
462 desired_temperature
= preset2
;
464 float last_target_temperature
= target_temperature
;
465 target_temperature
= desired_temperature
;
466 if (desired_temperature
<= 0.0F
){
468 heater_pin
.set((this->o
= 0));
470 }else if(last_target_temperature
<= 0.0F
) {
471 // if it was off and we are now turning it on we need to initialize
472 this->lastInput
= last_reading
;
473 // set to whatever the output currently is See http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-initialization/
474 this->iTerm
= this->o
;
475 if (this->iTerm
> this->i_max
) this->iTerm
= this->i_max
;
476 else if (this->iTerm
< 0.0) this->iTerm
= 0.0;
479 // reset the runaway state, even if it was a temp change
480 this->runaway_state
= NOT_HEATING
;
483 float TemperatureControl::get_temperature()
488 uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy
)
490 float temperature
= sensor
->get_temperature();
491 if(!this->readonly
&& target_temperature
> 2) {
492 if (isinf(temperature
) || temperature
< min_temp
|| temperature
> max_temp
) {
493 this->temp_violated
= true;
494 target_temperature
= UNDEFINED
;
495 heater_pin
.set((this->o
= 0));
497 pid_process(temperature
);
501 last_reading
= temperature
;
506 * Based on https://github.com/br3ttb/Arduino-PID-Library
508 void TemperatureControl::pid_process(float temperature
)
511 // bang bang is very simple, if temp is < target - hysteresis turn on full else if temp is > target + hysteresis turn heater off
513 if(temperature
> (target_temperature
+ hysteresis
) && this->o
> 0) {
514 heater_pin
.set(false);
515 this->o
= 0; // for display purposes only
517 } else if(temperature
< (target_temperature
- hysteresis
) && this->o
<= 0) {
518 if(heater_pin
.max_pwm() >= 255) {
520 this->heater_pin
.set(true);
521 this->o
= 255; // for display purposes only
523 // only to whatever max pwm is configured
524 this->heater_pin
.pwm(heater_pin
.max_pwm());
525 this->o
= heater_pin
.max_pwm(); // for display purposes only
531 // regular PID control
532 float error
= target_temperature
- temperature
;
534 float new_I
= this->iTerm
+ (error
* this->i_factor
);
535 if (new_I
> this->i_max
) new_I
= this->i_max
;
536 else if (new_I
< 0.0) new_I
= 0.0;
537 if(!this->windup
) this->iTerm
= new_I
;
539 float d
= (temperature
- this->lastInput
);
541 // calculate the PID output
542 // TODO does this need to be scaled by max_pwm/256? I think not as p_factor already does that
543 this->o
= (this->p_factor
* error
) + new_I
- (this->d_factor
* d
);
545 if (this->o
>= heater_pin
.max_pwm())
546 this->o
= heater_pin
.max_pwm();
547 else if (this->o
< 0)
549 else if(this->windup
)
550 this->iTerm
= new_I
; // Only update I term when output is not saturated.
552 this->heater_pin
.pwm(this->o
);
553 this->lastInput
= temperature
;
556 void TemperatureControl::on_second_tick(void *argument
)
559 // If waiting for a temperature to be reach, display it to keep host programs up to date on the progress
561 THEKERNEL
->streams
->printf("%s:%3.1f /%3.1f @%d\n", designator
.c_str(), get_temperature(), ((target_temperature
<= 0) ? 0.0 : target_temperature
), o
);
563 // Check whether or not there is a temperature runaway issue, if so stop everything and report it
564 if(THEKERNEL
->is_halted()) return;
566 // see if runaway detection is enabled
567 if(this->runaway_heating_timeout
== 0 && this->runaway_range
== 0) return;
569 // check every 8 seconds, depends on tick being 3 bits
570 if(++tick
!= 0) return;
572 if(this->target_temperature
<= 0){ // If we are not trying to heat, state is NOT_HEATING
573 this->runaway_state
= NOT_HEATING
;
576 float current_temperature
= this->get_temperature();
578 switch( this->runaway_state
){
579 case NOT_HEATING
: // If we were previously not trying to heat, but we are now, change to state WAITING_FOR_TEMP_TO_BE_REACHED
580 this->runaway_state
= (this->target_temperature
>= current_temperature
|| this->runaway_cooling_timeout
== 0) ? HEATING_UP
: COOLING_DOWN
;
581 this->runaway_timer
= 0;
587 // check temp has reached the target temperature within the given error range
588 if( (runaway_state
== HEATING_UP
&& current_temperature
>= (this->target_temperature
- this->runaway_error_range
)) ||
589 (runaway_state
== COOLING_DOWN
&& current_temperature
<= (this->target_temperature
+ this->runaway_error_range
)) ) {
590 this->runaway_state
= TARGET_TEMPERATURE_REACHED
;
591 this->runaway_timer
= 0;
595 uint16_t t
= (runaway_state
== HEATING_UP
) ? this->runaway_heating_timeout
: this->runaway_cooling_timeout
;
596 // we are still heating up see if we have hit the max time allowed
597 if(t
> 0 && ++this->runaway_timer
> t
){
598 THEKERNEL
->streams
->printf("ERROR: Temperature took too long to be reached on %s, HALT asserted, TURN POWER OFF IMMEDIATELY - reset or M999 required\n", designator
.c_str());
599 THEKERNEL
->call_event(ON_HALT
, nullptr);
600 this->runaway_state
= NOT_HEATING
;
601 this->runaway_timer
= 0;
606 case TARGET_TEMPERATURE_REACHED
:
607 if(this->runaway_range
!= 0) {
608 // we are in state TARGET_TEMPERATURE_REACHED, check for thermal runaway
609 float delta
= current_temperature
- this->target_temperature
;
611 // If the temperature is outside the acceptable range for 8 seconds, this allows for some noise spikes without halting
612 if(fabsf(delta
) > this->runaway_range
){
613 if(this->runaway_timer
++ >= 1) { // this being 8 seconds
614 THEKERNEL
->streams
->printf("ERROR: Temperature runaway on %s (delta temp %f), HALT asserted, TURN POWER OFF IMMEDIATELY - reset or M999 required\n", designator
.c_str(), delta
);
615 THEKERNEL
->call_event(ON_HALT
, nullptr);
616 this->runaway_state
= NOT_HEATING
;
617 this->runaway_timer
= 0;
621 this->runaway_timer
= 0;
630 void TemperatureControl::setPIDp(float p
)
635 void TemperatureControl::setPIDi(float i
)
637 this->i_factor
= i
* this->PIDdt
;
640 void TemperatureControl::setPIDd(float d
)
642 this->d_factor
= d
/ this->PIDdt
;