Society of Robots - Robot Forum
Software => Software => Topic started by: SilverFaith on January 29, 2011, 07:56:09 AM
-
i want to make a object avoiding car. and i don't know how to program it . please help me
This is the code for the 4 ping sensors to work
const int pingPin9 = 9;
const int pingPin8 = 8;
const int pingPin3 = 3;
const int pingPin2 = 2;
void setup()
{
Serial.begin(9600);
}
//declar pini ce sunt
void loop()
{
ping2();
delay(500);
ping8();
delay(500);
ping3();
delay(500);
ping9();
delay(500);
}
//viteza de rulare a algoritmului pt diferiti senzori sau motor DC
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
void ping9()
{
long duration9, cm9;
pinMode(pingPin9, OUTPUT);
digitalWrite(pingPin9, LOW);
delayMicroseconds(2);
digitalWrite(pingPin9, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin9, LOW);
pinMode(pingPin9, INPUT);
duration9 = pulseIn(pingPin9, HIGH);
cm9 = microsecondsToCentimeters(duration9);
Serial.print("sensorFataDreapta = ");
Serial.print(cm9);
Serial.print("cm");
Serial.println();
}
void ping8()
{
long duration8, cm8;
pinMode(pingPin8, OUTPUT);
digitalWrite(pingPin8, LOW);
delayMicroseconds(2);
digitalWrite(pingPin8, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin8, LOW);
pinMode(pingPin8, INPUT);
duration8 = pulseIn(pingPin8, HIGH);
cm8 = microsecondsToCentimeters(duration8);
Serial.print("sensorFataStanga = ");
Serial.print(cm8);
Serial.print("cm");
Serial.println();
}
void ping3()
{
long duration3, cm3;
pinMode(pingPin3, OUTPUT);
digitalWrite(pingPin3, LOW);
delayMicroseconds(2);
digitalWrite(pingPin3, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin3, LOW);
pinMode(pingPin3, INPUT);
duration3 = pulseIn(pingPin3, HIGH);
cm3 = microsecondsToCentimeters(duration3);
Serial.print("sensorFata = ");
Serial.print(cm3);
Serial.print("cm");
Serial.println();
}
void ping2()
{
long duration2, cm2;
pinMode(pingPin2, OUTPUT);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin2, LOW);
pinMode(pingPin2, INPUT);
duration2 = pulseIn(pingPin2, HIGH);
cm2 = microsecondsToCentimeters(duration2);
Serial.print("sensorSpate = ");
Serial.print(cm2);
Serial.print("cm");
Serial.println();
}
And this is the code for the 2 DC motors to work
int E1 = 7;
int M1 = 6;
int E2 = 4;
int M2 = 5;
void setup()
{
Serial.begin(9600);
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
}
void loop()
{
MergeFata();
delay(700);
MergeFataDreapta();
delay(700);
MergeFataStanga();
delay(700);
MergeSpate();
delay(700);
MergeSpateDreapta();
delay(700);
MergeSpateStanga();
delay(700);
Dreapta();
delay(700);
Stanga();
delay(700);
}
void MergeFata()
{
digitalWrite(M1,HIGH);
digitalWrite(M2,LOW);
analogWrite(E1, 0); //PLL Speed Control
analogWrite(E2, 128); //PLL Speed Control
}
void MergeSpate()
{
digitalWrite(M1,HIGH);
digitalWrite(M2,HIGH);
analogWrite(E1, 0); //PLL Speed Control
analogWrite(E2, 128); //PLL Speed Control
}
void Dreapta()
{
digitalWrite(M1,HIGH);
digitalWrite(M2,HIGH);
analogWrite(E1, 128); //PLL Speed Control
analogWrite(E2, 0); //PLL Speed Control
}
void Stanga()
{
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E1, 128);
analogWrite(E2, 0);
}
void MergeFataDreapta()
{
digitalWrite(M1,HIGH);
digitalWrite(M2,LOW);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void MergeFataStanga()
{
digitalWrite(M1,LOW);
digitalWrite(M2,LOW);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void MergeSpateDreapta()
{
digitalWrite(M1,HIGH);
digitalWrite(M2,HIGH);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void MergeSpateStanga()
{
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
This is the car
(http://i52.tinypic.com/2ijnhvt.jpg)
(http://i52.tinypic.com/212wzs7.jpg)
can anyone help me. By the way i'm using an Arduino Uno Board with a L298N shield and 4 ping sensors 2 DC motors(not stepmotor). I hope someone can help me. Thank YOU
-
Before anyone can help you, you have to ask a specific question. Saying that "you don't know how to program" your particular solution tends to be obvious: whenever most of us start a new project, we don't really know how to program it, but we do have an idea and soon, through iteration and testing, we come up with a working program.
What is your program not doing properly? Where are you stuck? As far as I can tell from your code, you haven't yet started to integrate the data from the sensors into the motor control loop, is that correct? What is the default behaviour of the robot (In other words, if you have no obstacles, what do you expect the robot to do? just drive forward?)
With more information someone may be able to help.
-
the codes are fine, and yes i did not integrate the data from the sensors into the motor control loop, and if is no obstacle in front of the vehicle i wanth to go forward and when ever is an obstecle i want the car to avoid the obstecle. If you can not test the code, please give me ideea and i will test it and tell you the result, and in the end mayby will end up with a working autonom object avoiding car :D. So if you can plese teach me how to integrate the data from the sensor into the motor loops. THANK YOU
-
well, first, start with a small experimental idea. And start small. Here's my best idea for a very simple object avoidance robot:
use only the central sensor on the front of the robot (I assume that the "front" of your car is the part with the 3 sensors on it, and not the part with only one sensor?). Have it drive forward until the distance that the central sensor reports back is below some value (say 30 centimeters, for example). If it is below that value, turn left until the sensor returns a larger value (then keep turning for a bit longer, since the robot's corner would collide with the obstacle if you didn't, since the sensor is in the center of the robot and not the edge), and then start moving forward again.
In pseudocode, this would be:
DRIVE_FORWARD
if (SENSOR_DISTANCE < 30 centimeters)
while(SENSOR_DISTANCE < 30 centimeters)
TURN_LEFT
end while
end if
TURN_LEFT for 0.5 more seconds (this amount of time is for you to tune, it may only need 0.1 seconds or whatever)
DRIVE_FORWARD
Then you just need to figure out how exactly you want to integrate the information coming from the other sensors. Maybe you can use them to determine which direction to turn, or maybe you can use them to figure out when to stop turning. Using the side sensors to do things like this will allow you to make the robot stay as far away from obstacles as possible, for example. I can't give you code since I don't have the same microcontroller you have, but my little experimental idea should (hopefully!) get you started.
MIKE