Autonomous Arduino Car

Hello,

I have just started programming with an Arduino at school, and as a project, we have decided to make an autonomous car. As of now, we have taken out the board of an old RC car and replaced it with an Arduino uno. To drive the motors, we are using a ladyada motor shield - L293D, and for sensing, we are using a HC-SR04 Ultrasonic Distance Sensor.We have managed to control the motors and the sensor is working. However we need some help writing a code so that the car reverses and turns when there is an obstacle in front of it.

Any help would be much appreciated.

Thanks

Could you please post the code you already have.

Also, how close do you want the robot to be before it reverses and turns around? Use the code below to see the analog value that the sensor is putting out. Place the robot as close to the wall as you want it then run the code and look at the serial monitor on your computer. It should read out the analog value of the sensor. When you post the code post that value as well.

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
 
 This example code is in the public domain.
 */

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(700);        // delay in between reads for stability
}

Best of luck,
Drew

Hi drew,

Thanks for your quick response. The anologue reading is 520. The code is just to make the car reverse then go forward:

#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor test!");

motor1.setSpeed(2000); // set the speed to 200/255
motor2.setSpeed(150); // set the speed to 200/255

}

void loop() {
Serial.print("tick");

motor1.run(FORWARD);
motor2.run(BACKWARD); // turn it on going forward
delay(1000);

motor1.run(BACKWARD);
motor2.setSpeed(150);
motor2.run(FORWARD); // turn it on going forward
delay(1000);

}

Try something like this…

F_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

int long sensorValue = 0;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
  
  motor1.setSpeed(2000);     // set the speed to 200/255
  motor2.setSpeed(150);     // set the speed to 200/255  /// change commits to be up to date
  
}

void loop() {
  Serial.print("tick");
  sensorValue = analogRead(A0);
    Serial.print(sensorValue);

   if (sensorValue > 520) { // I dont know if your sensor's readings get larger when it gets close to the wall or smaller so you might have to change > to < 
   
  motor1.run(FORWARD);
  motor2.run(BACKWARD);      // turn it on going backward
  delay(1000);
  
  
    motor1.run(FORWARD);
  motor2.run(FORWARD); 
   delay(500); //play around with the delay to make it turn the right amount
  
   }
   
   else {
   
  motor1.run(BACKWARD);
  motor2.setSpeed(150);
  motor2.run(FORWARD);      // turn it on going forward
  delay(1000);
  
  
   }
}

How is your car going?

Also interested in progress as I have started on a similar project using "The Source" or a Radio Shack cheap car--any chance of seeing full code so far??

Hi All,

I am using a 1/24 the scale tank chassis to work out object avoidance for my hexapod. Control setups are pretty simple for front and rear avoidance, speed is an issue as you you have to consider how far you travel between checks.

It gets way more interesting as you try to add in object avoidance.

My test environment is a box the tank sits on, the tracks do not touch the ground and I can get rough settings dialed in. I have an r/c receiver attached to allow me to kill the motors as required. I can post code but really I started with the samples for the ultrasonic and my sabretooth motor control and went from there. Currently only the stop process has been implemented as my side sensors have just arrived.

First off, what does your tank have to do with Gecko2903's car?

Furthermore, what do you need help with? After quickly reading your post it seems like you have everything under control!

Hello, I am sorry for the late reply!
The Autonomous car is going well but we have changed the design slightly and added two more motors making it a 4wd car. This was due to the unreliability of the motor at the front which was key for turning the car. We are using the same motor board and sensor and I have just written a basic code which turns the motors on, and if the distance (in front of the car) is less than 15 cm, it reverses:

#define trigPin 10
#define echoPin 12
#include <AFMotor.h>


int sound = 250;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(115200); 
  motor1.setSpeed(200);
  motor2.setSpeed(200);
  motor3.setSpeed(200);
  motor4.setSpeed(200);
  
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);

 
}

void loop() {
  uint8_t i;
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
 

  if (distance <= 10) {
    
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
    delay(100);
}
  else {
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
    delay(10);
  }
  
}

One problem which i am having is that when it detects that it an object is 15cm away, it reverses then moves forward straight away causing the wheels to crash. How can i solve this problem and how can i make it turn when an object is 15cm away and then go forwards?

Any help would be much appreciated

You need to make the movements longer than 100 msec. That's barely enough time for the motors to reverse, let alone move the car very far backwards.

oric_dan:
You need to make the movements longer than 100 msec.

How much time would you give for it to reverse?
Also when I increase the delay of the movement, for some reason the sensor becomes more delayed meaning the car crashes into the wall?
Does anyone know why this is happening?

You just have to experiment with the delays and see what works well. Depends upon motor speed, mechanical inertia, etc. From my own robots, I'd say 0.5 - 1.0 sec for a backward movement away from an obstacle, then stop, turn X-degrees, and go forward again. You need more complicated avoidance sequences.

You can always add a bumper on the rear-end to tell if the backup movement hits something else.

Robotics involves a lot of fine-tuning of delays and parameters. The robot will always be getting into unplanned for situations. That's what Robot Programming is all about. There is a really excellent book by the same name,

or buy the older book for almost nothing [it's actually my favorite],

Both books have extensive coverage of programming techniques with source code in C, perfect for Arduino.

Thanks oric_dan for your response. I will look into the links which you have supplied and hopefully it will help with the avoidance sequences.