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:
19:78d4b78fa736
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 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 17:6a0bb0ad5bb4 9 See datasheet for furter information
IonSystems 6:e64796f1f384 10 000 Sort red chip
IonSystems 6:e64796f1f384 11 001 Sort green chip
IonSystems 6:e64796f1f384 12 010 Sort blue chip
IonSystems 6:e64796f1f384 13 011 Bin the chip
IonSystems 6:e64796f1f384 14 100 Recycle the chip
IonSystems 6:e64796f1f384 15 101 Nothing
IonSystems 6:e64796f1f384 16 110 Nothing
IonSystems 6:e64796f1f384 17 111 Nothing
IonSystems 6:e64796f1f384 18 */
IonSystems 6:e64796f1f384 19
IonSystems 6:e64796f1f384 20 DigitalOut colourBit1(p6); //The 3 bits below are select bits for the sorter.
IonSystems 6:e64796f1f384 21 DigitalOut colourBit2(p7);
IonSystems 6:e64796f1f384 22 DigitalOut colourBit3(p8);
IonSystems 9:8d78eb55ad5e 23 DigitalOut startDispense(p18); //A positive edge tells the FPGA to start dispensing. p9
IonSystems 6:e64796f1f384 24 /*
IonSystems 6:e64796f1f384 25 00 Dispense red chip
IonSystems 6:e64796f1f384 26 01 Dispense green chip
IonSystems 6:e64796f1f384 27 10 Dispense blue chip
IonSystems 6:e64796f1f384 28 11 Nothing
IonSystems 6:e64796f1f384 29 */
IonSystems 6:e64796f1f384 30
IonSystems 9:8d78eb55ad5e 31 DigitalOut dispenseBit1(p19); //The 2 bits below are select bits for the dispenser.p10
IonSystems 9:8d78eb55ad5e 32 DigitalOut dispenseBit2(p20); //p11
IonSystems 8:b7771391adc9 33
IonSystems 12:814a8fdbb6f7 34 DigitalIn dispenseComplete(p11); //FPGA sets to 1 once complete until another signal is recieved.
IonSystems 10:8c0696b99692 35 DigitalOut select(p15); //0 for control, 1 for maintenance.
IonSystems 10:8c0696b99692 36
IonSystems 12:814a8fdbb6f7 37
IonSystems 12:814a8fdbb6f7 38 void setMaintenanceSelect(bool bit6, bool bit5, bool bit4, bool bit3, bool bit2, bool bit1){
IonSystems 12:814a8fdbb6f7 39 startSort = bit1;
IonSystems 12:814a8fdbb6f7 40 colourBit1 = bit2;
IonSystems 12:814a8fdbb6f7 41 colourBit2 = bit3;
IonSystems 12:814a8fdbb6f7 42 colourBit3 = bit4;
IonSystems 12:814a8fdbb6f7 43 startDispense = bit5;
IonSystems 12:814a8fdbb6f7 44 dispenseBit1 = bit6;
IonSystems 12:814a8fdbb6f7 45 }
IonSystems 12:814a8fdbb6f7 46
IonSystems 12:814a8fdbb6f7 47 void setMaintenanceStart(bool value){
IonSystems 12:814a8fdbb6f7 48 dispenseBit2 = value;
IonSystems 12:814a8fdbb6f7 49 }
IonSystems 12:814a8fdbb6f7 50 void waitForMaintenanceComplete(){
IonSystems 12:814a8fdbb6f7 51 while(!sortComplete){
IonSystems 14:31ba3e56c788 52 printLCD("Waiting for REC");
IonSystems 12:814a8fdbb6f7 53 wait(0.1);
IonSystems 12:814a8fdbb6f7 54 }
IonSystems 12:814a8fdbb6f7 55 }
IonSystems 12:814a8fdbb6f7 56 void maintain(StateMachine maintainState){
IonSystems 12:814a8fdbb6f7 57 select = 1; //setting FPGA to maintenance mode.
IonSystems 12:814a8fdbb6f7 58 setMaintenanceSelect(true,true,true,true,true,true);
IonSystems 12:814a8fdbb6f7 59 switch(maintainState){
IonSystems 12:814a8fdbb6f7 60 case RB_LEFT:
IonSystems 12:814a8fdbb6f7 61 setMaintenanceSelect(false,false,false,true,false,true);
IonSystems 12:814a8fdbb6f7 62 break;
IonSystems 12:814a8fdbb6f7 63 case RB_CENTRE:
IonSystems 12:814a8fdbb6f7 64 setMaintenanceSelect(false,false,false,true,true,false);
IonSystems 12:814a8fdbb6f7 65 break;
IonSystems 12:814a8fdbb6f7 66 case RB_RIGHT:
IonSystems 12:814a8fdbb6f7 67 setMaintenanceSelect(false,false,false,true,true,true);
IonSystems 12:814a8fdbb6f7 68 break;
IonSystems 12:814a8fdbb6f7 69 case GO_UP:
IonSystems 12:814a8fdbb6f7 70 setMaintenanceSelect(false,false,true,false,false,false);
IonSystems 12:814a8fdbb6f7 71 break;
IonSystems 12:814a8fdbb6f7 72 case GO_CENTRE:
IonSystems 12:814a8fdbb6f7 73 setMaintenanceSelect(false,false,true,false,false,true);
IonSystems 12:814a8fdbb6f7 74 break;
IonSystems 12:814a8fdbb6f7 75 case GO_DOWN:
IonSystems 12:814a8fdbb6f7 76 setMaintenanceSelect(false,false,true,false,true,false);
IonSystems 12:814a8fdbb6f7 77 break;
IonSystems 12:814a8fdbb6f7 78 case BR_LEFT:
IonSystems 12:814a8fdbb6f7 79 setMaintenanceSelect(false,false,true,false,true,true);
IonSystems 12:814a8fdbb6f7 80 break;
IonSystems 12:814a8fdbb6f7 81 case BR_RIGHT:
IonSystems 12:814a8fdbb6f7 82 setMaintenanceSelect(false,false,true,true,false,false);
IonSystems 12:814a8fdbb6f7 83 break;
IonSystems 12:814a8fdbb6f7 84 case R_PUSH:
IonSystems 12:814a8fdbb6f7 85 setMaintenanceSelect(false,false,true,true,false,true);
IonSystems 12:814a8fdbb6f7 86 break;
IonSystems 14:31ba3e56c788 87 default:
IonSystems 14:31ba3e56c788 88 log("Incorrect maintenance command recieved");
IonSystems 14:31ba3e56c788 89 printLCD("Incorrect maintenance command recieved");
IonSystems 14:31ba3e56c788 90 break;
IonSystems 12:814a8fdbb6f7 91 }
IonSystems 12:814a8fdbb6f7 92 setMaintenanceStart(true);//Bit 7
IonSystems 12:814a8fdbb6f7 93 waitForMaintenanceComplete();
IonSystems 15:0c5f20e15b6a 94 wait(0.5);
IonSystems 12:814a8fdbb6f7 95 setMaintenanceStart(false);
IonSystems 12:814a8fdbb6f7 96 }
IonSystems 12:814a8fdbb6f7 97
IonSystems 12:814a8fdbb6f7 98
IonSystems 10:8c0696b99692 99 void dispense(Colour colour){
IonSystems 13:0661d658d9d1 100
IonSystems 12:814a8fdbb6f7 101 bool validDispense = false;
IonSystems 10:8c0696b99692 102 select = 0; //Setting to operation mode just in case it has not been set.
IonSystems 10:8c0696b99692 103 dispenseBit1 = true;
IonSystems 10:8c0696b99692 104 dispenseBit2 = true;
IonSystems 14:31ba3e56c788 105 /* A dispense operation can only take place if there are more than zero chips
IonSystems 14:31ba3e56c788 106 */
IonSystems 10:8c0696b99692 107 switch(colour){
IonSystems 10:8c0696b99692 108 case RED:
IonSystems 14:31ba3e56c788 109 if(redAmount > 0 || operationMode == false){
IonSystems 12:814a8fdbb6f7 110 log("RED");
IonSystems 12:814a8fdbb6f7 111 log("Setting dispense bits to 00");
IonSystems 12:814a8fdbb6f7 112 dispenseBit1 = false;
IonSystems 12:814a8fdbb6f7 113 dispenseBit2 = false;
IonSystems 12:814a8fdbb6f7 114 redAmount--;
IonSystems 12:814a8fdbb6f7 115 validDispense = true;
IonSystems 12:814a8fdbb6f7 116 }
IonSystems 10:8c0696b99692 117 break;
IonSystems 10:8c0696b99692 118 case GREEN:
IonSystems 12:814a8fdbb6f7 119 if(greenAmount > 0 || operationMode == false){
IonSystems 12:814a8fdbb6f7 120 log("GREEN");
IonSystems 12:814a8fdbb6f7 121 log("Setting dispense bits to 10");
IonSystems 12:814a8fdbb6f7 122 dispenseBit1 = true;
IonSystems 12:814a8fdbb6f7 123 dispenseBit2 = false;
IonSystems 12:814a8fdbb6f7 124 greenAmount--;
IonSystems 12:814a8fdbb6f7 125 validDispense = true;
IonSystems 12:814a8fdbb6f7 126 }
IonSystems 10:8c0696b99692 127 break;
IonSystems 10:8c0696b99692 128 case BLUE:
IonSystems 12:814a8fdbb6f7 129 if(blueAmount > 0 || operationMode == false){
IonSystems 10:8c0696b99692 130 log("BLUE");
IonSystems 10:8c0696b99692 131 log("Setting dispense bits to 01");
IonSystems 10:8c0696b99692 132 dispenseBit1 = false;
IonSystems 10:8c0696b99692 133 dispenseBit2 = true;
IonSystems 12:814a8fdbb6f7 134 blueAmount--;
IonSystems 12:814a8fdbb6f7 135 validDispense = true;
IonSystems 12:814a8fdbb6f7 136 }
IonSystems 10:8c0696b99692 137 break;
IonSystems 14:31ba3e56c788 138
IonSystems 14:31ba3e56c788 139 }writeFile(redAmount,greenAmount,blueAmount);
IonSystems 10:8c0696b99692 140
IonSystems 12:814a8fdbb6f7 141 if(validDispense || operationMode == false){
IonSystems 10:8c0696b99692 142 log("Setting start dispense to true");
IonSystems 10:8c0696b99692 143 startDispense = true; //set the startDispense line high.
IonSystems 10:8c0696b99692 144
IonSystems 10:8c0696b99692 145
IonSystems 10:8c0696b99692 146 //wait(1); //temporary delay
IonSystems 10:8c0696b99692 147 while(!dispenseComplete){
IonSystems 11:06f6e82b40a8 148 wait(0.2);
IonSystems 10:8c0696b99692 149 log("Waiting for dispense complete");
IonSystems 10:8c0696b99692 150 printLCD("Waiting for dispense complete");
IonSystems 10:8c0696b99692 151 }
IonSystems 10:8c0696b99692 152
IonSystems 12:814a8fdbb6f7 153 wait(0.6);
IonSystems 10:8c0696b99692 154 log("Complete");
IonSystems 10:8c0696b99692 155 log("Setting start dispense to false");
IonSystems 10:8c0696b99692 156 startDispense = false;
IonSystems 10:8c0696b99692 157
IonSystems 10:8c0696b99692 158 log("Resetting dispense bits to 11");
IonSystems 10:8c0696b99692 159 dispenseBit1 = true; //reset the dispense select to do nothing
IonSystems 10:8c0696b99692 160 dispenseBit2 = true; //reset the dispense select to do nothing
IonSystems 10:8c0696b99692 161
IonSystems 10:8c0696b99692 162
IonSystems 10:8c0696b99692 163
IonSystems 10:8c0696b99692 164 printLCD("DISPENSE COMPLETE");
IonSystems 13:0661d658d9d1 165
IonSystems 12:814a8fdbb6f7 166 }
IonSystems 10:8c0696b99692 167 }
IonSystems 14:31ba3e56c788 168 void dispenseOrder(int r, int g, int b){
IonSystems 14:31ba3e56c788 169 for(int i = r; r >= 0;i--){
IonSystems 14:31ba3e56c788 170 dispense(RED);
IonSystems 14:31ba3e56c788 171 }
IonSystems 14:31ba3e56c788 172 for(int i = g; i >= 0;i--){
IonSystems 15:0c5f20e15b6a 173 dispense(GREEN);
IonSystems 15:0c5f20e15b6a 174 }
IonSystems 15:0c5f20e15b6a 175 for(int i = g; i >= 0;i--){
IonSystems 15:0c5f20e15b6a 176 dispense(BLUE);
IonSystems 14:31ba3e56c788 177 }
IonSystems 17:6a0bb0ad5bb4 178 }
IonSystems 17:6a0bb0ad5bb4 179
IonSystems 17:6a0bb0ad5bb4 180 void recycle(){
IonSystems 17:6a0bb0ad5bb4 181
IonSystems 17:6a0bb0ad5bb4 182 select = 0; //Setting to operation mode just in case it has not been set.
IonSystems 17:6a0bb0ad5bb4 183 colourBit1 = true;
IonSystems 17:6a0bb0ad5bb4 184 colourBit2 = true;
IonSystems 17:6a0bb0ad5bb4 185 colourBit3 = true;
IonSystems 17:6a0bb0ad5bb4 186
IonSystems 17:6a0bb0ad5bb4 187 log("Recycle");
IonSystems 17:6a0bb0ad5bb4 188 log("Setting sort bits to 100");
IonSystems 17:6a0bb0ad5bb4 189 colourBit1 = false;
IonSystems 17:6a0bb0ad5bb4 190 colourBit2 = false;
IonSystems 17:6a0bb0ad5bb4 191 colourBit3 = true;
IonSystems 17:6a0bb0ad5bb4 192
IonSystems 17:6a0bb0ad5bb4 193 log("Setting start sort to true");
IonSystems 17:6a0bb0ad5bb4 194 startSort = true; //set the startDispense line high.
IonSystems 17:6a0bb0ad5bb4 195
IonSystems 17:6a0bb0ad5bb4 196 //wait(1); //temporary delay
IonSystems 17:6a0bb0ad5bb4 197 while(!sortComplete){
IonSystems 17:6a0bb0ad5bb4 198 wait(0.5);
IonSystems 17:6a0bb0ad5bb4 199 log("Waiting for sort complete");
IonSystems 17:6a0bb0ad5bb4 200 printLCD("Waiting for sort complete");
IonSystems 17:6a0bb0ad5bb4 201 }
IonSystems 17:6a0bb0ad5bb4 202
IonSystems 17:6a0bb0ad5bb4 203
IonSystems 17:6a0bb0ad5bb4 204 log("Complete");
IonSystems 17:6a0bb0ad5bb4 205 wait(0.5);
IonSystems 17:6a0bb0ad5bb4 206 log("Setting start sort to false");
IonSystems 17:6a0bb0ad5bb4 207 startSort = false;
IonSystems 17:6a0bb0ad5bb4 208
IonSystems 17:6a0bb0ad5bb4 209 log("Resetting dispense bits to 11");
IonSystems 17:6a0bb0ad5bb4 210 colourBit1 = true; //reset the dispense select to do nothing
IonSystems 17:6a0bb0ad5bb4 211 colourBit2 = true; //reset the dispense select to do nothing
IonSystems 17:6a0bb0ad5bb4 212 colourBit3 = true;
IonSystems 17:6a0bb0ad5bb4 213 printLCD("RECYCLE COMPLETE");
IonSystems 17:6a0bb0ad5bb4 214
IonSystems 17:6a0bb0ad5bb4 215 }
IonSystems 10:8c0696b99692 216 void sort(Colour colour){
IonSystems 13:0661d658d9d1 217 if(colour == NONE){
IonSystems 13:0661d658d9d1 218 return;
IonSystems 13:0661d658d9d1 219 }else{
IonSystems 17:6a0bb0ad5bb4 220
IonSystems 10:8c0696b99692 221 select = 0; //Setting to operation mode just in case it has not been set.
IonSystems 10:8c0696b99692 222 colourBit1 = true;
IonSystems 10:8c0696b99692 223 colourBit2 = true;
IonSystems 10:8c0696b99692 224 colourBit3 = true;
IonSystems 10:8c0696b99692 225 switch(colour){
IonSystems 10:8c0696b99692 226 case RED:
IonSystems 17:6a0bb0ad5bb4 227 if(redAmount >= tubeSize){
IonSystems 17:6a0bb0ad5bb4 228 recycle();
IonSystems 17:6a0bb0ad5bb4 229 return;
IonSystems 17:6a0bb0ad5bb4 230 }
IonSystems 10:8c0696b99692 231 log("Sort RED");
IonSystems 10:8c0696b99692 232 log("Setting sort bits to 000");
IonSystems 10:8c0696b99692 233 colourBit1 = false;
IonSystems 10:8c0696b99692 234 colourBit2 = false;
IonSystems 10:8c0696b99692 235 colourBit3 = false;
IonSystems 12:814a8fdbb6f7 236 redAmount++;
IonSystems 10:8c0696b99692 237 break;
IonSystems 10:8c0696b99692 238 case GREEN:
IonSystems 17:6a0bb0ad5bb4 239 if(greenAmount >= tubeSize){
IonSystems 17:6a0bb0ad5bb4 240 recycle();
IonSystems 17:6a0bb0ad5bb4 241 return;
IonSystems 17:6a0bb0ad5bb4 242 }
IonSystems 10:8c0696b99692 243 log("Sort GREEN");
IonSystems 10:8c0696b99692 244 log("Setting sort bits to 001");
IonSystems 10:8c0696b99692 245 colourBit1 = true;
IonSystems 10:8c0696b99692 246 colourBit2 = false;
IonSystems 10:8c0696b99692 247 colourBit3 = false;
IonSystems 12:814a8fdbb6f7 248 greenAmount++;
IonSystems 10:8c0696b99692 249 break;
IonSystems 10:8c0696b99692 250 case BLUE:
IonSystems 17:6a0bb0ad5bb4 251 if(blueAmount >= tubeSize){
IonSystems 17:6a0bb0ad5bb4 252 recycle();
IonSystems 17:6a0bb0ad5bb4 253 return;
IonSystems 17:6a0bb0ad5bb4 254 }
IonSystems 10:8c0696b99692 255 log("Sort BLUE");
IonSystems 10:8c0696b99692 256 log("Setting sort bits to 010");
IonSystems 10:8c0696b99692 257 colourBit1 = false;
IonSystems 10:8c0696b99692 258 colourBit2 = true;
IonSystems 10:8c0696b99692 259 colourBit3 = false;
IonSystems 12:814a8fdbb6f7 260 blueAmount++;
IonSystems 10:8c0696b99692 261 break;
IonSystems 11:06f6e82b40a8 262 case BIN:
IonSystems 11:06f6e82b40a8 263 log("Sort BIN");
IonSystems 11:06f6e82b40a8 264 log("Setting sort bits to 011");
IonSystems 11:06f6e82b40a8 265 colourBit1 = true;
IonSystems 11:06f6e82b40a8 266 colourBit2 = true;
IonSystems 11:06f6e82b40a8 267 colourBit3 = false;
IonSystems 11:06f6e82b40a8 268 break;
IonSystems 10:8c0696b99692 269 }
IonSystems 10:8c0696b99692 270
IonSystems 10:8c0696b99692 271
IonSystems 10:8c0696b99692 272 log("Setting start sort to true");
IonSystems 10:8c0696b99692 273 startSort = true; //set the startDispense line high.
IonSystems 10:8c0696b99692 274
IonSystems 10:8c0696b99692 275
IonSystems 10:8c0696b99692 276 //wait(1); //temporary delay
IonSystems 10:8c0696b99692 277 while(!sortComplete){
IonSystems 11:06f6e82b40a8 278 wait(0.5);
IonSystems 10:8c0696b99692 279 log("Waiting for sort complete");
IonSystems 10:8c0696b99692 280 printLCD("Waiting for sort complete");
IonSystems 10:8c0696b99692 281 }
IonSystems 10:8c0696b99692 282
IonSystems 10:8c0696b99692 283
IonSystems 10:8c0696b99692 284 log("Complete");
IonSystems 12:814a8fdbb6f7 285 wait(0.6);
IonSystems 10:8c0696b99692 286 log("Setting start sort to false");
IonSystems 11:06f6e82b40a8 287 startSort = false;
IonSystems 10:8c0696b99692 288
IonSystems 10:8c0696b99692 289 log("Resetting dispense bits to 11");
IonSystems 10:8c0696b99692 290 colourBit1 = true; //reset the dispense select to do nothing
IonSystems 10:8c0696b99692 291 colourBit2 = true; //reset the dispense select to do nothing
IonSystems 10:8c0696b99692 292 colourBit3 = true;
IonSystems 10:8c0696b99692 293 printLCD("SORT COMPLETE");
IonSystems 12:814a8fdbb6f7 294 writeFile(redAmount,greenAmount,blueAmount);
IonSystems 13:0661d658d9d1 295 }
IonSystems 12:814a8fdbb6f7 296 }
IonSystems 12:814a8fdbb6f7 297
IonSystems 12:814a8fdbb6f7 298 void dispenseAll(){
IonSystems 12:814a8fdbb6f7 299 int red = redAmount;
IonSystems 12:814a8fdbb6f7 300 int green = greenAmount;
IonSystems 12:814a8fdbb6f7 301 int blue = blueAmount;
IonSystems 12:814a8fdbb6f7 302 for(int r = 0;r < red;r++){
IonSystems 12:814a8fdbb6f7 303 dispense(RED);
IonSystems 12:814a8fdbb6f7 304 wait(0.5);
IonSystems 12:814a8fdbb6f7 305 }redAmount = 0;
IonSystems 12:814a8fdbb6f7 306
IonSystems 12:814a8fdbb6f7 307 for(int g = 0;g < green;g++){
IonSystems 12:814a8fdbb6f7 308 dispense(GREEN);
IonSystems 12:814a8fdbb6f7 309 wait(0.5);
IonSystems 12:814a8fdbb6f7 310 }greenAmount = 0;
IonSystems 12:814a8fdbb6f7 311
IonSystems 12:814a8fdbb6f7 312 for(int b = 0;b < blue;b++){
IonSystems 12:814a8fdbb6f7 313 dispense(BLUE);
IonSystems 12:814a8fdbb6f7 314 wait(0.5);
IonSystems 12:814a8fdbb6f7 315 }blueAmount = 0;
IonSystems 12:814a8fdbb6f7 316 writeFile(redAmount,greenAmount,blueAmount);
IonSystems 12:814a8fdbb6f7 317 }