Robert Hill
/
FRDM_ALL_
Cycle Fit - All Sensors
All_Sensors.cpp@0:ef02694deaa8, 2016-02-24 (annotated)
- Committer:
- roberthill04
- Date:
- Wed Feb 24 16:53:45 2016 +0000
- Revision:
- 0:ef02694deaa8
Hello
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roberthill04 | 0:ef02694deaa8 | 1 | #include "mbed.h" |
roberthill04 | 0:ef02694deaa8 | 2 | |
roberthill04 | 0:ef02694deaa8 | 3 | #include "PulseSensor.h" //Needed for HeartBeat |
roberthill04 | 0:ef02694deaa8 | 4 | //#include "AnalogIn.h" |
roberthill04 | 0:ef02694deaa8 | 5 | |
roberthill04 | 0:ef02694deaa8 | 6 | #include "DigitalIn.h" //Needed for Hall Effect |
roberthill04 | 0:ef02694deaa8 | 7 | |
roberthill04 | 0:ef02694deaa8 | 8 | #include "FXOS8700Q.h" //Needed for Accelerometer |
roberthill04 | 0:ef02694deaa8 | 9 | #define PI 3.14159265 |
roberthill04 | 0:ef02694deaa8 | 10 | |
roberthill04 | 0:ef02694deaa8 | 11 | |
roberthill04 | 0:ef02694deaa8 | 12 | DigitalOut led_red(LED_RED); |
roberthill04 | 0:ef02694deaa8 | 13 | DigitalOut led_green(LED_GREEN); |
roberthill04 | 0:ef02694deaa8 | 14 | DigitalOut led_blue(LED_BLUE); |
roberthill04 | 0:ef02694deaa8 | 15 | DigitalIn sw2(SW2); |
roberthill04 | 0:ef02694deaa8 | 16 | DigitalIn sw3(SW3); |
roberthill04 | 0:ef02694deaa8 | 17 | Serial pc(USBTX, USBRX); |
roberthill04 | 0:ef02694deaa8 | 18 | bool Button_Pressed = true; //Initialize flag for output on terminal |
roberthill04 | 0:ef02694deaa8 | 19 | ///////////////////////////////////////Variables////////////////////////////////////////////////// |
roberthill04 | 0:ef02694deaa8 | 20 | |
roberthill04 | 0:ef02694deaa8 | 21 | ///////////////////////////////////////Heart Rate///////////////////////////////////////////////// |
roberthill04 | 0:ef02694deaa8 | 22 | |
roberthill04 | 0:ef02694deaa8 | 23 | AnalogIn Pulse_Signal(A0); //Initialize analog input for pulse signal(Heart Rate sensor) |
roberthill04 | 0:ef02694deaa8 | 24 | |
roberthill04 | 0:ef02694deaa8 | 25 | ///////////////////////////////////////Hall Effect///////////////////////////////////////////////// |
roberthill04 | 0:ef02694deaa8 | 26 | |
roberthill04 | 0:ef02694deaa8 | 27 | DigitalIn HallEffect(D2); //Intializes Digital input into pin D2 (Hall Effect sensor) |
roberthill04 | 0:ef02694deaa8 | 28 | int Hall_Counter = 0; //Initialize Counter for Hall Effect |
roberthill04 | 0:ef02694deaa8 | 29 | bool Low_Hall = true; //Initialize Flag for Hall Effect sensor |
roberthill04 | 0:ef02694deaa8 | 30 | Timer Hall_Timer; //Initialize timer for pedal speed calc |
roberthill04 | 0:ef02694deaa8 | 31 | int Pedal_Time; //Intialize int for Time Passed |
roberthill04 | 0:ef02694deaa8 | 32 | int Pedal_Speed; //Initialize int for Pedal Speed |
roberthill04 | 0:ef02694deaa8 | 33 | |
roberthill04 | 0:ef02694deaa8 | 34 | ////////////////////////////////////////Accelerometer//////////////////////////////////////////////// |
roberthill04 | 0:ef02694deaa8 | 35 | |
roberthill04 | 0:ef02694deaa8 | 36 | I2C i2c(PTE25, PTE24); //Initialize I2C connection for sensor |
roberthill04 | 0:ef02694deaa8 | 37 | FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1); // Configured for the FRDM-K64F with onboard sensors |
roberthill04 | 0:ef02694deaa8 | 38 | double angleX, denomX_T, denomX_A, denomX_B, denomX_C; //intializing variable to hold the angle calculation |
roberthill04 | 0:ef02694deaa8 | 39 | double angleY, denomY_T, denomY_A, denomY_B, denomY_C; //and denominator pieces of algorithm for both X&Y axis |
roberthill04 | 0:ef02694deaa8 | 40 | |
roberthill04 | 0:ef02694deaa8 | 41 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
roberthill04 | 0:ef02694deaa8 | 42 | |
roberthill04 | 0:ef02694deaa8 | 43 | /////////////////////////////////////////////Heart Rate Processing Section//////////////////////////////////////////////////////// |
roberthill04 | 0:ef02694deaa8 | 44 | PulseSensor::PulseSensor(PinName analogPin, void (*printDataCallback)(char,int), int callbackRateMs) |
roberthill04 | 0:ef02694deaa8 | 45 | { |
roberthill04 | 0:ef02694deaa8 | 46 | _started = false; |
roberthill04 | 0:ef02694deaa8 | 47 | |
roberthill04 | 0:ef02694deaa8 | 48 | _pAin = new AnalogIn(analogPin); |
roberthill04 | 0:ef02694deaa8 | 49 | |
roberthill04 | 0:ef02694deaa8 | 50 | _callbackRateMs = callbackRateMs; |
roberthill04 | 0:ef02694deaa8 | 51 | |
roberthill04 | 0:ef02694deaa8 | 52 | _printDataCallback = printDataCallback; |
roberthill04 | 0:ef02694deaa8 | 53 | } |
roberthill04 | 0:ef02694deaa8 | 54 | |
roberthill04 | 0:ef02694deaa8 | 55 | |
roberthill04 | 0:ef02694deaa8 | 56 | PulseSensor::~PulseSensor() |
roberthill04 | 0:ef02694deaa8 | 57 | { |
roberthill04 | 0:ef02694deaa8 | 58 | delete _pAin; |
roberthill04 | 0:ef02694deaa8 | 59 | } |
roberthill04 | 0:ef02694deaa8 | 60 | |
roberthill04 | 0:ef02694deaa8 | 61 | |
roberthill04 | 0:ef02694deaa8 | 62 | void PulseSensor::process_data_ticker_callback(void) |
roberthill04 | 0:ef02694deaa8 | 63 | { |
roberthill04 | 0:ef02694deaa8 | 64 | _printDataCallback('S', Signal); // send Processing the raw Pulse Sensor data |
roberthill04 | 0:ef02694deaa8 | 65 | if (QS == true) { // Quantified Self flag is true when a heartbeat is found |
roberthill04 | 0:ef02694deaa8 | 66 | //fadeRate = 255; // Set 'fadeRate' Variable to 255 to fade LED with pulse |
roberthill04 | 0:ef02694deaa8 | 67 | _printDataCallback('B',BPM); // send heart rate with a 'B' prefix |
roberthill04 | 0:ef02694deaa8 | 68 | _printDataCallback('Q',IBI); // send time between beats with a 'Q' prefix |
roberthill04 | 0:ef02694deaa8 | 69 | QS = false; // reset the Quantified Self flag for next time |
roberthill04 | 0:ef02694deaa8 | 70 | } |
roberthill04 | 0:ef02694deaa8 | 71 | } |
roberthill04 | 0:ef02694deaa8 | 72 | |
roberthill04 | 0:ef02694deaa8 | 73 | |
roberthill04 | 0:ef02694deaa8 | 74 | void PulseSensor::sensor_ticker_callback(void) |
roberthill04 | 0:ef02694deaa8 | 75 | { |
roberthill04 | 0:ef02694deaa8 | 76 | Signal = 1023 * _pAin->read(); // read the Pulse Sensor |
roberthill04 | 0:ef02694deaa8 | 77 | |
roberthill04 | 0:ef02694deaa8 | 78 | |
roberthill04 | 0:ef02694deaa8 | 79 | sampleCounter += 2; // keep track of the time in mS with this variable |
roberthill04 | 0:ef02694deaa8 | 80 | int N = sampleCounter - lastBeatTime; // monitor the time since the last beat to avoid noise |
roberthill04 | 0:ef02694deaa8 | 81 | |
roberthill04 | 0:ef02694deaa8 | 82 | // find the peak and trough of the pulse wave |
roberthill04 | 0:ef02694deaa8 | 83 | if(Signal < thresh && N > (IBI/5)*3) { // avoid dichrotic noise by waiting 3/5 of last IBI |
roberthill04 | 0:ef02694deaa8 | 84 | if (Signal < T) { // T is the trough |
roberthill04 | 0:ef02694deaa8 | 85 | T = Signal; // keep track of lowest point in pulse wave |
roberthill04 | 0:ef02694deaa8 | 86 | } |
roberthill04 | 0:ef02694deaa8 | 87 | } |
roberthill04 | 0:ef02694deaa8 | 88 | |
roberthill04 | 0:ef02694deaa8 | 89 | if(Signal > thresh && Signal > P) { // thresh condition helps avoid noise |
roberthill04 | 0:ef02694deaa8 | 90 | P = Signal; // P is the peak |
roberthill04 | 0:ef02694deaa8 | 91 | } // keep track of highest point in pulse wave |
roberthill04 | 0:ef02694deaa8 | 92 | |
roberthill04 | 0:ef02694deaa8 | 93 | // NOW IT'S TIME TO LOOK FOR THE HEART BEAT |
roberthill04 | 0:ef02694deaa8 | 94 | // signal surges up in value every time there is a pulse |
roberthill04 | 0:ef02694deaa8 | 95 | if (N > 250) { // avoid high frequency noise by waiting |
roberthill04 | 0:ef02694deaa8 | 96 | //this also sets limit to HR sensor to max =240 BPMs |
roberthill04 | 0:ef02694deaa8 | 97 | if ( (Signal > thresh) && (Pulse == false) && (N > (IBI/5)*3) ) { |
roberthill04 | 0:ef02694deaa8 | 98 | Pulse = true; // set the Pulse flag when we think there is a pulse |
roberthill04 | 0:ef02694deaa8 | 99 | //digitalWrite(blinkPin,HIGH); // turn on pin 13 LED |
roberthill04 | 0:ef02694deaa8 | 100 | IBI = sampleCounter - lastBeatTime; // measure time between beats in mS |
roberthill04 | 0:ef02694deaa8 | 101 | lastBeatTime = sampleCounter; // keep track of time for next pulse |
roberthill04 | 0:ef02694deaa8 | 102 | |
roberthill04 | 0:ef02694deaa8 | 103 | if(firstBeat) { // if it's the first time we found a beat, if firstBeat == TRUE |
roberthill04 | 0:ef02694deaa8 | 104 | firstBeat = false; // clear firstBeat flag |
roberthill04 | 0:ef02694deaa8 | 105 | return; // IBI value is unreliable so discard it |
roberthill04 | 0:ef02694deaa8 | 106 | } |
roberthill04 | 0:ef02694deaa8 | 107 | if(secondBeat) { // if this is the second beat, if secondBeat == TRUE |
roberthill04 | 0:ef02694deaa8 | 108 | secondBeat = false; // clear secondBeat flag |
roberthill04 | 0:ef02694deaa8 | 109 | for(int i=0; i<=9; i++) { // seed the running total to get a realisitic BPM at startup |
roberthill04 | 0:ef02694deaa8 | 110 | rate[i] = IBI; |
roberthill04 | 0:ef02694deaa8 | 111 | } |
roberthill04 | 0:ef02694deaa8 | 112 | } |
roberthill04 | 0:ef02694deaa8 | 113 | |
roberthill04 | 0:ef02694deaa8 | 114 | // keep a running total of the last 10 IBI values |
roberthill04 | 0:ef02694deaa8 | 115 | long runningTotal = 0; // clear the runningTotal variable |
roberthill04 | 0:ef02694deaa8 | 116 | |
roberthill04 | 0:ef02694deaa8 | 117 | for(int i=0; i<=8; i++) { // shift data in the rate array |
roberthill04 | 0:ef02694deaa8 | 118 | rate[i] = rate[i+1]; // and drop the oldest IBI value |
roberthill04 | 0:ef02694deaa8 | 119 | runningTotal += rate[i]; // add up the 9 oldest IBI values |
roberthill04 | 0:ef02694deaa8 | 120 | } |
roberthill04 | 0:ef02694deaa8 | 121 | |
roberthill04 | 0:ef02694deaa8 | 122 | rate[9] = IBI; // add the latest IBI to the rate array |
roberthill04 | 0:ef02694deaa8 | 123 | runningTotal += rate[9]; // add the latest IBI to runningTotal |
roberthill04 | 0:ef02694deaa8 | 124 | runningTotal /= 10; // average the last 10 IBI values |
roberthill04 | 0:ef02694deaa8 | 125 | BPM = 60000/runningTotal; // how many beats can fit into a minute? that's BPM! |
roberthill04 | 0:ef02694deaa8 | 126 | QS = true; // set Quantified Self flag |
roberthill04 | 0:ef02694deaa8 | 127 | // QS FLAG IS NOT CLEARED INSIDE THIS ISR |
roberthill04 | 0:ef02694deaa8 | 128 | } |
roberthill04 | 0:ef02694deaa8 | 129 | } |
roberthill04 | 0:ef02694deaa8 | 130 | |
roberthill04 | 0:ef02694deaa8 | 131 | if (Signal < thresh && Pulse == true) { // when the values are going down, the beat is over |
roberthill04 | 0:ef02694deaa8 | 132 | Pulse = false; // reset the Pulse flag so we can do it again |
roberthill04 | 0:ef02694deaa8 | 133 | amp = P - T; // get amplitude of the pulse wave |
roberthill04 | 0:ef02694deaa8 | 134 | thresh = amp/2 + T; // set thresh at 50% of the amplitude |
roberthill04 | 0:ef02694deaa8 | 135 | P = thresh; // reset these for next time |
roberthill04 | 0:ef02694deaa8 | 136 | T = thresh; |
roberthill04 | 0:ef02694deaa8 | 137 | } |
roberthill04 | 0:ef02694deaa8 | 138 | |
roberthill04 | 0:ef02694deaa8 | 139 | if (N > 2500) { // if 2.5 seconds go by without a beat |
roberthill04 | 0:ef02694deaa8 | 140 | thresh = 512; // set thresh default |
roberthill04 | 0:ef02694deaa8 | 141 | P = 512; // set P default |
roberthill04 | 0:ef02694deaa8 | 142 | T = 512; // set T default |
roberthill04 | 0:ef02694deaa8 | 143 | lastBeatTime = sampleCounter; // bring the lastBeatTime up to date |
roberthill04 | 0:ef02694deaa8 | 144 | firstBeat = true; // set these to avoid noise |
roberthill04 | 0:ef02694deaa8 | 145 | secondBeat = true; // when we get the heartbeat back |
roberthill04 | 0:ef02694deaa8 | 146 | } |
roberthill04 | 0:ef02694deaa8 | 147 | } |
roberthill04 | 0:ef02694deaa8 | 148 | |
roberthill04 | 0:ef02694deaa8 | 149 | void sendDataToProcessing(char symbol, int data) |
roberthill04 | 0:ef02694deaa8 | 150 | { |
roberthill04 | 0:ef02694deaa8 | 151 | pc.printf("%c%d\t\r\n", symbol, data); |
roberthill04 | 0:ef02694deaa8 | 152 | } |
roberthill04 | 0:ef02694deaa8 | 153 | |
roberthill04 | 0:ef02694deaa8 | 154 | bool PulseSensor::start() |
roberthill04 | 0:ef02694deaa8 | 155 | { |
roberthill04 | 0:ef02694deaa8 | 156 | if (_started == false) |
roberthill04 | 0:ef02694deaa8 | 157 | { |
roberthill04 | 0:ef02694deaa8 | 158 | sampleCounter = 0; |
roberthill04 | 0:ef02694deaa8 | 159 | lastBeatTime = 0; |
roberthill04 | 0:ef02694deaa8 | 160 | P =512; |
roberthill04 | 0:ef02694deaa8 | 161 | T = 512; |
roberthill04 | 0:ef02694deaa8 | 162 | thresh = 512; |
roberthill04 | 0:ef02694deaa8 | 163 | amp = 100; |
roberthill04 | 0:ef02694deaa8 | 164 | firstBeat = true; |
roberthill04 | 0:ef02694deaa8 | 165 | secondBeat = true; |
roberthill04 | 0:ef02694deaa8 | 166 | |
roberthill04 | 0:ef02694deaa8 | 167 | BPM=0; |
roberthill04 | 0:ef02694deaa8 | 168 | Signal=0; |
roberthill04 | 0:ef02694deaa8 | 169 | IBI = 600; |
roberthill04 | 0:ef02694deaa8 | 170 | Pulse = false; |
roberthill04 | 0:ef02694deaa8 | 171 | QS = false; |
roberthill04 | 0:ef02694deaa8 | 172 | |
roberthill04 | 0:ef02694deaa8 | 173 | _pulseSensorTicker.attach(this, &PulseSensor::sensor_ticker_callback, ((float)_sensorTickRateMs/1000)); |
roberthill04 | 0:ef02694deaa8 | 174 | _processDataTicker.attach(this, &PulseSensor::process_data_ticker_callback, ((float)_callbackRateMs/1000)); |
roberthill04 | 0:ef02694deaa8 | 175 | _started = true; |
roberthill04 | 0:ef02694deaa8 | 176 | return true; |
roberthill04 | 0:ef02694deaa8 | 177 | } |
roberthill04 | 0:ef02694deaa8 | 178 | else |
roberthill04 | 0:ef02694deaa8 | 179 | { |
roberthill04 | 0:ef02694deaa8 | 180 | return false; |
roberthill04 | 0:ef02694deaa8 | 181 | } |
roberthill04 | 0:ef02694deaa8 | 182 | } |
roberthill04 | 0:ef02694deaa8 | 183 | |
roberthill04 | 0:ef02694deaa8 | 184 | bool PulseSensor::stop() |
roberthill04 | 0:ef02694deaa8 | 185 | { |
roberthill04 | 0:ef02694deaa8 | 186 | if(_started == true) |
roberthill04 | 0:ef02694deaa8 | 187 | { |
roberthill04 | 0:ef02694deaa8 | 188 | _pulseSensorTicker.detach(); |
roberthill04 | 0:ef02694deaa8 | 189 | _processDataTicker.detach(); |
roberthill04 | 0:ef02694deaa8 | 190 | _started = false; |
roberthill04 | 0:ef02694deaa8 | 191 | return true; |
roberthill04 | 0:ef02694deaa8 | 192 | } |
roberthill04 | 0:ef02694deaa8 | 193 | else |
roberthill04 | 0:ef02694deaa8 | 194 | { |
roberthill04 | 0:ef02694deaa8 | 195 | return false; |
roberthill04 | 0:ef02694deaa8 | 196 | } |
roberthill04 | 0:ef02694deaa8 | 197 | } |
roberthill04 | 0:ef02694deaa8 | 198 | ////////////////////////////////////////////////////////End of Heart Rate Processing section////////////////////////////////////////////////////////////////////////////////// |
roberthill04 | 0:ef02694deaa8 | 199 | |
roberthill04 | 0:ef02694deaa8 | 200 | ////////////////////////////////////////////////////////Hall Effect Processing section///////////////////////////////////////////////////// |
roberthill04 | 0:ef02694deaa8 | 201 | |
roberthill04 | 0:ef02694deaa8 | 202 | void Hall_Effect_Count(void) |
roberthill04 | 0:ef02694deaa8 | 203 | { |
roberthill04 | 0:ef02694deaa8 | 204 | Hall_Timer.stop(); //stop the timer |
roberthill04 | 0:ef02694deaa8 | 205 | Pedal_Time=Hall_Timer.read()/60; //Divides Time in seconds by 60 so we have minutes for Pedal_Speed (RPM) |
roberthill04 | 0:ef02694deaa8 | 206 | Pedal_Speed=Hall_Counter/(Hall_Timer.read()/60); //Calculates pedal speed in units of RPM |
roberthill04 | 0:ef02694deaa8 | 207 | |
roberthill04 | 0:ef02694deaa8 | 208 | //Hall_Timer.reset(); //idea here is the timer is reset after the program outputs the pedal speed so the demo can be reran |
roberthill04 | 0:ef02694deaa8 | 209 | |
roberthill04 | 0:ef02694deaa8 | 210 | } |
roberthill04 | 0:ef02694deaa8 | 211 | |
roberthill04 | 0:ef02694deaa8 | 212 | void RPM (void) |
roberthill04 | 0:ef02694deaa8 | 213 | { |
roberthill04 | 0:ef02694deaa8 | 214 | Button_Pressed = true; |
roberthill04 | 0:ef02694deaa8 | 215 | |
roberthill04 | 0:ef02694deaa8 | 216 | if(HallEffect==0 && Low_Hall==true) { //If Hall Effect Digital Output is low |
roberthill04 | 0:ef02694deaa8 | 217 | Hall_Counter++; //Add one to counter for calc pedal speed |
roberthill04 | 0:ef02694deaa8 | 218 | led_green = 0; //Output Green on LED, simulates wheel rotation "sensed" |
roberthill04 | 0:ef02694deaa8 | 219 | led_red = 1; |
roberthill04 | 0:ef02694deaa8 | 220 | led_blue = 1; |
roberthill04 | 0:ef02694deaa8 | 221 | Low_Hall = false; //flag to avoid errors |
roberthill04 | 0:ef02694deaa8 | 222 | } |
roberthill04 | 0:ef02694deaa8 | 223 | else if(HallEffect==1 && Low_Hall==true){ //Additional logic for accurate readings |
roberthill04 | 0:ef02694deaa8 | 224 | led_green = 1; |
roberthill04 | 0:ef02694deaa8 | 225 | led_red = 0; //Stays red while hall effect outputs digital high |
roberthill04 | 0:ef02694deaa8 | 226 | led_blue = 1; |
roberthill04 | 0:ef02694deaa8 | 227 | } |
roberthill04 | 0:ef02694deaa8 | 228 | else if(HallEffect==0 && Low_Hall==false){ |
roberthill04 | 0:ef02694deaa8 | 229 | led_green = 0; |
roberthill04 | 0:ef02694deaa8 | 230 | led_red = 1; |
roberthill04 | 0:ef02694deaa8 | 231 | led_blue = 1; |
roberthill04 | 0:ef02694deaa8 | 232 | |
roberthill04 | 0:ef02694deaa8 | 233 | } |
roberthill04 | 0:ef02694deaa8 | 234 | else if(HallEffect==1 && Low_Hall==false){ |
roberthill04 | 0:ef02694deaa8 | 235 | led_green = 1; |
roberthill04 | 0:ef02694deaa8 | 236 | led_red = 0; |
roberthill04 | 0:ef02694deaa8 | 237 | led_blue = 1; |
roberthill04 | 0:ef02694deaa8 | 238 | Low_Hall = true; |
roberthill04 | 0:ef02694deaa8 | 239 | } |
roberthill04 | 0:ef02694deaa8 | 240 | |
roberthill04 | 0:ef02694deaa8 | 241 | |
roberthill04 | 0:ef02694deaa8 | 242 | } |
roberthill04 | 0:ef02694deaa8 | 243 | ////////////////////////////////////////////////////////End of Hall Effect Processing section////////////////////////////////////////////////////////////////////////////////// |
roberthill04 | 0:ef02694deaa8 | 244 | |
roberthill04 | 0:ef02694deaa8 | 245 | ////////////////////////////////////////////////////////Inclinometer Processing section////////////////////////////////////////////////////////////////////////////////// |
roberthill04 | 0:ef02694deaa8 | 246 | /* |
roberthill04 | 0:ef02694deaa8 | 247 | In order to calculate angle from accel data use the following algorithm: |
roberthill04 | 0:ef02694deaa8 | 248 | Ax = arctan( rawX/sqrt(rawY^2 + rawZ^2)) |
roberthill04 | 0:ef02694deaa8 | 249 | Ay = arctan( rawY/sqrt(rawX^2 + rawZ^2)) |
roberthill04 | 0:ef02694deaa8 | 250 | using Ax as an example: |
roberthill04 | 0:ef02694deaa8 | 251 | we have denom_T = the total denominator = (sqrt(rawY^2 + rawZ^2 ) |
roberthill04 | 0:ef02694deaa8 | 252 | denom_A = rawY^2 && denom_B = rawZ^2 && denom_C = denom_A + denom_B |
roberthill04 | 0:ef02694deaa8 | 253 | we may only be concerned with one of these angles for our application |
roberthill04 | 0:ef02694deaa8 | 254 | also note value is output in radians will need to convert to degrees using: 180/PI |
roberthill04 | 0:ef02694deaa8 | 255 | |
roberthill04 | 0:ef02694deaa8 | 256 | example of how to use inverse tangent function : |
roberthill04 | 0:ef02694deaa8 | 257 | |
roberthill04 | 0:ef02694deaa8 | 258 | int main () |
roberthill04 | 0:ef02694deaa8 | 259 | { |
roberthill04 | 0:ef02694deaa8 | 260 | double param, result; |
roberthill04 | 0:ef02694deaa8 | 261 | param = 1.0; |
roberthill04 | 0:ef02694deaa8 | 262 | result = atan (param) * 180 / PI; |
roberthill04 | 0:ef02694deaa8 | 263 | printf ("The arc tangent of %f is %f degrees\n", param, result ); |
roberthill04 | 0:ef02694deaa8 | 264 | return 0; |
roberthill04 | 0:ef02694deaa8 | 265 | } |
roberthill04 | 0:ef02694deaa8 | 266 | */ |
roberthill04 | 0:ef02694deaa8 | 267 | |
roberthill04 | 0:ef02694deaa8 | 268 | /* |
roberthill04 | 0:ef02694deaa8 | 269 | Example of pow: 7 ^ 3 would be written as pow(7.0, 3.0); |
roberthill04 | 0:ef02694deaa8 | 270 | we can use this find squareroots by making the power exponent = 0.5 |
roberthill04 | 0:ef02694deaa8 | 271 | */ |
roberthill04 | 0:ef02694deaa8 | 272 | void Y_AXIS(void) |
roberthill04 | 0:ef02694deaa8 | 273 | { |
roberthill04 | 0:ef02694deaa8 | 274 | |
roberthill04 | 0:ef02694deaa8 | 275 | float faX, faY, faZ; //intialize float variables for incoming raw sensor values |
roberthill04 | 0:ef02694deaa8 | 276 | acc.getX(faX); //get raw accelerometer data in the X axis |
roberthill04 | 0:ef02694deaa8 | 277 | acc.getY(faY); //"" Y axis |
roberthill04 | 0:ef02694deaa8 | 278 | acc.getZ(faZ); //"" Z axis |
roberthill04 | 0:ef02694deaa8 | 279 | denomY_A = pow(faX, 2); |
roberthill04 | 0:ef02694deaa8 | 280 | denomY_B = pow(faZ, 2); |
roberthill04 | 0:ef02694deaa8 | 281 | denomY_C = denomY_A + denomY_B; |
roberthill04 | 0:ef02694deaa8 | 282 | denomY_T = pow(denomY_C, .5); //pow returns base raised to the power exponent |
roberthill04 | 0:ef02694deaa8 | 283 | |
roberthill04 | 0:ef02694deaa8 | 284 | angleY = atan(faY/denomY_T) * 180/PI; //should calculate the angle on the Y axis in degrees based on raw data |
roberthill04 | 0:ef02694deaa8 | 285 | } |
roberthill04 | 0:ef02694deaa8 | 286 | ////////////////////////////////////////////////////////End of Inclinometer Processing section////////////////////////////////////////////////////////////////////////////////// |
roberthill04 | 0:ef02694deaa8 | 287 | |
roberthill04 | 0:ef02694deaa8 | 288 | int main() |
roberthill04 | 0:ef02694deaa8 | 289 | { |
roberthill04 | 0:ef02694deaa8 | 290 | led_blue = 1; //LED Off |
roberthill04 | 0:ef02694deaa8 | 291 | led_green = 1; |
roberthill04 | 0:ef02694deaa8 | 292 | led_red = 1; |
roberthill04 | 0:ef02694deaa8 | 293 | pc.baud(9600); |
roberthill04 | 0:ef02694deaa8 | 294 | pc.printf("Hello World from FRDM-K64F board. This is the Cycle Fit Sensor Demo Program. \t \r\n"); |
roberthill04 | 0:ef02694deaa8 | 295 | pc.printf("This program calcualtes and outputs: Heart Rate(in BPM), Pedal Speed(in RPM), and the Angle of incline(in degrees) Press Button SW2 to see fitness metrics.\t \r\n"); |
roberthill04 | 0:ef02694deaa8 | 296 | |
roberthill04 | 0:ef02694deaa8 | 297 | |
roberthill04 | 0:ef02694deaa8 | 298 | PulseSensor Pulse_Signal(A0, sendDataToProcessing); //Intializes Pulse_Signal to A0 for HR |
roberthill04 | 0:ef02694deaa8 | 299 | Pulse_Signal.start(); //Start collecting data from sensor |
roberthill04 | 0:ef02694deaa8 | 300 | |
roberthill04 | 0:ef02694deaa8 | 301 | Hall_Timer.start(); //Starts Timer for Pedal Speed Calculation |
roberthill04 | 0:ef02694deaa8 | 302 | |
roberthill04 | 0:ef02694deaa8 | 303 | acc.enable(); //enables Accel sensor so it can collect data |
roberthill04 | 0:ef02694deaa8 | 304 | |
roberthill04 | 0:ef02694deaa8 | 305 | |
roberthill04 | 0:ef02694deaa8 | 306 | |
roberthill04 | 0:ef02694deaa8 | 307 | while(1) //NEED TO RUN THOUGH ALL BUTTON PRESS LOGIC FOR COMBINED PROGRAM************************ |
roberthill04 | 0:ef02694deaa8 | 308 | { |
roberthill04 | 0:ef02694deaa8 | 309 | Button_Pressed = true; |
roberthill04 | 0:ef02694deaa8 | 310 | RPM(); //function that finds pedal speed through H.E. sensor |
roberthill04 | 0:ef02694deaa8 | 311 | Y_AXIS(); //function that finds incline angle through Accel sensor |
roberthill04 | 0:ef02694deaa8 | 312 | if (sw2==0 && Button_Pressed == true) |
roberthill04 | 0:ef02694deaa8 | 313 | { |
roberthill04 | 0:ef02694deaa8 | 314 | Pulse_Signal.stop(); //stops the continuous signal (do i need to stop the signal?) |
roberthill04 | 0:ef02694deaa8 | 315 | pc.printf("Current Heart Rate is: %d BPM\t \r\n", Pulse_Signal.BPM); //Outputs Heart Rate |
roberthill04 | 0:ef02694deaa8 | 316 | |
roberthill04 | 0:ef02694deaa8 | 317 | Hall_Effect_Count(); //function for calculating pedal speed |
roberthill04 | 0:ef02694deaa8 | 318 | //Hall_Timer.start(); //restart Hall Effect Timer |
roberthill04 | 0:ef02694deaa8 | 319 | pc.printf("Approximate pedal speed: %d RPM\t \r\n", Pedal_Speed); //Outputs Pedal Speed |
roberthill04 | 0:ef02694deaa8 | 320 | |
roberthill04 | 0:ef02694deaa8 | 321 | printf("Approximate angle in the Y-Axis =%f degrees\t \r\n", angleY); //Outputs Inclination Angle in Y axis |
roberthill04 | 0:ef02694deaa8 | 322 | puts(""); //clears a line under output |
roberthill04 | 0:ef02694deaa8 | 323 | |
roberthill04 | 0:ef02694deaa8 | 324 | //Pulse_Signal.start(); |
roberthill04 | 0:ef02694deaa8 | 325 | //Button_Pressed= false; |
roberthill04 | 0:ef02694deaa8 | 326 | //wait(0.5); |
roberthill04 | 0:ef02694deaa8 | 327 | } |
roberthill04 | 0:ef02694deaa8 | 328 | else if (sw3==0 && Button_Pressed == true) |
roberthill04 | 0:ef02694deaa8 | 329 | { |
roberthill04 | 0:ef02694deaa8 | 330 | Pulse_Signal.start(); |
roberthill04 | 0:ef02694deaa8 | 331 | Hall_Timer.start(); |
roberthill04 | 0:ef02694deaa8 | 332 | } |
roberthill04 | 0:ef02694deaa8 | 333 | } |
roberthill04 | 0:ef02694deaa8 | 334 | } |