Final Version of food controller

Dependencies:   MMA8451Q TSI mbed

Fork of foodcontroller_NoWiFi by Serpentine

Committer:
danlock10y
Date:
Fri Jun 10 14:10:17 2016 +0000
Revision:
10:c0514aae862c
Parent:
9:de6dd11d89ba
DON"T CHANGE THIS AGAIN EVER

Who changed what in which revision?

UserRevisionLine numberNew contents of line
danlock10y 7:c19655dbdef7 1 /**
danlock10y 7:c19655dbdef7 2 Food Controller
danlock10y 7:c19655dbdef7 3 main.cpp
danlock10y 10:c0514aae862c 4 Board ID: 1
danlock10y 7:c19655dbdef7 5 Purpose: This function runs on a FRDM KL25Z. It uses the accelerometer to measure the tilt of the board.
danlock10y 7:c19655dbdef7 6 It outputs a 2 bit number that describes the direction. The board is also fitted with four LEDS
danlock10y 7:c19655dbdef7 7 oriented in the following configuration: o8o to provide feedback to the user. This board also
danlock10y 10:c0514aae862c 8 makes use of the touch sensor input to set a variable speed for the food, formerly indicated by
danlock10y 10:c0514aae862c 9 RGB LED (functionality removed due to power issues)
danlock10y 7:c19655dbdef7 10 @author Daniel Lock
danlock10y 7:c19655dbdef7 11 @author Jamie Gnodde
danlock10y 7:c19655dbdef7 12 @version
danlock10y 7:c19655dbdef7 13 */
danlock10y 7:c19655dbdef7 14
danlock10y 7:c19655dbdef7 15 #include "mbed.h"
danlock10y 7:c19655dbdef7 16 #include "TSISensor.h"
danlock10y 7:c19655dbdef7 17 #include "MMA8451Q.h"
danlock10y 7:c19655dbdef7 18
danlock10y 7:c19655dbdef7 19 #define MMA8451_I2C_ADDRESS (0x1d<<1)
danlock10y 7:c19655dbdef7 20 Serial pc(USBTX, USBRX); // tx, rx used for printing to the PC screen during debugging
danlock10y 7:c19655dbdef7 21
danlock10y 7:c19655dbdef7 22 TSISensor tsi; //Setup touch sensor
danlock10y 7:c19655dbdef7 23 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); //Setup accelerometer
danlock10y 7:c19655dbdef7 24 DigitalOut lefthigh(PTC5); //LED for left
danlock10y 7:c19655dbdef7 25 DigitalOut leftlow(PTA4);
danlock10y 7:c19655dbdef7 26 DigitalOut righthigh(PTC11); //LED for right
danlock10y 7:c19655dbdef7 27 DigitalOut rightlow(PTC9);
danlock10y 7:c19655dbdef7 28 DigitalOut forwardhigh(PTC6); //LED for forward
danlock10y 7:c19655dbdef7 29 DigitalOut forwardlow(PTC10);
danlock10y 7:c19655dbdef7 30 DigitalOut backwardhigh(PTA5); //LED for backward
danlock10y 7:c19655dbdef7 31 DigitalOut backwardlow(PTC8);
danlock10y 7:c19655dbdef7 32
danlock10y 7:c19655dbdef7 33 RawSerial dev(PTE0,PTE1); // for KL25Z... asuming one can't use the PTA1 version which is the stdio
danlock10y 7:c19655dbdef7 34 DigitalOut rst(PTD1); // single digital pin to drive the esp8266 reset line
danlock10y 7:c19655dbdef7 35
danlock10y 7:c19655dbdef7 36 int Direction; //2 bit number to describe direction 0 forward assigned CW
danlock10y 7:c19655dbdef7 37 int PrevDirection; //2 bit number to describe last direction sent ot server
danlock10y 7:c19655dbdef7 38 int Velocity; //2 bit number to vary velocity in game
danlock10y 7:c19655dbdef7 39 int PrevVelocity; //2 bit number to describe last velocity sent ot server
danlock10y 7:c19655dbdef7 40
danlock10y 7:c19655dbdef7 41 bool right; //Right or left? right = 1
danlock10y 7:c19655dbdef7 42 bool forward; //Forward or Backward? forward = 1
danlock10y 7:c19655dbdef7 43
danlock10y 10:c0514aae862c 44 // subroutine to run anytime a serial interrupt arrives from the device
danlock10y 10:c0514aae862c 45 // this basically passes everything thatthe device produces on to the pc terminal screen
danlock10y 10:c0514aae862c 46 void dev_recv()
danlock10y 10:c0514aae862c 47 {
danlock10y 10:c0514aae862c 48 //led1 = !led1;
danlock10y 10:c0514aae862c 49 while(dev.readable()) {
danlock10y 10:c0514aae862c 50 pc.putc(dev.getc());
danlock10y 10:c0514aae862c 51 //wait_us(1);
danlock10y 10:c0514aae862c 52 }
danlock10y 10:c0514aae862c 53 }
danlock10y 10:c0514aae862c 54 // subroutine to service the serial interrupt on the pc connection
danlock10y 10:c0514aae862c 55 // this is a bit more complex - it takes what the use sends on the pc and copies it on to the device
danlock10y 10:c0514aae862c 56 // the esp should echo these straight back to the the pc if all is well
danlock10y 10:c0514aae862c 57 // this also detects the end of command character which is ascii 13 (0x0d) adn adds a linefeed after it =asscii 10 (0x0a)
danlock10y 10:c0514aae862c 58 void pc_recv()
danlock10y 10:c0514aae862c 59 {
danlock10y 10:c0514aae862c 60 char c;
danlock10y 10:c0514aae862c 61 //led4 = !led4;
danlock10y 10:c0514aae862c 62 while(pc.readable()) {
danlock10y 10:c0514aae862c 63 c=pc.getc();
danlock10y 10:c0514aae862c 64 dev.putc(c);
danlock10y 10:c0514aae862c 65 pc.putc(c); // echo back
danlock10y 10:c0514aae862c 66 if(c==13) {dev.putc(10); // send the linefeed to complement the carriage return generated by return key on the pc
danlock10y 10:c0514aae862c 67 pc.putc(10);
danlock10y 10:c0514aae862c 68 }
danlock10y 10:c0514aae862c 69 }
danlock10y 10:c0514aae862c 70 }
danlock10y 10:c0514aae862c 71
danlock10y 7:c19655dbdef7 72 void setupWiFi()
danlock10y 7:c19655dbdef7 73 {
danlock10y 7:c19655dbdef7 74 dev.printf("AT+RST\r\n"); //Reset WiFi
danlock10y 7:c19655dbdef7 75 pc.printf("RESET\r\n");
danlock10y 7:c19655dbdef7 76
danlock10y 7:c19655dbdef7 77 wait(2);
danlock10y 7:c19655dbdef7 78 dev.printf("AT+CWMODE=1\r\n"); //Set mode to client
danlock10y 7:c19655dbdef7 79
danlock10y 7:c19655dbdef7 80 wait(2);
danlock10y 7:c19655dbdef7 81 dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n"); //Login to the WiFi
danlock10y 7:c19655dbdef7 82
danlock10y 7:c19655dbdef7 83 wait(7);
danlock10y 7:c19655dbdef7 84 dev.printf("AT+CIFSR\r\n"); //Find IP and MAC address - not necessary?
danlock10y 7:c19655dbdef7 85
danlock10y 7:c19655dbdef7 86 wait(5);
danlock10y 7:c19655dbdef7 87 dev.printf("AT+CIPMUX=1\r\n"); //Allow multiple connections
danlock10y 7:c19655dbdef7 88
danlock10y 7:c19655dbdef7 89 wait(2);
danlock10y 10:c0514aae862c 90 dev.printf("AT+CIPSTART=0,\"TCP\",\"192.168.1.6\",5050\r\n"); //Open connection with the server
danlock10y 7:c19655dbdef7 91
danlock10y 7:c19655dbdef7 92 }
danlock10y 7:c19655dbdef7 93
danlock10y 7:c19655dbdef7 94
danlock10y 7:c19655dbdef7 95 int main()
danlock10y 7:c19655dbdef7 96 {
danlock10y 10:c0514aae862c 97 //Initialise WiFi stuff
danlock10y 10:c0514aae862c 98 rst=0;
danlock10y 10:c0514aae862c 99 wait(1);
danlock10y 10:c0514aae862c 100 rst=1; // send the esp8266 reset
danlock10y 10:c0514aae862c 101 wait(1);
danlock10y 10:c0514aae862c 102 pc.printf("ok off we go....\n\r");
danlock10y 10:c0514aae862c 103
danlock10y 10:c0514aae862c 104 pc.baud(115200); // NB maybe this should go before this
danlock10y 10:c0514aae862c 105 dev.baud(115200);
danlock10y 10:c0514aae862c 106
danlock10y 10:c0514aae862c 107 pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services
danlock10y 10:c0514aae862c 108 dev.attach(&dev_recv, Serial::RxIrq);
danlock10y 10:c0514aae862c 109
danlock10y 7:c19655dbdef7 110 PrevDirection = 4; //Initialise PrevDirection with a value that will mean it will always pass throught he if at the end
danlock10y 7:c19655dbdef7 111 PrevVelocity = 4; //Initialise PrevVelocity with a value that will mean it will always pass throught he if at the end
danlock10y 7:c19655dbdef7 112 setupWiFi(); //Run the WiFi setup
danlock10y 7:c19655dbdef7 113 while (1)
danlock10y 7:c19655dbdef7 114 {
danlock10y 7:c19655dbdef7 115
danlock10y 7:c19655dbdef7 116 float accX=acc.getAccX(); //Measure acceleration in X direction
danlock10y 7:c19655dbdef7 117 float accY=acc.getAccY(); //Measure acceleration in Y direction
danlock10y 7:c19655dbdef7 118
danlock10y 7:c19655dbdef7 119 if (tsi.readDistance() != 0) //Only execute if the touch sensor is being pressed
danlock10y 7:c19655dbdef7 120 {
danlock10y 7:c19655dbdef7 121 if (tsi.readDistance() <= 13) //Length of touch sensor divided into three distinct zones, this defines top zone
danlock10y 7:c19655dbdef7 122 {
danlock10y 7:c19655dbdef7 123 Velocity = 3; //Top zone -> highest velocity (varies 1-3)
danlock10y 10:c0514aae862c 124 /*rled = 0; //Use the RGB LED as an indicator for which speed is selected
danlock10y 7:c19655dbdef7 125 bled = 1;
danlock10y 10:c0514aae862c 126 gled = 1;*/ //removed due to power issues
danlock10y 7:c19655dbdef7 127 }
danlock10y 7:c19655dbdef7 128 if (tsi.readDistance() > 13 && tsi.readDistance() < 26) //Middle zone of touch sensor
danlock10y 7:c19655dbdef7 129 {
danlock10y 7:c19655dbdef7 130 Velocity = 2; //Middle zone -> middle velocity
danlock10y 10:c0514aae862c 131 /*rled = 1; //Use the RGB LED as an indicator for which speed is selected
danlock10y 7:c19655dbdef7 132 bled = 0;
danlock10y 10:c0514aae862c 133 gled = 1; */
danlock10y 7:c19655dbdef7 134 }
danlock10y 7:c19655dbdef7 135 if (tsi.readDistance() >= 26)
danlock10y 7:c19655dbdef7 136 {
danlock10y 7:c19655dbdef7 137 Velocity = 1; //Bottom zone -> lowest velocity
danlock10y 10:c0514aae862c 138 /* rled = 1; //Use the RGB LED as an indicator for which speed is selected
danlock10y 7:c19655dbdef7 139 bled = 1;
danlock10y 10:c0514aae862c 140 gled = 0;*/
danlock10y 7:c19655dbdef7 141 }
danlock10y 7:c19655dbdef7 142
danlock10y 10:c0514aae862c 143 //printf("x=%d\r\n",Velocity); //Print the velocity to the serial for debugging
danlock10y 7:c19655dbdef7 144
danlock10y 7:c19655dbdef7 145 wait(0.2); //May be left over from debugging?
danlock10y 7:c19655dbdef7 146
danlock10y 7:c19655dbdef7 147 }//endif touch sensor
danlock10y 7:c19655dbdef7 148
danlock10y 7:c19655dbdef7 149 //Establish whether the board is tilted left or right
danlock10y 7:c19655dbdef7 150 if (accX > 0.1)
danlock10y 7:c19655dbdef7 151 {
danlock10y 7:c19655dbdef7 152 right = false;
danlock10y 10:c0514aae862c 153 //printf("left \r\n");
danlock10y 7:c19655dbdef7 154 }//endif
danlock10y 7:c19655dbdef7 155
danlock10y 7:c19655dbdef7 156 if (accX < -0.1)
danlock10y 7:c19655dbdef7 157 {
danlock10y 7:c19655dbdef7 158 right = true;
danlock10y 10:c0514aae862c 159 //printf("right \r\n");
danlock10y 7:c19655dbdef7 160 }//endif
danlock10y 7:c19655dbdef7 161
danlock10y 7:c19655dbdef7 162 wait(0.1); //May be left over from debugging?
danlock10y 7:c19655dbdef7 163
danlock10y 7:c19655dbdef7 164 //Establish whether the board is tilted front or back
danlock10y 7:c19655dbdef7 165 if (accY > 0.1)
danlock10y 7:c19655dbdef7 166 {
danlock10y 7:c19655dbdef7 167 forward = false;
danlock10y 10:c0514aae862c 168 //printf("back \r\n");
danlock10y 7:c19655dbdef7 169 }//endif
danlock10y 7:c19655dbdef7 170
danlock10y 7:c19655dbdef7 171 if (accY < -0.1)
danlock10y 7:c19655dbdef7 172 {
danlock10y 7:c19655dbdef7 173 forward = true;
danlock10y 10:c0514aae862c 174 //printf("for \r\n");
danlock10y 7:c19655dbdef7 175 }//endif
danlock10y 7:c19655dbdef7 176
danlock10y 7:c19655dbdef7 177 wait(0.1); //May be left over from debugging?
danlock10y 7:c19655dbdef7 178
danlock10y 7:c19655dbdef7 179 //Establish the main axis of tilting so that the control outputs one direction
danlock10y 7:c19655dbdef7 180 if(abs(accY) > abs(accX))
danlock10y 7:c19655dbdef7 181 {
danlock10y 7:c19655dbdef7 182 if(forward == true)
danlock10y 7:c19655dbdef7 183 {
danlock10y 7:c19655dbdef7 184 Direction = 0; //Direction variable is a two bit number 0-3 0 is forward
danlock10y 7:c19655dbdef7 185
danlock10y 7:c19655dbdef7 186 lefthigh = 0; //Light up the forward LED, make sure all others are off
danlock10y 7:c19655dbdef7 187 leftlow = 0;
danlock10y 7:c19655dbdef7 188 righthigh = 0;
danlock10y 7:c19655dbdef7 189 rightlow = 0;
danlock10y 7:c19655dbdef7 190 forwardhigh = 1;
danlock10y 7:c19655dbdef7 191 forwardlow = 0;
danlock10y 7:c19655dbdef7 192 backwardhigh = 0;
danlock10y 7:c19655dbdef7 193 backwardlow = 0;
danlock10y 7:c19655dbdef7 194
danlock10y 7:c19655dbdef7 195 }//endif
danlock10y 7:c19655dbdef7 196 else
danlock10y 7:c19655dbdef7 197 {
danlock10y 7:c19655dbdef7 198 Direction = 2; //Direction variable is a two bit number 0-3 2 is backward
danlock10y 7:c19655dbdef7 199
danlock10y 7:c19655dbdef7 200 lefthigh = 0; //Light up the backward LED, make sure all others are off
danlock10y 7:c19655dbdef7 201 leftlow = 0;
danlock10y 7:c19655dbdef7 202 righthigh = 0;
danlock10y 7:c19655dbdef7 203 rightlow = 0;
danlock10y 7:c19655dbdef7 204 forwardhigh = 0;
danlock10y 7:c19655dbdef7 205 forwardlow = 0;
danlock10y 7:c19655dbdef7 206 backwardhigh = 1;
danlock10y 7:c19655dbdef7 207 backwardlow = 0;
danlock10y 7:c19655dbdef7 208
danlock10y 7:c19655dbdef7 209 }//endelse
danlock10y 7:c19655dbdef7 210 }//endif
danlock10y 7:c19655dbdef7 211 else
danlock10y 7:c19655dbdef7 212 {
danlock10y 7:c19655dbdef7 213 if(right == true)
danlock10y 7:c19655dbdef7 214 {
danlock10y 7:c19655dbdef7 215 Direction = 1; //Direction variable is a two bit number 0-3 1 is right
danlock10y 7:c19655dbdef7 216
danlock10y 7:c19655dbdef7 217 lefthigh = 0; //Light up the right LED, make sure all others are off
danlock10y 7:c19655dbdef7 218 leftlow = 0;
danlock10y 7:c19655dbdef7 219 righthigh = 1;
danlock10y 7:c19655dbdef7 220 rightlow = 0;
danlock10y 7:c19655dbdef7 221 forwardhigh = 0;
danlock10y 7:c19655dbdef7 222 forwardlow = 0;
danlock10y 7:c19655dbdef7 223 backwardhigh = 0;
danlock10y 7:c19655dbdef7 224 backwardlow = 0;
danlock10y 7:c19655dbdef7 225
danlock10y 7:c19655dbdef7 226 }//endif
danlock10y 7:c19655dbdef7 227 else
danlock10y 7:c19655dbdef7 228 {
danlock10y 7:c19655dbdef7 229 Direction = 3; //Direction variable is a two bit number 0-3 3 is left
danlock10y 7:c19655dbdef7 230
danlock10y 7:c19655dbdef7 231 lefthigh = 1; //Light up the left LED, make sure all others are off
danlock10y 7:c19655dbdef7 232 leftlow = 0;
danlock10y 7:c19655dbdef7 233 righthigh = 0;
danlock10y 7:c19655dbdef7 234 rightlow = 0;
danlock10y 7:c19655dbdef7 235 forwardhigh = 0;
danlock10y 7:c19655dbdef7 236 forwardlow = 0;
danlock10y 7:c19655dbdef7 237 backwardhigh = 0;
danlock10y 7:c19655dbdef7 238 backwardlow = 0;
danlock10y 7:c19655dbdef7 239
danlock10y 7:c19655dbdef7 240 }//endelse
danlock10y 7:c19655dbdef7 241 }//endelse
danlock10y 7:c19655dbdef7 242
danlock10y 10:c0514aae862c 243 //pc.printf("Direction = %d \r\n", Direction); //Print the direction variable to screen for debugging
danlock10y 7:c19655dbdef7 244
danlock10y 7:c19655dbdef7 245 //Only send data to the server if the direction has changed
danlock10y 8:047d427da574 246 if(Direction != PrevDirection || Velocity != PrevVelocity)
danlock10y 7:c19655dbdef7 247 {
danlock10y 7:c19655dbdef7 248 //Send Direction and Velocity to server
danlock10y 7:c19655dbdef7 249
danlock10y 10:c0514aae862c 250 dev.printf("AT+CIPSEND=0,11\r\n");
danlock10y 10:c0514aae862c 251 wait(0.1);
danlock10y 7:c19655dbdef7 252
danlock10y 10:c0514aae862c 253 //pc.printf("1dir%dvel%d\r\n",Direction,Velocity); //Identifier,Direction Tag,Direction Value,Velocity Tag,Velocity Value
danlock10y 10:c0514aae862c 254 dev.printf("1dir%dvel%d\r\n",Direction,Velocity); //Identifier,Direction Tag,Direction Value,Velocity Tag,Velocity Value
danlock10y 7:c19655dbdef7 255
danlock10y 10:c0514aae862c 256 PrevDirection = Direction; //Store Direction just sent as last sent direction
danlock10y 10:c0514aae862c 257 PrevVelocity = Velocity; //Store Velocity just sent as last sent velocity
danlock10y 7:c19655dbdef7 258 }
danlock10y 7:c19655dbdef7 259
danlock10y 7:c19655dbdef7 260 }//endwhile
danlock10y 7:c19655dbdef7 261 }//endmain