Merge pull request #933 from Smoothieware/edge
[clinton/Smoothieware.git] / src / modules / tools / temperaturecontrol / TemperatureControl.cpp
1 /*
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/>.
6 */
7
8 // TODO : THIS FILE IS LAME, MUST BE MADE MUCH BETTER
9
10 #include "libs/Module.h"
11 #include "libs/Kernel.h"
12 #include <math.h>
13 #include "TemperatureControl.h"
14 #include "TemperatureControlPool.h"
15 #include "libs/Pin.h"
16 #include "modules/robot/Conveyor.h"
17 #include "PublicDataRequest.h"
18
19 #include "PublicData.h"
20 #include "ToolManagerPublicAccess.h"
21 #include "StreamOutputPool.h"
22 #include "Config.h"
23 #include "checksumm.h"
24 #include "Gcode.h"
25 #include "SlowTicker.h"
26 #include "ConfigValue.h"
27 #include "PID_Autotuner.h"
28 #include "SerialMessage.h"
29 #include "utils.h"
30
31 // Temp sensor implementations:
32 #include "Thermistor.h"
33 #include "max31855.h"
34 #include "AD8495.h"
35
36 #include "MRI_Hooks.h"
37
38 #define UNDEFINED -1
39
40 #define sensor_checksum CHECKSUM("sensor")
41
42 #define readings_per_second_checksum CHECKSUM("readings_per_second")
43 #define max_pwm_checksum CHECKSUM("max_pwm")
44 #define pwm_frequency_checksum CHECKSUM("pwm_frequency")
45 #define bang_bang_checksum CHECKSUM("bang_bang")
46 #define hysteresis_checksum CHECKSUM("hysteresis")
47 #define heater_pin_checksum CHECKSUM("heater_pin")
48 #define max_temp_checksum CHECKSUM("max_temp")
49 #define min_temp_checksum CHECKSUM("min_temp")
50
51 #define get_m_code_checksum CHECKSUM("get_m_code")
52 #define set_m_code_checksum CHECKSUM("set_m_code")
53 #define set_and_wait_m_code_checksum CHECKSUM("set_and_wait_m_code")
54
55 #define designator_checksum CHECKSUM("designator")
56
57 #define p_factor_checksum CHECKSUM("p_factor")
58 #define i_factor_checksum CHECKSUM("i_factor")
59 #define d_factor_checksum CHECKSUM("d_factor")
60
61 #define i_max_checksum CHECKSUM("i_max")
62 #define windup_checksum CHECKSUM("windup")
63
64 #define preset1_checksum CHECKSUM("preset1")
65 #define preset2_checksum CHECKSUM("preset2")
66
67 #define runaway_range_checksum CHECKSUM("runaway_range")
68 #define runaway_timeout_checksum CHECKSUM("runaway_timeout")
69 #define runaway_heating_timeout_checksum CHECKSUM("runaway_heating_timeout")
70
71 TemperatureControl::TemperatureControl(uint16_t name, int index)
72 {
73 name_checksum= name;
74 pool_index= index;
75 waiting= false;
76 temp_violated= false;
77 sensor= nullptr;
78 readonly= false;
79 }
80
81 TemperatureControl::~TemperatureControl()
82 {
83 delete sensor;
84 }
85
86 void TemperatureControl::on_module_loaded()
87 {
88
89 // We start not desiring any temp
90 this->target_temperature = UNDEFINED;
91 this->sensor_settings= false; // set to true if sensor settings have been overriden
92
93 // Settings
94 this->load_config();
95
96 // Register for events
97 this->register_for_event(ON_GCODE_RECEIVED);
98 this->register_for_event(ON_GET_PUBLIC_DATA);
99
100 if(!this->readonly) {
101 this->register_for_event(ON_SECOND_TICK);
102 this->register_for_event(ON_MAIN_LOOP);
103 this->register_for_event(ON_SET_PUBLIC_DATA);
104 this->register_for_event(ON_HALT);
105 }
106 }
107
108 void TemperatureControl::on_halt(void *arg)
109 {
110 if(arg == nullptr) {
111 // turn off heater
112 this->o = 0;
113 this->heater_pin.set(0);
114 this->target_temperature = UNDEFINED;
115 }
116 }
117
118 void TemperatureControl::on_main_loop(void *argument)
119 {
120 if (this->temp_violated) {
121 this->temp_violated = false;
122 THEKERNEL->streams->printf("Error: MINTEMP or MAXTEMP triggered on %s. Check your temperature sensors!\n", designator.c_str());
123 THEKERNEL->streams->printf("HALT asserted - reset or M999 required\n");
124 THEKERNEL->call_event(ON_HALT, nullptr);
125 }
126 }
127
128 // Get configuration from the config file
129 void TemperatureControl::load_config()
130 {
131
132 // General config
133 this->set_m_code = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, set_m_code_checksum)->by_default(104)->as_number();
134 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();
135 this->get_m_code = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, get_m_code_checksum)->by_default(105)->as_number();
136 this->readings_per_second = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, readings_per_second_checksum)->by_default(20)->as_number();
137
138 this->designator = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, designator_checksum)->by_default(string("T"))->as_string();
139
140 // Runaway parameters
141 this->runaway_range = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_range_checksum)->by_default(0)->as_number();
142 this->runaway_timeout = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_timeout_checksum)->by_default(0)->as_number();
143 this->runaway_heating_timeout = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_heating_timeout_checksum)->by_default(0)->as_number();
144
145 // Max and min temperatures we are not allowed to get over (Safety)
146 this->max_temp = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, max_temp_checksum)->by_default(300)->as_number();
147 this->min_temp = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, min_temp_checksum)->by_default(0)->as_number();
148
149 // Heater pin
150 this->heater_pin.from_string( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, heater_pin_checksum)->by_default("nc")->as_string());
151 if(this->heater_pin.connected()){
152 this->readonly= false;
153 this->heater_pin.as_output();
154
155 } else {
156 this->readonly= true;
157 }
158
159 // For backward compatibility, default to a thermistor sensor.
160 std::string sensor_type = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, sensor_checksum)->by_default("thermistor")->as_string();
161
162 // Instantiate correct sensor (TBD: TempSensor factory?)
163 delete sensor;
164 sensor = nullptr; // In case we fail to create a new sensor.
165 if(sensor_type.compare("thermistor") == 0) {
166 sensor = new Thermistor();
167 } else if(sensor_type.compare("max31855") == 0) {
168 sensor = new Max31855();
169 } else if(sensor_type.compare("ad8495") == 0) {
170 sensor = new AD8495();
171 } else {
172 sensor = new TempSensor(); // A dummy implementation
173 }
174 sensor->UpdateConfig(temperature_control_checksum, this->name_checksum);
175
176 this->preset1 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, preset1_checksum)->by_default(0)->as_number();
177 this->preset2 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, preset2_checksum)->by_default(0)->as_number();
178
179
180 // sigma-delta output modulation
181 this->o = 0;
182
183 if(!this->readonly) {
184 // used to enable bang bang control of heater
185 this->use_bangbang = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, bang_bang_checksum)->by_default(false)->as_bool();
186 this->hysteresis = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, hysteresis_checksum)->by_default(2)->as_number();
187 this->windup = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, windup_checksum)->by_default(false)->as_bool();
188 this->heater_pin.max_pwm( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, max_pwm_checksum)->by_default(255)->as_number() );
189 this->heater_pin.set(0);
190 set_low_on_debug(heater_pin.port_number, heater_pin.pin);
191 // activate SD-DAC timer
192 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);
193 }
194
195
196 // reading tick
197 THEKERNEL->slow_ticker->attach( this->readings_per_second, this, &TemperatureControl::thermistor_read_tick );
198 this->PIDdt = 1.0 / this->readings_per_second;
199
200 // PID
201 setPIDp( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, p_factor_checksum)->by_default(10 )->as_number() );
202 setPIDi( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, i_factor_checksum)->by_default(0.3f)->as_number() );
203 setPIDd( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, d_factor_checksum)->by_default(200)->as_number() );
204
205 if(!this->readonly) {
206 // set to the same as max_pwm by default
207 this->i_max = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, i_max_checksum )->by_default(this->heater_pin.max_pwm())->as_number();
208 }
209
210 this->iTerm = 0.0;
211 this->lastInput = -1.0;
212 this->last_reading = 0.0;
213 }
214
215 void TemperatureControl::on_gcode_received(void *argument)
216 {
217 Gcode *gcode = static_cast<Gcode *>(argument);
218 if (gcode->has_m) {
219
220 if( gcode->m == this->get_m_code ) {
221 char buf[32]; // should be big enough for any status
222 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);
223 gcode->txt_after_ok.append(buf, n);
224 return;
225 }
226
227 if (gcode->m == 305) { // set or get sensor settings
228 if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) {
229 TempSensor::sensor_options_t args= gcode->get_args();
230 args.erase('S'); // don't include the S
231 if(args.size() > 0) {
232 // set the new options
233 if(sensor->set_optional(args)) {
234 this->sensor_settings= true;
235 }else{
236 gcode->stream->printf("Unable to properly set sensor settings, make sure you specify all required values\n");
237 }
238 }else{
239 // don't override
240 this->sensor_settings= false;
241 }
242
243 }else if(!gcode->has_letter('S')) {
244 gcode->stream->printf("%s(S%d): using %s\n", this->designator.c_str(), this->pool_index, this->readonly?"Readonly" : this->use_bangbang?"Bangbang":"PID");
245 sensor->get_raw();
246 TempSensor::sensor_options_t options;
247 if(sensor->get_optional(options)) {
248 for(auto &i : options) {
249 // foreach optional value
250 gcode->stream->printf("%s(S%d): %c %1.18f\n", this->designator.c_str(), this->pool_index, i.first, i.second);
251 }
252 }
253 }
254
255 return;
256 }
257
258 // readonly sensors don't handle the rest
259 if(this->readonly) return;
260
261 if (gcode->m == 143) {
262 if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) {
263 if(gcode->has_letter('P')) {
264 max_temp= gcode->get_value('P');
265
266 } else {
267 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");
268 }
269
270 }else if(gcode->get_num_args() == 0) {
271 gcode->stream->printf("Maximum temperature for %s(%d) is %f°C\n", this->designator.c_str(), this->pool_index, max_temp);
272 }
273
274 } else if (gcode->m == 301) {
275 if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) {
276 if (gcode->has_letter('P'))
277 setPIDp( gcode->get_value('P') );
278 if (gcode->has_letter('I'))
279 setPIDi( gcode->get_value('I') );
280 if (gcode->has_letter('D'))
281 setPIDd( gcode->get_value('D') );
282 if (gcode->has_letter('X'))
283 this->i_max = gcode->get_value('X');
284 if (gcode->has_letter('Y'))
285 this->heater_pin.max_pwm(gcode->get_value('Y'));
286
287 }else if(!gcode->has_letter('S')) {
288 gcode->stream->printf("%s(S%d): Pf:%g If:%g Df:%g X(I_max):%g 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);
289 }
290
291 } else if (gcode->m == 500 || gcode->m == 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
292 gcode->stream->printf(";PID settings:\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());
293
294 gcode->stream->printf(";Max temperature setting:\nM143 S%d P%1.4f\n", this->pool_index, this->max_temp);
295
296 if(this->sensor_settings) {
297 // get or save any sensor specific optional values
298 TempSensor::sensor_options_t options;
299 if(sensor->get_optional(options) && !options.empty()) {
300 gcode->stream->printf(";Optional temp sensor specific settings:\nM305 S%d", this->pool_index);
301 for(auto &i : options) {
302 gcode->stream->printf(" %c%1.18f", i.first, i.second);
303 }
304 gcode->stream->printf("\n");
305 }
306 }
307
308 } else if( ( gcode->m == this->set_m_code || gcode->m == this->set_and_wait_m_code ) && gcode->has_letter('S')) {
309 // this only gets handled if it is not controlled by the tool manager or is active in the toolmanager
310 this->active = true;
311
312 // this is safe as old configs as well as single extruder configs the toolmanager will not be running so will return false
313 // this will also ignore anything that the tool manager is not controlling and return false, otherwise it returns the active tool
314 void *returned_data;
315 bool ok = PublicData::get_value( tool_manager_checksum, is_active_tool_checksum, this->name_checksum, &returned_data );
316 if (ok) {
317 uint16_t active_tool_name = *static_cast<uint16_t *>(returned_data);
318 this->active = (active_tool_name == this->name_checksum);
319 }
320
321 if(this->active) {
322 // required so temp change happens in order
323 THEKERNEL->conveyor->wait_for_empty_queue();
324
325 float v = gcode->get_value('S');
326
327 if (v == 0.0) {
328 this->target_temperature = UNDEFINED;
329 this->heater_pin.set((this->o = 0));
330 } else {
331 this->set_desired_temperature(v);
332 // wait for temp to be reached, no more gcodes will be fetched until this is complete
333 if( gcode->m == this->set_and_wait_m_code) {
334 if(isinf(get_temperature()) && isinf(sensor->get_temperature())) {
335 THEKERNEL->streams->printf("Temperature reading is unreliable on %s HALT asserted - reset or M999 required\n", designator.c_str());
336 THEKERNEL->call_event(ON_HALT, nullptr);
337 return;
338 }
339
340 this->waiting = true; // on_second_tick will announce temps
341 while ( get_temperature() < target_temperature ) {
342 THEKERNEL->call_event(ON_IDLE, this);
343 // check if ON_HALT was called (usually by kill button)
344 if(THEKERNEL->is_halted() || this->target_temperature == UNDEFINED) {
345 THEKERNEL->streams->printf("Wait on temperature aborted by kill\n");
346 break;
347 }
348 }
349 this->waiting = false;
350 }
351 }
352 }
353 }
354 }
355 }
356
357 void TemperatureControl::on_get_public_data(void *argument)
358 {
359 PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument);
360
361 if(!pdr->starts_with(temperature_control_checksum)) return;
362
363 if(pdr->second_element_is(pool_index_checksum)) {
364 // asking for our instance pointer if we have this pool_index
365 if(pdr->third_element_is(this->pool_index)) {
366 static void *return_data;
367 return_data = this;
368 pdr->set_data_ptr(&return_data);
369 pdr->set_taken();
370 }
371
372 }else if(pdr->second_element_is(poll_controls_checksum)) {
373 // polling for all temperature controls
374 // add our data to the list which is passed in via the data_ptr
375
376 std::vector<struct pad_temperature> *v= static_cast<std::vector<pad_temperature>*>(pdr->get_data_ptr());
377
378 struct pad_temperature t;
379 // setup data
380 t.current_temperature = this->get_temperature();
381 t.target_temperature = (target_temperature <= 0) ? 0 : this->target_temperature;
382 t.pwm = this->o;
383 t.designator= this->designator;
384 t.id= this->name_checksum;
385 v->push_back(t);
386 pdr->set_taken();
387
388 }else if(pdr->second_element_is(current_temperature_checksum)) {
389 // if targeted at us
390 if(pdr->third_element_is(this->name_checksum)) {
391 // ok this is targeted at us, so set the requ3sted data in the pointer passed into us
392 struct pad_temperature *t= static_cast<pad_temperature*>(pdr->get_data_ptr());
393 t->current_temperature = this->get_temperature();
394 t->target_temperature = (target_temperature <= 0) ? 0 : this->target_temperature;
395 t->pwm = this->o;
396 t->designator= this->designator;
397 t->id= this->name_checksum;
398 pdr->set_taken();
399 }
400 }
401
402 }
403
404 void TemperatureControl::on_set_public_data(void *argument)
405 {
406 PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument);
407
408 if(!pdr->starts_with(temperature_control_checksum)) return;
409
410 if(!pdr->second_element_is(this->name_checksum)) return;
411
412 // ok this is targeted at us, so set the temp
413 // NOTE unlike the M code this will set the temp now not when the queue is empty
414 float t = *static_cast<float *>(pdr->get_data_ptr());
415 this->set_desired_temperature(t);
416 pdr->set_taken();
417 }
418
419 void TemperatureControl::set_desired_temperature(float desired_temperature)
420 {
421 // Never go over the configured max temperature
422 if( desired_temperature > this->max_temp ){
423 desired_temperature = this->max_temp;
424 }
425
426 if (desired_temperature == 1.0F)
427 desired_temperature = preset1;
428 else if (desired_temperature == 2.0F)
429 desired_temperature = preset2;
430
431 float last_target_temperature= target_temperature;
432 target_temperature = desired_temperature;
433 if (desired_temperature <= 0.0F){
434 // turning it off
435 heater_pin.set((this->o = 0));
436
437 }else if(last_target_temperature <= 0.0F) {
438 // if it was off and we are now turning it on we need to initialize
439 this->lastInput= last_reading;
440 // set to whatever the output currently is See http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-initialization/
441 this->iTerm= this->o;
442 if (this->iTerm > this->i_max) this->iTerm = this->i_max;
443 else if (this->iTerm < 0.0) this->iTerm = 0.0;
444 }
445 }
446
447 float TemperatureControl::get_temperature()
448 {
449 return last_reading;
450 }
451
452 uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy)
453 {
454 float temperature = sensor->get_temperature();
455 if(!this->readonly && target_temperature > 2) {
456 if (isinf(temperature) || temperature < min_temp || temperature > max_temp) {
457 this->temp_violated = true;
458 target_temperature = UNDEFINED;
459 heater_pin.set((this->o = 0));
460 } else {
461 pid_process(temperature);
462 }
463 }
464
465 last_reading = temperature;
466 return 0;
467 }
468
469 /**
470 * Based on https://github.com/br3ttb/Arduino-PID-Library
471 */
472 void TemperatureControl::pid_process(float temperature)
473 {
474 if(use_bangbang) {
475 // bang bang is very simple, if temp is < target - hysteresis turn on full else if temp is > target + hysteresis turn heater off
476 // good for relays
477 if(temperature > (target_temperature + hysteresis) && this->o > 0) {
478 heater_pin.set(false);
479 this->o = 0; // for display purposes only
480
481 } else if(temperature < (target_temperature - hysteresis) && this->o <= 0) {
482 if(heater_pin.max_pwm() >= 255) {
483 // turn on full
484 this->heater_pin.set(true);
485 this->o = 255; // for display purposes only
486 } else {
487 // only to whatever max pwm is configured
488 this->heater_pin.pwm(heater_pin.max_pwm());
489 this->o = heater_pin.max_pwm(); // for display purposes only
490 }
491 }
492 return;
493 }
494
495 // regular PID control
496 float error = target_temperature - temperature;
497
498 float new_I = this->iTerm + (error * this->i_factor);
499 if (new_I > this->i_max) new_I = this->i_max;
500 else if (new_I < 0.0) new_I = 0.0;
501 if(!this->windup) this->iTerm= new_I;
502
503 float d = (temperature - this->lastInput);
504
505 // calculate the PID output
506 // TODO does this need to be scaled by max_pwm/256? I think not as p_factor already does that
507 this->o = (this->p_factor * error) + new_I - (this->d_factor * d);
508
509 if (this->o >= heater_pin.max_pwm())
510 this->o = heater_pin.max_pwm();
511 else if (this->o < 0)
512 this->o = 0;
513 else if(this->windup)
514 this->iTerm = new_I; // Only update I term when output is not saturated.
515
516 this->heater_pin.pwm(this->o);
517 this->lastInput = temperature;
518 }
519
520 void TemperatureControl::on_second_tick(void *argument)
521 {
522
523 // If waiting for a temperature to be reach, display it to keep host programs up to date on the progress
524 if (waiting)
525 THEKERNEL->streams->printf("%s:%3.1f /%3.1f @%d\n", designator.c_str(), get_temperature(), ((target_temperature <= 0) ? 0.0 : target_temperature), o);
526
527 // Check whether or not there is a temperature runaway issue, if so stop everything and report it
528 if(THEKERNEL->is_halted()) return;
529
530 if( this->target_temperature <= 0 ){ // If we are not trying to heat, state is NOT_HEATING
531 this->runaway_state = NOT_HEATING;
532 this->runaway_timer = 0;
533 }else{
534 switch( this->runaway_state ){
535 case NOT_HEATING: // If we were previously not trying to heat, but we are now, change to state WAITING_FOR_TEMP_TO_BE_REACHED
536 if( this->target_temperature > 0 ){
537 this->runaway_state = WAITING_FOR_TEMP_TO_BE_REACHED;
538 this->runaway_heating_timer = 0;
539 }
540 break;
541 case WAITING_FOR_TEMP_TO_BE_REACHED: // In we are in state 1 ( waiting for temperature to be reached ), and the temperature has been reached, change to state TARGET_TEMPERATURE_REACHED
542 if( this->get_temperature() >= this->target_temperature ){
543 this->runaway_state = TARGET_TEMPERATURE_REACHED;
544 }
545 this->runaway_heating_timer++;
546 if( this->runaway_heating_timer > this->runaway_heating_timeout && this->runaway_heating_timeout != 0 ){
547 this->runaway_heating_timer = 0;
548 THEKERNEL->streams->printf("Error : Temperature too long to be reached on %s, HALT asserted, TURN POWER OFF IMMEDIATELY - reset or M999 required\n", designator.c_str());
549 THEKERNEL->call_event(ON_HALT, nullptr);
550 }
551 break;
552 case TARGET_TEMPERATURE_REACHED: // If we are in state TARGET_TEMPERATURE_REACHED, check for thermal runaway
553 // If the temperature is outside the acceptable range
554 if( fabs( this->get_temperature() - this->target_temperature ) > this->runaway_range && this->runaway_range != 0 ){
555 // Increase the timer, aka « One more second with a problem maybe occuring »
556 this->runaway_timer++;
557
558 // If the timer has a too large value ( we have been too long outside the desired temperature range )
559 if( this->runaway_timer > this->runaway_timeout && this->runaway_timeout != 0 ){
560 THEKERNEL->streams->printf("Error : Temperature runaway on %s, HALT asserted, TURN POWER OFF IMMEDIATELY - reset or M999 required\n", designator.c_str());
561 THEKERNEL->call_event(ON_HALT, nullptr);
562 }
563 }else{
564 // The temperature was inside the acceptable range, reset the timer
565 this->runaway_timer = 0;
566 }
567 break;
568 }
569 }
570 }
571
572 void TemperatureControl::setPIDp(float p)
573 {
574 this->p_factor = p;
575 }
576
577 void TemperatureControl::setPIDi(float i)
578 {
579 this->i_factor = i * this->PIDdt;
580 }
581
582 void TemperatureControl::setPIDd(float d)
583 {
584 this->d_factor = d / this->PIDdt;
585 }