Ion Systems / Mbed 2 deprecated Chipin_Main

Dependencies:   MCP23017 TCS3472_I2C WattBob_TextLCD mbed-rtos mbed

Committer:
IonSystems
Date:
Thu Nov 20 22:02:54 2014 +0000
Revision:
14:31ba3e56c788
Parent:
13:0661d658d9d1
Child:
15:0c5f20e15b6a
Euan broke dispense mode

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