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:
5:644bca33c1ca
Parent:
4:f3be545b3826
Child:
6:e64796f1f384
--- a/main.cpp	Wed Nov 05 12:03:40 2014 +0000
+++ b/main.cpp	Thu Nov 06 18:31:50 2014 +0000
@@ -1,6 +1,8 @@
 #include "mbed.h"
 #include "MCP23017.h"
 #include "WattBob_TextLCD.h"
+
+/*
 #include "TCS3472_I2C.h"
 #include <string>
 #include "Colour.h"
@@ -15,7 +17,7 @@
 DigitalIn testButton1(P1_8);
 
 //Boolean values to easily enable and disable certain features for individual testing
-/*bool colourSensor =  true;
+bool colourSensor =  true;
 bool cardReader =    true;
 bool sorter =        true;
 bool dispensor =     true;
@@ -28,7 +30,7 @@
 When operationMode is false, the MBED is in maintenance mode.
 */
 
-bool operationMode = true; //the MBED starts in operation mode.
+//bool operationMode = true; //the MBED starts in operation mode.
 
 #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT)
 #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT)
@@ -38,15 +40,15 @@
 WattBob_TextLCD *lcd;
 Serial pc(USBTX, USBRX);
 //Stuff that saves stuff to the MBED.
-LocalFileSystem local("local");
-
+//LocalFileSystem local("local");
 
 
-void writeFile(int colourQueue[]){
+/*
+void writeFile(int r, int g, int b){
     FILE* File1 = fopen("/local/datafile.txt","w"); // open file
-    fputc(colourQueue[0], File1); // put char (data value) into file
-    fputc(colourQueue[1], File1); // put char (data value) into file
-    fputc(colourQueue[2], File1); // put char (data value) into file
+    fputc(r, File1); // put char (data value) into file
+    fputc(g, File1); // put char (data value) into file
+    fputc(b, File1); // put char (data value) into file
     fclose(File1); // close file 
     }
     
@@ -56,7 +58,9 @@
     fclose(File2); // close file
     return read_var;   
     }
-int colourQueue = {readFile(),readFile(),readFile()};
+int redQueue = readFile();
+int greenQueue = readFile();
+int blueQueue = readFile();
 
 //Setup colour sensor variables
 int rgb_readings[4];
@@ -72,9 +76,7 @@
 int blueLT [3]= {25,47,47};
 int blueUT [3]= {28,52,52};
 
-int redQueue = 0;
-int greenQueue = 0;
-int blueQueue = 0;
+
 
 //setup Card Reader pins
 DigitalIn cardBit1(p21);
@@ -90,7 +92,7 @@
 
 //Setup pins to FPGA
 DigitalOut startSort(p5);       //A positive edge tells the FPGA to start sorting.
-/*  The command codes for the three sort select bits(ColourBit1 - ColourBit2)
+  The command codes for the three sort select bits(ColourBit1 - ColourBit2)
     000     Sort red chip
     001     Sort green chip
     010     Sort blue chip
@@ -100,16 +102,18 @@
     110     Nothing
     111     Nothing
 */
+/*
 DigitalOut colourBit1(p6);      //The 3 bits below are select bits for the sorter.
 DigitalOut colourBit2(p7);
 DigitalOut colourBit3(p8);
 DigitalOut startDispense(p9);   //A positive edge tells the FPGA to start dispensing.
-/*
+
     00      Dispense red chip
     01      Dispense green chip
     10      Dispense blue chip
     11      Nothing
 */
+/*
 DigitalOut DispenseBit1(p10);  //The 2 bits below are select bits for the dispenser.
 DigitalOut DispenseBit2(p11);
 
@@ -134,9 +138,10 @@
     testLED4 = true;
 }        
 
-/*  A simple print function for use on the MBED LCD Screen.
+  A simple print function for use on the MBED LCD Screen.
  *  Will format a char array into two lines and display the char array on the LCD.
- */      
+ */   
+ /*   
 void printLCD(const char * text){
         std::string txt_str = std::string(text); //Convert ther character array to a std::string, 
                                                  //to use in the string manipulation functions.
@@ -165,9 +170,10 @@
     testLED1 = par_port->read_bit(8); 
     
     }
-/* Reads all 6 bits from the card IO pins and prints them to the MBED LCD.
+ Reads all 6 bits from the card IO pins and prints them to the MBED LCD.
  * Stores the values read to the global variables cardValue1 to cardValue6.
 */
+/*
 void cardAcquisition(){
         lcd->reset();
         lcd->locate(0,0);   //Going to new line
@@ -201,13 +207,14 @@
         cardDataAcquired = true;
         
     }
-/*  valueToChar(int value):
+ valueToChar(int value):
     Expects an int value what it 0 or 1.
     Returns the int value as a char.
     0 will return '0'
     1 will return '1'
     Used to read the bits in the card reader.
 */
+/*
 char valueToChar(int value){
         if(value == 0){
             return '0';
@@ -217,10 +224,11 @@
             return NULL;    
             }
        }
-/*  readBit(int bitNumber):
+  readBit(int bitNumber):
     Takes a bit number between 1 and 6 inclusive.
     Returns the value of the cardBit associated with the bit number as a char.
 */
+/*
 char readBit(int bitNumber){
         int value = 0;
         switch(bitNumber){
@@ -245,14 +253,14 @@
             }
             return valueToChar(value);
     }
-/*  processMessage(char c):
+  processMessage(char c):
     Runs when the MBED recieves a character from the PC.
     Carries out tasks based on the char value.
     An acknowledge signal is always send back to the PC so that it knows the MBED 
     has recieved the command.
     Has two case statements: one for maintenance mode and one for operation mode.
 */
-   
+/*   
 void processMessage(char c){
      switch(c){
             case 'a' : //servo 1 clockwise
@@ -308,7 +316,7 @@
             break;
         
         
-   /*     else if (operationMode){
+        else if (operationMode){
             switch(c){
                 case 'n':// Start sorting
                     sendCharacter('N'); //Confirm to PC that we are processing the instruction.
@@ -326,108 +334,38 @@
                     sendCharacter('P');
                 break;
                 }
-          */  }
+           }
 }
-        
-Colour readColourSensor(){
-        rgb_sensor.getAllColors(rgb_readings);
-        Colour colour;
-        double redd = (rgb_readings[1] /gMax) * 255;
-        double greend = (rgb_readings[2] /bMax) * 255;
-        double blued = (rgb_readings[0] /rMax) * 255;
-        
-        int red = redd;
-        int green = greend;
-        int blue = blued;  
-        colourDataAcquired = true;
-        lcd->cls(); // clear display 
-        lcd->locate(0,0); 
-        lcd->printf("R:%d G:%d B:%d",red,green,blue);
-        
-        lcd->locate(1,0); 
-        /*if(red > 55){
-            lcd->printf("RED");
-        }
-        if(green > 55){
-            lcd->printf("GREEN");
-        } 
-        if(red < 30 && green > 30 && blue > 30){
-            lcd->printf("BLUE");
-        }  */
-        bool redWithinThreshold[3];
-        bool greenWithinThreshold[3];
-        bool blueWithinThreshold[3];
-        //Set red Thresholds
-        redWithinThreshold[0] = (red >= redLT[0]) && (red <= redUT[0]);
-        greenWithinThreshold[0] = (green >= redLT[1]) && (green <= redUT[1]);
-        blueWithinThreshold[0] = (blue >= redLT[2]) && (blue <= redUT[2]);
-        //Set green Thresholds
-        redWithinThreshold[1] = (red >= greenLT[0]) && (red <= greenUT[0]);
-        greenWithinThreshold[1] = (green >= greenLT[1]) && (green <= greenUT[1]);
-        blueWithinThreshold[1] = (blue >= greenLT[2]) && (blue <= greenUT[2]);
-        //Set blue Thresholds
-        redWithinThreshold[2] = (red >= blueLT[0]) && (red <= blueUT[0]);
-        greenWithinThreshold[2] = (green >= blueLT[1]) && (green <= blueUT[1]);
-        blueWithinThreshold[2] = blue >= blueLT[2] && blue <= blueUT[2];
-        
-        if(redWithinThreshold[0] && greenWithinThreshold[0] && blueWithinThreshold[0]){
-            lcd->printf("RED");
-            colour = RED;
-            }
-        else if(redWithinThreshold[1] && greenWithinThreshold[1] && blueWithinThreshold[1]){
-            lcd->printf("GREEN");
-            colour = GREEN;
-            }
-        else if(redWithinThreshold[2] && greenWithinThreshold[2] && blueWithinThreshold[2]){
-            lcd->printf("BLUE");
-            colour = BLUE;
-            }
-        else{
-            lcd->printf("BIN");
-            colour = OTHER;
-            }    
-        return colour;
-}
+  
 
-void sendColourSignal(Colour colour){
-    switch(colour){
-        case RED: 
-        
-        break;
-        case GREEN:
-        break;
-        case BLUE:
-        break;
-        case OTHER:
-        break;
-        }
-    }
+
+
 
 void resetForNextCustomer(){
         colourDataAcquired = false;
         cardDataAcquired = false;
         printLCD("Ready for next customer");
         }
-        
+     */   
 int main() {
     //Setting up all the global variables
     par_port = new MCP23017(p9, p10, 0x40);
     par_port->config(0x0F00, 0x0F00, 0x0F00); // configure MCP23017 chip on WattBob
     BACK_LIGHT_ON(par_port);
     lcd = new WattBob_TextLCD(par_port);
-    rgb_sensor.enablePowerAndRGBC();
-    rgb_sensor.setIntegrationTime(100);
+    //rgb_sensor.enablePowerAndRGBC();
+    //rgb_sensor.setIntegrationTime(100);
     //Writing initial text on LCD
-   // lcd->printf("Welcome to the");
-  //  lcd->locate(1,0);   //Going to new line
-  //  lcd->printf("Chipin Sorter");
+    lcd->printf("Welcome to the");
+    lcd->locate(1,0);   //Going to new line
+    lcd->printf("Chipin Sorter");
   
 
   
-    printLCD("Welcome to the  Chipin Sorter");
+    //printLCD("Welcome to the  Chipin Sorter");
     wait(1);
     lcd->reset();       //Clear LCD
-
+/*
     while(1){
        if(par_port->read_bit(11)) resetForNextCustomer();
        // char c = pc.getc(); //wait for a serial character to be recieved.
@@ -441,12 +379,12 @@
             sendColourSignal(colour);
         }
         
-        writeFile();
+        writeFile(redQueue, greenQueue, blueQueue);
         readFile();
         wait(2);
         
         }
       
-        
+    */    
 
     }