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
(Created page with "{{DISPLAYTITLE:Using the Sharp IR Sensor to detect objects and their distance}} <yambe:breadcrumb self="Using the Sharp IR Sensor to detect objects and their distance">Tutoria...")
 
Line 2: Line 2:
 
<yambe:breadcrumb self="Using the Sharp IR Sensor to detect objects and their distance">Tutorials/Arduino_Projects/Mobile_Robotics/BoeBot|Parallax BoeBot + Arduino Shield, Mobile Robotics Platform</yambe:breadcrumb>
 
<yambe:breadcrumb self="Using the Sharp IR Sensor to detect objects and their distance">Tutorials/Arduino_Projects/Mobile_Robotics/BoeBot|Parallax BoeBot + Arduino Shield, Mobile Robotics Platform</yambe:breadcrumb>
 
{{tl|1|}}
 
{{tl|1|}}
 +
== 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"
 +
 +
 +
{|
 +
|-
 +
|<syntaxhighlight lang="ROBOTC">
 +
insert config code</syntaxhighlight>
 +
|-
 +
|}
 +
 +
== 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)<sup>-1.2</sup>
 +
D = distance in mm
 +
V = the measured value
 +
 +
{{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 ==
 +
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.
 +
 +
{|
 +
|-
 +
|<syntaxhighlight lang="ROBOTC">
 +
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;
 +
    }
 +
  }
 +
}
 +
</syntaxhighlight>
 +
|-
 +
|}
 +
 +
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.
 +
 +
{|
 +
|-
 +
|<syntaxhighlight lang="ROBOTC">
 +
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()
 +
{
 +
  while (true)
 +
  {
 +
    if (getIrReading(leftIR) == 0 || getIrReading(rightIR) == 0) {
 +
      //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;
 +
    }
 +
  }
 +
}
 +
</syntaxhighlight>
 +
|-
 +
|}

Revision as of 17:54, 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

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.

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

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

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.

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

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.

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()
{
  while (true)
  {
    if (getIrReading(leftIR) == 0 || getIrReading(rightIR) == 0) {
      //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;
    }
  }
}