Final Version of food controller
Dependencies: MMA8451Q TSI mbed
Fork of foodcontroller_NoWiFi by
main.cpp@4:1961b60aa8fb, 2016-06-09 (annotated)
- Committer:
- danlock10y
- Date:
- Thu Jun 09 10:31:59 2016 +0000
- Revision:
- 4:1961b60aa8fb
- Parent:
- 3:768cca7d53fe
- Child:
- 5:c4bc5a92390a
Working food controller with 2 axis and variable velocity; ; needs comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pemb4660 | 0:c5a6d47904dc | 1 | #include "mbed.h" |
pemb4660 | 0:c5a6d47904dc | 2 | #include "TSISensor.h" |
pemb4660 | 0:c5a6d47904dc | 3 | #include "MMA8451Q.h" |
pemb4660 | 0:c5a6d47904dc | 4 | |
pemb4660 | 0:c5a6d47904dc | 5 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
pemb4660 | 0:c5a6d47904dc | 6 | Serial pc(USBTX, USBRX); // tx, rx |
pemb4660 | 0:c5a6d47904dc | 7 | |
danlock10y | 1:6dd9eca697db | 8 | PwmOut gled(LED_GREEN); //Indicator for touch sensor Output |
danlock10y | 1:6dd9eca697db | 9 | TSISensor tsi; //Setup touch sensor |
danlock10y | 1:6dd9eca697db | 10 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); //Setup accelerometer |
danlock10y | 1:6dd9eca697db | 11 | DigitalOut lefthigh(PTC11); //Test LED for left |
danlock10y | 1:6dd9eca697db | 12 | DigitalOut leftlow(PTC9); |
danlock10y | 1:6dd9eca697db | 13 | DigitalOut righthigh(PTC5); //Test LED for right |
danlock10y | 1:6dd9eca697db | 14 | DigitalOut rightlow(PTA4); |
danlock10y | 1:6dd9eca697db | 15 | DigitalOut forwardhigh(PTC16); //Test LED for forward |
danlock10y | 1:6dd9eca697db | 16 | DigitalOut forwardlow(PTD0); |
danlock10y | 1:6dd9eca697db | 17 | DigitalOut backwardhigh(PTA17); //Test LED for backward |
danlock10y | 1:6dd9eca697db | 18 | DigitalOut backwardlow(PTD1); |
danlock10y | 1:6dd9eca697db | 19 | |
danlock10y | 1:6dd9eca697db | 20 | float accX=acc.getAccX(); //Measure acceleration in X direction |
danlock10y | 1:6dd9eca697db | 21 | float accY=acc.getAccY(); //Measure acceleration in Y direction |
danlock10y | 1:6dd9eca697db | 22 | |
danlock10y | 1:6dd9eca697db | 23 | int Direction; //2 bit number to describe direction 0 forward assigned CW |
danlock10y | 1:6dd9eca697db | 24 | int Velocity; //2 bit number to vary velocity in game |
danlock10y | 1:6dd9eca697db | 25 | |
danlock10y | 1:6dd9eca697db | 26 | bool right; //Right or left? right = 1 |
danlock10y | 1:6dd9eca697db | 27 | bool forward; //Forward or Backward? forward = 1 |
danlock10y | 1:6dd9eca697db | 28 | |
pemb4660 | 0:c5a6d47904dc | 29 | int main() |
pemb4660 | 0:c5a6d47904dc | 30 | { |
danlock10y | 1:6dd9eca697db | 31 | while (1) |
danlock10y | 1:6dd9eca697db | 32 | { |
danlock10y | 3:768cca7d53fe | 33 | |
danlock10y | 3:768cca7d53fe | 34 | float accX=acc.getAccX(); //Measure acceleration in X direction |
danlock10y | 3:768cca7d53fe | 35 | float accY=acc.getAccY(); //Measure acceleration in Y direction |
danlock10y | 3:768cca7d53fe | 36 | |
danlock10y | 3:768cca7d53fe | 37 | if (tsi.readDistance() != 0) |
danlock10y | 1:6dd9eca697db | 38 | { |
danlock10y | 2:a6db66722be0 | 39 | // gled = 1.0 - tsi.readPercentage(); |
danlock10y | 2:a6db66722be0 | 40 | if (tsi.readDistance() <= 13) |
danlock10y | 2:a6db66722be0 | 41 | { |
danlock10y | 2:a6db66722be0 | 42 | Velocity = 3; |
danlock10y | 2:a6db66722be0 | 43 | } |
danlock10y | 2:a6db66722be0 | 44 | if (tsi.readDistance() > 13 && tsi.readDistance() < 26) |
danlock10y | 2:a6db66722be0 | 45 | { |
danlock10y | 2:a6db66722be0 | 46 | Velocity = 2; |
danlock10y | 2:a6db66722be0 | 47 | } |
danlock10y | 2:a6db66722be0 | 48 | if (tsi.readDistance() >= 26) |
danlock10y | 2:a6db66722be0 | 49 | { |
danlock10y | 2:a6db66722be0 | 50 | Velocity = 1; |
danlock10y | 2:a6db66722be0 | 51 | } |
danlock10y | 2:a6db66722be0 | 52 | |
danlock10y | 2:a6db66722be0 | 53 | printf("x=%d\r\n",Velocity); |
danlock10y | 2:a6db66722be0 | 54 | wait(0.2); |
danlock10y | 2:a6db66722be0 | 55 | |
danlock10y | 2:a6db66722be0 | 56 | |
danlock10y | 3:768cca7d53fe | 57 | }//endif touch sensor |
danlock10y | 3:768cca7d53fe | 58 | |
danlock10y | 3:768cca7d53fe | 59 | |
danlock10y | 1:6dd9eca697db | 60 | if((accX <= 0.1f) && (accX >= -0.1f)) |
danlock10y | 1:6dd9eca697db | 61 | { |
danlock10y | 3:768cca7d53fe | 62 | if (right == false) |
danlock10y | 1:6dd9eca697db | 63 | { |
danlock10y | 1:6dd9eca697db | 64 | righthigh = 1; |
danlock10y | 1:6dd9eca697db | 65 | rightlow = 0; |
danlock10y | 1:6dd9eca697db | 66 | } //endif |
danlock10y | 1:6dd9eca697db | 67 | else |
danlock10y | 1:6dd9eca697db | 68 | { |
danlock10y | 1:6dd9eca697db | 69 | lefthigh = 1; |
danlock10y | 1:6dd9eca697db | 70 | leftlow = 0; |
danlock10y | 1:6dd9eca697db | 71 | }//endelse |
danlock10y | 1:6dd9eca697db | 72 | }//endif |
danlock10y | 3:768cca7d53fe | 73 | |
danlock10y | 1:6dd9eca697db | 74 | if (accX > 0.1) |
danlock10y | 1:6dd9eca697db | 75 | { |
danlock10y | 1:6dd9eca697db | 76 | lefthigh = 0; |
danlock10y | 1:6dd9eca697db | 77 | leftlow = 0; |
danlock10y | 1:6dd9eca697db | 78 | righthigh = 1; |
danlock10y | 1:6dd9eca697db | 79 | rightlow = 0; |
danlock10y | 1:6dd9eca697db | 80 | forwardhigh = 0; |
danlock10y | 1:6dd9eca697db | 81 | forwardlow = 0; |
danlock10y | 1:6dd9eca697db | 82 | backwardhigh = 0; |
danlock10y | 1:6dd9eca697db | 83 | backwardlow = 0; |
danlock10y | 3:768cca7d53fe | 84 | right = false; |
danlock10y | 3:768cca7d53fe | 85 | printf("left \r\n"); |
danlock10y | 1:6dd9eca697db | 86 | }//endif |
pemb4660 | 0:c5a6d47904dc | 87 | |
danlock10y | 1:6dd9eca697db | 88 | if (accX < -0.1) |
danlock10y | 1:6dd9eca697db | 89 | { |
danlock10y | 1:6dd9eca697db | 90 | lefthigh = 1; |
danlock10y | 1:6dd9eca697db | 91 | leftlow = 0; |
danlock10y | 1:6dd9eca697db | 92 | righthigh = 0; |
danlock10y | 1:6dd9eca697db | 93 | rightlow = 0; |
danlock10y | 1:6dd9eca697db | 94 | forwardhigh = 0; |
danlock10y | 1:6dd9eca697db | 95 | forwardlow = 0; |
danlock10y | 1:6dd9eca697db | 96 | backwardhigh = 0; |
danlock10y | 1:6dd9eca697db | 97 | backwardlow = 0; |
danlock10y | 3:768cca7d53fe | 98 | right = true; |
danlock10y | 3:768cca7d53fe | 99 | printf("right \r\n"); |
danlock10y | 1:6dd9eca697db | 100 | }//endif |
danlock10y | 3:768cca7d53fe | 101 | wait(0.1); |
danlock10y | 1:6dd9eca697db | 102 | |
danlock10y | 3:768cca7d53fe | 103 | if((accY <= 0.1) && (accY >= -0.1)) |
danlock10y | 1:6dd9eca697db | 104 | { |
danlock10y | 3:768cca7d53fe | 105 | if (forward == false) |
danlock10y | 1:6dd9eca697db | 106 | { |
pemb4660 | 0:c5a6d47904dc | 107 | forwardhigh = 1; |
danlock10y | 1:6dd9eca697db | 108 | forwardlow = 0; |
danlock10y | 1:6dd9eca697db | 109 | }//endif |
danlock10y | 1:6dd9eca697db | 110 | else |
danlock10y | 1:6dd9eca697db | 111 | { |
pemb4660 | 0:c5a6d47904dc | 112 | backwardhigh = 1; |
danlock10y | 1:6dd9eca697db | 113 | backwardlow = 0; |
danlock10y | 1:6dd9eca697db | 114 | }//endelse |
danlock10y | 1:6dd9eca697db | 115 | }//endif |
pemb4660 | 0:c5a6d47904dc | 116 | |
danlock10y | 3:768cca7d53fe | 117 | if (accY > 0.1) |
danlock10y | 1:6dd9eca697db | 118 | { |
danlock10y | 1:6dd9eca697db | 119 | lefthigh = 0; |
danlock10y | 1:6dd9eca697db | 120 | leftlow = 0; |
danlock10y | 1:6dd9eca697db | 121 | righthigh = 0; |
danlock10y | 1:6dd9eca697db | 122 | rightlow = 0; |
danlock10y | 1:6dd9eca697db | 123 | forwardhigh = 1; |
danlock10y | 1:6dd9eca697db | 124 | forwardlow = 0; |
danlock10y | 1:6dd9eca697db | 125 | backwardhigh = 0; |
danlock10y | 1:6dd9eca697db | 126 | backwardlow = 0; |
danlock10y | 3:768cca7d53fe | 127 | forward = false; |
danlock10y | 3:768cca7d53fe | 128 | printf("back \r\n"); |
danlock10y | 1:6dd9eca697db | 129 | }//endif |
pemb4660 | 0:c5a6d47904dc | 130 | |
danlock10y | 3:768cca7d53fe | 131 | if (accY < -0.1) |
danlock10y | 1:6dd9eca697db | 132 | { |
danlock10y | 1:6dd9eca697db | 133 | lefthigh = 0; |
danlock10y | 1:6dd9eca697db | 134 | leftlow = 0; |
danlock10y | 1:6dd9eca697db | 135 | righthigh = 0; |
danlock10y | 1:6dd9eca697db | 136 | rightlow = 0; |
danlock10y | 1:6dd9eca697db | 137 | forwardhigh = 0; |
danlock10y | 1:6dd9eca697db | 138 | forwardlow = 0; |
danlock10y | 1:6dd9eca697db | 139 | backwardhigh = 1; |
danlock10y | 1:6dd9eca697db | 140 | backwardlow = 0; |
danlock10y | 3:768cca7d53fe | 141 | forward = true; |
danlock10y | 3:768cca7d53fe | 142 | printf("for \r\n"); |
danlock10y | 1:6dd9eca697db | 143 | }//endif |
danlock10y | 3:768cca7d53fe | 144 | |
danlock10y | 3:768cca7d53fe | 145 | wait(0.1); |
danlock10y | 3:768cca7d53fe | 146 | |
danlock10y | 4:1961b60aa8fb | 147 | if(abs(accY) > abs(accX)) |
danlock10y | 3:768cca7d53fe | 148 | { |
danlock10y | 4:1961b60aa8fb | 149 | if(forward == true) |
danlock10y | 4:1961b60aa8fb | 150 | { |
danlock10y | 4:1961b60aa8fb | 151 | Direction = 0; |
danlock10y | 4:1961b60aa8fb | 152 | }//endif |
danlock10y | 4:1961b60aa8fb | 153 | else |
danlock10y | 3:768cca7d53fe | 154 | { |
danlock10y | 4:1961b60aa8fb | 155 | Direction = 2; |
danlock10y | 4:1961b60aa8fb | 156 | }//endelse |
danlock10y | 4:1961b60aa8fb | 157 | }//endif |
danlock10y | 4:1961b60aa8fb | 158 | else |
danlock10y | 4:1961b60aa8fb | 159 | { |
danlock10y | 4:1961b60aa8fb | 160 | if(right == true) |
danlock10y | 4:1961b60aa8fb | 161 | { |
danlock10y | 4:1961b60aa8fb | 162 | Direction = 1; |
danlock10y | 4:1961b60aa8fb | 163 | }//endif |
danlock10y | 4:1961b60aa8fb | 164 | else |
danlock10y | 4:1961b60aa8fb | 165 | { |
danlock10y | 4:1961b60aa8fb | 166 | Direction = 3; |
danlock10y | 4:1961b60aa8fb | 167 | }//endelse |
danlock10y | 4:1961b60aa8fb | 168 | }//endelse |
danlock10y | 4:1961b60aa8fb | 169 | |
danlock10y | 4:1961b60aa8fb | 170 | printf("Direction = %d \r\n", Direction); |
danlock10y | 4:1961b60aa8fb | 171 | |
danlock10y | 1:6dd9eca697db | 172 | }//endwhile |
danlock10y | 1:6dd9eca697db | 173 | }//endmain |