Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MCP23017 TCS3472_I2C WattBob_TextLCD mbed-rtos mbed
colourProcessing.h
00001 #include "mbed.h" 00002 #include "TCS3472_I2C.h" 00003 #include "Colour.h" 00004 /* TCS3472_I2C 00005 * A MBED library specifically made for the TCS3472 RGB Colour Sensor. 00006 */ 00007 TCS3472_I2C rgb_sensor(p28,p27); //p28 =sda, p27=scl 00008 00009 int rgb_readings[100][4]; //A 2D array to store 100 values of each colour. 00010 int rgb_average[4] = {0,0,0,0}; //An array to store the average colour values calculated. 00011 double rMax = 9244; //The maximum possible value of red, from the colour sensor. 00012 double gMax = 3194; //The maximum possible value of red, from the colour sensor. 00013 double bMax = 3590; //The maximum possible value of red, from the colour sensor. 00014 00015 int tubeSize = 20; //The upper software limit for the number of chips that can be stored in each tube. 00016 00017 /* Threshold values for each colour are defined below. 00018 * Threshold values for none (nothing above the sensor) are also stored, 00019 * so that we know when there are no chips in the hopper. 00020 * This allows us to stop sorting when there are no chips in the hopper, 00021 * and differenciate between nothing there and an invalid token colour above the sensor. 00022 */ 00023 int redLT [3] = {308,84,162}; //The lower threshold value for red. 00024 int redUT [3]= {400,144,204}; //The upper threshold value for red. 00025 00026 int greenLT [3] = {91,180,142}; //The lower threshold value for green. 00027 int greenUT [3]= {132,220,184}; //The upper threshold value for green. 00028 00029 int blueLT [3]= {79,95,117}; //The lower threshold value for blue. 00030 int blueUT [3]= {117,139,157}; //The upper threshold value for blue. 00031 00032 int noneLT [3] = {0,0,0}; //The lower threshold value for none. 00033 int noneUT [3] = {80,80,80}; //The upper threshold value for none. 00034 00035 void initColourSensor() //Intialise the colour sensor. 00036 { 00037 rgb_sensor.enablePowerAndRGBC(); //Enable the internal oscillator and 2-channel ADC 00038 rgb_sensor.setIntegrationTime(100); //Set integration time to 100. Longer integration time increases sensitivity for low-light. 00039 } 00040 /* readColourSensor() 00041 * Takes 100 readings of all 4 colour values and takes an average of the 100 values. 00042 * The average is used to allow the chip to settle on the colour sensor. 00043 * 00044 */ 00045 Colour readColourSensor() 00046 { //Take 100 Colour readings 00047 for(int i = 0; i < 100; i++) { 00048 rgb_sensor.getAllColors(rgb_readings[i]); 00049 Thread::wait(1); 00050 } 00051 00052 //Add the 100 values together for each colour 00053 for(int i = 0; i < 100; i++) { 00054 for(int j = 0; j < 4; j++) { 00055 rgb_average[j] += rgb_readings[i][j]; 00056 } 00057 } 00058 00059 //Divide the four summated values by 100 to get the four averages (one for each colour). 00060 for(int i = 0; i < 4; i++) { 00061 rgb_average[i] = rgb_average[i] / 100; 00062 } 00063 00064 //Scale the red, green and blue values bwetween 0 and 255. 00065 double redd = (rgb_average[1] /gMax) * 255; 00066 double greend = (rgb_average[2] /bMax) * 255; 00067 double blued = (rgb_average[0] /rMax) * 255; 00068 00069 //Convert the values from doubles to integers. 00070 int red = redd; 00071 int green = greend; 00072 int blue = blued; 00073 00074 //Create boolean arrays to check threshold values. 00075 bool redWithinThreshold[4] = {0,0,0,0}; 00076 bool greenWithinThreshold[4]= {0,0,0,0}; 00077 bool blueWithinThreshold[4]= {0,0,0,0}; 00078 00079 //Set red Thresholds 00080 redWithinThreshold[0] = (red >= redLT[0]) && (red <= redUT[0]); 00081 greenWithinThreshold[0] = (green >= redLT[1]) && (green <= redUT[1]); 00082 blueWithinThreshold[0] = (blue >= redLT[2]) && (blue <= redUT[2]); 00083 00084 //Set green Thresholds 00085 redWithinThreshold[1] = (red >= greenLT[0]) && (red <= greenUT[0]); 00086 greenWithinThreshold[1] = (green >= greenLT[1]) && (green <= greenUT[1]); 00087 blueWithinThreshold[1] = (blue >= greenLT[2]) && (blue <= greenUT[2]); 00088 00089 //Set blue Thresholds 00090 redWithinThreshold[2] = (red >= blueLT[0]) && (red <= blueUT[0]); 00091 greenWithinThreshold[2] = (green >= blueLT[1]) && (green <= blueUT[1]); 00092 blueWithinThreshold[2] = (blue >= blueLT[2]) && (blue <= blueUT[2]); 00093 00094 //Set none Thresholds 00095 redWithinThreshold[3] = (red >= noneLT[0]) && (red <= noneUT[0]); 00096 greenWithinThreshold[3] = (green >= noneLT[1]) && (green <= noneUT[1]); 00097 blueWithinThreshold[3] = (blue >= noneLT[2]) && (blue <= noneUT[2]); 00098 00099 //Check the threshold values to find out what colour is on the sensor. 00100 if(redWithinThreshold[0] && greenWithinThreshold[0] && blueWithinThreshold[0]) { 00101 return RED; 00102 } else if(redWithinThreshold[1] && greenWithinThreshold[1] && blueWithinThreshold[1]) { 00103 return GREEN; 00104 } else if(redWithinThreshold[2] && greenWithinThreshold[2] && blueWithinThreshold[2]) { 00105 return BLUE; 00106 } else if(redWithinThreshold[3] && greenWithinThreshold[3] && blueWithinThreshold[3]) { 00107 return NONE; 00108 } else { 00109 return BIN; 00110 } 00111 00112 } 00113 00114 00115
Generated on Thu Jul 14 2022 19:06:18 by
1.7.2