ESP8266/Arduino-pid: Difference between revisions

From Playful Exploration
Jump to navigation Jump to search
m (Bcamp moved page Arduino-pid to ESP8266/Arduino-pid without leaving a redirect)
Line 2: Line 2:
== PID control on ESP8266 or other Arduino compatible chips ==
== PID control on ESP8266 or other Arduino compatible chips ==


<pre>
<syntaxhighlight lang=cpp>
void loop() {
void loop() {


Line 32: Line 32:
   Last = Actual; // save current value for next time
   Last = Actual; // save current value for next time
}
}
</pre>
</syntaxhighlight>


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

Revision as of 03:44, 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: