ESP8266/Arduino-pid: Difference between revisions

From Playful Exploration
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 51: Line 51:
float loww =-100;  // output low limit
float loww =-100;  // output low limit


float pid(float setp, float curr)
float pid(float setp, float inpu)
{
{
     float erro, prop, diff, outp;
     float erro, prop, diff, outp;


     // calulate the error
     // calulate the error
     erro = _.setp - _.curr;
     erro = setp - inpu;


     // calulate P, I and D term
     // calulate the P, I and D term
     prop = _.kkkP * erro;
     prop = kkkP * erro;
     inte = _.kkkI * erro + inte;
     inte = kkkI * erro + inte;
     diff = _.kkkD * (curr - prev);
     diff = kkkD * (inpu - prev);


     // limit the integral to combat integral windup
     // limit the integral to combat integral windup
Line 73: Line 73:
     if (outp > high) outp = high;
     if (outp > high) outp = high;
     else if (outp < loww) outp = loww;
     else if (outp < loww) outp = loww;
    // store the last value for the next run
    prev = inpu;


     return outp;
     return outp;
}
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 06:47, 22 July 2016

PID control on ESP8266 or other Arduino compatible chips

void loop() {

  //-------------------- Calculates the PID drive value --------------------

  Actual = analogRead(Position);
  Error = SetPt - Actual;
  
  if (abs(Error) < IntThresh){ // prevent integral 'windup'
    Integral = Integral + Error; // accumulate the error integral
  } else {
    Integral=0; // zero it if out of bounds
  }
  P = Error*kP; // calc proportional term
  I = Integral*kI; // integral term
  D = (Last-Actual)*kD; // derivative term
  Drive = P + I + D; // Total drive = P+I+D
  Drive = Drive*ScaleFactor; // scale Drive to be in the range 0-255

  if (Drive < 0){ // Check which direction to go.
    digitalWrite (Direction,LOW); // change direction as needed
  } else { // depending on the sign of Error
    digitalWrite (Direction,HIGH);
  }
  if (abs(Drive)>255) {
    Drive=255;
  }
  analogWrite (Motor,Drive); // send PWM command to motor board
  Last = Actual; // save current value for next time
}

Source: http://www.maelabs.ucsd.edu/mae156alib/control/PID-Control-Ardunio.pdf

Or use an Arudino library:

PID Refactored

This is attempt to make the pid code readable and understandable:

float prev;        // previous value, initialize with first current value
float inte = 0;    // integral memory, initialize to zero
float kkkP = 1;    // coefficient for proportional term
float kkkI = 1;    // coefficient for integral term
float kkkD = 1;    // coefficient for differential term
float high = 100;  // output high limit
float loww =-100;  // output low limit

float pid(float setp, float inpu)
{
    float erro, prop, diff, outp;

    // calulate the error
    erro = setp - inpu;

    // calulate the P, I and D term
    prop = kkkP * erro;
    inte = kkkI * erro + inte;
    diff = kkkD * (inpu - prev);

    // limit the integral to combat integral windup
    if (inte > high) inte = high;
    else if (inte < loww) inte = loww;

    // calulate the response
    outp = prop + inte + diff;

    // limit the response
    if (outp > high) outp = high;
    else if (outp < loww) outp = loww;

    // store the last value for the next run
    prev = inpu;

    return outp;
}