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