PID: use sensible defaults, auto-scale to PIN_PWM_MAX, reduce smoothing buffer to...
authorMichael Moon <triffid.hunter@gmail.com>
Fri, 28 Dec 2012 05:49:44 +0000 (16:49 +1100)
committerMichael Moon <triffid.hunter@gmail.com>
Wed, 2 Jan 2013 00:48:29 +0000 (11:48 +1100)
src/modules/tools/temperaturecontrol/TemperatureControl.cpp
src/modules/tools/temperaturecontrol/TemperatureControl.h

index c588420..5818789 100644 (file)
@@ -99,10 +99,10 @@ void TemperatureControl::on_config_reload(void* argument){
 
     // PID
     this->p_factor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, p_factor_checksum)->by_default(10 )->as_number();
-    this->i_factor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, i_factor_checksum)->by_default(0.1)->as_number();
-    this->d_factor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, d_factor_checksum)->by_default(20 )->as_number();
-    this->i_max    = this->kernel->config->value(temperature_control_checksum, this->name_checksum, i_max_checksum   )->by_default(10)->as_number();
-    this->i_accumulator  = 0.0;
+    this->i_factor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, i_factor_checksum)->by_default(0.3)->as_number();
+    this->d_factor = this->kernel->config->value(temperature_control_checksum, this->name_checksum, d_factor_checksum)->by_default(200)->as_number();
+    this->i_max    = this->kernel->config->value(temperature_control_checksum, this->name_checksum, i_max_checksum   )->by_default(50 )->as_number();
+    this->i = 0.0;
     this->last_reading = 0.0;
 }
 
@@ -165,7 +165,7 @@ void TemperatureControl::on_gcode_execute(void* argument){
                 this->d_factor = gcode->get_value('D');
             if (gcode->has_letter('X'))
                 this->i_max    = gcode->get_value('X');
-            gcode->stream->printf("%s: P:%g I:%g D:%g X(I_max):%g I_accum:%g P:%g I:%g D:%g O:%d\n", this->designator.c_str(), this->p_factor, this->i_factor, this->d_factor, this->i_max, this->i_accumulator, this->p, this->i, this->d, o);
+            gcode->stream->printf("%s: Pf:%g If:%g Df:%g X(I_max):%g Pv:%g Iv:%g Dv:%g O:%d\n", this->designator.c_str(), this->p_factor, this->i_factor, this->d_factor, this->i_max, this->p, this->i, this->d, o);
         }
     }
 }
@@ -224,7 +224,7 @@ void TemperatureControl::pid_process(double temperature)
     double error = target_temperature - temperature;
 
     p = error * p_factor;
-    i = this->i_accumulator + (error * this->i_factor);
+    i += (error * this->i_factor);
     d = (temperature - last_reading) * this->d_factor;
 
     if (i > this->i_max)
@@ -232,11 +232,9 @@ void TemperatureControl::pid_process(double temperature)
     if (i < -this->i_max)
         i = -this->i_max;
 
-    this->i_accumulator = i;
-
-    this->o = (p + i - d) * 4;
-    if (this->o > 1023)
-        this->o = 1023;
+    this->o = (p + i - d) * PIN_PWM_MAX / 256;
+    if (this->o >= PIN_PWM_MAX)
+        this->o = PIN_PWM_MAX - 1;
     if (this->o < 0)
         this->o = 0;
 
index 3b79208..e3abcf7 100644 (file)
@@ -85,7 +85,7 @@ class TemperatureControl : public Module {
         double acceleration_factor;
         double readings_per_second;
 
-        RingBuffer<uint16_t,16> queue;  // Queue of readings
+        RingBuffer<uint16_t,4> queue;  // Queue of readings
         int running_total;
 
         uint16_t name_checksum;