Using the Sharp IR Sensor to detect objects and their distance
|
Configuration
Now that we know how the sensor works and have it wired up, it is time to program it in ROBOTC. First we need to tell ROBOTC how the robot is configured.
In addition to the servos, you should configure analog pin 0 as an analog sensor, and call it sharpIR.
Your configuration code should now look like this:
#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, anlg0, sharpIR, sensorAnalog) #pragma config(Motor, servo_10, rightServo, tmotorServoContinuousRotation, openLoop, IOPins, dgtl10, None) #pragma config(Motor, motor_11, leftServo, tmotorServoContinuousRotation, openLoop, reversed, IOPins, dgtl11, None) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// |
Reading the Sensor
To get the reading from the sensor, you use "SesnorValue[]" just like with any other sensor. The Sharp IR 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 exact distance if needed.
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
To make it easier, and since we want the same result, we will be modifying an existing program to use the Sharp IR sensor. We will start with the code for the VEX ultrasonic rangefinder.
task main() { while (true) { //Make sure the value is not an error if(SensorValue[ultrasonic] > 0) { if (SensorValue[ultrasonic] <= 7) { //there is something within the threshold range motor[leftServo] = -50; motor[rightServo] = 50; wait1Msec(800); } else { //nothing in the threshold range motor[leftServo] = 50; motor[rightServo] = 50; } } } } |
Since the Sharp IR sensor also returns a range of values, all we really need to do is change any reference of the ultrasonic sensor to reference the Sharp IR sensor instead. We will also need to adjust the threshold by using the lookup table mentioned earlier. Remember to change the config code!
#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, anlg0, sharpIR, sensorAnalog) #pragma config(Motor, servo_10, rightServo, tmotorServoContinuousRotation, openLoop, IOPins, dgtl10, None) #pragma config(Motor, motor_11, leftServo, tmotorServoContinuousRotation, openLoop, reversed, IOPins, dgtl11, None) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// task main() { while (true) { if (SensorValue[sharpIR] >= 230) { //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; } } } |