Skip to content

Commit

Permalink
Wristband Software Version V1.4
Browse files Browse the repository at this point in the history
Final Implementation of Pulse/Blood Oxygen Sensor
The process runs on separate thread to maintain responsiveness of main loop.

Tuning Required:
Task priority, Task yield delay time
  • Loading branch information
PatrickChenHZ committed Mar 8, 2022
1 parent 04c005a commit ba2420d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
69 changes: 51 additions & 18 deletions WristBand_Software/wristband_v1/src/biological.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ static double eir = 0.0; //estimated lowpass filtered IR signal to find falling
static double firrate = 0.85; //IR filter coefficient to remove notch ,should be smaller than frate
static double eir_prev = 0.0;

double arrspo2[100];
double arrheartrate[100];
double arrspo2[30];
double arrheartrate[30];
int totalsamplespo2 = 0;
int totalsampleheartrate = 0;

Expand Down Expand Up @@ -223,14 +223,14 @@ void updatebio_v2(){
double R = (sqrt(sumredrms) / avered) / (sqrt(sumirrms) / aveir);
SpO2 = -23.3 * (R - 0.4) + 100;
ESpO2 = FSpO2 * ESpO2 + (1.0 - FSpO2) * SpO2;
Serial.print("SPO2: ");
Serial.print(SpO2);Serial.print(",");Serial.print(ESpO2);
Serial.print(" BPM : "); Serial.print(Ebpm);Serial.print(", R:");
Serial.println(R);
//Serial.print("SPO2: ");
//Serial.print(SpO2);Serial.print(",");Serial.print(ESpO2);
//Serial.print(" BPM : "); //Serial.print(Ebpm);//Serial.print(", R:");
//Serial.println(R);
sumredrms = 0.0; sumirrms = 0.0; i = 0;

Serial.print("Heart Rate: ");
Serial.println(Ebpm);
//Serial.print("Heart Rate: ");
//Serial.println(Ebpm);

//it is expected this algrithm does not throw null/out of range datas, so all data is admitted to array
arrspo2[totalsamplespo2] = ESpO2;
Expand Down Expand Up @@ -331,23 +331,24 @@ void biofinalize_v2(){
}

void pub_bio_data(){
pubstr(String(pulse_bpm,2),"clients/wb/wb1upstream1/heartrate");
pubstr(String(bloodoxy,2),"clients/wb/wb1upstream1/spo2");
pubstr(String(pulse_bpm,2),"clients/wb/wb1hr");
pubstr(String(bloodoxy,2),"clients/wb/wb1spo2");
}

/*
void getbiological(){
//45 sec per reading
//init is code v1 only
/*
if(!pulsesensorinit){
biosensorinit();
pulsesensorinit = true;
}
*/
//if(!pulsesensorinit){
// biosensorinit();
// pulsesensorinit = true;
//}
if((millis() > lastpulse + 45000) & !startpulse){
startpulse = true;
lastpulse = millis();
particleSensor.wakeUp();
//particleSensor.wakeUp();
Serial.println("New Pulse round");
}
if(startpulse){
Expand All @@ -359,7 +360,7 @@ void getbiological(){
}
else{
//pulse period expired
particleSensor.shutDown();
//particleSensor.shutDown();
Serial.println("Finished.");
//biofilter();
biofinalize_v2();
Expand All @@ -378,3 +379,35 @@ void getbiological(){
}
}
}
*/


unsigned long last_calc_bio = 0;

//constant reading, calculate average evry 25 second
void getbiological(){
if(last_calc_bio + 25000 < millis()){
biofinalize_v2();
Serial.print("Pulse: ");
Serial.print(finalheartrate);
Serial.print("Blood Oxygen: ");
Serial.println(finalspo2);
//commit to global
bloodoxy = finalspo2;
pulse_bpm = finalheartrate;
pub_bio_data();
last_calc_bio = millis();
}
else{
updatebio_v2();
}
}

void pulse_loop(void * unused){
//try to keep this at end of loop
for(;;){
getbiological();
//purpose is to allow task schedular to do its job, delay() or yield() also works.
vTaskDelay(50 / portTICK_PERIOD_MS);
}
}
13 changes: 11 additions & 2 deletions WristBand_Software/wristband_v1/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
PCF8563_Class rtc;
WiFiManager wifiManager;
TaskHandle_t Mqttthread;
TaskHandle_t pulsethread;
MPU9250_asukiaaa myIMU;
MAX30105 particleSensor;

Expand Down Expand Up @@ -376,6 +377,7 @@ void mqtt_loop(void * unused){
#include "handler.h"

//#include "imu_sensor.h"
#include "biological.h"

void setup() {
Serial.begin(115200);
Expand Down Expand Up @@ -476,10 +478,17 @@ void setup() {
&Mqttthread, //Task handle.
0); // Core where the task should run

xTaskCreatePinnedToCore(
pulse_loop, // Function to implement the task
"pulse_Loop", // Name of the task
10000, // Stack size in words
NULL, // Task input parameter
0, // Priority of the task
&pulsethread, //Task handle.
0); // Core where the task should run
}

#include "fall_detection.h"
#include "biological.h"

void loop() {
//warning 50ms delay included in this loop
Expand Down Expand Up @@ -569,7 +578,7 @@ void loop() {
}

//have the intention to move this to a task, so it does not block main loop
getbiological();
//getbiological();


/*
Expand Down

0 comments on commit ba2420d

Please sign in to comment.