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:
Thu Nov 20 11:15:47 2014 +0000
Revision:
13:0661d658d9d1
Parent:
12:814a8fdbb6f7
Child:
14:31ba3e56c788
Maintenance mode added, still need to do the printouts to the lcd.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IonSystems 6:e64796f1f384 1 #include "mbed.h"
IonSystems 11:06f6e82b40a8 2 //#include "Colour.h"
IonSystems 11:06f6e82b40a8 3 #include "colourProcessing.h"
IonSystems 6:e64796f1f384 4 //Setup pins to FPGA
IonSystems 6:e64796f1f384 5 DigitalOut startSort(p5); //A positive edge tells the FPGA to start sorting.
IonSystems 10:8c0696b99692 6 DigitalIn sortComplete(p16);
IonSystems 6:e64796f1f384 7 /*
IonSystems 6:e64796f1f384 8 The command codes for the three sort select bits(ColourBit1 - ColourBit2)
IonSystems 6:e64796f1f384 9 000 Sort red chip
IonSystems 6:e64796f1f384 10 001 Sort green chip
IonSystems 6:e64796f1f384 11 010 Sort blue chip
IonSystems 6:e64796f1f384 12 011 Bin the chip
IonSystems 6:e64796f1f384 13 100 Recycle the chip
IonSystems 6:e64796f1f384 14 101 Nothing
IonSystems 6:e64796f1f384 15 110 Nothing
IonSystems 6:e64796f1f384 16 111 Nothing
IonSystems 6:e64796f1f384 17 */
IonSystems 6:e64796f1f384 18
IonSystems 6:e64796f1f384 19 DigitalOut colourBit1(p6); //The 3 bits below are select bits for the sorter.
IonSystems 6:e64796f1f384 20 DigitalOut colourBit2(p7);
IonSystems 6:e64796f1f384 21 DigitalOut colourBit3(p8);
IonSystems 9:8d78eb55ad5e 22 DigitalOut startDispense(p18); //A positive edge tells the FPGA to start dispensing. p9
IonSystems 6:e64796f1f384 23 /*
IonSystems 6:e64796f1f384 24 00 Dispense red chip
IonSystems 6:e64796f1f384 25 01 Dispense green chip
IonSystems 6:e64796f1f384 26 10 Dispense blue chip
IonSystems 6:e64796f1f384 27 11 Nothing
IonSystems 6:e64796f1f384 28 */
IonSystems 6:e64796f1f384 29
IonSystems 9:8d78eb55ad5e 30 DigitalOut dispenseBit1(p19); //The 2 bits below are select bits for the dispenser.p10
IonSystems 9:8d78eb55ad5e 31 DigitalOut dispenseBit2(p20); //p11
IonSystems 8:b7771391adc9 32
IonSystems 12:814a8fdbb6f7 33 DigitalIn dispenseComplete(p11); //FPGA sets to 1 once complete until another signal is recieved.
IonSystems 10:8c0696b99692 34 DigitalOut select(p15); //0 for control, 1 for maintenance.
IonSystems 10:8c0696b99692 35
IonSystems 12:814a8fdbb6f7 36
IonSystems 12:814a8fdbb6f7 37 void setMaintenanceSelect(bool bit6, bool bit5, bool bit4, bool bit3, bool bit2, bool bit1){
IonSystems 12:814a8fdbb6f7 38 startSort = bit1;
IonSystems 12:814a8fdbb6f7 39 colourBit1 = bit2;
IonSystems 12:814a8fdbb6f7 40 colourBit2 = bit3;
IonSystems 12:814a8fdbb6f7 41 colourBit3 = bit4;
IonSystems 12:814a8fdbb6f7 42 startDispense = bit5;
IonSystems 12:814a8fdbb6f7 43 dispenseBit1 = bit6;
IonSystems 12:814a8fdbb6f7 44 }
IonSystems 12:814a8fdbb6f7 45
IonSystems 12:814a8fdbb6f7 46 void setMaintenanceStart(bool value){
IonSystems 12:814a8fdbb6f7 47 dispenseBit2 = value;
IonSystems 12:814a8fdbb6f7 48 }
IonSystems 12:814a8fdbb6f7 49 void waitForMaintenanceComplete(){
IonSystems 12:814a8fdbb6f7 50 while(!sortComplete){
IonSystems 12:814a8fdbb6f7 51 wait(0.1);
IonSystems 12:814a8fdbb6f7 52 }
IonSystems 12:814a8fdbb6f7 53 }
IonSystems 12:814a8fdbb6f7 54 void maintain(StateMachine maintainState){
IonSystems 12:814a8fdbb6f7 55 select = 1; //setting FPGA to maintenance mode.
IonSystems 12:814a8fdbb6f7 56 setMaintenanceSelect(true,true,true,true,true,true);
IonSystems 12:814a8fdbb6f7 57 switch(maintainState){
IonSystems 12:814a8fdbb6f7 58 case RB_LEFT:
IonSystems 12:814a8fdbb6f7 59 setMaintenanceSelect(false,false,false,true,false,true);
IonSystems 12:814a8fdbb6f7 60 break;
IonSystems 12:814a8fdbb6f7 61 case RB_CENTRE:
IonSystems 12:814a8fdbb6f7 62 setMaintenanceSelect(false,false,false,true,true,false);
IonSystems 12:814a8fdbb6f7 63 break;
IonSystems 12:814a8fdbb6f7 64 case RB_RIGHT:
IonSystems 12:814a8fdbb6f7 65 setMaintenanceSelect(false,false,false,true,true,true);
IonSystems 12:814a8fdbb6f7 66 break;
IonSystems 12:814a8fdbb6f7 67 case GO_UP:
IonSystems 12:814a8fdbb6f7 68 setMaintenanceSelect(false,false,true,false,false,false);
IonSystems 12:814a8fdbb6f7 69 break;
IonSystems 12:814a8fdbb6f7 70 case GO_CENTRE:
IonSystems 12:814a8fdbb6f7 71 setMaintenanceSelect(false,false,true,false,false,true);
IonSystems 12:814a8fdbb6f7 72 break;
IonSystems 12:814a8fdbb6f7 73 case GO_DOWN:
IonSystems 12:814a8fdbb6f7 74 setMaintenanceSelect(false,false,true,false,true,false);
IonSystems 12:814a8fdbb6f7 75 break;
IonSystems 12:814a8fdbb6f7 76 case BR_LEFT:
IonSystems 12:814a8fdbb6f7 77 setMaintenanceSelect(false,false,true,false,true,true);
IonSystems 12:814a8fdbb6f7 78 break;
IonSystems 12:814a8fdbb6f7 79 case BR_RIGHT:
IonSystems 12:814a8fdbb6f7 80 setMaintenanceSelect(false,false,true,true,false,false);
IonSystems 12:814a8fdbb6f7 81 break;
IonSystems 12:814a8fdbb6f7 82 case R_PUSH:
IonSystems 12:814a8fdbb6f7 83 setMaintenanceSelect(false,false,true,true,false,true);
IonSystems 12:814a8fdbb6f7 84 break;
IonSystems 12:814a8fdbb6f7 85 }
IonSystems 12:814a8fdbb6f7 86 setMaintenanceStart(true);//Bit 7
IonSystems 12:814a8fdbb6f7 87 waitForMaintenanceComplete();
IonSystems 12:814a8fdbb6f7 88 setMaintenanceStart(false);
IonSystems 12:814a8fdbb6f7 89 }
IonSystems 12:814a8fdbb6f7 90
IonSystems 12:814a8fdbb6f7 91
IonSystems 10:8c0696b99692 92 void dispense(Colour colour){
IonSystems 13:0661d658d9d1 93
IonSystems 12:814a8fdbb6f7 94 bool validDispense = false;
IonSystems 10:8c0696b99692 95 select = 0; //Setting to operation mode just in case it has not been set.
IonSystems 10:8c0696b99692 96 dispenseBit1 = true;
IonSystems 10:8c0696b99692 97 dispenseBit2 = true;
IonSystems 10:8c0696b99692 98 switch(colour){
IonSystems 10:8c0696b99692 99 case RED:
IonSystems 12:814a8fdbb6f7 100 if(redAmount > 0 || operationMode == false){
IonSystems 12:814a8fdbb6f7 101 log("RED");
IonSystems 12:814a8fdbb6f7 102 log("Setting dispense bits to 00");
IonSystems 12:814a8fdbb6f7 103 dispenseBit1 = false;
IonSystems 12:814a8fdbb6f7 104 dispenseBit2 = false;
IonSystems 12:814a8fdbb6f7 105 redAmount--;
IonSystems 12:814a8fdbb6f7 106 validDispense = true;
IonSystems 12:814a8fdbb6f7 107 }
IonSystems 10:8c0696b99692 108 break;
IonSystems 10:8c0696b99692 109 case GREEN:
IonSystems 12:814a8fdbb6f7 110 if(greenAmount > 0 || operationMode == false){
IonSystems 12:814a8fdbb6f7 111 log("GREEN");
IonSystems 12:814a8fdbb6f7 112 log("Setting dispense bits to 10");
IonSystems 12:814a8fdbb6f7 113 dispenseBit1 = true;
IonSystems 12:814a8fdbb6f7 114 dispenseBit2 = false;
IonSystems 12:814a8fdbb6f7 115 greenAmount--;
IonSystems 12:814a8fdbb6f7 116 validDispense = true;
IonSystems 12:814a8fdbb6f7 117 }
IonSystems 10:8c0696b99692 118 break;
IonSystems 10:8c0696b99692 119 case BLUE:
IonSystems 12:814a8fdbb6f7 120 if(blueAmount > 0 || operationMode == false){
IonSystems 10:8c0696b99692 121 log("BLUE");
IonSystems 10:8c0696b99692 122 log("Setting dispense bits to 01");
IonSystems 10:8c0696b99692 123 dispenseBit1 = false;
IonSystems 10:8c0696b99692 124 dispenseBit2 = true;
IonSystems 12:814a8fdbb6f7 125 blueAmount--;
IonSystems 12:814a8fdbb6f7 126 validDispense = true;
IonSystems 12:814a8fdbb6f7 127 }
IonSystems 10:8c0696b99692 128 break;
IonSystems 12:814a8fdbb6f7 129 writeFile(redAmount,greenAmount,blueAmount);
IonSystems 10:8c0696b99692 130 }
IonSystems 10:8c0696b99692 131
IonSystems 12:814a8fdbb6f7 132 if(validDispense || operationMode == false){
IonSystems 10:8c0696b99692 133 log("Setting start dispense to true");
IonSystems 10:8c0696b99692 134 startDispense = true; //set the startDispense line high.
IonSystems 10:8c0696b99692 135
IonSystems 10:8c0696b99692 136
IonSystems 10:8c0696b99692 137 //wait(1); //temporary delay
IonSystems 10:8c0696b99692 138 while(!dispenseComplete){
IonSystems 11:06f6e82b40a8 139 wait(0.2);
IonSystems 10:8c0696b99692 140 log("Waiting for dispense complete");
IonSystems 10:8c0696b99692 141 printLCD("Waiting for dispense complete");
IonSystems 10:8c0696b99692 142 }
IonSystems 10:8c0696b99692 143
IonSystems 12:814a8fdbb6f7 144 wait(0.6);
IonSystems 10:8c0696b99692 145 log("Complete");
IonSystems 10:8c0696b99692 146 log("Setting start dispense to false");
IonSystems 10:8c0696b99692 147 startDispense = false;
IonSystems 10:8c0696b99692 148
IonSystems 10:8c0696b99692 149 log("Resetting dispense bits to 11");
IonSystems 10:8c0696b99692 150 dispenseBit1 = true; //reset the dispense select to do nothing
IonSystems 10:8c0696b99692 151 dispenseBit2 = true; //reset the dispense select to do nothing
IonSystems 10:8c0696b99692 152
IonSystems 10:8c0696b99692 153
IonSystems 10:8c0696b99692 154
IonSystems 10:8c0696b99692 155 printLCD("DISPENSE COMPLETE");
IonSystems 13:0661d658d9d1 156
IonSystems 12:814a8fdbb6f7 157 }
IonSystems 10:8c0696b99692 158 }
IonSystems 10:8c0696b99692 159
IonSystems 10:8c0696b99692 160 void sort(Colour colour){
IonSystems 13:0661d658d9d1 161 if(colour == NONE){
IonSystems 13:0661d658d9d1 162 return;
IonSystems 13:0661d658d9d1 163 }else{
IonSystems 10:8c0696b99692 164 select = 0; //Setting to operation mode just in case it has not been set.
IonSystems 10:8c0696b99692 165 colourBit1 = true;
IonSystems 10:8c0696b99692 166 colourBit2 = true;
IonSystems 10:8c0696b99692 167 colourBit3 = true;
IonSystems 10:8c0696b99692 168 switch(colour){
IonSystems 10:8c0696b99692 169 case RED:
IonSystems 10:8c0696b99692 170 log("Sort RED");
IonSystems 10:8c0696b99692 171 log("Setting sort bits to 000");
IonSystems 10:8c0696b99692 172 colourBit1 = false;
IonSystems 10:8c0696b99692 173 colourBit2 = false;
IonSystems 10:8c0696b99692 174 colourBit3 = false;
IonSystems 12:814a8fdbb6f7 175 redAmount++;
IonSystems 10:8c0696b99692 176 break;
IonSystems 10:8c0696b99692 177 case GREEN:
IonSystems 10:8c0696b99692 178 log("Sort GREEN");
IonSystems 10:8c0696b99692 179 log("Setting sort bits to 001");
IonSystems 10:8c0696b99692 180 colourBit1 = true;
IonSystems 10:8c0696b99692 181 colourBit2 = false;
IonSystems 10:8c0696b99692 182 colourBit3 = false;
IonSystems 12:814a8fdbb6f7 183 greenAmount++;
IonSystems 10:8c0696b99692 184 break;
IonSystems 10:8c0696b99692 185 case BLUE:
IonSystems 10:8c0696b99692 186 log("Sort BLUE");
IonSystems 10:8c0696b99692 187 log("Setting sort bits to 010");
IonSystems 10:8c0696b99692 188 colourBit1 = false;
IonSystems 10:8c0696b99692 189 colourBit2 = true;
IonSystems 10:8c0696b99692 190 colourBit3 = false;
IonSystems 12:814a8fdbb6f7 191 blueAmount++;
IonSystems 10:8c0696b99692 192 break;
IonSystems 11:06f6e82b40a8 193 case BIN:
IonSystems 11:06f6e82b40a8 194 log("Sort BIN");
IonSystems 11:06f6e82b40a8 195 log("Setting sort bits to 011");
IonSystems 11:06f6e82b40a8 196 colourBit1 = true;
IonSystems 11:06f6e82b40a8 197 colourBit2 = true;
IonSystems 11:06f6e82b40a8 198 colourBit3 = false;
IonSystems 11:06f6e82b40a8 199 break;
IonSystems 11:06f6e82b40a8 200 case RECYCLE:
IonSystems 11:06f6e82b40a8 201 log("Recycle");
IonSystems 11:06f6e82b40a8 202 log("Setting sort bits to 100");
IonSystems 11:06f6e82b40a8 203 colourBit1 = false;
IonSystems 11:06f6e82b40a8 204 colourBit2 = false;
IonSystems 11:06f6e82b40a8 205 colourBit3 = true;
IonSystems 11:06f6e82b40a8 206 break;
IonSystems 10:8c0696b99692 207 }
IonSystems 10:8c0696b99692 208
IonSystems 10:8c0696b99692 209
IonSystems 10:8c0696b99692 210 log("Setting start sort to true");
IonSystems 10:8c0696b99692 211 startSort = true; //set the startDispense line high.
IonSystems 10:8c0696b99692 212
IonSystems 10:8c0696b99692 213
IonSystems 10:8c0696b99692 214 //wait(1); //temporary delay
IonSystems 10:8c0696b99692 215 while(!sortComplete){
IonSystems 11:06f6e82b40a8 216 wait(0.5);
IonSystems 10:8c0696b99692 217 log("Waiting for sort complete");
IonSystems 10:8c0696b99692 218 printLCD("Waiting for sort complete");
IonSystems 10:8c0696b99692 219 }
IonSystems 10:8c0696b99692 220
IonSystems 10:8c0696b99692 221
IonSystems 10:8c0696b99692 222 log("Complete");
IonSystems 12:814a8fdbb6f7 223 wait(0.6);
IonSystems 10:8c0696b99692 224 log("Setting start sort to false");
IonSystems 11:06f6e82b40a8 225 startSort = false;
IonSystems 10:8c0696b99692 226
IonSystems 10:8c0696b99692 227 log("Resetting dispense bits to 11");
IonSystems 10:8c0696b99692 228 colourBit1 = true; //reset the dispense select to do nothing
IonSystems 10:8c0696b99692 229 colourBit2 = true; //reset the dispense select to do nothing
IonSystems 10:8c0696b99692 230 colourBit3 = true;
IonSystems 10:8c0696b99692 231 printLCD("SORT COMPLETE");
IonSystems 12:814a8fdbb6f7 232 writeFile(redAmount,greenAmount,blueAmount);
IonSystems 13:0661d658d9d1 233 }
IonSystems 12:814a8fdbb6f7 234 }
IonSystems 12:814a8fdbb6f7 235
IonSystems 12:814a8fdbb6f7 236 void dispenseAll(){
IonSystems 12:814a8fdbb6f7 237 int red = redAmount;
IonSystems 12:814a8fdbb6f7 238 int green = greenAmount;
IonSystems 12:814a8fdbb6f7 239 int blue = blueAmount;
IonSystems 12:814a8fdbb6f7 240 for(int r = 0;r < red;r++){
IonSystems 12:814a8fdbb6f7 241 dispense(RED);
IonSystems 12:814a8fdbb6f7 242 wait(0.5);
IonSystems 12:814a8fdbb6f7 243 }redAmount = 0;
IonSystems 12:814a8fdbb6f7 244
IonSystems 12:814a8fdbb6f7 245 for(int g = 0;g < green;g++){
IonSystems 12:814a8fdbb6f7 246 dispense(GREEN);
IonSystems 12:814a8fdbb6f7 247 wait(0.5);
IonSystems 12:814a8fdbb6f7 248 }greenAmount = 0;
IonSystems 12:814a8fdbb6f7 249
IonSystems 12:814a8fdbb6f7 250 for(int b = 0;b < blue;b++){
IonSystems 12:814a8fdbb6f7 251 dispense(BLUE);
IonSystems 12:814a8fdbb6f7 252 wait(0.5);
IonSystems 12:814a8fdbb6f7 253 }blueAmount = 0;
IonSystems 12:814a8fdbb6f7 254 writeFile(redAmount,greenAmount,blueAmount);
IonSystems 12:814a8fdbb6f7 255 }