Programming Overview¶
The central point of the Canandcolor in ReduxLib is the Canandcolor object. The Canandcolor object lets you read the detected color, meausred proximity proximity, set and read settings, read faults, and more.
Constructor¶
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);
using namespace redux::sensors::canandcolor;
// Creates a Canandcolor object referencing a Canandcolor with CAN ID 0
Canandcolor canandcolor{0};
// Creates a Canandcolor object referencing a Canandcolor with CAN ID 3
Canandcolor canandcolor3{3};
Reading Proximity¶
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.
Why is proximity unitless?
The returned proximity value depends on the material as the reflectivity of IR will change how proximity relates to proximity.
This is because proximity is proportional to reflected IR light intensity, rather than proximity directly.
It is intended that users measure the proximity value in conditions they are going to use the sensor in, and then hard-code thresholds relevant to their application.
double proximity = canandcolor.getProximity();
double proximity = canandcolor.GetProximity();
Reading Color¶
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¶
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();
canandcolor.IsConnected();
Temperature¶
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();
canandcolor.GetTemperature();
Reset To Factory Default¶
Danger
Resetting the sensor to factory default will wipe all settings. There is no way to undo this other then manually re-inputting all of your settings.
Resets the sensor to factory defaults. CAN device IDs are preserved.
canandcolor.resetFactoryDefaults();
canandcolor.ResetFactoryDefaults();