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
FPGAcomms.h@15:0c5f20e15b6a, 2014-11-21 (annotated)
- 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?
User | Revision | Line number | New 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 | 14:31ba3e56c788 | 51 | printLCD("Waiting for REC"); |
IonSystems | 12:814a8fdbb6f7 | 52 | wait(0.1); |
IonSystems | 12:814a8fdbb6f7 | 53 | } |
IonSystems | 12:814a8fdbb6f7 | 54 | } |
IonSystems | 12:814a8fdbb6f7 | 55 | void maintain(StateMachine maintainState){ |
IonSystems | 12:814a8fdbb6f7 | 56 | select = 1; //setting FPGA to maintenance mode. |
IonSystems | 12:814a8fdbb6f7 | 57 | setMaintenanceSelect(true,true,true,true,true,true); |
IonSystems | 12:814a8fdbb6f7 | 58 | switch(maintainState){ |
IonSystems | 12:814a8fdbb6f7 | 59 | case RB_LEFT: |
IonSystems | 12:814a8fdbb6f7 | 60 | setMaintenanceSelect(false,false,false,true,false,true); |
IonSystems | 12:814a8fdbb6f7 | 61 | break; |
IonSystems | 12:814a8fdbb6f7 | 62 | case RB_CENTRE: |
IonSystems | 12:814a8fdbb6f7 | 63 | setMaintenanceSelect(false,false,false,true,true,false); |
IonSystems | 12:814a8fdbb6f7 | 64 | break; |
IonSystems | 12:814a8fdbb6f7 | 65 | case RB_RIGHT: |
IonSystems | 12:814a8fdbb6f7 | 66 | setMaintenanceSelect(false,false,false,true,true,true); |
IonSystems | 12:814a8fdbb6f7 | 67 | break; |
IonSystems | 12:814a8fdbb6f7 | 68 | case GO_UP: |
IonSystems | 12:814a8fdbb6f7 | 69 | setMaintenanceSelect(false,false,true,false,false,false); |
IonSystems | 12:814a8fdbb6f7 | 70 | break; |
IonSystems | 12:814a8fdbb6f7 | 71 | case GO_CENTRE: |
IonSystems | 12:814a8fdbb6f7 | 72 | setMaintenanceSelect(false,false,true,false,false,true); |
IonSystems | 12:814a8fdbb6f7 | 73 | break; |
IonSystems | 12:814a8fdbb6f7 | 74 | case GO_DOWN: |
IonSystems | 12:814a8fdbb6f7 | 75 | setMaintenanceSelect(false,false,true,false,true,false); |
IonSystems | 12:814a8fdbb6f7 | 76 | break; |
IonSystems | 12:814a8fdbb6f7 | 77 | case BR_LEFT: |
IonSystems | 12:814a8fdbb6f7 | 78 | setMaintenanceSelect(false,false,true,false,true,true); |
IonSystems | 12:814a8fdbb6f7 | 79 | break; |
IonSystems | 12:814a8fdbb6f7 | 80 | case BR_RIGHT: |
IonSystems | 12:814a8fdbb6f7 | 81 | setMaintenanceSelect(false,false,true,true,false,false); |
IonSystems | 12:814a8fdbb6f7 | 82 | break; |
IonSystems | 12:814a8fdbb6f7 | 83 | case R_PUSH: |
IonSystems | 12:814a8fdbb6f7 | 84 | setMaintenanceSelect(false,false,true,true,false,true); |
IonSystems | 12:814a8fdbb6f7 | 85 | break; |
IonSystems | 14:31ba3e56c788 | 86 | default: |
IonSystems | 14:31ba3e56c788 | 87 | log("Incorrect maintenance command recieved"); |
IonSystems | 14:31ba3e56c788 | 88 | printLCD("Incorrect maintenance command recieved"); |
IonSystems | 14:31ba3e56c788 | 89 | break; |
IonSystems | 12:814a8fdbb6f7 | 90 | } |
IonSystems | 12:814a8fdbb6f7 | 91 | setMaintenanceStart(true);//Bit 7 |
IonSystems | 12:814a8fdbb6f7 | 92 | waitForMaintenanceComplete(); |
IonSystems | 15:0c5f20e15b6a | 93 | wait(0.5); |
IonSystems | 12:814a8fdbb6f7 | 94 | setMaintenanceStart(false); |
IonSystems | 12:814a8fdbb6f7 | 95 | } |
IonSystems | 12:814a8fdbb6f7 | 96 | |
IonSystems | 12:814a8fdbb6f7 | 97 | |
IonSystems | 10:8c0696b99692 | 98 | void dispense(Colour colour){ |
IonSystems | 13:0661d658d9d1 | 99 | |
IonSystems | 12:814a8fdbb6f7 | 100 | bool validDispense = false; |
IonSystems | 10:8c0696b99692 | 101 | select = 0; //Setting to operation mode just in case it has not been set. |
IonSystems | 10:8c0696b99692 | 102 | dispenseBit1 = true; |
IonSystems | 10:8c0696b99692 | 103 | dispenseBit2 = true; |
IonSystems | 14:31ba3e56c788 | 104 | /* A dispense operation can only take place if there are more than zero chips |
IonSystems | 14:31ba3e56c788 | 105 | */ |
IonSystems | 10:8c0696b99692 | 106 | switch(colour){ |
IonSystems | 10:8c0696b99692 | 107 | case RED: |
IonSystems | 14:31ba3e56c788 | 108 | if(redAmount > 0 || operationMode == false){ |
IonSystems | 12:814a8fdbb6f7 | 109 | log("RED"); |
IonSystems | 12:814a8fdbb6f7 | 110 | log("Setting dispense bits to 00"); |
IonSystems | 12:814a8fdbb6f7 | 111 | dispenseBit1 = false; |
IonSystems | 12:814a8fdbb6f7 | 112 | dispenseBit2 = false; |
IonSystems | 12:814a8fdbb6f7 | 113 | redAmount--; |
IonSystems | 12:814a8fdbb6f7 | 114 | validDispense = true; |
IonSystems | 12:814a8fdbb6f7 | 115 | } |
IonSystems | 10:8c0696b99692 | 116 | break; |
IonSystems | 10:8c0696b99692 | 117 | case GREEN: |
IonSystems | 12:814a8fdbb6f7 | 118 | if(greenAmount > 0 || operationMode == false){ |
IonSystems | 12:814a8fdbb6f7 | 119 | log("GREEN"); |
IonSystems | 12:814a8fdbb6f7 | 120 | log("Setting dispense bits to 10"); |
IonSystems | 12:814a8fdbb6f7 | 121 | dispenseBit1 = true; |
IonSystems | 12:814a8fdbb6f7 | 122 | dispenseBit2 = false; |
IonSystems | 12:814a8fdbb6f7 | 123 | greenAmount--; |
IonSystems | 12:814a8fdbb6f7 | 124 | validDispense = true; |
IonSystems | 12:814a8fdbb6f7 | 125 | } |
IonSystems | 10:8c0696b99692 | 126 | break; |
IonSystems | 10:8c0696b99692 | 127 | case BLUE: |
IonSystems | 12:814a8fdbb6f7 | 128 | if(blueAmount > 0 || operationMode == false){ |
IonSystems | 10:8c0696b99692 | 129 | log("BLUE"); |
IonSystems | 10:8c0696b99692 | 130 | log("Setting dispense bits to 01"); |
IonSystems | 10:8c0696b99692 | 131 | dispenseBit1 = false; |
IonSystems | 10:8c0696b99692 | 132 | dispenseBit2 = true; |
IonSystems | 12:814a8fdbb6f7 | 133 | blueAmount--; |
IonSystems | 12:814a8fdbb6f7 | 134 | validDispense = true; |
IonSystems | 12:814a8fdbb6f7 | 135 | } |
IonSystems | 10:8c0696b99692 | 136 | break; |
IonSystems | 14:31ba3e56c788 | 137 | |
IonSystems | 14:31ba3e56c788 | 138 | }writeFile(redAmount,greenAmount,blueAmount); |
IonSystems | 10:8c0696b99692 | 139 | |
IonSystems | 12:814a8fdbb6f7 | 140 | if(validDispense || operationMode == false){ |
IonSystems | 10:8c0696b99692 | 141 | log("Setting start dispense to true"); |
IonSystems | 10:8c0696b99692 | 142 | startDispense = true; //set the startDispense line high. |
IonSystems | 10:8c0696b99692 | 143 | |
IonSystems | 10:8c0696b99692 | 144 | |
IonSystems | 10:8c0696b99692 | 145 | //wait(1); //temporary delay |
IonSystems | 10:8c0696b99692 | 146 | while(!dispenseComplete){ |
IonSystems | 11:06f6e82b40a8 | 147 | wait(0.2); |
IonSystems | 10:8c0696b99692 | 148 | log("Waiting for dispense complete"); |
IonSystems | 10:8c0696b99692 | 149 | printLCD("Waiting for dispense complete"); |
IonSystems | 10:8c0696b99692 | 150 | } |
IonSystems | 10:8c0696b99692 | 151 | |
IonSystems | 12:814a8fdbb6f7 | 152 | wait(0.6); |
IonSystems | 10:8c0696b99692 | 153 | log("Complete"); |
IonSystems | 10:8c0696b99692 | 154 | log("Setting start dispense to false"); |
IonSystems | 10:8c0696b99692 | 155 | startDispense = false; |
IonSystems | 10:8c0696b99692 | 156 | |
IonSystems | 10:8c0696b99692 | 157 | log("Resetting dispense bits to 11"); |
IonSystems | 10:8c0696b99692 | 158 | dispenseBit1 = true; //reset the dispense select to do nothing |
IonSystems | 10:8c0696b99692 | 159 | dispenseBit2 = true; //reset the dispense select to do nothing |
IonSystems | 10:8c0696b99692 | 160 | |
IonSystems | 10:8c0696b99692 | 161 | |
IonSystems | 10:8c0696b99692 | 162 | |
IonSystems | 10:8c0696b99692 | 163 | printLCD("DISPENSE COMPLETE"); |
IonSystems | 13:0661d658d9d1 | 164 | |
IonSystems | 12:814a8fdbb6f7 | 165 | } |
IonSystems | 10:8c0696b99692 | 166 | } |
IonSystems | 14:31ba3e56c788 | 167 | void dispenseOrder(int r, int g, int b){ |
IonSystems | 14:31ba3e56c788 | 168 | for(int i = r; r >= 0;i--){ |
IonSystems | 14:31ba3e56c788 | 169 | dispense(RED); |
IonSystems | 14:31ba3e56c788 | 170 | } |
IonSystems | 14:31ba3e56c788 | 171 | for(int i = g; i >= 0;i--){ |
IonSystems | 15:0c5f20e15b6a | 172 | dispense(GREEN); |
IonSystems | 15:0c5f20e15b6a | 173 | } |
IonSystems | 15:0c5f20e15b6a | 174 | for(int i = g; i >= 0;i--){ |
IonSystems | 15:0c5f20e15b6a | 175 | dispense(BLUE); |
IonSystems | 14:31ba3e56c788 | 176 | } |
IonSystems | 14:31ba3e56c788 | 177 | } |
IonSystems | 10:8c0696b99692 | 178 | void sort(Colour colour){ |
IonSystems | 13:0661d658d9d1 | 179 | if(colour == NONE){ |
IonSystems | 13:0661d658d9d1 | 180 | return; |
IonSystems | 13:0661d658d9d1 | 181 | }else{ |
IonSystems | 10:8c0696b99692 | 182 | select = 0; //Setting to operation mode just in case it has not been set. |
IonSystems | 10:8c0696b99692 | 183 | colourBit1 = true; |
IonSystems | 10:8c0696b99692 | 184 | colourBit2 = true; |
IonSystems | 10:8c0696b99692 | 185 | colourBit3 = true; |
IonSystems | 10:8c0696b99692 | 186 | switch(colour){ |
IonSystems | 10:8c0696b99692 | 187 | case RED: |
IonSystems | 10:8c0696b99692 | 188 | log("Sort RED"); |
IonSystems | 10:8c0696b99692 | 189 | log("Setting sort bits to 000"); |
IonSystems | 10:8c0696b99692 | 190 | colourBit1 = false; |
IonSystems | 10:8c0696b99692 | 191 | colourBit2 = false; |
IonSystems | 10:8c0696b99692 | 192 | colourBit3 = false; |
IonSystems | 12:814a8fdbb6f7 | 193 | redAmount++; |
IonSystems | 10:8c0696b99692 | 194 | break; |
IonSystems | 10:8c0696b99692 | 195 | case GREEN: |
IonSystems | 10:8c0696b99692 | 196 | log("Sort GREEN"); |
IonSystems | 10:8c0696b99692 | 197 | log("Setting sort bits to 001"); |
IonSystems | 10:8c0696b99692 | 198 | colourBit1 = true; |
IonSystems | 10:8c0696b99692 | 199 | colourBit2 = false; |
IonSystems | 10:8c0696b99692 | 200 | colourBit3 = false; |
IonSystems | 12:814a8fdbb6f7 | 201 | greenAmount++; |
IonSystems | 10:8c0696b99692 | 202 | break; |
IonSystems | 10:8c0696b99692 | 203 | case BLUE: |
IonSystems | 10:8c0696b99692 | 204 | log("Sort BLUE"); |
IonSystems | 10:8c0696b99692 | 205 | log("Setting sort bits to 010"); |
IonSystems | 10:8c0696b99692 | 206 | colourBit1 = false; |
IonSystems | 10:8c0696b99692 | 207 | colourBit2 = true; |
IonSystems | 10:8c0696b99692 | 208 | colourBit3 = false; |
IonSystems | 12:814a8fdbb6f7 | 209 | blueAmount++; |
IonSystems | 10:8c0696b99692 | 210 | break; |
IonSystems | 11:06f6e82b40a8 | 211 | case BIN: |
IonSystems | 11:06f6e82b40a8 | 212 | log("Sort BIN"); |
IonSystems | 11:06f6e82b40a8 | 213 | log("Setting sort bits to 011"); |
IonSystems | 11:06f6e82b40a8 | 214 | colourBit1 = true; |
IonSystems | 11:06f6e82b40a8 | 215 | colourBit2 = true; |
IonSystems | 11:06f6e82b40a8 | 216 | colourBit3 = false; |
IonSystems | 11:06f6e82b40a8 | 217 | break; |
IonSystems | 11:06f6e82b40a8 | 218 | case RECYCLE: |
IonSystems | 11:06f6e82b40a8 | 219 | log("Recycle"); |
IonSystems | 11:06f6e82b40a8 | 220 | log("Setting sort bits to 100"); |
IonSystems | 11:06f6e82b40a8 | 221 | colourBit1 = false; |
IonSystems | 11:06f6e82b40a8 | 222 | colourBit2 = false; |
IonSystems | 11:06f6e82b40a8 | 223 | colourBit3 = true; |
IonSystems | 11:06f6e82b40a8 | 224 | break; |
IonSystems | 10:8c0696b99692 | 225 | } |
IonSystems | 10:8c0696b99692 | 226 | |
IonSystems | 10:8c0696b99692 | 227 | |
IonSystems | 10:8c0696b99692 | 228 | log("Setting start sort to true"); |
IonSystems | 10:8c0696b99692 | 229 | startSort = true; //set the startDispense line high. |
IonSystems | 10:8c0696b99692 | 230 | |
IonSystems | 10:8c0696b99692 | 231 | |
IonSystems | 10:8c0696b99692 | 232 | //wait(1); //temporary delay |
IonSystems | 10:8c0696b99692 | 233 | while(!sortComplete){ |
IonSystems | 11:06f6e82b40a8 | 234 | wait(0.5); |
IonSystems | 10:8c0696b99692 | 235 | log("Waiting for sort complete"); |
IonSystems | 10:8c0696b99692 | 236 | printLCD("Waiting for sort complete"); |
IonSystems | 10:8c0696b99692 | 237 | } |
IonSystems | 10:8c0696b99692 | 238 | |
IonSystems | 10:8c0696b99692 | 239 | |
IonSystems | 10:8c0696b99692 | 240 | log("Complete"); |
IonSystems | 12:814a8fdbb6f7 | 241 | wait(0.6); |
IonSystems | 10:8c0696b99692 | 242 | log("Setting start sort to false"); |
IonSystems | 11:06f6e82b40a8 | 243 | startSort = false; |
IonSystems | 10:8c0696b99692 | 244 | |
IonSystems | 10:8c0696b99692 | 245 | log("Resetting dispense bits to 11"); |
IonSystems | 10:8c0696b99692 | 246 | colourBit1 = true; //reset the dispense select to do nothing |
IonSystems | 10:8c0696b99692 | 247 | colourBit2 = true; //reset the dispense select to do nothing |
IonSystems | 10:8c0696b99692 | 248 | colourBit3 = true; |
IonSystems | 10:8c0696b99692 | 249 | printLCD("SORT COMPLETE"); |
IonSystems | 12:814a8fdbb6f7 | 250 | writeFile(redAmount,greenAmount,blueAmount); |
IonSystems | 13:0661d658d9d1 | 251 | } |
IonSystems | 12:814a8fdbb6f7 | 252 | } |
IonSystems | 12:814a8fdbb6f7 | 253 | |
IonSystems | 12:814a8fdbb6f7 | 254 | void dispenseAll(){ |
IonSystems | 12:814a8fdbb6f7 | 255 | int red = redAmount; |
IonSystems | 12:814a8fdbb6f7 | 256 | int green = greenAmount; |
IonSystems | 12:814a8fdbb6f7 | 257 | int blue = blueAmount; |
IonSystems | 12:814a8fdbb6f7 | 258 | for(int r = 0;r < red;r++){ |
IonSystems | 12:814a8fdbb6f7 | 259 | dispense(RED); |
IonSystems | 12:814a8fdbb6f7 | 260 | wait(0.5); |
IonSystems | 12:814a8fdbb6f7 | 261 | }redAmount = 0; |
IonSystems | 12:814a8fdbb6f7 | 262 | |
IonSystems | 12:814a8fdbb6f7 | 263 | for(int g = 0;g < green;g++){ |
IonSystems | 12:814a8fdbb6f7 | 264 | dispense(GREEN); |
IonSystems | 12:814a8fdbb6f7 | 265 | wait(0.5); |
IonSystems | 12:814a8fdbb6f7 | 266 | }greenAmount = 0; |
IonSystems | 12:814a8fdbb6f7 | 267 | |
IonSystems | 12:814a8fdbb6f7 | 268 | for(int b = 0;b < blue;b++){ |
IonSystems | 12:814a8fdbb6f7 | 269 | dispense(BLUE); |
IonSystems | 12:814a8fdbb6f7 | 270 | wait(0.5); |
IonSystems | 12:814a8fdbb6f7 | 271 | }blueAmount = 0; |
IonSystems | 12:814a8fdbb6f7 | 272 | writeFile(redAmount,greenAmount,blueAmount); |
IonSystems | 12:814a8fdbb6f7 | 273 | } |