From 2c6f6cbebea93d0c97be0483eb074554e334deaa Mon Sep 17 00:00:00 2001 From: anpar Date: Thu, 23 Apr 2020 17:15:25 +0200 Subject: [PATCH 1/5] Abnormal frequency check attempt, cc #61 --- src/core/motor_control.c | 25 ++++++++++++++++++++++++- src/core/motor_control.h | 4 ++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/core/motor_control.c b/src/core/motor_control.c index ee04307..b919b6b 100644 --- a/src/core/motor_control.c +++ b/src/core/motor_control.c @@ -113,7 +113,8 @@ static bool waitTimeoutAllowed; static int32_t cycle_volume; static uint32_t cycleCount; - +static uint8_t bpmCheckCycleCount; +static TickType_t bpmCheckStartTime; #if DEBUG_MOTOR static const char *state_names[] = { @@ -265,6 +266,8 @@ void init_motor() { boundedWaitTime = 0; vTaskSetTimeOutState(&timeOutBoundedWait); cycleCount = 0; + + bpmCheckCycleCount = 0; } static void genMotorError(char *msg) { @@ -332,6 +335,21 @@ static void startCycleEnd() { breathState = cycleEnd; TickType_t curr_time = xTaskGetTickCount(); TickType_t cycle_elapsed_time = curr_time - cycleStartTime; + + bpmCheckCycleCount += 1; + // If averaging period of respiratory frequency is over + if (xTaskGetTickCount() - bpmCheckStartTime > pdMS_TO_TICKS(BPM_CHECK_PERIOD_MS)) { + // Measure the actual respiratory frequency + uint8_t bpmMeasured = bpmCheckCycleCount / BPM_CHECK_PERIOD_PER_MIN; + // Raise alarm if absolute difference is not within given tolerance + if (ABS(bpmMeasured - bpm) > BPM_TOL) { + sendNewAlarm(abnFreq); + } + + // Reset rspiratory + bpmCheckCycleCount = 0; + } + TickType_t wait_time; if (cycle_elapsed_time < ticksTctTime) { motor_disable(); @@ -406,6 +424,11 @@ static void startInsp() { motor_enable(); HOOK_START_INSP; breathState = insp; + // Initialize the respiratory frequency measurement + if (bpmCheckCycleCount == 0) { + bpmCheckStartTime = xTaskGetTickCount(); + } + targetPosition = homePosition + insp_pulses; DEBUG_PRINT("Ti pulses used %u",targetPosition); DEBUG_PRINT("=> target %u",targetPosition); diff --git a/src/core/motor_control.h b/src/core/motor_control.h index ed75250..b2832c5 100644 --- a/src/core/motor_control.h +++ b/src/core/motor_control.h @@ -25,6 +25,10 @@ // FIXME put 600000 #define VOLUME_CHECK_THRESHOLD 400000L +#define BPM_CHECK_PERIOD_MS 60 * 1000L +#define BPM_CHECK_PERIOD_PER_MIN (60 * 1000L)/BPM_CHECK_PERIOD_MS +#define BPM_TOL 2 + #define HOOK_START_INSP do { \ reset_volume(); \ reset_pressure(); \ From c9d5972771ea7652015a8410085a0099c352ca7a Mon Sep 17 00:00:00 2001 From: anpar Date: Thu, 23 Apr 2020 18:59:51 +0200 Subject: [PATCH 2/5] Move BPM check to main task, cc #61 --- src/core/main_task.c | 29 +++++++++++++++++++++++++++++ src/core/main_task.h | 4 ++++ src/core/motor_control.c | 25 +------------------------ src/core/motor_control.h | 6 ++---- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/core/main_task.c b/src/core/main_task.c index 3f5fa4c..14f44a2 100644 --- a/src/core/main_task.c +++ b/src/core/main_task.c @@ -29,6 +29,10 @@ volatile GlobalState_t globalState; +// Used to measure the actual respiratory frequency +static uint32_t lastCycleCount; +static TickType_t startBPMCheckTime; + static const char *globalStateDescr[] = { "welcome", "welcome_wait_cal", @@ -39,6 +43,8 @@ static const char *globalStateDescr[] = { }; static void setGlobalState(GlobalState_t newState, bool notifyMotor, bool populateDisplay); +static void resetBPMCheck(); +static void checkBPM(); void initMainTask() { @@ -109,6 +115,8 @@ void MainTask(void *pvParameters) upd_params(buttons_pressed); if (BUTTON_PRESSED(buttons_pressed, button_startstop)) { setGlobalState(run, true, false); + // Reset actual BPM measurement when moving from stop to run + resetBPMCheck(); } break; @@ -118,6 +126,8 @@ void MainTask(void *pvParameters) setGlobalState(stop, true, false); } + checkBPM(); + break; case critical_failure: break; @@ -174,6 +184,25 @@ static void setGlobalState( GlobalState_t newState, } } +static void resetBPMCheck() { + DEBUG_PRINT("Reset BPMCheck"); + lastCycleCount = cycleCount; + startBPMCheckTime = xTaskGetTickCount(); +} + +static void checkBPM() { + TickType_t elapsedTime = xTaskGetTickCount() - startBPMCheckTime; + if (elapsedTime > pdMS_TO_TICKS(BPM_CHECK_PERIOD_MS)) { + uint8_t measuredBPM = ((uint8_t) (cycleCount - lastCycleCount)) / BPM_CHECK_PERIOD_PER_MIN; + DEBUG_PRINT("Measured BPM: %u", measuredBPM); + if (ABS(measuredBPM - bpm) > BPM_TOL) { + sendNewAlarm(abnFreq); + } + + resetBPMCheck(); + } +} + // TODO: move, nothing to do here void check_volume(uint32_t actual_vol) { // Convert target tidal volume to tens µl diff --git a/src/core/main_task.h b/src/core/main_task.h index be146d7..721ceb3 100644 --- a/src/core/main_task.h +++ b/src/core/main_task.h @@ -16,6 +16,10 @@ #define ALARM_AUTO_UNMUTE_DELAY 120 * 1000L +#define BPM_CHECK_PERIOD_MS 60 * 1000L +#define BPM_CHECK_PERIOD_PER_MIN 1 +#define BPM_TOL 2 + // MAIN wake-up notification #define MAIN_NOTIF_ALARM 0x01 diff --git a/src/core/motor_control.c b/src/core/motor_control.c index b919b6b..9cadedb 100644 --- a/src/core/motor_control.c +++ b/src/core/motor_control.c @@ -112,9 +112,7 @@ static TickType_t boundedWaitTime; static bool waitTimeoutAllowed; static int32_t cycle_volume; -static uint32_t cycleCount; -static uint8_t bpmCheckCycleCount; -static TickType_t bpmCheckStartTime; +volatile uint32_t cycleCount; #if DEBUG_MOTOR static const char *state_names[] = { @@ -266,8 +264,6 @@ void init_motor() { boundedWaitTime = 0; vTaskSetTimeOutState(&timeOutBoundedWait); cycleCount = 0; - - bpmCheckCycleCount = 0; } static void genMotorError(char *msg) { @@ -335,21 +331,6 @@ static void startCycleEnd() { breathState = cycleEnd; TickType_t curr_time = xTaskGetTickCount(); TickType_t cycle_elapsed_time = curr_time - cycleStartTime; - - bpmCheckCycleCount += 1; - // If averaging period of respiratory frequency is over - if (xTaskGetTickCount() - bpmCheckStartTime > pdMS_TO_TICKS(BPM_CHECK_PERIOD_MS)) { - // Measure the actual respiratory frequency - uint8_t bpmMeasured = bpmCheckCycleCount / BPM_CHECK_PERIOD_PER_MIN; - // Raise alarm if absolute difference is not within given tolerance - if (ABS(bpmMeasured - bpm) > BPM_TOL) { - sendNewAlarm(abnFreq); - } - - // Reset rspiratory - bpmCheckCycleCount = 0; - } - TickType_t wait_time; if (cycle_elapsed_time < ticksTctTime) { motor_disable(); @@ -424,10 +405,6 @@ static void startInsp() { motor_enable(); HOOK_START_INSP; breathState = insp; - // Initialize the respiratory frequency measurement - if (bpmCheckCycleCount == 0) { - bpmCheckStartTime = xTaskGetTickCount(); - } targetPosition = homePosition + insp_pulses; DEBUG_PRINT("Ti pulses used %u",targetPosition); diff --git a/src/core/motor_control.h b/src/core/motor_control.h index b2832c5..40c5cc6 100644 --- a/src/core/motor_control.h +++ b/src/core/motor_control.h @@ -25,10 +25,6 @@ // FIXME put 600000 #define VOLUME_CHECK_THRESHOLD 400000L -#define BPM_CHECK_PERIOD_MS 60 * 1000L -#define BPM_CHECK_PERIOD_PER_MIN (60 * 1000L)/BPM_CHECK_PERIOD_MS -#define BPM_TOL 2 - #define HOOK_START_INSP do { \ reset_volume(); \ reset_pressure(); \ @@ -113,6 +109,8 @@ extern volatile BreathState_t breathState; */ extern volatile MotorErrorState_t motorErrorState; +extern volatile uint32_t cycleCount; + /** @init_motor Initialize the motor state machine */ void init_motor(); From f2a6b429a53b3225fe10c5e140bc155c04070149 Mon Sep 17 00:00:00 2001 From: anpar Date: Thu, 23 Apr 2020 19:01:55 +0200 Subject: [PATCH 3/5] Remove blank line --- src/core/motor_control.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/core/motor_control.c b/src/core/motor_control.c index 9cadedb..a206bae 100644 --- a/src/core/motor_control.c +++ b/src/core/motor_control.c @@ -405,7 +405,6 @@ static void startInsp() { motor_enable(); HOOK_START_INSP; breathState = insp; - targetPosition = homePosition + insp_pulses; DEBUG_PRINT("Ti pulses used %u",targetPosition); DEBUG_PRINT("=> target %u",targetPosition); From 95c9681e4aa369516aefacacb2aa3aecdd56b264 Mon Sep 17 00:00:00 2001 From: anpar Date: Thu, 23 Apr 2020 17:15:25 +0200 Subject: [PATCH 4/5] Abnormal frequency check, cc #61 --- src/core/main_task.c | 29 +++++++++++++++++++++++++++++ src/core/main_task.h | 4 ++++ src/core/motor_control.c | 3 +-- src/core/motor_control.h | 2 ++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/core/main_task.c b/src/core/main_task.c index 3f5fa4c..14f44a2 100644 --- a/src/core/main_task.c +++ b/src/core/main_task.c @@ -29,6 +29,10 @@ volatile GlobalState_t globalState; +// Used to measure the actual respiratory frequency +static uint32_t lastCycleCount; +static TickType_t startBPMCheckTime; + static const char *globalStateDescr[] = { "welcome", "welcome_wait_cal", @@ -39,6 +43,8 @@ static const char *globalStateDescr[] = { }; static void setGlobalState(GlobalState_t newState, bool notifyMotor, bool populateDisplay); +static void resetBPMCheck(); +static void checkBPM(); void initMainTask() { @@ -109,6 +115,8 @@ void MainTask(void *pvParameters) upd_params(buttons_pressed); if (BUTTON_PRESSED(buttons_pressed, button_startstop)) { setGlobalState(run, true, false); + // Reset actual BPM measurement when moving from stop to run + resetBPMCheck(); } break; @@ -118,6 +126,8 @@ void MainTask(void *pvParameters) setGlobalState(stop, true, false); } + checkBPM(); + break; case critical_failure: break; @@ -174,6 +184,25 @@ static void setGlobalState( GlobalState_t newState, } } +static void resetBPMCheck() { + DEBUG_PRINT("Reset BPMCheck"); + lastCycleCount = cycleCount; + startBPMCheckTime = xTaskGetTickCount(); +} + +static void checkBPM() { + TickType_t elapsedTime = xTaskGetTickCount() - startBPMCheckTime; + if (elapsedTime > pdMS_TO_TICKS(BPM_CHECK_PERIOD_MS)) { + uint8_t measuredBPM = ((uint8_t) (cycleCount - lastCycleCount)) / BPM_CHECK_PERIOD_PER_MIN; + DEBUG_PRINT("Measured BPM: %u", measuredBPM); + if (ABS(measuredBPM - bpm) > BPM_TOL) { + sendNewAlarm(abnFreq); + } + + resetBPMCheck(); + } +} + // TODO: move, nothing to do here void check_volume(uint32_t actual_vol) { // Convert target tidal volume to tens µl diff --git a/src/core/main_task.h b/src/core/main_task.h index be146d7..721ceb3 100644 --- a/src/core/main_task.h +++ b/src/core/main_task.h @@ -16,6 +16,10 @@ #define ALARM_AUTO_UNMUTE_DELAY 120 * 1000L +#define BPM_CHECK_PERIOD_MS 60 * 1000L +#define BPM_CHECK_PERIOD_PER_MIN 1 +#define BPM_TOL 2 + // MAIN wake-up notification #define MAIN_NOTIF_ALARM 0x01 diff --git a/src/core/motor_control.c b/src/core/motor_control.c index ee04307..a206bae 100644 --- a/src/core/motor_control.c +++ b/src/core/motor_control.c @@ -112,8 +112,7 @@ static TickType_t boundedWaitTime; static bool waitTimeoutAllowed; static int32_t cycle_volume; -static uint32_t cycleCount; - +volatile uint32_t cycleCount; #if DEBUG_MOTOR static const char *state_names[] = { diff --git a/src/core/motor_control.h b/src/core/motor_control.h index ed75250..40c5cc6 100644 --- a/src/core/motor_control.h +++ b/src/core/motor_control.h @@ -109,6 +109,8 @@ extern volatile BreathState_t breathState; */ extern volatile MotorErrorState_t motorErrorState; +extern volatile uint32_t cycleCount; + /** @init_motor Initialize the motor state machine */ void init_motor(); From 1e9e5bb0797de2893323b28c7d449db9ae462cc7 Mon Sep 17 00:00:00 2001 From: anpar Date: Thu, 4 Jun 2020 16:57:16 +0200 Subject: [PATCH 5/5] Cast measuredBPM and bpm to int8_t --- src/core/main_task.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/main_task.c b/src/core/main_task.c index 14f44a2..4ab9902 100644 --- a/src/core/main_task.c +++ b/src/core/main_task.c @@ -195,7 +195,7 @@ static void checkBPM() { if (elapsedTime > pdMS_TO_TICKS(BPM_CHECK_PERIOD_MS)) { uint8_t measuredBPM = ((uint8_t) (cycleCount - lastCycleCount)) / BPM_CHECK_PERIOD_PER_MIN; DEBUG_PRINT("Measured BPM: %u", measuredBPM); - if (ABS(measuredBPM - bpm) > BPM_TOL) { + if (ABS(((int8_t) measuredBPM) - ((int8_t) bpm)) > BPM_TOL) { sendNewAlarm(abnFreq); }