Merge branch 'edge' into extruderfix
[clinton/Smoothieware.git] / src / modules / tools / temperaturecontrol / TemperatureControl.cpp
CommitLineData
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"
ded56b35 18
8f91e4e6
MM
19#include "MRI_Hooks.h"
20
7e57bc2c
BG
21TemperatureControl::TemperatureControl(uint16_t name) :
22 name_checksum(name), waiting(false), min_temp_violated(false) {}
ded56b35
AW
23
24void TemperatureControl::on_module_loaded(){
907d5e8a 25
81b547a1 26 // We start not desiring any temp
907d5e8a 27 this->target_temperature = UNDEFINED;
ded56b35
AW
28
29 // Settings
7dd8133c 30 this->on_config_reload(this);
ded56b35
AW
31
32 this->acceleration_factor = 10;
33
ded56b35 34 // Register for events
476dcb96 35 register_for_event(ON_CONFIG_RELOAD);
df27a6a3 36 this->register_for_event(ON_GCODE_EXECUTE);
b0be67b5 37 this->register_for_event(ON_GCODE_RECEIVED);
df27a6a3 38 this->register_for_event(ON_MAIN_LOOP);
8ccab7cf 39 this->register_for_event(ON_SECOND_TICK);
ded56b35
AW
40
41}
42
7e57bc2c
BG
43void 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;
a7f12bed 47 }
7e57bc2c 48}
7dd8133c
AW
49
50// Get configuration from the config file
51void TemperatureControl::on_config_reload(void* argument){
52
1306ba99
AW
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();
f39da6fe 57 this->readings_per_second = this->kernel->config->value(temperature_control_checksum, this->name_checksum, readings_per_second_checksum)->by_default(20)->as_number();
7dee00e4 58
b0be67b5
MM
59 this->designator = this->kernel->config->value(temperature_control_checksum, this->name_checksum, designator_checksum)->by_default(string("T"))->as_string();
60
7dd8133c 61 // Values are here : http://reprap.org/wiki/Thermistor
423df6df
AW
62 this->r0 = 100000;
63 this->t0 = 25;
a98f72da 64 this->beta = 4066;
423df6df
AW
65 this->r1 = 0;
66 this->r2 = 4700;
a98f72da 67
3c132bd0 68 // Preset values for various common types of thermistors
df27a6a3 69 ConfigValue* thermistor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, thermistor_checksum);
423df6df
AW
70 if( thermistor->value.compare("EPCOS100K" ) == 0 ){ // Default
71 }else if( thermistor->value.compare("RRRF100K" ) == 0 ){ this->beta = 3960;
72 }else if( thermistor->value.compare("RRRF10K" ) == 0 ){ this->beta = 3964; this->r0 = 10000; this->r1 = 680; this->r2 = 1600;
73 }else if( thermistor->value.compare("Honeywell100K") == 0 ){ this->beta = 3974;
74 }else if( thermistor->value.compare("Semitec" ) == 0 ){ this->beta = 4267; }
a98f72da 75
3c132bd0 76 // Preset values are overriden by specified values
907d5e8a
MM
77 this->r0 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, r0_checksum )->by_default(this->r0 )->as_number(); // Stated resistance eg. 100K
78 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
79 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
80 this->r1 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, r1_checksum )->by_default(this->r1 )->as_number();
81 this->r2 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, r2_checksum )->by_default(this->r2 )->as_number();
82
83
df27a6a3 84 // Thermistor math
907d5e8a
MM
85 j = (1.0 / beta);
86 k = (1.0 / (t0 + 273.15));
87
c4f4cf73
MM
88 // sigma-delta output modulation
89 o = 0;
3c132bd0
AW
90
91 // Thermistor pin for ADC readings
7dee00e4
MM
92 this->thermistor_pin.from_string(this->kernel->config->value(temperature_control_checksum, this->name_checksum, thermistor_pin_checksum )->required()->as_string());
93 this->kernel->adc->enable_pin(&thermistor_pin);
3c132bd0
AW
94
95 // Heater pin
7dee00e4 96 this->heater_pin.from_string( this->kernel->config->value(temperature_control_checksum, this->name_checksum, heater_pin_checksum)->required()->as_string())->as_output();
a7f12bed 97 this->heater_pin.max_pwm( this->kernel->config->value(temperature_control_checksum, this->name_checksum, max_pwm_checksum)->by_default(255)->as_number() );
7dee00e4 98 this->heater_pin.set(0);
7dd8133c 99
cb3460e9 100 set_low_on_debug(heater_pin.port_number, heater_pin.pin);
8f91e4e6 101
907d5e8a 102 // activate SD-DAC timer
7dee00e4 103 this->kernel->slow_ticker->attach(1000, &heater_pin, &Pwm::on_tick);
907d5e8a 104
f39da6fe
MM
105 // reading tick
106 this->kernel->slow_ticker->attach( this->readings_per_second, this, &TemperatureControl::thermistor_read_tick );
107
907d5e8a
MM
108 // PID
109 this->p_factor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, p_factor_checksum)->by_default(10 )->as_number();
b6a72b99
MM
110 this->i_factor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, i_factor_checksum)->by_default(0.3)->as_number();
111 this->d_factor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, d_factor_checksum)->by_default(200)->as_number();
4b9388c2 112 this->i_max = this->kernel->config->value(temperature_control_checksum, this->name_checksum, i_max_checksum )->by_default(255)->as_number();
b6a72b99 113 this->i = 0.0;
907d5e8a 114 this->last_reading = 0.0;
7dd8133c
AW
115}
116
3c4f2dd8 117void TemperatureControl::on_gcode_received(void* argument){
b0be67b5
MM
118 Gcode* gcode = static_cast<Gcode*>(argument);
119 if (gcode->has_m)
120 {
121 // Get temperature
122 if( gcode->m == this->get_m_code ){
c4f4cf73 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);
ac9f9b7a 124 gcode->add_nl = true;
b0be67b5 125 }
827a49ca
MM
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 }
3c4f2dd8
AW
155
156 // Attach gcodes to the last block for on_gcode_execute
157 if( ( gcode->m == this->set_m_code || gcode->m == this->set_and_wait_m_code ) && gcode->has_letter('S') ){
158 if( this->kernel->conveyor->queue.size() == 0 ){
159 this->kernel->call_event(ON_GCODE_EXECUTE, gcode );
160 }else{
161 Block* block = this->kernel->conveyor->queue.get_ref( this->kernel->conveyor->queue.size() - 1 );
162 block->append_gcode(gcode);
163 gcode->queued++;
164 }
165 }
b0be67b5
MM
166 }
167}
db453125 168
ded56b35
AW
169void TemperatureControl::on_gcode_execute(void* argument){
170 Gcode* gcode = static_cast<Gcode*>(argument);
e6b5ae25
AW
171 if( gcode->has_m){
172 // Set temperature without waiting
907d5e8a
MM
173 if( gcode->m == this->set_m_code && gcode->has_letter('S') )
174 {
eabf7a9b
MM
175 if (gcode->get_value('S') == 0)
176 {
907d5e8a 177 this->target_temperature = UNDEFINED;
7dee00e4 178 this->heater_pin.set(0);
eabf7a9b
MM
179 }
180 else
181 {
907d5e8a
MM
182 this->set_desired_temperature(gcode->get_value('S'));
183 }
eabf7a9b 184 }
e6b5ae25 185 // Set temperature and wait
907d5e8a
MM
186 if( gcode->m == this->set_and_wait_m_code && gcode->has_letter('S') )
187 {
eabf7a9b
MM
188 if (gcode->get_value('S') == 0)
189 {
907d5e8a 190 this->target_temperature = UNDEFINED;
7dee00e4 191 this->heater_pin.set(0);
eabf7a9b
MM
192 }
193 else
194 {
907d5e8a
MM
195 this->set_desired_temperature(gcode->get_value('S'));
196 // Pause
197 this->kernel->pauser->take();
198 this->waiting = true;
199 }
df27a6a3 200 }
df27a6a3 201 }
ded56b35
AW
202}
203
db453125 204
ded56b35 205void TemperatureControl::set_desired_temperature(double desired_temperature){
907d5e8a 206 target_temperature = desired_temperature;
959dc7db 207 if (desired_temperature == 0.0)
7dee00e4 208 heater_pin.set((o = 0));
ded56b35
AW
209}
210
211double TemperatureControl::get_temperature(){
907d5e8a 212 return last_reading;
ded56b35
AW
213}
214
907d5e8a
MM
215double TemperatureControl::adc_value_to_temperature(int adc_value)
216{
217 if ((adc_value == 4095) || (adc_value == 0))
218 return INFINITY;
219 double r = r2 / ((4095.0 / adc_value) - 1.0);
220 if (r1 > 0)
221 r = (r1 * r) / (r1 - r);
222 return (1.0 / (k + (j * log(r / r0)))) - 273.15;
ded56b35
AW
223}
224
8b8b3339 225uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy){
907d5e8a
MM
226 int r = new_thermistor_reading();
227
228 double temperature = adc_value_to_temperature(r);
229
230 if (target_temperature > 0)
231 {
232 if ((r <= 1) || (r >= 4094))
36a3f3f0 233 {
7e57bc2c 234 this->min_temp_violated = true;
907d5e8a 235 target_temperature = UNDEFINED;
7dee00e4 236 heater_pin.set(0);
36a3f3f0
MM
237 }
238 else
239 {
907d5e8a
MM
240 pid_process(temperature);
241 if ((temperature > target_temperature) && waiting)
36a3f3f0 242 {
907d5e8a
MM
243 kernel->pauser->release();
244 waiting = false;
81b547a1 245 }
ded56b35
AW
246 }
247 }
959dc7db
MM
248 else
249 {
7dee00e4 250 heater_pin.set((o = 0));
959dc7db 251 }
907d5e8a 252 last_reading = temperature;
281967e4 253 return 0;
ded56b35
AW
254}
255
907d5e8a
MM
256void TemperatureControl::pid_process(double temperature)
257{
258 double error = target_temperature - temperature;
ded56b35 259
8cdae54c 260 p = error * p_factor;
b6a72b99 261 i += (error * this->i_factor);
827a49ca
MM
262 // d was imbued with oldest_raw earlier in new_thermistor_reading
263 d = adc_value_to_temperature(d);
264 d = (d - temperature) * this->d_factor;
ded56b35 265
907d5e8a
MM
266 if (i > this->i_max)
267 i = this->i_max;
268 if (i < -this->i_max)
269 i = -this->i_max;
ded56b35 270
7dee00e4 271 this->o = (p + i + d) * heater_pin.max_pwm() / 256;
8cdae54c 272
7dee00e4 273 if (this->o >= heater_pin.max_pwm())
8cdae54c 274 {
95b3d2ba 275 i = 0;
7dee00e4 276 this->o = heater_pin.max_pwm();
8cdae54c 277 }
907d5e8a 278 if (this->o < 0)
8cdae54c 279 {
7dee00e4 280 if (this->o < -(heater_pin.max_pwm()))
95b3d2ba 281 i = 0;
907d5e8a 282 this->o = 0;
8cdae54c 283 }
907d5e8a 284
7dee00e4 285 this->heater_pin.pwm(o);
907d5e8a 286}
ded56b35 287
907d5e8a
MM
288int TemperatureControl::new_thermistor_reading()
289{
7dee00e4 290 int last_raw = this->kernel->adc->read(&thermistor_pin);
907d5e8a
MM
291 if (queue.size() >= queue.capacity())
292 {
293 uint16_t l;
294 queue.pop_front(l);
827a49ca 295 d = l;
907d5e8a 296 }
c4f4cf73
MM
297 uint16_t r = last_raw;
298 queue.push_back(r);
e84c3be3
BG
299 for (int i=0; i<queue.size(); i++)
300 median_buffer[i] = *queue.get_ref(i);
301 uint16_t m = median_buffer[quick_median(median_buffer, queue.size())];
302 return m;
907d5e8a 303}
8ccab7cf
MM
304
305void TemperatureControl::on_second_tick(void* argument)
306{
307 if (waiting)
8f91e4e6 308 kernel->streams->printf("%s:%3.1f /%3.1f @%d\n", designator.c_str(), get_temperature(), ((target_temperature == UNDEFINED)?0.0:target_temperature), o);
8ccab7cf 309}