*Note: If you do not know anything about C++ or... hello world program to get started.

Transcription

*Note: If you do not know anything about C++ or... hello world program to get started.
*Note: If you do not know anything about C++ or Java programming, please try out a simple
hello world program to get started.
Ultrasonic Range Finder
In this tutorial, I will explain how to wire, program and
setup an ultrasonic range finder on your FRC robot. First
thing, the ultrasonic range finder is a sensor that detects
the distance from a solid object. The sensor does this by
sending out sound waves (un-noticeable to the human
ear). This sound wave travels out then hits and bounces
off a solid object returning to the sensor. The sensor then
measures how long it took for the sound wave to return
and outputs the distance in inches or millimeter
according to the WPI library.
20 inches
Sending data
Picture 1-1
Wiring the Ultrasonic Range Finder
I am using the Vex Ultrasonic sensor in this tutorial,
which works exceptionally well. On this sensor, there are
two ports which are the output and the input.
Steps:
1. Create two female to female PWM (pulse width
modulation) cables.
2. Attach one of the female to female cable to the
output wire, then attach the other to the input wire
on the sensor
3. Then attach the two wires to any two Digital Input
ports on the Digital Sidecar, in this tutorial I used
ports 1 and 2.
DI 1
DI 2
Picture 1-2
Programming The Ultrasonic Range Finder
When Programming the ultrasonic range finder, a
few things should be taken into consideration. Firstly,
when the ultrasonic range finder is out of range, it gives a
value of 285 inches, or 7239 millimeters. This can be due
to the sensor is not getting back the signal that is sent or
it is just out of range if that’s not the case.
Steps:
1. First you have to declare and ultrasonic sensor under
class using the ultrasonic class from the WPi library.
Ultrasonic *range;
2. Then you would declare the ports the sensor is
plugged into on the digital sidecar under public:
Range = new Ultrasonic(output, input);
In my case output is 2 and input is 1.
3. If your using Java, it should be:
Ultrasonic range = new Ultrasonic(output, input);
then fix the imports.
4. The you will need to enable the sensor using the
line: Range.SetAutomaticMode(true);
In C++ your code will look something like:
#include "WPILib.h"
class Team369 : public SimpleRobot
{
RobotDrive Robot;
Ultrasonic *Range;
public:
Team369(void):
Robot(1, 2)
{
Range = new Ultrasonic(1, 2);
Robot.SetExpiration(0.1);
Range->SetAutomaticMode(true);
}
void Autonomous(void)
{
// an ideal test for this sensor would be
Robot.SetSafetyEnabled(false);
while(Range->GetRangeInches() > 20){
Robot.TankDrive(0.5, 0.5);
}
Robot.TankDrive(0.0, 0.0);
}
};
START_ROBOT_CLASS(Team369);
In Java your code will look something like:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Utrasonic;
public class Team369 extends SimpleRobot {
RobotDrive robot = new RobotDrive(1, 2);
Ultrasonic range = new Ultrasonic(2, 1);
public void autonomous() {
// an ideal test for the sensor
Range.setAutomaticMode(true);
while (isAutonomous() && isEnabled() && range.getRangeInches() >
20){
robot.tankDrive(0.5, 0.5);
}
robot.tankDrive(0.0, 0.0);
}
}