Pacemaker code Implementation for SFWRENG 3K04

Dependencies:   FXOS8700Q Queue mbed-rtos mbed

Fork of Pacemaker by Eric dollar

Committer:
trane3
Date:
Thu Nov 24 03:39:31 2016 +0000
Revision:
21:fc6c33206152
Parent:
19:d58e1e1a9a24
Eric temp changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
trane3 19:d58e1e1a9a24 1 #include "rtos.h"
trane3 19:d58e1e1a9a24 2 #include "FXOS8700Q.h"
trane3 19:d58e1e1a9a24 3 #include "mbed.h"
trane3 19:d58e1e1a9a24 4 #include "hardware.h"
trane3 19:d58e1e1a9a24 5 #include "pulse.h"
trane3 19:d58e1e1a9a24 6 #include "chamberData.h"
trane3 19:d58e1e1a9a24 7
trane3 19:d58e1e1a9a24 8
trane3 19:d58e1e1a9a24 9 FXOS8700Q_acc acc(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1);
trane3 19:d58e1e1a9a24 10 DigitalOut red(LED_RED);
trane3 19:d58e1e1a9a24 11
trane3 19:d58e1e1a9a24 12 /* Constants and Declares */
trane3 19:d58e1e1a9a24 13 int numCount;
trane3 19:d58e1e1a9a24 14 chamberData* ventChamber;
trane3 19:d58e1e1a9a24 15 chamberData* atrChamber;
trane3 19:d58e1e1a9a24 16 int const MAX_NUM_COUNTS = 3;
trane3 19:d58e1e1a9a24 17 int const TIMING_PERIOD = 20; // Sensor polling interval
trane3 19:d58e1e1a9a24 18
trane3 19:d58e1e1a9a24 19 uint8_t motion_exceeded_threshold = 0;
trane3 19:d58e1e1a9a24 20
trane3 19:d58e1e1a9a24 21 void initialize_motion () {
trane3 19:d58e1e1a9a24 22 acc.enable();
trane3 19:d58e1e1a9a24 23 }
trane3 19:d58e1e1a9a24 24
trane3 19:d58e1e1a9a24 25 void motion_set_chamber(int i, chamberData* c){
trane3 19:d58e1e1a9a24 26 if (i == 0)
trane3 19:d58e1e1a9a24 27 ventChamber = c;
trane3 19:d58e1e1a9a24 28
trane3 19:d58e1e1a9a24 29 }
trane3 19:d58e1e1a9a24 30
trane3 21:fc6c33206152 31 //bool isMotionThresholdExceeded () {
trane3 21:fc6c33206152 32 // return motion_exceeded_threshold;
trane3 21:fc6c33206152 33 //}
trane3 21:fc6c33206152 34 //
trane3 21:fc6c33206152 35 //void resetMotionDetection () {
trane3 21:fc6c33206152 36 // motion_exceeded_threshold = 0;
trane3 21:fc6c33206152 37 //}
trane3 19:d58e1e1a9a24 38
trane3 19:d58e1e1a9a24 39 /**** Function: a_count
trane3 19:d58e1e1a9a24 40 * return: void
trane3 19:d58e1e1a9a24 41 * parameters: none
trane3 19:d58e1e1a9a24 42 * A function called if motion detection interrupt flag is set. Maintains
trane3 19:d58e1e1a9a24 43 * a global counter and sets a timer to keep track of number of flags within
trane3 19:d58e1e1a9a24 44 * timing limit.
trane3 19:d58e1e1a9a24 45 */
trane3 19:d58e1e1a9a24 46 void a_count(void) {
trane3 19:d58e1e1a9a24 47 /* step 1 increment the counter */
trane3 19:d58e1e1a9a24 48 numCount++;
trane3 19:d58e1e1a9a24 49
trane3 19:d58e1e1a9a24 50
trane3 19:d58e1e1a9a24 51 if (numCount >= MAX_NUM_COUNTS) {
trane3 21:fc6c33206152 52 ventChamber->chngPaceWidth(ventChamber->getPaceWidth() - 0.01);
trane3 19:d58e1e1a9a24 53 red = !red; // toggle LEDs to show acceleration threshold reached
trane3 19:d58e1e1a9a24 54
trane3 19:d58e1e1a9a24 55 motion_exceeded_threshold = 1;
trane3 19:d58e1e1a9a24 56 }
trane3 19:d58e1e1a9a24 57 }
trane3 19:d58e1e1a9a24 58
trane3 19:d58e1e1a9a24 59 void motion_thread () {
trane3 19:d58e1e1a9a24 60 while(true) {
trane3 19:d58e1e1a9a24 61
trane3 19:d58e1e1a9a24 62 float xAcc, yAcc, zAcc;
trane3 19:d58e1e1a9a24 63 acc.getX(&xAcc);
trane3 19:d58e1e1a9a24 64 acc.getY(&yAcc);
trane3 19:d58e1e1a9a24 65 acc.getZ(&zAcc);
trane3 19:d58e1e1a9a24 66 float magtd = xAcc*xAcc + yAcc*yAcc + zAcc*zAcc;
trane3 19:d58e1e1a9a24 67
trane3 19:d58e1e1a9a24 68 if (magtd > 3.0f) { // Greater than (1.5G of Accel.)^2
trane3 19:d58e1e1a9a24 69 a_count(); // increment acceleration event counter
trane3 19:d58e1e1a9a24 70 }
trane3 19:d58e1e1a9a24 71
trane3 19:d58e1e1a9a24 72 Thread::wait(TIMING_PERIOD);
trane3 19:d58e1e1a9a24 73 }
trane3 19:d58e1e1a9a24 74 }