Advertisement
Guest User

Untitled

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