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); |
573ed7c0 | 100 | this->register_for_event(ON_IDLE); |
71cc73eb JM |
101 | |
102 | if(!this->readonly) { | |
71cc73eb JM |
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 | } | |
3d1a4519 JM |
108 | } |
109 | ||
110 | void TemperatureControl::on_halt(void *arg) | |
111 | { | |
728477c4 JM |
112 | if(arg == nullptr) { |
113 | // turn off heater | |
114 | this->o = 0; | |
115 | this->heater_pin.set(0); | |
116 | this->target_temperature = UNDEFINED; | |
117 | } | |
ded56b35 AW |
118 | } |
119 | ||
edd99f33 GC |
120 | void TemperatureControl::on_idle(void *arg) |
121 | { | |
122 | sensor->on_idle(); | |
123 | } | |
124 | ||
125 | ||
4710532a JM |
126 | void TemperatureControl::on_main_loop(void *argument) |
127 | { | |
01004e36 JM |
128 | if (this->temp_violated) { |
129 | this->temp_violated = false; | |
f46f7916 | 130 | THEKERNEL->streams->printf("ERROR: MINTEMP or MAXTEMP triggered on %s. Check your temperature sensors!\n", designator.c_str()); |
01004e36 JM |
131 | THEKERNEL->streams->printf("HALT asserted - reset or M999 required\n"); |
132 | THEKERNEL->call_event(ON_HALT, nullptr); | |
a7f12bed | 133 | } |
7e57bc2c | 134 | } |
7dd8133c AW |
135 | |
136 | // Get configuration from the config file | |
71cc73eb | 137 | void TemperatureControl::load_config() |
4710532a | 138 | { |
7dd8133c | 139 | |
1306ba99 | 140 | // General config |
314ab8f7 MM |
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(); | |
7dee00e4 | 145 | |
314ab8f7 | 146 | this->designator = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, designator_checksum)->by_default(string("T"))->as_string(); |
b0be67b5 | 147 | |
1cd1bf1a | 148 | // Runaway parameters |
6c8b8ca0 | 149 | uint32_t n= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_range_checksum)->by_default(20)->as_number(); |
5cc0bc2c JM |
150 | if(n > 63) n= 63; |
151 | this->runaway_range= n; | |
4f97f8b8 | 152 | |
8123c72d | 153 | // these need to fit in 9 bits after dividing by 8 so max is 4088 secs or 68 minutes |
6c8b8ca0 | 154 | n= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_heating_timeout_checksum)->by_default(900)->as_number(); |
8123c72d | 155 | if(n > 4088) n= 4088; |
4f97f8b8 | 156 | this->runaway_heating_timeout = n/8; // we have 8 second ticks |
495afe33 | 157 | n= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_cooling_timeout_checksum)->by_default(0)->as_number(); // disable by default |
8123c72d | 158 | if(n > 4088) n= 4088; |
4f97f8b8 | 159 | this->runaway_cooling_timeout = n/8; |
1cd1bf1a | 160 | |
6682b332 JM |
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 | ||
3a014cbb | 163 | // Max and min temperatures we are not allowed to get over (Safety) |
01004e36 | 164 | this->max_temp = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, max_temp_checksum)->by_default(300)->as_number(); |
3a014cbb | 165 | this->min_temp = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, min_temp_checksum)->by_default(0)->as_number(); |
7d4baeee | 166 | |
71cc73eb JM |
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 | ||
d8baddd9 | 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. | |
4710532a | 183 | if(sensor_type.compare("thermistor") == 0) { |
d8baddd9 | 184 | sensor = new Thermistor(); |
4710532a | 185 | } else if(sensor_type.compare("max31855") == 0) { |
d8baddd9 | 186 | sensor = new Max31855(); |
073d88ec E |
187 | } else if(sensor_type.compare("ad8495") == 0) { |
188 | sensor = new AD8495(); | |
3b355b1c BM |
189 | } else if(sensor_type.compare("pt100_e3d") == 0) { |
190 | sensor = new PT100_E3D(); | |
4710532a | 191 | } else { |
d8baddd9 | 192 | sensor = new TempSensor(); // A dummy implementation |
193 | } | |
194 | sensor->UpdateConfig(temperature_control_checksum, this->name_checksum); | |
4710532a | 195 | |
71cc73eb JM |
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(); | |
f4bd4fc3 | 198 | |
907d5e8a | 199 | |
c4f4cf73 | 200 | // sigma-delta output modulation |
b35ac71e | 201 | this->o = 0; |
3c132bd0 | 202 | |
71cc73eb JM |
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(); | |
08f89868 | 207 | this->windup = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, windup_checksum)->by_default(false)->as_bool(); |
71cc73eb JM |
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 | } | |
8f91e4e6 | 214 | |
907d5e8a | 215 | |
f39da6fe | 216 | // reading tick |
314ab8f7 | 217 | THEKERNEL->slow_ticker->attach( this->readings_per_second, this, &TemperatureControl::thermistor_read_tick ); |
4710532a | 218 | this->PIDdt = 1.0 / this->readings_per_second; |
f39da6fe | 219 | |
907d5e8a | 220 | // PID |
314ab8f7 | 221 | setPIDp( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, p_factor_checksum)->by_default(10 )->as_number() ); |
1ad23cd3 | 222 | setPIDi( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, i_factor_checksum)->by_default(0.3f)->as_number() ); |
314ab8f7 | 223 | setPIDd( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, d_factor_checksum)->by_default(200)->as_number() ); |
71cc73eb JM |
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 | ||
10e66797 | 230 | this->iTerm = 0.0; |
4710532a | 231 | this->lastInput = -1.0; |
907d5e8a | 232 | this->last_reading = 0.0; |
7dd8133c AW |
233 | } |
234 | ||
4710532a JM |
235 | void TemperatureControl::on_gcode_received(void *argument) |
236 | { | |
237 | Gcode *gcode = static_cast<Gcode *>(argument); | |
8dfbc976 | 238 | if (gcode->has_m) { |
ee4711d1 | 239 | |
4710532a | 240 | if( gcode->m == this->get_m_code ) { |
93284f6f | 241 | char buf[32]; // should be big enough for any status |
3517edce | 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); |
93284f6f | 243 | gcode->txt_after_ok.append(buf, n); |
71cc73eb JM |
244 | return; |
245 | } | |
8dfbc976 | 246 | |
76f53dc6 | 247 | if (gcode->m == 305) { // set or get sensor settings |
76f53dc6 | 248 | if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) { |
1f8dab1a | 249 | TempSensor::sensor_options_t args= gcode->get_args(); |
d22755f7 JM |
250 | args.erase('S'); // don't include the S |
251 | if(args.size() > 0) { | |
76f53dc6 | 252 | // set the new options |
d22755f7 JM |
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; | |
76f53dc6 JM |
261 | } |
262 | ||
263 | }else if(!gcode->has_letter('S')) { | |
d22755f7 | 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"); |
76f53dc6 JM |
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 | |
a2e5f877 | 270 | gcode->stream->printf("%s(S%d): %c %1.18f\n", this->designator.c_str(), this->pool_index, i.first, i.second); |
76f53dc6 JM |
271 | } |
272 | } | |
273 | } | |
274 | ||
275 | return; | |
276 | } | |
277 | ||
71cc73eb JM |
278 | // readonly sensors don't handle the rest |
279 | if(this->readonly) return; | |
280 | ||
01004e36 JM |
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) { | |
4710532a | 295 | if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) { |
827a49ca | 296 | if (gcode->has_letter('P')) |
201e6dcf | 297 | setPIDp( gcode->get_value('P') ); |
827a49ca | 298 | if (gcode->has_letter('I')) |
201e6dcf | 299 | setPIDi( gcode->get_value('I') ); |
827a49ca | 300 | if (gcode->has_letter('D')) |
201e6dcf | 301 | setPIDd( gcode->get_value('D') ); |
827a49ca | 302 | if (gcode->has_letter('X')) |
f1e38d95 JM |
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')) { | |
fb785a37 | 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); |
f1e38d95 JM |
309 | } |
310 | ||
4710532a | 311 | } else if (gcode->m == 500 || gcode->m == 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings |
bc962896 | 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()); |
f1e38d95 | 313 | |
01004e36 JM |
314 | gcode->stream->printf(";Max temperature setting:\nM143 S%d P%1.4f\n", this->pool_index, this->max_temp); |
315 | ||
f1e38d95 JM |
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) { | |
7d678d16 | 322 | gcode->stream->printf(" %c%1.18f", i.first, i.second); |
f1e38d95 JM |
323 | } |
324 | gcode->stream->printf("\n"); | |
325 | } | |
326 | } | |
33e4cc02 | 327 | |
8e8b938e | 328 | } else if( ( gcode->m == this->set_m_code || gcode->m == this->set_and_wait_m_code ) && gcode->has_letter('S')) { |
eb5f6de4 CE |
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) { | |
42cb1b30 | 352 | // required so temp change happens in order |
eb5f6de4 CE |
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 | } | |
2134bcf2 | 357 | |
42cb1b30 | 358 | float v = gcode->get_value('S'); |
db453125 | 359 | |
42cb1b30 JM |
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); | |
ed52bf31 JM |
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) { | |
487976f7 | 367 | if(isinf(get_temperature()) && isinf(sensor->get_temperature())) { |
f0433db4 | 368 | THEKERNEL->streams->printf("Temperature reading is unreliable on %s HALT asserted - reset or M999 required\n", designator.c_str()); |
487976f7 JM |
369 | THEKERNEL->call_event(ON_HALT, nullptr); |
370 | return; | |
371 | } | |
372 | ||
ed52bf31 JM |
373 | this->waiting = true; // on_second_tick will announce temps |
374 | while ( get_temperature() < target_temperature ) { | |
375 | THEKERNEL->call_event(ON_IDLE, this); | |
798295c1 | 376 | // check if ON_HALT was called (usually by kill button) |
aa896868 JM |
377 | if(THEKERNEL->is_halted() || this->target_temperature == UNDEFINED) { |
378 | THEKERNEL->streams->printf("Wait on temperature aborted by kill\n"); | |
379 | break; | |
380 | } | |
ed52bf31 JM |
381 | } |
382 | this->waiting = false; | |
42cb1b30 | 383 | } |
cf1a7632 | 384 | } |
907d5e8a | 385 | } |
df27a6a3 | 386 | } |
df27a6a3 | 387 | } |
ded56b35 AW |
388 | } |
389 | ||
4710532a JM |
390 | void TemperatureControl::on_get_public_data(void *argument) |
391 | { | |
392 | PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument); | |
201e6dcf | 393 | |
b19aa09d JM |
394 | if(!pdr->starts_with(temperature_control_checksum)) return; |
395 | ||
8e8b938e JM |
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 | } | |
8e8b938e | 404 | |
56a6c8c1 | 405 | }else if(pdr->second_element_is(poll_controls_checksum)) { |
bab4e1bd | 406 | // polling for all temperature controls |
3bfb2639 JM |
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()); | |
bab4e1bd | 410 | |
3bfb2639 | 411 | struct pad_temperature t; |
bab4e1bd | 412 | // setup data |
3bfb2639 JM |
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(); | |
b19aa09d | 420 | |
56a6c8c1 JM |
421 | }else if(pdr->second_element_is(current_temperature_checksum)) { |
422 | // if targeted at us | |
423 | if(pdr->third_element_is(this->name_checksum)) { | |
564cf1f0 | 424 | // ok this is targeted at us, so set the requ3sted data in the pointer passed into us |
56a6c8c1 JM |
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(); | |
56a6c8c1 | 432 | } |
b19aa09d | 433 | } |
8e8b938e | 434 | |
8293d443 | 435 | } |
db453125 | 436 | |
4710532a JM |
437 | void TemperatureControl::on_set_public_data(void *argument) |
438 | { | |
439 | PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument); | |
77047e76 | 440 | |
991d98cc | 441 | if(!pdr->starts_with(temperature_control_checksum)) return; |
77047e76 | 442 | |
8e8b938e | 443 | if(!pdr->second_element_is(this->name_checksum)) return; |
77047e76 | 444 | |
991d98cc | 445 | // ok this is targeted at us, so set the temp |
ed52bf31 | 446 | // NOTE unlike the M code this will set the temp now not when the queue is empty |
4710532a | 447 | float t = *static_cast<float *>(pdr->get_data_ptr()); |
991d98cc JM |
448 | this->set_desired_temperature(t); |
449 | pdr->set_taken(); | |
77047e76 JM |
450 | } |
451 | ||
1ad23cd3 | 452 | void TemperatureControl::set_desired_temperature(float desired_temperature) |
cf1a7632 | 453 | { |
7d4baeee AW |
454 | // Never go over the configured max temperature |
455 | if( desired_temperature > this->max_temp ){ | |
456 | desired_temperature = this->max_temp; | |
457 | } | |
458 | ||
08f89868 | 459 | if (desired_temperature == 1.0F) |
cf1a7632 | 460 | desired_temperature = preset1; |
08f89868 | 461 | else if (desired_temperature == 2.0F) |
cf1a7632 MM |
462 | desired_temperature = preset2; |
463 | ||
08f89868 | 464 | float last_target_temperature= target_temperature; |
907d5e8a | 465 | target_temperature = desired_temperature; |
3517edce | 466 | if (desired_temperature <= 0.0F){ |
08f89868 | 467 | // turning it off |
b35ac71e | 468 | heater_pin.set((this->o = 0)); |
08f89868 | 469 | |
3517edce | 470 | }else if(last_target_temperature <= 0.0F) { |
08f89868 JM |
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 | } | |
fb778302 JM |
478 | |
479 | // reset the runaway state, even if it was a temp change | |
480 | this->runaway_state = NOT_HEATING; | |
ded56b35 AW |
481 | } |
482 | ||
4710532a JM |
483 | float TemperatureControl::get_temperature() |
484 | { | |
907d5e8a | 485 | return last_reading; |
ded56b35 AW |
486 | } |
487 | ||
4710532a JM |
488 | uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy) |
489 | { | |
9d955060 | 490 | float temperature = sensor->get_temperature(); |
3517edce | 491 | if(!this->readonly && target_temperature > 2) { |
01004e36 JM |
492 | if (isinf(temperature) || temperature < min_temp || temperature > max_temp) { |
493 | this->temp_violated = true; | |
907d5e8a | 494 | target_temperature = UNDEFINED; |
4710532a JM |
495 | heater_pin.set((this->o = 0)); |
496 | } else { | |
907d5e8a | 497 | pid_process(temperature); |
ded56b35 | 498 | } |
959dc7db | 499 | } |
3517edce | 500 | |
907d5e8a | 501 | last_reading = temperature; |
281967e4 | 502 | return 0; |
ded56b35 AW |
503 | } |
504 | ||
10e66797 JM |
505 | /** |
506 | * Based on https://github.com/br3ttb/Arduino-PID-Library | |
507 | */ | |
1ad23cd3 | 508 | void TemperatureControl::pid_process(float temperature) |
907d5e8a | 509 | { |
989d0e94 | 510 | if(use_bangbang) { |
b35ac71e | 511 | // bang bang is very simple, if temp is < target - hysteresis turn on full else if temp is > target + hysteresis turn heater off |
989d0e94 | 512 | // good for relays |
4710532a | 513 | if(temperature > (target_temperature + hysteresis) && this->o > 0) { |
989d0e94 | 514 | heater_pin.set(false); |
4710532a | 515 | this->o = 0; // for display purposes only |
989d0e94 | 516 | |
4710532a | 517 | } else if(temperature < (target_temperature - hysteresis) && this->o <= 0) { |
989d0e94 JM |
518 | if(heater_pin.max_pwm() >= 255) { |
519 | // turn on full | |
520 | this->heater_pin.set(true); | |
4710532a JM |
521 | this->o = 255; // for display purposes only |
522 | } else { | |
989d0e94 JM |
523 | // only to whatever max pwm is configured |
524 | this->heater_pin.pwm(heater_pin.max_pwm()); | |
4710532a | 525 | this->o = heater_pin.max_pwm(); // for display purposes only |
989d0e94 | 526 | } |
989d0e94 JM |
527 | } |
528 | return; | |
529 | } | |
ded56b35 | 530 | |
989d0e94 JM |
531 | // regular PID control |
532 | float error = target_temperature - temperature; | |
08f89868 | 533 | |
0d84905d PA |
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; | |
08f89868 | 537 | if(!this->windup) this->iTerm= new_I; |
ded56b35 | 538 | |
4710532a | 539 | float d = (temperature - this->lastInput); |
ded56b35 | 540 | |
10e66797 JM |
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 | |
0d84905d | 543 | this->o = (this->p_factor * error) + new_I - (this->d_factor * d); |
8cdae54c | 544 | |
7dee00e4 | 545 | if (this->o >= heater_pin.max_pwm()) |
7dee00e4 | 546 | this->o = heater_pin.max_pwm(); |
27aecda6 | 547 | else if (this->o < 0) |
907d5e8a | 548 | this->o = 0; |
08f89868 | 549 | else if(this->windup) |
0d84905d | 550 | this->iTerm = new_I; // Only update I term when output is not saturated. |
907d5e8a | 551 | |
27aecda6 | 552 | this->heater_pin.pwm(this->o); |
4710532a | 553 | this->lastInput = temperature; |
907d5e8a | 554 | } |
ded56b35 | 555 | |
4710532a | 556 | void TemperatureControl::on_second_tick(void *argument) |
8ccab7cf | 557 | { |
3bf54d11 AW |
558 | |
559 | // If waiting for a temperature to be reach, display it to keep host programs up to date on the progress | |
8ccab7cf | 560 | if (waiting) |
3517edce | 561 | 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 |
562 | |
563 | // Check whether or not there is a temperature runaway issue, if so stop everything and report it | |
e39c48c1 | 564 | if(THEKERNEL->is_halted()) return; |
f967a81b | 565 | |
fb778302 JM |
566 | // see if runaway detection is enabled |
567 | if(this->runaway_heating_timeout == 0 && this->runaway_range == 0) return; | |
568 | ||
4f97f8b8 JM |
569 | // check every 8 seconds, depends on tick being 3 bits |
570 | if(++tick != 0) return; | |
571 | ||
fb778302 | 572 | if(this->target_temperature <= 0){ // If we are not trying to heat, state is NOT_HEATING |
c6c03d35 | 573 | this->runaway_state = NOT_HEATING; |
fb778302 | 574 | |
3bf54d11 | 575 | }else{ |
4f97f8b8 | 576 | float current_temperature= this->get_temperature(); |
fb778302 | 577 | // heater is active |
3bf54d11 | 578 | switch( this->runaway_state ){ |
c6c03d35 | 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 |
6682b332 | 580 | this->runaway_state= (this->target_temperature >= current_temperature || this->runaway_cooling_timeout == 0) ? HEATING_UP : COOLING_DOWN; |
4f97f8b8 | 581 | this->runaway_timer = 0; |
8123c72d | 582 | tick= 0; |
3bf54d11 | 583 | break; |
fb778302 | 584 | |
4f97f8b8 JM |
585 | case HEATING_UP: |
586 | case COOLING_DOWN: | |
6682b332 JM |
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)) ) { | |
c6c03d35 | 590 | this->runaway_state = TARGET_TEMPERATURE_REACHED; |
4f97f8b8 | 591 | this->runaway_timer = 0; |
8123c72d | 592 | tick= 0; |
fb778302 | 593 | |
4f97f8b8 JM |
594 | }else{ |
595 | uint16_t t= (runaway_state == HEATING_UP) ? this->runaway_heating_timeout : this->runaway_cooling_timeout; | |
fb778302 | 596 | // we are still heating up see if we have hit the max time allowed |
4f97f8b8 | 597 | if(t > 0 && ++this->runaway_timer > t){ |
fb778302 JM |
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; | |
4f97f8b8 | 601 | this->runaway_timer = 0; |
fb778302 | 602 | } |
3bf54d11 AW |
603 | } |
604 | break; | |
fb778302 JM |
605 | |
606 | case TARGET_TEMPERATURE_REACHED: | |
607 | if(this->runaway_range != 0) { | |
608 | // we are in state TARGET_TEMPERATURE_REACHED, check for thermal runaway | |
4f97f8b8 | 609 | float delta= current_temperature - this->target_temperature; |
fb778302 | 610 | |
4f97f8b8 | 611 | // If the temperature is outside the acceptable range for 8 seconds, this allows for some noise spikes without halting |
fb778302 | 612 | if(fabsf(delta) > this->runaway_range){ |
4f97f8b8 JM |
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 | } | |
fb778302 JM |
619 | |
620 | }else{ | |
4f97f8b8 | 621 | this->runaway_timer= 0; |
fb778302 | 622 | } |
3bf54d11 | 623 | } |
4f97f8b8 | 624 | |
3bf54d11 AW |
625 | break; |
626 | } | |
627 | } | |
8ccab7cf | 628 | } |
201e6dcf | 629 | |
4710532a JM |
630 | void TemperatureControl::setPIDp(float p) |
631 | { | |
632 | this->p_factor = p; | |
201e6dcf JM |
633 | } |
634 | ||
4710532a JM |
635 | void TemperatureControl::setPIDi(float i) |
636 | { | |
637 | this->i_factor = i * this->PIDdt; | |
201e6dcf JM |
638 | } |
639 | ||
4710532a JM |
640 | void TemperatureControl::setPIDd(float d) |
641 | { | |
642 | this->d_factor = d / this->PIDdt; | |
201e6dcf | 643 | } |