Navigation Bar

Thursday, March 29, 2012

Amazing Obstacle Sensing Robot Mod

We love all of our products but every once and a while a tech savvy customer will come along with a 3rd party modification that is just too cool to not tell you about. Kevin Nichols has modified our OWI-536 All Terrain Robot to move on its own while automatically sensing and reacting to its surroundings. Check out the video below. 


Kevin gives a how to below:


This is the code for the yellow robot that I made with an arduino and motor shield. It is not the best code in the world, nor is it finished. I put explanations off to the side. They will not affect the way your robot works. They always have // before them so the arduino knows to ignore them. 

Also note, this code is setup to work with the EZ-1 sonor sensor that we have for sale. It is a great sensor and it is very accurate, however it has a few problems. When the robot is aimed at a hard surface and is at more than a 45 degree angle to it, the sound will just bounce off and the sensor will not see the echo. I plan to change the sonor sensor to an IR distance sensor. If you do the same, or use a different sensor to measure distance, you will have to change the code to make it work.

"//This code is setup to work with an arduino uno with a sparkfun Motor shield and a EZ-1 Sonor sensor. Change the code as nessasary to work with the componets you are using.
//This is still a work in progress, and the robot will not preform flawlessly.


#include <Servo.h> //Tells the arduino that there will be a servo on it and to load the servo library

Servo pan; //The name of the servo
int pwr_r = 3; //speed control for right power hookup A on pin 3
int pwr_l = 11; //speed control for left power hookup B on pin 11
int dir_r = 12; //Direction control for right motor A - LOW = foward, HIGH = Reverse on my robot, yours might be different on pin 12
int dir_l = 13; //Direction control for left motor B - LOW = foward, HIGH = Reverse on my robot, yours might be different on pin 13
int button = 2; //on off switch located on pin 2
int val = 0; //variable for current button status
int old_val = 0; //variable for old button status
int state = 0; //go = 1, no go = 0 this int is used with the button to turn the robot on and off
int dis = 0; //sensor reading

int Lspd = 255; //These int will be used to control the motor speed for the Left and Right motors.
int Rspd = 255; //The number can be anywhere from 0 to 255, with 0 being no speed and 255 max speed

int straight = 87; //These int are the numbers that the servo will use to look left and right. 
int left = 50; //87 is what my servo needed to look straight, and the other numbers were for left and right
int right = 130; // change them as nessasary to get your servo to work right. The number can be from 0 to 180.

int Sdis = 0; //These 3 int are just place holders to store the mesurment distance from the sensor.
int Ldis = 0;
int Rdis = 0;

void setup(){
pinMode(pwr_l, OUTPUT); //This it setting up the pins to their proper 
pinMode(pwr_r, OUTPUT); //input output settings. 
pinMode(dir_l, OUTPUT); //The sensor is attached to an analog pin, so it does not 
pinMode(dir_r, OUTPUT); // need to be told to be an input. It is set that way automaticaly
pinMode(button, INPUT); 
pan.attach(5); //this attaches the servo to pin number 5


digitalWrite(dir_l, HIGH); //Sets the motor direction to high, which is foward 
digitalWrite(dir_r, HIGH); //on my chassis, your might need to be set to low if it goes backwards
analogWrite(pwr_l, 0); //makes sure that the motors dont go anywhere when you first turn it on
analogWrite(pwr_r, 0);

}

void loop(){ //Start of the code
pan.write(straight); //Tells the servo to look straight

val = digitalRead(button); //this section is for the button
if((val == HIGH) && (old_val == LOW)){ //it allows you to turn on or off
state = 1 - state; //the motors without unpluging it
delay(10); 
}
old_val = val;



dis = ((analogRead(A0))/2); //this reads the sensor and assigns the value devided by 2 to the dis interger. Devide be 2 to make the number equal to inches

if(state == 1){ //the first If statement checks if the button was pressed, if off, robot does nothing, otherwise it does what follows
analogWrite(pwr_l, 0);
analogWrite(pwr_r, 0);
}else{
if(dis <= 10){ //checks to see if the distance is equal to or closer than 10 inches. change this number to change the distance it stops.
analogWrite(pwr_l, 0); //if it is closer than 10 inches
analogWrite(pwr_r, 0); //it stops both motors
Sdis = dis; //stores the current distance as Sdis
pan.write(left); //makes the servo look left
delay(500); //wait half a second for the servo to move
Ldis = ((analogRead(A0))/2); //makes Ldis the messured distance
delay(100); //waits agian
pan.write(right); //looks right 
delay(500); //waits for the servo
Rdis = ((analogRead(A0))/2); //makes Rdis the messured distance


if(Ldis > Rdis){ //If left is farther away, turn left and go
digitalWrite(dir_r, HIGH); 
digitalWrite(dir_l, LOW); //this makes the left side run backwards
analogWrite(pwr_l, Lspd);
analogWrite(pwr_r, Rspd);
pan.write(straight); //makes the servo look straight
delay(300); //this number affects how far it turns. the biger the number, the further it turns
}
if(Rdis > Ldis){ //If right is further away, turn right and go
digitalWrite(dir_l, HIGH); //this part is same as above, except it makes the robot 
digitalWrite(dir_r, LOW); //turn right instead of left
analogWrite(pwr_r, Rspd);
analogWrite(pwr_l, Lspd);
pan.write(straight);
delay(300); 
}
if(Rdis == Ldis){ //If both left and right are the same, like in a corner, turn around and go
digitalWrite(dir_r, HIGH); //Agian, same as above, but the delay is longer at the end, so the robot 
digitalWrite(dir_l, LOW); //has enough time to turn around. Change the delay at the end if your robot does 
analogWrite(pwr_r, Rspd); //not make it all the way around
analogWrite(pwr_l, Lspd);
pan.write(straight);
delay(1000);
}
}else{ //If nothing ahead, go straight
digitalWrite(dir_r, HIGH);
digitalWrite(dir_l, HIGH);
analogWrite(pwr_r, Rspd);
analogWrite(pwr_l, Lspd);
}
}
}

Just copy the whole quote and upload this to your arduino. I will post a tutorial on how to build the robot a bit later, just wanted to get the code out first.


I fixed the diagram for the robot. It does not show the motor controller, but that just bolts onto the arduino, so no diagram necessary. It also shows how to wire in the IR Range finder, but the EZ-1 sonar sensor is wired in the same way.

Check out the full post here.

1 comment: