Tighten up autopid to produce better results
[clinton/Smoothieware.git] / src / modules / tools / temperaturecontrol / PID_Autotuner.h
dissimilarity index 68%
index d50583b..dbf9214 100644 (file)
@@ -1,43 +1,55 @@
-#ifndef _PID_AUTOTUNE_H
-#define _PID_AUTOTUNE_H
-
-#include <stdint.h>
-
-#include "Module.h"
-#include "TemperatureControl.h"
-#include "StreamOutput.h"
-
-#define PID_AUTOTUNER_CYCLES 8
-
-class PID_Autotuner : public Module
-{
-public:
-             PID_Autotuner();
-    void     begin(TemperatureControl*, double, StreamOutput*);
-
-    void     on_module_loaded(void);
-    uint32_t on_tick(uint32_t);
-    void     on_idle(void*);
-
-    TemperatureControl *t;
-
-    double target_temperature;
-
-    int cycle;
-    bool output;
-    bool last_output;
-    StreamOutput *s;
-
-    volatile bool tick;
-
-    struct {
-        double t_max;
-        double t_min;
-        int ticks_low;
-        int ticks_high;
-    } cycles[PID_AUTOTUNER_CYCLES];
-
-    int bias, d;
-};
-
-#endif /* _PID_AUTOTUNE_H */
+/**
+ * Based on https://github.com/br3ttb/Arduino-PID-AutoTune-Library
+ */
+
+#ifndef _PID_AUTOTUNE_H
+#define _PID_AUTOTUNE_H
+
+#include <stdint.h>
+
+#include "Module.h"
+
+class TemperatureControl;
+class StreamOutput;
+
+class PID_Autotuner : public Module
+{
+public:
+    PID_Autotuner();
+
+    void on_module_loaded(void);
+    uint32_t on_tick(uint32_t);
+    void on_idle(void *);
+    void on_gcode_received(void *);
+
+private:
+    void begin(float, StreamOutput *, int );
+    void abort();
+    void finishUp();
+
+    TemperatureControl *temp_control;
+    float target_temperature;
+    StreamOutput *s;
+
+    float *peaks;
+    int requested_cycles;
+    float noiseBand;
+    unsigned long peak1, peak2;
+    int sampleTime;
+    int nLookBack;
+    int lookBackCnt;
+    int peakType;
+    float *lastInputs;
+    int peakCount;
+    float absMax, absMin;
+    float oStep;
+    int output;
+    volatile unsigned long tickCnt;
+    struct {
+        bool justchanged:1;
+        volatile bool tick:1;
+        bool firstPeak:1;
+    };
+};
+
+#endif /* _PID_AUTOTUNE_H */