Ultrasonic Sensor - LdhssRobotics/How-Tos-2015 GitHub Wiki

By: Bravween Perumalz

Intro

Sound is a natural phenomenon which allows us to recognize our surroundings without physical contact over a wide distance. This phenomenon is produced by simply causing the air to vibrate creating waves. Ultrasonic sensors produce over 20 000 Hz which humans are unable to hear. They're very commonly used to measure distances because they are inexpensive and very easy to handle. These sensors emit a sound wave which bounces off of objects creating an echo which the sensor picks up. By measuring time intervals, between sending the sound wave and receiving echos, the sensor can determine the distance of the target. They are used for avoiding obstacles, navigating, and positioning the robot.

Wiring

The sensor uses a 4 pin wire which connects to the roboRIO. As seen in the picture above, the sensor contains 4 pins each pin has a different function. VCC means 5 volts of power, TRIG sends out a pulse of sound, ECHO receives the pulse, and GRND is the ground pin. All 4 of these pins gets connected to the digital input/output port (DIO) of the roboRIO.

There are several types of sensors. We use either a 3 pin or a 4 pin. On the 3 pin, the SIG pin takes care of both the ECHO and the TRIG while 4 pin just separates the Signal pin into TRIG and ECHO.

The sensor must be plugged into the DIO ports. If using the 3 pin, connect the GRND and VCC wires into one port and the SIG into another. Remember the port which the signal wire is linked too.

If using the 4 pin, the GRND and VCC wires both connect into one port, the TRIG and ECHO pins must both be on their own seperate ports. So 3 ports should be taken up in the DIO section of the roboRIO. Remember the port number which the TRIG and ECHO wires are connected too.

Programming

For basic c++ programming go to https://github.com/LdhssRobotics/How-Tos/wiki/Basic-Programming

##Declare the ultrasonic class and give it a name. Once that is done, declare which ports the sensor and the joystick is connected to on the roboRIO. class Robot: public SampleRobot { Ultrasonic mySonic; // declare instance of ultrasonic class Joystick stick; // only joystick

public:
    Robot() :
			    mySonic(2,3), //Port numbers depend on which port on roboRIO
		    stick(0)
    {
    }

##Next it is a good idea to print the values you obtain onto the smartdashboard. In the void OperatorControl type if(stick.GetX()){ SmartDashBoard::PutNumber("Distance", mySonic.GetRangeInches()); }

This will print the distance of the sensor to the target onto the smartdashboard every time you move the joystick. Try moving the sensor away from the target and make sure that you are getting different readings each time you move the joystick. The if statement isn't too important. Removing the if statement will constantly be printing values to the smartdashboard.

##This distance sensor is really useful in autonomous mode. You can make the robot position itself for the task needed. With this code, if the robot is set to autonomous, it will constantly be checking the distance from the target in front of the sensor. In the if statement, you can make the robot do multiple things.( eg. 2015 recycle rush, in autonomous you can position the robot in front of the totes and have it position itself perfectly then lift the box.)

#Mounting the Sensor Remember that the sensor must be position perpendicular to the robot. You must measure the distance between the sensor to where you want to receive the distance. For example, if the robot is lifting a box, the sensor will measure the distance between itself and the box. Once that's done, your code must use this value to determine how far the robot must move in order to position itself. So if there are latches on the robot where it can hook onto the box, measure the distance between this latch and the sensor yourself and put it into the code. Next you would measure the distance from the front of the box to the hooking area to determine how far the robot must move to position itself with the latch and hooking area.