Pacemaker code Implementation for SFWRENG 3K04

Dependencies:   mbed Queue mbed-rtos FXOS8700Q

Fork of Pacemaker by Eric dollar

SWFRENG 3K04 Project to design, develop, and document a functional pacemaker.

The project uses the Freescale K64F Microcontroller and C++ mbed library.

Committer:
noahzwiep
Date:
Mon Dec 12 03:31:55 2016 +0000
Revision:
33:686a1a0c690f
Parent:
21:fc6c33206152
Child:
36:b6431cd8ecd6
Using proper pulse functions and pins

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);
noahzwiep 33:686a1a0c690f 10 //DigitalOut red(LED_RED);
noahzwiep 33:686a1a0c690f 11
trane3 19:d58e1e1a9a24 12
trane3 19:d58e1e1a9a24 13 /* Constants and Declares */
trane3 19:d58e1e1a9a24 14 int numCount;
trane3 19:d58e1e1a9a24 15 chamberData* ventChamber;
trane3 19:d58e1e1a9a24 16 chamberData* atrChamber;
trane3 19:d58e1e1a9a24 17 int const MAX_NUM_COUNTS = 3;
noahzwiep 33:686a1a0c690f 18 int const MIN_NUM_COUNTS = 3;
trane3 19:d58e1e1a9a24 19 int const TIMING_PERIOD = 20; // Sensor polling interval
trane3 19:d58e1e1a9a24 20
trane3 19:d58e1e1a9a24 21 uint8_t motion_exceeded_threshold = 0;
trane3 19:d58e1e1a9a24 22
trane3 19:d58e1e1a9a24 23 void initialize_motion () {
trane3 19:d58e1e1a9a24 24 acc.enable();
trane3 19:d58e1e1a9a24 25 }
trane3 19:d58e1e1a9a24 26
trane3 19:d58e1e1a9a24 27 void motion_set_chamber(int i, chamberData* c){
trane3 19:d58e1e1a9a24 28 if (i == 0)
trane3 19:d58e1e1a9a24 29 ventChamber = c;
trane3 19:d58e1e1a9a24 30
trane3 19:d58e1e1a9a24 31 }
trane3 19:d58e1e1a9a24 32
trane3 21:fc6c33206152 33 //bool isMotionThresholdExceeded () {
trane3 21:fc6c33206152 34 // return motion_exceeded_threshold;
trane3 21:fc6c33206152 35 //}
trane3 21:fc6c33206152 36 //
trane3 21:fc6c33206152 37 //void resetMotionDetection () {
trane3 21:fc6c33206152 38 // motion_exceeded_threshold = 0;
trane3 21:fc6c33206152 39 //}
trane3 19:d58e1e1a9a24 40
trane3 19:d58e1e1a9a24 41 /**** Function: a_count
trane3 19:d58e1e1a9a24 42 * return: void
trane3 19:d58e1e1a9a24 43 * parameters: none
trane3 19:d58e1e1a9a24 44 * A function called if motion detection interrupt flag is set. Maintains
trane3 19:d58e1e1a9a24 45 * a global counter and sets a timer to keep track of number of flags within
trane3 19:d58e1e1a9a24 46 * timing limit.
trane3 19:d58e1e1a9a24 47 */
trane3 19:d58e1e1a9a24 48 void a_count(void) {
trane3 19:d58e1e1a9a24 49 /* step 1 increment the counter */
trane3 19:d58e1e1a9a24 50 numCount++;
trane3 19:d58e1e1a9a24 51
trane3 19:d58e1e1a9a24 52
trane3 19:d58e1e1a9a24 53 if (numCount >= MAX_NUM_COUNTS) {
trane3 21:fc6c33206152 54 ventChamber->chngPaceWidth(ventChamber->getPaceWidth() - 0.01);
noahzwiep 33:686a1a0c690f 55 //red = !red; // toggle LEDs to show acceleration threshold reached
trane3 19:d58e1e1a9a24 56
trane3 19:d58e1e1a9a24 57 motion_exceeded_threshold = 1;
trane3 19:d58e1e1a9a24 58 }
trane3 19:d58e1e1a9a24 59 }
trane3 19:d58e1e1a9a24 60
noahzwiep 33:686a1a0c690f 61 void b_count(){
noahzwiep 33:686a1a0c690f 62 numCount++;
noahzwiep 33:686a1a0c690f 63
noahzwiep 33:686a1a0c690f 64 if (numCount >= MIN_NUM_COUNTS){
noahzwiep 33:686a1a0c690f 65 ventChamber->chngPaceWidth(ventChamber->getPaceWidth() + 0.01);
noahzwiep 33:686a1a0c690f 66 //red = !red;
noahzwiep 33:686a1a0c690f 67 }
noahzwiep 33:686a1a0c690f 68 }
noahzwiep 33:686a1a0c690f 69
trane3 19:d58e1e1a9a24 70 void motion_thread () {
trane3 19:d58e1e1a9a24 71 while(true) {
trane3 19:d58e1e1a9a24 72
trane3 19:d58e1e1a9a24 73 float xAcc, yAcc, zAcc;
trane3 19:d58e1e1a9a24 74 acc.getX(&xAcc);
trane3 19:d58e1e1a9a24 75 acc.getY(&yAcc);
trane3 19:d58e1e1a9a24 76 acc.getZ(&zAcc);
trane3 19:d58e1e1a9a24 77 float magtd = xAcc*xAcc + yAcc*yAcc + zAcc*zAcc;
trane3 19:d58e1e1a9a24 78
trane3 19:d58e1e1a9a24 79 if (magtd > 3.0f) { // Greater than (1.5G of Accel.)^2
trane3 19:d58e1e1a9a24 80 a_count(); // increment acceleration event counter
trane3 19:d58e1e1a9a24 81 }
trane3 19:d58e1e1a9a24 82
noahzwiep 33:686a1a0c690f 83 if (magtd < 1.0f){
noahzwiep 33:686a1a0c690f 84 b_count();
trane3 19:d58e1e1a9a24 85 Thread::wait(TIMING_PERIOD);
noahzwiep 33:686a1a0c690f 86 }
trane3 19:d58e1e1a9a24 87 }
trane3 19:d58e1e1a9a24 88 }