Teaching my Arduino robot how to drive.

This is my (first) second article in a series on how I build my autonomous Arduino based robot.

The final goal is to be able to set him or her free and watch him or her roaming around without touching anything. In addition I hope to keep my kids busy interacting with the robot, so I have more time making stuff :-).

A fully featured description of the robot and the planned functions will follow at a later point of time. Also all the coding is pretty much borrowed from other tutorials, which I will try to mention if possible.

But back to the topic. Target is to teach the robot some basic moves like:

  • Moving forward
  • Moving backward
  • Turn left 90 degrees
  • Turn right 90 degrees
  • Turn left 180 degrees
  • Turn right 180 degrees
  • Stop
  • Drive for a specific time

To do so you need:

  • A robot with wheels (in my case 2 wheels using the grove motor shield to connect them to the Arduino board)
  • An Arduino programming interface, like the one from Arduino (I use this for this tutorial) or codebender.cc
  • An USB cable
  • A computer obviously
  • A brain with limited functionality (I’ll take mine for this)
  • Some space for test driving

So lets start.

First I include the servo library and define the pins for the motors:

#include <Servo.h>
// pin definition wheel servos
int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead = 100; //variable the speed of motor

robot movement001 300x257 Teaching my Arduino robot how to drive.

The definition can differ, depending on the way you connected the servos

The next step is define the commands to setup the program loop:

void setup()
{
//define pin layout for wheel servos - all of them are outputs
 pinMode(pinI1,OUTPUT);
 pinMode(pinI2,OUTPUT);
 pinMode(speedpinA,OUTPUT);
 pinMode(pinI3,OUTPUT);
 pinMode(pinI4,OUTPUT);
 pinMode(speedpinB,OUTPUT);
}

robot movement002 265x300 Teaching my Arduino robot how to drive.

Now I create the basic functions for moving forward, backward, turning left, and turning right.. Each functions represents a type of movement.

//function to move forward
void moveForward()
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
    digitalWrite(pinI3,LOW);
    digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
    digitalWrite(pinI1,LOW);
}
void moveBackward()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
    digitalWrite(pinI3,HIGH);
    digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
    digitalWrite(pinI1,HIGH);
}
//function to turn right
void moveRight()
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
    digitalWrite(pinI3,LOW);
    digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
    digitalWrite(pinI1,HIGH);
}
//function to turn left
void moveLeft()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
    digitalWrite(pinI3,HIGH);
    digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
    digitalWrite(pinI1,LOW);
}
//function to stop moving
void moveStop()//
{
    digitalWrite(speedpinA,LOW);// disable the pin, to stop the motor. this should be done to avoid damaging the motor.
    digitalWrite(speedpinB,LOW);
}

So now we have the basic functions covered, we have to add some functions to turn the robot in a certain position. This is done via timing (and a lot of testing). How far the robot turns in a specific amount of time depends on what kind of servos you use, how big the wheels are, how much voltage you use to drive the motor, and so on. So you have to try a couple times, till everything work out as it should.

For me a timeframe of 500 milliseconds represents a turn of 90 degrees. In the function I will call the appropriate basic movement function and the turn for the time frame that represents a quarter turn.

void quarterright()//turn 90 degrees to the right
{
 moveRight();
 delay(500);
}
//Function to turn 90 degrees to the left
void quarterleft()//turn 90 degrees to the right
{
 moveLeft();
 delay(500);
}

I’ll do the same for the functions to turn the robot, I just double the time.

void turnaroundl()//turn 180 degrees to the right
{
 moveLeft();
 delay(1000);
}
void turnaroundr()//turn 180 degrees to the right
{
 moveRight();
 delay(1000);
}

Now that I have defined all the basic moves, I can call the function in a loop to let the robot drive as I like to.

void loop()
{
//progamm for robot
 moveForward();
 delay(2000);
 moveStop();
 moveBackward();
 delay(2000);
 moveStop();
// delay(2000);
 quarterleft();
 moveStop();
 quarterright();
 moveStop();
 turnaroundl();
 moveStop();
turnaroundr();
 moveStop();
}

robot movement003 266x300 Teaching my Arduino robot how to drive.

This is a great way to test some basic functions of the robot. If you are not sure you have wired everything the right way. Also it is the basis for the next steps.

Here is the full code example:

#include <Servo.h>
// pin definition wheel servos
int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead = 100; //variable the speed of motor
void setup()
{
//define pin layout for wheel servos - all of them are outputs
 pinMode(pinI1,OUTPUT);
 pinMode(pinI2,OUTPUT);
 pinMode(speedpinA,OUTPUT);
 pinMode(pinI3,OUTPUT);
 pinMode(pinI4,OUTPUT);
 pinMode(speedpinB,OUTPUT);
//define serial port for monitoring so I can print out status messages to a serial monitor
 Serial.begin(9600);
}
void loop()
{
//progamm for robot
 moveForward();
 delay(2000);
 moveStop();
 moveBackward();
 delay(2000);
 moveStop();
// delay(2000);
 quarterleft();
 moveStop();
 quarterright();
 moveStop();
 turnaroundl();
 moveStop();
turnaroundr();
 moveStop();
}
//function to move forward
void moveForward()
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
    digitalWrite(pinI3,LOW);
    digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
    digitalWrite(pinI1,LOW);
}
void moveBackward()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
    digitalWrite(pinI3,HIGH);
    digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
    digitalWrite(pinI1,HIGH);
}
//function to turn right
void moveRight()
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
    digitalWrite(pinI3,LOW);
    digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
    digitalWrite(pinI1,HIGH);
}
//function to turn left
void moveLeft()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
    digitalWrite(pinI3,HIGH);
    digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
    digitalWrite(pinI1,LOW);
}
//function to stop moving
void moveStop()//
{
    digitalWrite(speedpinA,LOW);// disable the pin, to stop the motor. this should be done to avoid damaging the motor.
    digitalWrite(speedpinB,LOW);
}
void quarterright()//turn 90 degrees to the right
{
 moveRight();
 delay(500);
}
//Function to turn 90 degrees to the left
void quarterleft()//turn 90 degrees to the right
{
 moveLeft();
 delay(500);
}
void turnaroundl()//turn 180 degrees to the right
{
 moveLeft();
 delay(1000);
}
void turnaroundr()//turn 180 degrees to the right
{
 moveRight();
 delay(1000);
}

Next time I will make the robot aware, what kind of moving action he or she is performing. And that it is possible to change the movement state if something occurs.

 

You may also like...

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

%d bloggers like this: