Index: firmware/App/Controllers/AirTrap.c =================================================================== diff -u -r395522dffef1348e176564925656012f529c1910 -r0a2ab0f2ca63d486018a0737bf692bb3d028bf28 --- firmware/App/Controllers/AirTrap.c (.../AirTrap.c) (revision 395522dffef1348e176564925656012f529c1910) +++ firmware/App/Controllers/AirTrap.c (.../AirTrap.c) (revision 0a2ab0f2ca63d486018a0737bf692bb3d028bf28) @@ -24,6 +24,7 @@ #include "OperationModes.h" #include "PersistentAlarm.h" #include "Pressures.h" +#include "Switches.h" #include "TaskGeneral.h" #include "Timers.h" #include "Utilities.h" @@ -36,54 +37,63 @@ // ********** private definitions ********** -#define AIR_TRAP_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the air trap data is published on the CAN bus. -#define AIR_TRAP_FILL_TIMEOUT_MS ( 10 * MS_PER_SECOND ) ///< Air trap fill timeout period (in ms). -#define AIR_PUMP_ON_DELAY_TIME_MS ( 10 * MS_PER_SECOND ) ///< Delay between air pump On (in ms). -#define AIR_PUMP_LOWER_OP_TIME_MIN_MS 200 ///< Air trap lower operation minimum time (in ms). -#define AIR_PUMP_ON_ERROR_MAX_CNT 6 ///< Maximum number of air pump on events within time window before alarm triggered. Do not exceed MAX_TIME_WINDOWED_COUNT. -#define AIR_PUMP_ON_ERROR_TIME_WIN_MS ( 60 * MS_PER_SECOND ) ///< Time window for Air Pump on count error. -#define AIR_TRAP_ILLEGAL_LEVELS_TIMEOUT_MS ( 2 * MS_PER_SECOND ) ///< Air trap illegal values timeout (in ms) -#define VENOUS_LINE_VOLUME_ML ( 200.0F ) ///< Volume (in mL) of venous portion of blood circuit line. TODO - get actual volume from Systems. -#define DATA_PUBLISH_COUNTER_START_COUNT 7 ///< Data publish counter start count. +#define AIR_TRAP_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the air trap data is published on the CAN bus. +#define AIR_TRAP_FILL_TIMEOUT_MS ( 3 * MS_PER_SECOND ) ///< Air trap fill timeout period (in ms). Air pump will turn on for 1 second 3 times +#define AIR_TRAP_LOWER_TIMEOUT_MS ( 35 * MS_PER_SECOND ) ///< Air trap lower timeout period (in ms). Maximum air pump turn on for 1 sec, then a 10 sec delay and repeat this for 3 times (1+10)*3 = 33 +#define AIR_PUMP_ON_DELAY_TIME_MS ( 10 * MS_PER_SECOND ) ///< Delay between air pump On (in ms). +#define AIR_PUMP_STOP_TIME_MS ( 1 * MS_PER_SECOND ) ///< Maximum air pump turn on time +//#define AIR_PUMP_ON_ERROR_MAX_CNT 6 ///< Maximum number of air pump on events within time window before alarm triggered. Do not exceed MAX_TIME_WINDOWED_COUNT. +//#define AIR_PUMP_ON_ERROR_TIME_WIN_MS ( 60 * MS_PER_SECOND ) ///< Time window for Air Pump on count error. +#define AIR_TRAP_ILLEGAL_LEVELS_TIMEOUT_MS ( 2 * MS_PER_SECOND ) ///< Air trap illegal values timeout (in ms) +//#define VENOUS_LINE_VOLUME_ML ( 200.0F ) ///< Volume (in mL) of venous portion of blood circuit line. TODO - get actual volume from Systems. +#define DATA_PUBLISH_COUNTER_START_COUNT 7 ///< Data publish counter start count. + /// Air pump on delay after fill adjustment static const U32 AIR_PUMP_ON_DELAY_ADJUST_AFTER_FILL = ( AIR_PUMP_ON_DELAY_TIME_MS - ( 1 * MS_PER_SECOND ) ); -#define AIR_PUMP_MOTOR_FILL_PWM 60 ///< Power level setting (PWM) for H12 air pump to fill (raise level) the air trap. -#define AIR_PUMP_MOTOR_LOWER_PWM 250 ///< Power level setting (PWM) for H12 air pump to lower the level of the air trap. -#define AIR_PUMP_MOTOR_MAX_PWM_OVERRIDE 255 ///< Air pump motor maximum PWM to override. +#define AIR_PUMP_MOTOR_FILL_PWM 150 ///< Power level setting (PWM) for H12 air pump to fill (raise level) the air trap. +#define AIR_PUMP_MOTOR_LOWER_PWM 250 ///< Power level setting (PWM) for H12 air pump to lower the level of the air trap. +#define AIR_PUMP_MOTOR_MIN_PWM_OVERRIDE 60 ///< Air pump motor minimum PWM to override. +#define AIR_PUMP_MOTOR_MAX_PWM_OVERRIDE 255 ///< Air pump motor maximum PWM to override. +/// from air pump manufacturer's data for pump current draw based on pump speed and venous pressure mmHg, +/// the currently set air pump duty cycle (raw PWM) = 0.0005x^2 + 0.2313x + 63.667, where x is H14 venous pressure in mmHg before air pump lower event +#define QUADRATIC_COEFFICIENT 0.005F ///< X2 quadratic coefficient +#define LINEAR_COEFFICIENT 0.2313 ///< X linear coefficient +#define CONSTANT_TERM 63.667 ///< Constant term + /// Defined states for the air trap controller state machine. typedef enum AirTrap_States { - AIR_TRAP_INIT_STATE = 0, ///< Initialization state. - AIR_TRAP_MANUAL_CONTROL_STATE, ///< Manually control air trap valve state. - AIR_TRAP_CLOSED_STATE, ///< Closed state - level between lower and upper sensors. - AIR_TRAP_RAISE_LEVEL_STATE, ///< Raise level state. - AIR_TRAP_LOWER_LEVEL_STATE, ///< Lower level state. - NUM_OF_AIR_TRAP_STATES ///< Number of air trap controller states. + AIR_TRAP_INIT_STATE = 0, ///< Initialization state. + AIR_TRAP_MANUAL_CONTROL_STATE, ///< Manually control air trap valve state. + AIR_TRAP_CLOSED_STATE, ///< Closed state - level between lower and upper sensors. + AIR_TRAP_RAISE_LEVEL_STATE, ///< Raise level state. + AIR_TRAP_LOWER_LEVEL_STATE, ///< Lower level state. + NUM_OF_AIR_TRAP_STATES ///< Number of air trap controller states. } AIR_TRAP_STATE_T; // ********** private data ********** -static AIR_TRAP_STATE_T airTrapControllerState; ///< Current state of air trap controller state machine. +static AIR_TRAP_STATE_T airTrapControllerState; ///< Current state of air trap controller state machine. -static U32 airTrapDataPublicationTimerCounter; ///< Used to schedule air trap data publication to CAN bus. -static OVERRIDE_U32_T airTrapDataPublishInterval; ///< Interval (in ms) at which to publish air trap data to CAN bus. +static U32 airTrapDataPublicationTimerCounter; ///< Used to schedule air trap data publication to CAN bus. +static OVERRIDE_U32_T airTrapDataPublishInterval; ///< Interval (in ms) at which to publish air trap data to CAN bus. -static OVERRIDE_U32_T airTrapAirPumpRaisePowerLevel; ///< Air pump power level to use when raising the air trap blood level. -static OVERRIDE_U32_T airTrapAirPumpLowerPowerLevel; ///< Air pump power level to use when lowering the air trap blood level. +static OVERRIDE_U32_T airTrapAirPumpRaisePowerLevel; ///< Air pump power level to use when raising the air trap blood level. +static OVERRIDE_U32_T airTrapAirPumpLowerPowerLevel; ///< Air pump power level to use when lowering the air trap blood level. -static BOOL pendingStartAirTrapController; ///< Flag indicates an air trap controller start request is pending. -static BOOL pendingStopAirTrapController; ///< Flag indicates an air trap controller stop request is pending. +static BOOL pendingStartAirTrapController; ///< Flag indicates an air trap controller start request is pending. +static BOOL pendingStopAirTrapController; ///< Flag indicates an air trap controller stop request is pending. -static U32 airTrapFillStartTime; ///< Air trap fill (raise level) operation start time. -static U32 airTrapLowerStartTime; ///< Air trap lower level operation start time. -static U32 airTrapLowerDelayStartTime; ///< Air trap lower level operation delay start time. +static U32 airTrapFillStartTime; ///< Air trap fill (raise level) operation start time. +static U32 airTrapLowerStartTime; ///< Air trap lower level operation start time. +static U32 airTrapLowerDelayStartTime; ///< Air trap lower level operation delay start time. -static BOOL airTrapWasFilledAtStartOfTreatement; ///< To ensure the fluid level high during start of treatment, we need to do a fill operation. +static BOOL airTrapWasFilledAtStartOfTreatement; ///< To ensure the fluid level high during start of treatment, we need to do a fill operation. -static BOOL airTrapHasDetectedFaultMode; ///< Flag indicates air trap unit has handled transition to fault mode. +static BOOL airTrapHasDetectedFaultMode; ///< Flag indicates air trap unit has handled transition to fault mode. // ********** private function prototypes ********** @@ -110,23 +120,27 @@ // Initialize controller variables resetAirTrap(); - airTrapDataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; + airTrapDataPublishInterval.data = AIR_TRAP_DATA_PUB_INTERVAL; airTrapDataPublishInterval.ovData = AIR_TRAP_DATA_PUB_INTERVAL; airTrapDataPublishInterval.ovInitData = AIR_TRAP_DATA_PUB_INTERVAL; airTrapDataPublishInterval.override = OVERRIDE_RESET; + airTrapAirPumpRaisePowerLevel.data = AIR_PUMP_MOTOR_FILL_PWM; airTrapAirPumpRaisePowerLevel.ovData = AIR_PUMP_MOTOR_FILL_PWM; airTrapAirPumpRaisePowerLevel.ovInitData = AIR_PUMP_MOTOR_FILL_PWM; airTrapAirPumpRaisePowerLevel.override = OVERRIDE_RESET; + airTrapAirPumpLowerPowerLevel.data = AIR_PUMP_MOTOR_LOWER_PWM; airTrapAirPumpLowerPowerLevel.ovData = AIR_PUMP_MOTOR_LOWER_PWM; airTrapAirPumpLowerPowerLevel.ovInitData = AIR_PUMP_MOTOR_LOWER_PWM; airTrapAirPumpLowerPowerLevel.override = OVERRIDE_RESET; - airTrapFillStartTime = 0; - airTrapLowerDelayStartTime = 0; - airTrapLowerStartTime = 0; - airTrapHasDetectedFaultMode = FALSE; + + airTrapDataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; + airTrapFillStartTime = 0; + airTrapLowerDelayStartTime = 0; + airTrapLowerStartTime = 0; + airTrapHasDetectedFaultMode = FALSE; } /*********************************************************************//** @@ -213,7 +227,7 @@ readAirTrapLevelSensors(); // Check for illegal levels alarm - if ( TRUE ) // TODO - need a way to determine whether a blood set is installed - if not installed, we would not trigger illegal levels alarm + if ( STATE_CLOSED == getSwitchState( H9_SWCH ) ) // determine whether a blood set is installed - if not installed, do not trigger illegal levels alarm { // If the pump track on open, zero the persistent counter to not check the air trap illegal level alarm checkPersistentAlarm( ALARM_ID_TD_AIR_TRAP_ILLEGAL_LEVELS, FALSE, 0.0, 0.0 ); @@ -256,11 +270,26 @@ #endif { #ifndef TEST_UI_ONLY - activateAlarmNoData( ALARM_ID_TD_AIR_TRAP_FILL_DURING_TREATMENT ); + activateAlarmNoData( ALARM_ID_TD_AIR_TRAP_FILL_TIMEOUT ); #endif } } } + // Check air trap lowering timeout during treatment + else if ( AIR_TRAP_LOWER_LEVEL_STATE == airTrapControllerState ) + { + if ( TRUE == didTimeout( airTrapLowerStartTime, AIR_TRAP_LOWER_TIMEOUT_MS ) ) + { +#ifndef _RELEASE_ +// if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_AIR_TRAP_LEVELING_ALARM ) != SW_CONFIG_ENABLE_VALUE ) +#endif + { +#ifndef TEST_UI_ONLY + activateAlarmNoData( ALARM_ID_TD_AIR_TRAP_LEVEL_HIGH ); +#endif + } + } + } } /*********************************************************************//** @@ -315,8 +344,26 @@ publishAirTrapData(); } + /*********************************************************************//** * @brief + * The getCalculatedAirPumpDutyCycle function get the Air Pump calculated raw dutycycle based on H14 pressure + * @details \b Inputs: H14 venous presure + * @details \b Outputs: none + * @return dutyCyclePct + *************************************************************************/ +F32 getCalculatedAirPumpDutyCycle( void ) +{ + F32 h14Pressure = getFilteredVenousPressure(); + // the currently set P12 duty cycle (raw PWM) must be 0.0005x^2 + 0.2313x + 63.667, where x = H14 venous pressure in mmHg before air pump lower event + F32 dutyCycle = ( ( QUADRATIC_COEFFICIENT * ( h14Pressure * h14Pressure ) ) - ( LINEAR_COEFFICIENT * ( h14Pressure ) ) ) + CONSTANT_TERM; + + return dutyCycle; +} + + +/*********************************************************************//** + * @brief * The handleAirTrapManualControlState function handles the manual control * state of the air trap. * @details \b Inputs: pendingStartAirTrapController @@ -367,6 +414,7 @@ ( TRUE == didTimeout( airTrapLowerDelayStartTime, AIR_PUMP_ON_DELAY_TIME_MS ) ) ) { set3WayValveState( H13_VALV, VALVE_3WAY_COMMON_TO_OPEN_STATE ); // open valve H13 to allow air to be pumped into air trap from atmosphere + airTrapAirPumpLowerPowerLevel.data = getCalculatedAirPumpDutyCycle(); setAirPumpState( AIR_PUMP_STATE_ON, getAirPumpLowerPowerLevel() ); airTrapLowerStartTime = getMSTimerCount(); signalInitiatePressureStabilization( USE_SHORT_STABILIZATION_PERIOD ); @@ -416,6 +464,7 @@ airTrapWasFilledAtStartOfTreatement = TRUE; set3WayValveState( H20_VALV, VALVE_3WAY_COMMON_TO_CLOSED_STATE ); set3WayValveState( H13_VALV, VALVE_3WAY_COMMON_TO_OPEN_STATE ); // open valve H13 to allow air to be pumped into air trap from atmosphere + airTrapAirPumpLowerPowerLevel.data = getCalculatedAirPumpDutyCycle(); setAirPumpState( AIR_PUMP_STATE_ON, getAirPumpLowerPowerLevel() ); airTrapLowerStartTime = getMSTimerCount(); signalInitiatePressureStabilization( USE_SHORT_STABILIZATION_PERIOD ); @@ -438,10 +487,6 @@ static AIR_TRAP_STATE_T handleAirTrapLowerlevelState( void ) { AIR_TRAP_STATE_T result = AIR_TRAP_LOWER_LEVEL_STATE; - // Air pump stop time based on the blood flow rate - S32 qB = 500; // TODO abs( getTargetBloodFlowRate() ); - U32 qBx = ( 0 == qB ? MIN_SET_BLOOD_FLOW_RATE : (U32)qB ); - U32 airPumpStopTime = 1000; //AIR_PUMP_LOWER_OP_TIME_MIN_MS + ( ( qBx - MIN_SET_BLOOD_FLOW_RATE ) / 2 ); // Transition to manual valve control state when requested if ( TRUE == pendingStopAirTrapController ) @@ -452,19 +497,14 @@ result = AIR_TRAP_MANUAL_CONTROL_STATE; } // Transition back to closed state when air trap lower level operation completed when air detected at upper level - else if ( ( TRUE == didTimeout( airTrapLowerStartTime, airPumpStopTime ) ) || + else if ( ( TRUE == didTimeout( airTrapLowerStartTime, AIR_PUMP_STOP_TIME_MS ) ) || ( AIR_TRAP_LEVEL_AIR == getRawLevelSensorState( H16_LEVL ) ) ) { setAirPumpState( AIR_PUMP_STATE_OFF, AIR_PUMP_MOTOR_OFF ); set3WayValveState( H13_VALV, VALVE_3WAY_COMMON_TO_CLOSED_STATE ); airTrapLowerDelayStartTime = getMSTimerCount(); signalLowVenousPressureCheck(); SEND_EVENT_WITH_2_U32_DATA( TD_EVENT_AIR_TRAP_LOWER, OFF, 0 ); - // if lower level operation times out, alarm - if ( TRUE == didTimeout( airTrapLowerStartTime, airPumpStopTime ) ) - { - // TODO - alarm??? - } result = AIR_TRAP_CLOSED_STATE; } @@ -506,7 +546,8 @@ * The publishAirTrapData function constructs and sends the air trap data * broadcast message. * @details \b Message \b Sent: MSG_ID_TD_AIR_TRAP_DATA to publish air trap data - * @details \b Inputs: airTrapDataPublicationTimerCounter, airTrapControllerState, TODO valvestate + * @details \b Inputs: airTrapDataPublicationTimerCounter, airTrapControllerState, h17State, h16State, + * h17RawState, h16RawState, h13State, h20State, airTrapControllerState * @details \b Outputs: airTrapDataPublicationTimerCounter * @return none *************************************************************************/