Final Program for Cycle Fit Project, Program is able to collect, process and output via bluetooth values for: Pedal Speed (RPM) Heart Rate (BPM) and incline (degrees)
Dependencies: FXAS21000 FXOS8700Q mbed
PulseSensor.h@0:30fa229c34d6, 2016-04-10 (annotated)
- Committer:
- roberthill04
- Date:
- Sun Apr 10 20:53:46 2016 +0000
- Revision:
- 0:30fa229c34d6
Final Cycle Fit Program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roberthill04 | 0:30fa229c34d6 | 1 | #ifndef PULSE_SENSOR_H |
roberthill04 | 0:30fa229c34d6 | 2 | #define PULSE_SENSOR_H |
roberthill04 | 0:30fa229c34d6 | 3 | |
roberthill04 | 0:30fa229c34d6 | 4 | #include "mbed.h" |
roberthill04 | 0:30fa229c34d6 | 5 | |
roberthill04 | 0:30fa229c34d6 | 6 | |
roberthill04 | 0:30fa229c34d6 | 7 | /** |
roberthill04 | 0:30fa229c34d6 | 8 | * Class for interfacing with a http://pulsesensor.myshopify.com/ 'Pulse Sensor Amped'. |
roberthill04 | 0:30fa229c34d6 | 9 | * The contents of this class are based on the "Pulse Sensor Amped 1.1" Arduino Sketch. |
roberthill04 | 0:30fa229c34d6 | 10 | * |
roberthill04 | 0:30fa229c34d6 | 11 | * When using this class for the first time, it is recommended that you use the Processing |
roberthill04 | 0:30fa229c34d6 | 12 | * GUI app available http://pulsesensor.myshopify.com/pages/code-and-guide. Using this, you |
roberthill04 | 0:30fa229c34d6 | 13 | * will easily be able to verify the operating of your sensor, and the integration of this |
roberthill04 | 0:30fa229c34d6 | 14 | * class into your mbed project. |
roberthill04 | 0:30fa229c34d6 | 15 | * WEBSITE is incorrect^^^ |
roberthill04 | 0:30fa229c34d6 | 16 | **/ |
roberthill04 | 0:30fa229c34d6 | 17 | |
roberthill04 | 0:30fa229c34d6 | 18 | class PulseSensor |
roberthill04 | 0:30fa229c34d6 | 19 | { |
roberthill04 | 0:30fa229c34d6 | 20 | private: |
roberthill04 | 0:30fa229c34d6 | 21 | volatile int rate[10]; // used to hold last ten IBI values |
roberthill04 | 0:30fa229c34d6 | 22 | volatile unsigned long sampleCounter; // used to determine pulse timing |
roberthill04 | 0:30fa229c34d6 | 23 | volatile unsigned long lastBeatTime; // used to find the inter beat interval |
roberthill04 | 0:30fa229c34d6 | 24 | volatile int P; // used to find peak in pulse wave |
roberthill04 | 0:30fa229c34d6 | 25 | volatile int T; // used to find trough in pulse wave |
roberthill04 | 0:30fa229c34d6 | 26 | volatile int thresh; // used to find instant moment of heart beat |
roberthill04 | 0:30fa229c34d6 | 27 | volatile int amp; // used to hold amplitude of pulse waveform |
roberthill04 | 0:30fa229c34d6 | 28 | volatile bool firstBeat; // used to seed rate array so we startup with reasonable BPM |
roberthill04 | 0:30fa229c34d6 | 29 | volatile bool secondBeat; // used to seed rate array so we startup with reasonable BPM |
roberthill04 | 0:30fa229c34d6 | 30 | |
roberthill04 | 0:30fa229c34d6 | 31 | // these variables are volatile because they are used during the interrupt service routine! |
roberthill04 | 0:30fa229c34d6 | 32 | |
roberthill04 | 0:30fa229c34d6 | 33 | volatile int Signal; // holds the incoming raw data |
roberthill04 | 0:30fa229c34d6 | 34 | volatile int IBI; // holds the time between beats, the Inter-Beat Interval |
roberthill04 | 0:30fa229c34d6 | 35 | volatile bool Pulse; // true when pulse wave is high, false when it's low |
roberthill04 | 0:30fa229c34d6 | 36 | volatile bool QS; // becomes true when a beat is found |
roberthill04 | 0:30fa229c34d6 | 37 | |
roberthill04 | 0:30fa229c34d6 | 38 | |
roberthill04 | 0:30fa229c34d6 | 39 | void (*_printDataCallback)(char,int); |
roberthill04 | 0:30fa229c34d6 | 40 | static const int _sensorTickRateMs = 2; |
roberthill04 | 0:30fa229c34d6 | 41 | int _callbackRateMs; |
roberthill04 | 0:30fa229c34d6 | 42 | bool _started; |
roberthill04 | 0:30fa229c34d6 | 43 | |
roberthill04 | 0:30fa229c34d6 | 44 | AnalogIn *_pAin; |
roberthill04 | 0:30fa229c34d6 | 45 | Ticker _pulseSensorTicker; |
roberthill04 | 0:30fa229c34d6 | 46 | Ticker _processDataTicker; |
roberthill04 | 0:30fa229c34d6 | 47 | |
roberthill04 | 0:30fa229c34d6 | 48 | void sensor_ticker_callback(void); |
roberthill04 | 0:30fa229c34d6 | 49 | void process_data_ticker_callback(void); |
roberthill04 | 0:30fa229c34d6 | 50 | |
roberthill04 | 0:30fa229c34d6 | 51 | public: |
roberthill04 | 0:30fa229c34d6 | 52 | |
roberthill04 | 0:30fa229c34d6 | 53 | volatile int BPM; // used to hold the pulse rate |
roberthill04 | 0:30fa229c34d6 | 54 | /** PulseSensor Constructor - Note this does not start the reading of the sensor. |
roberthill04 | 0:30fa229c34d6 | 55 | * @param analogPin Name of the analog pin that the sensor is connected to. |
roberthill04 | 0:30fa229c34d6 | 56 | * @param printDataCallback Pointer to function which will be called to print the latest data. Output format available here: http://pulsesensor.myshopify.com/pages/code-and-guide |
roberthill04 | 0:30fa229c34d6 | 57 | * @param callbackRateMs Rate at which the printDataCallback is to be called, recommended is 20ms for graphing of pulse signal. |
roberthill04 | 0:30fa229c34d6 | 58 | */ |
roberthill04 | 0:30fa229c34d6 | 59 | PulseSensor(PinName analogPin, void (*printDataCallback)(char,int), int callbackRateMs=20); |
roberthill04 | 0:30fa229c34d6 | 60 | |
roberthill04 | 0:30fa229c34d6 | 61 | /** Destructor */ |
roberthill04 | 0:30fa229c34d6 | 62 | ~PulseSensor(); |
roberthill04 | 0:30fa229c34d6 | 63 | |
roberthill04 | 0:30fa229c34d6 | 64 | /** Start reading the Pulse Sensor, and sending current readings to the print data callback. |
roberthill04 | 0:30fa229c34d6 | 65 | * @returns true if reading of the sensor is started, false if reading was aleady in progress. |
roberthill04 | 0:30fa229c34d6 | 66 | */ |
roberthill04 | 0:30fa229c34d6 | 67 | bool start(); |
roberthill04 | 0:30fa229c34d6 | 68 | |
roberthill04 | 0:30fa229c34d6 | 69 | /** Stops the current reading of the Pulse Senson. |
roberthill04 | 0:30fa229c34d6 | 70 | * @return true if reading is stopped, false if reading was already stopped. |
roberthill04 | 0:30fa229c34d6 | 71 | */ |
roberthill04 | 0:30fa229c34d6 | 72 | bool stop(); |
roberthill04 | 0:30fa229c34d6 | 73 | }; |
roberthill04 | 0:30fa229c34d6 | 74 | |
roberthill04 | 0:30fa229c34d6 | 75 | #endif |