Haozun Sun
/
Accelerometer2_hzsun
I got the structure of the code but did not get every details right
main.cpp@6:d58d982c647a, 2020-03-05 (annotated)
- Committer:
- 190299053
- Date:
- Thu Mar 05 21:13:16 2020 +0000
- Revision:
- 6:d58d982c647a
- Parent:
- 4:72b8fb7423dd
- Child:
- 7:884fb008cee8
Question 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 0:a1caba5c4e48 | 1 | #include "mbed.h" |
WilliamMarshQMUL | 0:a1caba5c4e48 | 2 | #include "MMA8451Q.h" |
WilliamMarshQMUL | 0:a1caba5c4e48 | 3 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 4 | PinName const SDA = PTE25; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 5 | PinName const SCL = PTE24; |
190299053 | 6:d58d982c647a | 6 | EventQueue queue; |
190299053 | 6:d58d982c647a | 7 | Serial pc(USBTX, USBRX); // tx, rx |
190299053 | 6:d58d982c647a | 8 | Thread eventThread ; |
190299053 | 6:d58d982c647a | 9 | DigitalOut rled(LED1, 1); //red led |
190299053 | 6:d58d982c647a | 10 | DigitalOut gled(LED2,1); //green led |
190299053 | 6:d58d982c647a | 11 | DigitalOut bled(LED3,1); //blue led |
190299053 | 6:d58d982c647a | 12 | EventFlags event_flags; |
190299053 | 6:d58d982c647a | 13 | Timer x; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 14 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
190299053 | 6:d58d982c647a | 15 | enum pos {up, down, left, right, flat, over, intermediate}; |
190299053 | 6:d58d982c647a | 16 | volatile int pos = intermediate; |
190299053 | 6:d58d982c647a | 17 | enum state {first, second, third, success, e}; |
190299053 | 6:d58d982c647a | 18 | volatile int state = first; |
190299053 | 6:d58d982c647a | 19 | volatile uint32_t flags_read = 1; |
190299053 | 6:d58d982c647a | 20 | void seqchecking(){ |
190299053 | 6:d58d982c647a | 21 | switch(state){ |
190299053 | 6:d58d982c647a | 22 | case first: |
190299053 | 6:d58d982c647a | 23 | flags_read = event_flags.wait_any(up|down|right|left|over|flat|intermediate,10000,true); |
190299053 | 6:d58d982c647a | 24 | pc.printf("flag_read:%d\r\n", flags_read); |
190299053 | 6:d58d982c647a | 25 | //if(flags_read == -2){rled = 1;} |
190299053 | 6:d58d982c647a | 26 | //else{ |
190299053 | 6:d58d982c647a | 27 | if(flags_read == 4){if(flags_read == -2){state = second; x.reset();pc.printf("turn right please");}} |
190299053 | 6:d58d982c647a | 28 | if(flags_read != 4 && x.read() < 10){state = e;} |
190299053 | 6:d58d982c647a | 29 | if((flags_read != 4 && flags_read != 6) && x.read() < 10){state = e;pc.printf("b");} |
190299053 | 6:d58d982c647a | 30 | //} |
190299053 | 6:d58d982c647a | 31 | break; |
190299053 | 6:d58d982c647a | 32 | case second: |
190299053 | 6:d58d982c647a | 33 | flags_read = event_flags.wait_any(up|down|right|left|over|flat|intermediate,6000,true); |
190299053 | 6:d58d982c647a | 34 | if(flags_read != 3 && x.read() < 2){state = e;} |
190299053 | 6:d58d982c647a | 35 | if(flags_read == 0 && x.read() >= 2){state = second; x.reset();pc.printf("turn upward please");} |
190299053 | 6:d58d982c647a | 36 | if((flags_read != 3 && flags_read != 0)){state = e;} |
190299053 | 6:d58d982c647a | 37 | break; |
190299053 | 6:d58d982c647a | 38 | case third: |
190299053 | 6:d58d982c647a | 39 | flags_read = event_flags.wait_any(up|down|right|left|over|flat|intermediate,8000,true); |
190299053 | 6:d58d982c647a | 40 | if(flags_read != 0 && x.read() < 4){state = e;} |
190299053 | 6:d58d982c647a | 41 | if(flags_read == 4 && x.read() >= 4){state = second; x.reset();} |
190299053 | 6:d58d982c647a | 42 | if((flags_read != 4 && flags_read != 0)){state = e;} |
190299053 | 6:d58d982c647a | 43 | break; |
190299053 | 6:d58d982c647a | 44 | case success: |
190299053 | 6:d58d982c647a | 45 | gled = 0; |
190299053 | 6:d58d982c647a | 46 | break; |
190299053 | 6:d58d982c647a | 47 | case e: |
190299053 | 6:d58d982c647a | 48 | rled = 0; |
190299053 | 6:d58d982c647a | 49 | event_flags.wait_any(flat, osWaitForever, true); |
190299053 | 6:d58d982c647a | 50 | state = first; |
190299053 | 6:d58d982c647a | 51 | x.reset(); |
190299053 | 6:d58d982c647a | 52 | break; |
190299053 | 6:d58d982c647a | 53 | } |
190299053 | 6:d58d982c647a | 54 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 55 | int main(void) |
WilliamMarshQMUL | 0:a1caba5c4e48 | 56 | { |
WilliamMarshQMUL | 0:a1caba5c4e48 | 57 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
190299053 | 6:d58d982c647a | 58 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 59 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 60 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 61 | pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
190299053 | 6:d58d982c647a | 62 | |
190299053 | 6:d58d982c647a | 63 | rled = 1; // turn off all leds when initialising |
190299053 | 6:d58d982c647a | 64 | gled = 1; |
190299053 | 6:d58d982c647a | 65 | bled = 1; |
190299053 | 6:d58d982c647a | 66 | x.start(); |
190299053 | 6:d58d982c647a | 67 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
190299053 | 6:d58d982c647a | 68 | queue.call_every(10, seqchecking) ; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 69 | while (true) { |
WilliamMarshQMUL | 0:a1caba5c4e48 | 70 | float x, y, z; |
190299053 | 6:d58d982c647a | 71 | x = acc.getAccX(); // get x axis value |
190299053 | 6:d58d982c647a | 72 | y = acc.getAccY(); // get y axis value |
190299053 | 6:d58d982c647a | 73 | z = acc.getAccZ(); // get z axis value |
WilliamMarshQMUL | 4:72b8fb7423dd | 74 | ThisThread::sleep_for(300); // wait(0.3); |
190299053 | 6:d58d982c647a | 75 | |
190299053 | 6:d58d982c647a | 76 | switch (pos){ |
190299053 | 6:d58d982c647a | 77 | case intermediate: |
190299053 | 6:d58d982c647a | 78 | if(x > 0.9){ //if the board is in position down |
190299053 | 6:d58d982c647a | 79 | pos = down; |
190299053 | 6:d58d982c647a | 80 | event_flags.set(down); |
190299053 | 6:d58d982c647a | 81 | pc.printf("down\n"); // print up on pc screen |
190299053 | 6:d58d982c647a | 82 | } |
190299053 | 6:d58d982c647a | 83 | if(x < -0.9){ //if the board is in position up |
190299053 | 6:d58d982c647a | 84 | pos = up; |
190299053 | 6:d58d982c647a | 85 | event_flags.set(up); |
190299053 | 6:d58d982c647a | 86 | pc.printf("Up\n");// print down on pc screen |
190299053 | 6:d58d982c647a | 87 | } |
190299053 | 6:d58d982c647a | 88 | if(y > 0.9){ //if the board is in position left |
190299053 | 6:d58d982c647a | 89 | pos = left; |
190299053 | 6:d58d982c647a | 90 | event_flags.set(left); |
190299053 | 6:d58d982c647a | 91 | pc.printf("left\n");// print left on pc screen |
190299053 | 6:d58d982c647a | 92 | } |
190299053 | 6:d58d982c647a | 93 | if(y < -0.9){ //if the board is in position right |
190299053 | 6:d58d982c647a | 94 | pos = right; |
190299053 | 6:d58d982c647a | 95 | event_flags.set(right); |
190299053 | 6:d58d982c647a | 96 | pc.printf("right\n");// print right on pc screen |
190299053 | 6:d58d982c647a | 97 | } |
190299053 | 6:d58d982c647a | 98 | if(z > 0.9){ //if the board is in position flat |
190299053 | 6:d58d982c647a | 99 | pos = flat; |
190299053 | 6:d58d982c647a | 100 | event_flags.set(flat); |
190299053 | 6:d58d982c647a | 101 | pc.printf("Flat\n");// print flat on pc screen |
190299053 | 6:d58d982c647a | 102 | } |
190299053 | 6:d58d982c647a | 103 | if(z < -0.9){ //if the board is in position over |
190299053 | 6:d58d982c647a | 104 | pos = over; |
190299053 | 6:d58d982c647a | 105 | event_flags.set(over); |
190299053 | 6:d58d982c647a | 106 | pc.printf("Over\n");// print over on pc screen |
190299053 | 6:d58d982c647a | 107 | } |
190299053 | 6:d58d982c647a | 108 | if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){ |
190299053 | 6:d58d982c647a | 109 | pos = intermediate; |
190299053 | 6:d58d982c647a | 110 | event_flags.set(intermediate); |
190299053 | 6:d58d982c647a | 111 | } |
190299053 | 6:d58d982c647a | 112 | break; |
190299053 | 6:d58d982c647a | 113 | case up: |
190299053 | 6:d58d982c647a | 114 | if(x > 0.9){ //if the board is in position down |
190299053 | 6:d58d982c647a | 115 | pos = down; |
190299053 | 6:d58d982c647a | 116 | pc.printf("Down\n");// print down on pc screen |
190299053 | 6:d58d982c647a | 117 | } |
190299053 | 6:d58d982c647a | 118 | if(y > 0.9){ //if the board is in position left |
190299053 | 6:d58d982c647a | 119 | pos = left; |
190299053 | 6:d58d982c647a | 120 | pc.printf("left\n");// print left on pc screen |
190299053 | 6:d58d982c647a | 121 | } |
190299053 | 6:d58d982c647a | 122 | if(y < -0.9){ //if the board is in position right |
190299053 | 6:d58d982c647a | 123 | pos = right; |
190299053 | 6:d58d982c647a | 124 | pc.printf("right\n");// print right on pc screen |
190299053 | 6:d58d982c647a | 125 | } |
190299053 | 6:d58d982c647a | 126 | if(z > 0.9){ //if the board is in position flat |
190299053 | 6:d58d982c647a | 127 | pos = flat; |
190299053 | 6:d58d982c647a | 128 | pc.printf("Flat\n");// print flat on pc screen |
190299053 | 6:d58d982c647a | 129 | } |
190299053 | 6:d58d982c647a | 130 | if(z < -0.9){ //if the board is in position over |
190299053 | 6:d58d982c647a | 131 | pos = over; |
190299053 | 6:d58d982c647a | 132 | pc.printf("Over\n");// print over on pc screen |
190299053 | 6:d58d982c647a | 133 | } |
190299053 | 6:d58d982c647a | 134 | if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){ |
190299053 | 6:d58d982c647a | 135 | pos = intermediate; |
190299053 | 6:d58d982c647a | 136 | } |
190299053 | 6:d58d982c647a | 137 | break; |
190299053 | 6:d58d982c647a | 138 | case down: |
190299053 | 6:d58d982c647a | 139 | if(x < -0.9){ //if the board is in position up |
190299053 | 6:d58d982c647a | 140 | pos = up; |
190299053 | 6:d58d982c647a | 141 | pc.printf("Up\n"); // print up on pc screen |
190299053 | 6:d58d982c647a | 142 | } |
190299053 | 6:d58d982c647a | 143 | if(y > 0.9){ //if the board is in position left |
190299053 | 6:d58d982c647a | 144 | pos = left; |
190299053 | 6:d58d982c647a | 145 | pc.printf("left\n");// print left on pc screen |
190299053 | 6:d58d982c647a | 146 | } |
190299053 | 6:d58d982c647a | 147 | if(y < -0.9){ //if the board is in position right |
190299053 | 6:d58d982c647a | 148 | pos = right; |
190299053 | 6:d58d982c647a | 149 | pc.printf("right\n");// print right on pc screen |
190299053 | 6:d58d982c647a | 150 | } |
190299053 | 6:d58d982c647a | 151 | if(z > 0.9){ //if the board is in position flat |
190299053 | 6:d58d982c647a | 152 | pos = flat; |
190299053 | 6:d58d982c647a | 153 | pc.printf("Flat\n");// print flat on pc screen |
190299053 | 6:d58d982c647a | 154 | } |
190299053 | 6:d58d982c647a | 155 | if(z < -0.9){ //if the board is in position over |
190299053 | 6:d58d982c647a | 156 | pos = over; |
190299053 | 6:d58d982c647a | 157 | pc.printf("Over\n");// print over on pc screen |
190299053 | 6:d58d982c647a | 158 | } |
190299053 | 6:d58d982c647a | 159 | if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){ |
190299053 | 6:d58d982c647a | 160 | pos = intermediate; |
190299053 | 6:d58d982c647a | 161 | } |
190299053 | 6:d58d982c647a | 162 | break; |
190299053 | 6:d58d982c647a | 163 | case left: |
190299053 | 6:d58d982c647a | 164 | if(x > 0.9){ //if the board is in position down |
190299053 | 6:d58d982c647a | 165 | pos = down; |
190299053 | 6:d58d982c647a | 166 | pc.printf("Down\n"); // print up on pc screen |
190299053 | 6:d58d982c647a | 167 | } |
190299053 | 6:d58d982c647a | 168 | if(x < -0.9){ //if the board is in position up |
190299053 | 6:d58d982c647a | 169 | pos = up; |
190299053 | 6:d58d982c647a | 170 | pc.printf("Up\n");// print down on pc screen |
190299053 | 6:d58d982c647a | 171 | } |
190299053 | 6:d58d982c647a | 172 | if(y < -0.9){ //if the board is in position right |
190299053 | 6:d58d982c647a | 173 | pos = right; |
190299053 | 6:d58d982c647a | 174 | pc.printf("right\n");// print right on pc screen |
190299053 | 6:d58d982c647a | 175 | } |
190299053 | 6:d58d982c647a | 176 | if(z > 0.9){ //if the board is in position flat |
190299053 | 6:d58d982c647a | 177 | pos = flat; |
190299053 | 6:d58d982c647a | 178 | pc.printf("Flat\n");// print flat on pc screen |
190299053 | 6:d58d982c647a | 179 | } |
190299053 | 6:d58d982c647a | 180 | if(z < -0.9){ //if the board is in position over |
190299053 | 6:d58d982c647a | 181 | pos = over; |
190299053 | 6:d58d982c647a | 182 | pc.printf("Over\n");// print over on pc screen |
190299053 | 6:d58d982c647a | 183 | } |
190299053 | 6:d58d982c647a | 184 | if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){ |
190299053 | 6:d58d982c647a | 185 | pos = intermediate; |
190299053 | 6:d58d982c647a | 186 | } |
190299053 | 6:d58d982c647a | 187 | break; |
190299053 | 6:d58d982c647a | 188 | case right: |
190299053 | 6:d58d982c647a | 189 | if(x > 0.9){ //if the board is in position down |
190299053 | 6:d58d982c647a | 190 | pos = down; |
190299053 | 6:d58d982c647a | 191 | pc.printf("Down\n"); // print up on pc screen |
190299053 | 6:d58d982c647a | 192 | } |
190299053 | 6:d58d982c647a | 193 | if(x < -0.9){ //if the board is in position up |
190299053 | 6:d58d982c647a | 194 | pos = up; |
190299053 | 6:d58d982c647a | 195 | pc.printf("Up\n");// print down on pc screen |
190299053 | 6:d58d982c647a | 196 | } |
190299053 | 6:d58d982c647a | 197 | if(y > 0.9){ //if the board is in position left |
190299053 | 6:d58d982c647a | 198 | pos = left; |
190299053 | 6:d58d982c647a | 199 | pc.printf("left\n");// print left on pc screen |
190299053 | 6:d58d982c647a | 200 | } |
190299053 | 6:d58d982c647a | 201 | if(z > 0.9){ //if the board is in position flat |
190299053 | 6:d58d982c647a | 202 | pos = flat; |
190299053 | 6:d58d982c647a | 203 | pc.printf("Flat\n");// print flat on pc screen |
190299053 | 6:d58d982c647a | 204 | } |
190299053 | 6:d58d982c647a | 205 | if(z < -0.9){ //if the board is in position over |
190299053 | 6:d58d982c647a | 206 | pos = over; |
190299053 | 6:d58d982c647a | 207 | pc.printf("Over\n");// print over on pc screen |
190299053 | 6:d58d982c647a | 208 | } |
190299053 | 6:d58d982c647a | 209 | if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){ |
190299053 | 6:d58d982c647a | 210 | pos = intermediate; |
190299053 | 6:d58d982c647a | 211 | } |
190299053 | 6:d58d982c647a | 212 | break; |
190299053 | 6:d58d982c647a | 213 | case flat: |
190299053 | 6:d58d982c647a | 214 | if(x > 0.9){ //if the board is in position down |
190299053 | 6:d58d982c647a | 215 | pos = down; |
190299053 | 6:d58d982c647a | 216 | pc.printf("Down\n"); // print up on pc screen |
190299053 | 6:d58d982c647a | 217 | } |
190299053 | 6:d58d982c647a | 218 | if(x < -0.9){ //if the board is in position up |
190299053 | 6:d58d982c647a | 219 | pos = up; |
190299053 | 6:d58d982c647a | 220 | pc.printf("Up\n");// print down on pc screen |
190299053 | 6:d58d982c647a | 221 | } |
190299053 | 6:d58d982c647a | 222 | if(y > 0.9){ //if the board is in position left |
190299053 | 6:d58d982c647a | 223 | pos = left; |
190299053 | 6:d58d982c647a | 224 | pc.printf("left\n");// print left on pc screen |
190299053 | 6:d58d982c647a | 225 | } |
190299053 | 6:d58d982c647a | 226 | if(y < -0.9){ //if the board is in position right |
190299053 | 6:d58d982c647a | 227 | pos = right; |
190299053 | 6:d58d982c647a | 228 | pc.printf("right\n");// print right on pc screen |
190299053 | 6:d58d982c647a | 229 | } |
190299053 | 6:d58d982c647a | 230 | if(z < -0.9){ //if the board is in position over |
190299053 | 6:d58d982c647a | 231 | pos = over; |
190299053 | 6:d58d982c647a | 232 | pc.printf("Over\n");// print over on pc screen |
190299053 | 6:d58d982c647a | 233 | } |
190299053 | 6:d58d982c647a | 234 | if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){ |
190299053 | 6:d58d982c647a | 235 | pos = intermediate; |
190299053 | 6:d58d982c647a | 236 | } |
190299053 | 6:d58d982c647a | 237 | break; |
190299053 | 6:d58d982c647a | 238 | case over: |
190299053 | 6:d58d982c647a | 239 | if(x > 0.9){ //if the board is in position down |
190299053 | 6:d58d982c647a | 240 | pos = down; |
190299053 | 6:d58d982c647a | 241 | pc.printf("Down\n"); // print up on pc screen |
190299053 | 6:d58d982c647a | 242 | } |
190299053 | 6:d58d982c647a | 243 | if(x < -0.9){ //if the board is in position up |
190299053 | 6:d58d982c647a | 244 | pos = up; |
190299053 | 6:d58d982c647a | 245 | pc.printf("Up\n");// print down on pc screen |
190299053 | 6:d58d982c647a | 246 | } |
190299053 | 6:d58d982c647a | 247 | if(y > 0.9){ //if the board is in position left |
190299053 | 6:d58d982c647a | 248 | pos = left; |
190299053 | 6:d58d982c647a | 249 | pc.printf("left\n");// print left on pc screen |
190299053 | 6:d58d982c647a | 250 | } |
190299053 | 6:d58d982c647a | 251 | if(y < -0.9){ //if the board is in position right |
190299053 | 6:d58d982c647a | 252 | pos = right; |
190299053 | 6:d58d982c647a | 253 | pc.printf("right\n");// print right on pc screen |
190299053 | 6:d58d982c647a | 254 | } |
190299053 | 6:d58d982c647a | 255 | if(z > 0.9){ //if the board is in position flat |
190299053 | 6:d58d982c647a | 256 | pos = flat; |
190299053 | 6:d58d982c647a | 257 | pc.printf("Flat\n");// print flat on pc screen |
190299053 | 6:d58d982c647a | 258 | } |
190299053 | 6:d58d982c647a | 259 | if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){ |
190299053 | 6:d58d982c647a | 260 | pos = intermediate; |
190299053 | 6:d58d982c647a | 261 | } |
190299053 | 6:d58d982c647a | 262 | break; |
190299053 | 6:d58d982c647a | 263 | } |
190299053 | 6:d58d982c647a | 264 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 265 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 266 | } |