kill heater and generate error if thermistor gives open or shorted reading
[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 "libs/Pin.h"
15
16 TemperatureControl::TemperatureControl(){}
17
18 TemperatureControl::TemperatureControl(uint16_t name){
19 this->name_checksum = name;
20 this->error_count = 0;
21 this->waiting = false;
22 }
23
24 void TemperatureControl::on_module_loaded(){
25
26 // We start not desiring any temp
27 this->desired_adc_value = UNDEFINED;
28
29 // Settings
30 this->on_config_reload(this);
31
32 this->acceleration_factor = 10;
33
34 this->kernel->slow_ticker->attach( 20, this, &TemperatureControl::thermistor_read_tick );
35
36 // Register for events
37 this->register_for_event(ON_GCODE_EXECUTE);
38 this->register_for_event(ON_MAIN_LOOP);
39
40 }
41
42 void TemperatureControl::on_main_loop(void* argument){ }
43
44 // Get configuration from the config file
45 void TemperatureControl::on_config_reload(void* argument){
46
47 // General config
48 this->set_m_code = this->kernel->config->value(temperature_control_checksum, this->name_checksum, set_m_code_checksum)->by_default(104)->as_number();
49 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();
50 this->get_m_code = this->kernel->config->value(temperature_control_checksum, this->name_checksum, get_m_code_checksum)->by_default(105)->as_number();
51 this->readings_per_second = this->kernel->config->value(temperature_control_checksum, this->name_checksum, readings_per_second_checksum)->by_default(5)->as_number();
52
53 // Values are here : http://reprap.org/wiki/Thermistor
54 this->r0 = 100000;
55 this->t0 = 25;
56 this->beta = 4066;
57 this->vadc = 3.3;
58 this->vcc = 3.3;
59 this->r1 = 0;
60 this->r2 = 4700;
61
62 // Preset values for various common types of thermistors
63 ConfigValue* thermistor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, thermistor_checksum);
64 if( thermistor->value.compare("EPCOS100K" ) == 0 ){ // Default
65 }else if( thermistor->value.compare("RRRF100K" ) == 0 ){ this->beta = 3960;
66 }else if( thermistor->value.compare("RRRF10K" ) == 0 ){ this->beta = 3964; this->r0 = 10000; this->r1 = 680; this->r2 = 1600;
67 }else if( thermistor->value.compare("Honeywell100K") == 0 ){ this->beta = 3974;
68 }else if( thermistor->value.compare("Semitec" ) == 0 ){ this->beta = 4267; }
69
70 // Preset values are overriden by specified values
71 this->r0 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, r0_checksum )->by_default(100000)->as_number(); // Stated resistance eg. 100K
72 this->t0 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, t0_checksum )->by_default(25 )->as_number() + 273.15; // Temperature at stated resistance, eg. 25C
73 this->beta = this->kernel->config->value(temperature_control_checksum, this->name_checksum, beta_checksum)->by_default(4066 )->as_number(); // Thermistor beta rating. See http://reprap.org/bin/view/Main/MeasuringThermistorBeta
74 this->vadc = this->kernel->config->value(temperature_control_checksum, this->name_checksum, vadc_checksum)->by_default(3.3 )->as_number(); // ADC Reference
75 this->vcc = this->kernel->config->value(temperature_control_checksum, this->name_checksum, vcc_checksum )->by_default(3.3 )->as_number(); // Supply voltage to potential divider
76 this->r1 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, r1_checksum )->by_default(0 )->as_number();
77 this->r2 = this->kernel->config->value(temperature_control_checksum, this->name_checksum, r2_checksum )->by_default(4700 )->as_number();
78
79 // Thermistor math
80 this->k = this->r0 * exp( -this->beta / this->t0 );
81 if( r1 > 0 ){ this->vs = r1 * this->vcc / ( r1 + r2 ); this->rs = r1 * r2 / ( r1 + r2 ); }else{ this->vs = this->vcc; this->rs = r2; }
82
83 // Thermistor pin for ADC readings
84 this->thermistor_pin = this->kernel->config->value(temperature_control_checksum, this->name_checksum, thermistor_pin_checksum )->required()->as_pin();
85 this->kernel->adc->enable_pin(this->thermistor_pin);
86
87 // Heater pin
88 this->heater_pin = this->kernel->config->value(temperature_control_checksum, this->name_checksum, heater_pin_checksum)->required()->as_pin()->as_output();
89 this->heater_pin->set(0);
90
91 }
92
93 //#pragma GCC push_options
94 //#pragma GCC optimize ("O0")
95
96
97 void TemperatureControl::on_gcode_execute(void* argument){
98 Gcode* gcode = static_cast<Gcode*>(argument);
99 if( gcode->has_m){
100 // Set temperature without waiting
101 if( gcode->m == this->set_m_code && gcode->has_letter('S') ){
102 //gcode->stream->printf("setting to %f meaning %u \r\n", gcode->get_value('S'), this->temperature_to_adc_value( gcode->get_value('S') ) );
103 if (gcode->get_value('S') == 0)
104 {
105 this->desired_adc_value = UNDEFINED;
106 this->heater_pin->set(0);
107 }
108 else
109 {
110 this->set_desired_temperature(gcode->get_value('S'));
111 }
112 }
113 // Set temperature and wait
114 if( gcode->m == this->set_and_wait_m_code && gcode->has_letter('S') ){
115 if (gcode->get_value('S') == 0)
116 {
117 this->desired_adc_value = UNDEFINED;
118 this->heater_pin->set(0);
119 }
120 else
121 {
122 this->set_desired_temperature(gcode->get_value('S'));
123 // Pause
124 this->kernel->pauser->take();
125 this->waiting = true;
126 }
127 }
128 // Get temperature
129 if( gcode->m == this->get_m_code ){
130 gcode->stream->printf("get temperature: %f current:%f target:%f bare_value:%u \r\n", this->get_temperature(), this->new_thermistor_reading(), this->desired_adc_value, this->kernel->adc->read(this->thermistor_pin) );
131 }
132 }
133 }
134
135 //#pragma GCC pop_options
136
137
138 void TemperatureControl::set_desired_temperature(double desired_temperature){
139 this->desired_adc_value = this->temperature_to_adc_value(desired_temperature);
140 }
141
142 double TemperatureControl::get_temperature(){
143 return this->adc_value_to_temperature( this->new_thermistor_reading() );
144 }
145
146 double TemperatureControl::adc_value_to_temperature(double adc_value){
147 double v = adc_value * this->vadc; // Convert from 0-1 adc value to voltage
148 double r = this->rs * v / ( this->vs - v ); // Resistance of thermistor
149 return ( this->beta / log( r / this->k )) - 273.15;
150 }
151
152 double TemperatureControl::temperature_to_adc_value(double temperature){
153 double r = this->r0 * exp( this->beta * ( 1 / (temperature + 273.15) -1 / this->t0 ) ); // Resistance of the thermistor
154 double v = this->vs * r / ( this->rs + r ); // Voltage at the potential divider
155 return v / this->vadc * 1.00000; // The ADC reading
156 }
157
158 uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy){
159 if( this->desired_adc_value != UNDEFINED ){
160 double r = this->new_thermistor_reading();
161 if ((r > 0.01) &&
162 (r < 0.99) &&
163 (r > this->desired_adc_value))
164 {
165 this->heater_pin->set(1);
166 }
167 else
168 {
169 if (((r <= 0.01) || (r >= 0.99)) && (this->desired_adc_value != UNDEFINED))
170 {
171 this->kernel->streams->printf("MINTEMP triggered on P%d.%d! check your thermistors!\n", this->thermistor_pin->port_number, this->thermistor_pin->pin);
172 this->desired_adc_value = UNDEFINED;
173 }
174 this->heater_pin->set(0);
175 if (this->waiting)
176 {
177 this->kernel->pauser->release();
178 this->waiting = false;
179 }
180 }
181 }
182 return 0;
183 }
184
185 double TemperatureControl::new_thermistor_reading(){
186
187 double new_reading = double( double(this->kernel->adc->read(this->thermistor_pin) / double(1<<12) ) );
188
189 if( this->queue.size() < 15 ){
190 this->queue.push_back( new_reading );
191 return new_reading;
192 }else{
193 double current_temp = this->average_adc_reading();
194 double error = fabs(new_reading - current_temp);
195 if( error < 0.1 ){
196 this->error_count = 0;
197 double test;
198 this->queue.pop_front(test);
199 this->queue.push_back( new_reading );
200 }else{
201 this->error_count++;
202 if( this->error_count > 4 ){
203 double test;
204 this->queue.pop_front(test);
205 }
206 }
207 return current_temp;
208 }
209 }
210
211
212 double TemperatureControl::average_adc_reading(){
213 double total = 0;
214 int j = 0;
215 int reading_index = this->queue.head;
216 while( reading_index != this->queue.tail ){
217 j++;
218 total += this->queue.buffer[reading_index];
219 reading_index = this->queue.next_block_index( reading_index );
220 }
221 return total / j;
222 }
223
224
225