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:
Sun Dec 18 02:29:59 2016 +0000
Revision:
36:b6431cd8ecd6
Parent:
33:686a1a0c690f
All ogre.

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