Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include RedBotMotors motors; RedBotEncoder encoder = RedBotEncoder(A2, A3); int buttonPin = 12; int countsPerRev = 192; // 4 pairs of N-S x 48:1 gearbox =

#include RedBotMotors motors; RedBotEncoder encoder = RedBotEncoder(A2, A3); int buttonPin = 12; int countsPerRev = 192; // 4 pairs of N-S x 48:1 gearbox = 192 ticks per wheel rev float wheelDiam = 2.56; // diam = 65mm / 25.4 mm/in float wheelCirc = PI*wheelDiam; // Redbot wheel circumference = pi*D

int iterations; void setup() { pinMode(buttonPin, INPUT_PULLUP); Serial.begin(9600); } void loop(void) { // set the power for left & right motors on button press //if (digitalRead(buttonPin) == LOW) //{ if (iterations < 12){ driveStraight(39.37, 90); delay(500); turn(4, 90); iterations++; } if (iterations == 12){ stop (); } } void driveStraight(float distance, int motorPower) { long lCount = 0; long rCount = 0; long targetCount; float numRev; // variables for tracking the left and right encoder counts long prevlCount, prevrCount; long lDiff, rDiff; // diff between current encoder count and previous count // variables for setting left and right motor power float leftPower = motorPower; float rightPower = motorPower -14; // variable used to offset motor power on right vs left to keep straight. int offsetDrive = 2; // offset amount to compensate Right vs. Left drive numRev = distance / wheelCirc; // calculate the target # of rotations targetCount = numRev * countsPerRev; // calculate the target count // debug Serial.print("driveStraight() "); Serial.print(distance); Serial.print(" inches at "); Serial.print(motorPower); Serial.println(" power."); Serial.print("Target: "); Serial.print(numRev, 3); Serial.println(" revolutions."); Serial.println(); // print out header Serial.print("Left\t"); // "Left" and tab Serial.print("Right\t"); // "Right" and tab Serial.println("Target count"); Serial.println("============================"); encoder.clearEnc(BOTH); // clear the encoder count delay(100); // short delay before starting the motors. motors.drive(motorPower); // start motors while (rCount < targetCount) { // while the right encoder is less than the target count -- debug print // the encoder values and wait -- this is a holding loop. lCount = encoder.getTicks(LEFT); rCount = encoder.getTicks(RIGHT); Serial.print(lCount); Serial.print("\t"); Serial.print(rCount); Serial.print("\t"); Serial.println(targetCount); motors.leftDrive(leftPower); motors.rightDrive(rightPower); // calculate the rotation "speed" as a difference in the count from previous cycle. lDiff = (lCount - prevlCount); rDiff = (rCount - prevrCount); // store the current count as the "previous" count for the next cycle. prevlCount = lCount; prevrCount = rCount; // if left is faster than the right, slow down the left / speed up right if (lDiff > rDiff) { leftPower = leftPower - offsetDrive; rightPower = rightPower + offsetDrive; } // if right is faster than the left, speed up the left / slow down right else if (lDiff < rDiff) { leftPower = leftPower + offsetDrive; rightPower = rightPower - offsetDrive; } delay(50); // short delay to give motors a chance to respond. } // now apply "brakes" to stop the motors. motors.brake(); }

void turn(float distance, int motorPower) { long lCount = 0; long rCount = 0; long targetCount; float numRev; // variables for tracking the left and right encoder counts long prevlCount, prevrCount; long lDiff, rDiff; // diff between current encoder count and previous count // variables for setting left and right motor power int leftPower = -motorPower; int rightPower = motorPower -8; // variable used to offset motor power on right vs left to keep straight. int offsetTurn = 2; // offset amount to compensate Right vs. Left drive numRev = distance / wheelCirc; // calculate the target # of rotations targetCount = numRev * countsPerRev; // calculate the target count // debug Serial.print("driveStraight() "); Serial.print(distance); Serial.print(" inches at "); Serial.print(motorPower); Serial.println(" power."); Serial.print("Target: "); Serial.print(numRev, 3); Serial.println(" revolutions."); Serial.println(); // print out header Serial.print("Left\t"); // "Left" and tab Serial.print("Right\t"); // "Right" and tab Serial.println("Target count"); Serial.println("============================"); encoder.clearEnc(BOTH); // clear the encoder count delay(100); // short delay before starting the motors. motors.drive(motorPower); // start motors while (rCount < targetCount) { // while the right encoder is less than the target count -- debug print // the encoder values and wait -- this is a holding loop. lCount = encoder.getTicks(LEFT); rCount = encoder.getTicks(RIGHT); Serial.print(lCount); Serial.print("\t"); Serial.print(rCount); Serial.print("\t"); Serial.println(targetCount); motors.leftDrive(leftPower); motors.rightDrive(rightPower); // calculate the rotation "speed" as a difference in the count from previous cycle. lDiff = (lCount - prevlCount); rDiff = (rCount - prevrCount); // store the current count as the "previous" count for the next cycle. prevlCount = lCount; prevrCount = rCount; // if left is faster than the right, slow down the left / speed up right if (lDiff > rDiff) { leftPower = leftPower - offsetTurn; rightPower = rightPower + offsetTurn; } // if right is faster than the left, speed up the left / slow down right else if (lDiff < rDiff) { leftPower = leftPower + offsetTurn; rightPower = rightPower - offsetTurn; } delay(50); // short delay to give motors a chance to respond. } // now apply "brakes" to stop the motors. motors.brake(); }

void stop(){ motors.brake(); }

-----------------------------------------------------------------------

#define trigPin 13 #define echoPin 12 #define led 11 #define led2 10

void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(led, OUTPUT); pinMode(led2, OUTPUT); }

void loop() { long duration, distance; digitalWrite(trigPin, LOW); // Added this line delayMicroseconds(2); // Added this line digitalWrite(trigPin, HIGH); // delayMicroseconds(1000); - Removed this line delayMicroseconds(10); // Added this line digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if (distance < 4) { // This is where the LED On/Off happens digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off digitalWrite(led2,LOW); } else { digitalWrite(led,LOW); digitalWrite(led2,HIGH); } if (distance >= 200 || distance <= 0){ Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); } delay(500); }

===================================================

I need the first code to adjested to make my robot follow an object and keeping a distance of 1m.The vehicle may move forward, move backwards, may remain stationary, and may turn down another hallway.

We just need one ultrasonic for following the robot

I need the code for this Assignment ASAP!!!!!!!!!!!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

Technology. Refer to Case

Answered: 1 week ago