Index: firmware/App/Modes/Prime.c =================================================================== diff -u -r4d2dcd2d5ee3c7684be139b7e560b16d8c3abc06 -r604a7f0b5ffb72a8c5989f7476e4423181f54e4d --- firmware/App/Modes/Prime.c (.../Prime.c) (revision 4d2dcd2d5ee3c7684be139b7e560b16d8c3abc06) +++ firmware/App/Modes/Prime.c (.../Prime.c) (revision 604a7f0b5ffb72a8c5989f7476e4423181f54e4d) @@ -24,31 +24,209 @@ // ********** 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_SEC / TASK_GENERAL_INTERVAL ) ///< No air detected time period count. + // ********** private data ********** -static BOOL primeStatus; +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. // ********** 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 ) { - primeStatus = FALSE; + 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; } +/*********************************************************************//** + * @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 ) { - primeStatus = TRUE; - return primeStatus; + 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 ); + // TODO: setValvePosition( VBT, VALVE_POSITION_B_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(); + 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 ( 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 ); + // TODO: setValvePosition( VBT, VALVE_POSITION_C_CLOSE ); + + 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(); + 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; +} + /**@}*/