Haozun Sun
/
Accelerometer2_hzsun
I got the structure of the code but did not get every details right
main.cpp@7:884fb008cee8, 2020-03-06 (annotated)
- Committer:
- hzsun
- Date:
- Fri Mar 06 02:13:24 2020 +0000
- Revision:
- 7:884fb008cee8
- Parent:
- 6:d58d982c647a
zxzx
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 | |
hzsun | 7:884fb008cee8 | 4 | PinName const SDA = PTE25; |
hzsun | 7:884fb008cee8 | 5 | PinName const SCL = PTE24; |
hzsun | 7:884fb008cee8 | 6 | EventQueue queue; |
hzsun | 7:884fb008cee8 | 7 | Serial pc(USBTX, USBRX); // tx, rx |
hzsun | 7:884fb008cee8 | 8 | |
hzsun | 7:884fb008cee8 | 9 | Thread eventThread ; |
hzsun | 7:884fb008cee8 | 10 | DigitalOut rled(LED1); //red led |
hzsun | 7:884fb008cee8 | 11 | DigitalOut gled(LED2); //green led |
hzsun | 7:884fb008cee8 | 12 | DigitalOut bled(LED3); //blue led |
hzsun | 7:884fb008cee8 | 13 | |
hzsun | 7:884fb008cee8 | 14 | |
hzsun | 7:884fb008cee8 | 15 | EventFlags event_flags; |
hzsun | 7:884fb008cee8 | 16 | Timer A1; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 17 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
hzsun | 7:884fb008cee8 | 18 | enum pos {up, down, left, right, flat, over, intermediate}; |
hzsun | 7:884fb008cee8 | 19 | volatile int pos = intermediate; |
hzsun | 7:884fb008cee8 | 20 | enum state {first, second, third, success, e}; |
hzsun | 7:884fb008cee8 | 21 | volatile int state = first; |
hzsun | 7:884fb008cee8 | 22 | volatile uint32_t flags_read = 1; |
hzsun | 7:884fb008cee8 | 23 | |
hzsun | 7:884fb008cee8 | 24 | void seqchecking() |
hzsun | 7:884fb008cee8 | 25 | { |
hzsun | 7:884fb008cee8 | 26 | switch(state) { |
190299053 | 6:d58d982c647a | 27 | case first: |
190299053 | 6:d58d982c647a | 28 | flags_read = event_flags.wait_any(up|down|right|left|over|flat|intermediate,10000,true); |
190299053 | 6:d58d982c647a | 29 | pc.printf("flag_read:%d\r\n", flags_read); |
190299053 | 6:d58d982c647a | 30 | //if(flags_read == -2){rled = 1;} |
190299053 | 6:d58d982c647a | 31 | //else{ |
hzsun | 7:884fb008cee8 | 32 | if(flags_read == 4) { |
hzsun | 7:884fb008cee8 | 33 | if(flags_read == -2) { |
hzsun | 7:884fb008cee8 | 34 | state = second; |
hzsun | 7:884fb008cee8 | 35 | A1.reset(); |
hzsun | 7:884fb008cee8 | 36 | pc.printf("turn right please"); |
hzsun | 7:884fb008cee8 | 37 | } |
hzsun | 7:884fb008cee8 | 38 | } |
hzsun | 7:884fb008cee8 | 39 | if(flags_read != 4 && A1.read() < 10) { |
hzsun | 7:884fb008cee8 | 40 | state = e; |
hzsun | 7:884fb008cee8 | 41 | } |
hzsun | 7:884fb008cee8 | 42 | if((flags_read != 4 && flags_read != 6) && A1.read() < 10) { |
hzsun | 7:884fb008cee8 | 43 | state = e; |
hzsun | 7:884fb008cee8 | 44 | pc.printf("b"); |
hzsun | 7:884fb008cee8 | 45 | } |
190299053 | 6:d58d982c647a | 46 | //} |
190299053 | 6:d58d982c647a | 47 | break; |
190299053 | 6:d58d982c647a | 48 | case second: |
190299053 | 6:d58d982c647a | 49 | flags_read = event_flags.wait_any(up|down|right|left|over|flat|intermediate,6000,true); |
hzsun | 7:884fb008cee8 | 50 | if(flags_read != 3 && A1.read() < 2) { |
hzsun | 7:884fb008cee8 | 51 | state = e; |
hzsun | 7:884fb008cee8 | 52 | } |
hzsun | 7:884fb008cee8 | 53 | if(flags_read == 0 && A1.read() >= 2) { |
hzsun | 7:884fb008cee8 | 54 | state = second; |
hzsun | 7:884fb008cee8 | 55 | A1.reset(); |
hzsun | 7:884fb008cee8 | 56 | pc.printf("turn upward please"); |
hzsun | 7:884fb008cee8 | 57 | } |
hzsun | 7:884fb008cee8 | 58 | if((flags_read != 3 && flags_read != 0)) { |
hzsun | 7:884fb008cee8 | 59 | state = e; |
hzsun | 7:884fb008cee8 | 60 | } |
190299053 | 6:d58d982c647a | 61 | break; |
190299053 | 6:d58d982c647a | 62 | case third: |
190299053 | 6:d58d982c647a | 63 | flags_read = event_flags.wait_any(up|down|right|left|over|flat|intermediate,8000,true); |
hzsun | 7:884fb008cee8 | 64 | if(flags_read != 0 && A1.read() < 4) { |
hzsun | 7:884fb008cee8 | 65 | state = e; |
hzsun | 7:884fb008cee8 | 66 | } |
hzsun | 7:884fb008cee8 | 67 | if(flags_read == 4 && A1.read() >= 4) { |
hzsun | 7:884fb008cee8 | 68 | state = second; |
hzsun | 7:884fb008cee8 | 69 | A1.reset(); |
hzsun | 7:884fb008cee8 | 70 | } |
hzsun | 7:884fb008cee8 | 71 | if((flags_read != 4 && flags_read != 0)) { |
hzsun | 7:884fb008cee8 | 72 | state = e; |
hzsun | 7:884fb008cee8 | 73 | } |
190299053 | 6:d58d982c647a | 74 | break; |
190299053 | 6:d58d982c647a | 75 | case success: |
190299053 | 6:d58d982c647a | 76 | gled = 0; |
190299053 | 6:d58d982c647a | 77 | break; |
190299053 | 6:d58d982c647a | 78 | case e: |
190299053 | 6:d58d982c647a | 79 | rled = 0; |
190299053 | 6:d58d982c647a | 80 | event_flags.wait_any(flat, osWaitForever, true); |
190299053 | 6:d58d982c647a | 81 | state = first; |
hzsun | 7:884fb008cee8 | 82 | A1.reset(); |
190299053 | 6:d58d982c647a | 83 | break; |
hzsun | 7:884fb008cee8 | 84 | } |
190299053 | 6:d58d982c647a | 85 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 86 | int main(void) |
WilliamMarshQMUL | 0:a1caba5c4e48 | 87 | { |
WilliamMarshQMUL | 0:a1caba5c4e48 | 88 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
190299053 | 6:d58d982c647a | 89 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 90 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 91 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 92 | pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
190299053 | 6:d58d982c647a | 93 | |
190299053 | 6:d58d982c647a | 94 | rled = 1; // turn off all leds when initialising |
190299053 | 6:d58d982c647a | 95 | gled = 1; |
190299053 | 6:d58d982c647a | 96 | bled = 1; |
190299053 | 6:d58d982c647a | 97 | x.start(); |
190299053 | 6:d58d982c647a | 98 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
190299053 | 6:d58d982c647a | 99 | queue.call_every(10, seqchecking) ; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 100 | while (true) { |
WilliamMarshQMUL | 0:a1caba5c4e48 | 101 | float x, y, z; |
190299053 | 6:d58d982c647a | 102 | x = acc.getAccX(); // get x axis value |
190299053 | 6:d58d982c647a | 103 | y = acc.getAccY(); // get y axis value |
190299053 | 6:d58d982c647a | 104 | z = acc.getAccZ(); // get z axis value |
WilliamMarshQMUL | 4:72b8fb7423dd | 105 | ThisThread::sleep_for(300); // wait(0.3); |
190299053 | 6:d58d982c647a | 106 | |
190299053 | 6:d58d982c647a | 107 | switch (pos){ |
190299053 | 6:d58d982c647a | 108 | case intermediate: |
190299053 | 6:d58d982c647a | 109 | if(x > 0.9){ //if the board is in position down |
190299053 | 6:d58d982c647a | 110 | pos = down; |
190299053 | 6:d58d982c647a | 111 | event_flags.set(down); |
190299053 | 6:d58d982c647a | 112 | pc.printf("down\n"); // print up on pc screen |
190299053 | 6:d58d982c647a | 113 | } |
190299053 | 6:d58d982c647a | 114 | if(x < -0.9){ //if the board is in position up |
190299053 | 6:d58d982c647a | 115 | pos = up; |
190299053 | 6:d58d982c647a | 116 | event_flags.set(up); |
190299053 | 6:d58d982c647a | 117 | pc.printf("Up\n");// print down on pc screen |
190299053 | 6:d58d982c647a | 118 | } |
190299053 | 6:d58d982c647a | 119 | if(y > 0.9){ //if the board is in position left |
190299053 | 6:d58d982c647a | 120 | pos = left; |
190299053 | 6:d58d982c647a | 121 | event_flags.set(left); |
190299053 | 6:d58d982c647a | 122 | pc.printf("left\n");// print left on pc screen |
190299053 | 6:d58d982c647a | 123 | } |
190299053 | 6:d58d982c647a | 124 | if(y < -0.9){ //if the board is in position right |
190299053 | 6:d58d982c647a | 125 | pos = right; |
190299053 | 6:d58d982c647a | 126 | event_flags.set(right); |
190299053 | 6:d58d982c647a | 127 | pc.printf("right\n");// print right on pc screen |
190299053 | 6:d58d982c647a | 128 | } |
190299053 | 6:d58d982c647a | 129 | if(z > 0.9){ //if the board is in position flat |
190299053 | 6:d58d982c647a | 130 | pos = flat; |
190299053 | 6:d58d982c647a | 131 | event_flags.set(flat); |
190299053 | 6:d58d982c647a | 132 | pc.printf("Flat\n");// print flat on pc screen |
190299053 | 6:d58d982c647a | 133 | } |
190299053 | 6:d58d982c647a | 134 | if(z < -0.9){ //if the board is in position over |
190299053 | 6:d58d982c647a | 135 | pos = over; |
190299053 | 6:d58d982c647a | 136 | event_flags.set(over); |
190299053 | 6:d58d982c647a | 137 | pc.printf("Over\n");// print over on pc screen |
190299053 | 6:d58d982c647a | 138 | } |
190299053 | 6:d58d982c647a | 139 | if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){ |
190299053 | 6:d58d982c647a | 140 | pos = intermediate; |
190299053 | 6:d58d982c647a | 141 | event_flags.set(intermediate); |
190299053 | 6:d58d982c647a | 142 | } |
190299053 | 6:d58d982c647a | 143 | break; |
hzsun | 7:884fb008cee8 | 144 | case flat: |
hzsun | 7:884fb008cee8 | 145 | if(z<0.8) { |
hzsun | 7:884fb008cee8 | 146 | pos=intermediate; |
190299053 | 6:d58d982c647a | 147 | } |
190299053 | 6:d58d982c647a | 148 | break; |
hzsun | 7:884fb008cee8 | 149 | |
hzsun | 7:884fb008cee8 | 150 | case over: |
hzsun | 7:884fb008cee8 | 151 | if(z>-0.8) { |
hzsun | 7:884fb008cee8 | 152 | pos=intermediate; |
190299053 | 6:d58d982c647a | 153 | } |
190299053 | 6:d58d982c647a | 154 | break; |
hzsun | 7:884fb008cee8 | 155 | |
190299053 | 6:d58d982c647a | 156 | case left: |
hzsun | 7:884fb008cee8 | 157 | if(y>-0.8) { |
hzsun | 7:884fb008cee8 | 158 | pos=intermediate; |
190299053 | 6:d58d982c647a | 159 | } |
190299053 | 6:d58d982c647a | 160 | break; |
hzsun | 7:884fb008cee8 | 161 | |
190299053 | 6:d58d982c647a | 162 | case right: |
hzsun | 7:884fb008cee8 | 163 | if(y<0.8) { |
hzsun | 7:884fb008cee8 | 164 | pos=intermediate; |
190299053 | 6:d58d982c647a | 165 | } |
190299053 | 6:d58d982c647a | 166 | break; |
hzsun | 7:884fb008cee8 | 167 | |
hzsun | 7:884fb008cee8 | 168 | case up: |
hzsun | 7:884fb008cee8 | 169 | if(x<0.8) { |
hzsun | 7:884fb008cee8 | 170 | pos=intermediate; |
190299053 | 6:d58d982c647a | 171 | } |
190299053 | 6:d58d982c647a | 172 | break; |
hzsun | 7:884fb008cee8 | 173 | |
hzsun | 7:884fb008cee8 | 174 | case down: |
hzsun | 7:884fb008cee8 | 175 | if(x>-0.8) { |
hzsun | 7:884fb008cee8 | 176 | pos=intermediate; |
190299053 | 6:d58d982c647a | 177 | } |
190299053 | 6:d58d982c647a | 178 | break; |
190299053 | 6:d58d982c647a | 179 | } |
hzsun | 7:884fb008cee8 | 180 | |
hzsun | 7:884fb008cee8 | 181 | } |
190299053 | 6:d58d982c647a | 182 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 183 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 184 | } |