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