Added bang bang control for heaters
authorJim Morris <morris@wolfman.com>
Sat, 29 Mar 2014 08:34:06 +0000 (01:34 -0700)
committerJim Morris <morris@wolfman.com>
Sat, 29 Mar 2014 08:34:06 +0000 (01:34 -0700)
ConfigSamples/Smoothieboard/config
src/libs/Pwm.h
src/modules/tools/temperaturecontrol/TemperatureControl.cpp
src/modules/tools/temperaturecontrol/TemperatureControl.h

index 5e1a6ab..677940a 100644 (file)
@@ -94,6 +94,9 @@ temperature_control.bed.set_m_code           140              #
 temperature_control.bed.set_and_wait_m_code  190              #
 temperature_control.bed.designator           B                #
 
+#temperature_control.bed.bang_bang            false            # set to true to use bang bang control rather than PID
+#temperature_control.bed.hysteresis           2.0              # set to the temperature in degrees C to use as hysteresis when using bang bang
+
 # Switch module for fan control
 switch.fan.enable                            true             #
 switch.fan.input_on_command                  M106             #
@@ -196,7 +199,7 @@ network.enable                               false            # enable the ether
 network.webserver.enable                     true             # enable the webserver
 network.telnet.enable                        true             # enable the telnet server
 network.ip_address                           auto             # use dhcp to get ip address
-# uncomment the 3 below to manually setup ip address 
+# uncomment the 3 below to manually setup ip address
 #network.ip_address                           192.168.3.222    # the IP address
 #network.ip_mask                              255.255.255.0    # the ip mask
 #network.ip_gateway                           192.168.3.1      # the gateway address
index 7e967bc..649fc58 100644 (file)
@@ -19,6 +19,7 @@ public:
     void     pwm(int);
     void     set(bool);
 
+private:
     int  _max;
     int  _pwm;
     int  _sd_accumulator;
index 5b5578b..c0d7b39 100644 (file)
@@ -27,6 +27,8 @@
 #define readings_per_second_checksum       CHECKSUM("readings_per_second")
 #define max_pwm_checksum                   CHECKSUM("max_pwm")
 #define pwm_frequency_checksum             CHECKSUM("pwm_frequency")
+#define bang_bang_checksum                 CHECKSUM("bang_bang")
+#define hysteresis_checksum                CHECKSUM("hysteresis")
 #define t0_checksum                        CHECKSUM("t0")
 #define beta_checksum                      CHECKSUM("beta")
 #define vadc_checksum                      CHECKSUM("vadc")
@@ -134,7 +136,13 @@ void TemperatureControl::on_config_reload(void* argument){
     // Heater pin
     this->heater_pin.from_string(    THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, heater_pin_checksum)->required()->as_string())->as_output();
     this->heater_pin.max_pwm(        THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, max_pwm_checksum)->by_default(255)->as_number() );
+
     this->heater_pin.set(0);
+    this->heater_on= false;
+
+    // used to enable bang bang control of heater
+    this->use_bangbang= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, bang_bang_checksum)->by_default(false)->as_bool();
+    this->hysteresis= THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, hysteresis_checksum)->by_default(2)->as_number();
 
     set_low_on_debug(heater_pin.port_number, heater_pin.pin);
 
@@ -336,8 +344,31 @@ uint32_t TemperatureControl::thermistor_read_tick(uint32_t dummy){
  */
 void TemperatureControl::pid_process(float temperature)
 {
-    float error = target_temperature - temperature;
+    if(use_bangbang) {
+        // bang bang if very simple, if temp is < target - hysteresis turn on full else if  temp is > target + hysteresis turn heater off
+        // good for relays
+        if(temperature > target_temperature+hysteresis && heater_on) {
+            heater_pin.set(false);
+            heater_on= false;
+            this->o= 0; // for display purposes only
+
+        }else if(temperature < target_temperature-hysteresis && !heater_on) {
+            if(heater_pin.max_pwm() >= 255) {
+                // turn on full
+                this->heater_pin.set(true);
+                this->o= 255; // for display purposes only
+            }else{
+                // only to whatever max pwm is configured
+                this->heater_pin.pwm(heater_pin.max_pwm());
+                this->o= heater_pin.max_pwm(); // for display purposes only
+            }
+            heater_on= true;
+        }
+        return;
+    }
 
+    // regular PID control
+    float error = target_temperature - temperature;
     this->iTerm += (error * this->i_factor);
     if (this->iTerm > this->i_max) this->iTerm = this->i_max;
     else if (this->iTerm < 0.0) this->iTerm = 0.0;
index 0bae540..ed5b1a4 100644 (file)
@@ -80,6 +80,8 @@ class TemperatureControl : public Module {
         Pin  thermistor_pin;
         Pwm  heater_pin;
 
+        bool use_bangbang;
+        bool heater_on;
         bool waiting;
         bool min_temp_violated;
 
@@ -94,6 +96,7 @@ class TemperatureControl : public Module {
         void setPIDi(float i);
         void setPIDd(float d);
 
+        float hysteresis;
         float iTerm;
         float lastInput;
         // PID settings