You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
*/
-// TODO : THIS FILE IS LAME, MUST BE MADE MUCH BETTER
-
#include "libs/Module.h"
#include "libs/Kernel.h"
#include <math.h>
#include "checksumm.h"
#include "Gcode.h"
#include "SlowTicker.h"
-#include "Pauser.h"
#include "ConfigValue.h"
#include "PID_Autotuner.h"
#include "SerialMessage.h"
// Temp sensor implementations:
#include "Thermistor.h"
#include "max31855.h"
+#include "AD8495.h"
+#include "PT100_E3D.h"
#include "MRI_Hooks.h"
#define preset1_checksum CHECKSUM("preset1")
#define preset2_checksum CHECKSUM("preset2")
+#define runaway_range_checksum CHECKSUM("runaway_range")
+#define runaway_heating_timeout_checksum CHECKSUM("runaway_heating_timeout")
+#define runaway_cooling_timeout_checksum CHECKSUM("runaway_cooling_timeout")
+#define runaway_error_range_checksum CHECKSUM("runaway_error_range")
+
TemperatureControl::TemperatureControl(uint16_t name, int index)
{
name_checksum= name;
temp_violated= false;
sensor= nullptr;
readonly= false;
+ tick= 0;
}
TemperatureControl::~TemperatureControl()
// Register for events
this->register_for_event(ON_GCODE_RECEIVED);
this->register_for_event(ON_GET_PUBLIC_DATA);
+ this->register_for_event(ON_IDLE);
if(!this->readonly) {
this->register_for_event(ON_SECOND_TICK);
}
}
+void TemperatureControl::on_idle(void *arg)
+{
+ sensor->on_idle();
+}
+
+
void TemperatureControl::on_main_loop(void *argument)
{
if (this->temp_violated) {
this->temp_violated = false;
- THEKERNEL->streams->printf("Error: MINTEMP or MAXTEMP triggered. Check your temperature sensors!\n");
+ THEKERNEL->streams->printf("ERROR: MINTEMP or MAXTEMP triggered on %s. Check your temperature sensors!\n", designator.c_str());
THEKERNEL->streams->printf("HALT asserted - reset or M999 required\n");
THEKERNEL->call_event(ON_HALT, nullptr);
}
this->designator = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, designator_checksum)->by_default(string("T"))->as_string();
+ // Runaway parameters
+ uint32_t n= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_range_checksum)->by_default(20)->as_number();
+ if(n > 63) n= 63;
+ this->runaway_range= n;
+
+ // these need to fit in 9 bits after dividing by 8 so max is 4088 secs or 68 minutes
+ n= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_heating_timeout_checksum)->by_default(900)->as_number();
+ if(n > 4088) n= 4088;
+ this->runaway_heating_timeout = n/8; // we have 8 second ticks
+ n= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_cooling_timeout_checksum)->by_default(0)->as_number(); // disable by default
+ if(n > 4088) n= 4088;
+ this->runaway_cooling_timeout = n/8;
+
+ this->runaway_error_range= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_error_range_checksum)->by_default(1.0F)->as_number();
+
// Max and min temperatures we are not allowed to get over (Safety)
this->max_temp = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, max_temp_checksum)->by_default(300)->as_number();
this->min_temp = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, min_temp_checksum)->by_default(0)->as_number();
sensor = new Thermistor();
} else if(sensor_type.compare("max31855") == 0) {
sensor = new Max31855();
+ } else if(sensor_type.compare("ad8495") == 0) {
+ sensor = new AD8495();
+ } else if(sensor_type.compare("pt100_e3d") == 0) {
+ sensor = new PT100_E3D();
} else {
sensor = new TempSensor(); // A dummy implementation
}
this->heater_pin.max_pwm(gcode->get_value('Y'));
}else if(!gcode->has_letter('S')) {
- gcode->stream->printf("%s(S%d): Pf:%g If:%g Df:%g X(I_max):%g max pwm: %d 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->heater_pin.max_pwm(), o);
+ gcode->stream->printf("%s(S%d): Pf:%g If:%g Df:%g X(I_max):%g Y(max pwm):%d 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->heater_pin.max_pwm(), o);
}
} else if (gcode->m == 500 || gcode->m == 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
- gcode->stream->printf(";PID settings:\nM301 S%d P%1.4f I%1.4f D%1.4f X%1.4f Y%d\n", this->pool_index, this->p_factor, this->i_factor / this->PIDdt, this->d_factor * this->PIDdt, this->i_max, this->heater_pin.max_pwm());
+ gcode->stream->printf(";PID settings, i_max, max_pwm:\nM301 S%d P%1.4f I%1.4f D%1.4f X%1.4f Y%d\n", this->pool_index, this->p_factor, this->i_factor / this->PIDdt, this->d_factor * this->PIDdt, this->i_max, this->heater_pin.max_pwm());
gcode->stream->printf(";Max temperature setting:\nM143 S%d P%1.4f\n", this->pool_index, this->max_temp);
}
} else if( ( gcode->m == this->set_m_code || gcode->m == this->set_and_wait_m_code ) && gcode->has_letter('S')) {
- // this only gets handled if it is not controlled by the tool manager or is active in the toolmanager
- this->active = true;
-
- // this is safe as old configs as well as single extruder configs the toolmanager will not be running so will return false
- // this will also ignore anything that the tool manager is not controlling and return false, otherwise it returns the active tool
- void *returned_data;
- bool ok = PublicData::get_value( tool_manager_checksum, is_active_tool_checksum, this->name_checksum, &returned_data );
- if (ok) {
- uint16_t active_tool_name = *static_cast<uint16_t *>(returned_data);
- this->active = (active_tool_name == this->name_checksum);
- }
-
- if(this->active) {
+ int tool = gcode->has_letter('T') ? gcode->get_value('T') : -1;
+ // default on when no T, for unmanaged tools (e.g. heatbed)
+ bool am_tool = (tool == -1) ? true : false;
+
+ if (tool == -1) {
+ // this is safe as old configs as well as single extruder configs the toolmanager will not be running so will return false
+ // this will also ignore anything that the tool manager is not controlling and return false, otherwise it returns the active tool
+ void *returned_data;
+ bool ok = PublicData::get_value( tool_manager_checksum, is_active_tool_checksum, this->name_checksum, &returned_data );
+ if (ok) {
+ uint16_t active_tool_name = *static_cast<uint16_t *>(returned_data);
+ am_tool = (active_tool_name == this->name_checksum);
+ }
+ } else {
+ // FIXME
+ // this is wrong because we can't get the names of any
+ // inactive tools to check that we are associated with
+ // one. Just assume one temperature control per
+ // tool...
+ am_tool = (tool == this->pool_index);
+ }
+
+ if(am_tool) {
// required so temp change happens in order
- THEKERNEL->conveyor->wait_for_empty_queue();
+ // Allow set without wait to be processed immediately
+ if( gcode->m == this->set_and_wait_m_code ) {
+ THEKERNEL->conveyor->wait_for_idle();
+ }
float v = gcode->get_value('S');
// wait for temp to be reached, no more gcodes will be fetched until this is complete
if( gcode->m == this->set_and_wait_m_code) {
if(isinf(get_temperature()) && isinf(sensor->get_temperature())) {
- THEKERNEL->streams->printf("Temperature reading is unreliable HALT asserted - reset or M999 required\n");
+ THEKERNEL->streams->printf("Temperature reading is unreliable on %s HALT asserted - reset or M999 required\n", designator.c_str());
THEKERNEL->call_event(ON_HALT, nullptr);
return;
}
this->waiting = true; // on_second_tick will announce temps
while ( get_temperature() < target_temperature ) {
THEKERNEL->call_event(ON_IDLE, this);
+ // check if ON_HALT was called (usually by kill button)
+ if(THEKERNEL->is_halted() || this->target_temperature == UNDEFINED) {
+ THEKERNEL->streams->printf("Wait on temperature aborted by kill\n");
+ break;
+ }
}
this->waiting = false;
}
if (this->iTerm > this->i_max) this->iTerm = this->i_max;
else if (this->iTerm < 0.0) this->iTerm = 0.0;
}
+
+ // reset the runaway state, even if it was a temp change
+ this->runaway_state = NOT_HEATING;
}
float TemperatureControl::get_temperature()
void TemperatureControl::on_second_tick(void *argument)
{
+
+ // If waiting for a temperature to be reach, display it to keep host programs up to date on the progress
if (waiting)
THEKERNEL->streams->printf("%s:%3.1f /%3.1f @%d\n", designator.c_str(), get_temperature(), ((target_temperature <= 0) ? 0.0 : target_temperature), o);
+
+ // Check whether or not there is a temperature runaway issue, if so stop everything and report it
+ if(THEKERNEL->is_halted()) return;
+
+ // see if runaway detection is enabled
+ if(this->runaway_heating_timeout == 0 && this->runaway_range == 0) return;
+
+ // check every 8 seconds, depends on tick being 3 bits
+ if(++tick != 0) return;
+
+ if(this->target_temperature <= 0){ // If we are not trying to heat, state is NOT_HEATING
+ this->runaway_state = NOT_HEATING;
+
+ }else{
+ float current_temperature= this->get_temperature();
+ // heater is active
+ switch( this->runaway_state ){
+ case NOT_HEATING: // If we were previously not trying to heat, but we are now, change to state WAITING_FOR_TEMP_TO_BE_REACHED
+ this->runaway_state= (this->target_temperature >= current_temperature || this->runaway_cooling_timeout == 0) ? HEATING_UP : COOLING_DOWN;
+ this->runaway_timer = 0;
+ tick= 0;
+ break;
+
+ case HEATING_UP:
+ case COOLING_DOWN:
+ // check temp has reached the target temperature within the given error range
+ if( (runaway_state == HEATING_UP && current_temperature >= (this->target_temperature - this->runaway_error_range)) ||
+ (runaway_state == COOLING_DOWN && current_temperature <= (this->target_temperature + this->runaway_error_range)) ) {
+ this->runaway_state = TARGET_TEMPERATURE_REACHED;
+ this->runaway_timer = 0;
+ tick= 0;
+
+ }else{
+ uint16_t t= (runaway_state == HEATING_UP) ? this->runaway_heating_timeout : this->runaway_cooling_timeout;
+ // we are still heating up see if we have hit the max time allowed
+ if(t > 0 && ++this->runaway_timer > t){
+ THEKERNEL->streams->printf("ERROR: Temperature took too long to be reached on %s, HALT asserted, TURN POWER OFF IMMEDIATELY - reset or M999 required\n", designator.c_str());
+ THEKERNEL->call_event(ON_HALT, nullptr);
+ this->runaway_state = NOT_HEATING;
+ this->runaway_timer = 0;
+ }
+ }
+ break;
+
+ case TARGET_TEMPERATURE_REACHED:
+ if(this->runaway_range != 0) {
+ // we are in state TARGET_TEMPERATURE_REACHED, check for thermal runaway
+ float delta= current_temperature - this->target_temperature;
+
+ // If the temperature is outside the acceptable range for 8 seconds, this allows for some noise spikes without halting
+ if(fabsf(delta) > this->runaway_range){
+ if(this->runaway_timer++ >= 1) { // this being 8 seconds
+ THEKERNEL->streams->printf("ERROR: Temperature runaway on %s (delta temp %f), HALT asserted, TURN POWER OFF IMMEDIATELY - reset or M999 required\n", designator.c_str(), delta);
+ THEKERNEL->call_event(ON_HALT, nullptr);
+ this->runaway_state = NOT_HEATING;
+ this->runaway_timer= 0;
+ }
+
+ }else{
+ this->runaway_timer= 0;
+ }
+ }
+
+ break;
+ }
+ }
}
void TemperatureControl::setPIDp(float p)