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