Arduino Object Avoidance with PING and Hacked RC Car
Finished Bot Here: https://www.robotshop.com/letsmakerobots/node/26101
Before I begin I am a Senior Electrical Engineer student in college. This is a project im working on for my design class. I am very new to arduino and its language. Having said that here we go:
Working on an object avoiding vehicle using Arduino and Ping ultrasound sensor. I hacked an old rc car. Basically removed all the guts and replaced it with a breadboard and the arduino. I am still using the 9V rechargeable battery pack that came with the car to power the 2 motors through an L293D chip and a regular 9v battery to power the arduino. When I turn on the robot it runs well and responds while im holding it in the air so the code seems to be working. But then things go bad. Here are the issues i've come across so far.
After a while it begins to slow down and stop as you can see in the video. Battery is charged
When changing directions it badly stutters. will post another video soon showing this.
Not enough torque to run on carpet. Runs on tile but slows to a stop when changing directions.
When I grab one wheel to stop it while its running the other slows and eventually stops. Not too much of an issue there but just wondering why it happens since they are running independantly? gueesing it has to do with the overheating of the L293D.
I left the original two capacitors going across the motors attached, tried removing them and had same issue so I put them back. Also the L293D (can handle 0.6 amps) chip gets VERY hot while its running and starts to smell like its burning. I ordered a chip called ‘SN754410‘ from Texas Instruments. It can handle double the current of the L293D and has the same pin layout. It comes in next week so hopefully that will make a difference. Will update when it comes
I have the PING sensor mounted on a servo. After these issues are addressed I would like to add a section to the code to allow the servo to pan while its driving so it will avoid it from running into anything at any angle OR when it comes to an object look left and right and choose the best direction. I am horrible at code writing so any links to similar codes I can use or if anyone can help would be greatly appreciated.
Will post more pictures and videos as soon as I can.
Heres how I connected everything: PINS
1 to pin 9 on Arduino board, 2 to pin 3 on Arduino board, 3 to motor1 (either + or -) it wont matter as its DC, 4 to the gnd rail on the breadboard, 5 to the gnd rail on the breadboard, 6 to motor1, 7 to pin 4 Arduino, 8 to power (+) rail., 9 to pin 10 Arduino, 10 to pin 5 Arduino, 11 to motor2, 12 to GND rail, 13 to GND rail, 14 to motor2, 15 to pin 6 Arduino, 16 to power (+) rail
Heres the code I used. Credit to @lucky_larry on twitter for most of the code. I made a few changes to allow it to work for my PING sensor. The original was written for the SRF05 ultrasound sensor.
#include <Ping.h>
const int numOfReadings = 10; // number of readings to take/ items in the array
int readings[numOfReadings]; // stores the distance readings in an array
int arrayIndex = 0; // arrayIndex of the current item in the array
int total = 0; // stores the cumlative total
int averageDistance = 0; // stores the average value
// setup pins and variables for SRF05 sonar device
const int pingpin = 12; // ping pin (digital 12)
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm)
int motor1Pin1 = 3; // pin 2 on L293D
int motor1Pin2 = 4; // pin 7 on L293D
int enable1Pin = 9; // pin 1 on L293D
int motor2Pin1 = 5; // pin 10 on L293D
int motor2Pin2 = 6; // pin 15 on L293D
int enable2Pin = 10; // pin 9 on L293D
void setup() {
// set the motor pins as outputs:
Serial.begin(9600);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enable2Pin, OUTPUT);
// set enablePins high so that motor can turn on:
digitalWrite(enable1Pin, HIGH);
digitalWrite(enable2Pin, HIGH);
pinMode(pingpin, OUTPUT);
pinMode(pingpin, INPUT);
// create array loop to iterate over every item in the array
for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
pinMode(pingpin, OUTPUT);
digitalWrite(pingpin, HIGH); // send 10 microsecond pulse
delayMicroseconds(10); // wait 10 microseconds before turning off
digitalWrite(pingpin, LOW); // stop sending the pulse
pinMode(pingpin, INPUT);
pulseTime = pulseIn(pingpin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.
total= total - readings[arrayIndex]; // subtract the last distance
readings[arrayIndex] = distance; // add distance reading to array
total= total + readings[arrayIndex]; // add the reading to the total
arrayIndex = arrayIndex + 1; // go to the next item in the array
// At the end of the array (10 items) then start again
if (arrayIndex >= numOfReadings) {
arrayIndex = 0;
}
averageDistance = total / numOfReadings; // calculate the average distance
delay(10);
// check the average distance and move accordingly
if (averageDistance <= 10){
// go backwards
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
if (averageDistance <= 25 && averageDistance > 10) {
// turn
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
if (averageDistance > 25) {
// go forward
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
}
Any comments, help, ideas or input is definately welcome. I also hope this can be used to help someone who is also having similar issues.