Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
motion.cpp@1:8f545f45d899, 2016-11-25 (annotated)
- Committer:
- kieftea
- Date:
- Fri Nov 25 03:44:54 2016 +0000
- Revision:
- 1:8f545f45d899
- Parent:
- 0:00669618559e
- Child:
- 2:4fb5a8d15f9c
for SHANE
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kieftea | 0:00669618559e | 1 | #include "rtos.h" |
| kieftea | 0:00669618559e | 2 | #include "pinmap.h" |
| kieftea | 0:00669618559e | 3 | #include "FXOS8700Q.h" |
| kieftea | 0:00669618559e | 4 | |
| kieftea | 0:00669618559e | 5 | |
| kieftea | 0:00669618559e | 6 | FXOS8700Q_acc acc(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); |
| kieftea | 0:00669618559e | 7 | |
| kieftea | 0:00669618559e | 8 | /* Constants and Declares */ |
| kieftea | 0:00669618559e | 9 | int numCount; |
| kieftea | 0:00669618559e | 10 | int const MAX_NUM_COUNTS = 3; |
| kieftea | 0:00669618559e | 11 | int const TIMING_PERIOD = 20; // Sensor polling interval |
| kieftea | 0:00669618559e | 12 | |
| kieftea | 0:00669618559e | 13 | uint8_t motion_exceeded_threshold = 0; |
| kieftea | 0:00669618559e | 14 | |
| kieftea | 0:00669618559e | 15 | void initialize_motion () { |
| kieftea | 0:00669618559e | 16 | acc.enable(); |
| kieftea | 0:00669618559e | 17 | } |
| kieftea | 0:00669618559e | 18 | |
| kieftea | 0:00669618559e | 19 | bool isMotionThresholdExceeded () { |
| kieftea | 0:00669618559e | 20 | return motion_exceeded_threshold; |
| kieftea | 0:00669618559e | 21 | } |
| kieftea | 0:00669618559e | 22 | |
| kieftea | 1:8f545f45d899 | 23 | float getAcceleration(){ |
| kieftea | 1:8f545f45d899 | 24 | float xAcc, yAcc, zAcc; |
| kieftea | 1:8f545f45d899 | 25 | acc.getX(&xAcc); |
| kieftea | 1:8f545f45d899 | 26 | acc.getY(&yAcc); |
| kieftea | 1:8f545f45d899 | 27 | acc.getZ(&zAcc); |
| kieftea | 1:8f545f45d899 | 28 | float magtd = xAcc*xAcc + yAcc*yAcc + zAcc*zAcc; |
| kieftea | 1:8f545f45d899 | 29 | return magtd; |
| kieftea | 1:8f545f45d899 | 30 | } |
| kieftea | 1:8f545f45d899 | 31 | |
| kieftea | 1:8f545f45d899 | 32 | |
| kieftea | 0:00669618559e | 33 | void resetMotionDetection () { |
| kieftea | 0:00669618559e | 34 | motion_exceeded_threshold = 0; |
| kieftea | 0:00669618559e | 35 | } |
| kieftea | 0:00669618559e | 36 | |
| kieftea | 0:00669618559e | 37 | /**** Function: a_count |
| kieftea | 0:00669618559e | 38 | * return: void |
| kieftea | 0:00669618559e | 39 | * parameters: none |
| kieftea | 0:00669618559e | 40 | * A function called if motion detection interrupt flag is set. Maintains |
| kieftea | 0:00669618559e | 41 | * a global counter and sets a timer to keep track of number of flags within |
| kieftea | 0:00669618559e | 42 | * timing limit. |
| kieftea | 0:00669618559e | 43 | */ |
| kieftea | 0:00669618559e | 44 | void a_count(void) { |
| kieftea | 0:00669618559e | 45 | /* step 1 increment the counter */ |
| kieftea | 0:00669618559e | 46 | numCount++; |
| kieftea | 0:00669618559e | 47 | |
| kieftea | 0:00669618559e | 48 | |
| kieftea | 0:00669618559e | 49 | if (numCount >= MAX_NUM_COUNTS) { |
| kieftea | 0:00669618559e | 50 | rled = !rled; // toggle LEDs to show acceleration threshold reached |
| kieftea | 0:00669618559e | 51 | gled = !gled; // toggle LEDS to show acceleration threshold reached |
| kieftea | 0:00669618559e | 52 | |
| kieftea | 0:00669618559e | 53 | motion_exceeded_threshold = 1; |
| kieftea | 0:00669618559e | 54 | } |
| kieftea | 0:00669618559e | 55 | } |
| kieftea | 0:00669618559e | 56 | |
| kieftea | 0:00669618559e | 57 | void motion_thread () { |
| kieftea | 0:00669618559e | 58 | while(true) { |
| kieftea | 0:00669618559e | 59 | |
| kieftea | 0:00669618559e | 60 | float xAcc, yAcc, zAcc; |
| kieftea | 0:00669618559e | 61 | acc.getX(&xAcc); |
| kieftea | 0:00669618559e | 62 | acc.getY(&yAcc); |
| kieftea | 0:00669618559e | 63 | acc.getZ(&zAcc); |
| kieftea | 0:00669618559e | 64 | float magtd = xAcc*xAcc + yAcc*yAcc + zAcc*zAcc; |
| kieftea | 0:00669618559e | 65 | |
| kieftea | 0:00669618559e | 66 | if (magtd > 3.0f) { // Greater than (1.5G of Accel.)^2 |
| kieftea | 0:00669618559e | 67 | a_count(); // increment acceleration event counter |
| kieftea | 0:00669618559e | 68 | } |
| kieftea | 0:00669618559e | 69 | |
| kieftea | 0:00669618559e | 70 | Thread::wait(TIMING_PERIOD); |
| kieftea | 0:00669618559e | 71 | } |
| kieftea | 0:00669618559e | 72 | } |