Skip to content

Programming Your Canandcolor

The central point of the Canandcolor in ReduxLib is the Canandcolor object. The Canandcolor object lets you read the detected color, measured proximity, set and read settings, read faults, and more.

Creating a Canandcolor object is simple. The one parameter you need is the CAN device ID assigned to the sensor, generally configured by Alchemist.

// Creates a Canandcolor object referencing a Canandcolor with CAN ID 0
Canandcolor canandcolor = new Canandcolor(0);
// Creates a Canandcolor object referencing a Canandcolor with CAN ID 3
Canandcolor canandcolor3 = new Canandcolor(3);

Proximity in the Canandcolor is represented as a value in the range [0, 1] which decreases as the sensed object approaches the sensor.

This represents the intensity of reflected IR to the sensor, and will saturate to 1 if an object is too close to the sensor.

double proximity = canandcolor.getProximity();

Color in the Canandcolor is represented as individual values in the range [0, 1] which represents the intensity of that channel of color.

For example, a pure red light will have a red color value close to 1, and blue/green color values close to 0.

In addition, ColorData contains helper functions to convert the sensed color to HSV, and to WPILib Color objects. For most applications, users will want to use the HSV functions instead of RGB, as HSV is much easier to tune and senses color more independent of ambient conditions and brightness.

//Get the raw RGB values from the sensor
double red = canandcolor.getRed();
double blue = canandcolor.getBlue();
double green = canandcolor.getGreen();
//Get the HSV readings from the color object
double hue = canandcolor.getHSVHue();
double saturation = canandcolor.getHSVSaturation();
double value = canandcolor.getHSVValue();
//Get a Canandcolor ColorData object from the sensor
ColorData color = canandcolor.getColor();
//Convert the color to a WPILib color instance
Color wpiColor = color.toWpilibColor();

Presence Detection checks if the sensor has sent a message in the last 2 seconds (by default). This is a useful tool to ensure that the sensor has not disconnected from the CAN bus. ReduxLib will also automatically warn the driver station of possibly disconnected devices.

canandcolor.isConnected();

The temperature function reports the onboard temperature reading of the device. This should be kept within -5 and 45 degrees Celsius for normal operation.

canandcolor.getTemperature();

Resets the sensor to factory defaults. CAN device IDs are preserved.

canandcolor.resetFactoryDefaults();