Rangefinder
The XRP's rangefinder is a mechanism in front of the robot that, as the name suggests, gives you the distance between your robot and the closest object in front of it. It emits an ultrasonic sound that bounces off objects close to it, and calculates the distance away from this target depending on how long it takes to bounce back.
This is useful for detecting whenever a gamepiece is in your robot's range to grab it. It is also very useful for not driving into a wall, and potentially breaking your arm mechanism, especially in Autonomous commands.
Using the Rangefinder
Unfortunately, unlike XRPMotors and XRPServos, you can't have more than one rangefinder on your XRP. So when making a Rangefinder object, you don't need any ID for it. Try to put these in either the XRPDrivetrain or a completely different Subsystem.
private final XRPRangefinder rangeFinder = new XRPRangefinder();Getting a Rangefinder's Distance
To get the distance calculated by the rangefinder, you can use these two methods:
rangeFinder.getDistanceInches();
rangeFinder.getDistanceMeters();They work fundimentally the same, aside from giving different units.
Potential Issues with Rangefinders
The rangefinder, although useful, has some flaws. Sometimes, the rangefinder will give you inaccurate data that could be literal hundreds of inches away from the actual position. To fix, this we simply need to make our code not use any outliers in the data via if-statements. Here is an example below
double distance = rangeFinder.getDistanceInches();
double previousDistance = distance; // temporary value
public void periodic() {
// data is probably valid
if (Math.abs(distance - previousDistance) < 100) {
previousDistance = distance;
// you can add whatever here, such as checking if something
// is too close to the robot
// data is invalid
} else {
System.out.println("Data from rangefinder is invalid!");
}
}
}Last updated