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/>.
8 // TODO : THIS FILE IS LAME, MUST BE MADE MUCH BETTER
10 #include "libs/Module.h"
11 #include "libs/Kernel.h"
13 #include "TemperatureControl.h"
14 #include "TemperatureControlPool.h"
16 #include "libs/Median.h"
18 TemperatureControl::TemperatureControl(){}
20 TemperatureControl::TemperatureControl(uint16_t name
){
21 this->name_checksum
= name
;
22 // this->error_count = 0;
23 this->waiting
= false;
26 void TemperatureControl::on_module_loaded(){
28 // We start not desiring any temp
29 this->target_temperature
= UNDEFINED
;
32 this->on_config_reload(this);
34 this->acceleration_factor
= 10;
36 // Register for events
37 this->register_for_event(ON_GCODE_EXECUTE
);
38 this->register_for_event(ON_GCODE_RECEIVED
);
39 this->register_for_event(ON_MAIN_LOOP
);
40 this->register_for_event(ON_SECOND_TICK
);
44 void TemperatureControl::on_main_loop(void* argument
){ }
46 // Get configuration from the config file
47 void TemperatureControl::on_config_reload(void* argument
){
50 this->set_m_code
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, set_m_code_checksum
)->by_default(104)->as_number();
51 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();
52 this->get_m_code
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, get_m_code_checksum
)->by_default(105)->as_number();
53 this->readings_per_second
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, readings_per_second_checksum
)->by_default(20)->as_number();
55 this->designator
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, designator_checksum
)->by_default(string("T"))->as_string();
57 // Values are here : http://reprap.org/wiki/Thermistor
64 // Preset values for various common types of thermistors
65 ConfigValue
* thermistor
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, thermistor_checksum
);
66 if( thermistor
->value
.compare("EPCOS100K" ) == 0 ){ // Default
67 }else if( thermistor
->value
.compare("RRRF100K" ) == 0 ){ this->beta
= 3960;
68 }else if( thermistor
->value
.compare("RRRF10K" ) == 0 ){ this->beta
= 3964; this->r0
= 10000; this->r1
= 680; this->r2
= 1600;
69 }else if( thermistor
->value
.compare("Honeywell100K") == 0 ){ this->beta
= 3974;
70 }else if( thermistor
->value
.compare("Semitec" ) == 0 ){ this->beta
= 4267; }
72 // Preset values are overriden by specified values
73 this->r0
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, r0_checksum
)->by_default(this->r0
)->as_number(); // Stated resistance eg. 100K
74 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
75 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
76 this->r1
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, r1_checksum
)->by_default(this->r1
)->as_number();
77 this->r2
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, r2_checksum
)->by_default(this->r2
)->as_number();
82 k
= (1.0 / (t0
+ 273.15));
84 // sigma-delta output modulation
87 // Thermistor pin for ADC readings
88 this->thermistor_pin
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, thermistor_pin_checksum
)->required()->as_pin();
89 this->kernel
->adc
->enable_pin(this->thermistor_pin
);
92 this->heater_pin
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, heater_pin_checksum
)->required()->as_pwm()->as_output();
93 this->heater_pin
->set(0);
95 // activate SD-DAC timer
96 this->kernel
->slow_ticker
->attach(1000, this->heater_pin
, &Pwm::on_tick
);
99 this->kernel
->slow_ticker
->attach( this->readings_per_second
, this, &TemperatureControl::thermistor_read_tick
);
102 this->p_factor
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, p_factor_checksum
)->by_default(10 )->as_number();
103 this->i_factor
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, i_factor_checksum
)->by_default(0.3)->as_number();
104 this->d_factor
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, d_factor_checksum
)->by_default(200)->as_number();
105 this->i_max
= this->kernel
->config
->value(temperature_control_checksum
, this->name_checksum
, i_max_checksum
)->by_default(255)->as_number();
107 this->last_reading
= 0.0;
110 void TemperatureControl::on_gcode_received(void* argument
)
112 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
116 if( gcode
->m
== this->get_m_code
){
117 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
);
118 gcode
->add_nl
= true;
122 if (gcode
->has_letter('S') && (gcode
->get_value('S') == this->pool_index
))
124 if (gcode
->has_letter('P'))
125 this->p_factor
= gcode
->get_value('P');
126 if (gcode
->has_letter('I'))
127 this->i_factor
= gcode
->get_value('I');
128 if (gcode
->has_letter('D'))
129 this->d_factor
= gcode
->get_value('D');
130 if (gcode
->has_letter('X'))
131 this->i_max
= gcode
->get_value('X');
133 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
);
137 if (gcode
->has_letter('S') && (gcode
->get_value('S') == this->pool_index
))
139 double target
= 150.0;
140 if (gcode
->has_letter('P'))
142 target
= gcode
->get_value('P');
143 gcode
->stream
->printf("Target: %5.1f\n", target
);
145 gcode
->stream
->printf("Start PID tune, command is %s\n", gcode
->command
.c_str());
146 this->pool
->PIDtuner
->begin(this, target
, gcode
->stream
);
152 void TemperatureControl::on_gcode_execute(void* argument
){
153 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
155 // Set temperature without waiting
156 if( gcode
->m
== this->set_m_code
&& gcode
->has_letter('S') )
158 if (gcode
->get_value('S') == 0)
160 this->target_temperature
= UNDEFINED
;
161 this->heater_pin
->set(0);
165 this->set_desired_temperature(gcode
->get_value('S'));
168 // Set temperature and wait
169 if( gcode
->m
== this->set_and_wait_m_code
&& gcode
->has_letter('S') )
171 if (gcode
->get_value('S') == 0)
173 this->target_temperature
= UNDEFINED
;
174 this->heater_pin
->set(0);
178 this->set_desired_temperature(gcode
->get_value('S'));
180 this->kernel
->pauser
->take();
181 this->waiting
= true;
188 void TemperatureControl::set_desired_temperature(double desired_temperature
){
189 target_temperature
= desired_temperature
;
190 if (desired_temperature
== 0.0)
191 heater_pin
->set((o
= 0));
194 double TemperatureControl::get_temperature(){
198 double TemperatureControl::adc_value_to_temperature(int adc_value
)
200 if ((adc_value
== 4095) || (adc_value
== 0))
202 double r
= r2
/ ((4095.0 / adc_value
) - 1.0);
204 r
= (r1
* r
) / (r1
- r
);
205 return (1.0 / (k
+ (j
* log(r
/ r0
)))) - 273.15;
208 uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy
){
209 int r
= new_thermistor_reading();
211 double temperature
= adc_value_to_temperature(r
);
213 if (target_temperature
> 0)
215 if ((r
<= 1) || (r
>= 4094))
217 kernel
->streams
->printf("MINTEMP triggered on P%d.%d! check your thermistors!\n", this->thermistor_pin
->port_number
, this->thermistor_pin
->pin
);
218 target_temperature
= UNDEFINED
;
223 pid_process(temperature
);
224 if ((temperature
> target_temperature
) && waiting
)
226 kernel
->pauser
->release();
233 heater_pin
->set((o
= 0));
235 last_reading
= temperature
;
239 void TemperatureControl::pid_process(double temperature
)
241 double error
= target_temperature
- temperature
;
243 p
= error
* p_factor
;
244 i
+= (error
* this->i_factor
);
245 // d was imbued with oldest_raw earlier in new_thermistor_reading
246 d
= adc_value_to_temperature(d
);
247 d
= (d
- temperature
) * this->d_factor
;
251 if (i
< -this->i_max
)
254 this->o
= (p
+ i
+ d
) * heater_pin
->max_pwm() / 256;
256 if (this->o
>= heater_pin
->max_pwm())
259 this->o
= heater_pin
->max_pwm();
263 if (this->o
< -(heater_pin
->max_pwm()))
268 this->heater_pin
->pwm(o
);
271 int TemperatureControl::new_thermistor_reading()
273 int last_raw
= this->kernel
->adc
->read(this->thermistor_pin
);
274 if (queue
.size() >= queue
.capacity())
280 uint16_t r
= last_raw
;
282 for (int i
=0; i
<queue
.size(); i
++)
283 median_buffer
[i
] = *queue
.get_ref(i
);
284 uint16_t m
= median_buffer
[quick_median(median_buffer
, queue
.size())];
288 void TemperatureControl::on_second_tick(void* argument
)
291 kernel
->streams
->printf("%s:%3.1f /%3.1f @%d\n", designator
.c_str(), get_temperature(), ((target_temperature
== UNDEFINED
)?0.0:target_temperature
), o
, waiting
);