/************************************************************************** * * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * @file ModeDrain.c * * @author (last) Quang Nguyen * @date (last) 24-Aug-2020 * * @author (original) Leonardo Baloa * @date (original) 20-Dec-2019 * ***************************************************************************/ #include "ConductivitySensors.h" #include "DrainPump.h" #include "ModeDrain.h" #include "OperationModes.h" #include "Pressures.h" #include "Reservoirs.h" #include "ROPump.h" #include "TaskGeneral.h" #include "TemperatureSensors.h" #include "Valves.h" /** * @addtogroup DGDrainMode * @{ */ // ********** private definitions ********** #define TARGET_DRAIN_PUMP_RPM 2100 ///< Target drain pump speed (in RPM). #define DRAIN_WEIGHT_UNCHANGE_TIMEOUT ( 15 * MS_PER_SECOND ) ///< Time period of unchanged weight during draining before timeout. #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. // ********** private data ********** static DG_DRAIN_STATE_T drainState = DG_DRAIN_STATE_START; ///< Currently active drain state. static U32 tempTrans = 0; // ********** private function prototypes ********** static DG_DRAIN_STATE_T handleDrainState( void ); /*********************************************************************//** * @brief * The initDrainMode function initializes the drain mode module. * @details Inputs: none * @details Outputs: drainState * @return none *************************************************************************/ void initDrainMode( void ) { drainState = DG_DRAIN_STATE_START; } /*********************************************************************//** * @brief * The transitionToDrainMode function prepares for transition to drain mode. * @details Inputs: none * @details Outputs: Drain mode initialized * @return none *************************************************************************/ void transitionToDrainMode( void ) { // 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 ) { setValveState( VRD1, VALVE_STATE_OPEN ); } else if ( DG_RESERVOIR_2 == inactiveReservoir ) { setValveState( VRD2, VALVE_STATE_OPEN ); } #endif*/ // TODO test code remove DG_RESERVOIR_ID_T inactiveReservoir = getInactiveReservoir(); tempTrans = 0; setValveState( VDR, VALVE_STATE_DRAIN_C_TO_NO ); // TODO test code remove // set initial actuator states //setValveState( VDR, VALVE_STATE_DRAIN_C_TO_NO ); TODO uncomment //setDrainPumpTargetRPM( TARGET_DRAIN_PUMP_RPM ); //TODO comment this for testing // 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(); } /*********************************************************************//** * @brief * The execDrainMode function executes the drain mode state machine. * @details Inputs: drainState * @details Outputs: Check water quality, drain mode state machine executed * @return current state. *************************************************************************/ U32 execDrainMode( void ) { // check inlet water conductivity, temperature, pressure, and RO rejection ratio checkInletWaterConductivity(); checkInletWaterTemperature(); checkInletPressure(); checkRORejectionRatio(); // execute current drain state switch ( drainState ) { case DG_DRAIN_STATE_START: drainState = DG_DRAIN_STATE_DRAIN; break; case DG_DRAIN_STATE_DRAIN: drainState = handleDrainState(); 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; break; } return drainState; } /*********************************************************************//** * @brief * The handleDrainState function handles the drain state of the drain mode state machine. * @details Inputs: none * @details Outputs: Drain out from reservoir * @return the next state *************************************************************************/ static DG_DRAIN_STATE_T handleDrainState( void ) { DG_DRAIN_STATE_T result = DG_DRAIN_STATE_DRAIN; // TODO test code remove DG_RESERVOIR_ID_T inactiveReservoir = getInactiveReservoir(); if ( ++tempTrans == 20 ) { if ( DG_RESERVOIR_2 == inactiveReservoir ) { setValveState( VRD2, VALVE_STATE_OPEN ); } else if ( DG_RESERVOIR_1 == inactiveReservoir ) { setValveState( VRD1, VALVE_STATE_OPEN ); } } else if ( tempTrans == 40 ) { setDrainPumpTargetRPM( TARGET_DRAIN_PUMP_RPM ); } // TODO test code remove // 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 ( TRUE == hasTargetDrainVolumeBeenReached( inactiveReservoir, DRAIN_WEIGHT_UNCHANGE_TIMEOUT ) ) { setDrainPumpTargetRPM( 0 ); requestNewOperationMode( DG_MODE_CIRC ); #ifndef V_2_SYSTEM setValveState( VRD1, VALVE_STATE_CLOSED ); setValveState( VRD2, VALVE_STATE_CLOSED ); #endif } return result; } /*********************************************************************//** * @brief * The getCurrentDrainState function returns the current state of the drain mode. * @details Inputs: drainState * @details Outputs: none * @return the current state of drain mode. *************************************************************************/ DG_DRAIN_STATE_T getCurrentDrainState( void ) { return drainState; } /**@}*/