Final Version of food controller
Dependencies: MMA8451Q TSI mbed
Fork of foodcontroller_NoWiFi by
main.cpp@9:de6dd11d89ba, 2016-06-10 (annotated)
- Committer:
- danlock10y
- Date:
- Fri Jun 10 12:07:02 2016 +0000
- Revision:
- 9:de6dd11d89ba
- Parent:
- 8:047d427da574
- Child:
- 10:c0514aae862c
adds a check on server;
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 | 9:de6dd11d89ba | 63 | dev.printf("AT+CIPSTART=2,\"TCP\",\"192.168.1.6\",5050\r\n"); //Open connection with the server |
danlock10y | 7:c19655dbdef7 | 64 | |
danlock10y | 9:de6dd11d89ba | 65 | //Testing connection |
danlock10y | 7:c19655dbdef7 | 66 | wait(2); |
danlock10y | 9:de6dd11d89ba | 67 | dev.printf("AT+CIPSEND=2,4\r\n"); |
danlock10y | 9:de6dd11d89ba | 68 | wait(2); |
danlock10y | 9:de6dd11d89ba | 69 | |
danlock10y | 9:de6dd11d89ba | 70 | dev.printf("hi\n\r"); |
danlock10y | 7:c19655dbdef7 | 71 | } |
danlock10y | 7:c19655dbdef7 | 72 | |
danlock10y | 7:c19655dbdef7 | 73 | |
danlock10y | 7:c19655dbdef7 | 74 | int main() |
danlock10y | 7:c19655dbdef7 | 75 | { |
danlock10y | 7:c19655dbdef7 | 76 | PrevDirection = 4; //Initialise PrevDirection with a value that will mean it will always pass throught he if at the end |
danlock10y | 7:c19655dbdef7 | 77 | PrevVelocity = 4; //Initialise PrevVelocity with a value that will mean it will always pass throught he if at the end |
danlock10y | 7:c19655dbdef7 | 78 | setupWiFi(); //Run the WiFi setup |
danlock10y | 7:c19655dbdef7 | 79 | while (1) |
danlock10y | 7:c19655dbdef7 | 80 | { |
danlock10y | 7:c19655dbdef7 | 81 | |
danlock10y | 7:c19655dbdef7 | 82 | float accX=acc.getAccX(); //Measure acceleration in X direction |
danlock10y | 7:c19655dbdef7 | 83 | float accY=acc.getAccY(); //Measure acceleration in Y direction |
danlock10y | 7:c19655dbdef7 | 84 | |
danlock10y | 7:c19655dbdef7 | 85 | if (tsi.readDistance() != 0) //Only execute if the touch sensor is being pressed |
danlock10y | 7:c19655dbdef7 | 86 | { |
danlock10y | 7:c19655dbdef7 | 87 | if (tsi.readDistance() <= 13) //Length of touch sensor divided into three distinct zones, this defines top zone |
danlock10y | 7:c19655dbdef7 | 88 | { |
danlock10y | 7:c19655dbdef7 | 89 | Velocity = 3; //Top zone -> highest velocity (varies 1-3) |
danlock10y | 7:c19655dbdef7 | 90 | rled = 0; //Use the RGB LED as an indicator for which speed is selected |
danlock10y | 7:c19655dbdef7 | 91 | bled = 1; |
danlock10y | 7:c19655dbdef7 | 92 | gled = 1; |
danlock10y | 7:c19655dbdef7 | 93 | } |
danlock10y | 7:c19655dbdef7 | 94 | if (tsi.readDistance() > 13 && tsi.readDistance() < 26) //Middle zone of touch sensor |
danlock10y | 7:c19655dbdef7 | 95 | { |
danlock10y | 7:c19655dbdef7 | 96 | Velocity = 2; //Middle zone -> middle velocity |
danlock10y | 7:c19655dbdef7 | 97 | rled = 1; //Use the RGB LED as an indicator for which speed is selected |
danlock10y | 7:c19655dbdef7 | 98 | bled = 0; |
danlock10y | 7:c19655dbdef7 | 99 | gled = 1; |
danlock10y | 7:c19655dbdef7 | 100 | } |
danlock10y | 7:c19655dbdef7 | 101 | if (tsi.readDistance() >= 26) |
danlock10y | 7:c19655dbdef7 | 102 | { |
danlock10y | 7:c19655dbdef7 | 103 | Velocity = 1; //Bottom zone -> lowest velocity |
danlock10y | 7:c19655dbdef7 | 104 | rled = 1; //Use the RGB LED as an indicator for which speed is selected |
danlock10y | 7:c19655dbdef7 | 105 | bled = 1; |
danlock10y | 7:c19655dbdef7 | 106 | gled = 0; |
danlock10y | 7:c19655dbdef7 | 107 | } |
danlock10y | 7:c19655dbdef7 | 108 | |
danlock10y | 7:c19655dbdef7 | 109 | printf("x=%d\r\n",Velocity); //Print the velocity to the serial for debugging |
danlock10y | 7:c19655dbdef7 | 110 | |
danlock10y | 7:c19655dbdef7 | 111 | wait(0.2); //May be left over from debugging? |
danlock10y | 7:c19655dbdef7 | 112 | |
danlock10y | 7:c19655dbdef7 | 113 | }//endif touch sensor |
danlock10y | 7:c19655dbdef7 | 114 | |
danlock10y | 7:c19655dbdef7 | 115 | //Establish whether the board is tilted left or right |
danlock10y | 7:c19655dbdef7 | 116 | if (accX > 0.1) |
danlock10y | 7:c19655dbdef7 | 117 | { |
danlock10y | 7:c19655dbdef7 | 118 | right = false; |
danlock10y | 7:c19655dbdef7 | 119 | printf("left \r\n"); |
danlock10y | 7:c19655dbdef7 | 120 | }//endif |
danlock10y | 7:c19655dbdef7 | 121 | |
danlock10y | 7:c19655dbdef7 | 122 | if (accX < -0.1) |
danlock10y | 7:c19655dbdef7 | 123 | { |
danlock10y | 7:c19655dbdef7 | 124 | right = true; |
danlock10y | 7:c19655dbdef7 | 125 | printf("right \r\n"); |
danlock10y | 7:c19655dbdef7 | 126 | }//endif |
danlock10y | 7:c19655dbdef7 | 127 | |
danlock10y | 7:c19655dbdef7 | 128 | wait(0.1); //May be left over from debugging? |
danlock10y | 7:c19655dbdef7 | 129 | |
danlock10y | 7:c19655dbdef7 | 130 | //Establish whether the board is tilted front or back |
danlock10y | 7:c19655dbdef7 | 131 | if (accY > 0.1) |
danlock10y | 7:c19655dbdef7 | 132 | { |
danlock10y | 7:c19655dbdef7 | 133 | forward = false; |
danlock10y | 7:c19655dbdef7 | 134 | printf("back \r\n"); |
danlock10y | 7:c19655dbdef7 | 135 | }//endif |
danlock10y | 7:c19655dbdef7 | 136 | |
danlock10y | 7:c19655dbdef7 | 137 | if (accY < -0.1) |
danlock10y | 7:c19655dbdef7 | 138 | { |
danlock10y | 7:c19655dbdef7 | 139 | forward = true; |
danlock10y | 7:c19655dbdef7 | 140 | printf("for \r\n"); |
danlock10y | 7:c19655dbdef7 | 141 | }//endif |
danlock10y | 7:c19655dbdef7 | 142 | |
danlock10y | 7:c19655dbdef7 | 143 | wait(0.1); //May be left over from debugging? |
danlock10y | 7:c19655dbdef7 | 144 | |
danlock10y | 7:c19655dbdef7 | 145 | //Establish the main axis of tilting so that the control outputs one direction |
danlock10y | 7:c19655dbdef7 | 146 | if(abs(accY) > abs(accX)) |
danlock10y | 7:c19655dbdef7 | 147 | { |
danlock10y | 7:c19655dbdef7 | 148 | if(forward == true) |
danlock10y | 7:c19655dbdef7 | 149 | { |
danlock10y | 7:c19655dbdef7 | 150 | Direction = 0; //Direction variable is a two bit number 0-3 0 is forward |
danlock10y | 7:c19655dbdef7 | 151 | |
danlock10y | 7:c19655dbdef7 | 152 | lefthigh = 0; //Light up the forward LED, make sure all others are off |
danlock10y | 7:c19655dbdef7 | 153 | leftlow = 0; |
danlock10y | 7:c19655dbdef7 | 154 | righthigh = 0; |
danlock10y | 7:c19655dbdef7 | 155 | rightlow = 0; |
danlock10y | 7:c19655dbdef7 | 156 | forwardhigh = 1; |
danlock10y | 7:c19655dbdef7 | 157 | forwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 158 | backwardhigh = 0; |
danlock10y | 7:c19655dbdef7 | 159 | backwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 160 | |
danlock10y | 7:c19655dbdef7 | 161 | }//endif |
danlock10y | 7:c19655dbdef7 | 162 | else |
danlock10y | 7:c19655dbdef7 | 163 | { |
danlock10y | 7:c19655dbdef7 | 164 | Direction = 2; //Direction variable is a two bit number 0-3 2 is backward |
danlock10y | 7:c19655dbdef7 | 165 | |
danlock10y | 7:c19655dbdef7 | 166 | lefthigh = 0; //Light up the backward LED, make sure all others are off |
danlock10y | 7:c19655dbdef7 | 167 | leftlow = 0; |
danlock10y | 7:c19655dbdef7 | 168 | righthigh = 0; |
danlock10y | 7:c19655dbdef7 | 169 | rightlow = 0; |
danlock10y | 7:c19655dbdef7 | 170 | forwardhigh = 0; |
danlock10y | 7:c19655dbdef7 | 171 | forwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 172 | backwardhigh = 1; |
danlock10y | 7:c19655dbdef7 | 173 | backwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 174 | |
danlock10y | 7:c19655dbdef7 | 175 | }//endelse |
danlock10y | 7:c19655dbdef7 | 176 | }//endif |
danlock10y | 7:c19655dbdef7 | 177 | else |
danlock10y | 7:c19655dbdef7 | 178 | { |
danlock10y | 7:c19655dbdef7 | 179 | if(right == true) |
danlock10y | 7:c19655dbdef7 | 180 | { |
danlock10y | 7:c19655dbdef7 | 181 | Direction = 1; //Direction variable is a two bit number 0-3 1 is right |
danlock10y | 7:c19655dbdef7 | 182 | |
danlock10y | 7:c19655dbdef7 | 183 | lefthigh = 0; //Light up the right LED, make sure all others are off |
danlock10y | 7:c19655dbdef7 | 184 | leftlow = 0; |
danlock10y | 7:c19655dbdef7 | 185 | righthigh = 1; |
danlock10y | 7:c19655dbdef7 | 186 | rightlow = 0; |
danlock10y | 7:c19655dbdef7 | 187 | forwardhigh = 0; |
danlock10y | 7:c19655dbdef7 | 188 | forwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 189 | backwardhigh = 0; |
danlock10y | 7:c19655dbdef7 | 190 | backwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 191 | |
danlock10y | 7:c19655dbdef7 | 192 | }//endif |
danlock10y | 7:c19655dbdef7 | 193 | else |
danlock10y | 7:c19655dbdef7 | 194 | { |
danlock10y | 7:c19655dbdef7 | 195 | Direction = 3; //Direction variable is a two bit number 0-3 3 is left |
danlock10y | 7:c19655dbdef7 | 196 | |
danlock10y | 7:c19655dbdef7 | 197 | lefthigh = 1; //Light up the left LED, make sure all others are off |
danlock10y | 7:c19655dbdef7 | 198 | leftlow = 0; |
danlock10y | 7:c19655dbdef7 | 199 | righthigh = 0; |
danlock10y | 7:c19655dbdef7 | 200 | rightlow = 0; |
danlock10y | 7:c19655dbdef7 | 201 | forwardhigh = 0; |
danlock10y | 7:c19655dbdef7 | 202 | forwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 203 | backwardhigh = 0; |
danlock10y | 7:c19655dbdef7 | 204 | backwardlow = 0; |
danlock10y | 7:c19655dbdef7 | 205 | |
danlock10y | 7:c19655dbdef7 | 206 | }//endelse |
danlock10y | 7:c19655dbdef7 | 207 | }//endelse |
danlock10y | 7:c19655dbdef7 | 208 | |
danlock10y | 7:c19655dbdef7 | 209 | pc.printf("Direction = %d \r\n", Direction); //Print the direction variable to screen for debugging |
danlock10y | 7:c19655dbdef7 | 210 | |
danlock10y | 7:c19655dbdef7 | 211 | //Only send data to the server if the direction has changed |
danlock10y | 8:047d427da574 | 212 | if(Direction != PrevDirection || Velocity != PrevVelocity) |
danlock10y | 7:c19655dbdef7 | 213 | { |
danlock10y | 7:c19655dbdef7 | 214 | //Send Direction and Velocity to server |
danlock10y | 7:c19655dbdef7 | 215 | |
danlock10y | 8:047d427da574 | 216 | // dev.printf("AT+CIPSEND=0,11\r\n"); |
danlock10y | 7:c19655dbdef7 | 217 | //wait(2); |
danlock10y | 7:c19655dbdef7 | 218 | |
danlock10y | 8:047d427da574 | 219 | //dev.printf("1dir%dvel%d\r\n",Direction,Velocity); //Identifier,Direction Tag,Direction Value,Velocity Tag,Velocity Value |
danlock10y | 7:c19655dbdef7 | 220 | |
danlock10y | 7:c19655dbdef7 | 221 | PrevDirection = Direction; |
danlock10y | 7:c19655dbdef7 | 222 | PrevVelocity = Velocity; |
danlock10y | 7:c19655dbdef7 | 223 | } |
danlock10y | 7:c19655dbdef7 | 224 | |
danlock10y | 7:c19655dbdef7 | 225 | }//endwhile |
danlock10y | 7:c19655dbdef7 | 226 | }//endmain |