Final Version of food controller

Dependencies:   MMA8451Q TSI mbed

Fork of foodcontroller_NoWiFi by Serpentine

Revision:
7:c19655dbdef7
Child:
8:047d427da574
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jun 10 09:14:01 2016 +0000
@@ -0,0 +1,237 @@
+/**
+    Food Controller
+    main.cpp
+    Purpose:    This function runs on a FRDM KL25Z. It uses the accelerometer to measure the tilt of the board.
+                It outputs a 2 bit number that describes the direction. The board is also fitted with four LEDS
+                oriented in the following configuration: o8o to provide feedback to the user. This board also 
+                makes use of the touch sensor input to set a variable speed for the food, indicated by RGB LED
+    @author Daniel Lock
+    @author Jamie Gnodde
+    @version
+*/
+
+#include "mbed.h"
+#include "TSISensor.h"
+#include "MMA8451Q.h"
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+Serial pc(USBTX, USBRX); // tx, rx  used for printing to the PC screen during debugging
+
+DigitalOut gled(LED_GREEN);                         //Indicator for touch sensor Output speed 1
+DigitalOut bled(LED_BLUE);                          //Indicator for touch sensor Output speed 2
+DigitalOut rled(LED_RED);                           //Indicator for touch sensor Output speed 3
+TSISensor tsi;                                      //Setup touch sensor
+MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);    //Setup accelerometer
+DigitalOut lefthigh(PTC5);                          //LED for left
+DigitalOut leftlow(PTA4);
+DigitalOut righthigh(PTC11);                         //LED for right
+DigitalOut rightlow(PTC9);
+DigitalOut forwardhigh(PTC6);                       //LED for forward
+DigitalOut forwardlow(PTC10);
+DigitalOut backwardhigh(PTA5);                      //LED for backward
+DigitalOut backwardlow(PTC8);
+
+RawSerial dev(PTE0,PTE1);  // for KL25Z... asuming one can't use the PTA1 version which is the stdio
+DigitalOut rst(PTD1); // single digital pin to drive the esp8266 reset line
+
+int Direction;                                      //2 bit number to describe direction 0 forward assigned CW
+int PrevDirection;                                  //2 bit number to describe last direction sent ot server
+int Velocity;                                       //2 bit number to vary velocity in game
+int PrevVelocity;                                   //2 bit number to describe last velocity sent ot server
+
+bool right;                                         //Right or left? right = 1
+bool forward;                                       //Forward or Backward? forward = 1
+/*
+int main()
+{
+    
+    pc.printf("hello\n\r");
+    for(int c=0; c<14;c++)
+    {
+        pc.printf("work %d\n\r",c);
+        rled = 0;
+        wait(0.2);
+        rled = 1;
+        wait(0.2);    
+    }   
+}*/
+
+
+
+void setupWiFi()
+{
+    dev.printf("AT+RST\r\n");                       //Reset WiFi
+    pc.printf("RESET\r\n");
+    
+    wait(2);
+    dev.printf("AT+CWMODE=1\r\n");                  //Set mode to client
+    
+    wait(2);
+    dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n");       //Login to the WiFi
+    
+    wait(7);
+    dev.printf("AT+CIFSR\r\n");                     //Find IP and MAC address - not necessary?
+    
+    wait(5);
+    dev.printf("AT+CIPMUX=1\r\n");                  //Allow multiple connections
+    
+    wait(2);
+    dev.printf("AT+CIPSTART=0,\"TCP\",\"192.168.1.6\",5050\r\n");       //Open connection with the server
+    
+    wait(2);
+}
+
+
+int main()
+{
+    PrevDirection = 4;                            //Initialise PrevDirection with a value that will mean it will always pass throught he if at the end
+    PrevVelocity = 4;                             //Initialise PrevVelocity with a value that will mean it will always pass throught he if at the end
+    setupWiFi();                                  //Run the WiFi setup
+    while (1) 
+    {
+         
+        float accX=acc.getAccX();                   //Measure acceleration in X direction
+        float accY=acc.getAccY();                   //Measure acceleration in Y direction
+
+         if (tsi.readDistance() != 0)               //Only execute if the touch sensor is being pressed
+        {          
+            if (tsi.readDistance() <= 13)           //Length of touch sensor divided into three distinct zones, this defines top zone
+            {
+                Velocity = 3;                       //Top zone -> highest velocity (varies 1-3)
+                rled = 0;                           //Use the RGB LED as an indicator for which speed is selected
+                bled = 1;
+                gled = 1;
+            }
+            if (tsi.readDistance() > 13 && tsi.readDistance() < 26)         //Middle zone of touch sensor
+            {
+                Velocity = 2;                       //Middle zone -> middle velocity
+                rled = 1;                           //Use the RGB LED as an indicator for which speed is selected
+                bled = 0;
+                gled = 1;              
+            }
+            if (tsi.readDistance() >= 26)
+            {
+                Velocity = 1;                       //Bottom zone -> lowest velocity
+                rled = 1;                           //Use the RGB LED as an indicator for which speed is selected
+                bled = 1;
+                gled = 0;
+            }
+                             
+            printf("x=%d\r\n",Velocity);            //Print the velocity to the serial for debugging
+           
+            wait(0.2);                              //May be left over from debugging?
+
+        }//endif touch sensor 
+     
+        //Establish whether the board is tilted left or right         
+        if (accX > 0.1) 
+        {
+            right = false;
+            printf("left \r\n");
+        }//endif
+
+        if (accX < -0.1) 
+        {
+            right = true;
+            printf("right \r\n");
+        }//endif
+        
+        wait(0.1);                                  //May be left over from debugging?
+
+        //Establish whether the board is tilted front or back
+        if (accY > 0.1) 
+        {
+            forward = false;
+            printf("back \r\n");
+        }//endif
+
+        if (accY < -0.1) 
+        {
+            forward = true;            
+            printf("for \r\n");
+        }//endif
+        
+        wait(0.1);                                  //May be left over from debugging?
+        
+        //Establish the main axis of tilting so that the control outputs one direction        
+        if(abs(accY) > abs(accX))
+        {
+            if(forward == true)
+            {
+                Direction = 0;                  //Direction variable is a two bit number 0-3 0 is forward
+                
+                lefthigh = 0;                   //Light up the forward LED, make sure all others are off
+                leftlow = 0;
+                righthigh = 0;
+                rightlow = 0;
+                forwardhigh = 1;
+                forwardlow = 0;
+                backwardhigh = 0;
+                backwardlow = 0;                
+            
+            }//endif
+            else
+            {
+                Direction = 2;                  //Direction variable is a two bit number 0-3 2 is backward
+                
+                lefthigh = 0;                   //Light up the backward LED, make sure all others are off
+                leftlow = 0;
+                righthigh = 0;
+                rightlow = 0;
+                forwardhigh = 0;
+                forwardlow = 0;
+                backwardhigh = 1;
+                backwardlow = 0;
+                   
+            }//endelse
+        }//endif
+        else
+        {
+            if(right == true)
+            {
+                Direction = 1;                  //Direction variable is a two bit number 0-3 1 is right
+                
+                lefthigh = 0;                   //Light up the right LED, make sure all others are off
+                leftlow = 0;
+                righthigh = 1;
+                rightlow = 0;
+                forwardhigh = 0;
+                forwardlow = 0;
+                backwardhigh = 0;
+                backwardlow = 0;
+             
+            }//endif
+            else
+            {
+                Direction = 3;                  //Direction variable is a two bit number 0-3 3 is left
+                
+                lefthigh = 1;                   //Light up the left LED, make sure all others are off
+                leftlow = 0;
+                righthigh = 0;
+                rightlow = 0;
+                forwardhigh = 0;
+                forwardlow = 0;
+                backwardhigh = 0;
+                backwardlow = 0;                
+                    
+            }//endelse
+        }//endelse
+        
+        pc.printf("Direction = %d \r\n", Direction);           //Print the direction variable to screen for debugging
+        
+        //Only send data to the server if the direction has changed
+      if(Direction != PrevDirection || Velocity != PrevVelocity)
+        {
+            //Send Direction and Velocity to server
+            
+            //dev.printf("AT+CIPSEND=0,11\r\n");
+            //wait(2);
+            
+            dev.printf("1dir%dvel%d\r\n",Direction,Velocity);              //Identifier,Direction Tag,Direction Value,Velocity Tag,Velocity Value
+            
+            PrevDirection = Direction;
+            PrevVelocity = Velocity;
+        }
+       
+    }//endwhile
+}//endmain