X-Git-Url: http://git.hcoop.net/clinton/Smoothieware.git/blobdiff_plain/a2623dd3b2fddb51a7e64291f2b20df67e1cb099..eb5f6de454a36e22a16d7bbff790df0ea8f2a76f:/src/modules/tools/temperaturecontrol/TemperatureControl.cpp diff --git a/src/modules/tools/temperaturecontrol/TemperatureControl.cpp b/src/modules/tools/temperaturecontrol/TemperatureControl.cpp index 3474c665..61bafaa4 100644 --- a/src/modules/tools/temperaturecontrol/TemperatureControl.cpp +++ b/src/modules/tools/temperaturecontrol/TemperatureControl.cpp @@ -5,8 +5,6 @@ You should have received a copy of the GNU General Public License along with Smoothie. If not, see . */ -// TODO : THIS FILE IS LAME, MUST BE MADE MUCH BETTER - #include "libs/Module.h" #include "libs/Kernel.h" #include @@ -32,6 +30,7 @@ #include "Thermistor.h" #include "max31855.h" #include "AD8495.h" +#include "PT100_E3D.h" #include "MRI_Hooks.h" @@ -66,6 +65,8 @@ #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) { @@ -75,6 +76,7 @@ TemperatureControl::TemperatureControl(uint16_t name, int index) temp_violated= false; sensor= nullptr; readonly= false; + tick= 0; } TemperatureControl::~TemperatureControl() @@ -95,6 +97,7 @@ void TemperatureControl::on_module_loaded() // 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); @@ -114,6 +117,12 @@ void TemperatureControl::on_halt(void *arg) } } +void TemperatureControl::on_idle(void *arg) +{ + sensor->on_idle(); +} + + void TemperatureControl::on_main_loop(void *argument) { if (this->temp_violated) { @@ -137,8 +146,19 @@ void TemperatureControl::load_config() this->designator = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, designator_checksum)->by_default(string("T"))->as_string(); // Runaway parameters - this->runaway_range = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_range_checksum)->by_default(0)->as_number(); - this->runaway_heating_timeout = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_heating_timeout_checksum)->by_default(0)->as_number(); + 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(); @@ -166,6 +186,8 @@ void TemperatureControl::load_config() 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 } @@ -283,11 +305,11 @@ void TemperatureControl::on_gcode_received(void *argument) 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); @@ -304,21 +326,34 @@ void TemperatureControl::on_gcode_received(void *argument) } } 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(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(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_idle(); + // 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'); @@ -440,6 +475,9 @@ void TemperatureControl::set_desired_temperature(float desired_temperature) 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() @@ -525,35 +563,65 @@ void TemperatureControl::on_second_tick(void *argument) // Check whether or not there is a temperature runaway issue, if so stop everything and report it if(THEKERNEL->is_halted()) return; - if( this->target_temperature <= 0 ){ // If we are not trying to heat, state is NOT_HEATING + // 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 - if( this->target_temperature > 0 ){ - this->runaway_state = WAITING_FOR_TEMP_TO_BE_REACHED; - this->runaway_heating_timer = 0; - } + 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 WAITING_FOR_TEMP_TO_BE_REACHED: // In we are in state 1 ( waiting for temperature to be reached ), and the temperature has been reached, change to state TARGET_TEMPERATURE_REACHED - if( this->get_temperature() >= this->target_temperature ){ + + 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_heating_timer++; - if( this->runaway_heating_timer > this->runaway_heating_timeout && this->runaway_heating_timeout != 0 ){ - this->runaway_heating_timer = 0; - 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_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 we are in state TARGET_TEMPERATURE_REACHED, check for thermal runaway - float delta= this->get_temperature() - this->target_temperature; - // If the temperature is outside the acceptable range - if(this->runaway_range != 0 && fabsf(delta) > this->runaway_range){ - 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); + + 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; } }