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 17 21:31:42 2014 +0000
Revision:
12:814a8fdbb6f7
Parent:
11:06f6e82b40a8
Child:
13:0661d658d9d1
dispense and sort working well, also stored correct chip values

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 12:814a8fdbb6f7 93 bool validDispense = false;
IonSystems 10:8c0696b99692 94 select = 0; //Setting to operation mode just in case it has not been set.
IonSystems 10:8c0696b99692 95 dispenseBit1 = true;
IonSystems 10:8c0696b99692 96 dispenseBit2 = true;
IonSystems 10:8c0696b99692 97 switch(colour){
IonSystems 10:8c0696b99692 98 case RED:
IonSystems 12:814a8fdbb6f7 99 if(redAmount > 0 || operationMode == false){
IonSystems 12:814a8fdbb6f7 100 log("RED");
IonSystems 12:814a8fdbb6f7 101 log("Setting dispense bits to 00");
IonSystems 12:814a8fdbb6f7 102 dispenseBit1 = false;
IonSystems 12:814a8fdbb6f7 103 dispenseBit2 = false;
IonSystems 12:814a8fdbb6f7 104 redAmount--;
IonSystems 12:814a8fdbb6f7 105 validDispense = true;
IonSystems 12:814a8fdbb6f7 106 }
IonSystems 10:8c0696b99692 107 break;
IonSystems 10:8c0696b99692 108 case GREEN:
IonSystems 12:814a8fdbb6f7 109 if(greenAmount > 0 || operationMode == false){
IonSystems 12:814a8fdbb6f7 110 log("GREEN");
IonSystems 12:814a8fdbb6f7 111 log("Setting dispense bits to 10");
IonSystems 12:814a8fdbb6f7 112 dispenseBit1 = true;
IonSystems 12:814a8fdbb6f7 113 dispenseBit2 = false;
IonSystems 12:814a8fdbb6f7 114 greenAmount--;
IonSystems 12:814a8fdbb6f7 115 validDispense = true;
IonSystems 12:814a8fdbb6f7 116 }
IonSystems 10:8c0696b99692 117 break;
IonSystems 10:8c0696b99692 118 case BLUE:
IonSystems 12:814a8fdbb6f7 119 if(blueAmount > 0 || operationMode == false){
IonSystems 10:8c0696b99692 120 log("BLUE");
IonSystems 10:8c0696b99692 121 log("Setting dispense bits to 01");
IonSystems 10:8c0696b99692 122 dispenseBit1 = false;
IonSystems 10:8c0696b99692 123 dispenseBit2 = true;
IonSystems 12:814a8fdbb6f7 124 blueAmount--;
IonSystems 12:814a8fdbb6f7 125 validDispense = true;
IonSystems 12:814a8fdbb6f7 126 }
IonSystems 10:8c0696b99692 127 break;
IonSystems 12:814a8fdbb6f7 128 writeFile(redAmount,greenAmount,blueAmount);
IonSystems 10:8c0696b99692 129 }
IonSystems 10:8c0696b99692 130
IonSystems 12:814a8fdbb6f7 131 if(validDispense || operationMode == false){
IonSystems 10:8c0696b99692 132 log("Setting start dispense to true");
IonSystems 10:8c0696b99692 133 startDispense = true; //set the startDispense line high.
IonSystems 10:8c0696b99692 134
IonSystems 10:8c0696b99692 135
IonSystems 10:8c0696b99692 136 //wait(1); //temporary delay
IonSystems 10:8c0696b99692 137 while(!dispenseComplete){
IonSystems 11:06f6e82b40a8 138 wait(0.2);
IonSystems 10:8c0696b99692 139 log("Waiting for dispense complete");
IonSystems 10:8c0696b99692 140 printLCD("Waiting for dispense complete");
IonSystems 10:8c0696b99692 141 }
IonSystems 10:8c0696b99692 142
IonSystems 12:814a8fdbb6f7 143 wait(0.6);
IonSystems 10:8c0696b99692 144 log("Complete");
IonSystems 10:8c0696b99692 145 log("Setting start dispense to false");
IonSystems 10:8c0696b99692 146 startDispense = false;
IonSystems 10:8c0696b99692 147
IonSystems 10:8c0696b99692 148 log("Resetting dispense bits to 11");
IonSystems 10:8c0696b99692 149 dispenseBit1 = true; //reset the dispense select to do nothing
IonSystems 10:8c0696b99692 150 dispenseBit2 = true; //reset the dispense select to do nothing
IonSystems 10:8c0696b99692 151
IonSystems 10:8c0696b99692 152
IonSystems 10:8c0696b99692 153
IonSystems 10:8c0696b99692 154 printLCD("DISPENSE COMPLETE");
IonSystems 12:814a8fdbb6f7 155 }
IonSystems 10:8c0696b99692 156 }
IonSystems 10:8c0696b99692 157
IonSystems 10:8c0696b99692 158 void sort(Colour colour){
IonSystems 10:8c0696b99692 159 select = 0; //Setting to operation mode just in case it has not been set.
IonSystems 10:8c0696b99692 160 colourBit1 = true;
IonSystems 10:8c0696b99692 161 colourBit2 = true;
IonSystems 10:8c0696b99692 162 colourBit3 = true;
IonSystems 10:8c0696b99692 163 switch(colour){
IonSystems 10:8c0696b99692 164 case RED:
IonSystems 10:8c0696b99692 165 log("Sort RED");
IonSystems 10:8c0696b99692 166 log("Setting sort bits to 000");
IonSystems 10:8c0696b99692 167 colourBit1 = false;
IonSystems 10:8c0696b99692 168 colourBit2 = false;
IonSystems 10:8c0696b99692 169 colourBit3 = false;
IonSystems 12:814a8fdbb6f7 170 redAmount++;
IonSystems 10:8c0696b99692 171 break;
IonSystems 10:8c0696b99692 172 case GREEN:
IonSystems 10:8c0696b99692 173 log("Sort GREEN");
IonSystems 10:8c0696b99692 174 log("Setting sort bits to 001");
IonSystems 10:8c0696b99692 175 colourBit1 = true;
IonSystems 10:8c0696b99692 176 colourBit2 = false;
IonSystems 10:8c0696b99692 177 colourBit3 = false;
IonSystems 12:814a8fdbb6f7 178 greenAmount++;
IonSystems 10:8c0696b99692 179 break;
IonSystems 10:8c0696b99692 180 case BLUE:
IonSystems 10:8c0696b99692 181 log("Sort BLUE");
IonSystems 10:8c0696b99692 182 log("Setting sort bits to 010");
IonSystems 10:8c0696b99692 183 colourBit1 = false;
IonSystems 10:8c0696b99692 184 colourBit2 = true;
IonSystems 10:8c0696b99692 185 colourBit3 = false;
IonSystems 12:814a8fdbb6f7 186 blueAmount++;
IonSystems 10:8c0696b99692 187 break;
IonSystems 11:06f6e82b40a8 188 case BIN:
IonSystems 11:06f6e82b40a8 189 log("Sort BIN");
IonSystems 11:06f6e82b40a8 190 log("Setting sort bits to 011");
IonSystems 11:06f6e82b40a8 191 colourBit1 = true;
IonSystems 11:06f6e82b40a8 192 colourBit2 = true;
IonSystems 11:06f6e82b40a8 193 colourBit3 = false;
IonSystems 11:06f6e82b40a8 194 break;
IonSystems 11:06f6e82b40a8 195 case RECYCLE:
IonSystems 11:06f6e82b40a8 196 log("Recycle");
IonSystems 11:06f6e82b40a8 197 log("Setting sort bits to 100");
IonSystems 11:06f6e82b40a8 198 colourBit1 = false;
IonSystems 11:06f6e82b40a8 199 colourBit2 = false;
IonSystems 11:06f6e82b40a8 200 colourBit3 = true;
IonSystems 11:06f6e82b40a8 201 break;
IonSystems 10:8c0696b99692 202 }
IonSystems 10:8c0696b99692 203
IonSystems 10:8c0696b99692 204
IonSystems 10:8c0696b99692 205 log("Setting start sort to true");
IonSystems 10:8c0696b99692 206 startSort = true; //set the startDispense line high.
IonSystems 10:8c0696b99692 207
IonSystems 10:8c0696b99692 208
IonSystems 10:8c0696b99692 209 //wait(1); //temporary delay
IonSystems 10:8c0696b99692 210 while(!sortComplete){
IonSystems 11:06f6e82b40a8 211 wait(0.5);
IonSystems 10:8c0696b99692 212 log("Waiting for sort complete");
IonSystems 10:8c0696b99692 213 printLCD("Waiting for sort complete");
IonSystems 10:8c0696b99692 214 }
IonSystems 10:8c0696b99692 215
IonSystems 10:8c0696b99692 216
IonSystems 10:8c0696b99692 217 log("Complete");
IonSystems 12:814a8fdbb6f7 218 wait(0.6);
IonSystems 10:8c0696b99692 219 log("Setting start sort to false");
IonSystems 11:06f6e82b40a8 220 startSort = false;
IonSystems 10:8c0696b99692 221
IonSystems 10:8c0696b99692 222 log("Resetting dispense bits to 11");
IonSystems 10:8c0696b99692 223 colourBit1 = true; //reset the dispense select to do nothing
IonSystems 10:8c0696b99692 224 colourBit2 = true; //reset the dispense select to do nothing
IonSystems 10:8c0696b99692 225 colourBit3 = true;
IonSystems 10:8c0696b99692 226 printLCD("SORT COMPLETE");
IonSystems 12:814a8fdbb6f7 227 writeFile(redAmount,greenAmount,blueAmount);
IonSystems 10:8c0696b99692 228
IonSystems 12:814a8fdbb6f7 229 }
IonSystems 12:814a8fdbb6f7 230
IonSystems 12:814a8fdbb6f7 231 void dispenseAll(){
IonSystems 12:814a8fdbb6f7 232 int red = redAmount;
IonSystems 12:814a8fdbb6f7 233 int green = greenAmount;
IonSystems 12:814a8fdbb6f7 234 int blue = blueAmount;
IonSystems 12:814a8fdbb6f7 235 for(int r = 0;r < red;r++){
IonSystems 12:814a8fdbb6f7 236 dispense(RED);
IonSystems 12:814a8fdbb6f7 237 wait(0.5);
IonSystems 12:814a8fdbb6f7 238 }redAmount = 0;
IonSystems 12:814a8fdbb6f7 239
IonSystems 12:814a8fdbb6f7 240 for(int g = 0;g < green;g++){
IonSystems 12:814a8fdbb6f7 241 dispense(GREEN);
IonSystems 12:814a8fdbb6f7 242 wait(0.5);
IonSystems 12:814a8fdbb6f7 243 }greenAmount = 0;
IonSystems 12:814a8fdbb6f7 244
IonSystems 12:814a8fdbb6f7 245 for(int b = 0;b < blue;b++){
IonSystems 12:814a8fdbb6f7 246 dispense(BLUE);
IonSystems 12:814a8fdbb6f7 247 wait(0.5);
IonSystems 12:814a8fdbb6f7 248 }blueAmount = 0;
IonSystems 12:814a8fdbb6f7 249 writeFile(redAmount,greenAmount,blueAmount);
IonSystems 12:814a8fdbb6f7 250 }