Sharp IR Distance Sensor

The Sharp GP2Y0A21YK is one of the most reliable and accurate sensors in the collection. The Sharp IR has many benefits that make it one of the best sensors for a robot for distance tracking.

../../_images/sharpIR-1.png
Electrical Characteristics

Function

Min

Nom

Max

Input Voltage

4.5VDC

5V

7VDC

Output Voltage

-0.3VDC

VIN + 0.3VDC

Sensing Range

10cm

80cm

Current

30mA

40mA

Operating Temperature

-10°C

60°C

Storage Temperature

-40°C

70°C

Programming the Sharp IR Sensor

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
//import the Analog Library
import edu.wpi.first.wpilibj.AnalogInput;

//Create the Analog Object
private AnalogInput sharp;

//Constuct a new instance
sharp = new AnalogInput(port);

//Create an accessor method
public double getDistance()
{
    return (Math.pow(sharp.getAverageVoltage(), -1.2045)) * 27.726;
}

The accessor method will output the range in cm.

Note

The valid Analog ports are 0-3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
//Include the Analog and Math Library
#include "frc/AnalogInput.h"
#include <cmath>

//Constructors
frc::AnalogInput sharp{port};

//Create an accessor function
double getDistance(void)
{
    return (pow(sharp.GetAverageVoltage(), -1.2045)) * 27.726;
}

The accessor function will output the range in cm.

Note

The valid Analog ports are 0-3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
//Include the Sharp Library
#include "Sharp_ros.h"

/**
 * Constructor
 * Sharp's ros threads (publishers and services) will run asynchronously in the background
 */

ros::NodeHandle nh; //internal reference to the ROS node that the program will use to interact with the ROS system
VMXPi vmx(true, (uint8_t)50); //realtime bool and the update rate to use for the VMXPi AHRS/IMU interface, default is 50hz within a valid range of 4-200Hz

 SharpROS sharp(&nh, &vmx);
 // or can use
 SharpROS sharp(&nh, &vmx, channel);

 //Use these to directly access the data
 sharp.GetIRDistance(); //converts the average voltage read
 sharp.GetRawVoltage(); //returns the average voltage

The accessor function will output the range in cm.

Note

The valid Analog channels are 22-25. These are different from the WPI Analog Input Channels.

Important

Subscribe to Sharp topics to access the data being published and write callbacks to pass messages between various processes.