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