Index: firmware/App/Modes/ModeDrain.c =================================================================== diff -u -rebbb1f85550a1f9b8f946655f7b2b63f76fbf67d -rcea079b61dbd17b2ddaec99b1124248147d14e72 --- firmware/App/Modes/ModeDrain.c (.../ModeDrain.c) (revision ebbb1f85550a1f9b8f946655f7b2b63f76fbf67d) +++ firmware/App/Modes/ModeDrain.c (.../ModeDrain.c) (revision cea079b61dbd17b2ddaec99b1124248147d14e72) @@ -17,6 +17,7 @@ #include "ConductivitySensors.h" #include "DrainPump.h" +#include "Heaters.h" #include "ModeDrain.h" #include "OperationModes.h" #include "Pressures.h" @@ -33,19 +34,26 @@ // ********** private definitions ********** -#define TARGET_DRAIN_PUMP_RPM 1800 ///< Target drain pump speed (in RPM). -#define DRAIN_WEIGHT_UNCHANGE_TIMEOUT ( 2 * MS_PER_SECOND ) ///< Time period of unchanged weight during draining before timeout. +#define TARGET_DRAIN_PUMP_RPM 2100 ///< Target drain pump speed (in RPM). +#define DRAIN_WEIGHT_UNCHANGE_TIMEOUT ( 2 * MS_PER_SECOND ) ///< Time period of unchanged weight during draining before timeout. +/// Time period to wait after drain complete and before taring load cells. +#define DRAIN_EMPTY_TARE_WAIT ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) #define TARGET_RO_PRESSURE_PSI 130 ///< Target pressure for RO pump. #define TARGET_RO_FLOW_RATE_L 0.3 ///< Target flow rate for RO pump. +#define DELAY_RES_DRAIN_VALVE_MS 1000 ///< Delay reservoir drain valve open by 1 second. +#define DELAY_DRAIN_PUMP_MS 2000 ///< Delay drain pump on by 2 seconds. + // ********** private data ********** -static DG_DRAIN_STATE_T drainState = DG_DRAIN_STATE_START; ///< Currently active drain state. +static DG_DRAIN_STATE_T drainState; ///< Currently active drain state. +static U32 drainEmptyTareTimerCtr; ///< Timer counter for delay between drain complete and load cell tare. // ********** private function prototypes ********** static DG_DRAIN_STATE_T handleDrainState( void ); +static DG_DRAIN_STATE_T handleTareState( void ); /*********************************************************************//** * @brief @@ -57,6 +65,7 @@ void initDrainMode( void ) { drainState = DG_DRAIN_STATE_START; + drainEmptyTareTimerCtr = 0; } /*********************************************************************//** @@ -71,10 +80,27 @@ // re-initialize each time we transition to drain mode initDrainMode(); +#ifndef V_2_SYSTEM + DG_RESERVOIR_ID_T inactiveReservoir = getInactiveReservoir(); + + if ( DG_RESERVOIR_1 == inactiveReservoir ) + { + setValveStateDelayed( VRD1, VALVE_STATE_OPEN, DELAY_RES_DRAIN_VALVE_MS ); + } + else if ( DG_RESERVOIR_2 == inactiveReservoir ) + { + setValveStateDelayed( VRD2, VALVE_STATE_OPEN, DELAY_RES_DRAIN_VALVE_MS ); + } +#endif // set initial actuator states setValveState( VDR, VALVE_STATE_DRAIN_C_TO_NO ); - setDrainPumpTargetRPM( TARGET_DRAIN_PUMP_RPM ); + setDrainPumpTargetRPMDelayed( TARGET_DRAIN_PUMP_RPM, DELAY_DRAIN_PUMP_MS ); + + // NOTE: The target flow rate should be set prior to setting the start primary heater + // because the initial guess in the heaters driver needs the target flow to calculate + // the new PWMs for the main and small primary heaters setROPumpTargetFlowRate( TARGET_RO_FLOW_RATE_L, TARGET_RO_PRESSURE_PSI ); + startPrimaryHeater(); } /*********************************************************************//** @@ -96,13 +122,20 @@ switch ( drainState ) { case DG_DRAIN_STATE_START: - drainState = DG_DRAIN_STATE_DRAIN; + if ( TRUE == isDrainPumpOn() ) + { + drainState = DG_DRAIN_STATE_DRAIN; + } break; case DG_DRAIN_STATE_DRAIN: drainState = handleDrainState(); break; + case DG_DRAIN_STATE_TARE: + drainState = handleTareState(); + break; + default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_DRAIN_MODE_INVALID_EXEC_STATE, drainState ) drainState = DG_DRAIN_STATE_START; @@ -114,9 +147,10 @@ /*********************************************************************//** * @brief - * The handleDrainState function handles the drain state of the drain mode state machine. + * The handleDrainState function handles the drain state of the drain mode + * state machine. * @details Inputs: none - * @details Outputs: Drain out from reservoir + * @details Outputs: none * @return the next state *************************************************************************/ static DG_DRAIN_STATE_T handleDrainState( void ) @@ -125,10 +159,57 @@ DG_RESERVOIR_ID_T inactiveReservoir = getInactiveReservoir(); // if we have reached our target drain to volume (by weight) or cannot drain anymore, we are done draining - go back to re-circ mode - if ( hasTargetDrainVolumeBeenReached( inactiveReservoir, DRAIN_WEIGHT_UNCHANGE_TIMEOUT ) ) + if ( TRUE == hasTargetDrainVolumeBeenReached( inactiveReservoir, DRAIN_WEIGHT_UNCHANGE_TIMEOUT ) ) { setDrainPumpTargetRPM( 0 ); + +#ifndef V_2_SYSTEM + if ( DG_RESERVOIR_1 == inactiveReservoir ) + { + setValveState( VRD1, VALVE_STATE_CLOSED ); + } + else if ( DG_RESERVOIR_2 == inactiveReservoir ) + { + setValveState( VRD2, VALVE_STATE_CLOSED ); + } +#endif + + if ( TRUE == isReservoirTarePending() ) + { // Tare reservoir load cells at empty if requested + result = DG_DRAIN_STATE_TARE; + } + else + { + requestNewOperationMode( DG_MODE_CIRC ); + } + } + + return result; +} + +/*********************************************************************//** + * @brief + * The handleTareState function handles the tare state of the drain mode + * state machine. + * @details Inputs: drainEmptyTareTimerCtr + * @details Outputs: drainEmptyTareTimerCtr + * @return the next state + *************************************************************************/ +static DG_DRAIN_STATE_T handleTareState( void ) +{ + DG_DRAIN_STATE_T result = DG_DRAIN_STATE_TARE; + DG_RESERVOIR_ID_T inactiveReservoir = getInactiveReservoir(); + + if ( ++drainEmptyTareTimerCtr > DRAIN_EMPTY_TARE_WAIT ) + { + drainEmptyTareTimerCtr = 0; + tareLoadCellsAtEmpty( inactiveReservoir ); requestNewOperationMode( DG_MODE_CIRC ); + +#ifndef V_2_SYSTEM + setValveState( VRD1, VALVE_STATE_CLOSED ); + setValveState( VRD2, VALVE_STATE_CLOSED ); +#endif } return result;