Merge remote-tracking branch 'upstream/edge' into feature/5-axis
authorJim Morris <morris@wolfman.com>
Sun, 26 Jun 2016 20:38:58 +0000 (13:38 -0700)
committerJim Morris <morris@wolfman.com>
Sun, 26 Jun 2016 20:38:58 +0000 (13:38 -0700)
src/modules/tools/temperaturecontrol/TemperatureControl.cpp
src/modules/tools/temperaturecontrol/TemperatureControl.h

index 76c44a0..e0daff8 100644 (file)
@@ -64,6 +64,9 @@
 #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")
+
 TemperatureControl::TemperatureControl(uint16_t name, int index)
 {
     name_checksum= name;
@@ -133,6 +136,10 @@ 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();
+
     // 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();
@@ -510,8 +517,44 @@ void TemperatureControl::pid_process(float 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;
+    
+    if( this->target_temperature <= 0 ){ // If we are not trying to heat, state is NOT_HEATING
+        this->runaway_state = NOT_HEATING;
+    }else{
+        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;
+                }
+                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 ){
+                    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 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);
+                }
+                break;
+            case TARGET_TEMPERATURE_REACHED: // If we are in state TARGET_TEMPERATURE_REACHED, check for thermal runaway
+                // If the temperature is outside the acceptable range
+                if( fabs( this->get_temperature() - this->target_temperature ) > this->runaway_range && this->runaway_range != 0 ){
+                    THEKERNEL->streams->printf("Error : Temperature runaway on %s, HALT asserted, TURN POWER OFF IMMEDIATELY - reset or M999 required\n", designator.c_str());
+                    THEKERNEL->call_event(ON_HALT, nullptr);
+                }
+                break;
+        }
+    }
 }
 
 void TemperatureControl::setPIDp(float p)
index aa5ef4a..2c95644 100644 (file)
@@ -30,6 +30,8 @@ class TemperatureControl : public Module {
         void set_desired_temperature(float desired_temperature);
 
         float get_temperature();
+        
+        enum RUNAWAY_TYPE {NOT_HEATING, WAITING_FOR_TEMP_TO_BE_REACHED, TARGET_TEMPERATURE_REACHED};
 
         friend class PID_Autotuner;
 
@@ -80,7 +82,15 @@ class TemperatureControl : public Module {
         float d_factor;
         float PIDdt;
 
+        // Temperature runaway values
+        RUNAWAY_TYPE runaway_state;      
+
+
         struct {
+            uint8_t runaway_heating_timer:8;
+            // Temperature runaway config options
+            uint8_t runaway_range:8;
+            uint8_t runaway_heating_timeout:8; 
             bool use_bangbang:1;
             bool waiting:1;
             bool temp_violated:1;