Index: firmware/App/Controllers/BloodFlow.c =================================================================== diff -u -r054fa08b67ed2a31f7848b179fbcd1b4da501b0f -r8e7158d8231435496fcf1d5649e51babf859ccc7 --- firmware/App/Controllers/BloodFlow.c (.../BloodFlow.c) (revision 054fa08b67ed2a31f7848b179fbcd1b4da501b0f) +++ firmware/App/Controllers/BloodFlow.c (.../BloodFlow.c) (revision 8e7158d8231435496fcf1d5649e51babf859ccc7) @@ -1,135 +1,195 @@ -/************************************************************************** - * - * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. - * - * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN - * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. - * - * @file BloodFlow.c - * - * @date 05-Nov-2019 - * @author S. Nash - * - * @brief Monitor/Controller for blood pump and flow sensor. - * - **************************************************************************/ +/************************************************************************** +* +* Copyright (c) 2019-2021 Diality Inc. - All Rights Reserved. +* +* THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN +* WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. +* +* @file BloodFlow.c +* +* @author (last) Sean Nash +* @date (last) 01-Dec-2020 +* +* @author (original) Sean Nash +* @date (original) 07-Nov-2019 +* +***************************************************************************/ -#ifndef _VECTORCAST_ - #include -#endif +#include #include "can.h" #include "etpwm.h" +// TODO - test includes - remove later +#include "DialInFlow.h" +#include "PresOccl.h" + +#include "BloodFlow.h" #include "FPGA.h" -#include "InternalADC.h" +#include "InternalADC.h" +#include "NVDataMgmt.h" #include "OperationModes.h" -#include "PIControllers.h" +#include "PersistentAlarm.h" +#include "PIControllers.h" +#include "SafetyShutdown.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" #include "TaskPriority.h" #include "Timers.h" -#include "BloodFlow.h" +/** + * @addtogroup BloodFlow + * @{ + */ + // ********** private definitions ********** + +/// interval (ms/task time) at which the blood flow data is published on the CAN bus. +#define BLOOD_FLOW_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) -#define BLOOD_FLOW_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) // interval (ms/task time) at which the blood flow data is published on the CAN bus +#define MAX_BLOOD_PUMP_PWM_STEP_UP_CHANGE 0.008 ///< max duty cycle change when ramping up ~ 100 mL/min/s. +#define MAX_BLOOD_PUMP_PWM_STEP_DN_CHANGE 0.016 ///< max duty cycle change when ramping down ~ 200 mL/min/s. +#define MAX_BLOOD_PUMP_PWM_DUTY_CYCLE 0.88 ///< controller will error if PWM duty cycle > 90%, so set max to 88% +#define MIN_BLOOD_PUMP_PWM_DUTY_CYCLE 0.12 ///< controller will error if PWM duty cycle < 10%, so set min to 12% + +/// interval (ms/task time) at which the blood pump is controlled. +#define BP_CONTROL_INTERVAL ( 10000 / TASK_GENERAL_INTERVAL ) +#define BP_P_COEFFICIENT 0.00035 ///< P term for blood pump control +#define BP_I_COEFFICIENT 0.00035 ///< I term for blood pump control -#define MAX_BLOOD_PUMP_PWM_STEP_CHANGE 0.01 // max duty cycle change when ramping -#define MAX_BLOOD_PUMP_PWM_DUTY_CYCLE 0.88 // controller will error if PWM duty cycle > 90%, so set max to 88% -#define MIN_BLOOD_PUMP_PWM_DUTY_CYCLE 0.12 // controller will error if PWM duty cycle < 10%, so set min to 12% +#define BP_HOME_RATE 100 ///< target pump speed (in estimate mL/min) for homing. +#define BP_HOME_TIMEOUT_MS 10000 ///< maximum time allowed for homing to complete (in ms). +/// interval (ms/task time) at which the blood pump speed is calculated (every 40 ms). +#define BP_SPEED_CALC_INTERVAL ( 40 / TASK_PRIORITY_INTERVAL ) +/// number of hall sensor counts kept in buffer to hold last 1 second of count data. +#define BP_SPEED_CALC_BUFFER_LEN ( 1000 / BP_SPEED_CALC_INTERVAL / TASK_PRIORITY_INTERVAL ) +#define BP_HALL_EDGE_COUNTS_PER_REV 48 ///< number of hall sensor edge counts per motor revolution. +#define BP_MAX_ROTOR_SPEED_RPM 100.0 ///< maximum rotor speed allowed for blood pump. -#define BP_CONTROL_INTERVAL ( 1000 / TASK_GENERAL_INTERVAL ) // interval (ms/task time) at which the blood pump is controlled -#define BP_P_COEFFICIENT 0.00005 // P term for blood pump control -#define BP_I_COEFFICIENT 0.00015 // I term for blood pump control +#define BP_MAX_FLOW_VS_SPEED_DIFF_RPM 200.0 ///< maximum difference between measured speed and speed implied by measured flow. +#define BP_MAX_MOTOR_SPEED_WHILE_OFF_RPM 100.0 ///< maximum motor speed (RPM) while motor is commanded off. +#define BP_MAX_ROTOR_VS_MOTOR_DIFF_RPM 5.0 ///< maximum difference in speed between motor and rotor (in rotor RPM). +#define BP_MAX_MOTOR_SPEED_ERROR_RPM 300.0 ///< maximum difference in speed between measured and commanded RPM. +#define BP_FLOW_VS_SPEED_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) for flow vs. motor speed error condition. +#define BP_OFF_ERROR_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) for motor off error condition. +#define BP_MOTOR_SPEED_ERROR_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) motor speed error condition. +#define BP_ROTOR_SPEED_ERROR_PERSIST ((12 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) rotor speed error condition. +#define BP_DIRECTION_ERROR_PERSIST (250 / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) pump direction error condition. +#define BP_MAX_ROTOR_SPEED_ERROR_PERSIST ((1 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) blood pump rotor speed too fast error condition. -#define BP_MAX_CURR_WHEN_STOPPED_MA 150.0 // motor controller current should not exceed this when pump should be stopped -#define BP_MIN_CURR_WHEN_RUNNING_MA 150.0 // motor controller current should always exceed this when pump should be running -#define BP_MAX_CURR_WHEN_RUNNING_MA 1000.0 // motor controller current should not exceed this when pump should be running -#define BP_MAX_CURR_ERROR_DURATION_MS 2000 // motor controller current errors persisting beyond this duration will trigger an alarm +#define BP_MAX_CURR_WHEN_STOPPED_MA 150.0 ///< motor controller current should not exceed this when pump should be stopped +#define BP_MIN_CURR_WHEN_RUNNING_MA 150.0 ///< motor controller current should always exceed this when pump should be running +#define BP_MAX_CURR_WHEN_RUNNING_MA 2000.0 ///< motor controller current should not exceed this when pump should be running +#define BP_MAX_CURR_ERROR_DURATION_MS 2000 ///< motor controller current errors persisting beyond this duration will trigger an alarm -#define BP_SPEED_ADC_TO_RPM_FACTOR 1.280938 // conversion factor from ADC counts to RPM for blood pump motor -#define BP_CURRENT_ADC_TO_MA_FACTOR 3.002 // conversion factor from ADC counts to mA for blood pump motor +#define BP_SPEED_ADC_TO_RPM_FACTOR 1.280938 ///< conversion factor from ADC counts to RPM for blood pump motor +#define BP_CURRENT_ADC_TO_MA_FACTOR 3.002 ///< conversion factor from ADC counts to mA for blood pump motor -#define BP_REV_PER_LITER 150.24 // rotor revolutions per liter -#define BP_ML_PER_MIN_TO_PUMP_RPM_FACTOR ( BP_REV_PER_LITER / ML_PER_LITER ) -#define BP_GEAR_RATIO 32.0 // blood pump motor to blood pump gear ratio -#define BP_MOTOR_RPM_TO_PWM_DC_FACTOR 0.00035 // ~28 BP motor RPM = 1% PWM duty cycle -#define BP_PWM_ZERO_OFFSET 0.1 // 10% PWM duty cycle = zero speed +#define BP_REV_PER_LITER 150.0 ///< rotor revolutions per liter +#define BP_ML_PER_MIN_TO_PUMP_RPM_FACTOR ( BP_REV_PER_LITER / ML_PER_LITER ) ///< conversion factor from mL/min to motor RPM. +#define BP_GEAR_RATIO 32.0 ///< blood pump motor to blood pump gear ratio +#define BP_MOTOR_RPM_TO_PWM_DC_FACTOR 0.00028 ///< ~28 BP motor RPM = 1% PWM duty cycle +#define BP_PWM_ZERO_OFFSET 0.1 ///< 10% PWM duty cycle = zero speed +/// conversion factor from mL/min to estimated PWM duty cycle %. #define BP_PWM_FROM_ML_PER_MIN(rate) ( (rate) * BP_ML_PER_MIN_TO_PUMP_RPM_FACTOR * BP_GEAR_RATIO * BP_MOTOR_RPM_TO_PWM_DC_FACTOR + BP_PWM_ZERO_OFFSET ) -#define BLOODPUMP_ADC_FULL_SCALE_V 3.0 // BP analog signals are 0-3V (while int. ADC ref may be different) -#define BLOODPUMP_ADC_ZERO ( (F32)( INT_ADC_ZERO ) * ( BLOODPUMP_ADC_FULL_SCALE_V / INT_ADC_REF_V ) ) +#define BLOODPUMP_ADC_FULL_SCALE_V 3.0 ///< BP analog signals are 0-3V (while int. ADC ref may be different) +#define BLOODPUMP_ADC_ZERO 1998 ///< Blood pump ADC channel zero offset. +/// macro converts 12 bit ADC value to signed 16-bit value. #define SIGN_FROM_12_BIT_VALUE(v) ( (S16)(v) - (S16)BLOODPUMP_ADC_ZERO ) +/// measured blood flow is filtered w/ moving average +#define SIZE_OF_ROLLING_AVG ( ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) * 10 ) + +/// blood flow sensor signal strength low alarm persistence. +#define FLOW_SIG_STRGTH_ALARM_PERSIST ( 5 * MS_PER_SECOND ) +#define MIN_FLOW_SIG_STRENGTH 0.9 ///< Minimum flow sensor signal strength (90%). -#define BLOOD_FLOW_SAMPLE_FREQ ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) -#define SIZE_OF_ROLLING_AVG ( BLOOD_FLOW_SAMPLE_FREQ * 2 ) // measured blood flow is filtered w/ moving average -#define MAX_FLOW_FILTER_INTERVAL 5 // slowest sample interval for filter is every 5th sample - +/// Enumeration of blood pump controller states. typedef enum BloodPump_States { - BLOOD_PUMP_OFF_STATE = 0, - BLOOD_PUMP_RAMPING_UP_STATE, - BLOOD_PUMP_RAMPING_DOWN_STATE, - BLOOD_PUMP_CONTROL_TO_TARGET_STATE, - NUM_OF_BLOOD_PUMP_STATES + BLOOD_PUMP_OFF_STATE = 0, ///< Blood pump off state. + BLOOD_PUMP_RAMPING_UP_STATE, ///< Blood pump ramping up state. + BLOOD_PUMP_RAMPING_DOWN_STATE, ///< Blood pump ramping down state. + BLOOD_PUMP_CONTROL_TO_TARGET_STATE, ///< Blood pump controlling to target state. + NUM_OF_BLOOD_PUMP_STATES ///< Number of blood pump states. } BLOOD_PUMP_STATE_T; +/// Enumeration of blood pump self-test states. typedef enum BloodFlow_Self_Test_States { - BLOOD_FLOW_SELF_TEST_STATE_START = 0, - BLOOD_FLOW_TEST_STATE_IN_PROGRESS, - BLOOD_FLOW_TEST_STATE_COMPLETE, - NUM_OF_BLOOD_FLOW_SELF_TEST_STATES + BLOOD_FLOW_SELF_TEST_STATE_START = 0, ///< Blood pump self-test start state. + BLOOD_FLOW_TEST_STATE_IN_PROGRESS, ///< Blood pump self-test in progress state. + BLOOD_FLOW_TEST_STATE_COMPLETE, ///< Blood pump self-test completed state. + NUM_OF_BLOOD_FLOW_SELF_TEST_STATES ///< Number of blood pump self-test states. } BLOOD_FLOW_SELF_TEST_STATE_T; // pin assignments for pump stop and direction outputs #define STOP_CAN3_PORT_MASK 0x00000002 // (Tx - re-purposed as output GPIO for blood pump stop signal) #define DIR_CAN3_PORT_MASK 0x00000002 // (Rx - re-purposed as output GPIO for blood pump direction signal) // blood pump stop and direction macros -#define SET_BP_DIR() {canREG3->RIOC |= DIR_CAN3_PORT_MASK;} -#define CLR_BP_DIR() {canREG3->RIOC &= ~DIR_CAN3_PORT_MASK;} +#define SET_BP_DIR() {canREG3->RIOC |= DIR_CAN3_PORT_MASK;} // macro to set blood pump direction signal high. +#define CLR_BP_DIR() {canREG3->RIOC &= ~DIR_CAN3_PORT_MASK;} // macro to set blood pump direction signal low. #ifndef BREADBOARD_TARGET - #define SET_BP_STOP() {canREG3->TIOC &= ~STOP_CAN3_PORT_MASK;} - #define CLR_BP_STOP() {canREG3->TIOC |= STOP_CAN3_PORT_MASK;} + #define SET_BP_STOP() {canREG3->TIOC &= ~STOP_CAN3_PORT_MASK;} // macro to set blood pump stop signal (active low). + #define CLR_BP_STOP() {canREG3->TIOC |= STOP_CAN3_PORT_MASK;} // macro to clear blood pump stop signal (active low). #else #define SET_BP_STOP() {canREG3->TIOC |= STOP_CAN3_PORT_MASK;} #define CLR_BP_STOP() {canREG3->TIOC &= ~STOP_CAN3_PORT_MASK;} #endif // ********** private data ********** -static BLOOD_PUMP_STATE_T bloodPumpState = BLOOD_PUMP_OFF_STATE; // current state of blood flow controller state machine -static U32 bloodFlowDataPublicationTimerCounter = 0; // used to schedule blood flow data publication to CAN bus -static BOOL isBloodPumpOn = FALSE; // blood pump is currently running -static F32 bloodPumpPWMDutyCyclePct = 0.0; // initial blood pump PWM duty cycle -static F32 bloodPumpPWMDutyCyclePctSet = 0.0; // currently set blood pump PWM duty cycle -static MOTOR_DIR_T bloodPumpDirection = MOTOR_DIR_FORWARD; // requested blood flow direction -static MOTOR_DIR_T bloodPumpDirectionSet = MOTOR_DIR_FORWARD; // currently set blood flow direction +static BLOOD_PUMP_STATE_T bloodPumpState = BLOOD_PUMP_OFF_STATE; ///< current state of blood flow controller state machine +static U32 bloodFlowDataPublicationTimerCounter = 0; ///< used to schedule blood flow data publication to CAN bus +static BOOL isBloodPumpOn = FALSE; ///< blood pump is currently running +static F32 bloodPumpPWMDutyCyclePct = 0.0; ///< initial blood pump PWM duty cycle +static F32 bloodPumpPWMDutyCyclePctSet = 0.0; ///< currently set blood pump PWM duty cycle +static MOTOR_DIR_T bloodPumpDirection = MOTOR_DIR_FORWARD; ///< requested blood flow direction +static MOTOR_DIR_T bloodPumpDirectionSet = MOTOR_DIR_FORWARD; ///< currently set blood flow direction static PUMP_CONTROL_MODE_T bloodPumpControlMode = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< requested blood pump control mode. -static PUMP_CONTROL_MODE_T bloodPumpControlModeSet = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< currently set blood pump control mode. +static PUMP_CONTROL_MODE_T bloodPumpControlModeSet = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< currently set blood pump control mode. +static F32 bloodFlowCalGain = 1.0; ///< blood flow calibration gain. +static F32 bloodFlowCalOffset = 0.0; ///< blood flow calibration offset. -DATA_DECL( U32, BloodFlowDataPub, bloodFlowDataPublishInterval, BLOOD_FLOW_DATA_PUB_INTERVAL, BLOOD_FLOW_DATA_PUB_INTERVAL ); // interval (in ms) at which to publish blood flow data to CAN bus -DATA_DECL( S32, TargetBloodFlowRate, targetBloodFlowRate, 0, 0 ); // requested blood flow rate -DATA_DECL( F32, MeasuredBloodFlowRate, measuredBloodFlowRate, 0.0, 0.0 ); // measured blood flow rate -DATA_DECL( F32, MeasuredBloodPumpRotorSpeed, bloodPumpRotorSpeedRPM, 0.0, 0.0 );// measured blood pump rotor speed -DATA_DECL( F32, MeasuredBloodPumpSpeed, bloodPumpSpeedRPM, 0.0, 0.0 ); // measured blood pump motor speed -DATA_DECL( F32, MeasuredBloodPumpMCSpeed, adcBloodPumpMCSpeedRPM, 0.0, 0.0 ); // measured blood pump motor controller speed -DATA_DECL( F32, MeasuredBloodPumpMCCurrent, adcBloodPumpMCCurrentmA, 0.0, 0.0 );// measured blood pump motor controller current +/// Interval (in task intervals) at which to publish blood flow data to CAN bus. +static OVERRIDE_U32_T bloodFlowDataPublishInterval = { BLOOD_FLOW_DATA_PUB_INTERVAL, BLOOD_FLOW_DATA_PUB_INTERVAL, BLOOD_FLOW_DATA_PUB_INTERVAL, 0 }; +static S32 targetBloodFlowRate = 0; ///< requested blood flow rate +static OVERRIDE_F32_T measuredBloodFlowRate = { 0.0, 0.0, 0.0, 0 }; ///< measured blood flow rate +static OVERRIDE_F32_T bloodPumpRotorSpeedRPM = { 0.0, 0.0, 0.0, 0 }; ///< measured blood pump rotor speed +static OVERRIDE_F32_T bloodPumpSpeedRPM = { 0.0, 0.0, 0.0, 0 }; ///< measured blood pump motor speed +static OVERRIDE_F32_T adcBloodPumpMCSpeedRPM = { 0.0, 0.0, 0.0, 0 }; ///< measured blood pump motor controller speed +static OVERRIDE_F32_T adcBloodPumpMCCurrentmA = { 0.0, 0.0, 0.0, 0 }; ///< measured blood pump motor controller current +static OVERRIDE_F32_T bloodFlowSignalStrength = { 0.0, 0.0, 0.0, 0 }; ///< measured blood flow signal strength (%) -static U32 bpControlTimerCounter = 0; // determines when to perform control on blood flow +static U32 bpControlTimerCounter = 0; ///< determines when to perform control on blood flow -static F32 flowReadings[ SIZE_OF_ROLLING_AVG ]; // holds flow samples for a rolling average -static U32 flowReadingsIdx = 0; // index for next sample in rolling average array -static F32 flowReadingsTotal = 0.0; // rolling total - used to calc average -static U32 flowReadingsCount = 0; // # of samples in flow rolling average buffer -static U32 flowReadingsTmrCtr = 0; // determines when to add samples to filter +static U32 bpRotorRevStartTime = 0; ///< blood pump rotor rotation start time (in ms) +static U32 bloodPumpRotorCounter = 0; ///< running counter for blood pump rotor revolutions +static BOOL bpStopAtHomePosition = FALSE; ///< stop blood pump at next home position +static U32 bpHomeStartTime = 0; ///< when did blood pump home command begin? (in ms) -static U32 bpCurrErrorDurationCtr = 0; // used for tracking persistence of bp current errors +static U32 bloodPumpMotorEdgeCount = 0; ///< running counter for blood pump motor revolutions +static U16 bpLastMotorHallSensorCounts[ BP_SPEED_CALC_BUFFER_LEN ]; ///< last hall sensor readings for the blood pump motor +static U32 bpMotorSpeedCalcIdx = 0; ///< index into 1 second buffer of motor speed hall sensor counts +static U32 bpMotorSpeedCalcTimerCtr = 0; ///< counter determines interval for calculating blood pump motor speed from hall sensor count. -static BLOOD_FLOW_SELF_TEST_STATE_T bloodPumpSelfTestState = BLOOD_FLOW_SELF_TEST_STATE_START; // current blood pump self test state -static U32 bloodPumpSelfTestTimerCount = 0; // timer counter for blood pump self test +static U32 errorBloodFlowVsMotorSpeedPersistTimerCtr = 0; ///< persistence timer counter for flow vs. motor speed error condition. +static U32 errorBloodMotorOffPersistTimerCtr = 0; ///< persistence timer counter for motor off check error condition. +static U32 errorBloodMotorSpeedPersistTimerCtr = 0; ///< persistence timer counter for motor speed error condition. +static U32 errorBloodRotorSpeedPersistTimerCtr = 0; ///< persistence timer counter for rotor speed error condition. +static U32 errorBloodPumpDirectionPersistTimerCtr = 0; ///< persistence timer counter for pump direction error condition. +static U32 errorBloodPumpRotorTooFastPersistTimerCtr = 0; ///< persistence timer counter for pump rotor too fast error condition. +static F32 flowReadings[ SIZE_OF_ROLLING_AVG ]; ///< holds flow samples for a rolling average +static U32 flowReadingsIdx = 0; ///< index for next sample in rolling average array +static F32 flowReadingsTotal = 0.0; ///< rolling total - used to calc average +static U32 flowReadingsCount = 0; ///< number of samples in flow rolling average buffer + +static U32 bpCurrErrorDurationCtr = 0; ///< used for tracking persistence of bp current errors + +static BLOOD_FLOW_SELF_TEST_STATE_T bloodPumpSelfTestState = BLOOD_FLOW_SELF_TEST_STATE_START; ///< current blood pump self-test state +static U32 bloodPumpSelfTestTimerCount = 0; ///< timer counter for blood pump self-test + // ********** private function prototypes ********** static BLOOD_PUMP_STATE_T handleBloodPumpOffState( void ); @@ -143,43 +203,59 @@ static void publishBloodFlowData( void ); static void resetBloodFlowMovingAverage( void ); static void filterBloodFlowReadings( F32 flow ); +static void updateBloodPumpSpeedAndDirectionFromHallSensors( void ); +static void checkBloodPumpRotor( void ); static void checkBloodPumpDirection( void ); -static void checkBloodPumpMCCurrent( void ); +static void checkBloodPumpSpeeds( void ); +static void checkBloodPumpFlowAgainstSpeed( void ); +static void checkBloodPumpMCCurrent( void ); +static void checkBloodFlowSensorSignalStrength( void ); static DATA_GET_PROTOTYPE( U32, getPublishBloodFlowDataInterval ); -/************************************************************************* - * @brief initBloodFlow +/*********************************************************************//** + * @brief * The initBloodFlow function initializes the BloodFlow module. - * @details - * Inputs : none - * Outputs : BloodFlow module initialized. - * @param none + * @details Inputs: none + * @details Outputs: BloodFlow module initialized. * @return none *************************************************************************/ void initBloodFlow( void ) -{ +{ + U32 i; + stopBloodPump(); setBloodPumpDirection( MOTOR_DIR_FORWARD ); // zero rolling flow average buffer resetBloodFlowMovingAverage(); - + + // zero motor hall sensors counts buffer + bpMotorSpeedCalcIdx = 0; + for ( i = 0; i < BP_SPEED_CALC_BUFFER_LEN; i++ ) + { + bpLastMotorHallSensorCounts[ i ] = 0; + } + // initialize blood flow PI controller initializePIController( PI_CONTROLLER_ID_BLOOD_FLOW, MIN_BLOOD_PUMP_PWM_DUTY_CYCLE, BP_P_COEFFICIENT, BP_I_COEFFICIENT, - MIN_BLOOD_PUMP_PWM_DUTY_CYCLE, MAX_BLOOD_PUMP_PWM_DUTY_CYCLE ); + MIN_BLOOD_PUMP_PWM_DUTY_CYCLE, MAX_BLOOD_PUMP_PWM_DUTY_CYCLE ); + + // initialize persistent alarm for flow sensor signal strength too low + initPersistentAlarm( PERSISTENT_ALARM_BLOOD_FLOW_SIGNAL_STRENGTH, + ALARM_ID_BLOOD_FLOW_SIGNAL_STRENGTH_TOO_LOW, + FALSE, FLOW_SIG_STRGTH_ALARM_PERSIST, FLOW_SIG_STRGTH_ALARM_PERSIST ); } -/************************************************************************* - * @brief setBloodPumpTargetFlowRate +/*********************************************************************//** + * @brief * The setBloodPumpTargetFlowRate function sets a new target flow rate and * pump direction. - * @details - * Inputs : isBloodPumpOn, bloodPumpDirectionSet - * Outputs : targetBloodFlowRate, bloodPumpdirection, bloodPumpPWMDutyCyclePct - * @param flowRate : new target blood flow rate - * @param dir : new blood flow direction - * @param mode : new control mode + * @details Inputs: isBloodPumpOn, bloodPumpDirectionSet + * @details Outputs: targetBloodFlowRate, bloodPumpdirection, bloodPumpPWMDutyCyclePct + * @param flowRate new target blood flow rate + * @param dir new blood flow direction + * @param mode new control mode * @return TRUE if new flow rate & dir are set, FALSE if not *************************************************************************/ BOOL setBloodPumpTargetFlowRate( U32 flowRate, MOTOR_DIR_T dir, PUMP_CONTROL_MODE_T mode ) @@ -193,7 +269,7 @@ if ( flowRate <= MAX_BLOOD_FLOW_RATE ) { resetBloodFlowMovingAverage(); - targetBloodFlowRate.data = ( dir == MOTOR_DIR_FORWARD ? (S32)flowRate : (S32)flowRate * -1 ); + targetBloodFlowRate = ( dir == MOTOR_DIR_FORWARD ? (S32)flowRate : (S32)flowRate * -1 ); bloodPumpDirection = dir; bloodPumpControlMode = mode; // set PWM duty cycle target to an estimated initial target to ramp to based on target flow rate - then we'll control to flow when ramp completed @@ -231,70 +307,167 @@ } else // requested flow rate too high { - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_BLOOD_FLOW_SET_TOO_HIGH, flowRate ) + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_BLOOD_FLOW_SET_TOO_HIGH, flowRate ) } } return result; } -/************************************************************************* - * @brief signalBloodPumpHardStop +/*********************************************************************//** + * @brief * The signalBloodPumpHardStop function stops the blood pump immediately. - * @details - * Inputs : none - * Outputs : Blood pump stopped, set point reset, state changed to off - * @param none + * @details Inputs: none + * @details Outputs: Blood pump stopped, set point reset, state changed to off * @return none *************************************************************************/ void signalBloodPumpHardStop( void ) { - targetBloodFlowRate.data = 0; + targetBloodFlowRate = 0; stopBloodPump(); bloodPumpState = BLOOD_PUMP_OFF_STATE; bloodPumpPWMDutyCyclePct = 0.0; bpControlTimerCounter = 0; resetPIController( PI_CONTROLLER_ID_BLOOD_FLOW, MIN_BLOOD_PUMP_PWM_DUTY_CYCLE ); } -/************************************************************************* - * @brief execBloodFlowMonitor +/*********************************************************************//** + * @brief + * The signalBloodPumpRotorHallSensor function handles the blood pump rotor + * hall sensor detection. Calculates rotor speed (in RPM). Stops pump if + * there is a pending request to home the pump. + * @details Inputs: bpRotorRevStartTime, bpStopAtHomePosition + * @details Outputs: bpRotorRevStartTime, bloodPumpRotorSpeedRPM + * @return none + *************************************************************************/ +void signalBloodPumpRotorHallSensor( void ) +{ + U32 rotTime = getMSTimerCount(); + U32 deltaTime = calcTimeBetween( bpRotorRevStartTime, rotTime ); + + // increment rotor counter + bloodPumpRotorCounter++; + + // calculate rotor speed (in RPM) + bloodPumpRotorSpeedRPM.data = ( 1.0 / (F32)deltaTime ) * (F32)MS_PER_SECOND * (F32)SEC_PER_MIN; + bpRotorRevStartTime = rotTime; + + // if we're supposed to stop pump at home position, stop pump now. + if ( TRUE == bpStopAtHomePosition ) + { + signalBloodPumpHardStop(); + bpStopAtHomePosition = FALSE; + } +} + +/*********************************************************************//** + * @brief + * The homeBloodPump function initiates a blood pump home operation. + * @details Inputs: bloodPumpState + * @details Outputs: bpStopAtHomePosition, bpHomeStartTime, blood pump started (slow) + * @return none + *************************************************************************/ +BOOL homeBloodPump( void ) +{ + BOOL result = FALSE; + + if ( BLOOD_PUMP_OFF_STATE == bloodPumpState ) + { + bpStopAtHomePosition = TRUE; + bpHomeStartTime = getMSTimerCount(); + result = setBloodPumpTargetFlowRate( BP_HOME_RATE, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); + } + + return result; +} + +/*********************************************************************//** + * @brief + * The getBloodPumpMotorCount function returns the current count for the + * blood pump motor revolution counter. + * @details Inputs: bloodPumpMotorCount + * @details Outputs: none + * @return bloodPumpMotorCount + *************************************************************************/ +U32 getBloodPumpMotorCount( void ) +{ + return bloodPumpMotorEdgeCount / BP_HALL_EDGE_COUNTS_PER_REV; +} + +/*********************************************************************//** + * @brief + * The getBloodPumpRotorCount function returns the current count for the + * blood pump rotor revolution counter. + * @details Inputs: bloodPumpRotorCounter + * @details Outputs: none + * @return bloodPumpRotorCounter + *************************************************************************/ +U32 getBloodPumpRotorCount( void ) +{ + return bloodPumpRotorCounter; +} + +/*********************************************************************//** + * @brief + * The isBloodPumpRunning function returns whether the blood pump is currently + * running or not. + * @details Inputs: isBloodPumpOn + * @details Outputs: none + * @return isBloodPumpOn + *************************************************************************/ +BOOL isBloodPumpRunning( void ) +{ + return isBloodPumpOn; +} + +/*********************************************************************//** + * @brief * The execBloodFlowMonitor function executes the blood flow monitor. - * @details - * Inputs : none - * Outputs : measuredBloodFlowRate, adcBloodPumpMCSpeedRPM, adcBloodPumpMCCurrentmA - * @param none + * @details Inputs: none + * @details Outputs: measuredBloodFlowRate, adcBloodPumpMCSpeedRPM, adcBloodPumpMCCurrentmA * @return none *************************************************************************/ void execBloodFlowMonitor( void ) -{ +{ + HD_OP_MODE_T opMode = getCurrentOperationMode(); U16 bpRPM = getIntADCReading( INT_ADC_BLOOD_PUMP_SPEED ); U16 bpmA = getIntADCReading( INT_ADC_BLOOD_PUMP_MOTOR_CURRENT ); - F32 bpFlow = getFPGABloodFlow(); + F32 bpFlow = ( ( getFPGABloodFlow() * -1.0 ) * bloodFlowCalGain ) + bloodFlowCalOffset; // blood flow sensor installed backwards on HD adcBloodPumpMCSpeedRPM.data = (F32)(SIGN_FROM_12_BIT_VALUE(bpRPM)) * BP_SPEED_ADC_TO_RPM_FACTOR; - adcBloodPumpMCCurrentmA.data = (F32)(SIGN_FROM_12_BIT_VALUE(bpmA)) * BP_CURRENT_ADC_TO_MA_FACTOR; + adcBloodPumpMCCurrentmA.data = (F32)(SIGN_FROM_12_BIT_VALUE(bpmA)) * BP_CURRENT_ADC_TO_MA_FACTOR; + bloodFlowSignalStrength.data = getFPGABloodFlowSignalStrength(); filterBloodFlowReadings( bpFlow ); - // don't start enforcing checks until out of init/POST mode - if ( getCurrentOperationMode() != MODE_INIT ) + // calculate blood pump motor speed/direction from hall sensor count + updateBloodPumpSpeedAndDirectionFromHallSensors(); + + // don't start enforcing checks until out of init/POST mode + if ( opMode != MODE_INIT ) { + // check pump direction checkBloodPumpDirection(); + // check pump controller current checkBloodPumpMCCurrent(); - } + // check pump speeds and flow + checkBloodPumpSpeeds(); + checkBloodPumpFlowAgainstSpeed(); + // check for home position, zero/low speed + checkBloodPumpRotor(); + // check flow sensor signal strength + checkBloodFlowSensorSignalStrength(); + } // publish blood flow data on interval publishBloodFlowData(); } -/************************************************************************* - * @brief execBloodFlowController +/*********************************************************************//** + * @brief * The execBloodFlowController function executes the blood flow controller. - * @details - * Inputs : bloodPumpState - * Outputs : bloodPumpState - * @param none + * @details Inputs: bloodPumpState + * @details Outputs: bloodPumpState * @return none *************************************************************************/ void execBloodFlowController( void ) @@ -318,30 +491,28 @@ break; default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_BLOOD_FLOW_INVALID_BLOOD_PUMP_STATE, bloodPumpState ) + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_BLOOD_FLOW_INVALID_BLOOD_PUMP_STATE, bloodPumpState ) break; } } -/************************************************************************* - * @brief handleBloodPumpOffState - * The handleBloodPumpOffState function handles the blood pump off state \n +/*********************************************************************//** + * @brief + * The handleBloodPumpOffState function handles the blood pump off state * of the blood pump controller state machine. - * @details - * Inputs : targetBloodFlowRate, bloodPumpDirection - * Outputs : bloodPumpPWMDutyCyclePctSet, bloodPumpDirectionSet, isBloodPumpOn - * @param none + * @details Inputs: targetBloodFlowRate, bloodPumpDirection + * @details Outputs: bloodPumpPWMDutyCyclePctSet, bloodPumpDirectionSet, isBloodPumpOn * @return next state *************************************************************************/ static BLOOD_PUMP_STATE_T handleBloodPumpOffState( void ) { BLOOD_PUMP_STATE_T result = BLOOD_PUMP_OFF_STATE; // if we've been given a flow rate, setup ramp up and transition to ramp up state - if ( getTargetBloodFlowRate() != 0 ) + if ( targetBloodFlowRate != 0 ) { // set initial PWM duty cycle - bloodPumpPWMDutyCyclePctSet = BP_PWM_ZERO_OFFSET + MAX_BLOOD_PUMP_PWM_STEP_CHANGE; + bloodPumpPWMDutyCyclePctSet = BP_PWM_ZERO_OFFSET + MAX_BLOOD_PUMP_PWM_STEP_UP_CHANGE; setBloodPumpControlSignalPWM( bloodPumpPWMDutyCyclePctSet ); // allow blood pump to run in requested direction setBloodPumpDirection( bloodPumpDirection ); @@ -353,25 +524,23 @@ return result; } -/************************************************************************* - * @brief handleBloodPumpRampingUpState - * The handleBloodPumpRampingUpState function handles the ramp up state \n +/*********************************************************************//** + * @brief + * The handleBloodPumpRampingUpState function handles the ramp up state * of the blood pump controller state machine. - * @details - * Inputs : bloodPumpPWMDutyCyclePctSet - * Outputs : bloodPumpPWMDutyCyclePctSet - * @param none + * @details Inputs: bloodPumpPWMDutyCyclePctSet + * @details Outputs: bloodPumpPWMDutyCyclePctSet * @return next state *************************************************************************/ static BLOOD_PUMP_STATE_T handleBloodPumpRampingUpState( void ) { BLOOD_PUMP_STATE_T result = BLOOD_PUMP_RAMPING_UP_STATE; // have we been asked to stop the blood pump? - if ( 0 == getTargetBloodFlowRate() ) + if ( 0 == targetBloodFlowRate ) { // start ramp down to stop - bloodPumpPWMDutyCyclePctSet -= MAX_BLOOD_PUMP_PWM_STEP_CHANGE; + bloodPumpPWMDutyCyclePctSet -= MAX_BLOOD_PUMP_PWM_STEP_DN_CHANGE; setBloodPumpControlSignalPWM( bloodPumpPWMDutyCyclePctSet ); result = BLOOD_PUMP_RAMPING_DOWN_STATE; } @@ -392,29 +561,27 @@ // continue ramp up else { - bloodPumpPWMDutyCyclePctSet += MAX_BLOOD_PUMP_PWM_STEP_CHANGE; + bloodPumpPWMDutyCyclePctSet += MAX_BLOOD_PUMP_PWM_STEP_UP_CHANGE; setBloodPumpControlSignalPWM( bloodPumpPWMDutyCyclePctSet ); } return result; } -/************************************************************************* - * @brief handleBloodPumpRampingDownState - * The handleBloodPumpRampingDownState function handles the ramp down state \n +/*********************************************************************//** + * @brief + * The handleBloodPumpRampingDownState function handles the ramp down state * of the blood pump controller state machine. - * @details - * Inputs : bloodPumpPWMDutyCyclePctSet - * Outputs : bloodPumpPWMDutyCyclePctSet - * @param none + * @details Inputs: bloodPumpPWMDutyCyclePctSet + * @details Outputs: bloodPumpPWMDutyCyclePctSet * @return next state *************************************************************************/ static BLOOD_PUMP_STATE_T handleBloodPumpRampingDownState( void ) { BLOOD_PUMP_STATE_T result = BLOOD_PUMP_RAMPING_DOWN_STATE; // have we essentially reached zero speed - if ( bloodPumpPWMDutyCyclePctSet < (MAX_BLOOD_PUMP_PWM_STEP_CHANGE + BP_PWM_ZERO_OFFSET) ) + if ( bloodPumpPWMDutyCyclePctSet < (MAX_BLOOD_PUMP_PWM_STEP_DN_CHANGE + BP_PWM_ZERO_OFFSET) ) { stopBloodPump(); result = BLOOD_PUMP_OFF_STATE; @@ -436,21 +603,19 @@ // continue ramp down else { - bloodPumpPWMDutyCyclePctSet -= MAX_BLOOD_PUMP_PWM_STEP_CHANGE; + bloodPumpPWMDutyCyclePctSet -= MAX_BLOOD_PUMP_PWM_STEP_DN_CHANGE; setBloodPumpControlSignalPWM( bloodPumpPWMDutyCyclePctSet ); } return result; } -/************************************************************************* - * @brief handleBloodPumpControlToTargetState - * The handleBloodPumpControlToTargetState function handles the "control to \n +/*********************************************************************//** + * @brief + * The handleBloodPumpControlToTargetState function handles the "control to * target" state of the blood pump controller state machine. - * @details - * Inputs : none - * Outputs : bloodPumpState - * @param none + * @details Inputs: none + * @details Outputs: bloodPumpState * @return next state *************************************************************************/ static BLOOD_PUMP_STATE_T handleBloodPumpControlToTargetState( void ) @@ -462,7 +627,7 @@ { if ( bloodPumpControlModeSet == PUMP_CONTROL_MODE_CLOSED_LOOP ) { - F32 tgtFlow = (F32)getTargetBloodFlowRate(); + F32 tgtFlow = (F32)targetBloodFlowRate; F32 actFlow = getMeasuredBloodFlowRate(); F32 newPWM; @@ -476,28 +641,25 @@ return result; } -/************************************************************************* - * @brief setBloodPumpControlSignalPWM - * The setBloodPumpControlSignalPWM function sets the PWM duty cycle for \n +/*********************************************************************//** + * @brief + * The setBloodPumpControlSignalPWM function sets the PWM duty cycle for * the blood pump to a given %. - * @details - * Inputs : none - * Outputs : blood pump stop signal activated, PWM duty cycle zeroed - * @param newPWM : new duty cycle % to apply to PWM + * @details Inputs: none + * @details Outputs: blood pump stop signal activated, PWM duty cycle zeroed + * @param newPWM new duty cycle % to apply to PWM * @return none *************************************************************************/ static void setBloodPumpControlSignalPWM( F32 newPWM ) { etpwmSetCmpA( etpwmREG1, (U32)( (S32)( ( newPWM * (F32)(etpwmREG1->TBPRD) ) + FLOAT_TO_INT_ROUNDUP_OFFSET ) ) ); } -/************************************************************************* - * @brief stopBloodPump +/*********************************************************************//** + * @brief * The stopBloodPump function sets the blood pump stop signal. - * @details - * Inputs : none - * Outputs : blood pump stop signal activated, PWM duty cycle zeroed - * @param none + * @details Inputs: none + * @details Outputs: blood pump stop signal activated, PWM duty cycle zeroed * @return none *************************************************************************/ static void stopBloodPump( void ) @@ -508,28 +670,25 @@ SET_BP_STOP(); } -/************************************************************************* - * @brief releaseBloodPumpStop +/*********************************************************************//** + * @brief * The releaseBloodPumpStop function clears the blood pump stop signal. - * @details - * Inputs : none - * Outputs : blood pump stop signal - * @param none + * @details Inputs: none + * @details Outputs: blood pump stop signal * @return none *************************************************************************/ static void releaseBloodPumpStop( void ) { CLR_BP_STOP(); } -/************************************************************************* - * @brief setBloodPumpDirection - * The setBloodPumpDirection function sets the set blood pump direction to \n +/*********************************************************************//** + * @brief + * The setBloodPumpDirection function sets the set blood pump direction to * the given direction. - * @details - * Inputs : bloodPumpState - * Outputs : bloodPumpState - * @param dir : blood pump direction to set + * @details Inputs: bloodPumpState + * @details Outputs: bloodPumpState + * @param dir blood pump direction to set * @return none *************************************************************************/ static void setBloodPumpDirection( MOTOR_DIR_T dir ) @@ -547,276 +706,482 @@ break; default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_BLOOD_FLOW_INVALID_BLOOD_PUMP_DIRECTION, dir ) + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_BLOOD_FLOW_INVALID_BLOOD_PUMP_DIRECTION, dir ) break; } } -/************************************************************************* - * @brief getPublishBloodFlowDataInterval - * The getPublishBloodFlowDataInterval function gets the blood flow data \n +/*********************************************************************//** + * @brief + * The getPublishBloodFlowDataInterval function gets the blood flow data * publication interval. - * @details - * Inputs : bloodFlowDataPublishInterval - * Outputs : none - * @param none - * @return the current blood flow data publication interval (in ms). + * @details Inputs: bloodFlowDataPublishInterval + * @details Outputs: none + * @return the current blood flow data publication interval (in task intervals). *************************************************************************/ -DATA_GET( U32, getPublishBloodFlowDataInterval, bloodFlowDataPublishInterval ) +U32 getPublishBloodFlowDataInterval( void ) +{ + U32 result = bloodFlowDataPublishInterval.data; -/************************************************************************* - * @brief getTargetBloodFlowRate - * The getTargetBloodFlowRate function gets the current target blood flow \n - * rate. - * @details - * Inputs : targetBloodFlowRate - * Outputs : none - * @param none - * @return the current target blood flow rate (in mL/min). - *************************************************************************/ -DATA_GET( S32, getTargetBloodFlowRate, targetBloodFlowRate ) + if ( OVERRIDE_KEY == bloodFlowDataPublishInterval.override ) + { + result = bloodFlowDataPublishInterval.ovData; + } -/************************************************************************* - * @brief getMeasuredBloodFlowRate - * The getMeasuredBloodFlowRate function gets the measured blood flow \n + return result; +} + +/*********************************************************************//** + * @brief + * The getMeasuredBloodFlowRate function gets the measured blood flow * rate. - * @details - * Inputs : measuredBloodFlowRate - * Outputs : none - * @param none + * @details Inputs: measuredBloodFlowRate + * @details Outputs: none * @return the current blood flow rate (in mL/min). *************************************************************************/ -DATA_GET( F32, getMeasuredBloodFlowRate, measuredBloodFlowRate ) +F32 getMeasuredBloodFlowRate( void ) +{ + F32 result = measuredBloodFlowRate.data; -/************************************************************************* - * @brief getMeasuredBloodPumpRotorSpeed - * The getMeasuredBloodPumpRotorSpeed function gets the measured blood flow \n + if ( OVERRIDE_KEY == measuredBloodFlowRate.override ) + { + result = measuredBloodFlowRate.ovData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The getMeasuredBloodFlowSignalStrength function gets the measured blood flow + * signal strength. + * @details Inputs: bloodFlowSignalStrength + * @details Outputs: none + * @return the current blood flow signal strength (in %). + *************************************************************************/ +F32 getMeasuredBloodFlowSignalStrength( void ) +{ + F32 result = bloodFlowSignalStrength.data; + + if ( OVERRIDE_KEY == bloodFlowSignalStrength.override ) + { + result = bloodFlowSignalStrength.ovData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The getMeasuredBloodPumpRotorSpeed function gets the measured blood flow * rate. - * @details - * Inputs : bloodPumpRotorSpeedRPM - * Outputs : none - * @param none + * @details Inputs: bloodPumpRotorSpeedRPM + * @details Outputs: none * @return the current blood flow rate (in mL/min). *************************************************************************/ -DATA_GET( F32, getMeasuredBloodPumpRotorSpeed, bloodPumpRotorSpeedRPM ) +F32 getMeasuredBloodPumpRotorSpeed( void ) +{ + F32 result = bloodPumpRotorSpeedRPM.data; -/************************************************************************* - * @brief getMeasuredBloodPumpSpeed - * The getMeasuredBloodPumpSpeed function gets the measured blood flow \n + if ( OVERRIDE_KEY == bloodPumpRotorSpeedRPM.override ) + { + result = bloodPumpRotorSpeedRPM.ovData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The getMeasuredBloodPumpSpeed function gets the measured blood flow * rate. - * @details - * Inputs : bloodPumpSpeedRPM - * Outputs : none - * @param none + * @details Inputs: bloodPumpSpeedRPM + * @details Outputs: none * @return the current blood flow rate (in mL/min). *************************************************************************/ -DATA_GET( F32, getMeasuredBloodPumpSpeed, bloodPumpSpeedRPM ) +F32 getMeasuredBloodPumpSpeed( void ) +{ + F32 result = bloodPumpSpeedRPM.data; -/************************************************************************* - * @brief getMeasuredBloodPumpMCSpeed - * The getMeasuredBloodPumpMCSpeed function gets the measured blood pump \n + if ( OVERRIDE_KEY == bloodPumpSpeedRPM.override ) + { + result = bloodPumpSpeedRPM.ovData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The getMeasuredBloodPumpMCSpeed function gets the measured blood pump * speed. - * @details - * Inputs : adcBloodPumpMCSpeedRPM - * Outputs : none - * @param none + * @details Inputs: adcBloodPumpMCSpeedRPM + * @details Outputs: none * @return the current blood pump speed (in RPM). *************************************************************************/ -DATA_GET( F32, getMeasuredBloodPumpMCSpeed, adcBloodPumpMCSpeedRPM ) +F32 getMeasuredBloodPumpMCSpeed( void ) +{ + F32 result = adcBloodPumpMCSpeedRPM.data; -/************************************************************************* - * @brief getMeasuredBloodPumpMCCurrent - * The getMeasuredBloodPumpMCCurrent function gets the measured blood pump \n + if ( OVERRIDE_KEY == adcBloodPumpMCSpeedRPM.override ) + { + result = adcBloodPumpMCSpeedRPM.ovData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The getMeasuredBloodPumpMCCurrent function gets the measured blood pump * current. - * @details - * Inputs : adcBloodPumpMCCurrentmA - * Outputs : none - * @param none + * @details Inputs: adcBloodPumpMCCurrentmA + * @details Outputs: none * @return the current blood pump current (in mA). *************************************************************************/ -DATA_GET( F32, getMeasuredBloodPumpMCCurrent, adcBloodPumpMCCurrentmA ) +F32 getMeasuredBloodPumpMCCurrent( void ) +{ + F32 result = adcBloodPumpMCCurrentmA.data; -/************************************************************************* - * @brief publishBloodFlowData - * The publishBloodFlowData function publishes blood flow data at the set \n + if ( OVERRIDE_KEY == adcBloodPumpMCCurrentmA.override ) + { + result = adcBloodPumpMCCurrentmA.ovData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The publishBloodFlowData function publishes blood flow data at the set * interval. - * @details - * Inputs : target flow rate, measured flow rate, measured MC speed, \n + * @details Inputs: target flow rate, measured flow rate, measured MC speed, * measured MC current - * Outputs : Blood flow data is published to CAN bus. - * @param none + * @details Outputs: Blood flow data is published to CAN bus. * @return none *************************************************************************/ static void publishBloodFlowData( void ) { - // publish blood flow data on interval + // publish blood flow data on interval if ( ++bloodFlowDataPublicationTimerCounter >= getPublishBloodFlowDataInterval() ) - { - S32 flowStPt = (S32)getTargetBloodFlowRate(); -#ifndef SHOW_RAW_FLOW_VALUES - F32 measFlow = getMeasuredBloodFlowRate(); -#else - F32 measFlow = getFPGABloodFlow(); -#endif - F32 measRotSpd = getMeasuredBloodPumpRotorSpeed(); - F32 measSpd = getMeasuredBloodPumpSpeed(); - F32 measMCSpd = getMeasuredBloodPumpMCSpeed(); - F32 measMCCurr = getMeasuredBloodPumpMCCurrent(); - F32 pumpPWMPctDutyCycle = bloodPumpPWMDutyCyclePctSet * FRACTION_TO_PERCENT_FACTOR; -//#ifdef DEBUG_ENABLED -// // TODO - temporary debug code - remove later -// char debugFlowStr[ 256 ]; -// -// sprintf( debugFlowStr, "Blood Set Pt.:%5d, Meas. Flow:%5d, Speed:%5d RPM, Current:%5d mA, PWM:%5d \n", flowStPt, (S32)measFlow, (S32)measMCSpd, (S32)measMCCurr, (S32)pumpPWMPctDutyCycle ); -// sendDebugData( (U08*)debugFlowStr, strlen(debugFlowStr) ); -//#endif - broadcastBloodFlowData( flowStPt, measFlow, measRotSpd, measSpd, measMCSpd, measMCCurr, pumpPWMPctDutyCycle ); + { + BLOOD_PUMP_STATUS_PAYLOAD_T payload; + + payload.setPoint = targetBloodFlowRate; + payload.measFlow = getMeasuredBloodFlowRate(); + payload.measRotorSpd = getMeasuredBloodPumpRotorSpeed(); + payload.measPumpSpd = getMeasuredBloodPumpSpeed(); + payload.measMCSpd = getMeasuredBloodPumpMCSpeed(); + payload.measMCCurr = getMeasuredBloodPumpMCCurrent(); + payload.pwmDC = bloodPumpPWMDutyCyclePctSet * FRACTION_TO_PERCENT_FACTOR; + payload.flowSigStrength = getMeasuredBloodFlowSignalStrength() * FRACTION_TO_PERCENT_FACTOR; + broadcastBloodFlowData( &payload ); bloodFlowDataPublicationTimerCounter = 0; } } -/************************************************************************* - * @brief resetBloodFlowMovingAverage - * The resetBloodFlowMovingAverage function re-sizes and re-initializes the \n - * blood flow moving average sample buffer. - * @details - * Inputs : none - * Outputs : flowReadingsTotal, flowReadingsIdx, flowReadingsCount all set to zero. - * @param initFlow : the new blood flow set pt. - * @param flowDir : the new set direction +/*********************************************************************//** + * @brief + * The resetBloodFlowMovingAverage function re-initializes the blood flow + * moving average sample buffer. + * @details Inputs: none + * @details Outputs: flowReadingsTotal, flowReadingsIdx, flowReadingsCount all set to zero. * @return none *************************************************************************/ static void resetBloodFlowMovingAverage( void ) { - flowReadingsTotal = 0.0; flowReadingsIdx = 0; flowReadingsCount = 0; - flowReadingsTmrCtr = 0; + flowReadingsTotal = 0.0; bpControlTimerCounter = 0; } -/************************************************************************* - * @brief filterBloodFlowReadings - * The filterBloodFlowReadings function adds a new flow sample to the filter \n - * if decimation rate for current set point calls for it. - * @details - * Inputs : none - * Outputs : flowReadings[], flowReadingsIdx, flowReadingsCount - * @param flow : newest blood flow sample +/*********************************************************************//** + * @brief + * The filterBloodFlowReadings function adds a new flow sample to the filter. + * @details Inputs: none + * @details Outputs: flowReadings[], flowReadingsIdx, flowReadingsCount, flowReadingsTotal + * @param flow newest blood flow sample * @return none *************************************************************************/ static void filterBloodFlowReadings( F32 flow ) { - BOOL addSampleToFilter = FALSE; - - if ( ( targetBloodFlowRate.data < MIN_BLOOD_FLOW_RATE ) || ( targetBloodFlowRate.data >= MAX_BLOOD_FLOW_RATE ) ) +#ifndef RAW_FLOW_SENSOR_DATA + if ( flowReadingsCount >= SIZE_OF_ROLLING_AVG ) { - addSampleToFilter = TRUE; + flowReadingsTotal -= flowReadings[ flowReadingsIdx ]; } - else + flowReadings[ flowReadingsIdx ] = flow; + flowReadingsTotal += flow; + flowReadingsIdx = INC_WRAP( flowReadingsIdx, 0, SIZE_OF_ROLLING_AVG - 1 ); + flowReadingsCount = INC_CAP( flowReadingsCount, SIZE_OF_ROLLING_AVG ); + measuredBloodFlowRate.data = flowReadingsTotal / (F32)flowReadingsCount; +#else + measuredBloodFlowRate.data = flow; +#endif +} + +/*********************************************************************//** + * @brief + * The updateBloodPumpSpeedAndDirectionFromHallSensors function calculates + * the blood pump motor speed and direction from hall sensor counter on + * a 1 second interval. + * @details Inputs: bpLastMotorHallSensorCount, bpMotorSpeedCalcTimerCtr, current count from FPGA + * @details Outputs: bpMotorDirectionFromHallSensors, bloodPumpSpeedRPM + * @return none + *************************************************************************/ +static void updateBloodPumpSpeedAndDirectionFromHallSensors( void ) +{ + if ( ++bpMotorSpeedCalcTimerCtr >= BP_SPEED_CALC_INTERVAL ) { - switch ( flowReadingsTmrCtr ) + U16 bpMotorHallSensorCount = getFPGABloodPumpHallSensorCount(); + U32 nextIdx = INC_WRAP( bpMotorSpeedCalcIdx, 0, BP_SPEED_CALC_BUFFER_LEN - 1 ); + U16 incDelta = ( bpMotorHallSensorCount >= bpLastMotorHallSensorCounts[ nextIdx ] ? \ + bpMotorHallSensorCount - bpLastMotorHallSensorCounts[ nextIdx ] : \ + ( HEX_64_K - bpLastMotorHallSensorCounts[ nextIdx ] ) + bpMotorHallSensorCount ); + U16 decDelta = HEX_64_K - incDelta; + U16 delta; + + // determine blood pump speed/direction from delta hall sensor count since last interval + if ( incDelta < decDelta ) { - case 0: - addSampleToFilter = TRUE; - break; + delta = incDelta; + bloodPumpSpeedRPM.data = ( (F32)delta / (F32)BP_HALL_EDGE_COUNTS_PER_REV ) * (F32)SEC_PER_MIN; + } + else + { + delta = decDelta; + bloodPumpSpeedRPM.data = ( (F32)delta / (F32)BP_HALL_EDGE_COUNTS_PER_REV ) * (F32)SEC_PER_MIN * -1.0; + } + bloodPumpMotorEdgeCount += delta; - case 1: - addSampleToFilter = FALSE; - break; + // update last count for next time + bpLastMotorHallSensorCounts[ nextIdx ] = bpMotorHallSensorCount; + bpMotorSpeedCalcIdx = nextIdx; + bpMotorSpeedCalcTimerCtr = 0; + } +} - case 2: - if ( targetBloodFlowRate.data >= 400 ) - { - addSampleToFilter = TRUE; - } - break; +/*********************************************************************//** + * @brief + * The checkBloodPumpRotor function checks the rotor for the blood + * pump. If homing, this function will stop when hall sensor detected. If pump + * is off or running very slowly, rotor speed will be set to zero. + * @details Inputs: bpStopAtHomePosition, bpHomeStartTime, bpRotorRevStartTime + * @details Outputs: pump may be stopped if homing, bloodPumpRotorSpeedRPM may be set to zero. + * @return none + *************************************************************************/ +static void checkBloodPumpRotor( void ) +{ + F32 rotorSpeed = getMeasuredBloodPumpRotorSpeed(); - case 3: - if ( targetBloodFlowRate.data >= 200 ) - { - addSampleToFilter = TRUE; - } - break; - - case 4: - if ( targetBloodFlowRate.data >= 300 ) - { - addSampleToFilter = TRUE; - } - break; - - default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_BLOOD_FLOW_INVALID_FILTER_STATE, flowReadingsTmrCtr ) - break; - } + // if homing, check timeout + if ( ( TRUE == bpStopAtHomePosition ) && ( TRUE == didTimeout( bpHomeStartTime, BP_HOME_TIMEOUT_MS ) ) ) + { + signalBloodPumpHardStop(); + bpStopAtHomePosition = FALSE; + // TODO - alarm??? } - if ( TRUE == addSampleToFilter ) + // ensure rotor speed below maximum + if ( rotorSpeed > BP_MAX_ROTOR_SPEED_RPM ) { - if ( flowReadingsCount >= SIZE_OF_ROLLING_AVG ) - { - flowReadingsTotal -= flowReadings[ flowReadingsIdx ]; + if ( ++errorBloodPumpRotorTooFastPersistTimerCtr >= BP_MAX_ROTOR_SPEED_ERROR_PERSIST ) + { + SET_ALARM_WITH_1_F32_DATA( ALARM_ID_BLOOD_PUMP_ROTOR_SPEED_TOO_HIGH, rotorSpeed ) } - flowReadings[ flowReadingsIdx ] = flow; - flowReadingsTotal += flow; - flowReadingsIdx = INC_WRAP( flowReadingsIdx, 0, SIZE_OF_ROLLING_AVG - 1 ); - flowReadingsCount = INC_CAP( flowReadingsCount, SIZE_OF_ROLLING_AVG ); - measuredBloodFlowRate.data = flowReadingsTotal / (F32)flowReadingsCount; + } + else + { + errorBloodPumpRotorTooFastPersistTimerCtr = 0; } - flowReadingsTmrCtr = INC_WRAP( flowReadingsTmrCtr, 0, MAX_FLOW_FILTER_INTERVAL - 1 ); -#ifdef DEBUG_ENABLED + // if pump is stopped or running very slowly, set rotor speed to zero + if ( TRUE == didTimeout( bpRotorRevStartTime, BP_HOME_TIMEOUT_MS ) ) { - // TODO - temporary debug code - remove later -// char debugFlowStr[ 40 ]; - //S32 num = (S32)(flow); - //S32 dec = (S32)(fabs(flow-(S32)(flow))*100.0); -// S32 numf = (S32)(measuredBloodFlowRate.data); -// S32 decf = (S32)(fabs(measuredBloodFlowRate.data-(S32)(measuredBloodFlowRate.data))*100.0); -// S32 nump = (S32)bloodPumpPWMDutyCyclePctSet; -// S32 decp = (S32)((bloodPumpPWMDutyCyclePctSet-(S32)bloodPumpPWMDutyCyclePctSet)*100.0); - -// sprintf( debugFlowStr, "%5d.%02d %5d.%02d\n", numf, decf, numf, decf ); -// sprintf( debugFlowStr, "%5d.%02d %5d.%02d\n", num, dec, numf, decf ); -// sendDebugData( (U08*)debugFlowStr, strlen(debugFlowStr) ); + bloodPumpRotorSpeedRPM.data = 0.0; } -#endif } -/************************************************************************* - * @brief checkBloodPumpDirection - * The checkBloodPumpDirection function checks the set direction vs. \n +/*********************************************************************//** + * @brief + * The checkBloodPumpDirection function checks the set direction vs. * the direction implied by the sign of the measured MC speed. - * @details - * Inputs : - * Outputs : - * @param none + * @details Inputs: + * @details Outputs: * @return none *************************************************************************/ static void checkBloodPumpDirection( void ) { - MOTOR_DIR_T bpMCDir; + if ( BLOOD_PUMP_CONTROL_TO_TARGET_STATE == bloodPumpState ) + { + MOTOR_DIR_T bpMCDir, bpDir; + + bpMCDir = ( getMeasuredBloodPumpMCSpeed() >= 0.0 ? MOTOR_DIR_FORWARD : MOTOR_DIR_REVERSE ); + bpDir = ( getMeasuredBloodPumpSpeed() >= 0.0 ? MOTOR_DIR_FORWARD : MOTOR_DIR_REVERSE ); + // check set direction vs. direction from hall sensors + if ( bloodPumpDirectionSet != bpDir ) + { + if ( ++errorBloodPumpDirectionPersistTimerCtr >= BP_DIRECTION_ERROR_PERSIST ) + { +#ifndef DISABLE_PUMP_DIRECTION_CHECKS + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_BLOOD_PUMP_MC_DIRECTION_CHECK, (U32)bloodPumpDirectionSet, (U32)bpDir ) +#endif + } + } + // check set direction vs. direction from sign of motor controller speed + else if ( bloodPumpDirectionSet != bpMCDir ) + { + if ( ++errorBloodPumpDirectionPersistTimerCtr >= BP_DIRECTION_ERROR_PERSIST ) + { +#ifndef DISABLE_PUMP_DIRECTION_CHECKS + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_BLOOD_PUMP_MC_DIRECTION_CHECK, (U32)bloodPumpDirectionSet, (U32)bpMCDir ) +#endif + } + } + else + { + errorBloodPumpDirectionPersistTimerCtr = 0; + } + } + else + { + errorBloodPumpDirectionPersistTimerCtr = 0; + } +} + +/*********************************************************************//** + * @brief + * The checkBloodPumpSpeeds function checks several aspects of the blood pump + * speed. + * 1. while pump is commanded off, measured motor speed should be < limit. + * 2. while pump is controlling, measured motor speed should be within allowed range of commanded speed. + * 3. measured motor speed should be within allowed range of measured rotor speed. + * All 3 checks have a persistence time that must be met before an alarm is triggered. + * @details Inputs: targetBloodFlowRate, bloodPumpSpeedRPM, bloodPumpRotorSpeedRPM + * @details Outputs: alarm(s) may be triggered + * @return none + *************************************************************************/ +static void checkBloodPumpSpeeds( void ) +{ + F32 measMotorSpeed = fabs( getMeasuredBloodPumpSpeed() ); + S32 cmdRate = targetBloodFlowRate; + + // check for pump running while commanded off + if ( 0 == cmdRate ) + { + if ( measMotorSpeed > BP_MAX_MOTOR_SPEED_WHILE_OFF_RPM ) + { + if ( ++errorBloodMotorOffPersistTimerCtr >= BP_OFF_ERROR_PERSIST ) + { +#ifndef DISABLE_PUMP_SPEED_CHECKS + SET_ALARM_WITH_1_F32_DATA( ALARM_ID_BLOOD_PUMP_OFF_CHECK, measMotorSpeed ); + activateSafetyShutdown(); +#endif + } + } + else + { + errorBloodMotorOffPersistTimerCtr = 0; + } + } + else + { + errorBloodMotorOffPersistTimerCtr = 0; + } + if ( BLOOD_PUMP_CONTROL_TO_TARGET_STATE == bloodPumpState ) { - // check set direction vs. direction from sign of motor controller speed - bpMCDir = ( getMeasuredBloodPumpMCSpeed() >= 0.0 ? MOTOR_DIR_FORWARD : MOTOR_DIR_REVERSE ); - if ( bloodPumpDirectionSet != bpMCDir ) + F32 cmdMotorSpeed = ( (F32)cmdRate / (F32)ML_PER_LITER ) * BP_REV_PER_LITER * BP_GEAR_RATIO; + F32 deltaMotorSpeed = fabs( measMotorSpeed - cmdMotorSpeed ); + F32 measRotorSpeed = getMeasuredBloodPumpRotorSpeed(); + F32 measMotorSpeedInRotorRPM = measMotorSpeed / BP_GEAR_RATIO; + F32 deltaRotorSpeed = fabs( measRotorSpeed - measMotorSpeedInRotorRPM ); + + // check measured motor speed vs. commanded motor speed while controlling to target + if ( deltaMotorSpeed > BP_MAX_MOTOR_SPEED_ERROR_RPM ) { - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_BLOOD_PUMP_MC_DIRECTION_CHECK, (U32)bloodPumpDirectionSet, (U32)bpMCDir ) + if ( ++errorBloodMotorSpeedPersistTimerCtr >= BP_MOTOR_SPEED_ERROR_PERSIST ) + { +#ifndef DISABLE_PUMP_SPEED_CHECKS + SET_ALARM_WITH_2_F32_DATA( ALARM_ID_BLOOD_PUMP_MOTOR_SPEED_CHECK, (F32)cmdRate, measMotorSpeed ); +#endif + } } + else + { + errorBloodMotorSpeedPersistTimerCtr = 0; + } + + // check measured rotor speed vs. measured motor speed while controlling to target + if ( deltaRotorSpeed > BP_MAX_ROTOR_VS_MOTOR_DIFF_RPM ) + { + if ( ++errorBloodRotorSpeedPersistTimerCtr >= BP_ROTOR_SPEED_ERROR_PERSIST ) + { +#ifndef DISABLE_PUMP_SPEED_CHECKS + SET_ALARM_WITH_2_F32_DATA( ALARM_ID_BLOOD_PUMP_ROTOR_SPEED_CHECK, measRotorSpeed, measMotorSpeed ); +#endif + } + } + else + { + errorBloodRotorSpeedPersistTimerCtr = 0; + } } + else + { + errorBloodMotorSpeedPersistTimerCtr = 0; + errorBloodRotorSpeedPersistTimerCtr = 0; + } } -/************************************************************************* - * @brief checkBloodPumpMCCurrent - * The checkBloodPumpMCCurrent function checks the measured MC current vs. \n +/*********************************************************************//** + * @brief + * The checkBloodPumpFlowAgainstSpeed function checks the measured blood flow + * against the implied flow of the measured pump speed when in treatment mode + * and controlling to target flow. If a sufficient difference persists, a + * flow vs. motor speed check error is triggered. + * @details Inputs: measuredBloodFlowRate, bloodPumpSpeedRPM, errorBloodFlowVsMotorSpeedPersistTimerCtr + * @details Outputs: alarm may be triggered + * @return none + *************************************************************************/ +static void checkBloodPumpFlowAgainstSpeed( void ) +{ + // check only performed while in treatment mode and while we're in control to target state + if ( ( MODE_TREA == getCurrentOperationMode() ) && ( BLOOD_PUMP_CONTROL_TO_TARGET_STATE == bloodPumpState ) ) + { + F32 flow = getMeasuredBloodFlowRate(); + F32 speed = getMeasuredBloodPumpSpeed(); + F32 impliedSpeed = ( flow / (F32)ML_PER_LITER ) * BP_REV_PER_LITER * BP_GEAR_RATIO; + F32 delta = fabs( speed - impliedSpeed ); + + if ( delta > BP_MAX_FLOW_VS_SPEED_DIFF_RPM ) + { + if ( ++errorBloodFlowVsMotorSpeedPersistTimerCtr >= BP_FLOW_VS_SPEED_PERSIST ) + { +#ifndef DISABLE_PUMP_FLOW_CHECKS + SET_ALARM_WITH_2_F32_DATA( ALARM_ID_BLOOD_PUMP_FLOW_VS_MOTOR_SPEED_CHECK, flow, speed ); +#endif + } + } + else + { + errorBloodFlowVsMotorSpeedPersistTimerCtr = 0; + } + } + else + { + errorBloodFlowVsMotorSpeedPersistTimerCtr = 0; + } +} + +/*********************************************************************//** + * @brief + * The checkBloodPumpMCCurrent function checks the measured MC current vs. * the set state of the blood pump (stopped or running). - * @details - * Inputs : - * Outputs : - * @param none + * @details Inputs: + * @details Outputs: * @return none *************************************************************************/ static void checkBloodPumpMCCurrent( void ) @@ -832,7 +1197,7 @@ bpCurrErrorDurationCtr += TASK_PRIORITY_INTERVAL; if ( bpCurrErrorDurationCtr > BP_MAX_CURR_ERROR_DURATION_MS ) { -#ifndef DISABLE_MOTOR_CURRENT_ERRORS +#ifndef DISABLE_MOTOR_CURRENT_CHECKS SET_ALARM_WITH_1_F32_DATA( ALARM_ID_BLOOD_PUMP_MC_CURRENT_CHECK, getMeasuredBloodPumpMCCurrent() ); #endif } @@ -851,7 +1216,7 @@ bpCurrErrorDurationCtr += TASK_PRIORITY_INTERVAL; if ( bpCurrErrorDurationCtr > BP_MAX_CURR_ERROR_DURATION_MS ) { -#ifndef DISABLE_MOTOR_CURRENT_ERRORS +#ifndef DISABLE_MOTOR_CURRENT_CHECKS SET_ALARM_WITH_1_F32_DATA( ALARM_ID_BLOOD_PUMP_MC_CURRENT_CHECK, getMeasuredBloodPumpMCCurrent() ); #endif } @@ -861,24 +1226,69 @@ bpCurrErrorDurationCtr = 0; } } +} + +/*********************************************************************//** + * @brief + * The checkBloodFlowSensorSignalStrength function checks the measured blood + * flow sensor signal strength is sufficient for accurate flow sensing. + * @details Inputs: + * @details Outputs: + * @return none + *************************************************************************/ +static void checkBloodFlowSensorSignalStrength( void ) +{ +#ifndef DISABLE_PUMP_FLOW_CHECKS + HD_OP_MODE_T opMode = getCurrentOperationMode(); + + // check flow sensor signal strength when appropriate TODO - in pre-treatment, must be far enough along for fluid to be in tubing + if ( MODE_TREA == opMode || ( MODE_PRET == opMode && FALSE ) ) + { + F32 sigStrength = getMeasuredBloodFlowSignalStrength(); + BOOL outOfRange = ( sigStrength < MIN_FLOW_SIG_STRENGTH ? TRUE : FALSE ); + + checkPersistentAlarm( PERSISTENT_ALARM_BLOOD_FLOW_SIGNAL_STRENGTH, outOfRange, sigStrength, MIN_FLOW_SIG_STRENGTH ); + } +#endif } -/************************************************************************* - * @brief execBloodFlowTest - * The execBloodFlowTest function executes the state machine for the \n - * BloodFlow self test. - * @details - * Inputs : none - * Outputs : none - * @param none - * @return the current state of the BloodFlow self test. +/*********************************************************************//** + * @brief + * The execBloodFlowTest function executes the state machine for the + * BloodFlow self-test. + * @details Inputs: none + * @details Outputs: none + * @return the current state of the BloodFlow self-test. *************************************************************************/ SELF_TEST_STATUS_T execBloodFlowTest( void ) { SELF_TEST_STATUS_T result = SELF_TEST_STATUS_FAILED; + CALIBRATION_DATA_T cal; + + switch ( bloodPumpSelfTestState ) + { + case BLOOD_FLOW_SELF_TEST_STATE_START: + // retrieve blood flow sensor calibration data + if ( TRUE == getCalibrationData( &cal ) ) + { + bloodFlowCalGain = cal.bloodFlowGain; + bloodFlowCalOffset = cal.bloodFlowOffset_mL_min; + bloodPumpSelfTestState = BLOOD_FLOW_TEST_STATE_COMPLETE; // TODO - implement rest of self-test(s) + result = SELF_TEST_STATUS_PASSED; + } + break; + + case BLOOD_FLOW_TEST_STATE_IN_PROGRESS: + break; + + case BLOOD_FLOW_TEST_STATE_COMPLETE: + break; + + default: + // TODO - s/w fault + break; + } - // TODO - implement self test(s) - return result; } @@ -888,14 +1298,63 @@ *************************************************************************/ -/************************************************************************* - * @brief testSetBloodFlowDataPublishIntervalOverride - * The testSetBloodFlowDataPublishIntervalOverride function overrides the \n +/*********************************************************************//** + * @brief + * The setBloodFlowCalibration function sets the blood flow calibration + * factors and has them stored in non-volatile memory. + * @details Inputs: none + * @details Outputs: bloodFlowCalGain, bloodFlowCalOffset + * @param gain gain calibration factor for blood flow sensor + * @param offset offset calibration factor for blood flow sensor + * @return TRUE if calibration factors successfully set/stored, FALSE if not + *************************************************************************/ +BOOL setBloodFlowCalibration( F32 gain, F32 offset ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + CALIBRATION_DATA_T cal; + + getCalibrationData( &cal ); + // keep locally and apply immediately + bloodFlowCalGain = gain; + bloodFlowCalOffset = offset; + // also update calibration record in non-volatile memory + cal.bloodFlowGain = gain; + cal.bloodFlowOffset_mL_min = offset; + if ( TRUE == setCalibrationData( cal ) ) + { + result = TRUE; + } + } + + return result; +} + +/*********************************************************************//** + * @brief + * The getBloodFlowCalibration function retrieves the current blood flow + * calibration factors. + * @details Inputs: bloodFlowCalGain, bloodFlowCalOffset + * @details Outputs: none + * @param gain value to populate with gain calibration factor for blood flow sensor + * @param offset value to populate with offset calibration factor for blood flow sensor + * @return none + *************************************************************************/ +void getBloodFlowCalibration( F32 *gain, F32 *offset ) +{ + *gain = bloodFlowCalGain; + *offset = bloodFlowCalOffset; +} + +/*********************************************************************//** + * @brief + * The testSetBloodFlowDataPublishIntervalOverride function overrides the * blood flow data publish interval. - * @details - * Inputs : none - * Outputs : bloodFlowDataPublishInterval - * @param value : override blood flow data publish interval with (in ms) + * @details Inputs: none + * @details Outputs: bloodFlowDataPublishInterval + * @param value override blood flow data publish interval with (in ms) * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetBloodFlowDataPublishIntervalOverride( U32 value ) @@ -914,13 +1373,12 @@ return result; } -/************************************************************************* - * @brief testResetBloodFlowDataPublishIntervalOverride - * The testResetBloodFlowDataPublishIntervalOverride function resets the override \n +/*********************************************************************//** + * @brief + * The testResetBloodFlowDataPublishIntervalOverride function resets the override * of the blood flow data publish interval. - * @details - * Inputs : none - * Outputs : bloodFlowDataPublishInterval + * @details Inputs: none + * @details Outputs: bloodFlowDataPublishInterval * @return TRUE if override reset successful, FALSE if not *************************************************************************/ BOOL testResetBloodFlowDataPublishIntervalOverride( void ) @@ -937,17 +1395,17 @@ return result; } -/************************************************************************* +/*********************************************************************//** * @brief - * The testSetTargetBloodFlowRateOverride function overrides the target \n - * blood flow rate. \n - * @details - * Inputs : none - * Outputs : targetBloodFlowRate - * @param value : override target blood flow rate (in mL/min) + * The testSetTargetBloodFlowRateOverride function overrides the target + * blood flow rate. + * @details Inputs: none + * @details Outputs: targetBloodFlowRate + * @param value override target blood flow rate (in mL/min) + * @param ctrlMode override pump control mode to this mode (0 = closed loop, 1 = open loop) * @return TRUE if override successful, FALSE if not *************************************************************************/ -BOOL testSetTargetBloodFlowRateOverride( S32 value ) +BOOL testSetTargetBloodFlowRateOverride( S32 value, U32 ctrlMode ) { BOOL result = FALSE; @@ -962,110 +1420,285 @@ else { dir = MOTOR_DIR_FORWARD; + } + if ( ctrlMode < NUM_OF_PUMP_CONTROL_MODES ) + { + targetBloodFlowRate = value; + result = setBloodPumpTargetFlowRate( abs(value), dir, (PUMP_CONTROL_MODE_T)ctrlMode ); } - targetBloodFlowRate.ovInitData = targetBloodFlowRate.data; // backup current target flow rate - targetBloodFlowRate.ovData = value; - targetBloodFlowRate.override = OVERRIDE_KEY; - result = setBloodPumpTargetFlowRate( ABS(value), dir, bloodPumpControlMode ); } return result; } -/************************************************************************* +/*********************************************************************//** * @brief - * The testResetTargetBloodFlowRateOverride function resets the override of the \n - * target blood flow rate. - * @details - * Inputs : none - * Outputs : targetBloodFlowRate - * @param none - * @return TRUE if override reset successful, FALSE if not + * The testResetMeasuredBloodFlowRateOverride function overrides the measured + * blood flow rate. + * @details Inputs: none + * @details Outputs: measuredBloodFlowRate + * @param value override measured blood flow rate (in mL/min) + * @return TRUE if override successful, FALSE if not *************************************************************************/ -BOOL testResetTargetBloodFlowRateOverride( void ) +BOOL testSetMeasuredBloodFlowRateOverride( F32 value ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { - targetBloodFlowRate.data = targetBloodFlowRate.ovInitData; // restore pre-override target flow rate - targetBloodFlowRate.override = OVERRIDE_RESET; - targetBloodFlowRate.ovInitData = 0; - targetBloodFlowRate.ovData = 0; - result = setBloodPumpTargetFlowRate( targetBloodFlowRate.data, bloodPumpDirection, bloodPumpControlMode ); + result = TRUE; + measuredBloodFlowRate.ovData = value; + measuredBloodFlowRate.override = OVERRIDE_KEY; } return result; } -/************************************************************************* - * @brief testSetMeasuredBloodFlowRateOverride and testResetMeasuredBloodFlowRateOverride - * The testResetMeasuredBloodFlowRateOverride function overrides the measured \n - * blood flow rate. \n - * The testResetOffButtonStateOverride function resets the override of the \n +/*********************************************************************//** + * @brief + * The testResetOffButtonStateOverride function resets the override of the * measured blood flow rate. - * @details - * Inputs : none - * Outputs : measuredBloodFlowRate - * @param value : override measured blood flow rate (in mL/min) + * @details Inputs: none + * @details Outputs: measuredBloodFlowRate + * @return TRUE if reset successful, FALSE if not + *************************************************************************/ +BOOL testResetMeasuredBloodFlowRateOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + measuredBloodFlowRate.override = OVERRIDE_RESET; + measuredBloodFlowRate.ovData = measuredBloodFlowRate.ovInitData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testSetMeasuredBloodPumpRotorSpeedOverride function overrides the measured + * blood pump rotor speed. + * @details Inputs: none + * @details Outputs: bloodPumpRotorSpeedRPM + * @param value override measured blood pump rotor speed (in RPM) * @return TRUE if override successful, FALSE if not *************************************************************************/ -DATA_OVERRIDE_FUNC( F32, testSetMeasuredBloodFlowRateOverride, testResetMeasuredBloodFlowRateOverride, measuredBloodFlowRate ) +BOOL testSetMeasuredBloodPumpRotorSpeedOverride( F32 value ) +{ + BOOL result = FALSE; -/************************************************************************* - * @brief testSetMeasuredBloodPumpRotorSpeedOverride and testResetMeasuredBloodPumpRotorSpeedOverride - * The testSetMeasuredBloodPumpRotorSpeedOverride function overrides the measured \n - * blood pump rotor speed. \n - * The testResetMeasuredBloodPumpRotorSpeedOverride function resets the override of the \n + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + bloodPumpRotorSpeedRPM.ovData = value; + bloodPumpRotorSpeedRPM.override = OVERRIDE_KEY; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetMeasuredBloodPumpRotorSpeedOverride function resets the override of the * measured blood pump rotor speed. - * @details - * Inputs : none - * Outputs : bloodPumpRotorSpeedRPM - * @param value : override measured blood pump rotor speed (in RPM) + * @details Inputs: none + * @details Outputs: bloodPumpRotorSpeedRPM + * @return TRUE if reset successful, FALSE if not + *************************************************************************/ +BOOL testResetMeasuredBloodPumpRotorSpeedOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + bloodPumpRotorSpeedRPM.override = OVERRIDE_RESET; + bloodPumpRotorSpeedRPM.ovData = bloodPumpRotorSpeedRPM.ovInitData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testSetMeasuredBloodPumpSpeedOverride function overrides the measured + * blood pump motor speed. + * @details Inputs: none + * @details Outputs: bloodPumpSpeedRPM + * @param value override measured blood pump motor speed (in RPM) * @return TRUE if override successful, FALSE if not *************************************************************************/ -DATA_OVERRIDE_FUNC( F32, testSetMeasuredBloodPumpRotorSpeedOverride, testResetMeasuredBloodPumpRotorSpeedOverride, bloodPumpRotorSpeedRPM ) +BOOL testSetMeasuredBloodPumpSpeedOverride( F32 value ) +{ + BOOL result = FALSE; -/************************************************************************* - * @brief testSetMeasuredBloodPumpSpeedOverride and testResetMeasuredBloodPumpSpeedOverride - * The testSetMeasuredBloodPumpSpeedOverride function overrides the measured \n - * blood pump motor speed. \n - * The testResetMeasuredBloodPumpSpeedOverride function resets the override of the \n + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + bloodPumpSpeedRPM.ovData = value; + bloodPumpSpeedRPM.override = OVERRIDE_KEY; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetMeasuredBloodPumpSpeedOverride function resets the override of the * measured blood pump motor speed. - * @details - * Inputs : none - * Outputs : bloodPumpSpeedRPM - * @param value : override measured blood pump motor speed (in RPM) + * @details Inputs: none + * @details Outputs: bloodPumpSpeedRPM + * @return TRUE if reset successful, FALSE if not + *************************************************************************/ +BOOL testResetMeasuredBloodPumpSpeedOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + bloodPumpSpeedRPM.override = OVERRIDE_RESET; + bloodPumpSpeedRPM.ovData = bloodPumpSpeedRPM.ovInitData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testSetMeasuredBloodPumpMCSpeedOverride function overrides the measured + * blood pump motor speed. + * @details Inputs: none + * @details Outputs: adcBloodPumpMCSpeedRPM + * @param value override measured blood pump speed (in RPM) * @return TRUE if override successful, FALSE if not *************************************************************************/ -DATA_OVERRIDE_FUNC( F32, testSetMeasuredBloodPumpSpeedOverride, testResetMeasuredBloodPumpSpeedOverride, bloodPumpSpeedRPM ) +BOOL testSetMeasuredBloodPumpMCSpeedOverride( F32 value ) +{ + BOOL result = FALSE; -/************************************************************************* - * @brief testSetMeasuredBloodPumpMCSpeedOverride and testResetMeasuredBloodPumpMCSpeedOverride - * The testSetMeasuredBloodPumpMCSpeedOverride function overrides the measured \n - * blood pump motor speed. \n - * The testResetMeasuredBloodPumpMCSpeedOverride function resets the override of the \n + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + adcBloodPumpMCSpeedRPM.ovData = value; + adcBloodPumpMCSpeedRPM.override = OVERRIDE_KEY; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetMeasuredBloodPumpMCSpeedOverride function resets the override of the * measured blood pump motor speed. - * @details - * Inputs : none - * Outputs : adcBloodPumpMCSpeedRPM - * @param value : override measured blood pump speed (in RPM) + * @details Inputs: none + * @details Outputs: adcBloodPumpMCSpeedRPM + * @return TRUE if reset successful, FALSE if not + *************************************************************************/ +BOOL testResetMeasuredBloodPumpMCSpeedOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + adcBloodPumpMCSpeedRPM.override = OVERRIDE_RESET; + adcBloodPumpMCSpeedRPM.ovData = adcBloodPumpMCSpeedRPM.ovInitData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testSetMeasuredBloodPumpMCCurrentOverride function overrides the measured + * blood pump motor current. + * @details Inputs: none + * @details Outputs: adcBloodPumpMCCurrentmA + * @param value override measured blood pump current (in mA) * @return TRUE if override successful, FALSE if not *************************************************************************/ -DATA_OVERRIDE_FUNC( F32, testSetMeasuredBloodPumpMCSpeedOverride, testResetMeasuredBloodPumpMCSpeedOverride, adcBloodPumpMCSpeedRPM ) +BOOL testSetMeasuredBloodPumpMCCurrentOverride( F32 value ) +{ + BOOL result = FALSE; -/************************************************************************* - * @brief testSetMeasuredBloodPumpMCCurrentOverride and testResetMeasuredBloodPumpMCCurrentOverride - * The testSetMeasuredBloodPumpMCCurrentOverride function overrides the measured \n - * blood pump motor current. \n - * The testResetMeasuredBloodPumpMCCurrentOverride function resets the override of the \n + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + adcBloodPumpMCCurrentmA.ovData = value; + adcBloodPumpMCCurrentmA.override = OVERRIDE_KEY; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetMeasuredBloodPumpMCCurrentOverride function resets the override of the * measured blood pump motor current. - * @details - * Inputs : none - * Outputs : adcBloodPumpMCCurrentmA - * @param value : override measured blood pump current (in mA) - * @return TRUE if override successful, FALSE if not + * @details Inputs: none + * @details Outputs: adcBloodPumpMCCurrentmA + * @return TRUE if reset successful, FALSE if not *************************************************************************/ -DATA_OVERRIDE_FUNC( F32, testSetMeasuredBloodPumpMCCurrentOverride, testResetMeasuredBloodPumpMCCurrentOverride, adcBloodPumpMCCurrentmA ) +BOOL testResetMeasuredBloodPumpMCCurrentOverride( void ) +{ + BOOL result = FALSE; + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + adcBloodPumpMCCurrentmA.override = OVERRIDE_RESET; + adcBloodPumpMCCurrentmA.ovData = adcBloodPumpMCCurrentmA.ovInitData; + } + return result; +} + +/*********************************************************************//** + * @brief + * The testSetMeasuredBloodFlowSignalStrengthOverride function overrides the measured + * blood flow signal strength. + * @details Inputs: none + * @details Outputs: bloodFlowSignalStrength + * @param value override measured blood flow signal strength (in %) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetMeasuredBloodFlowSignalStrengthOverride( F32 value ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + bloodFlowSignalStrength.ovData = value / 100.0; + bloodFlowSignalStrength.override = OVERRIDE_KEY; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetMeasuredBloodFlowSignalStrengthOverride function resets the override + * of the measured blood flow signal strength. + * @details Inputs: none + * @details Outputs: bloodFlowSignalStrength + * @return TRUE if reset successful, FALSE if not + *************************************************************************/ +BOOL testResetMeasuredBloodFlowSignalStrengthOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + bloodFlowSignalStrength.override = OVERRIDE_RESET; + bloodFlowSignalStrength.ovData = bloodFlowSignalStrength.ovInitData; + } + + return result; +} + +/**@}*/