Advertisement
Guest User

Untitled

a guest
May 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <PID_v1.h>
  2.  
  3. #define PIN_INPUT A0
  4.  
  5. //Define Variables we'll be connecting to
  6. double Setpoint, Input, Output;
  7.  
  8. //Define the aggressive and conservative Tuning Parameters
  9. double aggKp=4, aggKi=0.2, aggKd=1;
  10. double consKp=1, consKi=0.05, consKd=0.25;
  11.  
  12. //Specify the links and initial tuning parameters
  13. PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
  14.  
  15. void setup()
  16. {
  17. //initialize the variables we're linked to
  18. Serial.begin(9600); //Start a serial session
  19. Input = analogRead(PIN_INPUT);
  20. Setpoint = 336;
  21.  
  22. //turn the PID on
  23. myPID.SetMode(AUTOMATIC);
  24. }
  25.  
  26. void loop()
  27. {
  28. Input = analogRead(PIN_INPUT);
  29.  
  30. double gap = abs(Setpoint-Input); //distance away from setpoint
  31. if (gap < 10)
  32. { //we're close to setpoint, use conservative tuning parameters
  33. myPID.SetTunings(consKp, consKi, consKd);
  34. }
  35. else
  36. {
  37. //we're far from setpoint, use aggressive tuning parameters
  38. myPID.SetTunings(aggKp, aggKi, aggKd);
  39. }
  40.  
  41. myPID.Compute();
  42.  
  43. Serial.print("Setpoint = ");
  44. Serial.print(Setpoint);
  45. Serial.print(" Input = ");
  46. Serial.print(Input);
  47. Serial.print(" Output = ");
  48. Serial.print(Output);
  49. Serial.print("n");
  50. // analogWrite(PIN_OUTPUT, Output);
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement