Index: firmware/App/Controllers/BloodFlow.c =================================================================== diff -u -r6eb873c4bc96fb22e85ac23aeee1c37e5366d731 -r96a23b8fe38dda9442cd6f052aeb3eadca198e4b --- firmware/App/Controllers/BloodFlow.c (.../BloodFlow.c) (revision 6eb873c4bc96fb22e85ac23aeee1c37e5366d731) +++ firmware/App/Controllers/BloodFlow.c (.../BloodFlow.c) (revision 96a23b8fe38dda9442cd6f052aeb3eadca198e4b) @@ -24,32 +24,35 @@ #include "DialInFlow.h" #include "PresOccl.h" +#include "BloodFlow.h" #include "FPGA.h" #include "InternalADC.h" #include "NVDataMgmt.h" #include "OperationModes.h" +#include "PersistentAlarm.h" #include "PIControllers.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% - -#define BP_CONTROL_INTERVAL ( 10000 / TASK_GENERAL_INTERVAL ) ///< interval (ms/task time) at which the blood pump is controlled + +/// 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 @@ -81,14 +84,20 @@ #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 -#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 ) ///< conversion factor from mL/min to estimated PWM duty cycle %. +/// 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 1998 ///< Blood pump ADC channel zero offset. -#define SIGN_FROM_12_BIT_VALUE(v) ( (S16)(v) - (S16)BLOODPUMP_ADC_ZERO ) ///< macro converts 12 bit ADC value to signed 16-bit value. +/// 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 ( ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) * 5 ) +#define MIN_FLOW_SIG_STRENGTH 90.0 ///< Minimum flow sensor signal strength. -#define SIZE_OF_ROLLING_AVG ( ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) * 10 ) ///< measured blood flow is filtered w/ moving average - /// Enumeration of blood pump controller states. typedef enum BloodPump_States { @@ -215,7 +224,12 @@ // 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 ); } /*********************************************************************//** @@ -404,7 +418,8 @@ * @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() * -1.0 ) * bloodFlowCalGain ) + bloodFlowCalOffset; // blood flow sensor installed backwards on HD @@ -419,7 +434,7 @@ updateBloodPumpSpeedAndDirectionFromHallSensors(); // don't start enforcing checks until out of init/POST mode - if ( getCurrentOperationMode() != MODE_INIT ) + if ( opMode != MODE_INIT ) { // check pump direction checkBloodPumpDirection(); @@ -430,6 +445,16 @@ checkBloodPumpFlowAgainstSpeed(); // check for home position, zero/low speed checkBloodPumpRotor(); + } + + // 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 ); } // publish blood flow data on interval