Advertisement
MrLunk

4 wheel 4 DC-motor Rover with 3 forward facing sonar sensors

Jul 7th, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 4.85 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_MotorShield.h>
  3.  
  4. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  5.  
  6. Adafruit_DCMotor *MyMotor001 = AFMS.getMotor(1);
  7. Adafruit_DCMotor *MyMotor002 = AFMS.getMotor(2);
  8. Adafruit_DCMotor *MyMotor003 = AFMS.getMotor(3);
  9. Adafruit_DCMotor *MyMotor004 = AFMS.getMotor(4);
  10.  
  11. // Define Sonar-Sensor Pinout numbers
  12. const int trigPinBack = 3;
  13. const int echoPinBack = 4;
  14. const int trigPinOne = 5;
  15. const int echoPinOne = 6;
  16. const int trigPinTwo = 7;
  17. const int echoPinTwo = 8;
  18. const int trigPinThree = 11;
  19. const int echoPinThree = 12;
  20.  
  21. // define variables
  22. long duration;
  23. int Distance;
  24. String oneLine;
  25. int Speed = 80;
  26.  
  27. void setup() {
  28.   Serial.begin(9600);
  29.   pinMode(trigPinBack, OUTPUT);
  30.   pinMode(echoPinBack, INPUT);
  31.   pinMode(trigPinOne, OUTPUT);
  32.   pinMode(echoPinOne, INPUT);
  33.   pinMode(trigPinTwo, OUTPUT);
  34.   pinMode(echoPinTwo, INPUT);
  35.   pinMode(trigPinThree, OUTPUT);
  36.   pinMode(echoPinThree, INPUT);
  37.   digitalWrite(trigPinOne, LOW);
  38.   digitalWrite(trigPinTwo, LOW);
  39.   digitalWrite(trigPinThree, LOW);
  40.  
  41.   AFMS.begin();
  42.  
  43.   // ---------------- Initialise and neutralise all motors ---------
  44.   MyMotor001->setSpeed(Speed);
  45.   MyMotor001->run(FORWARD);
  46.   MyMotor001->run(RELEASE);
  47.   MyMotor002->setSpeed(Speed);
  48.   MyMotor002->run(FORWARD);
  49.   MyMotor002->run(RELEASE);
  50.   MyMotor003->setSpeed(Speed);
  51.   MyMotor003->run(FORWARD);
  52.   MyMotor003->run(RELEASE);
  53.   MyMotor004->setSpeed(Speed);
  54.   MyMotor004->run(FORWARD);
  55.   MyMotor004->run(RELEASE);
  56. }
  57.  
  58. // --------------Main Routine definitions------------------------------
  59.  
  60. void loop() {
  61.  
  62.   // ---- Start Sensor Polling
  63.   SonarSensor(trigPinOne, echoPinOne); // Poll Left Sonar-Sensor
  64.   int RightDistance = Distance;
  65.   delay(10);
  66.   SonarSensor(trigPinThree, echoPinThree); // Poll Right Sonar-Sensor
  67.   int LeftDistance = Distance;
  68.   delay(10);
  69.   SonarSensor(trigPinTwo, echoPinTwo); // Poll Middle Sonar-Sensor
  70.   int MiddleDistance = Distance;
  71.   delay(10);
  72.   SonarSensor(trigPinBack, echoPinBack); // Poll Backward Sonar-Sensor
  73.   int BackDistance = Distance;
  74.   delay(10);
  75.   // ---- End Sensor Polling ----
  76.  
  77.   // ------------------ Direction choices --------------------------------
  78.  
  79.   int MinMiddle = 15;
  80.   int MinLeftRight = 20;
  81.  
  82.   if (MiddleDistance > MinMiddle && LeftDistance > MinLeftRight && RightDistance > MinLeftRight) {
  83.     Forward();
  84.   }
  85.   else if (MiddleDistance <= MinMiddle) {
  86.     if (LeftDistance < RightDistance) {
  87.       TurnRight();
  88.     }
  89.     else if (RightDistance < LeftDistance) {
  90.       TurnLeft();
  91.     }
  92.   }
  93.  
  94.   // ---------------------------+++++++
  95.  
  96.   if (MiddleDistance > 30 && LeftDistance <= 20 || MiddleDistance > 30 && RightDistance <= 20) {
  97.     if (LeftDistance <= 20) {
  98.       TurnRight();
  99.     }
  100.     else if (RightDistance <= 20) {
  101.       TurnLeft();
  102.     }
  103.   }
  104.  
  105.   // ---- Create serial reporting String oneLine :
  106.   // "L:" + LeftDistance + " M:" + MiddleDistance + " R:" + RightDistance + " B:" + BackDistance
  107.  
  108.   /*oneLine += " L:";
  109.     oneLine += LeftDistance;
  110.     oneLine += " M:";
  111.     oneLine += MiddleDistance;
  112.     oneLine += " R:";
  113.     oneLine += RightDistance;
  114.     oneLine += " B:";
  115.     oneLine += BackDistance;
  116.     Serial.println(oneLine);
  117.     oneLine = "";
  118.   */
  119. }
  120.  
  121. // --------------Sub-Routine definitions------------------------------
  122.  
  123. void Forward() {
  124.   MyMotor001->run(BACKWARD);
  125.   MyMotor002->run(BACKWARD);
  126.   MyMotor003->run(BACKWARD);
  127.   MyMotor004->run(BACKWARD);
  128.   /*delay(500);
  129.     MyMotor001->run(RELEASE);
  130.     MyMotor002->run(RELEASE);
  131.     MyMotor003->run(RELEASE);
  132.     MyMotor004->run(RELEASE);
  133.   */
  134. }
  135.  
  136. void Backward() {
  137.   MyMotor001->run(FORWARD);
  138.   MyMotor002->run(FORWARD);
  139.   MyMotor003->run(FORWARD);
  140.   MyMotor004->run(FORWARD);
  141.   delay(500);
  142.   MyMotor001->run(BACKWARD);
  143.   MyMotor002->run(BACKWARD);
  144.   MyMotor003->run(FORWARD);
  145.   MyMotor004->run(FORWARD);
  146.   /*delay(500);
  147.     MyMotor001->run(RELEASE);
  148.     MyMotor002->run(RELEASE);
  149.     MyMotor003->run(RELEASE);
  150.     MyMotor004->run(RELEASE);
  151.   */
  152. }
  153.  
  154. void TurnRight() {
  155.   MyMotor001->run(BACKWARD);
  156.   MyMotor002->run(BACKWARD);
  157.   MyMotor003->run(FORWARD);
  158.   MyMotor004->run(FORWARD);
  159.   /*delay(500);
  160.     MyMotor001->run(RELEASE);
  161.     MyMotor002->run(RELEASE);
  162.     MyMotor003->run(RELEASE);
  163.     MyMotor004->run(RELEASE);
  164.   */
  165. }
  166.  
  167. void TurnLeft() {
  168.   MyMotor001->run(FORWARD);
  169.   MyMotor002->run(FORWARD);
  170.   MyMotor003->run(BACKWARD);
  171.   MyMotor004->run(BACKWARD);
  172.   /*delay(500);
  173.     MyMotor001->run(RELEASE);
  174.     MyMotor002->run(RELEASE);
  175.     MyMotor003->run(RELEASE);
  176.     MyMotor004->run(RELEASE);
  177.   */
  178. }
  179.  
  180. void SonarSensor(int trigPin, int echoPin) {
  181.   digitalWrite(trigPin, LOW);
  182.   delayMicroseconds(2);
  183.   digitalWrite(trigPin, HIGH);
  184.   delayMicroseconds(10);
  185.   digitalWrite(trigPin, LOW);
  186.   duration = pulseIn(echoPin, HIGH);
  187.   Distance = (duration / 2) / 29.1;
  188.   if (Distance >= 100) {
  189.     Distance = 100;
  190.   }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement