Using the Sharp IR Sensor to detect objects and their distance

From ROBOTC API Guide
Jump to: navigation, search
ArduinoArduino Tutorials and Guided ProjectsVEX + Arduino, Mobile Robotics Platform → 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.

noframe

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
Distance (mm) Distance (cm) ADC Value Voltage (V)
800 80 84 0.41
750 75 88 0.43
700 70 93 0.454
650 65 99 0.483
600 60 106 0.518
550 55 114 0.557
500 50 124 0.605
450 45 135 0.659
400 40 149 0.728
350 35 166 0.811
300 30 189 0.923
250 25 220 1.074
200 20 265 1.294
150 15 337 1.646
100 10 472 2.305
Distance (in) ADC Value Voltage (V)
32 82 0.4
30 87 0.425
28 92 0.449
26 98 0.479
24 105 0.513
22 113 0.552
20 122 0.596
18 133 0.649
16 147 0.718
14 164 0.801
12 187 0.913
10 217 1.06
8 262 1.279
6 333 1.626
4 466 2.275

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;
    }
  }
}