Introduction to Sensors Lesson

Introduction to Sensors

To build a truly "smart" robot, we need to give it senses. Sensors are the eyes, ears, and sense of touch that allow a robot to understand its own state and the world around it, enabling precise and automated movements.

Why Do We Need Sensors?

Sensors provide the real-world feedback necessary for reliable autonomous modes and more precise driver-controlled actions. They answer critical questions like "Have I driven exactly 3 feet?" or "Is my arm at a 45-degree angle?"

The Three Most Common Sensors

1. Encoders: The Sense of Position and Speed

An encoder is a sensor that measures the rotation of a shaft, typically on a motor or a wheel. By counting "ticks" of rotation, it can tell us about distance and speed.

Common Uses: Driving a precise distance in autonomous, moving an arm to a specific angle, or ensuring a shooter wheel is at the correct RPM.

// Encoders are plugged into two digital input ports on the roboRIO.
Encoder drivetrainEncoder = new Encoder(0, 1);

// Configure the encoder so getDistance() returns inches instead of raw ticks.
drivetrainEncoder.setDistancePerPulse( (Math.PI * 4) / 2048.0 ); // (Wheel Circumference / Ticks Per Revolution)

// Get the distance the robot has traveled in inches.
double distance = drivetrainEncoder.getDistance();

// Get the current speed in inches per second.
double speed = drivetrainEncoder.getRate();
    

2. Limit Switches: The Sense of Touch

A limit switch is a simple physical button that acts as a digital sensor. It tells you if something is physically touching it, returning either `true` or `false`.

Common Uses: "Zeroing" a mechanism (running it to a known start position), preventing a mechanism from moving too far, or detecting if a game piece is in an intake.

// A limit switch is plugged into one digital input port.
DigitalInput armLimitSwitch = new DigitalInput(2);

// The .get() method returns the switch's state.
// Due to wiring, the value is often 'false' when pressed, so we use '!' to invert it.
boolean isArmAtBottom = !armLimitSwitch.get();

if (isArmAtBottom) {
    // The arm has hit the bottom limit, so we should stop the motor.
}
    

3. Gyroscopes: The Sense of Direction

A gyroscope, or "gyro," is the robot's internal compass. It measures rotation and heading, telling us which direction the robot is facing.

Common Uses: Driving perfectly straight in autonomous, turning to a precise angle, or balancing on game elements.

// Gyros are often connected via specific ports like SPI.
// Different gyros have different classes. This is for the ADXRS450.
ADXRS450_Gyro gyro = new ADXRS450_Gyro();

// Reset the gyro's current heading to zero.
gyro.reset();

// Get the robot's current angle in degrees.
// Turning right is positive, turning left is negative.
double robotHeading = gyro.getAngle();
    

Test Your Knowledge

Question: Your team needs to program an autonomous routine to drive forward exactly 5 feet. Which sensor is the most appropriate for this task?