Implementation of spacecraft launch specification
Fork of Accelerometer_example by
Diff: main.cpp
- Revision:
- 2:3834b1d61eff
- Parent:
- 0:a1caba5c4e48
- Child:
- 3:f8a95cb07150
--- a/main.cpp Wed Feb 07 16:56:55 2018 +0000 +++ b/main.cpp Fri Feb 16 18:57:48 2018 +0000 @@ -7,26 +7,109 @@ #define MMA8451_I2C_ADDRESS (0x1d<<1) +Serial pc(USBTX, USBRX); // tx, rx +enum states { flat, right, left, down, up, over, intermediate }; + +int sequence = 0; +bool checkSequence(states state, states oldStableState) { + switch (state) { + case flat: + if (sequence == 3) { + sequence = 4; + } else { + sequence = 1; + } + pc.printf("%d\r\n", sequence); + break; + case right: + if (sequence == 1) { + sequence = 2; + } + pc.printf("%d\r\n", sequence); + break; + case up: + if (sequence == 2) { + sequence = 3; + } + pc.printf("%d\r\n", sequence); + break; + } +} + int main(void) -{ +{ MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); - PwmOut rled(LED1); - PwmOut gled(LED2); - PwmOut bled(LED3); - Serial pc(USBTX, USBRX); // tx, rx + + + states state = intermediate; + states oldState = intermediate; + states oldStableState = intermediate; + int stateCounter = 0; + int const stableState = 3; - - pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI()); + pc.printf("MMA8451 ID: %d\n\r", acc.getWhoAmI()); while (true) { float x, y, z; x = acc.getAccX(); y = acc.getAccY(); z = acc.getAccZ(); - rled = 1.0f - abs(x); - gled = 1.0f - abs(y); - bled = 1.0f - abs(z); + + float threshold = 0.10; + + if (z > 1 - threshold && z < 1 + threshold) { + state = flat; + } else if (z > -1 - threshold && z < -1 + threshold) { + state = over; + } else if (y > 1 - threshold && y < 1 + threshold) { + state = left; + } else if (y > -1 - threshold && y < -1 + threshold) { + state = right; + } else if (x > 1 - threshold && x < 1 + threshold) { + state = down; + } else if (x > -1 - threshold && x < -1 + threshold) { + state = up; + } else { + state = intermediate; + } + + if (state != intermediate) { + if (state == oldState) { + if (stateCounter < stableState) { + stateCounter ++; + } + } else { + oldState = state; + stateCounter = 0; + } + } + + // print the state + if (stateCounter == stableState && oldStableState != state) { + checkSequence(state, oldStableState); + oldStableState = state; + switch (state) { + case flat: + pc.printf("flat\n\r"); + break; + case right: + pc.printf("right\n\r"); + break; + case left: + pc.printf("left\n\r"); + break; + case down: + pc.printf("down\n\r"); + break; + case up: + pc.printf("up\n\r"); + break; + case over: + pc.printf("over\n\r"); + break; + } + } + Thread::wait(300); - pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z); } }