Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. //Ultrasonic Parameter
  2. int trigPin = 10; // Trigger
  3. int echoPin = 9; // Echo
  4. long duration, currentPosition;
  5.  
  6. //Motor Parameter
  7. int fan1 = 7; // IN1
  8. int fan2 = 6; // IN2
  9. int pwm = 5; // PWM Port
  10. //PID parameters.
  11. //Guide 1 : https://en.wikipedia.org/wiki/Ziegler%E2%80%93Nichols_method
  12. //Guide 2 : https://stackoverflow.com/questions/14614124/how-do-i-use-a-pid-controller
  13. // Set kp,ki,kd to Zero and follow instruction from above link to input value.
  14.  
  15. double kp=1; //proportional parameter
  16. double ki=0.0001; //integral parameter
  17. double kd=0 n; //derivative parameter
  18. unsigned long currentTime, previousTime;
  19.  
  20. double elapsedTime;
  21. double error;
  22. double lastError;
  23. double input, error_, output, setPoint;
  24. double cumError, rateError;
  25.  
  26.  
  27. // SetPoint or Height
  28. double SetP= 25;
  29.  
  30.  
  31. void setup() {
  32.  
  33. //Serial Port begin
  34. Serial.begin (38400);
  35. //Define inputs and outputs
  36. pinMode(trigPin, OUTPUT);
  37. pinMode(echoPin, INPUT);
  38. pinMode(fan1,OUTPUT);
  39. pinMode(fan2,OUTPUT);
  40. digitalWrite(fan1, HIGH);
  41. digitalWrite(fan2, LOW);
  42.  
  43. }
  44.  
  45. void loop() {
  46.  
  47. // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  48. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  49. digitalWrite(trigPin, LOW);
  50. digitalWrite(trigPin, HIGH);
  51.  
  52.  
  53. // Read the signal from the sensor: a HIGH pulse whose
  54. // duration is the time (in microseconds) from the sending
  55. // of the ping to the reception of its echo off of an object.
  56. pinMode(echoPin, INPUT);
  57. duration = pulseIn(echoPin, HIGH);
  58.  
  59. // Convert the time into a distance
  60. currentPosition = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
  61. error_ = computePID(currentPosition);
  62. if (error_ < -5){
  63. error_ = -5;
  64. }
  65. output = map(error_, 0,-5 , 187, 192);
  66. //
  67. // if (output < 0){
  68. // output = 190;
  69. // }
  70. // if (currentPosition <= 25){
  71. // output = 187;
  72. // delay(10000);
  73. // }
  74. analogWrite(pwm, output);
  75.  
  76. Serial.println(String(currentPosition) + "," + String(kp) + "," + String(ki) + "," + String(kd) + "," + String(SetP) + "," + String(output) + "," + String(error_));
  77. }
  78.  
  79. double computePID(double inp){
  80. currentTime = millis(); //get current time
  81. elapsedTime = (double)(currentTime - previousTime); //compute time elapsed from previous computation
  82.  
  83. error = SetP - inp; // determine error
  84. cumError += error * elapsedTime; // compute integral
  85. rateError = (error - lastError)/elapsedTime; // compute derivative
  86. double out = kp*error + ki*cumError + kd*rateError; //PID output
  87. lastError = error; //remember current error
  88. previousTime = currentTime; //remember current time
  89. return out;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement