© Fort Street High School Robotics
Colour sensors return red, green and blue (RGB) values. They're like light sensors but instead of shining red/white light and measuring the reflected light, they shine red, green and blue light and measure their reflected components.
This is a lot of data to work with (you will have six variables, 3 for left sensor, 3 for right). Sometimes we add them up to get a combined 'white' value (though you will notice some colours change slower than others). You will have to experiment with this.
Our eyes also do not see the same way as a colour sensor - what we say is green might not be green. So, always trust your colour sensor. (If you print a piece of paper that has a rgb(255,0,0) red square, you won't actually get a reading of 255, 0, 0 - printers usually print brighter than actual).
To get the RGB values, it depends on what sensor/brick you're using and what RobotC version you're using. For NXT users, we download a driver/library so that we can communicate with these sensors - this is because there are no NXT colour sensors so no 'in-built' command. Drivers are pieces of C code that let our normal RobotC code communicate with the sensor. Libraries are a broader term for any group of C code that helps us code.
Most of you are using this.
Download all drivers https://github.com/botbench/rdpartyrobotcdr/archive/master.zip
To read values, you need to create variables and then pass them into a function. The driver will place values back into the variables for you to use. To include the driver, you need to add one line at the top (with a path to the colour .h file you downloaded above).
#pragma config(Sensor, S1, LeftColourSensor, sensorI2CCustom)
// Attach the colour sensor to port 1
#include "hitechnic-colour-v2.h"
task main() {
// Create variables which we can store
int lRed, lGreen, lBlue;
// Repeatedly get values
while (1) {
// Read currently detected RGB values
// If this function returns true, the values are correct.
if (HTCS2readRGB(LeftColourSensor, lRed, lGreen, lBlue)) {
// Show the red, green and blue values on the screen
nxtDisplayCenteredTextLine(3, "%3d %3d %3d", lRed, lGreen, lBlue);
}
}
}
Download all drivers https://github.com/botbench/robotcdriversuite/archive/master.zip
To read values, you need to create a struct (a collection of data) and then pass it into a function. The driver will place values back into the struct for you to use. To include the driver, you need to add one line at the top (with a path to the colour .h file you downloaded above).
#pragma config(Sensor, S1, LeftColourSensor, sensorI2CCustom)
// Attach the colour sensor to port 1
#include "robotcdriversuite/drivers/hitechnic-colour-v2.h"
task main() {
// Create struct and some variables which we can store data
tHTCS2 leftColourData;
int lRed, lGreen, lBlue;
// Initialise and configure the sensor
initSensor(&leftColourData, S1);
// Repeatedly get values
while (1) {
// Read currently detected RGB values
// If this function returns true, the values are correct.
if (readSensor(&leftColourData)) {
// Move data from struct to ints
lRed = leftColourData.red;
lGreen = leftColourData.green;
lBlue = leftColourData.blue;
// Show the red, green and blue values on the screen
nxtDisplayCenteredTextLine(3, "%3d %3d %3d", lRed, lGreen, lBlue);
}
}
}
You don't need drivers for EV3 colour sensors - they're part of RobotCv4.
To read values, you need to create variables and then pass them into a function. The function will place values back into the variables for you to use.
#pragma config(Sensor, S1, LeftColourSensor, sensorEV3_Color, modeEV3Color_RGB_Raw)
// Attach the colour sensor to port 1
#include "hitechnic-colour-v2.h"
task main() {
// Create variables which we can store
int lRed, lGreen, lBlue;
// Repeatedly get values
while (1) {
// Read currently detected RGB values
getColorRGB(LeftColourSensor, lRed, lGreen, lBlue);
// Show the red, green and blue values on the screen
displayCenteredTextLine(3, "%3d %3d %3d", lRed, lGreen, lBlue);
}
}