Index: firmware/App/Modes/ModeFlush.c =================================================================== diff -u -r5d8530d242d8065178eab9e3e5d8e4561b790e01 -r45f4646609e6dd39691102e109d0b5c14f97e054 --- firmware/App/Modes/ModeFlush.c (.../ModeFlush.c) (revision 5d8530d242d8065178eab9e3e5d8e4561b790e01) +++ firmware/App/Modes/ModeFlush.c (.../ModeFlush.c) (revision 45f4646609e6dd39691102e109d0b5c14f97e054) @@ -40,8 +40,8 @@ // General defines #define FLUSH_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Mode flush data publish interval in counts. -#define RO_PUMP_TARGET_FLOW_RATE_LPM 0.8 ///< RO pump target flow rate during flush/fill in L/min. TODO original flow was 0.8 -#define RO_PUMP_MAX_PRESSURE_PSI 130 ///< Maximum RO pump pressure during flush/fill states in psi. +#define RO_PUMP_TARGET_FLOW_RATE_LPM 0.8 ///< RO pump target flow rate during flush/fill in L/min. TODO original flow was 0.8 +#define RO_PUMP_MAX_PRESSURE_PSI 130 ///< Maximum RO pump pressure during flush/fill states in psi. #define DRAIN_PUMP_TARGET_RPM 1800 ///< Drain pump target RPM during drain. // Start state defines @@ -52,7 +52,7 @@ #define DRAIN_WEIGHT_UNCHANGE_TIMEOUT ( 6 * MS_PER_SECOND ) ///< Time period of unchanged weight during draining before timeout. // Flush drain path state defines -#define FLUSH_DRAIN_WAIT_TIME_MS ( 1 * 60 * MS_PER_SECOND ) ///< Flush Drain path wait time in milliseconds. TODo original time is 2 minutes +#define FLUSH_DRAIN_WAIT_TIME_MS ( 1 * 60 * MS_PER_SECOND ) ///< Flush Drain path wait time in milliseconds. TODo original time is 2 minutes #define MIN_INLET_TEMPERATURE_C 15.0 ///< Minimum water inlet temperature in C. TODO original temperature was 25 C #define MIN_INLET_CONDUCTIVITY_US_PER_CM 0.0 ///< Minimum water inlet conductivity in uS/cm #define MAX_INLET_CONDUCTIVITY_US_PER_CM 2000.0 ///< Maximum water inlet conductivity in us/cm @@ -61,28 +61,31 @@ #define FLUSH_DIALYSATE_WAIT_TIME_MS ( 0.5 * 60 * MS_PER_SECOND ) ///< Flush dialysate wait time in milliseconds. // Flush concentrate straws state defines -#define FLUSH_CONCENTRATE_STRAWS_TIME_MS ( 0.5 * 60 * MS_PER_SECOND ) ///< Flush concentrate straws wait time in milliseconds. TODO original time is 3 minutes +#define FLUSH_CONCENTRATE_STRAWS_TIME_MS ( 0.5 * 60 * MS_PER_SECOND ) ///< Flush concentrate straws wait time in milliseconds. TODO original time is 3 minutes -// Flush and drain R1 and R2 -#define RSRVRS_FULL_VOL_ML 1750.0 ///< Reservoirs 1 & 2 full volume in mL. TODo original value was 1900 +// Flush and drain R1 and R2 state defines +#define RSRVRS_FULL_VOL_ML 1650.0 ///< Reservoirs 1 & 2 full volume in mL. TODo original value was 1900 #define RSRVRS_PARTIAL_FILL_VOL_ML 500.0 ///< Reservoirs 1 & 2 partial volume in mL. #define RSRVRS_FULL_STABLE_TIME_COUNT ( ( 4 * MS_PER_SECOND ) / TASK_GENERAL_INTERVAL ) ///< Reservoirs 1 & 2 full stable time in counts. #define RSRVRS_FILL_UP_TIMEOUT_MS ( 5 * 60 * MS_PER_SECOND ) ///< Reservoirs 1 & 2 full fill up timeout in ms. TODO original value was 5 mins #define RSRVRS_PARTIAL_FILL_TIMEOUT_MS ( 2 * 60 * MS_PER_SECOND ) ///< Reservoirs 1 & 2 partial fill up timeout in ms. #define RSRVRS_DRAIN_TIMEOUT_MS ( 2 * 60 * MS_PER_SECOND ) ///< Reservoirs 1 & 2 drain timeout in ms. +// Flush circulation state defines +#define FLUSH_CIRCULATION_WAIT_TIME_MS ( 0.5 * 60 * MS_PER_SECOND ) ///< Flush circulation wait time in milliseconds. + // ********** private data ********** -static DG_FLUSH_STATE_T flushState = DG_FLUSH_STATE_START; ///< Currently active flush state. -static DG_FLUSH_STATE_T prevFlushState = DG_FLUSH_STATE_START; +static DG_FLUSH_STATE_T flushState = DG_FLUSH_STATE_START; ///< Current active flush state. +static DG_FLUSH_STATE_T prevFlushState = DG_FLUSH_STATE_START; ///< Previous flush state. static U32 rsrvrFillStableTimeCounter = 0; ///< Reservoirs fill stable time counter. -static U32 overallFlushElapsedTime = 0; -static U32 stateTimer = 0; -static ALARM_ID_T alarm; -static DG_RESERVOIR_STATUS_T rsrvr1Status = NUM_OF_DG_RESERVOIR_STATUS; ///< Reservoir 1 status. -static DG_RESERVOIR_STATUS_T rsrvr2Status = NUM_OF_DG_RESERVOIR_STATUS; ///< Reservoir 2 status. -static BOOL isThisInitialDrain = TRUE; +static U32 overallFlushElapsedTime = 0; ///< Overall flush mode elapsed time. +static U32 stateTimer = 0; ///< State timer. +static ALARM_ID_T alarm; ///< Alarm ID. +static DG_RESERVOIR_STATUS_T rsrvr1Status = NUM_OF_DG_RESERVOIR_STATUS; ///< Reservoir 1 status. +static DG_RESERVOIR_STATUS_T rsrvr2Status = NUM_OF_DG_RESERVOIR_STATUS; ///< Reservoir 2 status. +static BOOL isThisInitialDrain = TRUE; ///< Initial drain boolean flag. static U32 dataPublishCounter = 0; ///< Flush data publish counter. // ********** private function prototypes ********** @@ -109,14 +112,25 @@ /*********************************************************************//** * @brief * The initFlushMode function initializes flush mode module. - * @details Inputs: none - * @details Outputs: Initialized flush mode module + * @details Inputs: flushState, prevFlushState, rsrvrFillStableTimeCounter, + * overallFlushElapsedTime, isThisInitialDrain, dataPublishCounter, + * rsrvr1Status, rsrvr2Status + * @details Outputs: flushState, prevFlushState, rsrvrFillStableTimeCounter, + * overallFlushElapsedTime, isThisInitialDrain, dataPublishCounter, + * rsrvr1Status, rsrvr2Status * @return none *************************************************************************/ void initFlushMode( void ) { - flushState = DG_FLUSH_STATE_START; - isThisInitialDrain = TRUE; + // Initialize the variables + flushState = DG_FLUSH_STATE_START; + prevFlushState = DG_FLUSH_STATE_START; + rsrvrFillStableTimeCounter = 0; + overallFlushElapsedTime = 0; + isThisInitialDrain = TRUE; + dataPublishCounter = 0; + rsrvr1Status = NUM_OF_DG_RESERVOIR_STATUS; + rsrvr2Status = NUM_OF_DG_RESERVOIR_STATUS; } /*********************************************************************//** @@ -140,8 +154,6 @@ *************************************************************************/ U32 execFlushMode( void ) { - //checkInletPressureFault(); TODO why do we need this? - // Execute current flush state switch ( flushState ) { @@ -199,6 +211,7 @@ break; } + // Publish the data publishFlushData(); return flushState; @@ -230,11 +243,22 @@ resetActuators(); // Transition to mode standby - requestNewOperationMode( DG_MODE_STAN ); + //requestNewOperationMode( DG_MODE_STAN ); + + requestNewOperationMode( DG_MODE_SOLO ); } // ********** private functions ********** +/*********************************************************************//** + * @brief + * The handleFlushModeStartState function handles the flush start state. + * If the sensors are in range, it transitions to the next state otherwise, + * it transitions to basic cancellation state. + * @details Inputs: stateTimer, rsrvr1Status + * @details Outputs: stateTimer, rsrvr1Status + * @return next state of the flush state machine + *************************************************************************/ static DG_FLUSH_STATE_T handleFlushModeStartState( void ) { DG_FLUSH_STATE_T state = DG_FLUSH_STATE_START; @@ -247,7 +271,7 @@ // Set all the actuators to reset and de-energized state resetActuators(); - if ( FALSE ) //TODO figure out the conductivity and pressure check + if ( FALSE ) //TODO figure out the conductivity and pressure range check { state = DG_FLUSH_STATE_CANCEL_BASIC_PATH; } @@ -274,6 +298,15 @@ return state; } +/*********************************************************************//** + * @brief + * The handleFlushModeDrainR1State function handles the drain reservoir 1. + * If the drain is completed within the defined time, it transitions to the + * next state, otherwise, it transitions to basic cancellation state. + * @details Inputs: stateTimer, rsrvr2Status, isThisInitialDrain + * @details Outputs: stateTimer, rsrvr2Status + * @return next state of the flush state machine + *************************************************************************/ static DG_FLUSH_STATE_T handleFlushModeDrainR1State( void ) { DG_FLUSH_STATE_T state = DG_FLUSH_STATE_DRAIN_R1; @@ -284,8 +317,11 @@ } else if ( rsrvr1Status == DG_RESERVOIR_REACHED_TARGET ) { - // Request a tare for reservoir 2 - tareReservoir(); + if ( TRUE == isThisInitialDrain ) + { + // Request a tare for reservoir 2 + tareReservoir(); + } // Set the actuators to drain R2 // NOTE: Drain pump is already on and VDr is already on drain state @@ -303,6 +339,15 @@ return state; } +/*********************************************************************//** + * @brief + * The handleFlushModeDrainR2State function handles the drain reservoir 2. + * If the drain is completed within the defined time, it transitions to the + * next state, otherwise, it transitions to basic cancellation state. + * @details Inputs: stateTimer, rsrvr2Status, isThisInitialDrain + * @details Outputs: none + * @return next state of the flush state machine + *************************************************************************/ static DG_FLUSH_STATE_T handleFlushModeDrainR2State( void ) { DG_FLUSH_STATE_T state = DG_FLUSH_STATE_DRAIN_R2; @@ -315,13 +360,27 @@ { signalDrainPumpHardStop(); - // Set the actuators to flush drain - setValveState( VPI, VALVE_STATE_OPEN ); - setValveState( VPD, VALVE_STATE_DRAIN_C_TO_NC ); + if ( TRUE == isThisInitialDrain ) + { + // Set the actuators to flush drain + setValveState( VPI, VALVE_STATE_OPEN ); + setValveState( VPD, VALVE_STATE_DRAIN_C_TO_NC ); - stateTimer = getMSTimerCount(); + state = DG_FLUSH_STATE_FLUSH_DRAIN; + } + else + { + signalDrainPumpHardStop(); + setValveState( VPI, VALVE_STATE_OPEN ); + setValveState( VPD, VALVE_STATE_DRAIN_C_TO_NC ); + setValveState( VRC, VALVE_STATE_RECIRC_C_TO_NC ); + setValveState( VDR, VALVE_STATE_RECIRC_C_TO_NC ); + setROPumpTargetFlowRate( RO_PUMP_TARGET_FLOW_RATE_LPM, RO_PUMP_MAX_PRESSURE_PSI ); - state = DG_FLUSH_STATE_FLUSH_DRAIN; + state = DG_FLUSH_STATE_FLUSH_CIRCULATION; + } + + stateTimer = getMSTimerCount(); } else if ( rsrvr2Status == DG_RESERVOIR_NOT_REACHED_TARGET ) { @@ -331,6 +390,14 @@ return state; } +/*********************************************************************//** + * @brief + * The handleFlushModeFlushDrainState function handles the flush drain state. + * Once the flush drain time has elapsed, it transitions to the next state. + * @details Inputs: stateTimer + * @details Outputs: stateTimer + * @return next state of the flush state machine + *************************************************************************/ static DG_FLUSH_STATE_T handleFlushModeFlushDrainState( void ) { DG_FLUSH_STATE_T state = DG_FLUSH_STATE_FLUSH_DRAIN; @@ -352,6 +419,16 @@ return state; } +/*********************************************************************//** + * @brief + * The handleFlushModeFlushDialysateState function handles the flush + * dialysate state. + * Once the flush dialysate time has elapsed, it transitions to the next + * state. + * @details Inputs: stateTimer + * @details Outputs: stateTimer + * @return next state of the flush state machine + *************************************************************************/ static DG_FLUSH_STATE_T handleFlushModeFlushDialysateState( void ) { DG_FLUSH_STATE_T state = DG_FLUSH_STATE_FLUSH_DIALYSATE; @@ -366,6 +443,16 @@ return state; } + +/*********************************************************************//** + * @brief + * The handleFlushModeFlushDialysateState function handles the flush + * dialysate state. Once the flush dialysate time has elapsed, it + * transitions to the next state. + * @details Inputs: rsrvr1Status, rsrvr2Status, stateTimer + * @details Outputs: rsrvr1Status, rsrvr2Status, stateTimer + * @return next state of the flush state machine + *************************************************************************/ static DG_FLUSH_STATE_T handleFlushModeFlushConcentrateStrawsState( void ) { DG_FLUSH_STATE_T state = DG_FLUSH_STATE_FLUSH_CONCENTRATE_STRAWS; @@ -386,6 +473,16 @@ return state; } +/*********************************************************************//** + * @brief + * The handleFlushModeFlushR1ToR2State function handles the flush + * reservoir 1 to reservoir 2 state. If any of the reservoirs flush within + * the defined period of time, it transitions to the next state, otherwise, + * it transitions to water cancellation state. + * @details Inputs: rsrvr1Status, rsrvr2Status, stateTimer + * @details Outputs: rsrvr1Status, rsrvr2Status, stateTimer + * @return next state of the flush state machine + *************************************************************************/ static DG_FLUSH_STATE_T handleFlushModeFlushR1ToR2State( void ) { DG_FLUSH_STATE_T state = DG_FLUSH_STATE_FLUSH_R1_TO_R2; @@ -410,7 +507,7 @@ setDrainPumpTargetRPM( DRAIN_PUMP_TARGET_RPM ); stateTimer = getMSTimerCount(); - // Set both reservoirs status + // Set both reservoirs' status rsrvr1Status = DG_RESERVOIR_ABOVE_TARGET; rsrvr2Status = DG_RESERVOIR_BELOW_TARGET; state = DG_FLUSH_STATE_FLUSH_R2_AND_DRAIN_R1; @@ -428,6 +525,18 @@ return state; } +/*********************************************************************//** + * @brief + * The handleFlushModeFlushR2AndDrainR1State function handles the flush + * reservoir 2 and drain reservoir 1 state. If reservoir 2 flushes within + * the defined period of time and reservoir 1 drains within the defined + * period of time, it transitions to the next state. If the flush times out, + * the function transitions to water cancellation state. If the drain times + * out the function transitions to basic cancellation state. + * @details Inputs: rsrvr1Status, rsrvr2Status, stateTimer + * @details Outputs: rsrvr1Status, rsrvr2Status, stateTimer + * @return next state of the flush state machine + *************************************************************************/ static DG_FLUSH_STATE_T handleFlushModeFlushR2AndDrainR1State( void ) { DG_FLUSH_STATE_T state = DG_FLUSH_STATE_FLUSH_R2_AND_DRAIN_R1; @@ -466,7 +575,8 @@ stateTimer = getMSTimerCount(); isThisInitialDrain = FALSE; - state = DG_FLUSH_STATE_DRAIN_R2; + rsrvr1Status = DG_RESERVOIR_ABOVE_TARGET; + state = DG_FLUSH_STATE_DRAIN_R1; } // TODO add timeout } @@ -475,10 +585,25 @@ return state; } +/*********************************************************************//** + * @brief + * The handleFlushModeFlushCirculationState function handles the flush + * circulation state. Once the flush circulation time has elapsed, it + * transitions to the next state. + * @details Inputs: none + * @details Outputs: none + * @return next state of the flush state machine + *************************************************************************/ static DG_FLUSH_STATE_T handleFlushModeFlushCirculationState( void ) { DG_FLUSH_STATE_T state = DG_FLUSH_STATE_FLUSH_CIRCULATION; + if ( didTimeout( stateTimer, FLUSH_CIRCULATION_WAIT_TIME_MS ) ) + { + resetActuators(); + state = DG_FLUSH_STATE_COMPLETE; + } + return state; } @@ -496,10 +621,19 @@ return state; } +/*********************************************************************//** + * @brief + * The handleFlushModeComplete function handles the complete state. + * @details Inputs: none + * @details Outputs: none + * @return next state of the flush state machine + *************************************************************************/ static DG_FLUSH_STATE_T handleFlushModeComplete( void ) { DG_FLUSH_STATE_T state = DG_FLUSH_STATE_COMPLETE; + stopDGFlush(); + return state; } @@ -537,11 +671,32 @@ stopTrimmerHeater(); } +/*********************************************************************//** + * @brief + * The setModeToFailed function sets the alarm that failed the flush mode. + * @details Inputs: alarm, prevHeatDisinfectState + * @details Outputs: none + * @return none + *************************************************************************/ static void setModeToFailed( void ) { SET_ALARM_WITH_1_U32_DATA( alarm, prevFlushState ) } +/*********************************************************************//** + * @brief + * The isRsrvrFull function checks whether the target reservoir is full or + * not. If the fill times out, the function sets the status to did not reach + * to target. + * @details Inputs: rsrvrFillStableTimeCounter, alarm, stateTimer, flushState, + * prevFlushState + * @details Outputs: rsrvrFillStableTimeCounter, alarm, stateTimer, + * prevFlushState + * @param r is DG_RESERVOIR_1 or DG_RESERVOIR_2 + * @param targetVol is the target fill volume + * @param timeout is the fill up time out that is checked against + * @return the status of the reservoirs during filling + *************************************************************************/ static DG_RESERVOIR_STATUS_T getRsrvrFillStatus( DG_RESERVOIR_ID_T r, F32 targetVol, U32 timeout ) { DG_RESERVOIR_STATUS_T status = DG_RESERVOIR_BELOW_TARGET; @@ -570,13 +725,30 @@ else if ( didTimeout( stateTimer, timeout ) ) { // Failed to fill on time + prevFlushState = flushState; alarm = ALARM_ID_DG_RESERVOIR_FILL_TIMEOUT; status = DG_RESERVOIR_NOT_REACHED_TARGET; } return status; } +/*********************************************************************//** + * @brief + * The getRsrvrDrainStatus function returns the status of draining a + * reservoir. If the drain times out, it set the status to did not reach + * target. + * @details Inputs: rsrvrFillStableTimeCounter, alarm, stateTimer, + * flushState, prevFlushState + * @details Outputs: rsrvrFillStableTimeCounter, alarm, stateTimer, + * prevFlushState + * @param r is DG_RESERVOIR_1 or DG_RESERVOIR_2 + * @param drainSteadyStateTimeout which is the time the reservoir's level + * does not change and is steady state + * @param timeout which is the timeout that a reservoir must be drained by + * then + * @return the status of the reservoirs during draining + *************************************************************************/ static DG_RESERVOIR_STATUS_T getRsrvrDrainStatus( DG_RESERVOIR_ID_T r, U32 drainSteadyStateTimeout, U32 timeout ) { DG_RESERVOIR_STATUS_T status = DG_RESERVOIR_ABOVE_TARGET; @@ -600,6 +772,14 @@ return status; } +/*********************************************************************//** + * @brief + * The publishFlushData function publishes the flush mode data at the set + * interval. + * @details Inputs: dataPublishCounter + * @details Outputs: dataPublishCounter + * @return: none + *************************************************************************/ static void publishFlushData( void ) { if ( ++dataPublishCounter > FLUSH_DATA_PUB_INTERVAL )