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" |
ded56b35 | 19 | |
8f91e4e6 MM |
20 | #include "MRI_Hooks.h" |
21 | ||
7e57bc2c BG |
22 | TemperatureControl::TemperatureControl(uint16_t name) : |
23 | name_checksum(name), waiting(false), min_temp_violated(false) {} | |
ded56b35 AW |
24 | |
25 | void TemperatureControl::on_module_loaded(){ | |
907d5e8a | 26 | |
81b547a1 | 27 | // We start not desiring any temp |
907d5e8a | 28 | this->target_temperature = UNDEFINED; |
ded56b35 AW |
29 | |
30 | // Settings | |
7dd8133c | 31 | this->on_config_reload(this); |
ded56b35 AW |
32 | |
33 | this->acceleration_factor = 10; | |
34 | ||
ded56b35 | 35 | // Register for events |
476dcb96 | 36 | register_for_event(ON_CONFIG_RELOAD); |
df27a6a3 | 37 | this->register_for_event(ON_GCODE_EXECUTE); |
b0be67b5 | 38 | this->register_for_event(ON_GCODE_RECEIVED); |
df27a6a3 | 39 | this->register_for_event(ON_MAIN_LOOP); |
8ccab7cf | 40 | this->register_for_event(ON_SECOND_TICK); |
8293d443 | 41 | this->register_for_event(ON_GET_PUBLIC_DATA); |
991d98cc | 42 | this->register_for_event(ON_SET_PUBLIC_DATA); |
ded56b35 AW |
43 | } |
44 | ||
7e57bc2c BG |
45 | void TemperatureControl::on_main_loop(void* argument){ |
46 | if (this->min_temp_violated) { | |
347854ff | 47 | THEKERNEL->streams->printf("Error: MINTEMP triggered on P%d.%d! check your thermistors!\n", this->thermistor_pin.port_number, this->thermistor_pin.pin); |
7e57bc2c | 48 | this->min_temp_violated = false; |
a7f12bed | 49 | } |
7e57bc2c | 50 | } |
7dd8133c AW |
51 | |
52 | // Get configuration from the config file | |
53 | void TemperatureControl::on_config_reload(void* argument){ | |
54 | ||
1306ba99 | 55 | // General config |
314ab8f7 MM |
56 | this->set_m_code = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, set_m_code_checksum)->by_default(104)->as_number(); |
57 | 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(); | |
58 | this->get_m_code = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, get_m_code_checksum)->by_default(105)->as_number(); | |
59 | this->readings_per_second = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, readings_per_second_checksum)->by_default(20)->as_number(); | |
7dee00e4 | 60 | |
314ab8f7 | 61 | this->designator = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, designator_checksum)->by_default(string("T"))->as_string(); |
b0be67b5 | 62 | |
7dd8133c | 63 | // Values are here : http://reprap.org/wiki/Thermistor |
423df6df AW |
64 | this->r0 = 100000; |
65 | this->t0 = 25; | |
a98f72da | 66 | this->beta = 4066; |
423df6df AW |
67 | this->r1 = 0; |
68 | this->r2 = 4700; | |
a98f72da | 69 | |
3c132bd0 | 70 | // Preset values for various common types of thermistors |
314ab8f7 | 71 | ConfigValue* thermistor = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, thermistor_checksum); |
423df6df AW |
72 | if( thermistor->value.compare("EPCOS100K" ) == 0 ){ // Default |
73 | }else if( thermistor->value.compare("RRRF100K" ) == 0 ){ this->beta = 3960; | |
74 | }else if( thermistor->value.compare("RRRF10K" ) == 0 ){ this->beta = 3964; this->r0 = 10000; this->r1 = 680; this->r2 = 1600; | |
75 | }else if( thermistor->value.compare("Honeywell100K") == 0 ){ this->beta = 3974; | |
201e6dcf | 76 | }else if( thermistor->value.compare("Semitec" ) == 0 ){ this->beta = 4267; |
93356612 | 77 | }else if( thermistor->value.compare("HT100K" ) == 0 ){ this->beta = 3990; } |
a98f72da | 78 | |
3c132bd0 | 79 | // Preset values are overriden by specified values |
314ab8f7 MM |
80 | this->r0 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, r0_checksum )->by_default(this->r0 )->as_number(); // Stated resistance eg. 100K |
81 | 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 | |
82 | 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 | |
83 | this->r1 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, r1_checksum )->by_default(this->r1 )->as_number(); | |
84 | this->r2 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, r2_checksum )->by_default(this->r2 )->as_number(); | |
907d5e8a | 85 | |
314ab8f7 MM |
86 | this->preset1 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, preset1_checksum)->by_default(0)->as_number(); |
87 | this->preset2 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, preset2_checksum)->by_default(0)->as_number(); | |
f4bd4fc3 | 88 | |
907d5e8a | 89 | |
df27a6a3 | 90 | // Thermistor math |
907d5e8a MM |
91 | j = (1.0 / beta); |
92 | k = (1.0 / (t0 + 273.15)); | |
93 | ||
c4f4cf73 MM |
94 | // sigma-delta output modulation |
95 | o = 0; | |
3c132bd0 AW |
96 | |
97 | // Thermistor pin for ADC readings | |
314ab8f7 MM |
98 | this->thermistor_pin.from_string(THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, thermistor_pin_checksum )->required()->as_string()); |
99 | THEKERNEL->adc->enable_pin(&thermistor_pin); | |
3c132bd0 AW |
100 | |
101 | // Heater pin | |
314ab8f7 MM |
102 | this->heater_pin.from_string( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, heater_pin_checksum)->required()->as_string())->as_output(); |
103 | this->heater_pin.max_pwm( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, max_pwm_checksum)->by_default(255)->as_number() ); | |
7dee00e4 | 104 | this->heater_pin.set(0); |
7dd8133c | 105 | |
cb3460e9 | 106 | set_low_on_debug(heater_pin.port_number, heater_pin.pin); |
8f91e4e6 | 107 | |
907d5e8a | 108 | // activate SD-DAC timer |
314ab8f7 | 109 | 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 | 110 | |
f39da6fe | 111 | // reading tick |
314ab8f7 | 112 | THEKERNEL->slow_ticker->attach( this->readings_per_second, this, &TemperatureControl::thermistor_read_tick ); |
201e6dcf | 113 | this->PIDdt= 1.0 / this->readings_per_second; |
f39da6fe | 114 | |
907d5e8a | 115 | // PID |
314ab8f7 | 116 | setPIDp( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, p_factor_checksum)->by_default(10 )->as_number() ); |
1ad23cd3 | 117 | setPIDi( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, i_factor_checksum)->by_default(0.3f)->as_number() ); |
314ab8f7 | 118 | setPIDd( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, d_factor_checksum)->by_default(200)->as_number() ); |
10e66797 | 119 | // set to the same as max_pwm by default |
314ab8f7 | 120 | 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 |
121 | this->iTerm = 0.0; |
122 | this->lastInput= -1.0; | |
907d5e8a | 123 | this->last_reading = 0.0; |
7dd8133c AW |
124 | } |
125 | ||
3c4f2dd8 | 126 | void TemperatureControl::on_gcode_received(void* argument){ |
b0be67b5 | 127 | Gcode* gcode = static_cast<Gcode*>(argument); |
8dfbc976 | 128 | if (gcode->has_m) { |
b0be67b5 MM |
129 | // Get temperature |
130 | if( gcode->m == this->get_m_code ){ | |
93284f6f JM |
131 | char buf[32]; // should be big enough for any status |
132 | 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); | |
133 | gcode->txt_after_ok.append(buf, n); | |
a24ab0ac | 134 | gcode->mark_as_taken(); |
8dfbc976 JM |
135 | |
136 | } else if (gcode->m == 301) { | |
74b6303c | 137 | gcode->mark_as_taken(); |
827a49ca MM |
138 | if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) |
139 | { | |
140 | if (gcode->has_letter('P')) | |
201e6dcf | 141 | setPIDp( gcode->get_value('P') ); |
827a49ca | 142 | if (gcode->has_letter('I')) |
201e6dcf | 143 | setPIDi( gcode->get_value('I') ); |
827a49ca | 144 | if (gcode->has_letter('D')) |
201e6dcf | 145 | setPIDd( gcode->get_value('D') ); |
827a49ca MM |
146 | if (gcode->has_letter('X')) |
147 | this->i_max = gcode->get_value('X'); | |
148 | } | |
10e66797 JM |
149 | //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); |
150 | 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 |
151 | |
152 | } else if (gcode->m == 303) { | |
153 | if (gcode->has_letter('E') && (gcode->get_value('E') == this->pool_index)) { | |
154 | gcode->mark_as_taken(); | |
1ad23cd3 | 155 | float target = 150.0; |
8dfbc976 JM |
156 | if (gcode->has_letter('S')) { |
157 | target = gcode->get_value('S'); | |
827a49ca MM |
158 | gcode->stream->printf("Target: %5.1f\n", target); |
159 | } | |
8dfbc976 JM |
160 | int ncycles= 8; |
161 | if (gcode->has_letter('C')) { | |
162 | ncycles= gcode->get_value('C'); | |
163 | } | |
827a49ca | 164 | gcode->stream->printf("Start PID tune, command is %s\n", gcode->command.c_str()); |
8dfbc976 | 165 | this->pool->PIDtuner->begin(this, target, gcode->stream, ncycles); |
827a49ca | 166 | } |
cf1a7632 | 167 | |
33e4cc02 | 168 | } else if (gcode->m == 500 || gcode->m == 503){// M500 saves some volatile settings to config override file, M503 just prints the settings |
458063f6 | 169 | 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 |
170 | gcode->mark_as_taken(); |
171 | ||
8dfbc976 | 172 | } else if( ( gcode->m == this->set_m_code || gcode->m == this->set_and_wait_m_code ) && gcode->has_letter('S') ) { |
8dfbc976 | 173 | // Attach gcodes to the last block for on_gcode_execute |
e0ee24ed | 174 | THEKERNEL->conveyor->append_gcode(gcode); |
3c4f2dd8 | 175 | } |
b0be67b5 MM |
176 | } |
177 | } | |
db453125 | 178 | |
ded56b35 AW |
179 | void TemperatureControl::on_gcode_execute(void* argument){ |
180 | Gcode* gcode = static_cast<Gcode*>(argument); | |
e6b5ae25 | 181 | if( gcode->has_m){ |
cf1a7632 MM |
182 | if (((gcode->m == this->set_m_code) || (gcode->m == this->set_and_wait_m_code)) |
183 | && gcode->has_letter('S')) | |
907d5e8a | 184 | { |
1ad23cd3 | 185 | float v = gcode->get_value('S'); |
f4bd4fc3 | 186 | |
cf1a7632 | 187 | if (v == 0.0) |
eabf7a9b | 188 | { |
907d5e8a | 189 | this->target_temperature = UNDEFINED; |
7dee00e4 | 190 | this->heater_pin.set(0); |
eabf7a9b MM |
191 | } |
192 | else | |
193 | { | |
f4bd4fc3 | 194 | this->set_desired_temperature(v); |
cf1a7632 MM |
195 | |
196 | if( gcode->m == this->set_and_wait_m_code) | |
197 | { | |
314ab8f7 | 198 | THEKERNEL->pauser->take(); |
cf1a7632 MM |
199 | this->waiting = true; |
200 | } | |
907d5e8a | 201 | } |
df27a6a3 | 202 | } |
df27a6a3 | 203 | } |
ded56b35 AW |
204 | } |
205 | ||
8293d443 | 206 | void TemperatureControl::on_get_public_data(void* argument){ |
b19aa09d | 207 | PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument); |
201e6dcf | 208 | |
b19aa09d JM |
209 | if(!pdr->starts_with(temperature_control_checksum)) return; |
210 | ||
211 | if(!pdr->second_element_is(this->name_checksum)) return; // will be bed or hotend | |
212 | ||
213 | // ok this is targeted at us, so send back the requested data | |
214 | if(pdr->third_element_is(current_temperature_checksum)) { | |
215 | // this must be static as it will be accessed long after we have returned | |
216 | static struct pad_temperature temp_return; | |
217 | temp_return.current_temperature= this->get_temperature(); | |
218 | temp_return.target_temperature= (target_temperature == UNDEFINED) ? 0 : this->target_temperature; | |
219 | temp_return.pwm= this->o; | |
201e6dcf | 220 | |
b19aa09d JM |
221 | pdr->set_data_ptr(&temp_return); |
222 | pdr->set_taken(); | |
223 | } | |
8293d443 | 224 | } |
db453125 | 225 | |
77047e76 | 226 | void TemperatureControl::on_set_public_data(void* argument){ |
991d98cc | 227 | PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument); |
77047e76 | 228 | |
991d98cc | 229 | if(!pdr->starts_with(temperature_control_checksum)) return; |
77047e76 | 230 | |
991d98cc | 231 | if(!pdr->second_element_is(this->name_checksum)) return; // will be bed or hotend |
77047e76 | 232 | |
991d98cc | 233 | // ok this is targeted at us, so set the temp |
1ad23cd3 | 234 | float t= *static_cast<float*>(pdr->get_data_ptr()); |
991d98cc JM |
235 | this->set_desired_temperature(t); |
236 | pdr->set_taken(); | |
77047e76 JM |
237 | } |
238 | ||
1ad23cd3 | 239 | void TemperatureControl::set_desired_temperature(float desired_temperature) |
cf1a7632 MM |
240 | { |
241 | if (desired_temperature == 1.0) | |
242 | desired_temperature = preset1; | |
243 | else if (desired_temperature == 2.0) | |
244 | desired_temperature = preset2; | |
245 | ||
907d5e8a | 246 | target_temperature = desired_temperature; |
959dc7db | 247 | if (desired_temperature == 0.0) |
7dee00e4 | 248 | heater_pin.set((o = 0)); |
ded56b35 AW |
249 | } |
250 | ||
1ad23cd3 | 251 | float TemperatureControl::get_temperature(){ |
907d5e8a | 252 | return last_reading; |
ded56b35 AW |
253 | } |
254 | ||
1ad23cd3 | 255 | float TemperatureControl::adc_value_to_temperature(int adc_value) |
907d5e8a MM |
256 | { |
257 | if ((adc_value == 4095) || (adc_value == 0)) | |
258 | return INFINITY; | |
1ad23cd3 | 259 | float r = r2 / ((4095.0 / adc_value) - 1.0); |
907d5e8a MM |
260 | if (r1 > 0) |
261 | r = (r1 * r) / (r1 - r); | |
262 | return (1.0 / (k + (j * log(r / r0)))) - 273.15; | |
ded56b35 AW |
263 | } |
264 | ||
8b8b3339 | 265 | uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy){ |
907d5e8a MM |
266 | int r = new_thermistor_reading(); |
267 | ||
1ad23cd3 | 268 | float temperature = adc_value_to_temperature(r); |
907d5e8a MM |
269 | |
270 | if (target_temperature > 0) | |
271 | { | |
272 | if ((r <= 1) || (r >= 4094)) | |
36a3f3f0 | 273 | { |
7e57bc2c | 274 | this->min_temp_violated = true; |
907d5e8a | 275 | target_temperature = UNDEFINED; |
7dee00e4 | 276 | heater_pin.set(0); |
36a3f3f0 MM |
277 | } |
278 | else | |
279 | { | |
907d5e8a MM |
280 | pid_process(temperature); |
281 | if ((temperature > target_temperature) && waiting) | |
36a3f3f0 | 282 | { |
347854ff | 283 | THEKERNEL->pauser->release(); |
907d5e8a | 284 | waiting = false; |
81b547a1 | 285 | } |
ded56b35 AW |
286 | } |
287 | } | |
959dc7db MM |
288 | else |
289 | { | |
7dee00e4 | 290 | heater_pin.set((o = 0)); |
959dc7db | 291 | } |
907d5e8a | 292 | last_reading = temperature; |
281967e4 | 293 | return 0; |
ded56b35 AW |
294 | } |
295 | ||
10e66797 JM |
296 | /** |
297 | * Based on https://github.com/br3ttb/Arduino-PID-Library | |
298 | */ | |
1ad23cd3 | 299 | void TemperatureControl::pid_process(float temperature) |
907d5e8a | 300 | { |
1ad23cd3 | 301 | float error = target_temperature - temperature; |
ded56b35 | 302 | |
10e66797 JM |
303 | this->iTerm += (error * this->i_factor); |
304 | if (this->iTerm > this->i_max) this->iTerm = this->i_max; | |
305 | else if (this->iTerm < 0.0) this->iTerm = 0.0; | |
ded56b35 | 306 | |
10e66797 | 307 | if(this->lastInput < 0.0) this->lastInput= temperature; // set first time |
1ad23cd3 | 308 | float d= (temperature - this->lastInput); |
ded56b35 | 309 | |
10e66797 JM |
310 | // calculate the PID output |
311 | // TODO does this need to be scaled by max_pwm/256? I think not as p_factor already does that | |
312 | this->o = (this->p_factor*error) + this->iTerm - (this->d_factor*d); | |
8cdae54c | 313 | |
7dee00e4 | 314 | if (this->o >= heater_pin.max_pwm()) |
7dee00e4 | 315 | this->o = heater_pin.max_pwm(); |
27aecda6 | 316 | else if (this->o < 0) |
907d5e8a MM |
317 | this->o = 0; |
318 | ||
27aecda6 | 319 | this->heater_pin.pwm(this->o); |
10e66797 | 320 | this->lastInput= temperature; |
907d5e8a | 321 | } |
ded56b35 | 322 | |
907d5e8a MM |
323 | int TemperatureControl::new_thermistor_reading() |
324 | { | |
314ab8f7 | 325 | int last_raw = THEKERNEL->adc->read(&thermistor_pin); |
10e66797 | 326 | if (queue.size() >= queue.capacity()) { |
907d5e8a MM |
327 | uint16_t l; |
328 | queue.pop_front(l); | |
907d5e8a | 329 | } |
c4f4cf73 MM |
330 | uint16_t r = last_raw; |
331 | queue.push_back(r); | |
e84c3be3 BG |
332 | for (int i=0; i<queue.size(); i++) |
333 | median_buffer[i] = *queue.get_ref(i); | |
334 | uint16_t m = median_buffer[quick_median(median_buffer, queue.size())]; | |
335 | return m; | |
907d5e8a | 336 | } |
8ccab7cf MM |
337 | |
338 | void TemperatureControl::on_second_tick(void* argument) | |
339 | { | |
340 | if (waiting) | |
347854ff | 341 | THEKERNEL->streams->printf("%s:%3.1f /%3.1f @%d\n", designator.c_str(), get_temperature(), ((target_temperature == UNDEFINED)?0.0:target_temperature), o); |
8ccab7cf | 342 | } |
201e6dcf | 343 | |
1ad23cd3 | 344 | void TemperatureControl::setPIDp(float p) { |
201e6dcf JM |
345 | this->p_factor= p; |
346 | } | |
347 | ||
1ad23cd3 | 348 | void TemperatureControl::setPIDi(float i) { |
201e6dcf JM |
349 | this->i_factor= i*this->PIDdt; |
350 | } | |
351 | ||
1ad23cd3 | 352 | void TemperatureControl::setPIDd(float d) { |
201e6dcf JM |
353 | this->d_factor= d/this->PIDdt; |
354 | } |