Merge remote-tracking branch 'origin/edge' into feature/new_build
[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
18 #include "MRI_Hooks.h"
19
20 TemperatureControl::TemperatureControl(uint16_t name) :
21 name_checksum(name), waiting(false), min_temp_violated(false) {}
22
23 void TemperatureControl::on_module_loaded(){
24
25 // We start not desiring any temp
26 this->target_temperature = UNDEFINED;
27
28 // Settings
29 this->on_config_reload(this);
30
31 this->acceleration_factor = 10;
32
33 // Register for events
34 register_for_event(ON_CONFIG_RELOAD);
35 this->register_for_event(ON_GCODE_EXECUTE);
36 this->register_for_event(ON_GCODE_RECEIVED);
37 this->register_for_event(ON_MAIN_LOOP);
38 this->register_for_event(ON_SECOND_TICK);
39
40 }
41
42 void TemperatureControl::on_main_loop(void* argument){
43 if (this->min_temp_violated) {
44 kernel->streams->printf("MINTEMP triggered on P%d.%d! check your thermistors!\n", this->thermistor_pin.port_number, this->thermistor_pin.pin);
45 this->min_temp_violated = false;
46 }
47 }
48
49 // Get configuration from the config file
50 void TemperatureControl::on_config_reload(void* argument){
51
52 // General config
53 this->set_m_code = this->kernel->config->value(temperature_control_checksum, this->name_checksum, set_m_code_checksum)->by_default(104)->as_number();
54 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();
55 this->get_m_code = this->kernel->config->value(temperature_control_checksum, this->name_checksum, get_m_code_checksum)->by_default(105)->as_number();
56 this->readings_per_second = this->kernel->config->value(temperature_control_checksum, this->name_checksum, readings_per_second_checksum)->by_default(20)->as_number();
57
58 this->designator = this->kernel->config->value(temperature_control_checksum, this->name_checksum, designator_checksum)->by_default(string("T"))->as_string();
59
60 // Values are here : http://reprap.org/wiki/Thermistor
61 this->r0 = 100000;
62 this->t0 = 25;
63 this->beta = 4066;
64 this->r1 = 0;
65 this->r2 = 4700;
66
67 // Preset values for various common types of thermistors
68 ConfigValue* thermistor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, thermistor_checksum);
69 if( thermistor->value.compare("EPCOS100K" ) == 0 ){ // Default
70 }else if( thermistor->value.compare("RRRF100K" ) == 0 ){ this->beta = 3960;
71 }else if( thermistor->value.compare("RRRF10K" ) == 0 ){ this->beta = 3964; this->r0 = 10000; this->r1 = 680; this->r2 = 1600;
72 }else if( thermistor->value.compare("Honeywell100K") == 0 ){ this->beta = 3974;
73 }else if( thermistor->value.compare("Semitec" ) == 0 ){ this->beta = 4267; }
74
75 // Preset values are overriden by specified values
76 this->r0 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, r0_checksum )->by_default(this->r0 )->as_number(); // Stated resistance eg. 100K
77 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
78 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
79 this->r1 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, r1_checksum )->by_default(this->r1 )->as_number();
80 this->r2 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, r2_checksum )->by_default(this->r2 )->as_number();
81
82
83 // Thermistor math
84 j = (1.0 / beta);
85 k = (1.0 / (t0 + 273.15));
86
87 // sigma-delta output modulation
88 o = 0;
89
90 // Thermistor pin for ADC readings
91 this->thermistor_pin.from_string(this->kernel->config->value(temperature_control_checksum, this->name_checksum, thermistor_pin_checksum )->required()->as_string());
92 this->kernel->adc->enable_pin(&thermistor_pin);
93
94 // Heater pin
95 this->heater_pin.from_string( this->kernel->config->value(temperature_control_checksum, this->name_checksum, heater_pin_checksum)->required()->as_string())->as_output();
96 this->heater_pin.max_pwm( this->kernel->config->value(temperature_control_checksum, this->name_checksum, max_pwm_checksum)->by_default(255)->as_number() );
97 this->heater_pin.set(0);
98
99 set_low_on_debug(heater_pin.port_number, heater_pin.pin);
100
101 // activate SD-DAC timer
102 this->kernel->slow_ticker->attach(1000, &heater_pin, &Pwm::on_tick);
103
104 // reading tick
105 this->kernel->slow_ticker->attach( this->readings_per_second, this, &TemperatureControl::thermistor_read_tick );
106
107 // PID
108 this->p_factor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, p_factor_checksum)->by_default(10 )->as_number();
109 this->i_factor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, i_factor_checksum)->by_default(0.3)->as_number();
110 this->d_factor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, d_factor_checksum)->by_default(200)->as_number();
111 this->i_max = this->kernel->config->value(temperature_control_checksum, this->name_checksum, i_max_checksum )->by_default(255)->as_number();
112 this->i = 0.0;
113 this->last_reading = 0.0;
114 }
115
116 void TemperatureControl::on_gcode_received(void* argument)
117 {
118 Gcode* gcode = static_cast<Gcode*>(argument);
119 if (gcode->has_m)
120 {
121 // Get temperature
122 if( gcode->m == this->get_m_code ){
123 gcode->stream->printf("%s:%3.1f /%3.1f @%d ", this->designator.c_str(), this->get_temperature(), ((target_temperature == UNDEFINED)?0.0:target_temperature), this->o);
124 gcode->add_nl = true;
125 }
126 if (gcode->m == 301)
127 {
128 if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index))
129 {
130 if (gcode->has_letter('P'))
131 this->p_factor = gcode->get_value('P');
132 if (gcode->has_letter('I'))
133 this->i_factor = gcode->get_value('I');
134 if (gcode->has_letter('D'))
135 this->d_factor = gcode->get_value('D');
136 if (gcode->has_letter('X'))
137 this->i_max = gcode->get_value('X');
138 }
139 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);
140 }
141 if (gcode->m == 303)
142 {
143 if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index))
144 {
145 double target = 150.0;
146 if (gcode->has_letter('P'))
147 {
148 target = gcode->get_value('P');
149 gcode->stream->printf("Target: %5.1f\n", target);
150 }
151 gcode->stream->printf("Start PID tune, command is %s\n", gcode->command.c_str());
152 this->pool->PIDtuner->begin(this, target, gcode->stream);
153 }
154 }
155 }
156 }
157
158 void TemperatureControl::on_gcode_execute(void* argument){
159 Gcode* gcode = static_cast<Gcode*>(argument);
160 if( gcode->has_m){
161 // Set temperature without waiting
162 if( gcode->m == this->set_m_code && gcode->has_letter('S') )
163 {
164 if (gcode->get_value('S') == 0)
165 {
166 this->target_temperature = UNDEFINED;
167 this->heater_pin.set(0);
168 }
169 else
170 {
171 this->set_desired_temperature(gcode->get_value('S'));
172 }
173 }
174 // Set temperature and wait
175 if( gcode->m == this->set_and_wait_m_code && gcode->has_letter('S') )
176 {
177 if (gcode->get_value('S') == 0)
178 {
179 this->target_temperature = UNDEFINED;
180 this->heater_pin.set(0);
181 }
182 else
183 {
184 this->set_desired_temperature(gcode->get_value('S'));
185 // Pause
186 this->kernel->pauser->take();
187 this->waiting = true;
188 }
189 }
190 }
191 }
192
193
194 void TemperatureControl::set_desired_temperature(double desired_temperature){
195 target_temperature = desired_temperature;
196 if (desired_temperature == 0.0)
197 heater_pin.set((o = 0));
198 }
199
200 double TemperatureControl::get_temperature(){
201 return last_reading;
202 }
203
204 double TemperatureControl::adc_value_to_temperature(int adc_value)
205 {
206 if ((adc_value == 4095) || (adc_value == 0))
207 return INFINITY;
208 double r = r2 / ((4095.0 / adc_value) - 1.0);
209 if (r1 > 0)
210 r = (r1 * r) / (r1 - r);
211 return (1.0 / (k + (j * log(r / r0)))) - 273.15;
212 }
213
214 uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy){
215 int r = new_thermistor_reading();
216
217 double temperature = adc_value_to_temperature(r);
218
219 if (target_temperature > 0)
220 {
221 if ((r <= 1) || (r >= 4094))
222 {
223 this->min_temp_violated = true;
224 target_temperature = UNDEFINED;
225 heater_pin.set(0);
226 }
227 else
228 {
229 pid_process(temperature);
230 if ((temperature > target_temperature) && waiting)
231 {
232 kernel->pauser->release();
233 waiting = false;
234 }
235 }
236 }
237 else
238 {
239 heater_pin.set((o = 0));
240 }
241 last_reading = temperature;
242 return 0;
243 }
244
245 void TemperatureControl::pid_process(double temperature)
246 {
247 double error = target_temperature - temperature;
248
249 p = error * p_factor;
250 i += (error * this->i_factor);
251 // d was imbued with oldest_raw earlier in new_thermistor_reading
252 d = adc_value_to_temperature(d);
253 d = (d - temperature) * this->d_factor;
254
255 if (i > this->i_max)
256 i = this->i_max;
257 if (i < -this->i_max)
258 i = -this->i_max;
259
260 this->o = (p + i + d) * heater_pin.max_pwm() / 256;
261
262 if (this->o >= heater_pin.max_pwm())
263 {
264 i = 0;
265 this->o = heater_pin.max_pwm();
266 }
267 if (this->o < 0)
268 {
269 if (this->o < -(heater_pin.max_pwm()))
270 i = 0;
271 this->o = 0;
272 }
273
274 this->heater_pin.pwm(o);
275 }
276
277 int TemperatureControl::new_thermistor_reading()
278 {
279 int last_raw = this->kernel->adc->read(&thermistor_pin);
280 if (queue.size() >= queue.capacity())
281 {
282 uint16_t l;
283 queue.pop_front(l);
284 d = l;
285 }
286 uint16_t r = last_raw;
287 queue.push_back(r);
288 for (int i=0; i<queue.size(); i++)
289 median_buffer[i] = *queue.get_ref(i);
290 uint16_t m = median_buffer[quick_median(median_buffer, queue.size())];
291 return m;
292 }
293
294 void TemperatureControl::on_second_tick(void* argument)
295 {
296 if (waiting)
297 kernel->streams->printf("%s:%3.1f /%3.1f @%d\n", designator.c_str(), get_temperature(), ((target_temperature == UNDEFINED)?0.0:target_temperature), o);
298 }