Wall sensing robot code help
gizahuna
Posts: 1
I am extremely new to programming and working with two students on this project. I have a 2WD robot chassis, arduino uno, motor shield, and Parallax ping sensor. We are attempting to build a wall sensing robot. We have the motors coded to run and even headlights (wow factor for 5th graders), but unfortunately we (and especially me) can't figure out how to write the code so the sensor will stop the robot when it comes close to a wall.
The code is below: Any help you someone can provide will be great.
The code is below: Any help you someone can provide will be great.
// names the LED and pin number int led = 10; int led2 = 6; int work = 0; // this constant won't change. It's the pin number for the sensor // of the sensor's output: const int pingPin = 7; //sets the interger for the pulse sensor int distance; unsigned long pulseDuration=0; void setup() { //Setup MOTOR RIGHT pinMode(12, OUTPUT); //Initiates Motor Channel A pin pinMode(9, OUTPUT); //Initiates Brake Channel A pin //Setup MOTOR LEFT pinMode(13, OUTPUT); //Initiates Motor Channel A pin pinMode(8, OUTPUT); //Initiates Brake Channel A pin //SETUP LED HEADLIGHTS // initialize the digital pin as an output. pinMode(led, OUTPUT); pinMode (led2, OUTPUT); // initialize serial communication: SENSOR Serial.begin(9600); } void loop() { lightson(); moveforward(); pingsensor(); turn(); //blinkers(); } void lightson() { // the loop for the LED to run as a headlight digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(led2, HIGH); //TURN THE LED2 ON (HIGH IS THE VOLTAGE LEVEL) } void moveforward() { //Motor A forward @ full speed (left motor) //full speed of motor is 255 digitalWrite(12, LOW); //Establishes forward direction of Channel A left motor digitalWrite(9, LOW); //Disengage the Brake for Channel A analogWrite(3, 239); //Spins the motor on Channel A //motor B forward @ full speed (right motor) digitalWrite (13, HIGH); //Establishes forward motion of Channel B right motor digitalWrite (8, LOW); //Disengages the brake for Channel B analogWrite (11, 254); //Spins the motor on Channel B } void pingsensor() { // establish variables for duration of the ping, // and the distance result in inches and centimeters: long duration, inches, cm; // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); // The same pin is used to read the signal from the PING))): a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); // convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); delay(100); } long microsecondsToInches(long microseconds) { // According to Parallax's datasheet for the PING))), there are // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per // second). This gives the distance travelled by the ping, outbound // and return, so we divide by 2 to get the distance of the obstacle. // See: [URL]http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf[/URL] return microseconds / 74 / 2; } long microsecondsToCentimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. return microseconds / 29 / 2; } void turn() { if (pulseDuration < 10 & pulseDuration > 5){ // stop motors //engages the brake for left motor digitalWrite(9, HIGH); //engages brake for right motorch digitalWrite (8, HIGH); delay (5000); //turns robot to another direction //full speed of motor is 255 //digitalWrite(12, HIGH); //Establishes REVERSE direction of Channel A left motor //digitalWrite(9, LOW); //Disengage the Brake for Channel A //analogWrite(3, 239); //Spins the motor on Channel A //motor B forward @ full speed (right motor) //digitalWrite (13, LOW); //Establishes REVERSE motion of Channel B right motor //digitalWrite (8, LOW); //Disengages the brake for Channel B //analogWrite (11, 100); //Spins the motor on Channel B SLOWLY } else{ work = work + 1; } }
Comments
Please be aware that this is a forum for Parallax oriented products. However, this is a friendly bunch and will attempt to assist if possible.
I've edited your code sample to make it more readable.
If you pay carefull attention to Mike Green's comments and are able to understand exactly how the Ping works,
the position of your ping unit relative to the robot platform is very important.
In my projects, If I'm mounting the ping to detect a wall, what works best for me, is to mount the ping at about a 45 degree angle from
front position.
When you read the distance using the ping, the 45 degree mounting angle allows you to measure the distance to the wall before you are at the location. If I am looking directly at the wall, I usually crash.
I've mounted my Ping unit on a servo that can pivot to where I want the Ping to look that is independent of the platform.
My only recent Microcontroller experience is with BS1 and BS2 Parallax controllers.
Sorry I can't help with the Arduino code.
+1. For a simple diff-drive robot, your PING sensor needs to be pretty far ahead of your drive wheels, so a small turn will rotate the chassis and move the sensor relative to the wall even before the wheels move. Easier to see in this video, where the bot "porpoises" off the wall to follow it. That is, there are only two modes. Steer away from the wall if the sensor sees it, or steer towards the wall if the sensor doesn't see it. This robot has a seperate front obstacle sensor to enable left turns.