/************************************************************************** * * 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 Prime.h * * @author (last) Quang Nguyen * @date (last) 08-Dec-2020 * * @author (original) Quang Nguyen * @date (original) 08-Dec-2020 * ***************************************************************************/ #include "AirTrap.h" #include "AlarmMgmt.h" #include "BloodFlow.h" #include "DGInterface.h" #include "Prime.h" #include "TaskGeneral.h" #include "Valves.h" /** * @addtogroup Prime * @{ */ // ********** private definitions ********** #define BLOOD_PUMP_FLOW_RATE_PURGE_AIR 100 ///< Blood pump flow rate during prime purge air state. #define BLOOD_PUMP_FLOW_RATE_COLLECT_AIR 300 ///< Blood pump flow rate during prime collect air state. #define NO_AIR_DETECTED_COUNT ( 5 * MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< No air detected time period count. #define PURGE_AIR_TIME_OUT_COUNT ( 5 * MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Time period count for purge air time out. // ********** private data ********** static PRE_TREATMENT_PRIME_STATE_T currentPrimeState; ///< Current state of the prime sub-mode state machine. static BOOL isPrimeCompleted; ///< Status if prime sequence has been completed. static BOOL primeStartReqReceived; ///< Flag to indicate if a request to start priming has been received. static U32 noAirDetectedTimerCounter; ///< No air detected timer counter. static U32 purgeAirTimeOutTimerCount; ///< timer counter for purse air state time out. // ********** private function prototypes ********** static void purgeAirValvesBloodPumpControl( void ); static PRE_TREATMENT_PRIME_STATE_T handlePrimeSalineSetupState( void ); static PRE_TREATMENT_PRIME_STATE_T handlePrimePurgeAirState( void ); static PRE_TREATMENT_PRIME_STATE_T handlePrimeCircBloodCircuitState( void ); static PRE_TREATMENT_PRIME_STATE_T handlePrimeDialysateDialyzerState( void ); /*********************************************************************//** * @brief * The initPrime function initializes the prime sub-mode module. * This function will reset anything required before the start of priming sequence. * @details Inputs: none * @details Outputs: Prime sub-mode module initialized. * @return none *************************************************************************/ void initPrime( void ) { transitionToPrime(); } /*********************************************************************//** * @brief * The transitionToPrime function prepares for transition to prime sub-mode. * This function will reset anything required before the start of priming sequence. * @details Inputs: none * @details Outputs: none * @return none *************************************************************************/ void transitionToPrime( void ) { currentPrimeState = PRIME_START_STATE; isPrimeCompleted = FALSE; primeStartReqReceived = FALSE; cmdSetDGActiveReservoir( DG_RESERVOIR_2 ); cmdStartDGFill( FILL_RESERVOIR_TO_VOLUME_ML ); } /*********************************************************************//** * @brief * The execPrime function executes the prime sub-mode state machine. * @details Inputs: currentPrimeState * @details Outputs: currentPrimeState * @return none *************************************************************************/ void execPrime( void ) { // execute prime sub-mode state machine switch ( currentPrimeState ) { case PRIME_START_STATE: currentPrimeState = PRIME_SALINE_SETUP_STATE; break; case PRIME_SALINE_SETUP_STATE: currentPrimeState = handlePrimeSalineSetupState(); break; case PRIME_SALINE_PURGE_AIR_STATE: currentPrimeState = handlePrimePurgeAirState(); break; case PRIME_SALINE_CIRC_BLOOD_CIRCUIT_STATE: currentPrimeState = handlePrimeCircBloodCircuitState(); break; case PRIME_DIALYSATE_DIALYZER_STATE: currentPrimeState = handlePrimeDialysateDialyzerState(); break; } } /*********************************************************************//** * @brief * The isPrimingPassed function returns the status of prime mode. * @details Inputs: none * @details Outputs: none * @return TRUE if prime has completed, otherwise FALSE *************************************************************************/ BOOL isPrimingPassed( void ) { return isPrimeCompleted; } /*********************************************************************//** * @brief * The purgeAirValvesBloodPumpControl function controls valves and blood pump * to purge air. * @details Inputs: none * @details Outputs: run blood pump, close VDI, VDO, VBA and VBV valves, open VBT valve * @return current state (sub-mode) *************************************************************************/ static void purgeAirValvesBloodPumpControl( void ) { setValvePosition( VDI, VALVE_POSITION_C_CLOSE ); setValvePosition( VDO, VALVE_POSITION_C_CLOSE ); setValvePosition( VBA, VALVE_POSITION_C_CLOSE ); setValvePosition( VBV, VALVE_POSITION_C_CLOSE ); setValveAirTrap( STATE_OPEN ); setBloodPumpTargetFlowRate( BLOOD_PUMP_FLOW_RATE_PURGE_AIR, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); } /*********************************************************************//** * @brief * The handlePrimeSalineSetupState function checks user's request to start * priming. * @details Inputs: primeStartReqReceived * @details Outputs: control valves to purge air * @return current state (sub-mode) *************************************************************************/ static PRE_TREATMENT_PRIME_STATE_T handlePrimeSalineSetupState( void ) { PRE_TREATMENT_PRIME_STATE_T state = PRIME_SALINE_SETUP_STATE; if ( TRUE == primeStartReqReceived ) { purgeAirValvesBloodPumpControl(); purgeAirTimeOutTimerCount = 0; state = PRIME_SALINE_PURGE_AIR_STATE; } return state; } /*********************************************************************//** * @brief * The handlePrimePurgeAirState function checks for air trap level and moves * to blood circuit circulation state if fluid is detected at upper sensor. * @details Inputs: air trap levels * @details Outputs: runs blood pump, control valves to trap air * @return current state (sub-mode) *************************************************************************/ static PRE_TREATMENT_PRIME_STATE_T handlePrimePurgeAirState( void ) { PRE_TREATMENT_PRIME_STATE_T state = PRIME_SALINE_PURGE_AIR_STATE; if ( ++purgeAirTimeOutTimerCount > PURGE_AIR_TIME_OUT_COUNT ) { activateAlarmNoData( ALARM_ID_HD_PRIME_PURGE_AIR_TIME_OUT ); } if ( AIR_TRAP_LEVEL_FLUID == getAirTrapLevel( AIR_TRAP_LEVEL_SENSOR_UPPER ) ) { setValvePosition( VDI, VALVE_POSITION_C_CLOSE ); setValvePosition( VDO, VALVE_POSITION_C_CLOSE ); setValvePosition( VBA, VALVE_POSITION_B_OPEN ); setValvePosition( VBV, VALVE_POSITION_B_OPEN ); setValveAirTrap( STATE_CLOSED ); setBloodPumpTargetFlowRate( BLOOD_PUMP_FLOW_RATE_COLLECT_AIR, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); noAirDetectedTimerCounter = 0; state = PRIME_SALINE_CIRC_BLOOD_CIRCUIT_STATE; } return state; } /*********************************************************************//** * @brief * The handlePrimeCircBloodCircuitState function checks for air trap level and * return to purge air state if air is detected at lower sensor. If no air * detected for a period of time, the blood pump is stopped. * @details Inputs: air trap levels * @details Outputs: stop blood pump, control valves to purge air * @return current state (sub-mode) *************************************************************************/ static PRE_TREATMENT_PRIME_STATE_T handlePrimeCircBloodCircuitState( void ) { PRE_TREATMENT_PRIME_STATE_T state = PRIME_SALINE_CIRC_BLOOD_CIRCUIT_STATE; if ( AIR_TRAP_LEVEL_AIR == getAirTrapLevel( AIR_TRAP_LEVEL_SENSOR_LOWER ) ) { purgeAirValvesBloodPumpControl(); purgeAirTimeOutTimerCount = 0; state = PRIME_SALINE_PURGE_AIR_STATE; } if ( ++noAirDetectedTimerCounter > NO_AIR_DETECTED_COUNT ) { signalBloodPumpHardStop(); state = PRIME_DIALYSATE_DIALYZER_STATE; } return state; } /*********************************************************************//** * @brief * The handlePrimeDialysateDialyzerState function handles priming for * dialysate dialyzer fluid path. * @details Inputs: none * @details Outputs: isPrimeCompleted * @return current state (sub-mode) *************************************************************************/ static PRE_TREATMENT_PRIME_STATE_T handlePrimeDialysateDialyzerState( void ) { // TODO: Add priming for dialysate circuit isPrimeCompleted = TRUE; return PRIME_DIALYSATE_DIALYZER_STATE; } /**@}*/