Difference between revisions of "Tutorials/Arduino Projects/Mobile Robotics/BoeBot/Using the Sharp IR Sensor to detect objects and their distance"

From ROBOTC API Guide
Jump to: navigation, search
Line 23: Line 23:
  
 
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
 
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)<sup>-1.2</sup>
 
D=10853481*(V*5000/1024)<sup>-1.2</sup>
 +
 
D = distance in mm
 
D = distance in mm
 +
 
V = the measured value
 
V = the measured value
  
 
{{Todo|insert a simple table for the distances}}
 
{{Todo|insert a simple table for the distances}}
 
=== Make Sensor Readings easier ===
 
To make the sensor readings easier, we can create a function that starts the IR LEDs flashing and then reads the IR detector and returns the result.
 
 
{|
 
|-
 
|<syntaxhighlight lang="ROBOTC">
 
bool getIrReading(tSensors irReceiverPin)
 
{
 
  // IRLED 38 kHz for at least 2 ms
 
  wait1Msec(1);                              // Wait 1 ms
 
  bool ir = SensorValue[irReceiverPin];      // IR receiver -> ir variable
 
  wait1Msec(1);                              // Down time before recheck
 
  return ir;
 
}</syntaxhighlight>
 
|-
 
|}
 
 
You will notice that we are passing a variable of type "tSensors" to the function. This is the variable type used to refer to a sensor pin. This allows us to specify what pin to read for the IR detector.
 
  
 
== Making the Program ==
 
== Making the Program ==
To demonstrate how to use the sensor, we are going to make the robot drive forward and look for obstacles. If it finds on, it will turn then continue driving forward. If you are thinking that this sound familiar, that is because we made a similar program using the Parallax PING sensor. So like any good programmer, we are going to copy the program using the PING sensor and modify it to use the IR sensors.
+
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.
  
 
{|
 
{|
Line 74: Line 58:
 
|}
 
|}
  
First lets add the function for reading the IR Sesnors. Then we can replace the ping sensor value calls with calls to the function for both the left and the right sensors. Once we do that we should end up with something like the following.
+
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.
  
 
{|
 
{|
Line 80: Line 64:
 
|<syntaxhighlight lang="ROBOTC">
 
|<syntaxhighlight lang="ROBOTC">
 
insert config code.
 
insert config code.
 
bool getIrReading(tSensors irReceiverPin)
 
{
 
  // IRLED 38 kHz for at least 2 ms
 
  wait1Msec(1);                              // Wait 1 ms
 
  bool ir = SensorValue[irReceiverPin];      // IR receiver -> ir variable
 
  wait1Msec(1);                              // Down time before recheck
 
  return ir;
 
}
 
  
 
task main()
 
task main()
Line 94: Line 69:
 
   while (true)
 
   while (true)
 
   {
 
   {
     if (getIrReading(leftIR) == 0 || getIrReading(rightIR) == 0) {
+
     if (SensorValue[sharpIR] <= threshold) {
 
       //there is something within the threshold range
 
       //there is something within the threshold range
 
       motor[leftServo] = -15;
 
       motor[leftServo] = -15;

Revision as of 18:00, 24 July 2012

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