Closed BeatrizPer closed 2 years ago
The heart rate sensor stores its values in a FIFO stack. Many readings must be done along with a peak to peak calculation to get the proper heart rate and SPO2 readings. Because of this, the functions for basic functionality are quite extensive. The functions included are the ones needed to get proper and semi-accurate readings. However, after much testing there is a few outlier readings in the returned values for the heart rate reading. It might shoot out a sporadic 200 BPM or 13 BPM. This is a known issue online.
setup(self, led_mode = 0x03) - sets the device with the values written in the Arduino sample code published for the chip by Sparkfun. It sets up the FIFO for the data registers, turns on the enables for reading values, and turns on the LEDs that are needed for the heart rate and SPO2 readings. These are done with the smbus function write_i2c_block_data(addr, cmd, val). The setup function is seen in the code section at 1.1
get_data_present(self) - is responsible for setting FIFO pointers to easily read and navigate the data registers and know when the FIFO is filled. Including these removes the need for using the interrupt on the MAX device. It then gets how much data is in the stack and returns this value.
read_fifo(self) is actually responsible for reading the data in the register. This information is masked and saved under the variables red_led and ir_led. These data values will be used to interpret and calculate the HR and SPO2 readings.
read_sequential(self, amount = 100) is responsible for calling read_fifo 100 times to get 100 values for red_led and ir_led. These 100 values will be stored in red_buf and ir_buf. This will get a bunch of readings that we will later use to get a more accurate reading for SPO2 and HR. All of these can be referenced in the sensor setup section of the code.
calc_hr_spo2(ir_data, red_data) is responsible for taking all the ir and red LED data collected and converting them to meaningful and valid readings. It calls the peak functions below to interpret the data and change the signals given. It gets rid of unnecessary and invalid readings that may occur. It returns HR and SPO2 readings. This is the biggest chunk of code. It is seen in the code section with comments.
find_peaks(x, size, min_height, min_dist, max_num) is responsible for calling the other two peak functions to clean up the peaks collected from the readings. This cleans them up and gets rid of error readings. There is a min and max set to go off of how many and how much to remove.
find_peaks_above_min_heights(x, size, min_height, max_num) is responsible for finding peaks of the ir values that are above the minimum height specified. If there is a height in the array that doesn't meet the specifications, it is removed. This edited array is returned as the new IR readings.
Remove_close_peaks(n_peaks, ir_valley_locs, x_min_dist) is responsible for removing peaks that are too close together and dont meet the specified min distance parameter. The array of IR values is sorted and anything that doesnt meet the requirement is removed from the array. The revised array is returned back to calc_hr_and_spo2 to return meaningful values with the new and updated values.
filled in and done