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