Difference between revisions of "Tutorials/Arduino Projects/Mobile Robotics/BoeBot/Using the Sharp IR Sensor to detect objects and their distance"
(→Reading the Sensors) |
(→Making the Program) |
||
Line 181: | Line 181: | ||
|- | |- | ||
|<syntaxhighlight lang="ROBOTC"> | |<syntaxhighlight lang="ROBOTC"> | ||
− | + | #pragma config(CircuitBoardType, typeCktBoardUNO) | |
+ | #pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot) | ||
+ | #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) | ||
+ | #pragma config(Sensor, anlg0, sharpIR, sensorAnalog) | ||
+ | #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 | ||
task main() | task main() |
Latest revision as of 20:12, 8 August 2012
NOTE: This is a place holder for a video.
Please add the id for the YouTube video with description "BoeBot detecting objects using a Sharp IR Sensor"
Configuration
Now that we know how the sensor works and have it wired up, it is time to program it in ROBOTC. But first we need to tell ROBOTC how the robot is configured.
- Parallax Boe Shield
- servo_10 as a continuous rotation servo named "leftServo"
- servo_11 as a reversed continuous rotation servo named "rightServo"
- Analog pin 0 as an analog input named "sharpIR"
#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, anlg0, sharpIR, sensorAnalog) #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 |
Reading the Sensors
To get the reading from the detector you use "SesnorValue[]" just like with any other sensor.
- returns lower values for object at further distances
- returns higher values for object that are closer.
Since the returned value does not have a linear relation to the distance, you can use the following formula and/or table to find the distance
D = distance in mm
V = the measured value
ADC Value | Voltage (V) | Distance (mm) | Distance (cm) | Distance (in) |
---|---|---|---|---|
80 | 0.391 | 841 | 84 | 33.1 |
120 | 0.586 | 518 | 52 | 20.4 |
160 | 0.781 | 367 | 37 | 14.4 |
200 | 0.977 | 280 | 28 | 11 |
240 | 1.172 | 225 | 23 | 8.9 |
280 | 1.367 | 187 | 19 | 7.4 |
320 | 1.563 | 160 | 16 | 6.3 |
360 | 1.758 | 139 | 14 | 5.5 |
400 | 1.953 | 122 | 12 | 4.8 |
440 | 2.148 | 109 | 11 | 4.3 |
480 | 2.344 | 98 | 10 | 3.9 |
520 | 2.539 | 89 | 9 | 3.5 |
560 | 2.734 | 82 | 8 | 3.2 |
600 | 2.93 | 75 | 8 | 3 |
640 | 3.125 | 69 | 7 | 2.7 |
680 | 3.32 | 65 | 6 | 2.5 |
|
|
Making the Program
Just like with the other IR sensors, we will be modifying an existing program to use the Sharp IR sensor. Also like last time, we will be starting with the code for avoiding obstacles using the Parallax PING sensor.
task main() { while (true) { if (SensorValue[ping] <= threshold) { //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; } } } |
Since the Sharp IR sensor also returns a range of values, all we really need to do is change any reference to the ping sensor to reference the Sharp IR sensor instead. We will also need to adjust the threshold.
#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, anlg0, sharpIR, sensorAnalog) #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 task main() { while (true) { if (SensorValue[sharpIR] <= threshold) { //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; } } } |