![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Part 2 lab 4
Fork of Accelerometer_example by
main.cpp@3:1861f741c2df, 2018-02-28 (annotated)
- Committer:
- Bossman
- Date:
- Wed Feb 28 14:24:52 2018 +0000
- Revision:
- 3:1861f741c2df
- Parent:
- 2:304358fa24aa
Version 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 "rtos.h" |
WilliamMarshQMUL | 0:a1caba5c4e48 | 3 | #include "MMA8451Q.h" |
WilliamMarshQMUL | 0:a1caba5c4e48 | 4 | |
Bossman | 3:1861f741c2df | 5 | PinName const SDA = PTE25; |
Bossman | 3:1861f741c2df | 6 | PinName const SCL = PTE24; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 7 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 8 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
WilliamMarshQMUL | 0:a1caba5c4e48 | 9 | |
Bossman | 3:1861f741c2df | 10 | Timer t; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 11 | int main(void) |
WilliamMarshQMUL | 0:a1caba5c4e48 | 12 | { |
WilliamMarshQMUL | 0:a1caba5c4e48 | 13 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 14 | PwmOut rled(LED1); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 15 | PwmOut gled(LED2); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 16 | PwmOut bled(LED3); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 17 | Serial pc(USBTX, USBRX); // tx, rx |
Bossman | 3:1861f741c2df | 18 | enum s { flat, right, up,error}; |
Bossman | 3:1861f741c2df | 19 | s state=flat; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 20 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 21 | pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 22 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 23 | while (true) { |
WilliamMarshQMUL | 0:a1caba5c4e48 | 24 | float x, y, z; |
Bossman | 3:1861f741c2df | 25 | double th,time1,time2,time3; |
Bossman | 3:1861f741c2df | 26 | th=0.5; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 27 | x = acc.getAccX(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 28 | y = acc.getAccY(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 29 | z = acc.getAccZ(); |
Bossman | 3:1861f741c2df | 30 | //rled = 1.0f - abs(x); |
Bossman | 3:1861f741c2df | 31 | //gled = 1.0f - abs(y); |
Bossman | 3:1861f741c2df | 32 | // bled = 1.0f - abs(z); |
Bossman | 3:1861f741c2df | 33 | if (z>0.5) { |
Bossman | 3:1861f741c2df | 34 | state=flat; |
Bossman | 3:1861f741c2df | 35 | } else if ((x>th)&&(abs(y)<th)&&(abs(z)<th)) { |
Bossman | 3:1861f741c2df | 36 | state=right; |
Bossman | 3:1861f741c2df | 37 | } else if ((y>th)&&(abs(x)<th)&&(abs(z)<th)) { |
Bossman | 3:1861f741c2df | 38 | state=up; |
Bossman | 3:1861f741c2df | 39 | } else { |
Bossman | 3:1861f741c2df | 40 | state=error; |
Bossman | 3:1861f741c2df | 41 | } |
Bossman | 3:1861f741c2df | 42 | switch (state) { |
Bossman | 3:1861f741c2df | 43 | case flat: |
Bossman | 3:1861f741c2df | 44 | t.start(); |
Bossman | 3:1861f741c2df | 45 | gled=0; |
Bossman | 3:1861f741c2df | 46 | bled=1; |
Bossman | 3:1861f741c2df | 47 | rled=1; |
Bossman | 3:1861f741c2df | 48 | pc.printf("flat"); |
Bossman | 3:1861f741c2df | 49 | t.stop(); |
Bossman | 3:1861f741c2df | 50 | time1=t.read(); |
Bossman | 3:1861f741c2df | 51 | t.reset(); |
Bossman | 3:1861f741c2df | 52 | if (time1>=10) { |
Bossman | 3:1861f741c2df | 53 | state=right; |
Bossman | 3:1861f741c2df | 54 | } else { |
Bossman | 3:1861f741c2df | 55 | state=error; |
Bossman | 3:1861f741c2df | 56 | } |
Bossman | 3:1861f741c2df | 57 | break; |
Bossman | 3:1861f741c2df | 58 | case right: |
Bossman | 3:1861f741c2df | 59 | gled=1; |
Bossman | 3:1861f741c2df | 60 | bled=1; |
Bossman | 3:1861f741c2df | 61 | rled=1; |
Bossman | 3:1861f741c2df | 62 | t.start(); |
Bossman | 3:1861f741c2df | 63 | pc.printf("right"); |
Bossman | 3:1861f741c2df | 64 | t.stop(); |
Bossman | 3:1861f741c2df | 65 | time2=t.read(); |
Bossman | 3:1861f741c2df | 66 | t.reset(); |
Bossman | 3:1861f741c2df | 67 | if ((time2>2)&&(time2<6)) { |
Bossman | 3:1861f741c2df | 68 | state=up; |
Bossman | 3:1861f741c2df | 69 | } else { |
Bossman | 3:1861f741c2df | 70 | state=error; |
Bossman | 3:1861f741c2df | 71 | } |
Bossman | 3:1861f741c2df | 72 | break; |
Bossman | 3:1861f741c2df | 73 | case up: |
Bossman | 3:1861f741c2df | 74 | gled=1; |
Bossman | 3:1861f741c2df | 75 | bled=1; |
Bossman | 3:1861f741c2df | 76 | rled=1; |
Bossman | 3:1861f741c2df | 77 | t.start(); |
Bossman | 3:1861f741c2df | 78 | pc.printf("up"); |
Bossman | 3:1861f741c2df | 79 | t.stop(); |
Bossman | 3:1861f741c2df | 80 | time3=t.read(); |
Bossman | 3:1861f741c2df | 81 | t.reset(); |
Bossman | 3:1861f741c2df | 82 | if ((time3>4)&&(time3)<8) { |
Bossman | 3:1861f741c2df | 83 | state=flat; |
Bossman | 3:1861f741c2df | 84 | } else { |
Bossman | 3:1861f741c2df | 85 | state=error; |
Bossman | 3:1861f741c2df | 86 | } |
Bossman | 3:1861f741c2df | 87 | break; |
Bossman | 3:1861f741c2df | 88 | case error: |
Bossman | 3:1861f741c2df | 89 | printf("error"); |
Bossman | 3:1861f741c2df | 90 | rled=0; |
Bossman | 3:1861f741c2df | 91 | gled=1; |
Bossman | 3:1861f741c2df | 92 | bled=1; |
Bossman | 3:1861f741c2df | 93 | break; |
Bossman | 3:1861f741c2df | 94 | |
Bossman | 3:1861f741c2df | 95 | |
Bossman | 3:1861f741c2df | 96 | |
Bossman | 3:1861f741c2df | 97 | |
Bossman | 3:1861f741c2df | 98 | |
Bossman | 3:1861f741c2df | 99 | |
Bossman | 3:1861f741c2df | 100 | } |
Bossman | 3:1861f741c2df | 101 | |
Bossman | 3:1861f741c2df | 102 | |
Bossman | 3:1861f741c2df | 103 | |
Bossman | 3:1861f741c2df | 104 | |
Bossman | 3:1861f741c2df | 105 | |
Bossman | 3:1861f741c2df | 106 | |
Bossman | 3:1861f741c2df | 107 | |
Bossman | 3:1861f741c2df | 108 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 109 | Thread::wait(300); |
Bossman | 3:1861f741c2df | 110 | //pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n \r", x, y, z); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 111 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 112 | } |