Advertisement
Wolvenspud

robot

Mar 25th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.19 KB | None | 0 0
  1. #include <Servo.h>
  2. // Pins & Motors
  3. const byte PIN_ANALOG_X = 0;
  4. const byte PIN_ANALOG_Y = 1;
  5. const byte PIN_BUTTON_LEFT = 2;
  6. const byte PIN_BUTTON_DOWN = 3;
  7. const byte PIN_BUTTON_RIGHT = 4;
  8. const byte PIN_BUTTON_UP = 5;
  9. Servo s1;
  10. Servo s2;
  11. #define echoPin 10
  12. #define trigPin 9
  13. // Mode switch
  14. enum mode {autonomous, autonomousNoMap, manual} ;
  15. mode currentMode;
  16. // Speeds & Directional vars
  17. int leftWheelSpeed = 84;// 80 ~ 90
  18. int rightWheelSpeed = 96;// 100 ~ 90
  19. int upperLimDif = 10;
  20. bool forwards = false;
  21. bool backwards = false;
  22. bool waitForRightButtonUp = false;
  23. bool waitForDownButtonUp = false;
  24. bool waitForUpButtonUp = false;
  25. bool waitForLeftButtonUp = false;
  26. float turnIntervalTimer = 0;
  27. bool stopped = false;
  28. // Navigation and Mapping
  29. int leftDistance;
  30. int rightDistance;
  31. int checkDelay = 300;
  32. int turnDelay = 400;
  33. int map2d[10][10] = {
  34.   {2, 2, 2, 2, 2,  2, 2, 2, 2, 2},
  35.   {2, 2, 2, 2, 2,  2, 2, 2, 2, 2},
  36.   {2, 2, 2, 2, 2,  2, 2, 2, 2, 2},
  37.   {2, 2, 2, 2, 2,  2, 2, 2, 2, 2},
  38.   {2, 2, 2, 2, 2,  2, 2, 2, 2, 2},
  39.  
  40.   {2, 2, 2, 2, 2,  2, 2, 2, 2, 2},
  41.   {2, 2, 2, 2, 2,  2, 2, 2, 2, 2},
  42.   {2, 2, 2, 2, 2,  2, 2, 2, 2, 2},
  43.   {2, 2, 2, 2, 2,  2, 2, 2, 2, 2},
  44.   {2, 2, 2, 2, 2,  2, 2, 2, 2, 2}
  45. };
  46. // Ultrasonic
  47. long duration;
  48. int forwardsDistance;
  49.  
  50.  
  51. void setup() {
  52.   currentMode = autonomousNoMap;
  53.   Serial.begin(9600);
  54.   s1.attach(13);
  55.   s2.attach(12);
  56.   //pinMode(9, OUTPUT);
  57.   //pinMode(10, OUTPUT);
  58.   pinMode(echoPin, INPUT);
  59.   pinMode(trigPin, OUTPUT);
  60.   pinMode(PIN_BUTTON_RIGHT, INPUT);
  61.   digitalWrite(PIN_BUTTON_RIGHT, HIGH);
  62.   pinMode(PIN_BUTTON_LEFT, INPUT);
  63.   digitalWrite(PIN_BUTTON_LEFT, HIGH);
  64.   pinMode(PIN_BUTTON_UP, INPUT);
  65.   digitalWrite(PIN_BUTTON_UP, HIGH);
  66.   pinMode(PIN_BUTTON_DOWN, INPUT);
  67.   digitalWrite(PIN_BUTTON_DOWN, HIGH);
  68.   map2d[1][4] = 0;
  69.   Serial.println(map2d[1][4]);
  70. }
  71.  
  72. void loop() {
  73.   switch(currentMode){
  74.     case manual:
  75.       ManualDrive();
  76.       break;
  77.     case autonomous:
  78.       Auto();
  79.       break;
  80.     case autonomousNoMap:
  81.       AutoNoMap();
  82.       break;
  83.   }
  84. }
  85. // Manual
  86. void ManualDrive(){
  87.   // Button press to INCREASE speed
  88.   if (digitalRead(PIN_BUTTON_DOWN) == LOW && !waitForUpButtonUp && upperLimDif < 10){
  89.     upperLimDif += 1;
  90.     waitForUpButtonUp = false;
  91.   }
  92.   if (waitForUpButtonUp && digitalRead(PIN_BUTTON_UP) == HIGH) {
  93.     waitForUpButtonUp = false;
  94.   }
  95.  
  96.   // Button press to DECREASE speed
  97.   if (digitalRead(PIN_BUTTON_DOWN) == LOW && !waitForDownButtonUp && upperLimDif > 0) {
  98.     upperLimDif -= 1;
  99.     waitForDownButtonUp = true;
  100.   }
  101.   if (waitForDownButtonUp && digitalRead(PIN_BUTTON_DOWN) == HIGH) {
  102.     waitForDownButtonUp = false;
  103.   }
  104.  
  105.   // Y-axis to directional change per second
  106.   int Y = analogRead(PIN_ANALOG_Y);
  107.   rightWheelSpeed = map(Y, 512, 1023, 90+upperLimDif, 90);
  108.   leftWheelSpeed = map(Y, 512, 0, 90-upperLimDif, 90);
  109.  
  110.   // Forwards
  111.   int X = analogRead(PIN_ANALOG_X);
  112.   if (X > 510) {
  113.     backwards = false;
  114.     forwards = true;
  115.   }
  116.   // Backwards
  117.   else if (X < 500) {
  118.     forwards = false;
  119.     backwards = true;
  120.   }
  121.  
  122.   // Echo
  123.   forwardsDistance = GetDistance();
  124.  
  125.   // Drive
  126.   if (X > 500 && X < 510) {
  127.     s1.write(90);
  128.     s2.write(90);
  129.   }
  130.   else if (forwards && forwardsDistance > 10) {
  131.     s1.write(rightWheelSpeed);
  132.     s2.write(leftWheelSpeed);
  133.   }
  134.   else if (backwards) {
  135.     s1.write(180 - rightWheelSpeed);
  136.     s2.write(180 - leftWheelSpeed);
  137.   }
  138. }
  139.  
  140. void AutoNoMap(){
  141.   forwardsDistance = GetDistance();
  142.   if (forwardsDistance > 10 && !stopped) {
  143.     s1.write(100);
  144.     s2.write(80);
  145.   }
  146.   else if (!stopped){
  147.     //CheckLeft();
  148.     //CheckRight();
  149.     TurnLeft(checkDelay);
  150.     leftDistance = GetDistance();
  151.     TurnRight(2*checkDelay);
  152.     rightDistance = GetDistance();
  153.     if (rightDistance > leftDistance && rightDistance > 10){
  154.       TurnRight(turnDelay-checkDelay);
  155.       Forwards(2000);
  156.       TurnLeft(turnDelay);
  157.       Forwards(0);
  158.     }
  159.     else if (leftDistance > 10){
  160.       TurnLeft(checkDelay+turnDelay);
  161.       Forwards(2000);
  162.       TurnRight(turnDelay);
  163.       Forwards(0);
  164.     }
  165.     else{
  166.       stopped = true;
  167.     }
  168.   }
  169.   else{
  170.     s1.write(90);
  171.     s2.write(90);
  172.   }
  173.   // Stop
  174.   if (digitalRead(PIN_BUTTON_UP) == LOW && !waitForUpButtonUp) {
  175.     stopped = !stopped;
  176.     waitForUpButtonUp = true;
  177.   }
  178.   if (waitForUpButtonUp && digitalRead(PIN_BUTTON_UP == HIGH)) {
  179.     waitForUpButtonUp = false;
  180.   }
  181. }
  182.  
  183. void Auto(){
  184.  
  185. }
  186. // Movement related Functions
  187. void Forwards(int _delay){
  188.   Serial.println(";lkxfngsagoansrg");
  189.   s1.write(100);
  190.   s2.write(80);
  191.   delay(_delay);
  192.  
  193. }
  194. void TurnLeft(int _delay){
  195.   s1.write(80);
  196.   s2.write(80);
  197.   delay(_delay);
  198. }
  199. void TurnRight(int _delay){
  200.   s1.write(100);
  201.   s2.write(100);
  202.   delay(_delay);
  203. }
  204. void CheckLeft() {
  205.   TurnLeft(checkDelay);
  206.   leftDistance =  GetDistance();
  207.   TurnRight(checkDelay);
  208. }
  209.  
  210. void CheckRight() {
  211.   TurnRight(checkDelay);
  212.   leftDistance =  GetDistance();
  213.   TurnLeft(checkDelay);
  214. }
  215.  
  216. int GetDistance(){
  217.   digitalWrite(trigPin, LOW);
  218.   delayMicroseconds(2);
  219.   digitalWrite(trigPin, HIGH);
  220.   delayMicroseconds(10);
  221.   digitalWrite(trigPin, LOW);
  222.   duration = pulseIn(echoPin, HIGH);
  223.   return duration * 0.034 / 2;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement