Tutorial: Game Piece Sensing
Detecting game pieces is a fundamental task for any FRC robot. The Canandcolor is uniquely suited for this because it combines a high-speed proximity sensor with a precise color sensor.
In this tutorial, we will walk through detecting a specific game piece (like a yellow Note or a white Coral) using both distance and color thresholds.
Prerequisites
Section titled “Prerequisites”- A Redux Canandcolor configured and wired (see Your First Canandcolor)
- A game piece for testing
1. Finding your Thresholds
Section titled “1. Finding your Thresholds”Before writing code, you need to know what “values” your game piece produces.
-
Open the Dashboard
Navigate tohttp://roborio-XXXX-frc.local:7244and find your Canandcolor. -
Measure Distance
Place your game piece at the distance where you want your intake to stop or trigger. Note the Proximity value. -
Measure Color
Look at the Hue value.- Yellow (Notes): Typically around 0.10 - 0.15
- Purple (Cubes): Typically around 0.70 - 0.80
- Green (Cones): Typically around 0.20 - 0.30
2. Implementation
Section titled “2. Implementation”We will create a helper method that returns true only when an object is both close enough AND the correct color.
import com.reduxrobotics.sensors.canandcolor.Canandcolor;
public class IntakeSubsystem { private final Canandcolor m_sensor = new Canandcolor(1);
// Thresholds (Adjust these based on your testing!) private static final double kMinHue = 0.10; private static final double kMaxHue = 0.20; private static final double kProximityThreshold = 0.3;
public boolean hasGamePiece() { double proximity = m_sensor.getProximity(); double hue = m_sensor.getHSVHue();
// Check if object is close (proximity < threshold) // AND hue is within the expected range return (proximity < kProximityThreshold) && (hue >= kMinHue && hue <= kMaxHue); }}3. Handling Ambient Light
Section titled “3. Handling Ambient Light”If your intake is open to the field, ambient light can sometimes mess with color readings.
// In your constructor or initm_sensor.setLampLEDBrightness(1.0); // Full brightnessNext Steps
Section titled “Next Steps”For even faster detection with zero CPU overhead, check out the Zero Latency Game Piece Sensing tutorial.