Graymist

Obs_Avoid

Feb 23rd, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. #include <Servo.h>
  2. #include <NewPing.h>
  3. #include <ZumoMotors.h>
  4. #include <ZumoBuzzer.h>
  5. #include <Pushbutton.h>
  6.  
  7. #define TRIGGER_PIN  5
  8. #define ECHO_PIN     4
  9. #define MAX_DISTANCE 200
  10.  
  11. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
  12. ZumoBuzzer buzzer;
  13. ZumoMotors motors;
  14. Pushbutton button(ZUMO_BUTTON);
  15.  
  16. Servo myservo;
  17.  
  18. int pos = 90; // pos for position and set a 0 degrees
  19. int lastError1 = 0; // Used for Kd
  20. int lastError2 = 0;
  21. const int MAX_SPEED = 200; // Max speed up to 400
  22. int m1Speed = 0;
  23. int m2Speed = 0;
  24.  
  25. void setup()
  26. {
  27.   Serial.begin(9600);
  28.     // Play a little welcome song
  29.   buzzer.play(">g32>>c32");
  30.   // Wait for the user button to be pressed and released
  31.    button.waitForButton();
  32.    
  33.    // Turn on LED to indicate we are in calibration mode
  34.   pinMode(13, OUTPUT);
  35.   digitalWrite(13, HIGH);
  36.    
  37.   // Serial.begin (9600); Used for troubleshooting
  38.   myservo.attach(11);
  39.   myservo.write(89);
  40.    
  41.     // Turn off LED to indicate we are through with calibration
  42.   digitalWrite(13, LOW);
  43.   buzzer.play(">g32>>c32");
  44.  
  45.   // Wait for the user button to be pressed and released
  46.   button.waitForButton();
  47.  
  48.   // Play music and wait for it to finish before we start driving.
  49.   buzzer.play("L16 cdegreg4");
  50.   while(buzzer.isPlaying());
  51.  
  52.   delay(500);  
  53. }
  54.  
  55. void loop()
  56. {
  57.     myservo.write(36);  // check distance to the right
  58.     delay(400);
  59.     int rightDistance = sonar.ping_cm(); //set right distance in cm
  60.     delay(100);
  61.     myservo.write(144);  // check distace to the left
  62.     delay(400);
  63.     int leftDistance = sonar.ping_cm(); //set left distance in cm
  64.     delay(100);
  65.     myservo.write(90); //return to center
  66.     delay(100);
  67.    
  68.   Serial.print("Right: ");
  69.   Serial.print(rightDistance);
  70.   Serial.print(" cm  ");
  71.   Serial.print("Left: ");
  72.   Serial.print(leftDistance);
  73.   Serial.println(" cm");
  74.  
  75.     int error1 = 15 - rightDistance; //distance is in cm
  76.     int error2 = 15 - leftDistance;
  77.    
  78.   // Get motor speed difference using proportional and derivative PID terms
  79.   // (the integral term is generally not very useful for line following).
  80.   // Here we are using a proportional constant of 1/4 and a derivative
  81.   // constant of 6, which should work decently for many Zumo motor choices.
  82.   // You probably want to use trial and error to tune these constants for
  83.   // your particular Zumo and line course.
  84.  
  85.   // Error 1 is from the right distance reading feeding into the
  86.   // left motor control
  87.   // Error 2 is from the left distance reading feeding into the
  88.   // right motor control
  89.  
  90.  
  91.     int speedDifference = error1 / 4 + 6 * (error1 - lastError1);
  92.     int speedDifference2 = error2 / 4 + 6 * (error2 - lastError2);
  93.    
  94.     // Defining the difference between the two reading for the
  95.     // differential controller
  96.    
  97.     lastError1 = error1;
  98.     lastError2 = error2;
  99.    
  100.     // Uses the right error to drive the left wheel
  101.     // Uses the left error to drive the right wheel
  102.     m1Speed = MAX_SPEED - speedDifference;
  103.     m2Speed = MAX_SPEED - speedDifference2;
  104.      
  105.     // Limits the wheel speed so that if theres a negative
  106.     // speed difference the wheel speed does go over max
  107.  
  108.   if (m1Speed > MAX_SPEED)
  109.     m1Speed = MAX_SPEED;
  110.   if (m2Speed > MAX_SPEED)
  111.     m2Speed = MAX_SPEED;
  112.  
  113.   motors.setSpeeds(m1Speed, m2Speed);
  114.    
  115. }
Advertisement
Add Comment
Please, Sign In to add comment