Tobore Denedo
/
Accelerometer_Lab4code2
Lab4Code2(modified version)
Fork of Accelerometer_example by
main.cpp@3:2e32120a45f3, 2018-02-23 (annotated)
- Committer:
- Tobden
- Date:
- Fri Feb 23 14:39:04 2018 +0000
- Revision:
- 3:2e32120a45f3
- Parent:
- 2:e553dd3fca46
Revised version of the part 2 of Lab 4
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 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 5 | PinName const SDA = PTE25; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 6 | PinName const SCL = PTE24; |
Tobden | 3:2e32120a45f3 | 7 | Timer t; |
Tobden | 3:2e32120a45f3 | 8 | Thread change; |
Tobden | 3:2e32120a45f3 | 9 | enum orientation { flat, up, right, final, errors }; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 10 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
WilliamMarshQMUL | 0:a1caba5c4e48 | 11 | |
Tobden | 3:2e32120a45f3 | 12 | /*void position() |
Tobden | 3:2e32120a45f3 | 13 | { |
Tobden | 3:2e32120a45f3 | 14 | |
Tobden | 3:2e32120a45f3 | 15 | }*/ |
Tobden | 3:2e32120a45f3 | 16 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 17 | int main(void) |
WilliamMarshQMUL | 0:a1caba5c4e48 | 18 | { |
WilliamMarshQMUL | 0:a1caba5c4e48 | 19 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 20 | PwmOut rled(LED1); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 21 | PwmOut gled(LED2); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 22 | PwmOut bled(LED3); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 23 | Serial pc(USBTX, USBRX); // tx, rx |
Tobden | 3:2e32120a45f3 | 24 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 25 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 26 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 27 | pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
Tobden | 3:2e32120a45f3 | 28 | |
Tobden | 3:2e32120a45f3 | 29 | orientation type = flat; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 30 | while (true) { |
Tobden | 3:2e32120a45f3 | 31 | Thread::wait(500); |
Tobden | 3:2e32120a45f3 | 32 | float x, y, z, trled, tgled, tbled; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 33 | x = acc.getAccX(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 34 | y = acc.getAccY(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 35 | z = acc.getAccZ(); |
Tobden | 3:2e32120a45f3 | 36 | trled = 1.0f - abs(x); |
Tobden | 3:2e32120a45f3 | 37 | tgled = 1.0f - abs(y); |
Tobden | 3:2e32120a45f3 | 38 | tbled = 1.0f - abs(z); |
Tobden | 3:2e32120a45f3 | 39 | int rd = t.read(); |
Tobden | 3:2e32120a45f3 | 40 | switch (type) { |
Tobden | 3:2e32120a45f3 | 41 | case flat : |
Tobden | 3:2e32120a45f3 | 42 | t.start(); |
Tobden | 3:2e32120a45f3 | 43 | |
Tobden | 3:2e32120a45f3 | 44 | |
Tobden | 3:2e32120a45f3 | 45 | if (trled>0.8 && tgled > 0.8) |
Tobden | 3:2e32120a45f3 | 46 | { |
Tobden | 3:2e32120a45f3 | 47 | rled = 1; |
Tobden | 3:2e32120a45f3 | 48 | gled = 1; |
Tobden | 3:2e32120a45f3 | 49 | bled = 0; |
Tobden | 3:2e32120a45f3 | 50 | if (z>0){ |
Tobden | 3:2e32120a45f3 | 51 | if (rd<10){ |
Tobden | 3:2e32120a45f3 | 52 | pc.printf("Current position: FLAT\n\r"); |
Tobden | 3:2e32120a45f3 | 53 | // pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
Tobden | 3:2e32120a45f3 | 54 | pc.printf("Time remaining: %d seconds\n\r", 10-rd);} |
Tobden | 3:2e32120a45f3 | 55 | else |
Tobden | 3:2e32120a45f3 | 56 | pc.printf("Change to the RIGHT now\n\r"); |
Tobden | 3:2e32120a45f3 | 57 | } |
Tobden | 3:2e32120a45f3 | 58 | } |
Tobden | 3:2e32120a45f3 | 59 | if (tgled > 0.8 && tbled > 0.8) |
Tobden | 3:2e32120a45f3 | 60 | { rled = 0; |
Tobden | 3:2e32120a45f3 | 61 | gled = 1; |
Tobden | 3:2e32120a45f3 | 62 | bled = 1; |
Tobden | 3:2e32120a45f3 | 63 | pc.printf("SEQUENCE ERROR\n\r"); |
Tobden | 3:2e32120a45f3 | 64 | t.reset(); |
Tobden | 3:2e32120a45f3 | 65 | t.stop(); |
Tobden | 3:2e32120a45f3 | 66 | type = errors; |
Tobden | 3:2e32120a45f3 | 67 | break;} |
Tobden | 3:2e32120a45f3 | 68 | |
Tobden | 3:2e32120a45f3 | 69 | if (rd>=10 && (trled > 0.8 && tbled > 0.8)) |
Tobden | 3:2e32120a45f3 | 70 | { t.reset(); |
Tobden | 3:2e32120a45f3 | 71 | t.stop(); |
Tobden | 3:2e32120a45f3 | 72 | type = right; |
Tobden | 3:2e32120a45f3 | 73 | } |
Tobden | 3:2e32120a45f3 | 74 | if (rd<10 && (trled > 0.8 && tbled > 0.8)) |
Tobden | 3:2e32120a45f3 | 75 | { rled = 0; |
Tobden | 3:2e32120a45f3 | 76 | gled = 1; |
Tobden | 3:2e32120a45f3 | 77 | bled = 1; |
Tobden | 3:2e32120a45f3 | 78 | t.reset(); |
Tobden | 3:2e32120a45f3 | 79 | t.stop(); |
Tobden | 3:2e32120a45f3 | 80 | type = errors; |
Tobden | 3:2e32120a45f3 | 81 | } |
Tobden | 3:2e32120a45f3 | 82 | break; |
Tobden | 3:2e32120a45f3 | 83 | |
Tobden | 3:2e32120a45f3 | 84 | case right: |
Tobden | 3:2e32120a45f3 | 85 | |
Tobden | 3:2e32120a45f3 | 86 | |
Tobden | 3:2e32120a45f3 | 87 | if (trled > 0.8 && tbled > 0.8) |
Tobden | 3:2e32120a45f3 | 88 | {t.start(); |
Tobden | 3:2e32120a45f3 | 89 | rled = 1; |
Tobden | 3:2e32120a45f3 | 90 | gled = 1; |
Tobden | 3:2e32120a45f3 | 91 | bled = 0; |
Tobden | 3:2e32120a45f3 | 92 | if (y<0){ |
Tobden | 3:2e32120a45f3 | 93 | |
Tobden | 3:2e32120a45f3 | 94 | pc.printf("Current position: RIGHT\n"); |
Tobden | 3:2e32120a45f3 | 95 | if (rd<2){ |
Tobden | 3:2e32120a45f3 | 96 | pc.printf("Please wait %d seconds\n\r ", 2-rd); |
Tobden | 3:2e32120a45f3 | 97 | } |
Tobden | 3:2e32120a45f3 | 98 | else if (rd>=2 && rd<=6) |
Tobden | 3:2e32120a45f3 | 99 | { pc.printf("You have: %d seconds to change to UP: \n\r", 6-rd);} |
Tobden | 3:2e32120a45f3 | 100 | else |
Tobden | 3:2e32120a45f3 | 101 | pc.printf("Too late\n\r"); |
Tobden | 3:2e32120a45f3 | 102 | } |
Tobden | 3:2e32120a45f3 | 103 | } |
Tobden | 3:2e32120a45f3 | 104 | if (trled<=0.8 || tbled <= 0.8) |
Tobden | 3:2e32120a45f3 | 105 | {t.stop();} |
Tobden | 3:2e32120a45f3 | 106 | if (trled > 0.8 && tgled > 0.8 && rd > 0) |
Tobden | 3:2e32120a45f3 | 107 | { rled = 0; |
Tobden | 3:2e32120a45f3 | 108 | gled = 1; |
Tobden | 3:2e32120a45f3 | 109 | bled = 1; |
Tobden | 3:2e32120a45f3 | 110 | pc.printf("SEQUENCE ERROR\n\r"); |
Tobden | 3:2e32120a45f3 | 111 | t.reset(); |
Tobden | 3:2e32120a45f3 | 112 | t.stop(); |
Tobden | 3:2e32120a45f3 | 113 | type = errors; |
Tobden | 3:2e32120a45f3 | 114 | break;} |
Tobden | 3:2e32120a45f3 | 115 | if (rd>=2 && rd<=6 && (tgled > 0.8 && tbled > 0.8)) |
Tobden | 3:2e32120a45f3 | 116 | { t.reset(); |
Tobden | 3:2e32120a45f3 | 117 | type = up; |
Tobden | 3:2e32120a45f3 | 118 | |
Tobden | 3:2e32120a45f3 | 119 | } |
Tobden | 3:2e32120a45f3 | 120 | if ((rd<2 || rd>6) && (tgled > 0.8 && tbled > 0.8)) |
Tobden | 3:2e32120a45f3 | 121 | { rled = 0; |
Tobden | 3:2e32120a45f3 | 122 | gled = 1; |
Tobden | 3:2e32120a45f3 | 123 | bled = 1; |
Tobden | 3:2e32120a45f3 | 124 | t.reset(); |
Tobden | 3:2e32120a45f3 | 125 | t.stop(); |
Tobden | 3:2e32120a45f3 | 126 | type = errors; |
Tobden | 3:2e32120a45f3 | 127 | } |
Tobden | 3:2e32120a45f3 | 128 | break; |
Tobden | 3:2e32120a45f3 | 129 | |
Tobden | 3:2e32120a45f3 | 130 | case up: |
Tobden | 3:2e32120a45f3 | 131 | |
Tobden | 3:2e32120a45f3 | 132 | t.start(); |
Tobden | 3:2e32120a45f3 | 133 | |
Tobden | 3:2e32120a45f3 | 134 | |
Tobden | 3:2e32120a45f3 | 135 | if (tgled > 0.8 && tbled > 0.8) |
Tobden | 3:2e32120a45f3 | 136 | { |
Tobden | 3:2e32120a45f3 | 137 | rled = 1; |
Tobden | 3:2e32120a45f3 | 138 | gled = 1; |
Tobden | 3:2e32120a45f3 | 139 | bled = 0; |
Tobden | 3:2e32120a45f3 | 140 | if (x<0){ |
Tobden | 3:2e32120a45f3 | 141 | |
Tobden | 3:2e32120a45f3 | 142 | |
Tobden | 3:2e32120a45f3 | 143 | |
Tobden | 3:2e32120a45f3 | 144 | pc.printf("Current position: UP\n"); |
Tobden | 3:2e32120a45f3 | 145 | if (rd<4){ |
Tobden | 3:2e32120a45f3 | 146 | pc.printf("Please wait %d seconds\n\r ", 4-rd); |
Tobden | 3:2e32120a45f3 | 147 | } |
Tobden | 3:2e32120a45f3 | 148 | else if (rd>=4 && rd<=8) |
Tobden | 3:2e32120a45f3 | 149 | { pc.printf("You have: %d seconds to change to FLAT: \n\r", 8-rd);} |
Tobden | 3:2e32120a45f3 | 150 | else |
Tobden | 3:2e32120a45f3 | 151 | pc.printf("Too late\n\r"); |
Tobden | 3:2e32120a45f3 | 152 | } |
Tobden | 3:2e32120a45f3 | 153 | |
Tobden | 3:2e32120a45f3 | 154 | |
Tobden | 3:2e32120a45f3 | 155 | |
Tobden | 3:2e32120a45f3 | 156 | } |
Tobden | 3:2e32120a45f3 | 157 | if (tgled <= 0.8 || tbled <= 0.8) |
Tobden | 3:2e32120a45f3 | 158 | {t.stop();} |
Tobden | 3:2e32120a45f3 | 159 | if (trled > 0.8 && tbled > 0.8) |
Tobden | 3:2e32120a45f3 | 160 | { rled = 0; |
Tobden | 3:2e32120a45f3 | 161 | gled = 1; |
Tobden | 3:2e32120a45f3 | 162 | bled = 1; |
Tobden | 3:2e32120a45f3 | 163 | pc.printf("SEQUENCE ERROR\n\r"); |
Tobden | 3:2e32120a45f3 | 164 | t.reset(); |
Tobden | 3:2e32120a45f3 | 165 | t.stop(); |
Tobden | 3:2e32120a45f3 | 166 | type = errors; |
Tobden | 3:2e32120a45f3 | 167 | break;} |
Tobden | 3:2e32120a45f3 | 168 | if (rd>=4 && rd<=8 && (tgled > 0.8 && trled > 0.8)) |
Tobden | 3:2e32120a45f3 | 169 | { |
Tobden | 3:2e32120a45f3 | 170 | t.reset(); |
Tobden | 3:2e32120a45f3 | 171 | type = final; |
Tobden | 3:2e32120a45f3 | 172 | |
Tobden | 3:2e32120a45f3 | 173 | } |
Tobden | 3:2e32120a45f3 | 174 | if ((rd<4 || rd>8 )&& (trled > 0.8 && tgled > 0.8)) |
Tobden | 3:2e32120a45f3 | 175 | { rled = 0; |
Tobden | 3:2e32120a45f3 | 176 | gled = 1; |
Tobden | 3:2e32120a45f3 | 177 | bled = 1; |
Tobden | 3:2e32120a45f3 | 178 | t.reset(); |
Tobden | 3:2e32120a45f3 | 179 | t.stop(); |
Tobden | 3:2e32120a45f3 | 180 | type = errors; |
Tobden | 3:2e32120a45f3 | 181 | } |
Tobden | 3:2e32120a45f3 | 182 | break; |
Tobden | 3:2e32120a45f3 | 183 | |
Tobden | 3:2e32120a45f3 | 184 | case final: |
Tobden | 3:2e32120a45f3 | 185 | if (trled>0.8 && tgled > 0.8) |
Tobden | 3:2e32120a45f3 | 186 | { |
Tobden | 3:2e32120a45f3 | 187 | rled = 1; |
Tobden | 3:2e32120a45f3 | 188 | gled = 0; |
Tobden | 3:2e32120a45f3 | 189 | bled = 1; |
Tobden | 3:2e32120a45f3 | 190 | if (z>0){ |
Tobden | 3:2e32120a45f3 | 191 | pc.printf("SEQUENCE COMPLETE: SYSTEM TRIGGERED \n\r"); |
Tobden | 3:2e32120a45f3 | 192 | } |
Tobden | 3:2e32120a45f3 | 193 | } |
Tobden | 3:2e32120a45f3 | 194 | break; |
Tobden | 3:2e32120a45f3 | 195 | |
Tobden | 3:2e32120a45f3 | 196 | case errors: |
Tobden | 3:2e32120a45f3 | 197 | if (trled< 0.8 || tgled < 0.8 || z<0) |
Tobden | 3:2e32120a45f3 | 198 | {pc.printf("Error has occured. Please orientate back to the flat position\n\r");} |
Tobden | 3:2e32120a45f3 | 199 | else if (trled>0.8 && tgled > 0.8 && z>0 && rd<10) |
Tobden | 3:2e32120a45f3 | 200 | {t.start(); |
Tobden | 3:2e32120a45f3 | 201 | pc.printf("FLAT\n"); |
Tobden | 2:e553dd3fca46 | 202 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
Tobden | 3:2e32120a45f3 | 203 | pc.printf("system resetting in %d seconds\n\r", 10-rd); |
Tobden | 3:2e32120a45f3 | 204 | } |
Tobden | 3:2e32120a45f3 | 205 | else if (rd>=10) |
Tobden | 3:2e32120a45f3 | 206 | { |
Tobden | 3:2e32120a45f3 | 207 | rled = 1; |
Tobden | 3:2e32120a45f3 | 208 | gled = 1; |
Tobden | 3:2e32120a45f3 | 209 | bled = 0; |
Tobden | 3:2e32120a45f3 | 210 | pc.printf("Error successfully cleared\n\r"); |
Tobden | 3:2e32120a45f3 | 211 | t.reset(); |
Tobden | 3:2e32120a45f3 | 212 | t.stop(); |
Tobden | 3:2e32120a45f3 | 213 | type = right; |
Tobden | 3:2e32120a45f3 | 214 | break;} |
Tobden | 3:2e32120a45f3 | 215 | |
Tobden | 3:2e32120a45f3 | 216 | } |
Tobden | 3:2e32120a45f3 | 217 | } |
Tobden | 3:2e32120a45f3 | 218 | } |
Tobden | 3:2e32120a45f3 | 219 |