Setting the I and D values needs to be scaled by the sample time.
[clinton/Smoothieware.git] / src / modules / tools / temperaturecontrol / TemperatureControl.cpp
1 /*
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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8 // TODO : THIS FILE IS LAME, MUST BE MADE MUCH BETTER
9
10 #include "libs/Module.h"
11 #include "libs/Kernel.h"
12 #include <math.h>
13 #include "TemperatureControl.h"
14 #include "TemperatureControlPool.h"
15 #include "libs/Pin.h"
16 #include "libs/Median.h"
17 #include "modules/robot/Conveyor.h"
18 #include "PublicDataRequest.h"
19
20 #include "MRI_Hooks.h"
21
22 TemperatureControl::TemperatureControl(uint16_t name) :
23 name_checksum(name), waiting(false), min_temp_violated(false) {}
24
25 void TemperatureControl::on_module_loaded(){
26
27 // We start not desiring any temp
28 this->target_temperature = UNDEFINED;
29
30 // Settings
31 this->on_config_reload(this);
32
33 this->acceleration_factor = 10;
34
35 // Register for events
36 register_for_event(ON_CONFIG_RELOAD);
37 this->register_for_event(ON_GCODE_EXECUTE);
38 this->register_for_event(ON_GCODE_RECEIVED);
39 this->register_for_event(ON_MAIN_LOOP);
40 this->register_for_event(ON_SECOND_TICK);
41 this->register_for_event(ON_GET_PUBLIC_DATA);
42 this->register_for_event(ON_SET_PUBLIC_DATA);
43 }
44
45 void TemperatureControl::on_main_loop(void* argument){
46 if (this->min_temp_violated) {
47 kernel->streams->printf("Error: MINTEMP triggered on P%d.%d! check your thermistors!\n", this->thermistor_pin.port_number, this->thermistor_pin.pin);
48 this->min_temp_violated = false;
49 }
50 }
51
52 // Get configuration from the config file
53 void TemperatureControl::on_config_reload(void* argument){
54
55 // General config
56 this->set_m_code = this->kernel->config->value(temperature_control_checksum, this->name_checksum, set_m_code_checksum)->by_default(104)->as_number();
57 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();
58 this->get_m_code = this->kernel->config->value(temperature_control_checksum, this->name_checksum, get_m_code_checksum)->by_default(105)->as_number();
59 this->readings_per_second = this->kernel->config->value(temperature_control_checksum, this->name_checksum, readings_per_second_checksum)->by_default(20)->as_number();
60
61 this->designator = this->kernel->config->value(temperature_control_checksum, this->name_checksum, designator_checksum)->by_default(string("T"))->as_string();
62
63 // Values are here : http://reprap.org/wiki/Thermistor
64 this->r0 = 100000;
65 this->t0 = 25;
66 this->beta = 4066;
67 this->r1 = 0;
68 this->r2 = 4700;
69
70 // Preset values for various common types of thermistors
71 ConfigValue* thermistor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, thermistor_checksum);
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;
76 }else if( thermistor->value.compare("Semitec" ) == 0 ){ this->beta = 4267;
77 }else if( thermistor->value.compare("HT100K" ) == 0 ){ this->beta = 3990; }
78
79 // Preset values are overriden by specified values
80 this->r0 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, r0_checksum )->by_default(this->r0 )->as_number(); // Stated resistance eg. 100K
81 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
82 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
83 this->r1 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, r1_checksum )->by_default(this->r1 )->as_number();
84 this->r2 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, r2_checksum )->by_default(this->r2 )->as_number();
85
86 this->preset1 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, preset1_checksum)->by_default(0)->as_number();
87 this->preset2 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, preset2_checksum)->by_default(0)->as_number();
88
89
90 // Thermistor math
91 j = (1.0 / beta);
92 k = (1.0 / (t0 + 273.15));
93
94 // sigma-delta output modulation
95 o = 0;
96
97 // Thermistor pin for ADC readings
98 this->thermistor_pin.from_string(this->kernel->config->value(temperature_control_checksum, this->name_checksum, thermistor_pin_checksum )->required()->as_string());
99 this->kernel->adc->enable_pin(&thermistor_pin);
100
101 // Heater pin
102 this->heater_pin.from_string( this->kernel->config->value(temperature_control_checksum, this->name_checksum, heater_pin_checksum)->required()->as_string())->as_output();
103 this->heater_pin.max_pwm( this->kernel->config->value(temperature_control_checksum, this->name_checksum, max_pwm_checksum)->by_default(255)->as_number() );
104 this->heater_pin.set(0);
105
106 set_low_on_debug(heater_pin.port_number, heater_pin.pin);
107
108 // activate SD-DAC timer
109 this->kernel->slow_ticker->attach( this->kernel->config->value(temperature_control_checksum, this->name_checksum, pwm_frequency_checksum)->by_default(2000)->as_number() , &heater_pin, &Pwm::on_tick);
110
111 // reading tick
112 this->kernel->slow_ticker->attach( this->readings_per_second, this, &TemperatureControl::thermistor_read_tick );
113 this->PIDdt= 1.0 / this->readings_per_second;
114
115 // PID
116 setPIDp( this->kernel->config->value(temperature_control_checksum, this->name_checksum, p_factor_checksum)->by_default(10 )->as_number() );
117 setPIDi( this->kernel->config->value(temperature_control_checksum, this->name_checksum, i_factor_checksum)->by_default(0.3)->as_number() );
118 setPIDd( this->kernel->config->value(temperature_control_checksum, this->name_checksum, d_factor_checksum)->by_default(200)->as_number() );
119 this->i_max = this->kernel->config->value(temperature_control_checksum, this->name_checksum, i_max_checksum )->by_default(255)->as_number();
120 this->i = 0.0;
121 this->last_reading = 0.0;
122 }
123
124 void TemperatureControl::on_gcode_received(void* argument){
125 Gcode* gcode = static_cast<Gcode*>(argument);
126 if (gcode->has_m)
127 {
128 // Get temperature
129 if( gcode->m == this->get_m_code ){
130 char buf[32]; // should be big enough for any status
131 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);
132 gcode->txt_after_ok.append(buf, n);
133 gcode->mark_as_taken();
134 }
135 if (gcode->m == 301)
136 {
137 gcode->mark_as_taken();
138 if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index))
139 {
140 if (gcode->has_letter('P'))
141 setPIDp( gcode->get_value('P') );
142 if (gcode->has_letter('I'))
143 setPIDi( gcode->get_value('I') );
144 if (gcode->has_letter('D'))
145 setPIDd( gcode->get_value('D') );
146 if (gcode->has_letter('X'))
147 this->i_max = gcode->get_value('X');
148 }
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 }
151 if (gcode->m == 303)
152 {
153 gcode->mark_as_taken();
154 if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index))
155 {
156 double target = 150.0;
157 if (gcode->has_letter('P'))
158 {
159 target = gcode->get_value('P');
160 gcode->stream->printf("Target: %5.1f\n", target);
161 }
162 gcode->stream->printf("Start PID tune, command is %s\n", gcode->command.c_str());
163 this->pool->PIDtuner->begin(this, target, gcode->stream);
164 }
165 }
166
167 // Attach gcodes to the last block for on_gcode_execute
168 if( ( gcode->m == this->set_m_code || gcode->m == this->set_and_wait_m_code ) && gcode->has_letter('S') ){
169 if( this->kernel->conveyor->queue.size() == 0 ){
170 this->kernel->call_event(ON_GCODE_EXECUTE, gcode );
171 }else{
172 Block* block = this->kernel->conveyor->queue.get_ref( this->kernel->conveyor->queue.size() - 1 );
173 block->append_gcode(gcode);
174 }
175
176 }
177 }
178 }
179
180 void TemperatureControl::on_gcode_execute(void* argument){
181 Gcode* gcode = static_cast<Gcode*>(argument);
182 if( gcode->has_m){
183 if (((gcode->m == this->set_m_code) || (gcode->m == this->set_and_wait_m_code))
184 && gcode->has_letter('S'))
185 {
186 double v = gcode->get_value('S');
187
188 if (v == 0.0)
189 {
190 this->target_temperature = UNDEFINED;
191 this->heater_pin.set(0);
192 }
193 else
194 {
195 this->set_desired_temperature(v);
196
197 if( gcode->m == this->set_and_wait_m_code)
198 {
199 gcode->mark_as_taken();
200 this->kernel->pauser->take();
201 this->waiting = true;
202 }
203 }
204 }
205 }
206 }
207
208 void TemperatureControl::on_get_public_data(void* argument){
209 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
210
211 if(!pdr->starts_with(temperature_control_checksum)) return;
212
213 if(!pdr->second_element_is(this->name_checksum)) return; // will be bed or hotend
214
215 // ok this is targeted at us, so send back the requested data
216 if(pdr->third_element_is(current_temperature_checksum)) {
217 // this must be static as it will be accessed long after we have returned
218 static struct pad_temperature temp_return;
219 temp_return.current_temperature= this->get_temperature();
220 temp_return.target_temperature= (target_temperature == UNDEFINED) ? 0 : this->target_temperature;
221 temp_return.pwm= this->o;
222
223 pdr->set_data_ptr(&temp_return);
224 pdr->set_taken();
225 }
226 }
227
228 void TemperatureControl::on_set_public_data(void* argument){
229 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
230
231 if(!pdr->starts_with(temperature_control_checksum)) return;
232
233 if(!pdr->second_element_is(this->name_checksum)) return; // will be bed or hotend
234
235 // ok this is targeted at us, so set the temp
236 double t= *static_cast<double*>(pdr->get_data_ptr());
237 this->set_desired_temperature(t);
238 pdr->set_taken();
239 }
240
241 void TemperatureControl::set_desired_temperature(double desired_temperature)
242 {
243 if (desired_temperature == 1.0)
244 desired_temperature = preset1;
245 else if (desired_temperature == 2.0)
246 desired_temperature = preset2;
247
248 target_temperature = desired_temperature;
249 if (desired_temperature == 0.0)
250 heater_pin.set((o = 0));
251 }
252
253 double TemperatureControl::get_temperature(){
254 return last_reading;
255 }
256
257 double TemperatureControl::adc_value_to_temperature(int adc_value)
258 {
259 if ((adc_value == 4095) || (adc_value == 0))
260 return INFINITY;
261 double r = r2 / ((4095.0 / adc_value) - 1.0);
262 if (r1 > 0)
263 r = (r1 * r) / (r1 - r);
264 return (1.0 / (k + (j * log(r / r0)))) - 273.15;
265 }
266
267 uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy){
268 int r = new_thermistor_reading();
269
270 double temperature = adc_value_to_temperature(r);
271
272 if (target_temperature > 0)
273 {
274 if ((r <= 1) || (r >= 4094))
275 {
276 this->min_temp_violated = true;
277 target_temperature = UNDEFINED;
278 heater_pin.set(0);
279 }
280 else
281 {
282 pid_process(temperature);
283 if ((temperature > target_temperature) && waiting)
284 {
285 kernel->pauser->release();
286 waiting = false;
287 }
288 }
289 }
290 else
291 {
292 heater_pin.set((o = 0));
293 }
294 last_reading = temperature;
295 return 0;
296 }
297
298 void TemperatureControl::pid_process(double temperature)
299 {
300 double error = target_temperature - temperature;
301
302 p = error * p_factor;
303 i += (error * this->i_factor);
304 // d was imbued with oldest_raw earlier in new_thermistor_reading
305 d = adc_value_to_temperature(d);
306 d = (d - temperature) * this->d_factor;
307
308 if (i > this->i_max)
309 i = this->i_max;
310 if (i < -this->i_max)
311 i = -this->i_max;
312
313 this->o = (p + i + d) * heater_pin.max_pwm() / 256;
314
315 if (this->o >= heater_pin.max_pwm())
316 this->o = heater_pin.max_pwm();
317 else if (this->o < 0)
318 this->o = 0;
319
320 this->heater_pin.pwm(this->o);
321 }
322
323 int TemperatureControl::new_thermistor_reading()
324 {
325 int last_raw = this->kernel->adc->read(&thermistor_pin);
326 if (queue.size() >= queue.capacity())
327 {
328 uint16_t l;
329 queue.pop_front(l);
330 d = l;
331 }
332 uint16_t r = last_raw;
333 queue.push_back(r);
334 for (int i=0; i<queue.size(); i++)
335 median_buffer[i] = *queue.get_ref(i);
336 uint16_t m = median_buffer[quick_median(median_buffer, queue.size())];
337 return m;
338 }
339
340 void TemperatureControl::on_second_tick(void* argument)
341 {
342 if (waiting)
343 kernel->streams->printf("%s:%3.1f /%3.1f @%d\n", designator.c_str(), get_temperature(), ((target_temperature == UNDEFINED)?0.0:target_temperature), o);
344 }
345
346 void TemperatureControl::setPIDp(double p) {
347 this->p_factor= p;
348 }
349
350 void TemperatureControl::setPIDi(double i) {
351 this->i_factor= i*this->PIDdt;
352 }
353
354 void TemperatureControl::setPIDd(double d) {
355 this->d_factor= d/this->PIDdt;
356 }