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:
Wed Oct 29 09:20:19 2014 +0000
Revision:
0:8d54ffcf256e
Child:
1:a8a01df48d1a
Added comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IonSystems 0:8d54ffcf256e 1 #include "mbed.h"
IonSystems 0:8d54ffcf256e 2 #include "MCP23017.h"
IonSystems 0:8d54ffcf256e 3 #include "WattBob_TextLCD.h"
IonSystems 0:8d54ffcf256e 4 #include "TCS3472_I2C.h"
IonSystems 0:8d54ffcf256e 5
IonSystems 0:8d54ffcf256e 6 //This LED is on when the servo is rotating clockwise.
IonSystems 0:8d54ffcf256e 7 DigitalOut clockwiseLED(LED1);
IonSystems 0:8d54ffcf256e 8 //This LED is on when the servo is rotating anti-clockwise.
IonSystems 0:8d54ffcf256e 9 DigitalOut anticlockwiseLED(LED2);
IonSystems 0:8d54ffcf256e 10 //This is the control line that tells the FPGA to move the servo clockwise.
IonSystems 0:8d54ffcf256e 11 DigitalOut clockwise(p22);
IonSystems 0:8d54ffcf256e 12 //This is the control line that tells the FPGA to move the servo anti-clockwise.
IonSystems 0:8d54ffcf256e 13 DigitalOut anticlockwise(p21);
IonSystems 0:8d54ffcf256e 14
IonSystems 0:8d54ffcf256e 15 //Boolean values to easily enable and disable certain features for individual testing
IonSystems 0:8d54ffcf256e 16 bool colourSensor = true;
IonSystems 0:8d54ffcf256e 17 bool cardReader = true;
IonSystems 0:8d54ffcf256e 18 bool sorter = true;
IonSystems 0:8d54ffcf256e 19 bool dispensor = true;
IonSystems 0:8d54ffcf256e 20
IonSystems 0:8d54ffcf256e 21 /*
IonSystems 0:8d54ffcf256e 22 There are two high level states representing the two main
IonSystems 0:8d54ffcf256e 23 functionalities of the MBED: Maintenance mode and Operation mode.
IonSystems 0:8d54ffcf256e 24
IonSystems 0:8d54ffcf256e 25 When operationMode is true, the MBED is in operation mode.
IonSystems 0:8d54ffcf256e 26 When operationMode is false, the MBED is in maintenance mode.
IonSystems 0:8d54ffcf256e 27 */
IonSystems 0:8d54ffcf256e 28
IonSystems 0:8d54ffcf256e 29 bool operationMode = true; //the MBED starts in operation mode.
IonSystems 0:8d54ffcf256e 30
IonSystems 0:8d54ffcf256e 31 #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT)
IonSystems 0:8d54ffcf256e 32 #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT)
IonSystems 0:8d54ffcf256e 33
IonSystems 0:8d54ffcf256e 34 //Creating all the global variables.
IonSystems 0:8d54ffcf256e 35 MCP23017 *par_port;
IonSystems 0:8d54ffcf256e 36 WattBob_TextLCD *lcd;
IonSystems 0:8d54ffcf256e 37
IonSystems 0:8d54ffcf256e 38 Serial pc(USBTX, USBRX);
IonSystems 0:8d54ffcf256e 39
IonSystems 0:8d54ffcf256e 40 //Setup colour sensor variables
IonSystems 0:8d54ffcf256e 41 int rgb_readings[4];
IonSystems 0:8d54ffcf256e 42 double rMax = 9244;
IonSystems 0:8d54ffcf256e 43 double gMax = 3194;
IonSystems 0:8d54ffcf256e 44 double bMax = 3590;
IonSystems 0:8d54ffcf256e 45 TCS3472_I2C rgb_sensor(p28, p27);
IonSystems 0:8d54ffcf256e 46
IonSystems 0:8d54ffcf256e 47 //setup Card Reader stuff
IonSystems 0:8d54ffcf256e 48 DigitalIn cardBit1(p21);
IonSystems 0:8d54ffcf256e 49 DigitalIn cardBit2(p22);
IonSystems 0:8d54ffcf256e 50 DigitalIn cardBit3(p23);
IonSystems 0:8d54ffcf256e 51 DigitalIn cardBit4(p24);
IonSystems 0:8d54ffcf256e 52 DigitalIn cardBit5(p25);
IonSystems 0:8d54ffcf256e 53 DigitalIn cardBit6(p26);
IonSystems 0:8d54ffcf256e 54
IonSystems 0:8d54ffcf256e 55 //Setup pins to FPGA
IonSystems 0:8d54ffcf256e 56 DigitalOut startSort(p5); //A positive edge tells the FPGA to start sorting.
IonSystems 0:8d54ffcf256e 57 /* The command codes for the three sort select bits(ColourBit1 - ColourBit2)
IonSystems 0:8d54ffcf256e 58 000 Sort red chip
IonSystems 0:8d54ffcf256e 59 001 Sort green chip
IonSystems 0:8d54ffcf256e 60 010 Sort blue chip
IonSystems 0:8d54ffcf256e 61 011 Bin the chip
IonSystems 0:8d54ffcf256e 62 100 Recycle the chip
IonSystems 0:8d54ffcf256e 63 101 Nothing
IonSystems 0:8d54ffcf256e 64 110 Nothing
IonSystems 0:8d54ffcf256e 65 111 Nothing
IonSystems 0:8d54ffcf256e 66 */
IonSystems 0:8d54ffcf256e 67 DigitalOut ColourBit1(p6); //The 3 bits below are select bits for the sorter.
IonSystems 0:8d54ffcf256e 68 DigitalOut ColourBit2(p7);
IonSystems 0:8d54ffcf256e 69 DigitalOut ColourBit3(p8);
IonSystems 0:8d54ffcf256e 70 DigitalOut startDispense(p9); //A positive edge tells the FPGA to start dispensing.
IonSystems 0:8d54ffcf256e 71 /*
IonSystems 0:8d54ffcf256e 72 00 Dispense red chip
IonSystems 0:8d54ffcf256e 73 01 Dispense green chip
IonSystems 0:8d54ffcf256e 74 10 Dispense blue chip
IonSystems 0:8d54ffcf256e 75 11 Nothing
IonSystems 0:8d54ffcf256e 76 */
IonSystems 0:8d54ffcf256e 77 DigitalOut DispenseBit1(p10); //The 2 bits below are select bits for the dispenser.
IonSystems 0:8d54ffcf256e 78 DigitalOut DispenseBit2(p11);
IonSystems 0:8d54ffcf256e 79
IonSystems 0:8d54ffcf256e 80
IonSystems 0:8d54ffcf256e 81 //Functions go here
IonSystems 0:8d54ffcf256e 82 void sendCharacter(char ch){
IonSystems 0:8d54ffcf256e 83 pc.putc(ch);
IonSystems 0:8d54ffcf256e 84 }
IonSystems 0:8d54ffcf256e 85
IonSystems 0:8d54ffcf256e 86 void printLCD(char * text){
IonSystems 0:8d54ffcf256e 87 lcd->reset();
IonSystems 0:8d54ffcf256e 88 lcd->printf(text);
IonSystems 0:8d54ffcf256e 89 }
IonSystems 0:8d54ffcf256e 90
IonSystems 0:8d54ffcf256e 91 char valueToChar(int value){
IonSystems 0:8d54ffcf256e 92 if(!value)
IonSystems 0:8d54ffcf256e 93 return '0';
IonSystems 0:8d54ffcf256e 94 else return '1';
IonSystems 0:8d54ffcf256e 95 }
IonSystems 0:8d54ffcf256e 96 /* readBit(int bitNumber):
IonSystems 0:8d54ffcf256e 97 Takes a bit number between 1 and 6 inclusive.
IonSystems 0:8d54ffcf256e 98 Returns the value of the cardBit associated with the bit number as a char.
IonSystems 0:8d54ffcf256e 99 */
IonSystems 0:8d54ffcf256e 100 char readBit(int bitNumber){
IonSystems 0:8d54ffcf256e 101 int value = 0;
IonSystems 0:8d54ffcf256e 102 switch(bitNumber){
IonSystems 0:8d54ffcf256e 103 case 1:
IonSystems 0:8d54ffcf256e 104 value = cardBit1;
IonSystems 0:8d54ffcf256e 105 break;
IonSystems 0:8d54ffcf256e 106 case 2:
IonSystems 0:8d54ffcf256e 107 value = cardBit2;
IonSystems 0:8d54ffcf256e 108 break;
IonSystems 0:8d54ffcf256e 109 case 3:
IonSystems 0:8d54ffcf256e 110 value = cardBit3;
IonSystems 0:8d54ffcf256e 111 break;
IonSystems 0:8d54ffcf256e 112 case 4:
IonSystems 0:8d54ffcf256e 113 value = cardBit4;
IonSystems 0:8d54ffcf256e 114 break;
IonSystems 0:8d54ffcf256e 115 case 5:
IonSystems 0:8d54ffcf256e 116 value = cardBit5;
IonSystems 0:8d54ffcf256e 117 break;
IonSystems 0:8d54ffcf256e 118 case 6:
IonSystems 0:8d54ffcf256e 119 value = cardBit6;
IonSystems 0:8d54ffcf256e 120 break;
IonSystems 0:8d54ffcf256e 121 }
IonSystems 0:8d54ffcf256e 122 return valueToChar(value);
IonSystems 0:8d54ffcf256e 123 }
IonSystems 0:8d54ffcf256e 124 /*
IonSystems 0:8d54ffcf256e 125 The processMessage function has two case statements: one for maintenance mode and one for operation mode.
IonSystems 0:8d54ffcf256e 126 */
IonSystems 0:8d54ffcf256e 127
IonSystems 0:8d54ffcf256e 128 void processMessage(char c){
IonSystems 0:8d54ffcf256e 129 if(!operation){ switch(c){
IonSystems 0:8d54ffcf256e 130 case 'a' : //servo 1 clockwise
IonSystems 0:8d54ffcf256e 131 sendCharacter('a'); //bounce back for acknowledgement
IonSystems 0:8d54ffcf256e 132 printLCD("S1 clockwise");
IonSystems 0:8d54ffcf256e 133 break;
IonSystems 0:8d54ffcf256e 134 case 'b' : //servo 1 anticlockwise
IonSystems 0:8d54ffcf256e 135 sendCharacter('b'); //bounce back for acknowledgement
IonSystems 0:8d54ffcf256e 136 printLCD("S1 anticlockwise");
IonSystems 0:8d54ffcf256e 137 break;
IonSystems 0:8d54ffcf256e 138 case 'c':
IonSystems 0:8d54ffcf256e 139 sendCharacter('c'); //bounce back for acknowledgement
IonSystems 0:8d54ffcf256e 140 printLCD("S1 stop");
IonSystems 0:8d54ffcf256e 141 break;
IonSystems 0:8d54ffcf256e 142 case 'C':// Read card command
IonSystems 0:8d54ffcf256e 143 sendCharacter('R'); //Notify PC that command has been recieved
IonSystems 0:8d54ffcf256e 144 printLCD("Reading Card");
IonSystems 0:8d54ffcf256e 145 //read card here
IonSystems 0:8d54ffcf256e 146 char value = ' ';
IonSystems 0:8d54ffcf256e 147 for(int num = 1;num <= 6;num++){
IonSystems 0:8d54ffcf256e 148 value = readBit(num);
IonSystems 0:8d54ffcf256e 149 // sendCharacter('R');
IonSystems 0:8d54ffcf256e 150 sendCharacter(value);
IonSystems 0:8d54ffcf256e 151 printLCD("Bit read");
IonSystems 0:8d54ffcf256e 152 //wait(0.1);
IonSystems 0:8d54ffcf256e 153 }
IonSystems 0:8d54ffcf256e 154 sendCharacter('C'); //Tells PC that the card has been read
IonSystems 0:8d54ffcf256e 155 printLCD("Card read success");
IonSystems 0:8d54ffcf256e 156 break;
IonSystems 0:8d54ffcf256e 157 case 'X':
IonSystems 0:8d54ffcf256e 158 sendCharacter('X'); //Notify the PC that this is an MBED
IonSystems 0:8d54ffcf256e 159 printLCD("Connected to PC");
IonSystems 0:8d54ffcf256e 160 break;
IonSystems 0:8d54ffcf256e 161 }
IonSystems 0:8d54ffcf256e 162 }
IonSystems 0:8d54ffcf256e 163 else if (operation){
IonSystems 0:8d54ffcf256e 164 switch(c){
IonSystems 0:8d54ffcf256e 165 case 'a':// Start sorting
IonSystems 0:8d54ffcf256e 166 sendCharacter('A'); //Confirm to PC that we are processing the instruction.
IonSystems 0:8d54ffcf256e 167 startSort = true;
IonSystems 0:8d54ffcf256e 168 wait(0.1);
IonSystems 0:8d54ffcf256e 169 startSort = false;
IonSystems 0:8d54ffcf256e 170 break;
IonSystems 0:8d54ffcf256e 171 }
IonSystems 0:8d54ffcf256e 172 }
IonSystems 0:8d54ffcf256e 173 }
IonSystems 0:8d54ffcf256e 174
IonSystems 0:8d54ffcf256e 175 void colourSensorTick(){
IonSystems 0:8d54ffcf256e 176 rgb_sensor.getAllColors(rgb_readings);
IonSystems 0:8d54ffcf256e 177
IonSystems 0:8d54ffcf256e 178 double redd = (rgb_readings[1] /gMax) * 255;
IonSystems 0:8d54ffcf256e 179 double greend = (rgb_readings[2] /bMax) * 255;
IonSystems 0:8d54ffcf256e 180 double blued = (rgb_readings[0] /rMax) * 255;
IonSystems 0:8d54ffcf256e 181
IonSystems 0:8d54ffcf256e 182 int red = redd;
IonSystems 0:8d54ffcf256e 183 int green = greend;
IonSystems 0:8d54ffcf256e 184 int blue = blued;
IonSystems 0:8d54ffcf256e 185
IonSystems 0:8d54ffcf256e 186 lcd->cls(); // clear display
IonSystems 0:8d54ffcf256e 187 lcd->locate(0,0);
IonSystems 0:8d54ffcf256e 188 lcd->printf("R:%d G:%d B:%d",red,green,blue);
IonSystems 0:8d54ffcf256e 189
IonSystems 0:8d54ffcf256e 190 lcd->locate(1,0);
IonSystems 0:8d54ffcf256e 191 if(red > 55){
IonSystems 0:8d54ffcf256e 192 lcd->printf("RED");
IonSystems 0:8d54ffcf256e 193 }
IonSystems 0:8d54ffcf256e 194 if(green > 55){
IonSystems 0:8d54ffcf256e 195 lcd->printf("GREEN");
IonSystems 0:8d54ffcf256e 196 }
IonSystems 0:8d54ffcf256e 197 if(red < 30 && green > 30 && blue > 30){
IonSystems 0:8d54ffcf256e 198 lcd->printf("BLUE");
IonSystems 0:8d54ffcf256e 199 }
IonSystems 0:8d54ffcf256e 200 }
IonSystems 0:8d54ffcf256e 201
IonSystems 0:8d54ffcf256e 202 int main() {
IonSystems 0:8d54ffcf256e 203 //Setting up all the global variables
IonSystems 0:8d54ffcf256e 204 par_port = new MCP23017(p9, p10, 0x40);
IonSystems 0:8d54ffcf256e 205 par_port->config(0x0F00, 0x0F00, 0x0F00); // configure MCP23017 chip on WattBob
IonSystems 0:8d54ffcf256e 206 BACK_LIGHT_ON(par_port);
IonSystems 0:8d54ffcf256e 207 lcd = new WattBob_TextLCD(par_port);
IonSystems 0:8d54ffcf256e 208 rgb_sensor.enablePowerAndRGBC();
IonSystems 0:8d54ffcf256e 209 rgb_sensor.setIntegrationTime(100);
IonSystems 0:8d54ffcf256e 210 //Writing initial text on LCD
IonSystems 0:8d54ffcf256e 211 lcd->printf("Welcome to the");
IonSystems 0:8d54ffcf256e 212 lcd->locate(1,0); //Going to new line
IonSystems 0:8d54ffcf256e 213 lcd->printf("Chipin Sorter");
IonSystems 0:8d54ffcf256e 214 wait(1); //Wait 1 second
IonSystems 0:8d54ffcf256e 215 lcd->reset(); //Clear LCD
IonSystems 0:8d54ffcf256e 216
IonSystems 0:8d54ffcf256e 217 while(1){
IonSystems 0:8d54ffcf256e 218 char c = pc.getc();
IonSystems 0:8d54ffcf256e 219 processMessage(c);
IonSystems 0:8d54ffcf256e 220 }
IonSystems 0:8d54ffcf256e 221
IonSystems 0:8d54ffcf256e 222
IonSystems 0:8d54ffcf256e 223 /*
IonSystems 0:8d54ffcf256e 224 if(par_port->read_bit(8)){
IonSystems 0:8d54ffcf256e 225 lcd->printf("ON");
IonSystems 0:8d54ffcf256e 226 pc.printf("btn1");
IonSystems 0:8d54ffcf256e 227 wait(0.5);
IonSystems 0:8d54ffcf256e 228 lcd->reset();
IonSystems 0:8d54ffcf256e 229 }else{
IonSystems 0:8d54ffcf256e 230 lcd->printf("OFF");
IonSystems 0:8d54ffcf256e 231 pc.printf("btn0");
IonSystems 0:8d54ffcf256e 232 wait(0.5);
IonSystems 0:8d54ffcf256e 233 lcd->reset();
IonSystems 0:8d54ffcf256e 234 }
IonSystems 0:8d54ffcf256e 235
IonSystems 0:8d54ffcf256e 236 */
IonSystems 0:8d54ffcf256e 237 }