The MBED firmware used on the Chipin sorter, developed over 12 weeks for a 3rd year university systems project. Chipin is a token sorter, it sorts tokens by colours and dispenses them to order through an online booking system and card reader. This program interfaces with an FPGA, PC and LCD screen to control the sorter. The sorter has an operation mode where it can process orders when a card is entered into the machine. There is also a maintenance mode where the device responds to maintenance instructions such as 'dispense all'. More information at http://www.ionsystems.uk/

Dependencies:   MCP23017 TCS3472_I2C WattBob_TextLCD mbed-rtos mbed

Committer:
IonSystems
Date:
Fri Nov 21 22:14:21 2014 +0000
Revision:
15:0c5f20e15b6a
Parent:
14:31ba3e56c788
Child:
17:6a0bb0ad5bb4
card object created and database object created for testing language emun created

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IonSystems 6:e64796f1f384 1 #include "mbed.h"
IonSystems 6:e64796f1f384 2 #include "TCS3472_I2C.h"
IonSystems 6:e64796f1f384 3 #include "Colour.h"
IonSystems 11:06f6e82b40a8 4
IonSystems 6:e64796f1f384 5 TCS3472_I2C rgb_sensor(p28,p27); //p28 =sda, p27=scl
IonSystems 6:e64796f1f384 6
IonSystems 14:31ba3e56c788 7 int rgb_readings[1000][4];
IonSystems 14:31ba3e56c788 8 int rgb_average[4] = {0,0,0,0};
IonSystems 6:e64796f1f384 9 double rMax = 9244;
IonSystems 6:e64796f1f384 10 double gMax = 3194;
IonSystems 6:e64796f1f384 11 double bMax = 3590;
IonSystems 6:e64796f1f384 12
IonSystems 6:e64796f1f384 13 //thresholds are in the RGB format.
IonSystems 11:06f6e82b40a8 14 /*
IonSystems 11:06f6e82b40a8 15 Values for black casing
IonSystems 6:e64796f1f384 16 int redLT [3] = {79,23,41};
IonSystems 6:e64796f1f384 17 int redUT [3]= {92,28,49};
IonSystems 6:e64796f1f384 18 int greenLT [3] = {46,81,61};
IonSystems 6:e64796f1f384 19 int greenUT [3]= {60,107,81};
IonSystems 6:e64796f1f384 20 int blueLT [3]= {25,47,47};
IonSystems 11:06f6e82b40a8 21 int blueUT [3]= {28,52,52};*/
IonSystems 11:06f6e82b40a8 22 //values for blue backing
IonSystems 14:31ba3e56c788 23 int redLT [3] = {280,50,50};
IonSystems 14:31ba3e56c788 24 int redUT [3]= {420,180,260};
IonSystems 14:31ba3e56c788 25 int greenLT [3] = {60,190,106};
IonSystems 14:31ba3e56c788 26 int greenUT [3]= {180,270,226};
IonSystems 14:31ba3e56c788 27 int blueLT [3]= {59,68,85};
IonSystems 14:31ba3e56c788 28 int blueUT [3]= {169,188,205};
IonSystems 13:0661d658d9d1 29 int noneLT [3] = {0,0,0};
IonSystems 13:0661d658d9d1 30 int noneUT [3] = {80,80,80};
IonSystems 6:e64796f1f384 31
IonSystems 6:e64796f1f384 32 void initColourSensor(){
IonSystems 6:e64796f1f384 33 rgb_sensor.enablePowerAndRGBC();
IonSystems 6:e64796f1f384 34 rgb_sensor.setIntegrationTime(100);
IonSystems 6:e64796f1f384 35 }
IonSystems 6:e64796f1f384 36
IonSystems 5:644bca33c1ca 37 Colour readColourSensor(){
IonSystems 14:31ba3e56c788 38 lcd->cls(); // clear display
IonSystems 14:31ba3e56c788 39 lcd->locate(0,0);
IonSystems 14:31ba3e56c788 40 lcd->printf("Reading Sensor");
IonSystems 14:31ba3e56c788 41 for(int i = 0; i < 1000; i++){
IonSystems 14:31ba3e56c788 42 rgb_sensor.getAllColors(rgb_readings[i]);
IonSystems 14:31ba3e56c788 43 wait(0.0025);
IonSystems 14:31ba3e56c788 44 }
IonSystems 14:31ba3e56c788 45 for(int i = 0; i < 1000; i++){
IonSystems 14:31ba3e56c788 46 for(int j = 0; j < 4; j++){
IonSystems 14:31ba3e56c788 47 rgb_average[j] += rgb_readings[i][j];
IonSystems 14:31ba3e56c788 48 }
IonSystems 14:31ba3e56c788 49 }
IonSystems 14:31ba3e56c788 50 for(int i = 0; i < 4; i++){
IonSystems 15:0c5f20e15b6a 51 rgb_average[i] = rgb_average[i] / 1000;
IonSystems 14:31ba3e56c788 52 }
IonSystems 14:31ba3e56c788 53 double redd = (rgb_average[1] /gMax) * 255;
IonSystems 14:31ba3e56c788 54 double greend = (rgb_average[2] /bMax) * 255;
IonSystems 14:31ba3e56c788 55 double blued = (rgb_average[0] /rMax) * 255;
IonSystems 5:644bca33c1ca 56
IonSystems 5:644bca33c1ca 57 int red = redd;
IonSystems 5:644bca33c1ca 58 int green = greend;
IonSystems 5:644bca33c1ca 59 int blue = blued;
IonSystems 11:06f6e82b40a8 60 //colourDataAcquired = true;
IonSystems 5:644bca33c1ca 61 lcd->cls(); // clear display
IonSystems 5:644bca33c1ca 62 lcd->locate(0,0);
IonSystems 11:06f6e82b40a8 63 lcd->printf("R:%dG:%dB:%d",red,green,blue);
IonSystems 5:644bca33c1ca 64
IonSystems 5:644bca33c1ca 65 lcd->locate(1,0);
IonSystems 11:06f6e82b40a8 66 /*if(red > 55){
IonSystems 5:644bca33c1ca 67 lcd->printf("RED");
IonSystems 5:644bca33c1ca 68 }
IonSystems 5:644bca33c1ca 69 if(green > 55){
IonSystems 5:644bca33c1ca 70 lcd->printf("GREEN");
IonSystems 5:644bca33c1ca 71 }
IonSystems 5:644bca33c1ca 72 if(red < 30 && green > 30 && blue > 30){
IonSystems 5:644bca33c1ca 73 lcd->printf("BLUE");
IonSystems 11:06f6e82b40a8 74 }*/
IonSystems 5:644bca33c1ca 75
IonSystems 13:0661d658d9d1 76 bool redWithinThreshold[4] = {0,0,0,0};
IonSystems 13:0661d658d9d1 77 bool greenWithinThreshold[4]= {0,0,0,0};
IonSystems 13:0661d658d9d1 78 bool blueWithinThreshold[4]= {0,0,0,0};
IonSystems 5:644bca33c1ca 79 //Set red Thresholds
IonSystems 5:644bca33c1ca 80 redWithinThreshold[0] = (red >= redLT[0]) && (red <= redUT[0]);
IonSystems 5:644bca33c1ca 81 greenWithinThreshold[0] = (green >= redLT[1]) && (green <= redUT[1]);
IonSystems 5:644bca33c1ca 82 blueWithinThreshold[0] = (blue >= redLT[2]) && (blue <= redUT[2]);
IonSystems 5:644bca33c1ca 83 //Set green Thresholds
IonSystems 5:644bca33c1ca 84 redWithinThreshold[1] = (red >= greenLT[0]) && (red <= greenUT[0]);
IonSystems 5:644bca33c1ca 85 greenWithinThreshold[1] = (green >= greenLT[1]) && (green <= greenUT[1]);
IonSystems 5:644bca33c1ca 86 blueWithinThreshold[1] = (blue >= greenLT[2]) && (blue <= greenUT[2]);
IonSystems 5:644bca33c1ca 87 //Set blue Thresholds
IonSystems 5:644bca33c1ca 88 redWithinThreshold[2] = (red >= blueLT[0]) && (red <= blueUT[0]);
IonSystems 5:644bca33c1ca 89 greenWithinThreshold[2] = (green >= blueLT[1]) && (green <= blueUT[1]);
IonSystems 5:644bca33c1ca 90 blueWithinThreshold[2] = blue >= blueLT[2] && blue <= blueUT[2];
IonSystems 13:0661d658d9d1 91 //Set none Thresholds
IonSystems 13:0661d658d9d1 92 redWithinThreshold[3] = (red >= noneLT[0]) && (red <= noneUT[0]);
IonSystems 13:0661d658d9d1 93 greenWithinThreshold[3] = (green >= noneLT[1]) && (green <= noneUT[1]);
IonSystems 13:0661d658d9d1 94 blueWithinThreshold[3] = blue >= noneLT[2] && blue <= noneUT[2];
IonSystems 5:644bca33c1ca 95
IonSystems 5:644bca33c1ca 96 if(redWithinThreshold[0] && greenWithinThreshold[0] && blueWithinThreshold[0]){
IonSystems 5:644bca33c1ca 97 lcd->printf("RED");
IonSystems 13:0661d658d9d1 98 return RED;
IonSystems 5:644bca33c1ca 99 }
IonSystems 5:644bca33c1ca 100 else if(redWithinThreshold[1] && greenWithinThreshold[1] && blueWithinThreshold[1]){
IonSystems 5:644bca33c1ca 101 lcd->printf("GREEN");
IonSystems 13:0661d658d9d1 102 return GREEN;
IonSystems 5:644bca33c1ca 103 }
IonSystems 5:644bca33c1ca 104 else if(redWithinThreshold[2] && greenWithinThreshold[2] && blueWithinThreshold[2]){
IonSystems 5:644bca33c1ca 105 lcd->printf("BLUE");
IonSystems 13:0661d658d9d1 106 return BLUE;
IonSystems 13:0661d658d9d1 107 }
IonSystems 13:0661d658d9d1 108 else if(redWithinThreshold[3] && greenWithinThreshold[3] && blueWithinThreshold[3]){
IonSystems 13:0661d658d9d1 109 lcd->printf("NONE");
IonSystems 13:0661d658d9d1 110 return NONE;
IonSystems 5:644bca33c1ca 111 }
IonSystems 5:644bca33c1ca 112 else{
IonSystems 5:644bca33c1ca 113 lcd->printf("BIN");
IonSystems 13:0661d658d9d1 114 return BIN;
IonSystems 5:644bca33c1ca 115 }
IonSystems 13:0661d658d9d1 116
IonSystems 5:644bca33c1ca 117 }
IonSystems 5:644bca33c1ca 118
IonSystems 13:0661d658d9d1 119
IonSystems 13:0661d658d9d1 120