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