Advertisement
Guest User

ard

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