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

Revision:
23:f9e7e64784be
Parent:
22:8f11d1c178ab
Child:
24:8868101d01d0
--- a/main.cpp	Tue Nov 25 22:32:26 2014 +0000
+++ b/main.cpp	Sat Nov 29 16:32:57 2014 +0000
@@ -1,12 +1,15 @@
 bool operationMode      = true; //the MBED starts in operation mode.
+
+
 #include "mbed.h"
+Serial pc(USBTX, USBRX);
+#include "rtos.h"
 #include "Language.h"
 DigitalIn languageButton(p30);
 Language currentLanguage = ENGLISH;
 #include <sstream>
 #include <string>
 #include "mbedStorage.h"
-#include "rgbLED.h"
 #include "mbedLCD.h"
 #include "stateMachine.h"
 #include "FPGAcomms.h"
@@ -14,7 +17,12 @@
 #define serialComm
 #include "serialCommunication.h"
 #endif
-#include "testFunctions.h"
+
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
 
 StateMachine stateMachine;
 
@@ -26,251 +34,329 @@
 bool testFunctions  =   true;
 bool serialComms    =   true;
 
-int mode = 4;
+int mode = 5;
 int maintenanceModeSelect = 0;
 
 Colour lastColour = NONE;
 Colour colourStore = NONE;
 
-bool buttonPressed(int buttonNumber){
-    if(buttonNumber > -1 && buttonNumber < 4){
+bool buttonPressed(int buttonNumber)
+{
+    if(buttonNumber > -1 && buttonNumber < 4) {
         return par_port->read_bit(buttonNumber + 8);
     }
     return false;
 }
-    
-void checkSortDispenseButtons(){
-        
-        if(buttonPressed(3)){
-            mode++;
-            if(mode >= 7) mode = 0;
-        }
-        
-        
-        if(mode == 0){                                     //Dispense Mode
+
+void checkSortDispenseButtons()
+{
+
+    if(buttonPressed(3)) {
+        mode++;
+        if(mode >= 7) mode = 0;
+    }
+
+    /*
+        if(mode == 0) {                                    //Dispense Mode
             printLCD("Dispense Mode");
             operationMode=false;
-            if(buttonPressed(0)){
+            if(buttonPressed(0)) {
                 printLCD("Red Button pressed");
                 dispense(RED);
-            }else if(buttonPressed(1)){
+            } else if(buttonPressed(1)) {
                 printLCD("Green Button pressed");
                 dispense(GREEN);
-            }else if(buttonPressed(2)){
+            } else if(buttonPressed(2)) {
                 printLCD("Blue Button pressed");
                 dispense(BLUE);
             }
-        }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);
-            }
-            
-        }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 == 3){  
-        printLCD("Colour Sensor Mode");                              //Colour Sensing Mode
-            if(colourSensor){
-                operationMode=true;
-                wait(0.2);   
-                Colour colour = readColourSensor();  
-                Colour colour2 = readColourSensor();
-                if(colour != colour2){
-                    return;
-                }
-                if(colour == BIN){ //Make tripley sure that it is actuall a bin chip
-                    printLCD("BIN CHECK");
-                    wait(0.2);
-                    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;
-                }
-                //wait(5);
-                if(sorter) sort(colour);  
-                            //Put (sorter && colour != BIN) in if statement.
-            }
-            //if
-        }else if(mode == 4){                                //Card Reader Mode
-            operationMode= true;
-            if(cardReader && cardDetect){
-                cardAcquisition();
-                
-            }
-        }else if(mode == 5){                                //Storage Mode
-            operationMode=true;
-            stringstream ss;
-            ss << "Stored Values   " << "R:" << redAmount << "G:" << greenAmount << "B:" << blueAmount;
-            string tmp = ss.str();
-            printLCD(tmp.c_str());
-            wait(0.1);
-            if(buttonPressed(0)){
-                log("Dispense all button pressed");
-                dispenseAll();
-            }else if(buttonPressed(1)){
-                log("Clear stored numbers button pressed");
-                writeFile(0,0,0);
-                redAmount = 0;greenAmount = 0;blueAmount = 0;
-                printLCD("Cleared values");
-                
-            }else if(buttonPressed(2)){
-                log("Increment all stored values by 2.");
-                redAmount += 2;greenAmount += 2;blueAmount += 2;
-                writeFile(redAmount,greenAmount,blueAmount);
-                
-                printLCD("set values to 2");
-                
-            }
-        }else if(mode == 6){ 
+
+        } *//*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);
+        }
+
+    }*//* 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
+        operationMode=true;
+        stringstream ss;
+        ss << "Stored Values   " << "R:" << redAmount << "G:" << greenAmount << "B:" << blueAmount << "R:" << recycleAmount;
+        string tmp = ss.str();
+        printLCD(tmp.c_str());
+        wait(0.1);
+        if(buttonPressed(0)) {
+            log("Dispense all button pressed");
+            dispenseAll();
+        } else if(buttonPressed(1)) {
+            log("Clear stored numbers button pressed");
+            writeFile(0,0,0,recycleAmount);
+            redAmount = 0;
+            greenAmount = 0;
+            blueAmount = 0;
+            printLCD("Cleared values");
+
+        } else if(buttonPressed(2)) {
+            log("Increment all stored values by 2.");
+            redAmount += 2;
+            greenAmount += 2;
+            blueAmount += 2;
+            writeFile(redAmount,greenAmount,blueAmount,recycleAmount);
+
+            printLCD("set values to 2");
+
+        }
+    } else if(mode == 6) {
         operationMode = false;
         stringstream ss;
         ss << maintenanceModeSelect;
         string mainModeSelect = ss.str();
-        printLCD("Maintenance: " + mainModeSelect);   
+        printLCD("Maintenance: " + mainModeSelect);
         wait(0.1);                           //Maintenance Mode
         //printLCD("Maintenance Mode");
-            if(buttonPressed(0)){                      //Next maintenance function
+        if(buttonPressed(0)) {                     //Next maintenance function
             maintenanceModeSelect++;
-            if(maintenanceModeSelect > 13) maintenanceModeSelect = 0;    
-            }else if(buttonPressed(1)){                //Previous maintenance function
+            if(maintenanceModeSelect > 13) maintenanceModeSelect = 0;
+        } else if(buttonPressed(1)) {               //Previous maintenance function
             maintenanceModeSelect--;
-            if(maintenanceModeSelect < 0) maintenanceModeSelect = 13;    
-            }else if(buttonPressed(2)){               //Process Instruction
-                switch(maintenanceModeSelect){
-                    case 0://RB_LEFT:
-                        printLCD("Moving Red/Blue Slider left.");
-                        maintain(RB_LEFT);
+            if(maintenanceModeSelect < 0) maintenanceModeSelect = 13;
+        } else if(buttonPressed(2)) {              //Process Instruction
+            switch(maintenanceModeSelect) {
+                case 0://RB_LEFT:
+                    printLCD("Moving Red/Blue Slider left.");
+                    maintain(RB_LEFT);
                     break;
-                    
-                    case 1://RB_CENTRE:
-                        printLCD("Centering Red/Blue Slider.");
-                        maintain(RB_CENTRE);
+
+                case 1://RB_CENTRE:
+                    printLCD("Centering Red/Blue Slider.");
+                    maintain(RB_CENTRE);
                     break;
-                    
-                    case 2://RB_RIGHT:
-                        printLCD("Moving Red/Blue Slider right.");
-                        maintain(RB_RIGHT);
+
+                case 2://RB_RIGHT:
+                    printLCD("Moving Red/Blue Slider right.");
+                    maintain(RB_RIGHT);
                     break;
-                    
-                    case 3://GO_UP:
-                        printLCD("Moving Green Slider up.");
-                        maintain(GO_UP);
+
+                case 3://GO_UP:
+                    printLCD("Moving Green Slider up.");
+                    maintain(GO_UP);
                     break;
-                    
-                    case 4://GO_CENTRE:
-                        printLCD("Centering Green Slider.");
-                        maintain(GO_CENTRE);
+
+                case 4://GO_CENTRE:
+                    printLCD("Centering Green Slider.");
+                    maintain(GO_CENTRE);
                     break;
-                    
-                    case 5://GO_DOWN:
-                        printLCD("Moving Green Slider down.");
-                        maintain(GO_DOWN);
+
+                case 5://GO_DOWN:
+                    printLCD("Moving Green Slider down.");
+                    maintain(GO_DOWN);
                     break;
-                    
-                    case 6://BR_LEFT:
-                        printLCD("Moving Bin Slider left.");
-                        maintain(BR_LEFT);
+
+                case 6://BR_LEFT:
+                    printLCD("Moving Bin Slider left.");
+                    maintain(BR_LEFT);
                     break;
-                    
-                    case 7://BR_RIGHT:
-                        printLCD("Moving Bin Slider right.");
-                        maintain(BR_RIGHT);
+
+                case 7://BR_RIGHT:
+                    printLCD("Moving Bin Slider right.");
+                    maintain(BR_RIGHT);
                     break;
-                    
-                    case 8://R_PUSH:
-                        printLCD("Pushing red dispensor");
-                        maintain(R_PUSH);
+
+                case 8://R_PUSH:
+                    printLCD("Pushing red dispensor");
+                    maintain(R_PUSH);
                     break;
-                    
-                    case 9://R_HOME:
-                        printLCD("Homing red dispensor");
-                        maintain(R_HOME);
+
+                case 9://R_HOME:
+                    printLCD("Homing red dispensor");
+                    maintain(R_HOME);
                     break;
-                    
-                    case 10://GB_LEFT:
-                        printLCD("Pushing green dispensor");
-                        maintain(GB_LEFT);
+
+                case 10://GB_LEFT:
+                    printLCD("Pushing green dispensor");
+                    maintain(GB_LEFT);
                     break;
-                    
-                    case 11://GB_CENTRE:
-                        printLCD("Centre green/blue dispensor");
-                        maintain(GB_CENTRE);
+
+                case 11://GB_CENTRE:
+                    printLCD("Centre green/blue dispensor");
+                    maintain(GB_CENTRE);
                     break;
-                    
-                    case 12://GB_RIGHT:
-                        printLCD("Pushing blue dispensor");
-                        maintain(GB_RIGHT );
+
+                case 12://GB_RIGHT:
+                    printLCD("Pushing blue dispensor");
+                    maintain(GB_RIGHT );
                     break;
-                    
-                    case 13: //MaintenanceEND
-                        printLCD("Ending Maintenance Mode");
-                        maintain(maintenanceEND);
+
+                case 13: //MaintenanceEND
+                    printLCD("Ending Maintenance Mode");
+                    maintain(maintenanceEND);
                     break;
-                    
-                
-            }  printLCD("Ready for next command");
-                wait(1);
-                }
+
+
             }
+            printLCD("Ready for next command");
+            wait(1);
         }
-    
-void changeLanguage(){
+    }
+
+}
+
+void changeLanguage()
+{
     currentLanguage = nextLanguage(currentLanguage);        //Change the language on teh MBED.
     sendLanguageCharacter();                                //Tell PC to change language
     //Change Language                                       //Tell Arduino to change language.
-    printLCD("setting language to english");
     wait(1);
-}   
-      
-int main() { 
-    
+}
+
+
+void sortFunction()
+{
+    Colour colour = readColourSensor();    
+    Colour colour2 = readColourSensor();
+    if(colour != colour2) {
+        return;
+    }
+
+    if(colour == BIN) {        
+        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);
+}
+
+void sortMode(void const* args)
+{
+    if(operationMode) {
+        while(true && colourSensor && sorter) {
+            led2 = 1;
+            sortFunction();
+            led2 = 0;
+            Thread::wait(50);
+        }
+    }
+}
+bool pcReady = false;
+void dispenseFunction()
+{
+    if(!pcReady) {
+        if(pc.getc() == '*') {
+            pcReady = true;
+        }
+    } else {
+        if(cardReader && cardDetect) {
+            //Read card
+            int cardNumber = cardAcquisition();
+
+            //Send message to pc
+            pc.putc('*');
+            wait(0.5);
+//log("sending card number to pc");
+            pc.printf("%i\n",cardNumber);
+            int red = 0;
+            int green = 0;
+            int blue = 0;
+//log("getting rgb 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('(');
+        }
+    }
+}
+
+void dispenseMode(void const* args)
+{
+    if(operationMode) {
+        while(true) {
+            led3 = 1;
+            dispenseFunction();
+            led3 =0;
+        }
+    }
+}
+
+
+int main()
+{
+    led1 = 0;
     setupLCD();
     printLCD("Welcome to the  Chipin Sorter");
     wait(1);
     readChipNumbers();
     printStoredChipValues();
-    //displayOperationMode();
-    while(true){
-        if(serialComms){
+    printLCD("Resetting all sliders");
+    maintain(maintenanceEND);
+    wait(1);
+
+    Thread sortThread(sortMode);
+    Thread dispenseThread(dispenseMode);
+
+
+    while(true) {
+        led1 = 1;
+        led4 = cardDetect;
+
+        if(serialComms) {
             checkSerial();
-        }if(testFunctions) checkSortDispenseButtons();
+        }
+
+        if(testFunctions) checkSortDispenseButtons();
+
         if(languageButton) changeLanguage();
-        wait(0.05);    //Slowi8ng things down a little bit.
-    } 
+
+        led1=0;
+        //Thread::wait(50);
+    }
 }
-       
-        
-    
+
+
+
 
-    
-  
+
 
-    
-    
\ No newline at end of file
+
+