Tutorials
Zero to Hero
Episode 4: Sensors & Perception

🎯 The Sensor Arsenal


πŸ›‘ Chapter 1: The Limit Switch (Digital Input)

The humble limit switch. It prevents your elevator from smashing through the ceiling.

// Hardware
DigitalInput m_limitSwitch = new DigitalInput(0); // Plugged into DIO 0
 
public boolean isAtTop() {
    // Note: Most switches are "Normally Open" (False when not pressed)
    // BUT checking wiring is crucial.
    return m_limitSwitch.get();
}

Debouncing

Sometimes switches flicker (On-Off-On-Off) very fast when hit. You can "debounce" this in software.

Debouncer m_debouncer = new Debouncer(0.1); // Must stay stable for 0.1s
 
public boolean isPressedStable() {
    return m_debouncer.calculate(m_limitSwitch.get());
}

πŸ“ Chapter 2: Encoders

Encoders count rotations.

  • Relative Encoders: "I moved 50 steps." (Forgot where 0 was when power cut).
  • Absolute Encoders: "I am at 45 degrees." (Remembers position even when off).

Using TalonFX Integrated Encoders (Relative)

The TalonFX (Falcon 500 / Kraken) has a built-in encoder.

// Get position in Rotations
double rotations = m_motor.getPosition().getValue();
 
// Get velocity in Rotations per Second (RPS)
double velocity = m_motor.getVelocity().getValue();

The Math: Rotations to Meters

Robots don't drive in "rotations". They drive in meters or inches.

public double getDistanceMeters() {
    double motorRotations = m_motor.getPosition().getValue();
    double gearRatio = 8.14; // e.g., L2 SDS Module
    double wheelDiameterMeters = Units.inchesToMeters(4);
 
    double wheelRotations = motorRotations / gearRatio;
    return wheelRotations * (Math.PI * wheelDiameterMeters);
}
πŸ’‘

Math Tip: Circumference = Pi * Diameter. Distance = Rotations * Circumference.


🧭 Chapter 3: The Gyroscope (IMU)

The Gyro tells you your Heading (Yaw). Essential for Field-Oriented Drive. Common Gyros: Pigeon 2.0, NavX.

Pigeon2 m_gyro = new Pigeon2(0, "rio");
 
public double getHeadingDegrees() {
    // Returns continuous angle (0, 360, 720...)
    return m_gyro.getYaw().getValue();
}
 
public Rotation2d getRotation2d() {
    // Returns -180 to 180 degrees standardized
    return m_gyro.getRotation2d();
}
 
public void resetHeading() {
    m_gyro.setYaw(0);
}

Why Field-Oriented Drive?

If the robot spins 90 degrees, "Forward" on the joystick should still move the robot away from the driver, not "Robot Forward". You need a Gyro to do the math to rotate the joystick inputs!


🏁 Summary

Now your robot can:

  1. Feel walls (Limit Switches).
  2. Measure distance (Encoders).
  3. Know direction (Gyros).

But knowing where you are isn't enough. You need to know how to get to where you want to go. That requires PID Control, the topic of Episode 5!


πŸ“ž Need Help? We've Got Your Back!

Keep coding, keep learning, and remember: every expert was once a beginner who refused to give up! πŸš€

P.S. - If your robot starts talking back, that's either very advanced AI or you need more sleep. Probably more sleep. πŸ€–πŸ’€


This documentation is part of the Zero to Hero programming series. For the complete learning experience, watch the accompanying video and practice with real robot code. Remember: the only way to get good at programming is to write lots of bad code first! πŸ˜„

πŸ’

Special Thanks: To all the students who asked "Why doesn't this work?" and inspired us to create better documentation. Your questions make us better teachers, and your curiosity drives innovation. You're the real MVPs! πŸ†

Remember: Every expert was once a beginner who refused to give up. Keep coding, keep learning, and most importantly - keep having fun! πŸš€