Using the Parallax PING sensor to avoid walls
|
NOTE: This is a place holder for a video.
Please add the id for the YouTube video with description "BoeBot avoiding walls using a PING sensor"
How it Works
We have already done wall and obstacle detection using switches (the whiskers). However, this can be problematic if you need to detect an obstacle at a distance. This is where the ultrasonic sensor comes into play.
We already know that the ultrasonic sensor can be used to detect objects and measure the distance to them. By using an ultrasonic sensor we can program the robot to avoid them at a variety of distances.
Configuration
The robot will need
- Parallax Boe Shield
- Continuous Rotation Servo on pin 10, named "leftServo"
- reversed Continuous Rotation Servo on pin 11, named "rightServo"
- Parallax PING sensor on pin 3, named "ping"
Once configured, you should have the following code at the top of your program file.
#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, dgtl3, ping, sensorSONAR_Ping_inch) #pragma config(Sensor, dgtl5, led1, sensorDigitalOut) #pragma config(Sensor, dgtl6, led2, sensorDigitalOut) #pragma config(Motor, servo_10, leftServo, tmotorServoContinuousRotation, openLoop, IOPins, dgtl10, None) #pragma config(Motor, servo_11, rightServo, tmotorServoContinuousRotation, openLoop, reversed, IOPins, dgtl11, None) //*!!Code automatically generated by 'ROBOTC' configuration wizard |
Programming
We already have some code that monitors the distance to an object using the PING sensor, so let's modify that code to control how the robot drives.
task main() { while (true) { if (SensorValue[ping] > 7) { //led1 on and led2 off SensorValue[led1] = 1; SensorValue[led2] = 0; } else if (SensorValue[ping] == 7) { //both leds on SensorValue[led1] = 1; SensorValue[led2] = 1; } else if (SensorValue[ping] < 7) { //led1 off and led2 on SensorValue[led1] = 0; SensorValue[led2] = 1; } else { //something happened //we don't know what to do so //let's just turn off the leds SensorValue[led1] = 0; SensorValue[led2] = 0; } } } |
Since we only care about when there is an object that is less than or equal to the distance threshold, we can change the first if statement to be <= instead of >. We can also remove the else-if statements.
task main() { while (true) { if (SensorValue[ping] <= 7) { //there is something within the threshold range } else { //nothing in the threshold range } } } |
Now that we have the logic part done, we need to add the action parts. When there is no object detected, we want to just drive forward at power level 15. When we detect an obstacle, we need to turn then resume driving and looking for obstacles.
task main() { while (true) { if (SensorValue[ping] <= 7) { //there is something within the threshold range motor[leftServo] = -15; motor[rightServo] = 15; wait1Msec(400); } else { //nothing in the threshold range motor[leftServo] = 15; motor[rightServo] = 15; } } } |