Final Version of food controller
Dependencies: MMA8451Q TSI mbed
Fork of foodcontroller_NoWiFi by
main.cpp@8:047d427da574, 2016-06-10 (annotated)
- Committer:
- danlock10y
- Date:
- Fri Jun 10 12:05:09 2016 +0000
- Revision:
- 8:047d427da574
- Parent:
- 7:c19655dbdef7
- Child:
- 9:de6dd11d89ba
2 axis tilt 3 velocity settings and rudimentary WiFi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
danlock10y | 7:c19655dbdef7 | 1 | /** |
danlock10y | 7:c19655dbdef7 | 2 | Food Controller |
danlock10y | 7:c19655dbdef7 | 3 | main.cpp |
danlock10y | 7:c19655dbdef7 | 4 | Purpose: This function runs on a FRDM KL25Z. It uses the accelerometer to measure the tilt of the board. |
danlock10y | 7:c19655dbdef7 | 5 | It outputs a 2 bit number that describes the direction. The board is also fitted with four LEDS |
danlock10y | 7:c19655dbdef7 | 6 | oriented in the following configuration: o8o to provide feedback to the user. This board also |
danlock10y | 7:c19655dbdef7 | 7 | makes use of the touch sensor input to set a variable speed for the food, indicated by RGB LED |
danlock10y | 7:c19655dbdef7 | 8 | @author Daniel Lock |
danlock10y | 7:c19655dbdef7 | 9 | @author Jamie Gnodde |
danlock10y | 7:c19655dbdef7 | 10 | @version |
danlock10y | 7:c19655dbdef7 | 11 | */ |
danlock10y | 7:c19655dbdef7 | 12 | |
danlock10y | 7:c19655dbdef7 | 13 | #include "mbed.h" |
danlock10y | 7:c19655dbdef7 | 14 | #include "TSISensor.h" |
danlock10y | 7:c19655dbdef7 | 15 | #include "MMA8451Q.h" |
danlock10y | 7:c19655dbdef7 | 16 | |
danlock10y | 7:c19655dbdef7 | 17 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
danlock10y | 7:c19655dbdef7 | 18 | Serial pc(USBTX, USBRX); // tx, rx used for printing to the PC screen during debugging |
danlock10y | 7:c19655dbdef7 | 19 | |
danlock10y | 7:c19655dbdef7 | 20 | DigitalOut gled(LED_GREEN); //Indicator for touch sensor Output speed 1 |
danlock10y | 7:c19655dbdef7 | 21 | DigitalOut bled(LED_BLUE); //Indicator for touch sensor Output speed 2 |
danlock10y | 7:c19655dbdef7 | 22 | DigitalOut rled(LED_RED); //Indicator for touch sensor Output speed 3 |
danlock10y | 7:c19655dbdef7 | 23 | TSISensor tsi; //Setup touch sensor |
danlock10y | 7:c19655dbdef7 | 24 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); //Setup accelerometer |
danlock10y | 7:c19655dbdef7 | 25 | DigitalOut lefthigh(PTC5); //LED for left |
danlock10y | 7:c19655dbdef7 | 26 | DigitalOut leftlow(PTA4); |
danlock10y | 7:c19655dbdef7 | 27 | DigitalOut righthigh(PTC11); //LED for right |
danlock10y | 7:c19655dbdef7 | 28 | DigitalOut rightlow(PTC9); |
danlock10y | 7:c19655dbdef7 | 29 | DigitalOut forwardhigh(PTC6); //LED for forward |
danlock10y | 7:c19655dbdef7 | 30 | DigitalOut forwardlow(PTC10); |
danlock10y | 7:c19655dbdef7 | 31 | DigitalOut backwardhigh(PTA5); //LED for backward |
danlock10y | 7:c19655dbdef7 | 32 | DigitalOut backwardlow(PTC8); |
danlock10y | 7:c19655dbdef7 | 33 | |
danlock10y | 7:c19655dbdef7 | 34 | RawSerial dev(PTE0,PTE1); // for KL25Z... asuming one can't use the PTA1 version which is the stdio |
danlock10y | 7:c19655dbdef7 | 35 | DigitalOut rst(PTD1); // single digital pin to drive the esp8266 reset line |
danlock10y | 7:c19655dbdef7 | 36 | |
danlock10y | 7:c19655dbdef7 | 37 | int Direction; //2 bit number to describe direction 0 forward assigned CW |
danlock10y | 7:c19655dbdef7 | 38 | int PrevDirection; //2 bit number to describe last direction sent ot server |
danlock10y | 7:c19655dbdef7 | 39 | int Velocity; //2 bit number to vary velocity in game |
danlock10y | 7:c19655dbdef7 | 40 | int PrevVelocity; //2 bit number to describe last velocity sent ot server |
danlock10y | 7:c19655dbdef7 | 41 | |
danlock10y | 7:c19655dbdef7 | 42 | bool right; //Right or left? right = 1 |
danlock10y | 7:c19655dbdef7 | 43 | bool forward; //Forward or Backward? forward = 1 |
danlock10y | 7:c19655dbdef7 | 44 | |
danlock10y | 7:c19655dbdef7 | 45 | void setupWiFi() |
danlock10y | 7:c19655dbdef7 | 46 | { |
danlock10y | 7:c19655dbdef7 | 47 | dev.printf("AT+RST\r\n"); //Reset WiFi |
danlock10y | 7:c19655dbdef7 | 48 | pc.printf("RESET\r\n"); |
danlock10y | 7:c19655dbdef7 | 49 | |
danlock10y | 7:c19655dbdef7 | 50 | wait(2); |
danlock10y | 7:c19655dbdef7 | 51 | dev.printf("AT+CWMODE=1\r\n"); //Set mode to client |
danlock10y | 7:c19655dbdef7 | 52 | |
danlock10y | 7:c19655dbdef7 | 53 | wait(2); |
danlock10y | 7:c19655dbdef7 | 54 | dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n"); //Login to the WiFi |
danlock10y | 7:c19655dbdef7 | 55 | |
danlock10y | 7:c19655dbdef7 | 56 | wait(7); |
danlock10y | 7:c19655dbdef7 | 57 | dev.printf("AT+CIFSR\r\n"); //Find IP and MAC address - not necessary? |
danlock10y | 7:c19655dbdef7 | 58 | |
danlock10y | 7:c19655dbdef7 | 59 | wait(5); |
danlock10y | 7:c19655dbdef7 | 60 | dev.printf("AT+CIPMUX=1\r\n"); //Allow multiple connections |
danlock10y | 7:c19655dbdef7 | 61 | |
danlock10y | 7:c19655dbdef7 | 62 | wait(2); |
danlock10y | 7:c19655dbdef7 | 63 | dev.printf("AT+CIPSTART=0,\"TCP\",\"192.168.1.6\",5050\r\n"); //Open connection with the server |
danlock10y | 7:c19655dbdef7 | 64 | |
danlock10y | 7:c19655dbdef7 | 65 | wait(2); |
danlock10y | 7:c19655dbdef7 | 66 | } |
danlock10y | 7:c19655dbdef7 | 67 | |
danlock10y | 7:c19655dbdef7 | 68 | |
danlock10y | 7:c19655dbdef7 | 69 | int main() |
danlock10y | 7:c19655dbdef7 | 70 | { |
danlock10y | 7:c19655dbdef7 | 71 | PrevDirection = 4; //Initialise PrevDirection with a value that will mean it will always pass throught he if at the end |
danlock10y | 7:c19655dbdef7 | 72 | PrevVelocity = 4; //Initialise PrevVelocity with a value that will mean it will always pass throught he if at the end |
danlock10y | 7:c19655dbdef7 | 73 | setupWiFi(); //Run the WiFi setup |
danlock10y | 7:c19655dbdef7 | 74 | while (1) |
danlock10y | 7:c19655dbdef7 | 75 | { |
danlock10y | 7:c19655dbdef7 | 76 | |
danlock10y | 7:c19655dbdef7 | 77 | float accX=acc.getAccX(); //Measure acceleration in X direction |
danlock10y | 7:c19655dbdef7 | 78 | float accY=acc.getAccY(); //Measure acceleration in Y direction |
danlock10y | 7:c19655dbdef7 | 79 | |
danlock10y | 7:c19655dbdef7 | 80 | if (tsi.readDistance() != 0) //Only execute if the touch sensor is being pressed |
danlock10y | 7:c19655dbdef7 | 81 | { |
danlock10y | 7:c19655dbdef7 | 82 | if (tsi.readDistance() <= 13) //Length of touch sensor divided into three distinct zones, this defines top zone |
danlock10y | 7:c19655dbdef7 | 83 | { |
danlock10y | 7:c19655dbdef7 | 84 | Velocity = 3; //Top zone -> highest velocity (varies 1-3) |
danlock10y | 7:c19655dbdef7 | 85 | rled = 0; //Use the RGB LED as an indicator for which speed is selected |
danlock10y | 7:c19655dbdef7 | 86 | bled = 1; |
danlock10y | 7:c19655dbdef7 | 87 | gled = 1; |
danlock10y | 7:c19655dbdef7 | 88 | } |
danlock10y | 7:c19655dbdef7 | 89 | if (tsi.readDistance() > 13 && tsi.readDistance() < 26) //Middle zone of touch sensor |
danlock10y | 7:c19655dbdef7 | 90 | { |
danlock10y | 7:c19655dbdef7 | 91 | Velocity = 2; //Middle zone -> middle velocity |
danlock10y | 7:c19655dbdef7 | 92 | rled = 1; //Use the RGB LED as an indicator for which speed is selected |
danlock10y | 7:c19655dbdef7 | 93 | bled = 0; |
danlock10y | 7:c19655dbdef7 | 94 | gled = 1; |
danlock10y | 7:c19655dbdef7 | 95 | } |
danlock10y | 7:c19655dbdef7 | 96 | if (tsi.readDistance() >= 26) |
danlock10y | 7:c19655dbdef7 | 97 | { |
danlock10y | 7:c19655dbdef7 | 98 | Velocity = 1; //Bottom zone -> lowest velocity |
danlock10y | 7:c19655dbdef7 | 99 | rled = 1; //Use the RGB LED as an indicator for which speed is selected |
danlock10y | 7:c19655dbdef7 | 100 | bled = 1; |
danlock10y | 7:c19655dbdef7 | 101 | gled = 0; |
danlock10y | 7:c19655dbdef7 | 102 | } |
danlock10y | 7:c19655dbdef7 | 103 | |
danlock10y | 7:c19655dbdef7 | 104 | printf("x=%d\r\n",Velocity); //Print the velocity to the serial for debugging |
danlock10y | 7:c19655dbdef7 | 105 | |
danlock10y | 7:c19655dbdef7 | 106 | wait(0.2); //May be left over from debugging? |
danlock10y | 7:c19655dbdef7 | 107 | |
danlock10y | 7:c19655dbdef7 | 108 | }//endif touch sensor |
danlock10y | 7:c19655dbdef7 | 109 | |
danlock10y | 7:c19655dbdef7 | 110 | //Establish whether the board is tilted left or right |
danlock10y | 7:c19655dbdef7 | 111 | if (accX > 0.1) |
danlock10y | 7:c19655dbdef7 | 112 | { |
danlock10y | 7:c19655dbdef7 | 113 | right = false; |
danlock10y | 7:c19655dbdef7 | 114 | printf("left \r\n"); |
danlock10y | 7:c19655dbdef7 | 115 | }//endif |
danlock10y | 7:c19655dbdef7 | 116 | |
danlock10y | 7:c19655dbdef7 | 117 | if (accX < -0.1) |
danlock10y | 7:c19655dbdef7 | 118 | { |
danlock10y | 7:c19655dbdef7 | 119 | right = true; |
danlock10y | 7:c19655dbdef7 | 120 | printf("right \r\n"); |
danlock10y | 7:c19655dbdef7 | 121 | }//endif |
danlock10y | 7:c19655dbdef7 | 122 | |
danlock10y | 7:c19655dbdef7 | 123 | wait(0.1); //May be left over from debugging? |
danlock10y | 7:c19655dbdef7 | 124 | |
danlock10y | 7:c19655dbdef7 | 125 | //Establish whether the board is tilted front or back |
danlock10y | 7:c19655dbdef7 | 126 | if (accY > 0.1) |
danlock10y | 7:c19655dbdef7 | 127 | { |
danlock10y | 7:c19655dbdef7 | 128 | forward = false; |
danlock10y | 7:c19655dbdef7 | 129 | printf("back \r\n"); |
danlock10y | 7:c19655dbdef7 | 130 | }//endif |
danlock10y | 7:c19655dbdef7 | 131 | |
danlock10y | 7:c19655dbdef7 | 132 | if (accY < -0.1) |
danlock10y | 7:c19655dbdef7 | 133 | { |
danlock10y | 7:c19655dbdef7 | 134 | forward = true; |
danlock10y | 7:c19655dbdef7 | 135 | printf("for \r\n"); |
danlock10y | 7:c19655dbdef7 | 136 | }//endif |
danlock10y | 7:c19655dbdef7 | 137 | |
danlock10y | 7:c19655dbdef7 | 138 | wait(0.1); //May be left over from debugging? |
danlock10y | 7:c19655dbdef7 | 139 | |
danlock10y | 7:c19655dbdef7 | 140 | //Establish the main axis of tilting so that the control outputs one direction |
danlock10y | 7:c19655dbdef7 | 141 | if(abs(accY) > abs(accX)) |
danlock10y | 7:c19655dbdef7 | 142 | { |
danlock10y | 7:c19655dbdef7 | 143 | if(forward == true) |
danlock10y | 7:c19655dbdef7 | 144 | { |
danlock10y | 7:c19655dbdef7 | 145 | Direction = 0; //Direction variable is a two bit number 0-3 0 is forward |
danlock10y | 7:c19655dbdef7 | 146 | |
danlock10y | 7:c19655dbdef7 | 147 | lefthigh = 0; //Light up the forward LED, make sure all others are off |
danlock10y | 7:c19655dbdef7 | 148 | leftlow = 0; |
danlock10y | 7:c19655dbdef7 | 149 | righthigh = 0; |
danlock10y | 7:c19655dbdef7 | 150 | rightlow = 0; |
danlock10y | 7:c19655dbdef7 | 151 | forwardhigh = 1; |
danlock10y | 7:c19655dbdef7 | 152 | forwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 153 | backwardhigh = 0; |
danlock10y | 7:c19655dbdef7 | 154 | backwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 155 | |
danlock10y | 7:c19655dbdef7 | 156 | }//endif |
danlock10y | 7:c19655dbdef7 | 157 | else |
danlock10y | 7:c19655dbdef7 | 158 | { |
danlock10y | 7:c19655dbdef7 | 159 | Direction = 2; //Direction variable is a two bit number 0-3 2 is backward |
danlock10y | 7:c19655dbdef7 | 160 | |
danlock10y | 7:c19655dbdef7 | 161 | lefthigh = 0; //Light up the backward LED, make sure all others are off |
danlock10y | 7:c19655dbdef7 | 162 | leftlow = 0; |
danlock10y | 7:c19655dbdef7 | 163 | righthigh = 0; |
danlock10y | 7:c19655dbdef7 | 164 | rightlow = 0; |
danlock10y | 7:c19655dbdef7 | 165 | forwardhigh = 0; |
danlock10y | 7:c19655dbdef7 | 166 | forwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 167 | backwardhigh = 1; |
danlock10y | 7:c19655dbdef7 | 168 | backwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 169 | |
danlock10y | 7:c19655dbdef7 | 170 | }//endelse |
danlock10y | 7:c19655dbdef7 | 171 | }//endif |
danlock10y | 7:c19655dbdef7 | 172 | else |
danlock10y | 7:c19655dbdef7 | 173 | { |
danlock10y | 7:c19655dbdef7 | 174 | if(right == true) |
danlock10y | 7:c19655dbdef7 | 175 | { |
danlock10y | 7:c19655dbdef7 | 176 | Direction = 1; //Direction variable is a two bit number 0-3 1 is right |
danlock10y | 7:c19655dbdef7 | 177 | |
danlock10y | 7:c19655dbdef7 | 178 | lefthigh = 0; //Light up the right LED, make sure all others are off |
danlock10y | 7:c19655dbdef7 | 179 | leftlow = 0; |
danlock10y | 7:c19655dbdef7 | 180 | righthigh = 1; |
danlock10y | 7:c19655dbdef7 | 181 | rightlow = 0; |
danlock10y | 7:c19655dbdef7 | 182 | forwardhigh = 0; |
danlock10y | 7:c19655dbdef7 | 183 | forwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 184 | backwardhigh = 0; |
danlock10y | 7:c19655dbdef7 | 185 | backwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 186 | |
danlock10y | 7:c19655dbdef7 | 187 | }//endif |
danlock10y | 7:c19655dbdef7 | 188 | else |
danlock10y | 7:c19655dbdef7 | 189 | { |
danlock10y | 7:c19655dbdef7 | 190 | Direction = 3; //Direction variable is a two bit number 0-3 3 is left |
danlock10y | 7:c19655dbdef7 | 191 | |
danlock10y | 7:c19655dbdef7 | 192 | lefthigh = 1; //Light up the left LED, make sure all others are off |
danlock10y | 7:c19655dbdef7 | 193 | leftlow = 0; |
danlock10y | 7:c19655dbdef7 | 194 | righthigh = 0; |
danlock10y | 7:c19655dbdef7 | 195 | rightlow = 0; |
danlock10y | 7:c19655dbdef7 | 196 | forwardhigh = 0; |
danlock10y | 7:c19655dbdef7 | 197 | forwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 198 | backwardhigh = 0; |
danlock10y | 7:c19655dbdef7 | 199 | backwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 200 | |
danlock10y | 7:c19655dbdef7 | 201 | }//endelse |
danlock10y | 7:c19655dbdef7 | 202 | }//endelse |
danlock10y | 7:c19655dbdef7 | 203 | |
danlock10y | 7:c19655dbdef7 | 204 | pc.printf("Direction = %d \r\n", Direction); //Print the direction variable to screen for debugging |
danlock10y | 7:c19655dbdef7 | 205 | |
danlock10y | 7:c19655dbdef7 | 206 | //Only send data to the server if the direction has changed |
danlock10y | 8:047d427da574 | 207 | if(Direction != PrevDirection || Velocity != PrevVelocity) |
danlock10y | 7:c19655dbdef7 | 208 | { |
danlock10y | 7:c19655dbdef7 | 209 | //Send Direction and Velocity to server |
danlock10y | 7:c19655dbdef7 | 210 | |
danlock10y | 8:047d427da574 | 211 | // dev.printf("AT+CIPSEND=0,11\r\n"); |
danlock10y | 7:c19655dbdef7 | 212 | //wait(2); |
danlock10y | 7:c19655dbdef7 | 213 | |
danlock10y | 8:047d427da574 | 214 | //dev.printf("1dir%dvel%d\r\n",Direction,Velocity); //Identifier,Direction Tag,Direction Value,Velocity Tag,Velocity Value |
danlock10y | 7:c19655dbdef7 | 215 | |
danlock10y | 7:c19655dbdef7 | 216 | PrevDirection = Direction; |
danlock10y | 7:c19655dbdef7 | 217 | PrevVelocity = Velocity; |
danlock10y | 7:c19655dbdef7 | 218 | } |
danlock10y | 7:c19655dbdef7 | 219 | |
danlock10y | 7:c19655dbdef7 | 220 | }//endwhile |
danlock10y | 7:c19655dbdef7 | 221 | }//endmain |