Paul's Tutorials - logo3-1Sensors and Information



Now that you know how to make the robot move, now it is time to make it move intelligently. However, this can't be done without some sort of indication of the robot's state. This is what sensors are for.

This lesson will discuss how to obtain values from the various sensors on the robot.

Providing Information

Sensors are useless unless you can get a reading from them. During competition, the robot monitors these sensors and acts upon the values it receives completely of its own accord, but the robot does not know what values to act upon unless you tell the robot those values.

So, the important thing when you start dealing with sensors is that you output the value of the sensor directly to SmartDashboard, so you can see the raw value of the sensor at any given moment and gather the necessary data from it. You can do this by feeding the sensor's read function directly into the value parameter of the Put functions.

Reading from Sensors

The following section contains a selection of functions from commonly used sensors. Refer to the WPI doxygen for the API of all sensor classes.

A Special Function

Most sensors should have this function:

int PIDGet()

This function is special, because it will always return the sensor's main reading. This is important for later, because this provides the input to the PIDController that you will learn to use in lesson 3-3.

Analog Sensors

AnalogInput

Analog devices use voltage as a means of measurement. What the voltage range is depends on the sensor being used. Potentiometers, the device most commonly used that falls under this class, have a range of about 0-6 volts.

float AnalogInput::GetAverageVoltage()

Digital Sensors

DigitalInput

Devices that use the DigitalInput class return true/false values, as they are all switches of some sort.

bool DigitalInput::Get()

Encoder

When you are dealing with an encoder, you should think of it as the trip odometer in your car. It counts up in the direction it deems forward, and then down if driven backward. You use its input to determine if something needs to be done, and how it needs to be done. Should you do something that will only work when a certain value is hit, then you should reset the encoder.

The following are functions that you might commonly use with an encoder:

//Get the distance recorded by the encoder as per Encoder::SetDistancePerPulse()
double Encoder::GetDistance()

//Get the rate (velocity) of the encoder measured in distance units per second
float Encoder::GetRate()

//Reset the encoder count
void Encoder::Reset()

Accelerometer

Note: Different accelerometers (such as the onboard accelerometer in the roboRIO or external accelerometers) will use different class names, but they all inherit Acceleromter, so these functions will be available in any accelerometer object.

Since accelerometers can measure acceleration on multiple axes, you need to call the function with the appropriate axis.

double Accelerometer::GetX()
double Accelerometer::GetY()
double Accelerometer::GetZ()

Activity

Find a wiring schematic for a robot that is available right now. Program it so that you can actuate all of the motors, and print all of the sensor inputs to SmartDashboard.

What is given below is for the 2015 robot.


← Chapter 3 Overview 3-1 Sensors and Information 3-2 PID Part 1: PID Theory →