![](/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
Diff: main.cpp
- Revision:
- 24:8868101d01d0
- Parent:
- 23:f9e7e64784be
- Child:
- 27:47a7bc902587
--- a/main.cpp Sat Nov 29 16:32:57 2014 +0000 +++ b/main.cpp Wed Dec 03 17:49:41 2014 +0000 @@ -3,6 +3,7 @@ #include "mbed.h" Serial pc(USBTX, USBRX); +Serial glcd(p13, p14); #include "rtos.h" #include "Language.h" DigitalIn languageButton(p30); @@ -34,11 +35,10 @@ bool testFunctions = true; bool serialComms = true; -int mode = 5; +int mode = 0; int maintenanceModeSelect = 0; -Colour lastColour = NONE; -Colour colourStore = NONE; + bool buttonPressed(int buttonNumber) { @@ -52,8 +52,7 @@ { if(buttonPressed(3)) { - mode++; - if(mode >= 7) mode = 0; + mode = !mode; } /* @@ -72,48 +71,48 @@ } } *//*else if(mode == 1) { //Sort Mode - printLCD("Sort Mode"); - operationMode=false; - if(buttonPressed(0)) { - log("Red Button pressed"); - sort(RED); - } else if(buttonPressed(1)) { - log("Green Button pressed"); - sort(GREEN); - } else if(buttonPressed(2)) { - log("Blue Button pressed"); - sort(BLUE); - } +printLCD("Sort Mode"); +operationMode=false; +if(buttonPressed(0)) { +log("Red Button pressed"); +sort(RED); +} else if(buttonPressed(1)) { +log("Green Button pressed"); +sort(GREEN); +} else if(buttonPressed(2)) { +log("Blue Button pressed"); +sort(BLUE); +} - }*//* else if(mode == 2) { //Bin/Recycle Mode - printLCD("Bin/Recycle Mode"); - operationMode=false; - if(buttonPressed(0)) { - log("Bin button pressed"); - sort(BIN); - } else if(buttonPressed(1)) { - log("Recycle Button"); - recycle(); - } - } */ +}*//* else if(mode == 2) { //Bin/Recycle Mode +printLCD("Bin/Recycle Mode"); +operationMode=false; +if(buttonPressed(0)) { +log("Bin button pressed"); +sort(BIN); +} else if(buttonPressed(1)) { +log("Recycle Button"); +recycle(); +} +} */ /*else if(mode == 4) { //Card Reader Mode operationMode= true; if(cardReader && cardDetect) { cardAcquisition(); }*/ - if(mode == 5) { //Storage Mode + if(mode == 0) { //Storage Mode operationMode=true; stringstream ss; - ss << "Stored Values " << "R:" << redAmount << "G:" << greenAmount << "B:" << blueAmount << "R:" << recycleAmount; + ss << "Operation: " << "R:" << redAmount << "G:" << greenAmount << "B:" << blueAmount << "RE:" << recycleAmount; string tmp = ss.str(); printLCD(tmp.c_str()); wait(0.1); if(buttonPressed(0)) { - log("Dispense all button pressed"); + //log("Dispense all button pressed"); dispenseAll(); } else if(buttonPressed(1)) { - log("Clear stored numbers button pressed"); + //log("Clear stored numbers button pressed"); writeFile(0,0,0,recycleAmount); redAmount = 0; greenAmount = 0; @@ -121,7 +120,7 @@ printLCD("Cleared values"); } else if(buttonPressed(2)) { - log("Increment all stored values by 2."); + //log("Increment all stored values by 2."); redAmount += 2; greenAmount += 2; blueAmount += 2; @@ -130,7 +129,7 @@ printLCD("set values to 2"); } - } else if(mode == 6) { + } else if(mode == 1) { operationMode = false; stringstream ss; ss << maintenanceModeSelect; @@ -236,37 +235,36 @@ void sortFunction() { - Colour colour = readColourSensor(); + Thread::wait(100); + Colour colour = readColourSensor(); + Thread::wait(100); Colour colour2 = readColourSensor(); if(colour != colour2) { return; } - - if(colour == BIN) { + if(colour == BIN) { + Thread::wait(100); Colour binCheck = readColourSensor(); if(binCheck != colour) { return; } } - lastColour = colourStore; - colourStore = colour; /* Tries to prevent a colour mis-read. When a chip is read as NONE, the chip may be falling down the tube as the reading was taken. The chip may also be out of position on the slider. Ignoring the next reading is trying to prevent a mis-read being valid.*/ - if(lastColour == NONE) { - return; - } - if(sorter) sort(colour); + //log("Sorting colour"); + sort(colour); } void sortMode(void const* args) { - if(operationMode) { - while(true && colourSensor && sorter) { + + while(colourSensor && sorter) { + if(operationMode) { led2 = 1; sortFunction(); led2 = 0; @@ -282,6 +280,10 @@ pcReady = true; } } else { + //Check language button + if(languageButton) { + changeLanguage(); + } if(cardReader && cardDetect) { //Read card int cardNumber = cardAcquisition(); @@ -289,33 +291,69 @@ //Send message to pc pc.putc('*'); wait(0.5); -//log("sending card number to pc"); + + //Send card number to pc pc.printf("%i\n",cardNumber); + int red = 0; int green = 0; int blue = 0; -//log("getting rgb from pc"); + + //Get order from pc red = (int) pc.getc() - '0'; green = (int) pc.getc() - '0'; blue = (int) pc.getc() - '0'; -//log("recieved from pc"); -//log("dispensing order"); - dispenseOrder(red,green,blue); -//log("order dispensed"); - pcReady = false; - pc.putc('('); + + //Check if there are enough chips to dispense order + Thread::wait(100); + if(red > redAmount || green > greenAmount || blue > blueAmount) { + //If not then do not dispense + pc.putc('<'); + pcReady = false; + } else if(red ==0 && green == 0 && blue ==0){ + pc.putc('@'); + pcReady = false; + }else { + //Otherwise there are enough tokens so dispense the order + pc.putc('>'); + + //Display order on LCD + glcd.putc(27); + glcd.putc(18); + glcd.putc(red + '0'); + glcd.putc(green + '0'); + glcd.putc(blue + '0'); + glcd.putc(255); + + //Dispense Order + dispenseOrder(red,green,blue); + + //Display finished dispensing on lcd + glcd.putc(27); + glcd.putc(19); + glcd.putc(255); + + //Reset pc ready + pcReady = false; + wait(1); + pc.putc('('); + } + } } } void dispenseMode(void const* args) { - if(operationMode) { - while(true) { + + while(true) { + if(operationMode) { led3 = 1; dispenseFunction(); led3 =0; + Thread::wait(50); } + } } @@ -325,31 +363,36 @@ led1 = 0; setupLCD(); printLCD("Welcome to the Chipin Sorter"); - wait(1); + Thread::wait(1000); readChipNumbers(); printStoredChipValues(); printLCD("Resetting all sliders"); maintain(maintenanceEND); - wait(1); + printLCD(""); + Thread::wait(1000); + sendLanguageCharacter(); Thread sortThread(sortMode); - Thread dispenseThread(dispenseMode); - - +if(!languageButton){ + operationMode = true; + mode = 0; + +}else{ + operationMode = false; + mode = 1; + } +Thread dispenseThread(dispenseMode); while(true) { led1 = 1; led4 = cardDetect; - if(serialComms) { + if(!operationMode) { checkSerial(); } - if(testFunctions) checkSortDispenseButtons(); - - if(languageButton) changeLanguage(); - + Thread::wait(100); led1=0; - //Thread::wait(50); + Thread::wait(100); } }