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 "Thermistor.h"
#include "max31855.h"
#include "AD8495.h"
+#include "PT100_E3D.h"
#include "MRI_Hooks.h"
#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)
{
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->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(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;
- n= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, runaway_heating_timeout_checksum)->by_default(0)->as_number();
- if(n > 511) n= 511;
- this->runaway_heating_timeout = 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();
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_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');
// 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:
- // 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 = 0;
+ this->runaway_timer = 0;
+ tick= 0;
- }else if(this->runaway_heating_timeout != 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(++this->runaway_heating_timer > this->runaway_heating_timeout){
- this->runaway_heating_timer = 0;
+ 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= this->get_temperature() - this->target_temperature;
+ float delta= current_temperature - this->target_temperature;
- // If the temperature is outside the acceptable range for 10 seconds, this allows for some noise spikes without halting
+ // 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){
- this->runaway_heating_timer++;
+ 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_heating_timer= 0;
- }
-
- if(this->runaway_heating_timer > 10) {
- 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;
}
}
+
break;
}
}