Advertisement
MrLunk

3 front sonar sensors for a autonomous car.

Mar 13th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.83 KB | None | 0 0
  1. // Car finds middle path between right and left obstacles / walls / track boundaries ... MrLunk.net march-2017(gnu)
  2. // Max track widt 2 meters.
  3. // Max Right steer = 90+25 degrees servo angle. (max 115)
  4. // Max Left steer = 90-25 degrees servo angle. (min 65)
  5.  
  6. #include <Servo.h>
  7.  
  8. // Define Sonar-Sensor Pinout numbers
  9. const int trigPinOne = 5;
  10. const int trigPinTwo = 6;
  11. const int trigPinThree = 7;
  12. const int echoPinOne = 8;
  13. const int echoPinTwo = 9;
  14. const int echoPinThree = 10;
  15.  
  16. Servo SteeringServo;
  17.  
  18. // define variables
  19. long duration;
  20. int Direction = 90;
  21. int Distance;
  22. int LeftDistance = 100;
  23. int RightDistance = 100;
  24. int MiddleDistance = 100;
  25. int pos = 90;
  26. int MinSideDistance = 100; // min Distance to side sensors before proportional steering kicks in.
  27.  
  28. void setup() {
  29.   Serial.begin(9600);
  30.   pinMode(trigPinOne, OUTPUT);
  31.   pinMode(echoPinOne, INPUT);
  32.   pinMode(trigPinTwo, OUTPUT);
  33.   pinMode(echoPinTwo, INPUT);
  34.   pinMode(trigPinThree, OUTPUT);
  35.   pinMode(echoPinThree, INPUT);
  36.   digitalWrite(trigPinOne, LOW);
  37.   digitalWrite(trigPinTwo, LOW);
  38.   digitalWrite(trigPinThree, LOW);
  39.   SteeringServo.attach(11);  
  40.   delay (10);
  41.   SteeringServo.write(90);  
  42. }
  43.  
  44. // --------------Main Routine definitions------------------------------
  45.  
  46. void loop() {
  47.  
  48.   // ---- Start Front Sensor Polling
  49.   SonarSensor(trigPinOne, echoPinOne); // Poll Left Sonar-Sensor
  50.   int LeftDistance = Distance;
  51.   delay(10);
  52.   SonarSensor(trigPinThree, echoPinThree); // Poll Right Sonar-Sensor
  53.   int RightDistance = Distance;
  54.   delay(10);
  55.   SonarSensor(trigPinTwo, echoPinTwo); // Poll Middle Sonar-Sensor
  56.   int MiddleDistance = Distance;
  57.   delay(10);
  58.   // ---- End Front Sensor Polling ----
  59.  
  60.   int LeftSteer = map(RightDistance,20,MinSideDistance,0,-25); // Proportional LeftSteer to Left object/wall distance
  61.   if (LeftSteer <= -25){LeftSteer = -25;} // Max Right Sensor Error Protection
  62.  
  63.   int RightSteer = map(LeftDistance,20,MinSideDistance,0,25); // Proportional RightSteer to Left object/wall distance
  64.   if (LeftSteer >= 25){LeftSteer = 25;} // Max Left Sensor Error Protection
  65.  
  66.   int Direction = (90+LeftSteer+RightSteer); // Final Direction calculation
  67.   if (Direction <= 65){Direction = 65;}    // Max SteerLeft protection
  68.   if (Direction >= 115){Direction = 115;}  // Max SteerRight Protection
  69.  
  70.   SteeringServo.write(Direction); // Send Final direction to Servo
  71.  
  72.   delay(50);
  73. }
  74.  
  75. // --------------Sub-Routine definitions------------------------------
  76.  
  77. void Forward(){
  78. }
  79.  
  80. void Reverse(){
  81. }
  82.  
  83. void SonarSensor(int trigPin,int echoPin){
  84.   digitalWrite(trigPin, LOW);
  85.   delayMicroseconds(2);
  86.   digitalWrite(trigPin, HIGH);
  87.   delayMicroseconds(10);
  88.   digitalWrite(trigPin, LOW);
  89.   duration = pulseIn(echoPin, HIGH);
  90.   Distance = (duration/2) / 29.1;
  91.   if (Distance > MinSideDistance) {
  92.     Distance=MinSideDistance;
  93.   }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement