Code for Autonomous Robot to Follow Pre Programmed Route

Hi All,

First post here, I am new to programming and was woundering if someone could point me in the right direction here as I'm sure there is some good info on this I am just having trouble finding it :roll_eyes:

I have an arduino uno powered robot with a motor sheild and mars rover type tracked base. I want to be able to programme the robot so that it will follow a pre programmed path making several turns to avoid objects. I dont want to go down the track of using sensors as I know exactly the dimensions of the path I want it too follow and just want it to follow these basics lines.

Any help on this would be great, let me know if you require any further information :slight_smile:

to make it follow a pre-programmed path you have to think about several aspects that can influence the direction of the robot:

  • all wheels have to turn exactly at the same speed, which is hard to accomplish
  • to turn, you servo(s) have to be prefect
  • you have to keep track of the driven distance, and therefor you'll need sensors (unless you calculate how long you have to turn the wheels, but that is very inaccurate)

as you see, it's not that easy to do.. wheels often have the tendency to not run at exactly the same speed, so using sensors is a lot easier than pre-programming it

hi steen,

thanks for your quick reply! damn i was hoping it would be a fairley simple task.
At the moment I have it controled via bluetooth off my laptop and the code i'm using uses the keys to go forward turn etc. But i need to to follow a premediated path without my input, It seems to track in a fairley straight line when im using the keyboard control?

Cheers for any thoughts you have

jfullbrook:
It seems to track in a fairley straight line when im using the keyboard control

that's good to hear. that surely makes it easier, but still difficult to get it perfect..
you could use something like this to keep track of the driven distance and to see if both wheels are exactly running the same (and correct them if not)

it should be possible, but don't expect it to work flawlessly

yes i'm starting to see what you mean now it is going to be inacurate, is there anyway you can cotrol it manually and correct the error using the controls and then have it remember the commands and then repeat the same line autonomously? It's not a large distance that I want to run about 5meters all up including 4 turns?

Below is the code I am using to control the robot but i want it to do certain commands for a certain ammount of time so that I can acheive this I want to be able to do the following of 1 command, is it easy to change this basic code below to do the following?

Start
Forward for x amount of time
Stop
Right for x amount of time
Stop
Forward for x amount of time
Stop
Right for x amount of time
Stop
Forward for x amount of time
Stop
Left for x amount of time
Stop
Forward for x amount of time
Stop

Add times in after testing

int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
char command = '\0';
int pwm_speed; //from 0 - 255
void setup() {
// initialize the digital pin as an output.
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
Serial.begin(9600);
pinMode(13, OUTPUT);

digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
}

void loop() {

char *help_menu = "'e' go forward\n'd' stop\n's' left\n'f' right\n'c' go backward\n";

if (Serial.available()>0)
{
command = Serial.read();
}

switch (command)
{
case 'e': //go forward
Serial.println("Go!!\r\n");
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
analogWrite(E1, 255); //PWM speed control
analogWrite(E2, 255); //PWM speed control
command = '\0'; //reset command
break;
case 'c': //backward
Serial.println("Back!!\r\n");
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1, 255); //PWM speed control
analogWrite(E2, 255); //PWM speed control
command = '\0'; //reset command
break;
case 's': //left
Serial.println("Left!!\r\n");
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 255); //PWM speed control
analogWrite(E2, 255); //PWM speed control
command = '\0'; //reset command
break;
case 'f': //right
Serial.println("Right!!\r\n");
digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 255); //PWM speed control
analogWrite(E2, 255); //PWM speed control
command = '\0'; //reset command
break;
case 'd': //stop
Serial.println("Stop!\r\n");
analogWrite(E1, 0); //PWM speed control
analogWrite(E2, 0); //PWM speed control
command = '\0'; //reset command
break;
case '\0':
//do nothing
break;
default:
Serial.println(help_menu);
command = '\0'; //reset command
break;
}

}

you can for example make an array of ints that tell it how long and in what direction it must go, like this:

int dirs[8] = {1,10,2,13,1,5,4,20}

where dirs[0], [2], [4] and [6] are the directions with 1=forwards, 2=right, 3=backwards and 4=left.
then dirs[1], [3], [5] and [7] are the length of the direction, so in this case the robot will to the following:

drive forwards for 10 loops/msec/whatever, drive right for 13 loops/msec/whatever, drive forwards for 5loops/msec/whatever and drive left for 20 loops/msec/whatever.

understood? this might not be the best, but it seems like a reliable way to use it, and it’ll be easy to adapt it to your needs.
you can use the following code to make it work

for(int dir=0; dir<sizeof(dirs)/sizeof(dirs[0])/2; dir++)
{
  switch (dirs[i])
  {
    case 1:
      for(int len=0; len<dirs[i+1]; len++)
        //move forwards
    case 2:
      for(int len=0; len<dirs[i+1]; len++)
        //move right
    case 3:
      for(int len=0; len<dirs[i+1]; len++)
        //move backwards
    case 4:
      for(int len=0; len<dirs[i+1]; len++)
        //move left
  }
}

i did not test anything, but i suppose it should work. you still have to add your own moving code, but that won’t be hard i suppose