Guest User

Untitled

a guest
Oct 25th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   #include <RedBot.h>
  2.  
  3. RedBotMotors motors;
  4.  
  5.  
  6.  
  7.  
  8.  
  9. RedBotBumper lBumper = RedBotBumper(3);  // initialzes bumper object on pin 3
  10. RedBotBumper rBumper = RedBotBumper(11); // initialzes bumper object on pin 11
  11. RedBotEncoder encoder = RedBotEncoder(A2, 10);
  12.  
  13. int buttonPin = 12;
  14. int buzzerPin = 9;
  15.  
  16. int countsPerRev = 192;
  17.  
  18. int lBumperState;  // state variable to store the bumper value
  19. int rBumperState;  // state variable to store the bumper value
  20. float wheelDiam = 2.56;  // diam = 65mm / 25.4 mm/in
  21. float wheelCirc = PI * wheelDiam; // Redbot wheel circumference = pi*D
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. void setup()
  31. {
  32.   pinMode(buttonPin, INPUT_PULLUP);
  33.   pinMode(buzzerPin, OUTPUT);
  34.   Serial.begin(9600);
  35. }
  36.  
  37. void loop(void)
  38. {
  39.   // reverse() function -- backs up at full power
  40.   void reverse()
  41.   {
  42.     motors.drive(-255);
  43.     delay(500);
  44.     motors.stop();
  45.     delay(100);  // short delay to let robot fully stop
  46.   }
  47.  
  48.   // turnRight() function -- turns RedBot to the Right
  49.   void turnRight()
  50.   {
  51.     motors.leftMotor(-150);  // spin CCW
  52.     motors.rightMotor(-150); // spin CCW
  53.     delay(500);
  54.     motors.stop();
  55.     delay(100);  // short delay to let robot fully stop
  56.   }
  57.  
  58.   // turnRight() function -- turns RedBot to the Left
  59.   void turnLeft()
  60.   {
  61.     motors.leftMotor(+150);  // spin CW
  62.     motors.rightMotor(+150); // spin CW
  63.     delay(500);
  64.     motors.stop();
  65.     delay(100);  // short delay to let robot fully stop
  66.   }
  67.  
  68.   void driveStraight(float distance, int motorPower)
  69.   {
  70.     long lCount = 0;
  71.     long rCount = 0;
  72.     long targetCount;
  73.     float numRev;
  74.  
  75.     // variables for tracking the left and right encoder counts
  76.     long prevlCount, prevrCount;
  77.  
  78.     long lDiff, rDiff;  // diff between current encoder count and previous count
  79.  
  80.     // variables for setting left and right motor power
  81.     int leftPower = motorPower;
  82.     int rightPower = motorPower;
  83.  
  84.     // variable used to offset motor power on right vs left to keep straight.
  85.     int offset = 5;  // offset amount to compensate Right vs. Left drive
  86.  
  87.     numRev = distance / wheelCirc;  // calculate the target # of rotations
  88.     targetCount = numRev * countsPerRev;    // calculate the target count
  89.  
  90.     // debug
  91.     Serial.print("driveStraight() ");
  92.     Serial.print(distance);
  93.     Serial.print(" inches at ");
  94.     Serial.print(motorPower);
  95.     Serial.println(" power.");
  96.  
  97.     Serial.print("Target: ");
  98.     Serial.print(numRev, 3);
  99.     Serial.println(" revolutions.");
  100.     Serial.println();
  101.  
  102.     // print out header
  103.     Serial.print("Left\t");   // "Left" and tab
  104.     Serial.print("Right\t");  // "Right" and tab
  105.     Serial.println("Target count");
  106.     Serial.println("============================");
  107.  
  108.     encoder.clearEnc(BOTH);    // clear the encoder count
  109.     delay(100);  // short delay before starting the motors.
  110.  
  111.     motors.drive(motorPower);  // start motors
  112.  
  113.     while (rCount < targetCount)
  114.     {
  115.       // while the right encoder is less than the target count -- debug print
  116.       // the encoder values and wait -- this is a holding loop.
  117.       lCount = encoder.getTicks(LEFT);
  118.       rCount = encoder.getTicks(RIGHT);
  119.       Serial.print(lCount);
  120.       Serial.print("\t");
  121.       Serial.print(rCount);
  122.       Serial.print("\t");
  123.       Serial.println(targetCount);
  124.  
  125.       motors.leftDrive(leftPower);
  126.       motors.rightDrive(rightPower);
  127.  
  128.       // calculate the rotation "speed" as a difference in the count from previous cycle.
  129.       lDiff = (lCount - prevlCount);
  130.       rDiff = (rCount - prevrCount);
  131.  
  132.       // store the current count as the "previous" count for the next cycle.
  133.       prevlCount = lCount;
  134.       prevrCount = rCount;
  135.  
  136.       // if left is faster than the right, slow down the left / speed up right
  137.       if (lDiff > rDiff)
  138.       {
  139.         leftPower = leftPower - offset;
  140.         rightPower = rightPower + offset;
  141.       }
  142.       // if right is faster than the left, speed up the left / slow down right
  143.       else if (lDiff < rDiff)
  144.       {
  145.         leftPower = leftPower + offset;
  146.         rightPower = rightPower - offset;
  147.       }
  148.       delay(50);  // short delay to give motors a chance to respond.
  149.     }
  150.     // now apply "brakes" to stop the motors.
  151.     motors.brake();
  152.   }
  153.   if (digitalRead(buttonPin) == LOW)
  154.   {
  155.     delay(1000);
  156.     driveStraight(999, 150);
  157.     lBumperState = lBumper.read();
  158.     rBumperState = rBumper.read();
  159.  
  160.     if (lBumperState == LOW) // left side is bumped/
  161.     {
  162.       reverse();    // backs up
  163.       turnRight();  // turns
  164.     }
  165.  
  166.     if (rBumperState == LOW) // right side is bumped/
  167.     {
  168.       reverse();   // backs up
  169.       turnLeft();  // turns
  170.     }
  171.   }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment