PID Autotune: M304 aborts
authorMichael Moon <triffid.hunter@gmail.com>
Sat, 23 Feb 2013 02:42:09 +0000 (13:42 +1100)
committerMichael Moon <triffid.hunter@gmail.com>
Sat, 23 Feb 2013 02:42:09 +0000 (13:42 +1100)
src/modules/tools/temperaturecontrol/PID_Autotuner.cpp
src/modules/tools/temperaturecontrol/PID_Autotuner.h

index e5faa4c..e4b19f6 100644 (file)
@@ -13,6 +13,7 @@ void PID_Autotuner::on_module_loaded()
     tick = false;
     this->kernel->slow_ticker->attach(20, this, &PID_Autotuner::on_tick );
     register_for_event(ON_IDLE);
+    register_for_event(ON_GCODE_RECEIVED);
 }
 
 void PID_Autotuner::begin(TemperatureControl *temp, double target, StreamOutput *stream)
@@ -20,6 +21,7 @@ void PID_Autotuner::begin(TemperatureControl *temp, double target, StreamOutput
     if (t)
         t->heater_pin.set(0);
 
+    s = stream;
     t = temp;
 
     t->target_temperature = 0.0;
@@ -35,9 +37,7 @@ void PID_Autotuner::begin(TemperatureControl *temp, double target, StreamOutput
     }
     cycle = 0;
 
-    s = stream;
-
-    s->printf("%s: Starting PID Autotune\n", t->designator.c_str());
+    s->printf("%s: Starting PID Autotune, M304 aborts\n", t->designator.c_str());
 
     bias = d = t->heater_pin.max_pwm() >> 1;
 
@@ -45,6 +45,28 @@ void PID_Autotuner::begin(TemperatureControl *temp, double target, StreamOutput
     last_output = true;
 }
 
+void PID_Autotuner::abort()
+{
+    if (!t)
+        return;
+
+    t->target_temperature = 0;
+    t->heater_pin.set(0);
+    t = NULL;
+
+    if (s)
+        s->printf("PID Autotune Aborted\n");
+    s = NULL;
+}
+
+void PID_Autotuner::on_gcode_received(void* argument)
+{
+    Gcode* gcode = static_cast<Gcode*>(argument);
+
+    if ((gcode->has_m) && (gcode->m == 304))
+        abort();
+}
+
 uint32_t PID_Autotuner::on_tick(uint32_t dummy)
 {
     if (t)
@@ -123,6 +145,8 @@ void PID_Autotuner::on_idle(void*)
             t->i_factor = ki;
             t->d_factor = kd;
 
+            s->printf("PID Autotune Complete! The settings above have been loaded into memory, but not written to your config file.\n");
+
             t = NULL;
             s = NULL;
 
index d50583b..bc32a98 100644 (file)
@@ -14,10 +14,12 @@ class PID_Autotuner : public Module
 public:
              PID_Autotuner();
     void     begin(TemperatureControl*, double, StreamOutput*);
+    void     abort();
 
     void     on_module_loaded(void);
     uint32_t on_tick(uint32_t);
     void     on_idle(void*);
+    void     on_gcode_received(void*);
 
     TemperatureControl *t;