Updated 7/22/2017 to include Rev Robotics hardware.
The following is a quick overview that should help you get up-and-running with a color sensor on your robot. Although we’ll use the Modern Robotics color sensor as an example, most of the code will work for the Adafruit RGB color sensor and Rev Robotics color sensor as well. We assume you’re using Android Studio to write and build your programs. Want to contribute information related to another method, or make a correction? Let us know.
Hardware
In order to work with color sensors, we’ll need a few things:
- Modern Robotics Power Distribution Module (PDM)
- Modern Robotics Device Interface Module
- Mordern Robotics or Adafruit RGB color sensor
OR
- Rev Robotics Expansion Hub
- Rev Robotics color sensor or Modern Robotics sensor + Rev level shifter
Modern robotics and Adafruit sensors can be attached to one of the I2C ports on the Modern Robotics Device Interface module. The I2C port numbers are not printed on the module, but you can follow the example of the analog and digital ports to figure it out (port numbers increase from zero at the bottom). Align the black wire with the black mark above the port. A USB A-to-B-Mini cable runs from the Device Interface module to one of the USB-A ports on the PDM. Luckily, this module doesn’t require any power besides what is supplied by the USB cable.
Rev Robotics color sensors can be plugged in directly to one of the Rev Expansion Hub’s I2C ports. Adafruit RGB sensors can be plugged in directly as well. To use a Modern Robotics sensor, we’ll need a Rev level shifter (the sensor is a 5V device while the Expansion Hub is a 3.3V device). It is necessary to use the short cable (optionally available with the level shifters) to change the order of the wires for the sensor.
Software Setup
To declare a variable representing the color sensor, we’ll need to include an import statement at the top of our file, right after the package statement:
1 |
import com.qualcomm.robotcore.hardware.ColorSensor; |
We probably want to declare a class-level variable for the sensor. It’s a good idea to name variables something relevant to what they represent:
1 2 3 4 5 6 |
public class YourClassName extends OpMode { ColorSensor color_sensor; // ... } |
During the init() process (or before waitForStart() in a Linear OpMode) we should initialize the color sensor variable. Inside the get("...") method, we need to use the name we gave the sensor in the configuration file on the Robot Controller phone (in this case, we imagine it’s called “color”). It’s a good idea to do this in init() because if the robot can’t find our sensor, we’ll want to know before starting the match.
1 2 3 4 5 6 |
public void init() { color_sensor = hardwareMap.colorSensor.get("color"); // ... } |
Reading the Sensor
Chances are, we want to do everything you see below in the loop() method (or in runOpMode() after the waitForStart() if we’re writing a Linear OpMode). There are a number of different values we can read from the sensor:
1 2 3 4 5 6 |
color_sensor.red(); // Red channel value color_sensor.green(); // Green channel value color_sensor.blue(); // Blue channel value color_sensor.alpha(); // Total luminosity color_sensor.argb(); // Combined color value |
We can learn about these methods using the JavaDocs for the FTC App. It’s a good idea to try printing these values to the Driver Station phone using telemetry.addData() . The range of values reported for any of these variables depends on how the sensor is calibrated.
With the Modern Robotics sensor, we can also turn the sensor’s LED on and off without much difficulty:
1 2 |
color_sensor.enableLed(true); // Turn the LED on color_sensor.enableLed(false); // Turn the LED off |
In Practice
Here’s an example of how we might use the color sensor in an Linear autonomous program:
1 2 3 4 5 |
while (color_sensor.alpha() < 20) { // Drive Forward } // Stop and do something else. |
If the sensor is pointed at the ground, the robot would drive forward until sensing a white piece of tape, then stop and do something else. We’d need to change the threshold (20) based on the kinds of readings given by the color sensor. In general, a threshold halfway between the two extremes (the grey tile and the white tape) is a good choice.