merged latest edge branch and fixed the spindle module. Also added ON_IDLE calls...
[clinton/Smoothieware.git] / src / modules / tools / temperaturecontrol / TemperatureControl.cpp
index 36c1fda..e0daff8 100644 (file)
@@ -23,7 +23,6 @@
 #include "checksumm.h"
 #include "Gcode.h"
 #include "SlowTicker.h"
-#include "Pauser.h"
 #include "ConfigValue.h"
 #include "PID_Autotuner.h"
 #include "SerialMessage.h"
@@ -32,6 +31,7 @@
 // Temp sensor implementations:
 #include "Thermistor.h"
 #include "max31855.h"
+#include "AD8495.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")
+
 TemperatureControl::TemperatureControl(uint16_t name, int index)
 {
     name_checksum= name;
     pool_index= index;
     waiting= false;
-    min_temp_violated= false;
+    temp_violated= false;
     sensor= nullptr;
     readonly= false;
 }
@@ -113,9 +116,11 @@ void TemperatureControl::on_halt(void *arg)
 
 void TemperatureControl::on_main_loop(void *argument)
 {
-    if (this->min_temp_violated) {
-        THEKERNEL->streams->printf("Error: MINTEMP triggered. Check your temperature sensors!\n");
-        this->min_temp_violated = false;
+    if (this->temp_violated) {
+        this->temp_violated = false;
+        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);
     }
 }
 
@@ -131,8 +136,12 @@ 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(1000)->as_number();
+    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();
 
     // Heater pin
@@ -155,6 +164,8 @@ void TemperatureControl::load_config()
         sensor = new Thermistor();
     } else if(sensor_type.compare("max31855") == 0) {
         sensor = new Max31855();
+    } else if(sensor_type.compare("ad8495") == 0) {
+        sensor = new AD8495();
     } else {
         sensor = new TempSensor(); // A dummy implementation
     }
@@ -206,14 +217,12 @@ void TemperatureControl::on_gcode_received(void *argument)
 
         if( gcode->m == this->get_m_code ) {
             char buf[32]; // should be big enough for any status
-            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);
+            int n = snprintf(buf, sizeof(buf), "%s:%3.1f /%3.1f @%d ", this->designator.c_str(), this->get_temperature(), ((target_temperature <= 0) ? 0.0 : target_temperature), this->o);
             gcode->txt_after_ok.append(buf, n);
-            gcode->mark_as_taken();
             return;
         }
 
         if (gcode->m == 305) { // set or get sensor settings
-            gcode->mark_as_taken();
             if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) {
                 TempSensor::sensor_options_t args= gcode->get_args();
                 args.erase('S'); // don't include the S
@@ -236,7 +245,7 @@ void TemperatureControl::on_gcode_received(void *argument)
                 if(sensor->get_optional(options)) {
                     for(auto &i : options) {
                         // foreach optional value
-                        gcode->stream->printf("%s(S%d): %c%1.18f\n", this->designator.c_str(), this->pool_index, i.first, i.second);
+                        gcode->stream->printf("%s(S%d): %c %1.18f\n", this->designator.c_str(), this->pool_index, i.first, i.second);
                     }
                 }
             }
@@ -247,8 +256,20 @@ void TemperatureControl::on_gcode_received(void *argument)
         // readonly sensors don't handle the rest
         if(this->readonly) return;
 
-        if (gcode->m == 301) {
-            gcode->mark_as_taken();
+        if (gcode->m == 143) {
+            if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) {
+                if(gcode->has_letter('P')) {
+                    max_temp= gcode->get_value('P');
+
+                } else {
+                    gcode->stream->printf("Nothing set NOTE Usage is M143 S0 P300 where <S> is the hotend index and <P> is the maximum temp to set\n");
+                }
+
+            }else if(gcode->get_num_args() == 0) {
+                gcode->stream->printf("Maximum temperature for %s(%d) is %f°C\n", this->designator.c_str(), this->pool_index, max_temp);
+            }
+
+        } else if (gcode->m == 301) {
             if (gcode->has_letter('S') && (gcode->get_value('S') == this->pool_index)) {
                 if (gcode->has_letter('P'))
                     setPIDp( gcode->get_value('P') );
@@ -268,6 +289,8 @@ void TemperatureControl::on_gcode_received(void *argument)
         } 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(";Max temperature setting:\nM143 S%d P%1.4f\n", this->pool_index, this->max_temp);
+
             if(this->sensor_settings) {
                 // get or save any sensor specific optional values
                 TempSensor::sensor_options_t options;
@@ -279,10 +302,8 @@ void TemperatureControl::on_gcode_received(void *argument)
                     gcode->stream->printf("\n");
                 }
             }
-            gcode->mark_as_taken();
 
         } else if( ( gcode->m == this->set_m_code || gcode->m == this->set_and_wait_m_code ) && gcode->has_letter('S')) {
-            gcode->mark_as_taken();
             // this only gets handled if it is not controlled by the tool manager or is active in the toolmanager
             this->active = true;
 
@@ -297,7 +318,7 @@ void TemperatureControl::on_gcode_received(void *argument)
 
             if(this->active) {
                 // required so temp change happens in order
-                THEKERNEL->conveyor->wait_for_empty_queue();
+                THEKERNEL->conveyor->wait_for_idle();
 
                 float v = gcode->get_value('S');
 
@@ -309,7 +330,7 @@ void TemperatureControl::on_gcode_received(void *argument)
                     // 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;
                         }
@@ -317,6 +338,11 @@ void TemperatureControl::on_gcode_received(void *argument)
                         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;
                     }
@@ -340,18 +366,35 @@ void TemperatureControl::on_get_public_data(void *argument)
             pdr->set_data_ptr(&return_data);
             pdr->set_taken();
         }
-        return;
 
-    }else if(!pdr->second_element_is(this->name_checksum)) return;
+    }else if(pdr->second_element_is(poll_controls_checksum)) {
+        // polling for all temperature controls
+        // add our data to the list which is passed in via the data_ptr
 
-    // ok this is targeted at us, so send back the requested data
-    if(pdr->third_element_is(current_temperature_checksum)) {
-        this->public_data_return.current_temperature = this->get_temperature();
-        this->public_data_return.target_temperature = (target_temperature == UNDEFINED) ? 0 : this->target_temperature;
-        this->public_data_return.pwm = this->o;
-        this->public_data_return.designator= this->designator;
-        pdr->set_data_ptr(&this->public_data_return);
+        std::vector<struct pad_temperature> *v= static_cast<std::vector<pad_temperature>*>(pdr->get_data_ptr());
+
+        struct pad_temperature t;
+        // setup data
+        t.current_temperature = this->get_temperature();
+        t.target_temperature = (target_temperature <= 0) ? 0 : this->target_temperature;
+        t.pwm = this->o;
+        t.designator= this->designator;
+        t.id= this->name_checksum;
+        v->push_back(t);
         pdr->set_taken();
+
+    }else if(pdr->second_element_is(current_temperature_checksum)) {
+        // if targeted at us
+        if(pdr->third_element_is(this->name_checksum)) {
+            // ok this is targeted at us, so set the requ3sted data in the pointer passed into us
+            struct pad_temperature *t= static_cast<pad_temperature*>(pdr->get_data_ptr());
+            t->current_temperature = this->get_temperature();
+            t->target_temperature = (target_temperature <= 0) ? 0 : this->target_temperature;
+            t->pwm = this->o;
+            t->designator= this->designator;
+            t->id= this->name_checksum;
+            pdr->set_taken();
+        }
     }
 
 }
@@ -385,11 +428,11 @@ void TemperatureControl::set_desired_temperature(float desired_temperature)
 
     float last_target_temperature= target_temperature;
     target_temperature = desired_temperature;
-    if (desired_temperature == 0.0F){
+    if (desired_temperature <= 0.0F){
         // turning it off
         heater_pin.set((this->o = 0));
 
-    }else if(last_target_temperature == 0.0F) {
+    }else if(last_target_temperature <= 0.0F) {
         // if it was off and we are now turning it on we need to initialize
         this->lastInput= last_reading;
         // set to whatever the output currently is See http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-initialization/
@@ -407,22 +450,16 @@ float TemperatureControl::get_temperature()
 uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy)
 {
     float temperature = sensor->get_temperature();
-    if(this->readonly) {
-        last_reading = temperature;
-        return 0;
-    }
-
-    if (target_temperature > 0) {
-        if (isinf(temperature) || temperature < min_temp) {
-            this->min_temp_violated = true;
+    if(!this->readonly && target_temperature > 2) {
+        if (isinf(temperature) || temperature < min_temp || temperature > max_temp) {
+            this->temp_violated = true;
             target_temperature = UNDEFINED;
             heater_pin.set((this->o = 0));
         } else {
             pid_process(temperature);
         }
-    } else {
-        heater_pin.set((this->o = 0));
     }
+
     last_reading = temperature;
     return 0;
 }
@@ -480,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 == UNDEFINED) ? 0.0 : target_temperature), o);
+        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)