temperaturecontrol: allow setting background tool without activating
[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 #include "libs/Module.h"
9 #include "libs/Kernel.h"
10 #include <math.h>
11 #include "TemperatureControl.h"
12 #include "TemperatureControlPool.h"
13 #include "libs/Pin.h"
14 #include "modules/robot/Conveyor.h"
15 #include "PublicDataRequest.h"
16
17 #include "PublicData.h"
18 #include "ToolManagerPublicAccess.h"
19 #include "StreamOutputPool.h"
20 #include "Config.h"
21 #include "checksumm.h"
22 #include "Gcode.h"
23 #include "SlowTicker.h"
24 #include "ConfigValue.h"
25 #include "PID_Autotuner.h"
26 #include "SerialMessage.h"
27 #include "utils.h"
28
29 // Temp sensor implementations:
30 #include "Thermistor.h"
31 #include "max31855.h"
32 #include "AD8495.h"
33 #include "PT100_E3D.h"
34
35 #include "MRI_Hooks.h"
36
37 #define UNDEFINED -1
38
39 #define sensor_checksum CHECKSUM("sensor")
40
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")
49
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")
53
54 #define designator_checksum CHECKSUM("designator")
55
56 #define p_factor_checksum CHECKSUM("p_factor")
57 #define i_factor_checksum CHECKSUM("i_factor")
58 #define d_factor_checksum CHECKSUM("d_factor")
59
60 #define i_max_checksum CHECKSUM("i_max")
61 #define windup_checksum CHECKSUM("windup")
62
63 #define preset1_checksum CHECKSUM("preset1")
64 #define preset2_checksum CHECKSUM("preset2")
65
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")
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 tick= 0;
80 }
81
82 TemperatureControl::~TemperatureControl()
83 {
84 delete sensor;
85 }
86
87 void TemperatureControl::on_module_loaded()
88 {
89
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
93
94 // Settings
95 this->load_config();
96
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);
101
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);
107 }
108 }
109
110 void TemperatureControl::on_halt(void *arg)
111 {
112 if(arg == nullptr) {
113 // turn off heater
114 this->o = 0;
115 this->heater_pin.set(0);
116 this->target_temperature = UNDEFINED;
117 }
118 }
119
120 void TemperatureControl::on_idle(void *arg)
121 {
122 sensor->on_idle();
123 }
124
125
126 void TemperatureControl::on_main_loop(void *argument)
127 {
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);
133 }
134 }
135
136 // Get configuration from the config file
137 void TemperatureControl::load_config()
138 {
139
140 // General 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();
145
146 this->designator = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, designator_checksum)->by_default(string("T"))->as_string();
147
148 // Runaway parameters
149 uint32_t n= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_range_checksum)->by_default(20)->as_number();
150 if(n > 63) n= 63;
151 this->runaway_range= n;
152
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;
160
161 this->runaway_error_range= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_error_range_checksum)->by_default(1.0F)->as_number();
162
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();
166
167 // Heater pin
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();
172
173 } else {
174 this->readonly= true;
175 }
176
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();
179
180 // Instantiate correct sensor (TBD: TempSensor factory?)
181 delete sensor;
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();
191 } else {
192 sensor = new TempSensor(); // A dummy implementation
193 }
194 sensor->UpdateConfig(temperature_control_checksum, this->name_checksum);
195
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();
198
199
200 // sigma-delta output modulation
201 this->o = 0;
202
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);
213 }
214
215
216 // reading tick
217 THEKERNEL->slow_ticker->attach( this->readings_per_second, this, &TemperatureControl::thermistor_read_tick );
218 this->PIDdt = 1.0 / this->readings_per_second;
219
220 // PID
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() );
224
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();
228 }
229
230 this->iTerm = 0.0;
231 this->lastInput = -1.0;
232 this->last_reading = 0.0;
233 }
234
235 void TemperatureControl::on_gcode_received(void *argument)
236 {
237 Gcode *gcode = static_cast<Gcode *>(argument);
238 if (gcode->has_m) {
239
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);
244 return;
245 }
246
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;
255 }else{
256 gcode->stream->printf("Unable to properly set sensor settings, make sure you specify all required values\n");
257 }
258 }else{
259 // don't override
260 this->sensor_settings= false;
261 }
262
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");
265 sensor->get_raw();
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);
271 }
272 }
273 }
274
275 return;
276 }
277
278 // readonly sensors don't handle the rest
279 if(this->readonly) return;
280
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');
285
286 } else {
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");
288 }
289
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);
292 }
293
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'));
306
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);
309 }
310
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());
313
314 gcode->stream->printf(";Max temperature setting:\nM143 S%d P%1.4f\n", this->pool_index, this->max_temp);
315
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);
323 }
324 gcode->stream->printf("\n");
325 }
326 }
327
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;
332
333 if (tool == -1) {
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
336 void *returned_data;
337 bool ok = PublicData::get_value( tool_manager_checksum, is_active_tool_checksum, this->name_checksum, &returned_data );
338 if (ok) {
339 uint16_t active_tool_name = *static_cast<uint16_t *>(returned_data);
340 am_tool = (active_tool_name == this->name_checksum);
341 }
342 } else {
343 // FIXME
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
347 // tool...
348 am_tool = (tool == this->pool_index);
349 }
350
351 if(am_tool) {
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();
356 }
357
358 float v = gcode->get_value('S');
359
360 if (v == 0.0) {
361 this->target_temperature = UNDEFINED;
362 this->heater_pin.set((this->o = 0));
363 } else {
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);
370 return;
371 }
372
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");
379 break;
380 }
381 }
382 this->waiting = false;
383 }
384 }
385 }
386 }
387 }
388 }
389
390 void TemperatureControl::on_get_public_data(void *argument)
391 {
392 PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument);
393
394 if(!pdr->starts_with(temperature_control_checksum)) return;
395
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;
400 return_data = this;
401 pdr->set_data_ptr(&return_data);
402 pdr->set_taken();
403 }
404
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
408
409 std::vector<struct pad_temperature> *v= static_cast<std::vector<pad_temperature>*>(pdr->get_data_ptr());
410
411 struct pad_temperature t;
412 // setup data
413 t.current_temperature = this->get_temperature();
414 t.target_temperature = (target_temperature <= 0) ? 0 : this->target_temperature;
415 t.pwm = this->o;
416 t.designator= this->designator;
417 t.id= this->name_checksum;
418 v->push_back(t);
419 pdr->set_taken();
420
421 }else if(pdr->second_element_is(current_temperature_checksum)) {
422 // if targeted at us
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;
428 t->pwm = this->o;
429 t->designator= this->designator;
430 t->id= this->name_checksum;
431 pdr->set_taken();
432 }
433 }
434
435 }
436
437 void TemperatureControl::on_set_public_data(void *argument)
438 {
439 PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument);
440
441 if(!pdr->starts_with(temperature_control_checksum)) return;
442
443 if(!pdr->second_element_is(this->name_checksum)) return;
444
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);
449 pdr->set_taken();
450 }
451
452 void TemperatureControl::set_desired_temperature(float desired_temperature)
453 {
454 // Never go over the configured max temperature
455 if( desired_temperature > this->max_temp ){
456 desired_temperature = this->max_temp;
457 }
458
459 if (desired_temperature == 1.0F)
460 desired_temperature = preset1;
461 else if (desired_temperature == 2.0F)
462 desired_temperature = preset2;
463
464 float last_target_temperature= target_temperature;
465 target_temperature = desired_temperature;
466 if (desired_temperature <= 0.0F){
467 // turning it off
468 heater_pin.set((this->o = 0));
469
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;
477 }
478
479 // reset the runaway state, even if it was a temp change
480 this->runaway_state = NOT_HEATING;
481 }
482
483 float TemperatureControl::get_temperature()
484 {
485 return last_reading;
486 }
487
488 uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy)
489 {
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));
496 } else {
497 pid_process(temperature);
498 }
499 }
500
501 last_reading = temperature;
502 return 0;
503 }
504
505 /**
506 * Based on https://github.com/br3ttb/Arduino-PID-Library
507 */
508 void TemperatureControl::pid_process(float temperature)
509 {
510 if(use_bangbang) {
511 // bang bang is very simple, if temp is < target - hysteresis turn on full else if temp is > target + hysteresis turn heater off
512 // good for relays
513 if(temperature > (target_temperature + hysteresis) && this->o > 0) {
514 heater_pin.set(false);
515 this->o = 0; // for display purposes only
516
517 } else if(temperature < (target_temperature - hysteresis) && this->o <= 0) {
518 if(heater_pin.max_pwm() >= 255) {
519 // turn on full
520 this->heater_pin.set(true);
521 this->o = 255; // for display purposes only
522 } else {
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
526 }
527 }
528 return;
529 }
530
531 // regular PID control
532 float error = target_temperature - temperature;
533
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;
538
539 float d = (temperature - this->lastInput);
540
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);
544
545 if (this->o >= heater_pin.max_pwm())
546 this->o = heater_pin.max_pwm();
547 else if (this->o < 0)
548 this->o = 0;
549 else if(this->windup)
550 this->iTerm = new_I; // Only update I term when output is not saturated.
551
552 this->heater_pin.pwm(this->o);
553 this->lastInput = temperature;
554 }
555
556 void TemperatureControl::on_second_tick(void *argument)
557 {
558
559 // If waiting for a temperature to be reach, display it to keep host programs up to date on the progress
560 if (waiting)
561 THEKERNEL->streams->printf("%s:%3.1f /%3.1f @%d\n", designator.c_str(), get_temperature(), ((target_temperature <= 0) ? 0.0 : target_temperature), o);
562
563 // Check whether or not there is a temperature runaway issue, if so stop everything and report it
564 if(THEKERNEL->is_halted()) return;
565
566 // see if runaway detection is enabled
567 if(this->runaway_heating_timeout == 0 && this->runaway_range == 0) return;
568
569 // check every 8 seconds, depends on tick being 3 bits
570 if(++tick != 0) return;
571
572 if(this->target_temperature <= 0){ // If we are not trying to heat, state is NOT_HEATING
573 this->runaway_state = NOT_HEATING;
574
575 }else{
576 float current_temperature= this->get_temperature();
577 // heater is active
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;
582 tick= 0;
583 break;
584
585 case HEATING_UP:
586 case COOLING_DOWN:
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;
592 tick= 0;
593
594 }else{
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;
602 }
603 }
604 break;
605
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;
610
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;
618 }
619
620 }else{
621 this->runaway_timer= 0;
622 }
623 }
624
625 break;
626 }
627 }
628 }
629
630 void TemperatureControl::setPIDp(float p)
631 {
632 this->p_factor = p;
633 }
634
635 void TemperatureControl::setPIDi(float i)
636 {
637 this->i_factor = i * this->PIDdt;
638 }
639
640 void TemperatureControl::setPIDd(float d)
641 {
642 this->d_factor = d / this->PIDdt;
643 }