Commit | Line | Data |
---|---|---|
df27a6a3 | 1 | /* |
cd011f58 AW |
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. | |
df27a6a3 | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
cd011f58 AW |
6 | */ |
7 | ||
ded56b35 AW |
8 | #include "libs/Module.h" |
9 | #include "libs/Kernel.h" | |
10 | #include <math.h> | |
11 | #include "TemperatureControl.h" | |
3c308aeb | 12 | #include "TemperatureControlPool.h" |
3c132bd0 | 13 | #include "libs/Pin.h" |
3c4f2dd8 | 14 | #include "modules/robot/Conveyor.h" |
8293d443 | 15 | #include "PublicDataRequest.h" |
2fa50ca0 | 16 | |
38b9f24a L |
17 | #include "PublicData.h" |
18 | #include "ToolManagerPublicAccess.h" | |
61134a65 JM |
19 | #include "StreamOutputPool.h" |
20 | #include "Config.h" | |
21 | #include "checksumm.h" | |
22 | #include "Gcode.h" | |
61134a65 | 23 | #include "SlowTicker.h" |
8d54c34c | 24 | #include "ConfigValue.h" |
66383b80 | 25 | #include "PID_Autotuner.h" |
1f8dab1a JM |
26 | #include "SerialMessage.h" |
27 | #include "utils.h" | |
ded56b35 | 28 | |
9d955060 | 29 | // Temp sensor implementations: |
30 | #include "Thermistor.h" | |
4710532a | 31 | #include "max31855.h" |
073d88ec | 32 | #include "AD8495.h" |
3b355b1c | 33 | #include "PT100_E3D.h" |
9d955060 | 34 | |
8f91e4e6 MM |
35 | #include "MRI_Hooks.h" |
36 | ||
85eabc50 JM |
37 | #define UNDEFINED -1 |
38 | ||
9d955060 | 39 | #define sensor_checksum CHECKSUM("sensor") |
40 | ||
85eabc50 JM |
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") | |
989d0e94 JM |
44 | #define bang_bang_checksum CHECKSUM("bang_bang") |
45 | #define hysteresis_checksum CHECKSUM("hysteresis") | |
85eabc50 | 46 | #define heater_pin_checksum CHECKSUM("heater_pin") |
bb02929e | 47 | #define max_temp_checksum CHECKSUM("max_temp") |
3a014cbb | 48 | #define min_temp_checksum CHECKSUM("min_temp") |
85eabc50 JM |
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") | |
08f89868 | 61 | #define windup_checksum CHECKSUM("windup") |
85eabc50 JM |
62 | |
63 | #define preset1_checksum CHECKSUM("preset1") | |
64 | #define preset2_checksum CHECKSUM("preset2") | |
65 | ||
1cd1bf1a | 66 | #define runaway_range_checksum CHECKSUM("runaway_range") |
1cd1bf1a | 67 | #define runaway_heating_timeout_checksum CHECKSUM("runaway_heating_timeout") |
4f97f8b8 | 68 | #define runaway_cooling_timeout_checksum CHECKSUM("runaway_cooling_timeout") |
6682b332 | 69 | #define runaway_error_range_checksum CHECKSUM("runaway_error_range") |
1cd1bf1a | 70 | |
8e8b938e | 71 | TemperatureControl::TemperatureControl(uint16_t name, int index) |
d8baddd9 | 72 | { |
8e8b938e JM |
73 | name_checksum= name; |
74 | pool_index= index; | |
75 | waiting= false; | |
01004e36 | 76 | temp_violated= false; |
8e8b938e | 77 | sensor= nullptr; |
71cc73eb | 78 | readonly= false; |
4f97f8b8 | 79 | tick= 0; |
d8baddd9 | 80 | } |
ded56b35 | 81 | |
d8baddd9 | 82 | TemperatureControl::~TemperatureControl() |
83 | { | |
84 | delete sensor; | |
85 | } | |
4710532a JM |
86 | |
87 | void TemperatureControl::on_module_loaded() | |
88 | { | |
907d5e8a | 89 | |
81b547a1 | 90 | // We start not desiring any temp |
907d5e8a | 91 | this->target_temperature = UNDEFINED; |
f1e38d95 | 92 | this->sensor_settings= false; // set to true if sensor settings have been overriden |
ded56b35 AW |
93 | |
94 | // Settings | |
71cc73eb | 95 | this->load_config(); |
ded56b35 | 96 | |
ded56b35 | 97 | // Register for events |
b0be67b5 | 98 | this->register_for_event(ON_GCODE_RECEIVED); |
8293d443 | 99 | this->register_for_event(ON_GET_PUBLIC_DATA); |
71cc73eb JM |
100 | |
101 | if(!this->readonly) { | |
71cc73eb JM |
102 | this->register_for_event(ON_SECOND_TICK); |
103 | this->register_for_event(ON_MAIN_LOOP); | |
104 | this->register_for_event(ON_SET_PUBLIC_DATA); | |
105 | this->register_for_event(ON_HALT); | |
106 | } | |
3d1a4519 JM |
107 | } |
108 | ||
109 | void TemperatureControl::on_halt(void *arg) | |
110 | { | |
728477c4 JM |
111 | if(arg == nullptr) { |
112 | // turn off heater | |
113 | this->o = 0; | |
114 | this->heater_pin.set(0); | |
115 | this->target_temperature = UNDEFINED; | |
116 | } | |
ded56b35 AW |
117 | } |
118 | ||
4710532a JM |
119 | void TemperatureControl::on_main_loop(void *argument) |
120 | { | |
01004e36 JM |
121 | if (this->temp_violated) { |
122 | this->temp_violated = false; | |
f46f7916 | 123 | THEKERNEL->streams->printf("ERROR: MINTEMP or MAXTEMP triggered on %s. Check your temperature sensors!\n", designator.c_str()); |
01004e36 JM |
124 | THEKERNEL->streams->printf("HALT asserted - reset or M999 required\n"); |
125 | THEKERNEL->call_event(ON_HALT, nullptr); | |
a7f12bed | 126 | } |
7e57bc2c | 127 | } |
7dd8133c AW |
128 | |
129 | // Get configuration from the config file | |
71cc73eb | 130 | void TemperatureControl::load_config() |
4710532a | 131 | { |
7dd8133c | 132 | |
1306ba99 | 133 | // General config |
314ab8f7 MM |
134 | this->set_m_code = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, set_m_code_checksum)->by_default(104)->as_number(); |
135 | 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(); | |
136 | this->get_m_code = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, get_m_code_checksum)->by_default(105)->as_number(); | |
137 | this->readings_per_second = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, readings_per_second_checksum)->by_default(20)->as_number(); | |
7dee00e4 | 138 | |
314ab8f7 | 139 | this->designator = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, designator_checksum)->by_default(string("T"))->as_string(); |
b0be67b5 | 140 | |
1cd1bf1a | 141 | // Runaway parameters |
6c8b8ca0 | 142 | uint32_t n= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_range_checksum)->by_default(20)->as_number(); |
5cc0bc2c JM |
143 | if(n > 63) n= 63; |
144 | this->runaway_range= n; | |
4f97f8b8 | 145 | |
8123c72d | 146 | // these need to fit in 9 bits after dividing by 8 so max is 4088 secs or 68 minutes |
6c8b8ca0 | 147 | n= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_heating_timeout_checksum)->by_default(900)->as_number(); |
8123c72d | 148 | if(n > 4088) n= 4088; |
4f97f8b8 | 149 | this->runaway_heating_timeout = n/8; // we have 8 second ticks |
495afe33 | 150 | n= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_cooling_timeout_checksum)->by_default(0)->as_number(); // disable by default |
8123c72d | 151 | if(n > 4088) n= 4088; |
4f97f8b8 | 152 | this->runaway_cooling_timeout = n/8; |
1cd1bf1a | 153 | |
6682b332 JM |
154 | this->runaway_error_range= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_error_range_checksum)->by_default(1.0F)->as_number(); |
155 | ||
3a014cbb | 156 | // Max and min temperatures we are not allowed to get over (Safety) |
01004e36 | 157 | this->max_temp = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, max_temp_checksum)->by_default(300)->as_number(); |
3a014cbb | 158 | this->min_temp = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, min_temp_checksum)->by_default(0)->as_number(); |
7d4baeee | 159 | |
71cc73eb JM |
160 | // Heater pin |
161 | this->heater_pin.from_string( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, heater_pin_checksum)->by_default("nc")->as_string()); | |
162 | if(this->heater_pin.connected()){ | |
163 | this->readonly= false; | |
164 | this->heater_pin.as_output(); | |
165 | ||
166 | } else { | |
167 | this->readonly= true; | |
168 | } | |
169 | ||
d8baddd9 | 170 | // For backward compatibility, default to a thermistor sensor. |
171 | std::string sensor_type = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, sensor_checksum)->by_default("thermistor")->as_string(); | |
172 | ||
173 | // Instantiate correct sensor (TBD: TempSensor factory?) | |
174 | delete sensor; | |
175 | sensor = nullptr; // In case we fail to create a new sensor. | |
4710532a | 176 | if(sensor_type.compare("thermistor") == 0) { |
d8baddd9 | 177 | sensor = new Thermistor(); |
4710532a | 178 | } else if(sensor_type.compare("max31855") == 0) { |
d8baddd9 | 179 | sensor = new Max31855(); |
073d88ec E |
180 | } else if(sensor_type.compare("ad8495") == 0) { |
181 | sensor = new AD8495(); | |
3b355b1c BM |
182 | } else if(sensor_type.compare("pt100_e3d") == 0) { |
183 | sensor = new PT100_E3D(); | |
4710532a | 184 | } else { |
d8baddd9 | 185 | sensor = new TempSensor(); // A dummy implementation |
186 | } | |
187 | sensor->UpdateConfig(temperature_control_checksum, this->name_checksum); | |
4710532a | 188 | |
71cc73eb JM |
189 | this->preset1 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, preset1_checksum)->by_default(0)->as_number(); |
190 | this->preset2 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, preset2_checksum)->by_default(0)->as_number(); | |
f4bd4fc3 | 191 | |
907d5e8a | 192 | |
c4f4cf73 | 193 | // sigma-delta output modulation |
b35ac71e | 194 | this->o = 0; |
3c132bd0 | 195 | |
71cc73eb JM |
196 | if(!this->readonly) { |
197 | // used to enable bang bang control of heater | |
198 | this->use_bangbang = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, bang_bang_checksum)->by_default(false)->as_bool(); | |
199 | this->hysteresis = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, hysteresis_checksum)->by_default(2)->as_number(); | |
08f89868 | 200 | this->windup = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, windup_checksum)->by_default(false)->as_bool(); |
71cc73eb JM |
201 | this->heater_pin.max_pwm( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, max_pwm_checksum)->by_default(255)->as_number() ); |
202 | this->heater_pin.set(0); | |
203 | set_low_on_debug(heater_pin.port_number, heater_pin.pin); | |
204 | // activate SD-DAC timer | |
205 | 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); | |
206 | } | |
8f91e4e6 | 207 | |
907d5e8a | 208 | |
f39da6fe | 209 | // reading tick |
314ab8f7 | 210 | THEKERNEL->slow_ticker->attach( this->readings_per_second, this, &TemperatureControl::thermistor_read_tick ); |
4710532a | 211 | this->PIDdt = 1.0 / this->readings_per_second; |
f39da6fe | 212 | |
907d5e8a | 213 | // PID |
314ab8f7 | 214 | setPIDp( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, p_factor_checksum)->by_default(10 )->as_number() ); |
1ad23cd3 | 215 | setPIDi( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, i_factor_checksum)->by_default(0.3f)->as_number() ); |
314ab8f7 | 216 | setPIDd( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, d_factor_checksum)->by_default(200)->as_number() ); |
71cc73eb JM |
217 | |
218 | if(!this->readonly) { | |
219 | // set to the same as max_pwm by default | |
220 | this->i_max = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, i_max_checksum )->by_default(this->heater_pin.max_pwm())->as_number(); | |
221 | } | |
222 | ||
10e66797 | 223 | this->iTerm = 0.0; |
4710532a | 224 | this->lastInput = -1.0; |
907d5e8a | 225 | this->last_reading = 0.0; |
7dd8133c AW |
226 | } |
227 | ||
4710532a JM |
228 | void TemperatureControl::on_gcode_received(void *argument) |
229 | { | |
230 | Gcode *gcode = static_cast<Gcode *>(argument); | |
8dfbc976 | 231 | if (gcode->has_m) { |
ee4711d1 | 232 | |
4710532a | 233 | if( gcode->m == this->get_m_code ) { |
93284f6f | 234 | char buf[32]; // should be big enough for any status |
3517edce | 235 | 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); |
93284f6f | 236 | gcode->txt_after_ok.append(buf, n); |
71cc73eb JM |
237 | return; |
238 | } | |
8dfbc976 | 239 | |
76f53dc6 | 240 | if (gcode->m == 305) { // set or get sensor settings |
76f53dc6 | 241 | if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) { |
1f8dab1a | 242 | TempSensor::sensor_options_t args= gcode->get_args(); |
d22755f7 JM |
243 | args.erase('S'); // don't include the S |
244 | if(args.size() > 0) { | |
76f53dc6 | 245 | // set the new options |
d22755f7 JM |
246 | if(sensor->set_optional(args)) { |
247 | this->sensor_settings= true; | |
248 | }else{ | |
249 | gcode->stream->printf("Unable to properly set sensor settings, make sure you specify all required values\n"); | |
250 | } | |
251 | }else{ | |
252 | // don't override | |
253 | this->sensor_settings= false; | |
76f53dc6 JM |
254 | } |
255 | ||
256 | }else if(!gcode->has_letter('S')) { | |
d22755f7 | 257 | gcode->stream->printf("%s(S%d): using %s\n", this->designator.c_str(), this->pool_index, this->readonly?"Readonly" : this->use_bangbang?"Bangbang":"PID"); |
76f53dc6 JM |
258 | sensor->get_raw(); |
259 | TempSensor::sensor_options_t options; | |
260 | if(sensor->get_optional(options)) { | |
261 | for(auto &i : options) { | |
262 | // foreach optional value | |
a2e5f877 | 263 | gcode->stream->printf("%s(S%d): %c %1.18f\n", this->designator.c_str(), this->pool_index, i.first, i.second); |
76f53dc6 JM |
264 | } |
265 | } | |
266 | } | |
267 | ||
268 | return; | |
269 | } | |
270 | ||
71cc73eb JM |
271 | // readonly sensors don't handle the rest |
272 | if(this->readonly) return; | |
273 | ||
01004e36 JM |
274 | if (gcode->m == 143) { |
275 | if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) { | |
276 | if(gcode->has_letter('P')) { | |
277 | max_temp= gcode->get_value('P'); | |
278 | ||
279 | } else { | |
280 | 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"); | |
281 | } | |
282 | ||
283 | }else if(gcode->get_num_args() == 0) { | |
284 | gcode->stream->printf("Maximum temperature for %s(%d) is %f°C\n", this->designator.c_str(), this->pool_index, max_temp); | |
285 | } | |
286 | ||
287 | } else if (gcode->m == 301) { | |
4710532a | 288 | if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) { |
827a49ca | 289 | if (gcode->has_letter('P')) |
201e6dcf | 290 | setPIDp( gcode->get_value('P') ); |
827a49ca | 291 | if (gcode->has_letter('I')) |
201e6dcf | 292 | setPIDi( gcode->get_value('I') ); |
827a49ca | 293 | if (gcode->has_letter('D')) |
201e6dcf | 294 | setPIDd( gcode->get_value('D') ); |
827a49ca | 295 | if (gcode->has_letter('X')) |
f1e38d95 JM |
296 | this->i_max = gcode->get_value('X'); |
297 | if (gcode->has_letter('Y')) | |
298 | this->heater_pin.max_pwm(gcode->get_value('Y')); | |
299 | ||
300 | }else if(!gcode->has_letter('S')) { | |
301 | 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); | |
302 | } | |
303 | ||
4710532a | 304 | } else if (gcode->m == 500 || gcode->m == 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings |
f1e38d95 JM |
305 | 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()); |
306 | ||
01004e36 JM |
307 | gcode->stream->printf(";Max temperature setting:\nM143 S%d P%1.4f\n", this->pool_index, this->max_temp); |
308 | ||
f1e38d95 JM |
309 | if(this->sensor_settings) { |
310 | // get or save any sensor specific optional values | |
311 | TempSensor::sensor_options_t options; | |
312 | if(sensor->get_optional(options) && !options.empty()) { | |
313 | gcode->stream->printf(";Optional temp sensor specific settings:\nM305 S%d", this->pool_index); | |
314 | for(auto &i : options) { | |
7d678d16 | 315 | gcode->stream->printf(" %c%1.18f", i.first, i.second); |
f1e38d95 JM |
316 | } |
317 | gcode->stream->printf("\n"); | |
318 | } | |
319 | } | |
33e4cc02 | 320 | |
8e8b938e | 321 | } else if( ( gcode->m == this->set_m_code || gcode->m == this->set_and_wait_m_code ) && gcode->has_letter('S')) { |
42cb1b30 | 322 | // this only gets handled if it is not controlled by the tool manager or is active in the toolmanager |
8e8b938e JM |
323 | this->active = true; |
324 | ||
325 | // this is safe as old configs as well as single extruder configs the toolmanager will not be running so will return false | |
326 | // this will also ignore anything that the tool manager is not controlling and return false, otherwise it returns the active tool | |
327 | void *returned_data; | |
75e6428d | 328 | bool ok = PublicData::get_value( tool_manager_checksum, is_active_tool_checksum, this->name_checksum, &returned_data ); |
8e8b938e JM |
329 | if (ok) { |
330 | uint16_t active_tool_name = *static_cast<uint16_t *>(returned_data); | |
331 | this->active = (active_tool_name == this->name_checksum); | |
332 | } | |
333 | ||
334 | if(this->active) { | |
42cb1b30 | 335 | // required so temp change happens in order |
04782655 | 336 | THEKERNEL->conveyor->wait_for_idle(); |
2134bcf2 | 337 | |
42cb1b30 | 338 | float v = gcode->get_value('S'); |
db453125 | 339 | |
42cb1b30 JM |
340 | if (v == 0.0) { |
341 | this->target_temperature = UNDEFINED; | |
342 | this->heater_pin.set((this->o = 0)); | |
343 | } else { | |
344 | this->set_desired_temperature(v); | |
ed52bf31 JM |
345 | // wait for temp to be reached, no more gcodes will be fetched until this is complete |
346 | if( gcode->m == this->set_and_wait_m_code) { | |
487976f7 | 347 | if(isinf(get_temperature()) && isinf(sensor->get_temperature())) { |
f0433db4 | 348 | THEKERNEL->streams->printf("Temperature reading is unreliable on %s HALT asserted - reset or M999 required\n", designator.c_str()); |
487976f7 JM |
349 | THEKERNEL->call_event(ON_HALT, nullptr); |
350 | return; | |
351 | } | |
352 | ||
ed52bf31 JM |
353 | this->waiting = true; // on_second_tick will announce temps |
354 | while ( get_temperature() < target_temperature ) { | |
355 | THEKERNEL->call_event(ON_IDLE, this); | |
798295c1 | 356 | // check if ON_HALT was called (usually by kill button) |
aa896868 JM |
357 | if(THEKERNEL->is_halted() || this->target_temperature == UNDEFINED) { |
358 | THEKERNEL->streams->printf("Wait on temperature aborted by kill\n"); | |
359 | break; | |
360 | } | |
ed52bf31 JM |
361 | } |
362 | this->waiting = false; | |
42cb1b30 | 363 | } |
cf1a7632 | 364 | } |
907d5e8a | 365 | } |
df27a6a3 | 366 | } |
df27a6a3 | 367 | } |
ded56b35 AW |
368 | } |
369 | ||
4710532a JM |
370 | void TemperatureControl::on_get_public_data(void *argument) |
371 | { | |
372 | PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument); | |
201e6dcf | 373 | |
b19aa09d JM |
374 | if(!pdr->starts_with(temperature_control_checksum)) return; |
375 | ||
8e8b938e JM |
376 | if(pdr->second_element_is(pool_index_checksum)) { |
377 | // asking for our instance pointer if we have this pool_index | |
378 | if(pdr->third_element_is(this->pool_index)) { | |
379 | static void *return_data; | |
380 | return_data = this; | |
381 | pdr->set_data_ptr(&return_data); | |
382 | pdr->set_taken(); | |
383 | } | |
8e8b938e | 384 | |
56a6c8c1 | 385 | }else if(pdr->second_element_is(poll_controls_checksum)) { |
bab4e1bd | 386 | // polling for all temperature controls |
3bfb2639 JM |
387 | // add our data to the list which is passed in via the data_ptr |
388 | ||
389 | std::vector<struct pad_temperature> *v= static_cast<std::vector<pad_temperature>*>(pdr->get_data_ptr()); | |
bab4e1bd | 390 | |
3bfb2639 | 391 | struct pad_temperature t; |
bab4e1bd | 392 | // setup data |
3bfb2639 JM |
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 | v->push_back(t); | |
399 | pdr->set_taken(); | |
b19aa09d | 400 | |
56a6c8c1 JM |
401 | }else if(pdr->second_element_is(current_temperature_checksum)) { |
402 | // if targeted at us | |
403 | if(pdr->third_element_is(this->name_checksum)) { | |
564cf1f0 | 404 | // ok this is targeted at us, so set the requ3sted data in the pointer passed into us |
56a6c8c1 JM |
405 | struct pad_temperature *t= static_cast<pad_temperature*>(pdr->get_data_ptr()); |
406 | t->current_temperature = this->get_temperature(); | |
407 | t->target_temperature = (target_temperature <= 0) ? 0 : this->target_temperature; | |
408 | t->pwm = this->o; | |
409 | t->designator= this->designator; | |
410 | t->id= this->name_checksum; | |
411 | pdr->set_taken(); | |
56a6c8c1 | 412 | } |
b19aa09d | 413 | } |
8e8b938e | 414 | |
8293d443 | 415 | } |
db453125 | 416 | |
4710532a JM |
417 | void TemperatureControl::on_set_public_data(void *argument) |
418 | { | |
419 | PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument); | |
77047e76 | 420 | |
991d98cc | 421 | if(!pdr->starts_with(temperature_control_checksum)) return; |
77047e76 | 422 | |
8e8b938e | 423 | if(!pdr->second_element_is(this->name_checksum)) return; |
77047e76 | 424 | |
991d98cc | 425 | // ok this is targeted at us, so set the temp |
ed52bf31 | 426 | // NOTE unlike the M code this will set the temp now not when the queue is empty |
4710532a | 427 | float t = *static_cast<float *>(pdr->get_data_ptr()); |
991d98cc JM |
428 | this->set_desired_temperature(t); |
429 | pdr->set_taken(); | |
77047e76 JM |
430 | } |
431 | ||
1ad23cd3 | 432 | void TemperatureControl::set_desired_temperature(float desired_temperature) |
cf1a7632 | 433 | { |
7d4baeee AW |
434 | // Never go over the configured max temperature |
435 | if( desired_temperature > this->max_temp ){ | |
436 | desired_temperature = this->max_temp; | |
437 | } | |
438 | ||
08f89868 | 439 | if (desired_temperature == 1.0F) |
cf1a7632 | 440 | desired_temperature = preset1; |
08f89868 | 441 | else if (desired_temperature == 2.0F) |
cf1a7632 MM |
442 | desired_temperature = preset2; |
443 | ||
08f89868 | 444 | float last_target_temperature= target_temperature; |
907d5e8a | 445 | target_temperature = desired_temperature; |
3517edce | 446 | if (desired_temperature <= 0.0F){ |
08f89868 | 447 | // turning it off |
b35ac71e | 448 | heater_pin.set((this->o = 0)); |
08f89868 | 449 | |
3517edce | 450 | }else if(last_target_temperature <= 0.0F) { |
08f89868 JM |
451 | // if it was off and we are now turning it on we need to initialize |
452 | this->lastInput= last_reading; | |
453 | // set to whatever the output currently is See http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-initialization/ | |
454 | this->iTerm= this->o; | |
455 | if (this->iTerm > this->i_max) this->iTerm = this->i_max; | |
456 | else if (this->iTerm < 0.0) this->iTerm = 0.0; | |
457 | } | |
fb778302 JM |
458 | |
459 | // reset the runaway state, even if it was a temp change | |
460 | this->runaway_state = NOT_HEATING; | |
ded56b35 AW |
461 | } |
462 | ||
4710532a JM |
463 | float TemperatureControl::get_temperature() |
464 | { | |
907d5e8a | 465 | return last_reading; |
ded56b35 AW |
466 | } |
467 | ||
4710532a JM |
468 | uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy) |
469 | { | |
9d955060 | 470 | float temperature = sensor->get_temperature(); |
3517edce | 471 | if(!this->readonly && target_temperature > 2) { |
01004e36 JM |
472 | if (isinf(temperature) || temperature < min_temp || temperature > max_temp) { |
473 | this->temp_violated = true; | |
907d5e8a | 474 | target_temperature = UNDEFINED; |
4710532a JM |
475 | heater_pin.set((this->o = 0)); |
476 | } else { | |
907d5e8a | 477 | pid_process(temperature); |
ded56b35 | 478 | } |
959dc7db | 479 | } |
3517edce | 480 | |
907d5e8a | 481 | last_reading = temperature; |
281967e4 | 482 | return 0; |
ded56b35 AW |
483 | } |
484 | ||
10e66797 JM |
485 | /** |
486 | * Based on https://github.com/br3ttb/Arduino-PID-Library | |
487 | */ | |
1ad23cd3 | 488 | void TemperatureControl::pid_process(float temperature) |
907d5e8a | 489 | { |
989d0e94 | 490 | if(use_bangbang) { |
b35ac71e | 491 | // bang bang is very simple, if temp is < target - hysteresis turn on full else if temp is > target + hysteresis turn heater off |
989d0e94 | 492 | // good for relays |
4710532a | 493 | if(temperature > (target_temperature + hysteresis) && this->o > 0) { |
989d0e94 | 494 | heater_pin.set(false); |
4710532a | 495 | this->o = 0; // for display purposes only |
989d0e94 | 496 | |
4710532a | 497 | } else if(temperature < (target_temperature - hysteresis) && this->o <= 0) { |
989d0e94 JM |
498 | if(heater_pin.max_pwm() >= 255) { |
499 | // turn on full | |
500 | this->heater_pin.set(true); | |
4710532a JM |
501 | this->o = 255; // for display purposes only |
502 | } else { | |
989d0e94 JM |
503 | // only to whatever max pwm is configured |
504 | this->heater_pin.pwm(heater_pin.max_pwm()); | |
4710532a | 505 | this->o = heater_pin.max_pwm(); // for display purposes only |
989d0e94 | 506 | } |
989d0e94 JM |
507 | } |
508 | return; | |
509 | } | |
ded56b35 | 510 | |
989d0e94 JM |
511 | // regular PID control |
512 | float error = target_temperature - temperature; | |
08f89868 | 513 | |
0d84905d PA |
514 | float new_I = this->iTerm + (error * this->i_factor); |
515 | if (new_I > this->i_max) new_I = this->i_max; | |
516 | else if (new_I < 0.0) new_I = 0.0; | |
08f89868 | 517 | if(!this->windup) this->iTerm= new_I; |
ded56b35 | 518 | |
4710532a | 519 | float d = (temperature - this->lastInput); |
ded56b35 | 520 | |
10e66797 JM |
521 | // calculate the PID output |
522 | // TODO does this need to be scaled by max_pwm/256? I think not as p_factor already does that | |
0d84905d | 523 | this->o = (this->p_factor * error) + new_I - (this->d_factor * d); |
8cdae54c | 524 | |
7dee00e4 | 525 | if (this->o >= heater_pin.max_pwm()) |
7dee00e4 | 526 | this->o = heater_pin.max_pwm(); |
27aecda6 | 527 | else if (this->o < 0) |
907d5e8a | 528 | this->o = 0; |
08f89868 | 529 | else if(this->windup) |
0d84905d | 530 | this->iTerm = new_I; // Only update I term when output is not saturated. |
907d5e8a | 531 | |
27aecda6 | 532 | this->heater_pin.pwm(this->o); |
4710532a | 533 | this->lastInput = temperature; |
907d5e8a | 534 | } |
ded56b35 | 535 | |
4710532a | 536 | void TemperatureControl::on_second_tick(void *argument) |
8ccab7cf | 537 | { |
3bf54d11 AW |
538 | |
539 | // If waiting for a temperature to be reach, display it to keep host programs up to date on the progress | |
8ccab7cf | 540 | if (waiting) |
3517edce | 541 | THEKERNEL->streams->printf("%s:%3.1f /%3.1f @%d\n", designator.c_str(), get_temperature(), ((target_temperature <= 0) ? 0.0 : target_temperature), o); |
3bf54d11 AW |
542 | |
543 | // Check whether or not there is a temperature runaway issue, if so stop everything and report it | |
e39c48c1 | 544 | if(THEKERNEL->is_halted()) return; |
f967a81b | 545 | |
fb778302 JM |
546 | // see if runaway detection is enabled |
547 | if(this->runaway_heating_timeout == 0 && this->runaway_range == 0) return; | |
548 | ||
4f97f8b8 JM |
549 | // check every 8 seconds, depends on tick being 3 bits |
550 | if(++tick != 0) return; | |
551 | ||
fb778302 | 552 | if(this->target_temperature <= 0){ // If we are not trying to heat, state is NOT_HEATING |
c6c03d35 | 553 | this->runaway_state = NOT_HEATING; |
fb778302 | 554 | |
3bf54d11 | 555 | }else{ |
4f97f8b8 | 556 | float current_temperature= this->get_temperature(); |
fb778302 | 557 | // heater is active |
3bf54d11 | 558 | switch( this->runaway_state ){ |
c6c03d35 | 559 | case NOT_HEATING: // If we were previously not trying to heat, but we are now, change to state WAITING_FOR_TEMP_TO_BE_REACHED |
6682b332 | 560 | this->runaway_state= (this->target_temperature >= current_temperature || this->runaway_cooling_timeout == 0) ? HEATING_UP : COOLING_DOWN; |
4f97f8b8 | 561 | this->runaway_timer = 0; |
8123c72d | 562 | tick= 0; |
3bf54d11 | 563 | break; |
fb778302 | 564 | |
4f97f8b8 JM |
565 | case HEATING_UP: |
566 | case COOLING_DOWN: | |
6682b332 JM |
567 | // check temp has reached the target temperature within the given error range |
568 | if( (runaway_state == HEATING_UP && current_temperature >= (this->target_temperature - this->runaway_error_range)) || | |
569 | (runaway_state == COOLING_DOWN && current_temperature <= (this->target_temperature + this->runaway_error_range)) ) { | |
c6c03d35 | 570 | this->runaway_state = TARGET_TEMPERATURE_REACHED; |
4f97f8b8 | 571 | this->runaway_timer = 0; |
8123c72d | 572 | tick= 0; |
fb778302 | 573 | |
4f97f8b8 JM |
574 | }else{ |
575 | uint16_t t= (runaway_state == HEATING_UP) ? this->runaway_heating_timeout : this->runaway_cooling_timeout; | |
fb778302 | 576 | // we are still heating up see if we have hit the max time allowed |
4f97f8b8 | 577 | if(t > 0 && ++this->runaway_timer > t){ |
fb778302 JM |
578 | 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()); |
579 | THEKERNEL->call_event(ON_HALT, nullptr); | |
580 | this->runaway_state = NOT_HEATING; | |
4f97f8b8 | 581 | this->runaway_timer = 0; |
fb778302 | 582 | } |
3bf54d11 AW |
583 | } |
584 | break; | |
fb778302 JM |
585 | |
586 | case TARGET_TEMPERATURE_REACHED: | |
587 | if(this->runaway_range != 0) { | |
588 | // we are in state TARGET_TEMPERATURE_REACHED, check for thermal runaway | |
4f97f8b8 | 589 | float delta= current_temperature - this->target_temperature; |
fb778302 | 590 | |
4f97f8b8 | 591 | // If the temperature is outside the acceptable range for 8 seconds, this allows for some noise spikes without halting |
fb778302 | 592 | if(fabsf(delta) > this->runaway_range){ |
4f97f8b8 JM |
593 | if(this->runaway_timer++ >= 1) { // this being 8 seconds |
594 | 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); | |
595 | THEKERNEL->call_event(ON_HALT, nullptr); | |
596 | this->runaway_state = NOT_HEATING; | |
597 | this->runaway_timer= 0; | |
598 | } | |
fb778302 JM |
599 | |
600 | }else{ | |
4f97f8b8 | 601 | this->runaway_timer= 0; |
fb778302 | 602 | } |
3bf54d11 | 603 | } |
4f97f8b8 | 604 | |
3bf54d11 AW |
605 | break; |
606 | } | |
607 | } | |
8ccab7cf | 608 | } |
201e6dcf | 609 | |
4710532a JM |
610 | void TemperatureControl::setPIDp(float p) |
611 | { | |
612 | this->p_factor = p; | |
201e6dcf JM |
613 | } |
614 | ||
4710532a JM |
615 | void TemperatureControl::setPIDi(float i) |
616 | { | |
617 | this->i_factor = i * this->PIDdt; | |
201e6dcf JM |
618 | } |
619 | ||
4710532a JM |
620 | void TemperatureControl::setPIDd(float d) |
621 | { | |
622 | this->d_factor = d / this->PIDdt; | |
201e6dcf | 623 | } |