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:
Mon Nov 24 14:00:46 2014 +0000
Revision:
17:6a0bb0ad5bb4
Parent:
15:0c5f20e15b6a
Child:
22:8f11d1c178ab
fixed some bugs, great comment i know

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 17:6a0bb0ad5bb4 7 int rgb_readings[100][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 17:6a0bb0ad5bb4 12 int tubeSize = 33;
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 17:6a0bb0ad5bb4 23 int redLT [3] = {308,84,162};
IonSystems 17:6a0bb0ad5bb4 24 int redUT [3]= {352,124,204};
IonSystems 17:6a0bb0ad5bb4 25 int greenLT [3] = {91,180,142};
IonSystems 17:6a0bb0ad5bb4 26 int greenUT [3]= {132,220,184};
IonSystems 17:6a0bb0ad5bb4 27 int blueLT [3]= {79,95,117};
IonSystems 17:6a0bb0ad5bb4 28 int blueUT [3]= {117,139,157};
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 17:6a0bb0ad5bb4 38 wait(0.1);
IonSystems 14:31ba3e56c788 39 lcd->cls(); // clear display
IonSystems 14:31ba3e56c788 40 lcd->locate(0,0);
IonSystems 14:31ba3e56c788 41 lcd->printf("Reading Sensor");
IonSystems 17:6a0bb0ad5bb4 42 for(int i = 0; i < 100; i++){
IonSystems 14:31ba3e56c788 43 rgb_sensor.getAllColors(rgb_readings[i]);
IonSystems 17:6a0bb0ad5bb4 44 wait(0.001);
IonSystems 14:31ba3e56c788 45 }
IonSystems 17:6a0bb0ad5bb4 46 for(int i = 0; i < 100; i++){
IonSystems 14:31ba3e56c788 47 for(int j = 0; j < 4; j++){
IonSystems 14:31ba3e56c788 48 rgb_average[j] += rgb_readings[i][j];
IonSystems 14:31ba3e56c788 49 }
IonSystems 14:31ba3e56c788 50 }
IonSystems 14:31ba3e56c788 51 for(int i = 0; i < 4; i++){
IonSystems 17:6a0bb0ad5bb4 52 rgb_average[i] = rgb_average[i] / 100;
IonSystems 14:31ba3e56c788 53 }
IonSystems 14:31ba3e56c788 54 double redd = (rgb_average[1] /gMax) * 255;
IonSystems 14:31ba3e56c788 55 double greend = (rgb_average[2] /bMax) * 255;
IonSystems 14:31ba3e56c788 56 double blued = (rgb_average[0] /rMax) * 255;
IonSystems 5:644bca33c1ca 57
IonSystems 5:644bca33c1ca 58 int red = redd;
IonSystems 5:644bca33c1ca 59 int green = greend;
IonSystems 5:644bca33c1ca 60 int blue = blued;
IonSystems 11:06f6e82b40a8 61 //colourDataAcquired = true;
IonSystems 5:644bca33c1ca 62 lcd->cls(); // clear display
IonSystems 5:644bca33c1ca 63 lcd->locate(0,0);
IonSystems 11:06f6e82b40a8 64 lcd->printf("R:%dG:%dB:%d",red,green,blue);
IonSystems 5:644bca33c1ca 65
IonSystems 5:644bca33c1ca 66 lcd->locate(1,0);
IonSystems 11:06f6e82b40a8 67 /*if(red > 55){
IonSystems 5:644bca33c1ca 68 lcd->printf("RED");
IonSystems 5:644bca33c1ca 69 }
IonSystems 5:644bca33c1ca 70 if(green > 55){
IonSystems 5:644bca33c1ca 71 lcd->printf("GREEN");
IonSystems 5:644bca33c1ca 72 }
IonSystems 5:644bca33c1ca 73 if(red < 30 && green > 30 && blue > 30){
IonSystems 5:644bca33c1ca 74 lcd->printf("BLUE");
IonSystems 11:06f6e82b40a8 75 }*/
IonSystems 5:644bca33c1ca 76
IonSystems 13:0661d658d9d1 77 bool redWithinThreshold[4] = {0,0,0,0};
IonSystems 13:0661d658d9d1 78 bool greenWithinThreshold[4]= {0,0,0,0};
IonSystems 13:0661d658d9d1 79 bool blueWithinThreshold[4]= {0,0,0,0};
IonSystems 5:644bca33c1ca 80 //Set red Thresholds
IonSystems 5:644bca33c1ca 81 redWithinThreshold[0] = (red >= redLT[0]) && (red <= redUT[0]);
IonSystems 5:644bca33c1ca 82 greenWithinThreshold[0] = (green >= redLT[1]) && (green <= redUT[1]);
IonSystems 5:644bca33c1ca 83 blueWithinThreshold[0] = (blue >= redLT[2]) && (blue <= redUT[2]);
IonSystems 5:644bca33c1ca 84 //Set green Thresholds
IonSystems 5:644bca33c1ca 85 redWithinThreshold[1] = (red >= greenLT[0]) && (red <= greenUT[0]);
IonSystems 5:644bca33c1ca 86 greenWithinThreshold[1] = (green >= greenLT[1]) && (green <= greenUT[1]);
IonSystems 5:644bca33c1ca 87 blueWithinThreshold[1] = (blue >= greenLT[2]) && (blue <= greenUT[2]);
IonSystems 5:644bca33c1ca 88 //Set blue Thresholds
IonSystems 5:644bca33c1ca 89 redWithinThreshold[2] = (red >= blueLT[0]) && (red <= blueUT[0]);
IonSystems 5:644bca33c1ca 90 greenWithinThreshold[2] = (green >= blueLT[1]) && (green <= blueUT[1]);
IonSystems 5:644bca33c1ca 91 blueWithinThreshold[2] = blue >= blueLT[2] && blue <= blueUT[2];
IonSystems 13:0661d658d9d1 92 //Set none Thresholds
IonSystems 13:0661d658d9d1 93 redWithinThreshold[3] = (red >= noneLT[0]) && (red <= noneUT[0]);
IonSystems 13:0661d658d9d1 94 greenWithinThreshold[3] = (green >= noneLT[1]) && (green <= noneUT[1]);
IonSystems 13:0661d658d9d1 95 blueWithinThreshold[3] = blue >= noneLT[2] && blue <= noneUT[2];
IonSystems 5:644bca33c1ca 96
IonSystems 5:644bca33c1ca 97 if(redWithinThreshold[0] && greenWithinThreshold[0] && blueWithinThreshold[0]){
IonSystems 5:644bca33c1ca 98 lcd->printf("RED");
IonSystems 13:0661d658d9d1 99 return RED;
IonSystems 5:644bca33c1ca 100 }
IonSystems 5:644bca33c1ca 101 else if(redWithinThreshold[1] && greenWithinThreshold[1] && blueWithinThreshold[1]){
IonSystems 5:644bca33c1ca 102 lcd->printf("GREEN");
IonSystems 13:0661d658d9d1 103 return GREEN;
IonSystems 5:644bca33c1ca 104 }
IonSystems 5:644bca33c1ca 105 else if(redWithinThreshold[2] && greenWithinThreshold[2] && blueWithinThreshold[2]){
IonSystems 5:644bca33c1ca 106 lcd->printf("BLUE");
IonSystems 13:0661d658d9d1 107 return BLUE;
IonSystems 13:0661d658d9d1 108 }
IonSystems 13:0661d658d9d1 109 else if(redWithinThreshold[3] && greenWithinThreshold[3] && blueWithinThreshold[3]){
IonSystems 13:0661d658d9d1 110 lcd->printf("NONE");
IonSystems 13:0661d658d9d1 111 return NONE;
IonSystems 5:644bca33c1ca 112 }
IonSystems 5:644bca33c1ca 113 else{
IonSystems 5:644bca33c1ca 114 lcd->printf("BIN");
IonSystems 13:0661d658d9d1 115 return BIN;
IonSystems 5:644bca33c1ca 116 }
IonSystems 13:0661d658d9d1 117
IonSystems 5:644bca33c1ca 118 }
IonSystems 5:644bca33c1ca 119
IonSystems 13:0661d658d9d1 120
IonSystems 13:0661d658d9d1 121