Skip to content

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.

Before writing code, you need to know what “values” your game piece produces.

  1. Open the Dashboard
    Navigate to http://roborio-XXXX-frc.local:7244 and find your Canandcolor.

  2. Measure Distance
    Place your game piece at the distance where you want your intake to stop or trigger. Note the Proximity value.

  3. 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

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);
}
}

If your intake is open to the field, ambient light can sometimes mess with color readings.

// In your constructor or init
m_sensor.setLampLEDBrightness(1.0); // Full brightness

For even faster detection with zero CPU overhead, check out the Zero Latency Game Piece Sensing tutorial.