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 "modules/robot/Conveyor.h"
17 #include "PublicDataRequest.h"
19 #include "PublicData.h"
20 #include "ToolManagerPublicAccess.h"
21 #include "StreamOutputPool.h"
23 #include "checksumm.h"
25 #include "SlowTicker.h"
27 #include "ConfigValue.h"
28 #include "PID_Autotuner.h"
30 // Temp sensor implementations:
31 #include "Thermistor.h"
34 #include "MRI_Hooks.h"
38 #define sensor_checksum CHECKSUM("sensor")
40 #define readings_per_second_checksum CHECKSUM("readings_per_second")
41 #define max_pwm_checksum CHECKSUM("max_pwm")
42 #define pwm_frequency_checksum CHECKSUM("pwm_frequency")
43 #define bang_bang_checksum CHECKSUM("bang_bang")
44 #define hysteresis_checksum CHECKSUM("hysteresis")
45 #define heater_pin_checksum CHECKSUM("heater_pin")
47 #define get_m_code_checksum CHECKSUM("get_m_code")
48 #define set_m_code_checksum CHECKSUM("set_m_code")
49 #define set_and_wait_m_code_checksum CHECKSUM("set_and_wait_m_code")
51 #define designator_checksum CHECKSUM("designator")
53 #define p_factor_checksum CHECKSUM("p_factor")
54 #define i_factor_checksum CHECKSUM("i_factor")
55 #define d_factor_checksum CHECKSUM("d_factor")
57 #define i_max_checksum CHECKSUM("i_max")
59 #define preset1_checksum CHECKSUM("preset1")
60 #define preset2_checksum CHECKSUM("preset2")
62 TemperatureControl::TemperatureControl(uint16_t name
, int index
)
67 min_temp_violated
= false;
72 TemperatureControl::~TemperatureControl()
77 void TemperatureControl::on_module_loaded()
80 // We start not desiring any temp
81 this->target_temperature
= UNDEFINED
;
86 // Register for events
87 this->register_for_event(ON_GCODE_RECEIVED
);
88 this->register_for_event(ON_GET_PUBLIC_DATA
);
91 this->register_for_event(ON_GCODE_EXECUTE
);
92 this->register_for_event(ON_SECOND_TICK
);
93 this->register_for_event(ON_MAIN_LOOP
);
94 this->register_for_event(ON_SET_PUBLIC_DATA
);
95 this->register_for_event(ON_HALT
);
99 void TemperatureControl::on_halt(void *arg
)
103 this->heater_pin
.set(0);
104 this->target_temperature
= UNDEFINED
;
107 void TemperatureControl::on_main_loop(void *argument
)
109 if (this->min_temp_violated
) {
110 THEKERNEL
->streams
->printf("Error: MINTEMP triggered. Check your temperature sensors!\n");
111 this->min_temp_violated
= false;
115 // Get configuration from the config file
116 void TemperatureControl::load_config()
120 this->set_m_code
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, set_m_code_checksum
)->by_default(104)->as_number();
121 this->set_and_wait_m_code
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, set_and_wait_m_code_checksum
)->by_default(109)->as_number();
122 this->get_m_code
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, get_m_code_checksum
)->by_default(105)->as_number();
123 this->readings_per_second
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, readings_per_second_checksum
)->by_default(20)->as_number();
125 this->designator
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, designator_checksum
)->by_default(string("T"))->as_string();
128 this->heater_pin
.from_string( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, heater_pin_checksum
)->by_default("nc")->as_string());
129 if(this->heater_pin
.connected()){
130 this->readonly
= false;
131 this->heater_pin
.as_output();
134 this->readonly
= true;
137 // For backward compatibility, default to a thermistor sensor.
138 std::string sensor_type
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, sensor_checksum
)->by_default("thermistor")->as_string();
140 // Instantiate correct sensor (TBD: TempSensor factory?)
142 sensor
= nullptr; // In case we fail to create a new sensor.
143 if(sensor_type
.compare("thermistor") == 0) {
144 sensor
= new Thermistor();
145 } else if(sensor_type
.compare("max31855") == 0) {
146 sensor
= new Max31855();
148 sensor
= new TempSensor(); // A dummy implementation
150 sensor
->UpdateConfig(temperature_control_checksum
, this->name_checksum
);
152 this->preset1
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, preset1_checksum
)->by_default(0)->as_number();
153 this->preset2
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, preset2_checksum
)->by_default(0)->as_number();
156 // sigma-delta output modulation
159 if(!this->readonly
) {
160 // used to enable bang bang control of heater
161 this->use_bangbang
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, bang_bang_checksum
)->by_default(false)->as_bool();
162 this->hysteresis
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, hysteresis_checksum
)->by_default(2)->as_number();
163 this->heater_pin
.max_pwm( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, max_pwm_checksum
)->by_default(255)->as_number() );
164 this->heater_pin
.set(0);
165 set_low_on_debug(heater_pin
.port_number
, heater_pin
.pin
);
166 // activate SD-DAC timer
167 THEKERNEL
->slow_ticker
->attach( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, pwm_frequency_checksum
)->by_default(2000)->as_number(), &heater_pin
, &Pwm::on_tick
);
172 THEKERNEL
->slow_ticker
->attach( this->readings_per_second
, this, &TemperatureControl::thermistor_read_tick
);
173 this->PIDdt
= 1.0 / this->readings_per_second
;
176 setPIDp( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, p_factor_checksum
)->by_default(10 )->as_number() );
177 setPIDi( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, i_factor_checksum
)->by_default(0.3f
)->as_number() );
178 setPIDd( THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, d_factor_checksum
)->by_default(200)->as_number() );
180 if(!this->readonly
) {
181 // set to the same as max_pwm by default
182 this->i_max
= THEKERNEL
->config
->value(temperature_control_checksum
, this->name_checksum
, i_max_checksum
)->by_default(this->heater_pin
.max_pwm())->as_number();
186 this->lastInput
= -1.0;
187 this->last_reading
= 0.0;
190 void TemperatureControl::on_gcode_received(void *argument
)
192 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
195 if( gcode
->m
== this->get_m_code
) {
196 char buf
[32]; // should be big enough for any status
197 int n
= snprintf(buf
, sizeof(buf
), "%s:%3.1f /%3.1f @%d ", this->designator
.c_str(), this->get_temperature(), ((target_temperature
== UNDEFINED
) ? 0.0 : target_temperature
), this->o
);
198 gcode
->txt_after_ok
.append(buf
, n
);
199 gcode
->mark_as_taken();
203 // readonly sensors don't handle the rest
204 if(this->readonly
) return;
206 if (gcode
->m
== 301) {
207 gcode
->mark_as_taken();
208 if (gcode
->has_letter('S') && (gcode
->get_value('S') == this->pool_index
)) {
209 if (gcode
->has_letter('P'))
210 setPIDp( gcode
->get_value('P') );
211 if (gcode
->has_letter('I'))
212 setPIDi( gcode
->get_value('I') );
213 if (gcode
->has_letter('D'))
214 setPIDd( gcode
->get_value('D') );
215 if (gcode
->has_letter('X'))
216 this->i_max
= gcode
->get_value('X');
218 //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->PIDdt, this->d_factor*this->PIDdt, this->i_max, this->p, this->i, this->d, o);
219 gcode
->stream
->printf("%s(S%d): Pf:%g If:%g Df:%g X(I_max):%g O:%d\n", this->designator
.c_str(), this->pool_index
, this->p_factor
, this->i_factor
/ this->PIDdt
, this->d_factor
* this->PIDdt
, this->i_max
, o
);
221 } else if (gcode
->m
== 500 || gcode
->m
== 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
222 gcode
->stream
->printf(";PID settings:\nM301 S%d P%1.4f I%1.4f D%1.4f\n", this->pool_index
, this->p_factor
, this->i_factor
/ this->PIDdt
, this->d_factor
* this->PIDdt
);
223 gcode
->mark_as_taken();
225 } else if( ( gcode
->m
== this->set_m_code
|| gcode
->m
== this->set_and_wait_m_code
) && gcode
->has_letter('S')) {
226 // this only gets handled if it is not controlle dby the tool manager or is active in the toolmanager
229 // this is safe as old configs as well as single extruder configs the toolmanager will not be running so will return false
230 // this will also ignore anything that the tool manager is not controlling and return false, otherwise it returns the active tool
232 bool ok
= PublicData::get_value( tool_manager_checksum
, is_active_tool_checksum
, this->name_checksum
, &returned_data
);
234 uint16_t active_tool_name
= *static_cast<uint16_t *>(returned_data
);
235 this->active
= (active_tool_name
== this->name_checksum
);
239 // Attach gcodes to the last block for on_gcode_execute
240 THEKERNEL
->conveyor
->append_gcode(gcode
);
242 // push an empty block if we have to wait, so the Planner can get things right, and we can prevent subsequent non-move gcodes from executing
243 if (gcode
->m
== this->set_and_wait_m_code
) {
244 // ensure that no subsequent gcodes get executed with our M109 or similar
245 THEKERNEL
->conveyor
->queue_head_block();
252 void TemperatureControl::on_gcode_execute(void *argument
)
254 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
256 if (((gcode
->m
== this->set_m_code
) || (gcode
->m
== this->set_and_wait_m_code
))
257 && gcode
->has_letter('S') && this->active
) {
258 float v
= gcode
->get_value('S');
261 this->target_temperature
= UNDEFINED
;
262 this->heater_pin
.set((this->o
= 0));
264 this->set_desired_temperature(v
);
266 if( gcode
->m
== this->set_and_wait_m_code
&& !this->waiting
) {
267 THEKERNEL
->pauser
->take();
268 this->waiting
= true;
275 void TemperatureControl::on_get_public_data(void *argument
)
277 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
279 if(!pdr
->starts_with(temperature_control_checksum
)) return;
281 if(pdr
->second_element_is(pool_index_checksum
)) {
282 // asking for our instance pointer if we have this pool_index
283 if(pdr
->third_element_is(this->pool_index
)) {
284 static void *return_data
;
286 pdr
->set_data_ptr(&return_data
);
291 }else if(!pdr
->second_element_is(this->name_checksum
)) return;
293 // ok this is targeted at us, so send back the requested data
294 if(pdr
->third_element_is(current_temperature_checksum
)) {
295 this->public_data_return
.current_temperature
= this->get_temperature();
296 this->public_data_return
.target_temperature
= (target_temperature
== UNDEFINED
) ? 0 : this->target_temperature
;
297 this->public_data_return
.pwm
= this->o
;
298 this->public_data_return
.designator
= this->designator
;
299 pdr
->set_data_ptr(&this->public_data_return
);
305 void TemperatureControl::on_set_public_data(void *argument
)
307 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
309 if(!pdr
->starts_with(temperature_control_checksum
)) return;
311 if(!pdr
->second_element_is(this->name_checksum
)) return;
313 // ok this is targeted at us, so set the temp
314 float t
= *static_cast<float *>(pdr
->get_data_ptr());
315 this->set_desired_temperature(t
);
319 void TemperatureControl::set_desired_temperature(float desired_temperature
)
321 if (desired_temperature
== 1.0)
322 desired_temperature
= preset1
;
323 else if (desired_temperature
== 2.0)
324 desired_temperature
= preset2
;
326 target_temperature
= desired_temperature
;
327 if (desired_temperature
== 0.0)
328 heater_pin
.set((this->o
= 0));
331 float TemperatureControl::get_temperature()
336 uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy
)
338 float temperature
= sensor
->get_temperature();
340 last_reading
= temperature
;
344 if (target_temperature
> 0) {
345 if (isinf(temperature
)) {
346 this->min_temp_violated
= true;
347 target_temperature
= UNDEFINED
;
348 heater_pin
.set((this->o
= 0));
350 pid_process(temperature
);
351 if ((temperature
> target_temperature
) && waiting
) {
352 THEKERNEL
->pauser
->release();
357 heater_pin
.set((this->o
= 0));
359 last_reading
= temperature
;
364 * Based on https://github.com/br3ttb/Arduino-PID-Library
366 void TemperatureControl::pid_process(float temperature
)
369 // bang bang is very simple, if temp is < target - hysteresis turn on full else if temp is > target + hysteresis turn heater off
371 if(temperature
> (target_temperature
+ hysteresis
) && this->o
> 0) {
372 heater_pin
.set(false);
373 this->o
= 0; // for display purposes only
375 } else if(temperature
< (target_temperature
- hysteresis
) && this->o
<= 0) {
376 if(heater_pin
.max_pwm() >= 255) {
378 this->heater_pin
.set(true);
379 this->o
= 255; // for display purposes only
381 // only to whatever max pwm is configured
382 this->heater_pin
.pwm(heater_pin
.max_pwm());
383 this->o
= heater_pin
.max_pwm(); // for display purposes only
389 // regular PID control
390 float error
= target_temperature
- temperature
;
391 this->iTerm
+= (error
* this->i_factor
);
392 if (this->iTerm
> this->i_max
) this->iTerm
= this->i_max
;
393 else if (this->iTerm
< 0.0) this->iTerm
= 0.0;
395 if(this->lastInput
< 0.0) this->lastInput
= temperature
; // set first time
396 float d
= (temperature
- this->lastInput
);
398 // calculate the PID output
399 // TODO does this need to be scaled by max_pwm/256? I think not as p_factor already does that
400 this->o
= (this->p_factor
* error
) + this->iTerm
- (this->d_factor
* d
);
402 if (this->o
>= heater_pin
.max_pwm())
403 this->o
= heater_pin
.max_pwm();
404 else if (this->o
< 0)
407 this->heater_pin
.pwm(this->o
);
408 this->lastInput
= temperature
;
411 void TemperatureControl::on_second_tick(void *argument
)
414 THEKERNEL
->streams
->printf("%s:%3.1f /%3.1f @%d\n", designator
.c_str(), get_temperature(), ((target_temperature
== UNDEFINED
) ? 0.0 : target_temperature
), o
);
417 void TemperatureControl::setPIDp(float p
)
422 void TemperatureControl::setPIDi(float i
)
424 this->i_factor
= i
* this->PIDdt
;
427 void TemperatureControl::setPIDd(float d
)
429 this->d_factor
= d
/ this->PIDdt
;