Using the Sharp IR Sensor to detect objects and their distance

From ROBOTC API Guide
Jump to: navigation, search
ArduinoArduino Tutorials and Guided ProjectsParallax BoeBot + Arduino Shield, 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. 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"


insert config code

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=10853481*(V*5000/1024)-1.2

D = distance in mm

V = the measured value

Notepad.gif TODO: insert a simple table for the distances

Making the Program

Just like with the other IR sesnors, 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 Parllax 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.

insert config code.
 
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;
    }
  }
}