Fix positive trim values for delta M666
[clinton/Smoothieware.git] / src / modules / tools / temperaturecontrol / TemperatureControl.h
dissimilarity index 87%
index 9b4e061..e5b729e 100644 (file)
-/*  
-      This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
-      Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
-      Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-      You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. 
-*/
-
-#ifndef TEMPERATURECONTROL_H
-#define TEMPERATURECONTROL_H
-
-#include "mbed.h"
-#include "libs/Module.h"
-#include "libs/Kernel.h"
-#include <math.h>
-
-#define UNDEFINED -1
-
-#define temperature_control_thermistor_checksum 22986
-#define temperature_control_r0_ckeckusm    8728
-#define readings_per_second_ckeckusm       18645 
-#define temperature_control_t0_ckeckusm    9754
-#define temperature_control_beta_ckeckusm  64275 
-#define temperature_control_vadc_ckeckusm  8725
-#define temperature_control_vcc_ckeckusm   4274
-#define temperature_control_r1_ckeckusm    8985
-#define temperature_control_r2_ckeckusm    9242
-
-
-
-
-class TemperatureControl : public Module {
-    public:
-        TemperatureControl();
-        
-        void on_module_loaded();
-        void on_main_loop(void* argument);
-        void on_gcode_execute(void* argument);
-        void on_config_reload(void* argument);
-        void set_desired_temperature(double desired_temperature);
-        double get_temperature();
-        double adc_value_to_temperature(double adc_value);
-        double temperature_to_adc_value(double temperature);
-        void thermistor_read_tick();
-        double new_thermistor_reading();
-        double average_adc_reading();
-        
-    
-        AnalogIn* thermistor_pin;
-        PwmOut*   heater_pwm;
-        double    pwm_value; 
-        double    desired_adc_value;
-        double    tail_adc_value;
-        double    head_adc_value;
-
-        // Thermistor computation settings
-        double r0;
-        double t0;
-        double r1;
-        double r2;
-        double beta;
-        double vadc;
-        double vcc;
-        double k;
-        double vs;
-        double rs;
-        
-        double acceleration_factor;
-        double readings_per_second;
-
-        RingBuffer<double,16> queue;  // Queue of Blocks
-        int error_count;
-};
-
-#endif
+/*
+      this file is part of smoothie (http://smoothieware.org/). the motion control part is heavily based on grbl (https://github.com/simen/grbl).
+      smoothie is free software: you can redistribute it and/or modify it under the terms of the gnu general public license as published by the free software foundation, either version 3 of the license, or (at your option) any later version.
+      smoothie is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. see the gnu general public license for more details.
+      you should have received a copy of the gnu general public license along with smoothie. if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef temperaturecontrol_h
+#define temperaturecontrol_h
+
+#include "Pwm.h"
+#include <math.h>
+
+#include "RingBuffer.h"
+
+#define QUEUE_LEN 8
+
+class Module;
+class TemperatureControlPool;
+
+class TemperatureControl : public Module {
+
+    public:
+        TemperatureControl(uint16_t name);
+
+        void on_module_loaded();
+        void on_main_loop(void* argument);
+        void on_gcode_execute(void* argument);
+        void on_gcode_received(void* argument);
+        void on_config_reload(void* argument);
+        void on_second_tick(void* argument);
+        void on_get_public_data(void* argument);
+        void on_set_public_data(void* argument);
+
+        void set_desired_temperature(float desired_temperature);
+        float get_temperature();
+        float adc_value_to_temperature(int adc_value);
+        uint32_t thermistor_read_tick(uint32_t dummy);
+        int new_thermistor_reading();
+
+
+        int pool_index;
+        TemperatureControlPool *pool;
+        friend class PID_Autotuner;
+
+    private:
+        void pid_process(float);
+
+        float target_temperature;
+
+        float preset1;
+        float preset2;
+
+        // Thermistor computation settings
+        float r0;
+        float t0;
+        int r1;
+        int r2;
+        float beta;
+        float j;
+        float k;
+
+
+        // PID runtime
+        float i_max;
+
+        int o;
+
+        float last_reading;
+
+        float acceleration_factor;
+        float readings_per_second;
+
+        RingBuffer<uint16_t,QUEUE_LEN> queue;  // Queue of readings
+        uint16_t median_buffer[QUEUE_LEN];
+        int running_total;
+
+        uint16_t name_checksum;
+
+        Pin  thermistor_pin;
+        Pwm  heater_pin;
+
+        bool use_bangbang;
+        bool waiting;
+        bool min_temp_violated;
+
+        uint16_t set_m_code;
+        uint16_t set_and_wait_m_code;
+        uint16_t get_m_code;
+
+        string designator;
+
+
+        void setPIDp(float p);
+        void setPIDi(float i);
+        void setPIDd(float d);
+
+        float hysteresis;
+        float iTerm;
+        float lastInput;
+        // PID settings
+        float p_factor;
+        float i_factor;
+        float d_factor;
+        float PIDdt;
+};
+
+#endif