Index: firmware/App/Modes/ModeFlush.c =================================================================== diff -u -reb6220e0641dc3aa3e924972186caedf3d564bf3 -rd7926685f2fe3086bab183166119f0965a192a69 --- firmware/App/Modes/ModeFlush.c (.../ModeFlush.c) (revision eb6220e0641dc3aa3e924972186caedf3d564bf3) +++ firmware/App/Modes/ModeFlush.c (.../ModeFlush.c) (revision d7926685f2fe3086bab183166119f0965a192a69) @@ -18,6 +18,7 @@ #include "DrainPump.h" #include "Heaters.h" #include "LoadCell.h" +#include "ModeFault.h" #include "ModeFlush.h" #include "OperationModes.h" #include "Pressures.h" @@ -37,47 +38,55 @@ // ********** private definitions ********** // 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 DRAIN_PUMP_TARGET_RPM 1800 ///< Drain pump target RPM during drain. +#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 DRAIN_PUMP_TARGET_RPM 1800 ///< Drain pump target RPM during drain. // Drain R1 & R2 states defines -#define DRAIN_WEIGHT_UNCHANGE_TIMEOUT ( 6 * MS_PER_SECOND ) ///< Time period of unchanged weight during draining before timeout. +#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 // Flush dialysate state defines -#define FLUSH_DIALYSATE_WAIT_TIME_MS ( 0.5 * 60 * MS_PER_SECOND ) ///< Flush dialysate wait time in milliseconds. +#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 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 minutes -#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 ( 3 * 60 * MS_PER_SECOND ) ///< Reservoirs 1 & 2 drain timeout in ms. TODO original value was 2 minutes +#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 minutes +#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 ( 3 * 60 * MS_PER_SECOND ) ///< Reservoirs 1 & 2 drain timeout in ms. TODO original value was 2 minutes +#define FINAL_DRAIN_RO_PUMP_FLOW_LPM 0.6 ///< Final drain RO pump flow rate in L/min. This is used to flush the drain line during drain. +// Flush drain line state define +#define FLUSH_LINES_VOLUME_L 0.1 ///< Water volume to flush in liters. + // Flush circulation state defines -#define FLUSH_CIRCULATION_WAIT_TIME_MS ( 0.5 * 60 * MS_PER_SECOND ) ///< Flush circulation wait time in milliseconds. +#define FLUSH_CIRCULATION_WAIT_TIME_MS ( 0.5 * 60 * MS_PER_SECOND ) ///< Flush circulation wait time in milliseconds. +// Flush with fresh water state defines +#define FLUSH_WITH_FRESH_WATER_WAIT_TIME_MS ( 0.5 * 60 * MS_PER_SECOND ) ///< Flush with fresh water wait time in milliseconds. + // ********** private data ********** -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 overallFlushElapsedTimeStart = 0; ///< Overall flush mode elapsed time start. -static U32 stateTimerStart = 0; ///< State timer start. -static ALARM_ID_T alarmDetectedPendingTrigger; ///< Alarm ID that is detected and is pending to be triggered. -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. -static BOOL hasWaterCancellationBeenSet = FALSE; ///< Water cancellation set/not set boolean flag. +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 overallFlushElapsedTimeStart = 0; ///< Overall flush mode elapsed time start. +static U32 stateTimerStart = 0; ///< State timer start. +static ALARM_ID_T alarmDetectedPendingTrigger; ///< Alarm ID that is detected and is pending to be triggered. +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. +static BOOL hasWaterCancellationBeenSet = FALSE; ///< Water cancellation set/not set boolean flag. +static F32 flushLinesVolumeL = 0.0; ///< Volume of water pumped by RO pump during flush lines state. // ********** private function prototypes ********** @@ -89,12 +98,13 @@ static DG_FLUSH_STATE_T handleFlushModeFlushConcentrateStrawsState( void ); static DG_FLUSH_STATE_T handleFlushModeFlushR1ToR2State( void ); static DG_FLUSH_STATE_T handleFlushModeFlushR2AndDrainR1State( void ); +static DG_FLUSH_STATE_T handleFlushModeFlushDrainLineState( void ); static DG_FLUSH_STATE_T handleFlushModeFlushCirculationState( void ); +static DG_FLUSH_STATE_T handleFlushModeFlushWithFreshWaterState( void ); static DG_FLUSH_STATE_T handleFlushModeCancelBasicPathState( void ); static DG_FLUSH_STATE_T handleFlushModeCancelWaterPathState( void ); static DG_FLUSH_STATE_T handleFlushModeComplete( void ); -static void resetActuators( void ); static void failFlushMode( void ); static DG_RESERVOIR_STATUS_T getRsrvrFillStatus( DG_RESERVOIR_ID_T r, F32 targetVol, U32 timeout ); static DG_RESERVOIR_STATUS_T getRsrvrDrainStatus( DG_RESERVOIR_ID_T r, U32 drainSteadyStateTimeout, U32 timeout ); @@ -133,7 +143,7 @@ void transitionToFlushMode( void ) { // Reset all the actuators - resetActuators(); + deenergizeActuators(); initFlushMode(); } @@ -182,10 +192,18 @@ flushState = handleFlushModeFlushR2AndDrainR1State(); break; + case DG_FLUSH_STATE_FLUSH_DRAIN_LINE: + flushState = handleFlushModeFlushDrainLineState(); + break; + case DG_FLUSH_STATE_FLUSH_CIRCULATION: flushState = handleFlushModeFlushCirculationState(); break; + case DG_FLUSH_STATE_FLUSH_WITH_FRESH_WATER: + flushState = handleFlushModeFlushWithFreshWaterState(); + break; + case DG_FLUSH_STATE_CANCEL_BASIC_PATH: flushState = handleFlushModeCancelBasicPathState(); break; @@ -238,7 +256,7 @@ if ( DG_MODE_FLUS == getCurrentOperationMode() ) { // Reset all the actuators - resetActuators(); + deenergizeActuators(); // Transition to mode standby requestNewOperationMode( DG_MODE_STAN ); @@ -357,15 +375,7 @@ } else { - // Set the actuators to transition to flush circulation - setValveState( VPI, VALVE_STATE_OPEN ); - setValveState( VPD, VALVE_STATE_DRAIN_C_TO_NC ); - setValveState( VPO, VALVE_STATE_NOFILL_C_TO_NO ); - 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_CIRCULATION; + state = DG_FLUSH_STATE_FLUSH_DRAIN_LINE; } stateTimerStart = getMSTimerCount(); @@ -584,20 +594,85 @@ /*********************************************************************//** * @brief + * The handleFlushModeFlushDrainLineState function handles the flush + * drain line state. Once the drain line was flushed with sufficient water, + * it transitions to the next state. + * @details Inputs: stateTimerStart, flushLinesVolumeL + * @details Outputs: stateTimerStart, flushLinesVolumeL + * @return next state of the flush state machine + *************************************************************************/ +static DG_FLUSH_STATE_T handleFlushModeFlushDrainLineState( void ) +{ + DG_FLUSH_STATE_T state = DG_FLUSH_STATE_FLUSH_DRAIN_LINE; + + F32 waterFlowRate = getMeasuredROFlowRate(); + F32 waterVolume = ( ( waterFlowRate / SEC_PER_MIN ) / ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ); + + // integrate volume of water moved through line + flushLinesVolumeL += waterVolume; + + // when enough water volume has flowed to flush the lines, transition to re-circ state + if ( flushLinesVolumeL >= FLUSH_LINES_VOLUME_L ) + { + // Set the actuators to transition to flush circulation + // The valves are set to do flush drain and flush circulation + 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 ); + + stateTimerStart = getMSTimerCount(); + + state = DG_FLUSH_STATE_FLUSH_CIRCULATION; + } + + 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 + * @details Inputs: stateTimerStart + * @details Outputs: stateTimerStart * @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( stateTimerStart, FLUSH_CIRCULATION_WAIT_TIME_MS ) ) + if ( TRUE == didTimeout( stateTimerStart, FLUSH_CIRCULATION_WAIT_TIME_MS ) ) { - resetActuators(); + setValveState( VPD, VALVE_STATE_OPEN_C_TO_NO ); + setValveState( VRC, VALVE_STATE_DRAIN_C_TO_NO ); + setValveState( VDR, VALVE_STATE_DRAIN_C_TO_NO ); + + stateTimerStart = getMSTimerCount(); + + state = DG_FLUSH_STATE_FLUSH_WITH_FRESH_WATER; + } + + return state; +} + +/*********************************************************************//** + * @brief + * The handleFlushModeFlushWithFreshWaterState function handles the + * flush with fresh water state. It runs the circulation state with fresh + * water for the defined period of time. + * @details Inputs: none + * @details Outputs: none + * @return next state of the flush state machine + *************************************************************************/ +static DG_FLUSH_STATE_T handleFlushModeFlushWithFreshWaterState( void ) +{ + DG_FLUSH_STATE_T state = DG_FLUSH_STATE_FLUSH_WITH_FRESH_WATER; + + if ( TRUE == didTimeout( stateTimerStart, FLUSH_WITH_FRESH_WATER_WAIT_TIME_MS ) ) + { + deenergizeActuators(); + state = DG_FLUSH_STATE_COMPLETE; } @@ -639,7 +714,7 @@ if ( FALSE == hasWaterCancellationBeenSet ) { // Stop all the actuators first then decide who should run next - resetActuators(); + deenergizeActuators(); // Set the actuators setValveState( VPI, VALVE_STATE_CLOSED ); @@ -680,7 +755,7 @@ // If reservoir 2 has already been drained and reservoir 1 is empty, reset and switch to complete if ( ( DG_RESERVOIR_REACHED_TARGET == rsrvr2Status ) && ( DG_RESERVOIR_ABOVE_TARGET == rsrvr1Status ) ) { - // If the cancellation water path cannot be done, got to basic cancellation path + // If the cancellation water path cannot be done, go to basic cancellation path rsrvr1Status = getRsrvrDrainStatus( DG_RESERVOIR_1, DRAIN_WEIGHT_UNCHANGE_TIMEOUT, RSRVRS_DRAIN_TIMEOUT_MS ); if ( DG_RESERVOIR_REACHED_TARGET == rsrvr1Status ) @@ -714,40 +789,6 @@ /*********************************************************************//** * @brief - * The resetActuators function sets all the actuators to reset and - * de-energized state. - * @details Inputs: none - * @details Outputs: none - * @return none - *************************************************************************/ -static void resetActuators( void ) -{ - // Turn off the UV reactors - turnOffUVReactor( INLET_UV_REACTOR ); - turnOffUVReactor( OUTLET_UV_REACTOR ); - - // De-energize all the valves - setValveState( VPI, VALVE_STATE_CLOSED ); - setValveState( VBF, VALVE_STATE_CLOSED ); - setValveState( VSP, VALVE_STATE_CLOSED ); - setValveState( VPD, VALVE_STATE_OPEN_C_TO_NO ); - setValveState( VPO, VALVE_STATE_NOFILL_C_TO_NO ); - setValveState( VDR, VALVE_STATE_DRAIN_C_TO_NO ); - setValveState( VRC, VALVE_STATE_DRAIN_C_TO_NO ); - setValveState( VRO, VALVE_STATE_R1_C_TO_NO ); - setValveState( VRD, VALVE_STATE_R2_C_TO_NO ); - setValveState( VRI, VALVE_STATE_R1_C_TO_NO ); - setValveState( VRF, VALVE_STATE_R2_C_TO_NO ); - - //TODO add the composition pumps - signalROPumpHardStop(); - signalDrainPumpHardStop(); - stopPrimaryHeater(); - stopTrimmerHeater(); -} - -/*********************************************************************//** - * @brief * The failFlushMode function sets the alarm that failed the flush mode. * @details Inputs: alarm, prevHeatDisinfectState * @details Outputs: none @@ -797,7 +838,7 @@ stateTimerStart = getMSTimerCount(); } } - else if ( didTimeout( stateTimerStart, timeout ) ) + else if ( TRUE == didTimeout( stateTimerStart, timeout ) ) { // Failed to fill on time prevFlushState = flushState; @@ -836,7 +877,7 @@ stateTimerStart = getMSTimerCount(); status = DG_RESERVOIR_REACHED_TARGET; } - else if ( didTimeout( stateTimerStart, timeout ) ) + else if ( TRUE == didTimeout( stateTimerStart, timeout ) ) { // Failed to drain on time prevFlushState = flushState;