Jump to content


Photo

Need Help on Robot Programming ASAP

robot programming needhelp asap help

  • Please log in to reply
1 reply to this topic

#1 professorpancake21

professorpancake21

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 16 April 2015 - 01:34 AM

Hello everyone! I made an Arduino Uno robot using the guide here: http://www.foxytroni...bot/programming

 

And here's the problem: When the robot detects an object in front of it, it turns left. I want it to back up for 2 seconds and then turn left.

 

I am new to Arduino programming, so I need the code for my robot. Please modify and reply the following code:

 

#include <Servo.h>

// create servo objects
Servo leftMotor;
Servo rightMotor;

const int serialPeriod = 250; // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 20; // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay = 0;

// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 8;
const int ultrasonic2EchoPin = 9;

int ultrasonic2Distance;
int ultrasonic2Duration;

// define the states
#define DRIVE_FORWARD 0
#define TURN_LEFT 1

int state = DRIVE_FORWARD; // 0 = drive forward (DEFAULT), 1 = turn left

void setup()
{
Serial.begin(9600);

// ultrasonic sensor pin configurations
pinMode(ultrasonic2TrigPin, OUTPUT);
pinMode(ultrasonic2EchoPin, INPUT);

leftMotor.attach(13);
rightMotor.attach(12);
}


void loop()
{
debugOutput(); // prints debugging messages to the serial console

if(millis() - timeLoopDelay >= loopPeriod)
{
readUltrasonicSensors(); // read and store the measured distances

stateMachine();

timeLoopDelay = millis();
}
}


void stateMachine()
{
if(state == DRIVE_FORWARD) // no obstacles detected
{
if(ultrasonic2Distance > 6 || ultrasonic2Distance < 0) // if there's nothing in front of us (note: ultrasonicDistance will be negative for some ultrasonics if there's nothing in range)
{
// drive forward
rightMotor.write(180);
leftMotor.write(0);
}
else // there's an object in front of us
{
state = TURN_LEFT;
}
}
else if(state == TURN_LEFT) // obstacle detected -- turn left
{
unsigned long timeToTurnLeft = 1100; // it takes around 1.1 seconds to turn 90 degrees

unsigned long turnStartTime = millis(); // save the time that we started turning

while((millis()-turnStartTime) < timeToTurnLeft) // stay in this loop until timeToTurnLeft (1.1 seconds) has elapsed
{
// turn left
rightMotor.write(180);
leftMotor.write(180);
}

state = DRIVE_FORWARD;
}
}


void readUltrasonicSensors()
{
// ultrasonic 2
digitalWrite(ultrasonic2TrigPin, HIGH);
delayMicroseconds(10); // must keep the trig pin high for at least 10us
digitalWrite(ultrasonic2TrigPin, LOW);

ultrasonic2Duration = pulseIn(ultrasonic2EchoPin, HIGH);
ultrasonic2Distance = (ultrasonic2Duration/2)/29;
}


void debugOutput()
{
if((millis() - timeSerialDelay) > serialPeriod)
{
Serial.print("ultrasonic2Distance: ");
Serial.print(ultrasonic2Distance);
Serial.print("cm");
Serial.println();

timeSerialDelay = millis();
}
}

 

This code is from the guide I used. Please modify and reply this code so that my robot will back up and then turn left when it detects an obstacle.

 

I need it ASAP!!

 

Thanks so much in advance!

 

:)


  • 0

#2 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 18 April 2015 - 05:45 PM

Hi,

 

Sorry for not replying sooner.

 

When the robot detects an object in front of it, it turns left. I want it to back up for 2 seconds and then turn left.

 

Ok, that shouldn't be too hard to do.

 

The TURN_LEFT state looks like this:

    else if(state == TURN_LEFT) // obstacle detected -- turn left
    {
        unsigned long timeToTurnLeft = 500; // it takes around .5 seconds to turn 90 degrees
        
        unsigned long turnStartTime = millis(); // save the time that we started turning

        while((millis()-turnStartTime) < timeToTurnLeft) // stay in this loop until timeToTurnLeft (.5 seconds) has elapsed
        {
            // turn left
            rightMotor.write(180);
            leftMotor.write(180);
        }
        
        state = DRIVE_FORWARD;
    }

If you want the robot to backup for 2 seconds before turning left, you can just add the additional code above the while loop above:

else if(state == TURN_LEFT) // obstacle detected -- turn left
{
	unsigned long timeToTurnLeft = 500; // it takes around .5 seconds to turn 90 degrees
	unsigned long timeToBackUp   = 2000; // back up for 2 seconds
	
	unsigned long backUpStartTime = millis(); // save the time that we started backing up

	while((millis()-backUpStartTime) < timeToBackUp) // stay in this loop until timeToTurnLeft (.5 seconds) has elapsed
	{
		// back up (you may have to switch these values depending on how you oriented the motors)
		rightMotor.write(0);
		leftMotor.write(180);
	}
	
	unsigned long turnStartTime = millis(); // save the time that we started turning

	while((millis()-turnStartTime) < timeToTurnLeft) // stay in this loop until timeToTurnLeft (.5 seconds) has elapsed
	{
		// turn left
		rightMotor.write(180);
		leftMotor.write(180);
	}
	
	state = DRIVE_FORWARD;
}

That's untested, but I think it should work!

 


  • 0




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users