Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MMA8451Q TSI mbed
Fork of foodcontroller_NoWiFi by
foodController.cpp@6:57b9a6427bc2, 2016-06-09 (annotated)
- Committer:
- danlock10y
- Date:
- Thu Jun 09 18:42:57 2016 +0000
- Revision:
- 6:57b9a6427bc2
Fully functional control with 2 axis tilt controller and touch sensor velocity control. ; ; Fully Commented
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| danlock10y | 6:57b9a6427bc2 | 1 | /** |
| danlock10y | 6:57b9a6427bc2 | 2 | Food Controller |
| danlock10y | 6:57b9a6427bc2 | 3 | foodController.cpp |
| danlock10y | 6:57b9a6427bc2 | 4 | Purpose: This function runs on a FRDM KL25Z. It uses the accelerometer to measure the tilt of the board. |
| danlock10y | 6:57b9a6427bc2 | 5 | It outputs a 2 bit number that describes the direction. The board is also fitted with four LEDS |
| danlock10y | 6:57b9a6427bc2 | 6 | oriented in the following configuration: o8o to provide feedback to the user. This board also |
| danlock10y | 6:57b9a6427bc2 | 7 | makes use of the touch sensor input to set a variable speed for the food, indicated by RGB LED |
| danlock10y | 6:57b9a6427bc2 | 8 | @author Daniel Lock |
| danlock10y | 6:57b9a6427bc2 | 9 | @author Jamie Gnodde |
| danlock10y | 6:57b9a6427bc2 | 10 | @version |
| danlock10y | 6:57b9a6427bc2 | 11 | */ |
| danlock10y | 6:57b9a6427bc2 | 12 | |
| danlock10y | 6:57b9a6427bc2 | 13 | #include "mbed.h" |
| danlock10y | 6:57b9a6427bc2 | 14 | #include "TSISensor.h" |
| danlock10y | 6:57b9a6427bc2 | 15 | #include "MMA8451Q.h" |
| danlock10y | 6:57b9a6427bc2 | 16 | |
| danlock10y | 6:57b9a6427bc2 | 17 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
| danlock10y | 6:57b9a6427bc2 | 18 | Serial pc(USBTX, USBRX); // tx, rx used for printing to the PC screen during debugging |
| danlock10y | 6:57b9a6427bc2 | 19 | |
| danlock10y | 6:57b9a6427bc2 | 20 | DigitalOut gled(LED_GREEN); //Indicator for touch sensor Output speed 1 |
| danlock10y | 6:57b9a6427bc2 | 21 | DigitalOut bled(LED_BLUE); //Indicator for touch sensor Output speed 2 |
| danlock10y | 6:57b9a6427bc2 | 22 | DigitalOut rled(LED_RED); //Indicator for touch sensor Output speed 3 |
| danlock10y | 6:57b9a6427bc2 | 23 | TSISensor tsi; //Setup touch sensor |
| danlock10y | 6:57b9a6427bc2 | 24 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); //Setup accelerometer |
| danlock10y | 6:57b9a6427bc2 | 25 | DigitalOut lefthigh(PTC7); //LED for left |
| danlock10y | 6:57b9a6427bc2 | 26 | DigitalOut leftlow(PTA1); |
| danlock10y | 6:57b9a6427bc2 | 27 | DigitalOut righthigh(PTC4); //LED for right |
| danlock10y | 6:57b9a6427bc2 | 28 | DigitalOut rightlow(PTA12); |
| danlock10y | 6:57b9a6427bc2 | 29 | DigitalOut forwardhigh(PTC0); //LED for forward |
| danlock10y | 6:57b9a6427bc2 | 30 | DigitalOut forwardlow(PTC3); |
| danlock10y | 6:57b9a6427bc2 | 31 | DigitalOut backwardhigh(PTA2); //LED for backward |
| danlock10y | 6:57b9a6427bc2 | 32 | DigitalOut backwardlow(PTD4); |
| danlock10y | 6:57b9a6427bc2 | 33 | |
| danlock10y | 6:57b9a6427bc2 | 34 | int Direction; //2 bit number to describe direction 0 forward assigned CW |
| danlock10y | 6:57b9a6427bc2 | 35 | int Velocity; //2 bit number to vary velocity in game |
| danlock10y | 6:57b9a6427bc2 | 36 | |
| danlock10y | 6:57b9a6427bc2 | 37 | bool right; //Right or left? right = 1 |
| danlock10y | 6:57b9a6427bc2 | 38 | bool forward; //Forward or Backward? forward = 1 |
| danlock10y | 6:57b9a6427bc2 | 39 | |
| danlock10y | 6:57b9a6427bc2 | 40 | int main() |
| danlock10y | 6:57b9a6427bc2 | 41 | { |
| danlock10y | 6:57b9a6427bc2 | 42 | while (1) |
| danlock10y | 6:57b9a6427bc2 | 43 | { |
| danlock10y | 6:57b9a6427bc2 | 44 | |
| danlock10y | 6:57b9a6427bc2 | 45 | float accX=acc.getAccX(); //Measure acceleration in X direction |
| danlock10y | 6:57b9a6427bc2 | 46 | float accY=acc.getAccY(); //Measure acceleration in Y direction |
| danlock10y | 6:57b9a6427bc2 | 47 | |
| danlock10y | 6:57b9a6427bc2 | 48 | if (tsi.readDistance() != 0) //Only execute if the touch sensor is being pressed |
| danlock10y | 6:57b9a6427bc2 | 49 | { |
| danlock10y | 6:57b9a6427bc2 | 50 | if (tsi.readDistance() <= 13) //Length of touch sensor divided into three distinct zones, this defines top zone |
| danlock10y | 6:57b9a6427bc2 | 51 | { |
| danlock10y | 6:57b9a6427bc2 | 52 | Velocity = 3; //Top zone -> highest velocity (varies 1-3) |
| danlock10y | 6:57b9a6427bc2 | 53 | rled = 0; //Use the RGB LED as an indicator for which speed is selected |
| danlock10y | 6:57b9a6427bc2 | 54 | bled = 1; |
| danlock10y | 6:57b9a6427bc2 | 55 | gled = 1; |
| danlock10y | 6:57b9a6427bc2 | 56 | } |
| danlock10y | 6:57b9a6427bc2 | 57 | if (tsi.readDistance() > 13 && tsi.readDistance() < 26) //Middle zone of touch sensor |
| danlock10y | 6:57b9a6427bc2 | 58 | { |
| danlock10y | 6:57b9a6427bc2 | 59 | Velocity = 2; //Middle zone -> middle velocity |
| danlock10y | 6:57b9a6427bc2 | 60 | rled = 1; //Use the RGB LED as an indicator for which speed is selected |
| danlock10y | 6:57b9a6427bc2 | 61 | bled = 0; |
| danlock10y | 6:57b9a6427bc2 | 62 | gled = 1; |
| danlock10y | 6:57b9a6427bc2 | 63 | } |
| danlock10y | 6:57b9a6427bc2 | 64 | if (tsi.readDistance() >= 26) |
| danlock10y | 6:57b9a6427bc2 | 65 | { |
| danlock10y | 6:57b9a6427bc2 | 66 | Velocity = 1; //Bottom zone -> lowest velocity |
| danlock10y | 6:57b9a6427bc2 | 67 | rled = 1; //Use the RGB LED as an indicator for which speed is selected |
| danlock10y | 6:57b9a6427bc2 | 68 | bled = 1; |
| danlock10y | 6:57b9a6427bc2 | 69 | gled = 0; |
| danlock10y | 6:57b9a6427bc2 | 70 | } |
| danlock10y | 6:57b9a6427bc2 | 71 | |
| danlock10y | 6:57b9a6427bc2 | 72 | printf("x=%d\r\n",Velocity); //Print the velocity to the serial for debugging |
| danlock10y | 6:57b9a6427bc2 | 73 | |
| danlock10y | 6:57b9a6427bc2 | 74 | wait(0.2); //May be left over from debugging? |
| danlock10y | 6:57b9a6427bc2 | 75 | |
| danlock10y | 6:57b9a6427bc2 | 76 | }//endif touch sensor |
| danlock10y | 6:57b9a6427bc2 | 77 | |
| danlock10y | 6:57b9a6427bc2 | 78 | //Establish whether the board is tilted left or right |
| danlock10y | 6:57b9a6427bc2 | 79 | if (accX > 0.1) |
| danlock10y | 6:57b9a6427bc2 | 80 | { |
| danlock10y | 6:57b9a6427bc2 | 81 | right = false; |
| danlock10y | 6:57b9a6427bc2 | 82 | //printf("left \r\n"); |
| danlock10y | 6:57b9a6427bc2 | 83 | }//endif |
| danlock10y | 6:57b9a6427bc2 | 84 | |
| danlock10y | 6:57b9a6427bc2 | 85 | if (accX < -0.1) |
| danlock10y | 6:57b9a6427bc2 | 86 | { |
| danlock10y | 6:57b9a6427bc2 | 87 | right = true; |
| danlock10y | 6:57b9a6427bc2 | 88 | //printf("right \r\n"); |
| danlock10y | 6:57b9a6427bc2 | 89 | }//endif |
| danlock10y | 6:57b9a6427bc2 | 90 | |
| danlock10y | 6:57b9a6427bc2 | 91 | wait(0.1); //May be left over from debugging? |
| danlock10y | 6:57b9a6427bc2 | 92 | |
| danlock10y | 6:57b9a6427bc2 | 93 | //Establish whether the board is tilted front or back |
| danlock10y | 6:57b9a6427bc2 | 94 | if (accY > 0.1) |
| danlock10y | 6:57b9a6427bc2 | 95 | { |
| danlock10y | 6:57b9a6427bc2 | 96 | forward = false; |
| danlock10y | 6:57b9a6427bc2 | 97 | //printf("back \r\n"); |
| danlock10y | 6:57b9a6427bc2 | 98 | }//endif |
| danlock10y | 6:57b9a6427bc2 | 99 | |
| danlock10y | 6:57b9a6427bc2 | 100 | if (accY < -0.1) |
| danlock10y | 6:57b9a6427bc2 | 101 | { |
| danlock10y | 6:57b9a6427bc2 | 102 | forward = true; |
| danlock10y | 6:57b9a6427bc2 | 103 | //printf("for \r\n"); |
| danlock10y | 6:57b9a6427bc2 | 104 | }//endif |
| danlock10y | 6:57b9a6427bc2 | 105 | |
| danlock10y | 6:57b9a6427bc2 | 106 | wait(0.1); //May be left over from debugging? |
| danlock10y | 6:57b9a6427bc2 | 107 | |
| danlock10y | 6:57b9a6427bc2 | 108 | //Establish the main axis of tilting so that the control outputs one direction |
| danlock10y | 6:57b9a6427bc2 | 109 | if(abs(accY) > abs(accX)) |
| danlock10y | 6:57b9a6427bc2 | 110 | { |
| danlock10y | 6:57b9a6427bc2 | 111 | if(forward == true) |
| danlock10y | 6:57b9a6427bc2 | 112 | { |
| danlock10y | 6:57b9a6427bc2 | 113 | Direction = 0; //Direction variable is a two bit number 0-3 0 is forward |
| danlock10y | 6:57b9a6427bc2 | 114 | |
| danlock10y | 6:57b9a6427bc2 | 115 | lefthigh = 0; //Light up the forward LED, make sure all others are off |
| danlock10y | 6:57b9a6427bc2 | 116 | leftlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 117 | righthigh = 0; |
| danlock10y | 6:57b9a6427bc2 | 118 | rightlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 119 | forwardhigh = 1; |
| danlock10y | 6:57b9a6427bc2 | 120 | forwardlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 121 | backwardhigh = 0; |
| danlock10y | 6:57b9a6427bc2 | 122 | backwardlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 123 | |
| danlock10y | 6:57b9a6427bc2 | 124 | }//endif |
| danlock10y | 6:57b9a6427bc2 | 125 | else |
| danlock10y | 6:57b9a6427bc2 | 126 | { |
| danlock10y | 6:57b9a6427bc2 | 127 | Direction = 2; //Direction variable is a two bit number 0-3 2 is backward |
| danlock10y | 6:57b9a6427bc2 | 128 | |
| danlock10y | 6:57b9a6427bc2 | 129 | lefthigh = 0; //Light up the backward LED, make sure all others are off |
| danlock10y | 6:57b9a6427bc2 | 130 | leftlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 131 | righthigh = 0; |
| danlock10y | 6:57b9a6427bc2 | 132 | rightlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 133 | forwardhigh = 0; |
| danlock10y | 6:57b9a6427bc2 | 134 | forwardlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 135 | backwardhigh = 1; |
| danlock10y | 6:57b9a6427bc2 | 136 | backwardlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 137 | |
| danlock10y | 6:57b9a6427bc2 | 138 | }//endelse |
| danlock10y | 6:57b9a6427bc2 | 139 | }//endif |
| danlock10y | 6:57b9a6427bc2 | 140 | else |
| danlock10y | 6:57b9a6427bc2 | 141 | { |
| danlock10y | 6:57b9a6427bc2 | 142 | if(right == true) |
| danlock10y | 6:57b9a6427bc2 | 143 | { |
| danlock10y | 6:57b9a6427bc2 | 144 | Direction = 1; //Direction variable is a two bit number 0-3 1 is right |
| danlock10y | 6:57b9a6427bc2 | 145 | |
| danlock10y | 6:57b9a6427bc2 | 146 | lefthigh = 0; //Light up the right LED, make sure all others are off |
| danlock10y | 6:57b9a6427bc2 | 147 | leftlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 148 | righthigh = 1; |
| danlock10y | 6:57b9a6427bc2 | 149 | rightlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 150 | forwardhigh = 0; |
| danlock10y | 6:57b9a6427bc2 | 151 | forwardlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 152 | backwardhigh = 0; |
| danlock10y | 6:57b9a6427bc2 | 153 | backwardlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 154 | |
| danlock10y | 6:57b9a6427bc2 | 155 | }//endif |
| danlock10y | 6:57b9a6427bc2 | 156 | else |
| danlock10y | 6:57b9a6427bc2 | 157 | { |
| danlock10y | 6:57b9a6427bc2 | 158 | Direction = 3; //Direction variable is a two bit number 0-3 3 is left |
| danlock10y | 6:57b9a6427bc2 | 159 | |
| danlock10y | 6:57b9a6427bc2 | 160 | lefthigh = 1; //Light up the left LED, make sure all others are off |
| danlock10y | 6:57b9a6427bc2 | 161 | leftlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 162 | righthigh = 0; |
| danlock10y | 6:57b9a6427bc2 | 163 | rightlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 164 | forwardhigh = 0; |
| danlock10y | 6:57b9a6427bc2 | 165 | forwardlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 166 | backwardhigh = 0; |
| danlock10y | 6:57b9a6427bc2 | 167 | backwardlow = 0; |
| danlock10y | 6:57b9a6427bc2 | 168 | |
| danlock10y | 6:57b9a6427bc2 | 169 | }//endelse |
| danlock10y | 6:57b9a6427bc2 | 170 | }//endelse |
| danlock10y | 6:57b9a6427bc2 | 171 | |
| danlock10y | 6:57b9a6427bc2 | 172 | printf("Direction = %d \r\n", Direction); //Print the direction variable to screen for debugging |
| danlock10y | 6:57b9a6427bc2 | 173 | |
| danlock10y | 6:57b9a6427bc2 | 174 | }//endwhile |
| danlock10y | 6:57b9a6427bc2 | 175 | }//endmain |
| danlock10y | 6:57b9a6427bc2 | 176 |
