Index: firmware/App/Modes/ModeGenIdle.c =================================================================== diff -u -r025612ad77fe630889a364586de54bffe5262d56 -rd502fb7b72c8b3aa4f3a7cdbc4b3c4f7b5ae7c92 --- firmware/App/Modes/ModeGenIdle.c (.../ModeGenIdle.c) (revision 025612ad77fe630889a364586de54bffe5262d56) +++ firmware/App/Modes/ModeGenIdle.c (.../ModeGenIdle.c) (revision d502fb7b72c8b3aa4f3a7cdbc4b3c4f7b5ae7c92) @@ -21,6 +21,7 @@ #include "DrainPump.h" #include "FPGA.h" #include "Heaters.h" +#include "ModeFill.h" #include "ModeGenIdle.h" #include "NVDataMgmt.h" #include "OperationModes.h" @@ -52,14 +53,23 @@ // ********** private data ********** static DG_GEN_IDLE_MODE_STATE_T genIdleState; ///< Currently active generation idle state. +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T badFillState; ///< Current active bad fill state. static F32 flushLinesVolumeL = 0.0; ///< Volume of water pumped by RO pump during flush lines state. static U32 hdLostCommStartTime_ms = 0; ///< Lost communication with HD start time in ms. // ********** private function prototypes ********** +static DG_GEN_IDLE_MODE_STATE_T handleIdleStartState( void ); static DG_GEN_IDLE_MODE_STATE_T handleFlushLinesState( void ); static DG_GEN_IDLE_MODE_STATE_T handleFlushWaterState( void ); +static DG_GEN_IDLE_MODE_STATE_T handleBadFillState( void ); // This state has sub-states 1.0 to 1.4 +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleFirstDrainState( void ); // idle 1.0 +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleFlushFillState( void ); // idle 1.1 +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleSecondDrainState( void ); // idle 1.2 +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleRefillState( void ); // idle 1.3 +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleClearAlarmState( void ); // idle 1.4 + /*********************************************************************//** * @brief * The initGenIdleMode function initializes the generation idle mode module. @@ -70,6 +80,7 @@ void initGenIdleMode( void ) { genIdleState = DG_GEN_IDLE_MODE_STATE_START; + badFillState = DG_HANDLE_BAD_FILL_STATE_FIRST_DRAIN; flushLinesVolumeL = 0.0; hdLostCommStartTime_ms = 0; } @@ -118,6 +129,35 @@ /*********************************************************************//** * @brief + * The getCurrentGenIdleState function returns the current state of the + * generation idle mode. + * @details Inputs: genIdleState + * @details Outputs: none + * @return the current state of generation idle mode + *************************************************************************/ +DG_GEN_IDLE_MODE_STATE_T getCurrentGenIdleState( void ) +{ + return genIdleState; +} + +/*********************************************************************//** + * @brief + * The requestDGStop function handles an HD request to stop (return to standby mode). + * @details Inputs: none + * @details Outputs: DG standby mode requested + * @return TRUE if request accepted, FALSE if not. + *************************************************************************/ +BOOL requestDGStop( void ) +{ + BOOL result = TRUE; + + requestNewOperationMode( DG_MODE_STAN ); + + return result; +} + +/*********************************************************************//** + * @brief * The execGenIdleMode function executes the generation idle mode state machine. * @details Inputs: genIdleState * @details Outputs: Check water quality, generation idle mode state machine executed @@ -148,7 +188,7 @@ switch ( genIdleState ) { case DG_GEN_IDLE_MODE_STATE_START: - genIdleState = DG_GEN_IDLE_MODE_STATE_FLUSH_LINES; + genIdleState = handleIdleStartState(); break; case DG_GEN_IDLE_MODE_STATE_FLUSH_LINES: @@ -159,6 +199,10 @@ genIdleState = handleFlushWaterState(); break; + case DG_GEN_IDLE_MODE_STATE_HANDLE_BAD_FILL: + genIdleState = handleBadFillState(); + break; + default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_GEN_IDLE_MODE_INVALID_EXEC_STATE, genIdleState ) genIdleState = DG_GEN_IDLE_MODE_STATE_START; @@ -170,30 +214,25 @@ /*********************************************************************//** * @brief - * The getCurrentGenIdleState function returns the current state of the - * generation idle mode. - * @details Inputs: genIdleState - * @details Outputs: none - * @return the current state of generation idle mode + * The handleIdleStartState function executes the start state of the + * generation idle mode state machine. + * @details Inputs: flushLinesVolumeL + * @details Outputs: Integrate volume of water moved through line + * @return the next state *************************************************************************/ -DG_GEN_IDLE_MODE_STATE_T getCurrentGenIdleState( void ) +static DG_GEN_IDLE_MODE_STATE_T handleIdleStartState( void ) { - return genIdleState; -} + DG_GEN_IDLE_MODE_STATE_T result = DG_GEN_IDLE_MODE_STATE_START; -/*********************************************************************//** - * @brief - * The requestDGStop function handles an HD request to stop (return to standby mode). - * @details Inputs: none - * @details Outputs: DG standby mode requested - * @return TRUE if request accepted, FALSE if not. - *************************************************************************/ -BOOL requestDGStop( void ) -{ - BOOL result = TRUE; + if ( TRUE == isAvgConductivityOutOfRange() ) + { + result = DG_GEN_IDLE_MODE_STATE_HANDLE_BAD_FILL ; + } + else + { + result = DG_GEN_IDLE_MODE_STATE_FLUSH_LINES; + } - requestNewOperationMode( DG_MODE_STAN ); - return result; } @@ -229,14 +268,154 @@ /*********************************************************************//** * @brief * The handleFlushWaterState function executes the flush water state - * of the generation idle mode state machine. + * generation idle mode state machine. * @details Inputs: none * @details Outputs: none * @return the next state *************************************************************************/ static DG_GEN_IDLE_MODE_STATE_T handleFlushWaterState( void ) { - return DG_GEN_IDLE_MODE_STATE_FLUSH_WATER; + DG_GEN_IDLE_MODE_STATE_T result = DG_GEN_IDLE_MODE_STATE_FLUSH_WATER; + + return result; } +/*********************************************************************//** + * @brief + * The handleBadFillState function executes the bad fill state of the + * generation idle mode state machine. + * @details Inputs: + * @details Outputs: + * @return the next state + *************************************************************************/ +static DG_GEN_IDLE_MODE_STATE_T handleBadFillState( void ) +{ + DG_GEN_IDLE_MODE_STATE_T result = DG_GEN_IDLE_MODE_STATE_HANDLE_BAD_FILL; + + // Execute current bad fill state + switch ( badFillState ) + { + case DG_HANDLE_BAD_FILL_STATE_START: + badFillState = DG_HANDLE_BAD_FILL_STATE_FIRST_DRAIN; + break; + + case DG_HANDLE_BAD_FILL_STATE_FIRST_DRAIN: // idle 1.0 + badFillState = handleFirstDrainState(); + break; + + case DG_HANDLE_BAD_FILL_STATE_FLUSH_FILL: // idle 1.1 + badFillState = handleFlushFillState(); + break; + + case DG_HANDLE_BAD_FILL_STATE_SECOND_DRAIN: // idle 1.2 + badFillState = handleSecondDrainState(); + break; + + case DG_HANDLE_BAD_FILL_STATE_REFILL: + badFillState = handleRefillState(); // idle 1.3 + break; + + case DG_HANDLE_BAD_FILL_STATE_CLEAR_ALARM: + badFillState = handleClearAlarmState(); // idle 1.4 + break; + + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_GEN_IDLE_MODE_INVALID_EXEC_STATE, genIdleState ) + badFillState = DG_HANDLE_BAD_FILL_STATE_START; + break; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The handleFirstDrainState function executes the first drain state of the + * handle bad fill state machine. + * @details Inputs: + * @details Outputs: + * @return the next state + *************************************************************************/ +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleFirstDrainState( void ) +{ + DG_GEN_IDLE_MODE_BAD_FILL_STATE_T result = DG_HANDLE_BAD_FILL_STATE_FIRST_DRAIN; + + requestNewOperationMode( DG_MODE_DRAI ); // go to drain mode to empty bad dialysate because this is a bad fill + result = DG_HANDLE_BAD_FILL_STATE_FLUSH_FILL; // after first drain is completed, go to next sub-state + + return result; +} + +/*********************************************************************//** + * @brief + * The handleFlushFillState function executes the flush fill state of the + * handle bad fill state machine. + * @details Inputs: + * @details Outputs: + * @return the next state + *************************************************************************/ +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleFlushFillState( void ) +{ + DG_GEN_IDLE_MODE_BAD_FILL_STATE_T result = DG_HANDLE_BAD_FILL_STATE_FLUSH_FILL; + + requestNewOperationMode( DG_MODE_FILL ); // When fill is completed, it goes to generation idle mode (DG_GEN_IDLE_MODE_STATE_START) + result = DG_HANDLE_BAD_FILL_STATE_SECOND_DRAIN; // (idle 1.2) + + return result; +} + +/*********************************************************************//** + * @brief + * The handleSecondDrainState function executes the second drain state of the + * handle bad fill state machine. + * @details Inputs: + * @details Outputs: + * @return the next state + *************************************************************************/ +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleSecondDrainState( void ) +{ + DG_GEN_IDLE_MODE_BAD_FILL_STATE_T result = DG_HANDLE_BAD_FILL_STATE_SECOND_DRAIN; + + requestNewOperationMode( DG_MODE_DRAI ); // go to drain mode to empty bad dialysate because this is a bad fill + result = DG_HANDLE_BAD_FILL_STATE_REFILL; // after second drain completed, go to refill sub-state (idle 1.3) + + return result; +} + +/*********************************************************************//** + * @brief + * The handleRefillState function executes refill state of the + * handle bad fill state machine. + * @details Inputs: + * @details Outputs: + * @return the next state + *************************************************************************/ +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleRefillState( void ) +{ + DG_GEN_IDLE_MODE_BAD_FILL_STATE_T result = DG_HANDLE_BAD_FILL_STATE_REFILL; + + requestNewOperationMode( DG_MODE_FILL ); + result = DG_HANDLE_BAD_FILL_STATE_CLEAR_ALARM; // (idle 1.4) + + return result; +} + +/*********************************************************************//** + * @brief + * The handleClearAlarmState function executes the clear alarm state of the + * handle bad fill state machine. + * @details Inputs: + * @details Outputs: + * @return the next state + *************************************************************************/ +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleClearAlarmState( void ) +{ + DG_GEN_IDLE_MODE_BAD_FILL_STATE_T result = DG_HANDLE_BAD_FILL_STATE_CLEAR_ALARM; + + // clear "Wait for dialysate alarm" before resume to normal state + requestNewOperationMode( DG_MODE_GENE ); // go to generation idle mode + + return result; +} + /**@}*/