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 | ||
7b49793d | 8 | // TODO : THIS FILE IS LAME, MUST BE MADE MUCH BETTER |
cd011f58 | 9 | |
ded56b35 AW |
10 | #include "libs/Module.h" |
11 | #include "libs/Kernel.h" | |
12 | #include <math.h> | |
13 | #include "TemperatureControl.h" | |
3c308aeb | 14 | #include "TemperatureControlPool.h" |
3c132bd0 | 15 | #include "libs/Pin.h" |
e84c3be3 | 16 | #include "libs/Median.h" |
3c4f2dd8 | 17 | #include "modules/robot/Conveyor.h" |
8293d443 | 18 | #include "PublicDataRequest.h" |
85eabc50 | 19 | #include "TemperatureControlPublicAccess.h" |
61134a65 JM |
20 | #include "StreamOutputPool.h" |
21 | #include "Config.h" | |
22 | #include "checksumm.h" | |
23 | #include "Gcode.h" | |
24 | #include "Adc.h" | |
25 | #include "SlowTicker.h" | |
26 | #include "Pauser.h" | |
8d54c34c | 27 | #include "ConfigValue.h" |
66383b80 JM |
28 | #include "TemperatureControl.h" |
29 | #include "PID_Autotuner.h" | |
ded56b35 | 30 | |
8f91e4e6 MM |
31 | #include "MRI_Hooks.h" |
32 | ||
85eabc50 JM |
33 | #define UNDEFINED -1 |
34 | ||
35 | #define thermistor_checksum CHECKSUM("thermistor") | |
36 | #define r0_checksum CHECKSUM("r0") | |
37 | #define readings_per_second_checksum CHECKSUM("readings_per_second") | |
38 | #define max_pwm_checksum CHECKSUM("max_pwm") | |
39 | #define pwm_frequency_checksum CHECKSUM("pwm_frequency") | |
989d0e94 JM |
40 | #define bang_bang_checksum CHECKSUM("bang_bang") |
41 | #define hysteresis_checksum CHECKSUM("hysteresis") | |
85eabc50 JM |
42 | #define t0_checksum CHECKSUM("t0") |
43 | #define beta_checksum CHECKSUM("beta") | |
44 | #define vadc_checksum CHECKSUM("vadc") | |
45 | #define vcc_checksum CHECKSUM("vcc") | |
46 | #define r1_checksum CHECKSUM("r1") | |
47 | #define r2_checksum CHECKSUM("r2") | |
48 | #define thermistor_pin_checksum CHECKSUM("thermistor_pin") | |
49 | #define heater_pin_checksum CHECKSUM("heater_pin") | |
50 | ||
51 | #define get_m_code_checksum CHECKSUM("get_m_code") | |
52 | #define set_m_code_checksum CHECKSUM("set_m_code") | |
53 | #define set_and_wait_m_code_checksum CHECKSUM("set_and_wait_m_code") | |
54 | ||
55 | #define designator_checksum CHECKSUM("designator") | |
56 | ||
57 | #define p_factor_checksum CHECKSUM("p_factor") | |
58 | #define i_factor_checksum CHECKSUM("i_factor") | |
59 | #define d_factor_checksum CHECKSUM("d_factor") | |
60 | ||
61 | #define i_max_checksum CHECKSUM("i_max") | |
62 | ||
63 | #define preset1_checksum CHECKSUM("preset1") | |
64 | #define preset2_checksum CHECKSUM("preset2") | |
65 | ||
66 | ||
7e57bc2c BG |
67 | TemperatureControl::TemperatureControl(uint16_t name) : |
68 | name_checksum(name), waiting(false), min_temp_violated(false) {} | |
ded56b35 AW |
69 | |
70 | void TemperatureControl::on_module_loaded(){ | |
907d5e8a | 71 | |
81b547a1 | 72 | // We start not desiring any temp |
907d5e8a | 73 | this->target_temperature = UNDEFINED; |
ded56b35 AW |
74 | |
75 | // Settings | |
7dd8133c | 76 | this->on_config_reload(this); |
ded56b35 AW |
77 | |
78 | this->acceleration_factor = 10; | |
79 | ||
ded56b35 | 80 | // Register for events |
476dcb96 | 81 | register_for_event(ON_CONFIG_RELOAD); |
df27a6a3 | 82 | this->register_for_event(ON_GCODE_EXECUTE); |
b0be67b5 | 83 | this->register_for_event(ON_GCODE_RECEIVED); |
df27a6a3 | 84 | this->register_for_event(ON_MAIN_LOOP); |
8ccab7cf | 85 | this->register_for_event(ON_SECOND_TICK); |
8293d443 | 86 | this->register_for_event(ON_GET_PUBLIC_DATA); |
991d98cc | 87 | this->register_for_event(ON_SET_PUBLIC_DATA); |
ded56b35 AW |
88 | } |
89 | ||
7e57bc2c BG |
90 | void TemperatureControl::on_main_loop(void* argument){ |
91 | if (this->min_temp_violated) { | |
347854ff | 92 | THEKERNEL->streams->printf("Error: MINTEMP triggered on P%d.%d! check your thermistors!\n", this->thermistor_pin.port_number, this->thermistor_pin.pin); |
7e57bc2c | 93 | this->min_temp_violated = false; |
a7f12bed | 94 | } |
7e57bc2c | 95 | } |
7dd8133c AW |
96 | |
97 | // Get configuration from the config file | |
98 | void TemperatureControl::on_config_reload(void* argument){ | |
99 | ||
1306ba99 | 100 | // General config |
314ab8f7 MM |
101 | this->set_m_code = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, set_m_code_checksum)->by_default(104)->as_number(); |
102 | 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(); | |
103 | this->get_m_code = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, get_m_code_checksum)->by_default(105)->as_number(); | |
104 | this->readings_per_second = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, readings_per_second_checksum)->by_default(20)->as_number(); | |
7dee00e4 | 105 | |
314ab8f7 | 106 | this->designator = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, designator_checksum)->by_default(string("T"))->as_string(); |
b0be67b5 | 107 | |
7dd8133c | 108 | // Values are here : http://reprap.org/wiki/Thermistor |
423df6df AW |
109 | this->r0 = 100000; |
110 | this->t0 = 25; | |
a98f72da | 111 | this->beta = 4066; |
423df6df AW |
112 | this->r1 = 0; |
113 | this->r2 = 4700; | |
a98f72da | 114 | |
3c132bd0 | 115 | // Preset values for various common types of thermistors |
314ab8f7 | 116 | ConfigValue* thermistor = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, thermistor_checksum); |
a2f7633f JM |
117 | if( thermistor->as_string().compare("EPCOS100K" ) == 0 ){ // Default |
118 | }else if( thermistor->as_string().compare("RRRF100K" ) == 0 ){ this->beta = 3960; | |
119 | }else if( thermistor->as_string().compare("RRRF10K" ) == 0 ){ this->beta = 3964; this->r0 = 10000; this->r1 = 680; this->r2 = 1600; | |
120 | }else if( thermistor->as_string().compare("Honeywell100K") == 0 ){ this->beta = 3974; | |
121 | }else if( thermistor->as_string().compare("Semitec" ) == 0 ){ this->beta = 4267; | |
122 | }else if( thermistor->as_string().compare("HT100K" ) == 0 ){ this->beta = 3990; } | |
a98f72da | 123 | |
3c132bd0 | 124 | // Preset values are overriden by specified values |
314ab8f7 MM |
125 | this->r0 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, r0_checksum )->by_default(this->r0 )->as_number(); // Stated resistance eg. 100K |
126 | this->t0 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, t0_checksum )->by_default(this->t0 )->as_number(); // Temperature at stated resistance, eg. 25C | |
127 | this->beta = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, beta_checksum)->by_default(this->beta)->as_number(); // Thermistor beta rating. See http://reprap.org/bin/view/Main/MeasuringThermistorBeta | |
128 | this->r1 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, r1_checksum )->by_default(this->r1 )->as_number(); | |
129 | this->r2 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, r2_checksum )->by_default(this->r2 )->as_number(); | |
907d5e8a | 130 | |
314ab8f7 MM |
131 | this->preset1 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, preset1_checksum)->by_default(0)->as_number(); |
132 | this->preset2 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, preset2_checksum)->by_default(0)->as_number(); | |
f4bd4fc3 | 133 | |
907d5e8a | 134 | |
df27a6a3 | 135 | // Thermistor math |
907d5e8a MM |
136 | j = (1.0 / beta); |
137 | k = (1.0 / (t0 + 273.15)); | |
138 | ||
c4f4cf73 | 139 | // sigma-delta output modulation |
b35ac71e | 140 | this->o = 0; |
3c132bd0 AW |
141 | |
142 | // Thermistor pin for ADC readings | |
314ab8f7 MM |
143 | this->thermistor_pin.from_string(THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, thermistor_pin_checksum )->required()->as_string()); |
144 | THEKERNEL->adc->enable_pin(&thermistor_pin); | |
3c132bd0 AW |
145 | |
146 | // Heater pin | |
314ab8f7 MM |
147 | this->heater_pin.from_string( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, heater_pin_checksum)->required()->as_string())->as_output(); |
148 | this->heater_pin.max_pwm( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, max_pwm_checksum)->by_default(255)->as_number() ); | |
989d0e94 | 149 | |
7dee00e4 | 150 | this->heater_pin.set(0); |
989d0e94 JM |
151 | |
152 | // used to enable bang bang control of heater | |
153 | this->use_bangbang= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, bang_bang_checksum)->by_default(false)->as_bool(); | |
154 | this->hysteresis= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, hysteresis_checksum)->by_default(2)->as_number(); | |
7dd8133c | 155 | |
cb3460e9 | 156 | set_low_on_debug(heater_pin.port_number, heater_pin.pin); |
8f91e4e6 | 157 | |
907d5e8a | 158 | // activate SD-DAC timer |
314ab8f7 | 159 | 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); |
907d5e8a | 160 | |
f39da6fe | 161 | // reading tick |
314ab8f7 | 162 | THEKERNEL->slow_ticker->attach( this->readings_per_second, this, &TemperatureControl::thermistor_read_tick ); |
201e6dcf | 163 | this->PIDdt= 1.0 / this->readings_per_second; |
f39da6fe | 164 | |
907d5e8a | 165 | // PID |
314ab8f7 | 166 | setPIDp( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, p_factor_checksum)->by_default(10 )->as_number() ); |
1ad23cd3 | 167 | setPIDi( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, i_factor_checksum)->by_default(0.3f)->as_number() ); |
314ab8f7 | 168 | setPIDd( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, d_factor_checksum)->by_default(200)->as_number() ); |
10e66797 | 169 | // set to the same as max_pwm by default |
314ab8f7 | 170 | this->i_max = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, i_max_checksum )->by_default(this->heater_pin.max_pwm())->as_number(); |
10e66797 JM |
171 | this->iTerm = 0.0; |
172 | this->lastInput= -1.0; | |
907d5e8a | 173 | this->last_reading = 0.0; |
7dd8133c AW |
174 | } |
175 | ||
3c4f2dd8 | 176 | void TemperatureControl::on_gcode_received(void* argument){ |
b0be67b5 | 177 | Gcode* gcode = static_cast<Gcode*>(argument); |
8dfbc976 | 178 | if (gcode->has_m) { |
b0be67b5 MM |
179 | // Get temperature |
180 | if( gcode->m == this->get_m_code ){ | |
93284f6f JM |
181 | char buf[32]; // should be big enough for any status |
182 | int n= snprintf(buf, sizeof(buf), "%s:%3.1f /%3.1f @%d ", this->designator.c_str(), this->get_temperature(), ((target_temperature == UNDEFINED)?0.0:target_temperature), this->o); | |
183 | gcode->txt_after_ok.append(buf, n); | |
a24ab0ac | 184 | gcode->mark_as_taken(); |
8dfbc976 JM |
185 | |
186 | } else if (gcode->m == 301) { | |
74b6303c | 187 | gcode->mark_as_taken(); |
827a49ca MM |
188 | if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) |
189 | { | |
190 | if (gcode->has_letter('P')) | |
201e6dcf | 191 | setPIDp( gcode->get_value('P') ); |
827a49ca | 192 | if (gcode->has_letter('I')) |
201e6dcf | 193 | setPIDi( gcode->get_value('I') ); |
827a49ca | 194 | if (gcode->has_letter('D')) |
201e6dcf | 195 | setPIDd( gcode->get_value('D') ); |
827a49ca MM |
196 | if (gcode->has_letter('X')) |
197 | this->i_max = gcode->get_value('X'); | |
198 | } | |
10e66797 JM |
199 | //gcode->stream->printf("%s(S%d): Pf:%g If:%g Df:%g X(I_max):%g Pv:%g Iv:%g Dv:%g 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->p, this->i, this->d, o); |
200 | gcode->stream->printf("%s(S%d): Pf:%g If:%g Df:%g X(I_max):%g 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, o); | |
8dfbc976 JM |
201 | |
202 | } else if (gcode->m == 303) { | |
203 | if (gcode->has_letter('E') && (gcode->get_value('E') == this->pool_index)) { | |
204 | gcode->mark_as_taken(); | |
1ad23cd3 | 205 | float target = 150.0; |
8dfbc976 JM |
206 | if (gcode->has_letter('S')) { |
207 | target = gcode->get_value('S'); | |
827a49ca MM |
208 | gcode->stream->printf("Target: %5.1f\n", target); |
209 | } | |
8dfbc976 JM |
210 | int ncycles= 8; |
211 | if (gcode->has_letter('C')) { | |
212 | ncycles= gcode->get_value('C'); | |
213 | } | |
827a49ca | 214 | gcode->stream->printf("Start PID tune, command is %s\n", gcode->command.c_str()); |
8dfbc976 | 215 | this->pool->PIDtuner->begin(this, target, gcode->stream, ncycles); |
827a49ca | 216 | } |
cf1a7632 | 217 | |
33e4cc02 | 218 | } else if (gcode->m == 500 || gcode->m == 503){// M500 saves some volatile settings to config override file, M503 just prints the settings |
458063f6 | 219 | gcode->stream->printf(";PID settings:\nM301 S%d P%1.4f I%1.4f D%1.4f\n", this->pool_index, this->p_factor, this->i_factor/this->PIDdt, this->d_factor*this->PIDdt); |
33e4cc02 JM |
220 | gcode->mark_as_taken(); |
221 | ||
8dfbc976 | 222 | } else if( ( gcode->m == this->set_m_code || gcode->m == this->set_and_wait_m_code ) && gcode->has_letter('S') ) { |
8dfbc976 | 223 | // Attach gcodes to the last block for on_gcode_execute |
e0ee24ed | 224 | THEKERNEL->conveyor->append_gcode(gcode); |
2134bcf2 MM |
225 | |
226 | // push an empty block if we have to wait, so the Planner can get things right, and we can prevent subsequent non-move gcodes from executing | |
227 | if (gcode->m == this->set_and_wait_m_code) | |
518e225f | 228 | // ensure that no subsequent gcodes get executed with our M109 or similar |
2134bcf2 | 229 | THEKERNEL->conveyor->queue_head_block(); |
3c4f2dd8 | 230 | } |
b0be67b5 MM |
231 | } |
232 | } | |
db453125 | 233 | |
ded56b35 AW |
234 | void TemperatureControl::on_gcode_execute(void* argument){ |
235 | Gcode* gcode = static_cast<Gcode*>(argument); | |
e6b5ae25 | 236 | if( gcode->has_m){ |
cf1a7632 MM |
237 | if (((gcode->m == this->set_m_code) || (gcode->m == this->set_and_wait_m_code)) |
238 | && gcode->has_letter('S')) | |
907d5e8a | 239 | { |
1ad23cd3 | 240 | float v = gcode->get_value('S'); |
f4bd4fc3 | 241 | |
cf1a7632 | 242 | if (v == 0.0) |
eabf7a9b | 243 | { |
907d5e8a | 244 | this->target_temperature = UNDEFINED; |
b35ac71e | 245 | this->heater_pin.set((this->o=0)); |
eabf7a9b MM |
246 | } |
247 | else | |
248 | { | |
f4bd4fc3 | 249 | this->set_desired_temperature(v); |
cf1a7632 MM |
250 | |
251 | if( gcode->m == this->set_and_wait_m_code) | |
252 | { | |
314ab8f7 | 253 | THEKERNEL->pauser->take(); |
cf1a7632 MM |
254 | this->waiting = true; |
255 | } | |
907d5e8a | 256 | } |
df27a6a3 | 257 | } |
df27a6a3 | 258 | } |
ded56b35 AW |
259 | } |
260 | ||
8293d443 | 261 | void TemperatureControl::on_get_public_data(void* argument){ |
b19aa09d | 262 | PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument); |
201e6dcf | 263 | |
b19aa09d JM |
264 | if(!pdr->starts_with(temperature_control_checksum)) return; |
265 | ||
266 | if(!pdr->second_element_is(this->name_checksum)) return; // will be bed or hotend | |
267 | ||
268 | // ok this is targeted at us, so send back the requested data | |
269 | if(pdr->third_element_is(current_temperature_checksum)) { | |
270 | // this must be static as it will be accessed long after we have returned | |
271 | static struct pad_temperature temp_return; | |
272 | temp_return.current_temperature= this->get_temperature(); | |
273 | temp_return.target_temperature= (target_temperature == UNDEFINED) ? 0 : this->target_temperature; | |
274 | temp_return.pwm= this->o; | |
201e6dcf | 275 | |
b19aa09d JM |
276 | pdr->set_data_ptr(&temp_return); |
277 | pdr->set_taken(); | |
278 | } | |
8293d443 | 279 | } |
db453125 | 280 | |
77047e76 | 281 | void TemperatureControl::on_set_public_data(void* argument){ |
991d98cc | 282 | PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument); |
77047e76 | 283 | |
991d98cc | 284 | if(!pdr->starts_with(temperature_control_checksum)) return; |
77047e76 | 285 | |
991d98cc | 286 | if(!pdr->second_element_is(this->name_checksum)) return; // will be bed or hotend |
77047e76 | 287 | |
991d98cc | 288 | // ok this is targeted at us, so set the temp |
1ad23cd3 | 289 | float t= *static_cast<float*>(pdr->get_data_ptr()); |
991d98cc JM |
290 | this->set_desired_temperature(t); |
291 | pdr->set_taken(); | |
77047e76 JM |
292 | } |
293 | ||
1ad23cd3 | 294 | void TemperatureControl::set_desired_temperature(float desired_temperature) |
cf1a7632 MM |
295 | { |
296 | if (desired_temperature == 1.0) | |
297 | desired_temperature = preset1; | |
298 | else if (desired_temperature == 2.0) | |
299 | desired_temperature = preset2; | |
300 | ||
907d5e8a | 301 | target_temperature = desired_temperature; |
959dc7db | 302 | if (desired_temperature == 0.0) |
b35ac71e | 303 | heater_pin.set((this->o = 0)); |
ded56b35 AW |
304 | } |
305 | ||
1ad23cd3 | 306 | float TemperatureControl::get_temperature(){ |
907d5e8a | 307 | return last_reading; |
ded56b35 AW |
308 | } |
309 | ||
1ad23cd3 | 310 | float TemperatureControl::adc_value_to_temperature(int adc_value) |
907d5e8a MM |
311 | { |
312 | if ((adc_value == 4095) || (adc_value == 0)) | |
313 | return INFINITY; | |
1ad23cd3 | 314 | float r = r2 / ((4095.0 / adc_value) - 1.0); |
907d5e8a MM |
315 | if (r1 > 0) |
316 | r = (r1 * r) / (r1 - r); | |
317 | return (1.0 / (k + (j * log(r / r0)))) - 273.15; | |
ded56b35 AW |
318 | } |
319 | ||
8b8b3339 | 320 | uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy){ |
907d5e8a MM |
321 | int r = new_thermistor_reading(); |
322 | ||
1ad23cd3 | 323 | float temperature = adc_value_to_temperature(r); |
907d5e8a MM |
324 | |
325 | if (target_temperature > 0) | |
326 | { | |
327 | if ((r <= 1) || (r >= 4094)) | |
36a3f3f0 | 328 | { |
7e57bc2c | 329 | this->min_temp_violated = true; |
907d5e8a | 330 | target_temperature = UNDEFINED; |
b35ac71e | 331 | heater_pin.set((this->o=0)); |
36a3f3f0 MM |
332 | } |
333 | else | |
334 | { | |
907d5e8a MM |
335 | pid_process(temperature); |
336 | if ((temperature > target_temperature) && waiting) | |
36a3f3f0 | 337 | { |
347854ff | 338 | THEKERNEL->pauser->release(); |
907d5e8a | 339 | waiting = false; |
81b547a1 | 340 | } |
ded56b35 AW |
341 | } |
342 | } | |
959dc7db MM |
343 | else |
344 | { | |
b35ac71e | 345 | heater_pin.set((this->o = 0)); |
959dc7db | 346 | } |
907d5e8a | 347 | last_reading = temperature; |
281967e4 | 348 | return 0; |
ded56b35 AW |
349 | } |
350 | ||
10e66797 JM |
351 | /** |
352 | * Based on https://github.com/br3ttb/Arduino-PID-Library | |
353 | */ | |
1ad23cd3 | 354 | void TemperatureControl::pid_process(float temperature) |
907d5e8a | 355 | { |
989d0e94 | 356 | if(use_bangbang) { |
b35ac71e | 357 | // bang bang is very simple, if temp is < target - hysteresis turn on full else if temp is > target + hysteresis turn heater off |
989d0e94 | 358 | // good for relays |
b35ac71e | 359 | if(temperature > (target_temperature+hysteresis) && this->o > 0) { |
989d0e94 | 360 | heater_pin.set(false); |
989d0e94 JM |
361 | this->o= 0; // for display purposes only |
362 | ||
b35ac71e | 363 | }else if(temperature < (target_temperature-hysteresis) && this->o <= 0) { |
989d0e94 JM |
364 | if(heater_pin.max_pwm() >= 255) { |
365 | // turn on full | |
366 | this->heater_pin.set(true); | |
367 | this->o= 255; // for display purposes only | |
368 | }else{ | |
369 | // only to whatever max pwm is configured | |
370 | this->heater_pin.pwm(heater_pin.max_pwm()); | |
371 | this->o= heater_pin.max_pwm(); // for display purposes only | |
372 | } | |
989d0e94 JM |
373 | } |
374 | return; | |
375 | } | |
ded56b35 | 376 | |
989d0e94 JM |
377 | // regular PID control |
378 | float error = target_temperature - temperature; | |
10e66797 JM |
379 | this->iTerm += (error * this->i_factor); |
380 | if (this->iTerm > this->i_max) this->iTerm = this->i_max; | |
381 | else if (this->iTerm < 0.0) this->iTerm = 0.0; | |
ded56b35 | 382 | |
10e66797 | 383 | if(this->lastInput < 0.0) this->lastInput= temperature; // set first time |
1ad23cd3 | 384 | float d= (temperature - this->lastInput); |
ded56b35 | 385 | |
10e66797 JM |
386 | // calculate the PID output |
387 | // TODO does this need to be scaled by max_pwm/256? I think not as p_factor already does that | |
388 | this->o = (this->p_factor*error) + this->iTerm - (this->d_factor*d); | |
8cdae54c | 389 | |
7dee00e4 | 390 | if (this->o >= heater_pin.max_pwm()) |
7dee00e4 | 391 | this->o = heater_pin.max_pwm(); |
27aecda6 | 392 | else if (this->o < 0) |
907d5e8a MM |
393 | this->o = 0; |
394 | ||
27aecda6 | 395 | this->heater_pin.pwm(this->o); |
10e66797 | 396 | this->lastInput= temperature; |
907d5e8a | 397 | } |
ded56b35 | 398 | |
907d5e8a MM |
399 | int TemperatureControl::new_thermistor_reading() |
400 | { | |
314ab8f7 | 401 | int last_raw = THEKERNEL->adc->read(&thermistor_pin); |
10e66797 | 402 | if (queue.size() >= queue.capacity()) { |
907d5e8a MM |
403 | uint16_t l; |
404 | queue.pop_front(l); | |
907d5e8a | 405 | } |
c4f4cf73 MM |
406 | uint16_t r = last_raw; |
407 | queue.push_back(r); | |
e84c3be3 BG |
408 | for (int i=0; i<queue.size(); i++) |
409 | median_buffer[i] = *queue.get_ref(i); | |
410 | uint16_t m = median_buffer[quick_median(median_buffer, queue.size())]; | |
411 | return m; | |
907d5e8a | 412 | } |
8ccab7cf MM |
413 | |
414 | void TemperatureControl::on_second_tick(void* argument) | |
415 | { | |
416 | if (waiting) | |
347854ff | 417 | THEKERNEL->streams->printf("%s:%3.1f /%3.1f @%d\n", designator.c_str(), get_temperature(), ((target_temperature == UNDEFINED)?0.0:target_temperature), o); |
8ccab7cf | 418 | } |
201e6dcf | 419 | |
1ad23cd3 | 420 | void TemperatureControl::setPIDp(float p) { |
201e6dcf JM |
421 | this->p_factor= p; |
422 | } | |
423 | ||
1ad23cd3 | 424 | void TemperatureControl::setPIDi(float i) { |
201e6dcf JM |
425 | this->i_factor= i*this->PIDdt; |
426 | } | |
427 | ||
1ad23cd3 | 428 | void TemperatureControl::setPIDd(float d) { |
201e6dcf JM |
429 | this->d_factor= d/this->PIDdt; |
430 | } |