![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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@23:f9e7e64784be, 2014-11-29 (annotated)
- Committer:
- IonSystems
- Date:
- Sat Nov 29 16:32:57 2014 +0000
- Revision:
- 23:f9e7e64784be
- Parent:
- 22:8f11d1c178ab
- Child:
- 24:8868101d01d0
might be working
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 | 23:f9e7e64784be | 4 | |
IonSystems | 6:e64796f1f384 | 5 | //Setup pins to FPGA |
IonSystems | 6:e64796f1f384 | 6 | DigitalOut startSort(p5); //A positive edge tells the FPGA to start sorting. |
IonSystems | 10:8c0696b99692 | 7 | DigitalIn sortComplete(p16); |
IonSystems | 6:e64796f1f384 | 8 | /* |
IonSystems | 6:e64796f1f384 | 9 | The command codes for the three sort select bits(ColourBit1 - ColourBit2) |
IonSystems | 17:6a0bb0ad5bb4 | 10 | See datasheet for furter information |
IonSystems | 6:e64796f1f384 | 11 | 000 Sort red chip |
IonSystems | 6:e64796f1f384 | 12 | 001 Sort green chip |
IonSystems | 6:e64796f1f384 | 13 | 010 Sort blue chip |
IonSystems | 6:e64796f1f384 | 14 | 011 Bin the chip |
IonSystems | 6:e64796f1f384 | 15 | 100 Recycle the chip |
IonSystems | 6:e64796f1f384 | 16 | 101 Nothing |
IonSystems | 6:e64796f1f384 | 17 | 110 Nothing |
IonSystems | 6:e64796f1f384 | 18 | 111 Nothing |
IonSystems | 6:e64796f1f384 | 19 | */ |
IonSystems | 6:e64796f1f384 | 20 | |
IonSystems | 6:e64796f1f384 | 21 | DigitalOut colourBit1(p6); //The 3 bits below are select bits for the sorter. |
IonSystems | 6:e64796f1f384 | 22 | DigitalOut colourBit2(p7); |
IonSystems | 6:e64796f1f384 | 23 | DigitalOut colourBit3(p8); |
IonSystems | 9:8d78eb55ad5e | 24 | DigitalOut startDispense(p18); //A positive edge tells the FPGA to start dispensing. p9 |
IonSystems | 6:e64796f1f384 | 25 | /* |
IonSystems | 6:e64796f1f384 | 26 | 00 Dispense red chip |
IonSystems | 6:e64796f1f384 | 27 | 01 Dispense green chip |
IonSystems | 6:e64796f1f384 | 28 | 10 Dispense blue chip |
IonSystems | 6:e64796f1f384 | 29 | 11 Nothing |
IonSystems | 6:e64796f1f384 | 30 | */ |
IonSystems | 6:e64796f1f384 | 31 | |
IonSystems | 9:8d78eb55ad5e | 32 | DigitalOut dispenseBit1(p19); //The 2 bits below are select bits for the dispenser.p10 |
IonSystems | 9:8d78eb55ad5e | 33 | DigitalOut dispenseBit2(p20); //p11 |
IonSystems | 8:b7771391adc9 | 34 | |
IonSystems | 12:814a8fdbb6f7 | 35 | DigitalIn dispenseComplete(p11); //FPGA sets to 1 once complete until another signal is recieved. |
IonSystems | 10:8c0696b99692 | 36 | DigitalOut select(p15); //0 for control, 1 for maintenance. |
IonSystems | 10:8c0696b99692 | 37 | |
IonSystems | 12:814a8fdbb6f7 | 38 | |
IonSystems | 19:78d4b78fa736 | 39 | void setMaintenanceSelect(bool bit6, bool bit5, bool bit4, bool bit3, bool bit2, bool bit1) |
IonSystems | 19:78d4b78fa736 | 40 | { |
IonSystems | 12:814a8fdbb6f7 | 41 | startSort = bit1; |
IonSystems | 12:814a8fdbb6f7 | 42 | colourBit1 = bit2; |
IonSystems | 12:814a8fdbb6f7 | 43 | colourBit2 = bit3; |
IonSystems | 12:814a8fdbb6f7 | 44 | colourBit3 = bit4; |
IonSystems | 12:814a8fdbb6f7 | 45 | startDispense = bit5; |
IonSystems | 12:814a8fdbb6f7 | 46 | dispenseBit1 = bit6; |
IonSystems | 19:78d4b78fa736 | 47 | } |
IonSystems | 19:78d4b78fa736 | 48 | |
IonSystems | 19:78d4b78fa736 | 49 | void setMaintenanceStart(bool value) |
IonSystems | 19:78d4b78fa736 | 50 | { |
IonSystems | 12:814a8fdbb6f7 | 51 | dispenseBit2 = value; |
IonSystems | 19:78d4b78fa736 | 52 | } |
IonSystems | 19:78d4b78fa736 | 53 | void waitForMaintenanceComplete() |
IonSystems | 19:78d4b78fa736 | 54 | { |
IonSystems | 19:78d4b78fa736 | 55 | while(!sortComplete) { |
IonSystems | 23:f9e7e64784be | 56 | |
IonSystems | 19:78d4b78fa736 | 57 | } |
IonSystems | 23:f9e7e64784be | 58 | |
IonSystems | 19:78d4b78fa736 | 59 | } |
IonSystems | 19:78d4b78fa736 | 60 | |
IonSystems | 19:78d4b78fa736 | 61 | void setDispenseSelect(bool dBit1, bool dBit2) |
IonSystems | 19:78d4b78fa736 | 62 | { |
IonSystems | 19:78d4b78fa736 | 63 | dispenseBit1 = dBit1; |
IonSystems | 19:78d4b78fa736 | 64 | dispenseBit2 = dBit2; |
IonSystems | 12:814a8fdbb6f7 | 65 | } |
IonSystems | 19:78d4b78fa736 | 66 | |
IonSystems | 19:78d4b78fa736 | 67 | void setSortSelect(bool sBit1, bool sBit2, bool sBit3) |
IonSystems | 19:78d4b78fa736 | 68 | { |
IonSystems | 19:78d4b78fa736 | 69 | colourBit1 = sBit1; |
IonSystems | 19:78d4b78fa736 | 70 | colourBit2 = sBit2; |
IonSystems | 19:78d4b78fa736 | 71 | colourBit3 = sBit3; |
IonSystems | 19:78d4b78fa736 | 72 | } |
IonSystems | 19:78d4b78fa736 | 73 | |
IonSystems | 19:78d4b78fa736 | 74 | void maintain(StateMachine maintainState) |
IonSystems | 19:78d4b78fa736 | 75 | { |
IonSystems | 12:814a8fdbb6f7 | 76 | select = 1; //setting FPGA to maintenance mode. |
IonSystems | 19:78d4b78fa736 | 77 | setMaintenanceSelect(true,true,true,true,true,true); |
IonSystems | 19:78d4b78fa736 | 78 | switch(maintainState) { |
IonSystems | 12:814a8fdbb6f7 | 79 | case RB_LEFT: |
IonSystems | 12:814a8fdbb6f7 | 80 | setMaintenanceSelect(false,false,false,true,false,true); |
IonSystems | 19:78d4b78fa736 | 81 | break; |
IonSystems | 12:814a8fdbb6f7 | 82 | case RB_CENTRE: |
IonSystems | 12:814a8fdbb6f7 | 83 | setMaintenanceSelect(false,false,false,true,true,false); |
IonSystems | 19:78d4b78fa736 | 84 | break; |
IonSystems | 12:814a8fdbb6f7 | 85 | case RB_RIGHT: |
IonSystems | 12:814a8fdbb6f7 | 86 | setMaintenanceSelect(false,false,false,true,true,true); |
IonSystems | 19:78d4b78fa736 | 87 | break; |
IonSystems | 12:814a8fdbb6f7 | 88 | case GO_UP: |
IonSystems | 12:814a8fdbb6f7 | 89 | setMaintenanceSelect(false,false,true,false,false,false); |
IonSystems | 19:78d4b78fa736 | 90 | break; |
IonSystems | 12:814a8fdbb6f7 | 91 | case GO_CENTRE: |
IonSystems | 12:814a8fdbb6f7 | 92 | setMaintenanceSelect(false,false,true,false,false,true); |
IonSystems | 19:78d4b78fa736 | 93 | break; |
IonSystems | 12:814a8fdbb6f7 | 94 | case GO_DOWN: |
IonSystems | 12:814a8fdbb6f7 | 95 | setMaintenanceSelect(false,false,true,false,true,false); |
IonSystems | 19:78d4b78fa736 | 96 | break; |
IonSystems | 12:814a8fdbb6f7 | 97 | case BR_LEFT: |
IonSystems | 12:814a8fdbb6f7 | 98 | setMaintenanceSelect(false,false,true,false,true,true); |
IonSystems | 19:78d4b78fa736 | 99 | break; |
IonSystems | 12:814a8fdbb6f7 | 100 | case BR_RIGHT: |
IonSystems | 12:814a8fdbb6f7 | 101 | setMaintenanceSelect(false,false,true,true,false,false); |
IonSystems | 19:78d4b78fa736 | 102 | break; |
IonSystems | 12:814a8fdbb6f7 | 103 | case R_PUSH: |
IonSystems | 12:814a8fdbb6f7 | 104 | setMaintenanceSelect(false,false,true,true,false,true); |
IonSystems | 19:78d4b78fa736 | 105 | break; |
IonSystems | 22:8f11d1c178ab | 106 | case R_HOME: |
IonSystems | 22:8f11d1c178ab | 107 | setMaintenanceSelect(false,false,true,true,true,false); |
IonSystems | 22:8f11d1c178ab | 108 | break; |
IonSystems | 22:8f11d1c178ab | 109 | case GB_LEFT: |
IonSystems | 22:8f11d1c178ab | 110 | setMaintenanceSelect(false,false,true,true,true,true); |
IonSystems | 22:8f11d1c178ab | 111 | break; |
IonSystems | 22:8f11d1c178ab | 112 | case GB_CENTRE: |
IonSystems | 22:8f11d1c178ab | 113 | setMaintenanceSelect(false,true,false,false,false,false); |
IonSystems | 22:8f11d1c178ab | 114 | break; |
IonSystems | 22:8f11d1c178ab | 115 | case GB_RIGHT: |
IonSystems | 22:8f11d1c178ab | 116 | setMaintenanceSelect(false,true,false,false,false,true); |
IonSystems | 22:8f11d1c178ab | 117 | break; |
IonSystems | 22:8f11d1c178ab | 118 | case maintenanceEND: |
IonSystems | 22:8f11d1c178ab | 119 | setMaintenanceSelect(false,true,false,false,true,false); |
IonSystems | 23:f9e7e64784be | 120 | break; |
IonSystems | 14:31ba3e56c788 | 121 | default: |
IonSystems | 19:78d4b78fa736 | 122 | break; |
IonSystems | 19:78d4b78fa736 | 123 | } |
IonSystems | 23:f9e7e64784be | 124 | setMaintenanceStart(true); |
IonSystems | 12:814a8fdbb6f7 | 125 | waitForMaintenanceComplete(); |
IonSystems | 12:814a8fdbb6f7 | 126 | setMaintenanceStart(false); |
IonSystems | 23:f9e7e64784be | 127 | setMaintenanceSelect(true,false,true,true,true,false); |
IonSystems | 19:78d4b78fa736 | 128 | } |
IonSystems | 19:78d4b78fa736 | 129 | |
IonSystems | 19:78d4b78fa736 | 130 | void waitForDispenseComplete() |
IonSystems | 19:78d4b78fa736 | 131 | { |
IonSystems | 23:f9e7e64784be | 132 | while(!dispenseComplete) { |
IonSystems | 23:f9e7e64784be | 133 | |
IonSystems | 12:814a8fdbb6f7 | 134 | } |
IonSystems | 23:f9e7e64784be | 135 | |
IonSystems | 19:78d4b78fa736 | 136 | } |
IonSystems | 19:78d4b78fa736 | 137 | |
IonSystems | 19:78d4b78fa736 | 138 | void dispense(Colour colour) |
IonSystems | 19:78d4b78fa736 | 139 | { |
IonSystems | 23:f9e7e64784be | 140 | startDispense = false; |
IonSystems | 19:78d4b78fa736 | 141 | bool validDispense = false; |
IonSystems | 23:f9e7e64784be | 142 | select = 0; |
IonSystems | 19:78d4b78fa736 | 143 | setDispenseSelect(true,true); |
IonSystems | 23:f9e7e64784be | 144 | // A dispense operation can only take place if there are chips |
IonSystems | 23:f9e7e64784be | 145 | |
IonSystems | 19:78d4b78fa736 | 146 | switch(colour) { |
IonSystems | 19:78d4b78fa736 | 147 | case RED: |
IonSystems | 19:78d4b78fa736 | 148 | if(redAmount > 0 || operationMode == false) { |
IonSystems | 19:78d4b78fa736 | 149 | setDispenseSelect(false,false); |
IonSystems | 19:78d4b78fa736 | 150 | redAmount--; |
IonSystems | 19:78d4b78fa736 | 151 | validDispense = true; |
IonSystems | 19:78d4b78fa736 | 152 | } |
IonSystems | 10:8c0696b99692 | 153 | break; |
IonSystems | 19:78d4b78fa736 | 154 | case GREEN: |
IonSystems | 19:78d4b78fa736 | 155 | if(greenAmount > 0 || operationMode == false) { |
IonSystems | 19:78d4b78fa736 | 156 | setDispenseSelect(true,false); |
IonSystems | 19:78d4b78fa736 | 157 | greenAmount--; |
IonSystems | 19:78d4b78fa736 | 158 | validDispense = true; |
IonSystems | 19:78d4b78fa736 | 159 | } |
IonSystems | 10:8c0696b99692 | 160 | break; |
IonSystems | 19:78d4b78fa736 | 161 | case BLUE: |
IonSystems | 19:78d4b78fa736 | 162 | if(blueAmount > 0 || operationMode == false) { |
IonSystems | 19:78d4b78fa736 | 163 | setDispenseSelect(false,true); |
IonSystems | 12:814a8fdbb6f7 | 164 | blueAmount--; |
IonSystems | 12:814a8fdbb6f7 | 165 | validDispense = true; |
IonSystems | 19:78d4b78fa736 | 166 | } |
IonSystems | 10:8c0696b99692 | 167 | break; |
IonSystems | 19:78d4b78fa736 | 168 | } |
IonSystems | 23:f9e7e64784be | 169 | |
IonSystems | 23:f9e7e64784be | 170 | writeFile(redAmount,greenAmount,blueAmount,recycleAmount); |
IonSystems | 19:78d4b78fa736 | 171 | if(validDispense || operationMode == false) { |
IonSystems | 10:8c0696b99692 | 172 | startDispense = true; //set the startDispense line high. |
IonSystems | 19:78d4b78fa736 | 173 | waitForDispenseComplete(); |
IonSystems | 22:8f11d1c178ab | 174 | |
IonSystems | 10:8c0696b99692 | 175 | startDispense = false; |
IonSystems | 19:78d4b78fa736 | 176 | setDispenseSelect(true,true); |
IonSystems | 10:8c0696b99692 | 177 | } |
IonSystems | 19:78d4b78fa736 | 178 | } |
IonSystems | 19:78d4b78fa736 | 179 | void dispenseOrder(int r, int g, int b) |
IonSystems | 19:78d4b78fa736 | 180 | { |
IonSystems | 23:f9e7e64784be | 181 | //TODO: Check if there are enough chips of each colour to dispense the order. |
IonSystems | 23:f9e7e64784be | 182 | operationMode = false; |
IonSystems | 23:f9e7e64784be | 183 | for(int i = r; i > 0; i--) { |
IonSystems | 14:31ba3e56c788 | 184 | dispense(RED); |
IonSystems | 19:78d4b78fa736 | 185 | } |
IonSystems | 23:f9e7e64784be | 186 | for(int i = g; i > 0; i--) { |
IonSystems | 15:0c5f20e15b6a | 187 | dispense(GREEN); |
IonSystems | 19:78d4b78fa736 | 188 | } |
IonSystems | 23:f9e7e64784be | 189 | for(int i = b; i > 0; i--) { |
IonSystems | 15:0c5f20e15b6a | 190 | dispense(BLUE); |
IonSystems | 17:6a0bb0ad5bb4 | 191 | } |
IonSystems | 23:f9e7e64784be | 192 | operationMode = true; |
IonSystems | 19:78d4b78fa736 | 193 | } |
IonSystems | 19:78d4b78fa736 | 194 | |
IonSystems | 20:6de191ac7ff3 | 195 | void waitForSortComplete() |
IonSystems | 20:6de191ac7ff3 | 196 | { |
IonSystems | 23:f9e7e64784be | 197 | while(!sortComplete) {} |
IonSystems | 20:6de191ac7ff3 | 198 | } |
IonSystems | 20:6de191ac7ff3 | 199 | |
IonSystems | 20:6de191ac7ff3 | 200 | void lift() |
IonSystems | 20:6de191ac7ff3 | 201 | { |
IonSystems | 20:6de191ac7ff3 | 202 | select = 0; //Setting to operation mode just in case it has not been set. |
IonSystems | 20:6de191ac7ff3 | 203 | setSortSelect(true,true,true); |
IonSystems | 20:6de191ac7ff3 | 204 | setSortSelect(true,false,true); |
IonSystems | 20:6de191ac7ff3 | 205 | startSort = true; //set the startDispense line high. |
IonSystems | 23:f9e7e64784be | 206 | waitForSortComplete(); |
IonSystems | 23:f9e7e64784be | 207 | startSort = false; |
IonSystems | 23:f9e7e64784be | 208 | setSortSelect(true,true,true); |
IonSystems | 23:f9e7e64784be | 209 | } |
IonSystems | 20:6de191ac7ff3 | 210 | |
IonSystems | 20:6de191ac7ff3 | 211 | |
IonSystems | 20:6de191ac7ff3 | 212 | |
IonSystems | 19:78d4b78fa736 | 213 | void recycle() |
IonSystems | 19:78d4b78fa736 | 214 | { |
IonSystems | 19:78d4b78fa736 | 215 | select = 0; //Setting to operation mode just in case it has not been set. |
IonSystems | 19:78d4b78fa736 | 216 | setSortSelect(true,true,true); |
IonSystems | 19:78d4b78fa736 | 217 | setSortSelect(false,false,true); |
IonSystems | 22:8f11d1c178ab | 218 | recycleAmount++; |
IonSystems | 19:78d4b78fa736 | 219 | startSort = true; //set the startDispense line high. |
IonSystems | 20:6de191ac7ff3 | 220 | waitForSortComplete(); |
IonSystems | 19:78d4b78fa736 | 221 | startSort = false; |
IonSystems | 19:78d4b78fa736 | 222 | setSortSelect(true,true,true); |
IonSystems | 23:f9e7e64784be | 223 | if(recycleAmount >= 5) { |
IonSystems | 23:f9e7e64784be | 224 | wait(1); |
IonSystems | 23:f9e7e64784be | 225 | lift(); |
IonSystems | 23:f9e7e64784be | 226 | wait(1); |
IonSystems | 23:f9e7e64784be | 227 | recycleAmount = 0; |
IonSystems | 23:f9e7e64784be | 228 | } |
IonSystems | 19:78d4b78fa736 | 229 | } |
IonSystems | 19:78d4b78fa736 | 230 | |
IonSystems | 19:78d4b78fa736 | 231 | |
IonSystems | 19:78d4b78fa736 | 232 | |
IonSystems | 19:78d4b78fa736 | 233 | void sort(Colour colour) |
IonSystems | 19:78d4b78fa736 | 234 | { |
IonSystems | 19:78d4b78fa736 | 235 | if(colour == NONE) { |
IonSystems | 19:78d4b78fa736 | 236 | return; |
IonSystems | 19:78d4b78fa736 | 237 | } else { |
IonSystems | 23:f9e7e64784be | 238 | startSort = false; |
IonSystems | 10:8c0696b99692 | 239 | select = 0; //Setting to operation mode just in case it has not been set. |
IonSystems | 19:78d4b78fa736 | 240 | setSortSelect(true,true,true); |
IonSystems | 19:78d4b78fa736 | 241 | switch(colour) { |
IonSystems | 10:8c0696b99692 | 242 | case RED: |
IonSystems | 19:78d4b78fa736 | 243 | if(redAmount >= tubeSize) { |
IonSystems | 17:6a0bb0ad5bb4 | 244 | recycle(); |
IonSystems | 17:6a0bb0ad5bb4 | 245 | return; |
IonSystems | 19:78d4b78fa736 | 246 | } |
IonSystems | 19:78d4b78fa736 | 247 | setSortSelect(false,false,false); |
IonSystems | 12:814a8fdbb6f7 | 248 | redAmount++; |
IonSystems | 19:78d4b78fa736 | 249 | break; |
IonSystems | 10:8c0696b99692 | 250 | case GREEN: |
IonSystems | 19:78d4b78fa736 | 251 | if(greenAmount >= tubeSize) { |
IonSystems | 17:6a0bb0ad5bb4 | 252 | recycle(); |
IonSystems | 17:6a0bb0ad5bb4 | 253 | return; |
IonSystems | 19:78d4b78fa736 | 254 | } |
IonSystems | 19:78d4b78fa736 | 255 | setSortSelect(true,false,false); |
IonSystems | 12:814a8fdbb6f7 | 256 | greenAmount++; |
IonSystems | 19:78d4b78fa736 | 257 | break; |
IonSystems | 10:8c0696b99692 | 258 | case BLUE: |
IonSystems | 19:78d4b78fa736 | 259 | if(blueAmount >= tubeSize) { |
IonSystems | 17:6a0bb0ad5bb4 | 260 | recycle(); |
IonSystems | 17:6a0bb0ad5bb4 | 261 | return; |
IonSystems | 19:78d4b78fa736 | 262 | } |
IonSystems | 19:78d4b78fa736 | 263 | setSortSelect(false,true,false); |
IonSystems | 12:814a8fdbb6f7 | 264 | blueAmount++; |
IonSystems | 19:78d4b78fa736 | 265 | break; |
IonSystems | 11:06f6e82b40a8 | 266 | case BIN: |
IonSystems | 19:78d4b78fa736 | 267 | setSortSelect(true,true,false); |
IonSystems | 19:78d4b78fa736 | 268 | break; |
IonSystems | 10:8c0696b99692 | 269 | } |
IonSystems | 23:f9e7e64784be | 270 | startSort = true; |
IonSystems | 19:78d4b78fa736 | 271 | waitForSortComplete(); |
IonSystems | 19:78d4b78fa736 | 272 | startSort = false; |
IonSystems | 19:78d4b78fa736 | 273 | setSortSelect(true,true,true); |
IonSystems | 23:f9e7e64784be | 274 | writeFile(redAmount,greenAmount,blueAmount,recycleAmount); |
IonSystems | 23:f9e7e64784be | 275 | wait(0.3); |
IonSystems | 19:78d4b78fa736 | 276 | } |
IonSystems | 12:814a8fdbb6f7 | 277 | } |
IonSystems | 12:814a8fdbb6f7 | 278 | |
IonSystems | 19:78d4b78fa736 | 279 | |
IonSystems | 19:78d4b78fa736 | 280 | |
IonSystems | 19:78d4b78fa736 | 281 | void dispenseAll() |
IonSystems | 19:78d4b78fa736 | 282 | { |
IonSystems | 23:f9e7e64784be | 283 | dispenseOrder(redAmount,greenAmount,blueAmount); |
IonSystems | 23:f9e7e64784be | 284 | redAmount=0; blueAmount=0; greenAmount=0; |
IonSystems | 23:f9e7e64784be | 285 | writeFile(redAmount,greenAmount,blueAmount,recycleAmount); |
IonSystems | 19:78d4b78fa736 | 286 | } |