/************************************************************************** * * 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 ModeRecirculate.c * * @author (last) Quang Nguyen * @date (last) 24-Aug-2020 * * @author (original) Sean * @date (original) 04-Apr-2020 * ***************************************************************************/ #include "etpwm.h" #include "ConcentratePumps.h" #include "ConductivitySensors.h" #include "DrainPump.h" #include "FPGA.h" #include "Heaters.h" #include "ModeRecirculate.h" #include "OperationModes.h" #include "Pressures.h" #include "ROPump.h" #include "TaskGeneral.h" #include "TemperatureSensors.h" #include "Timers.h" #include "Valves.h" /** * @addtogroup DGRecirculateMode * @{ */ // ********** private definitions ********** #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 TARGET_FLUSH_LINES_RO_FLOW_RATE_L 0.6 ///< Target flow rate for RO pump. #define FLUSH_LINES_VOLUME_L 0.1 ///< Water volume (in Liters) to flush when starting re-circulate mode. // ********** private data ********** static DG_RECIRCULATE_MODE_STATE_T recircState; ///< Currently active re-circulation state. static F32 flushLinesVolumeL = 0.0; ///< Volume of water pumped by RO pump during flush lines state. // ********** private function prototypes ********** static DG_RECIRCULATE_MODE_STATE_T handleFlushLinesState( void ); static DG_RECIRCULATE_MODE_STATE_T handleRecircWaterState( void ); static DG_RECIRCULATE_MODE_STATE_T handleRecircPauseState( void ); /*********************************************************************//** * @brief * The initRecirculateMode function initializes the re-circulate mode module. * @details Inputs: none * @details Outputs: Re-circulate mode module initialized * @return none *************************************************************************/ void initRecirculateMode( void ) { recircState = DG_RECIRCULATE_MODE_STATE_START; flushLinesVolumeL = 0.0; } /*********************************************************************//** * @brief * The transitionToRecirculateMode function prepares for transition to re-circulate mode. * @details Inputs: none * @details Outputs: Re-initialized re-circulate mode * @return none *************************************************************************/ void transitionToRecirculateMode( void ) { // re-initialize each time we transition to re-circulate mode initRecirculateMode(); // set initial actuator states setValveState( VSP, VALVE_STATE_CLOSED ); setValveState( VPI, VALVE_STATE_OPEN ); setValveState( VRC, VALVE_STATE_DRAIN_C_TO_NO ); setValveState( VDR, VALVE_STATE_DRAIN_C_TO_NO ); setValveState( VPO, VALVE_STATE_NOFILL_C_TO_NO ); setROPumpTargetFlowRate( TARGET_FLUSH_LINES_RO_FLOW_RATE_L, TARGET_RO_PRESSURE_PSI ); signalDrainPumpHardStop(); startPrimaryHeater(); requestConcentratePumpsOff( CONCENTRATEPUMPS_CP1 ); requestConcentratePumpsOff( CONCENTRATEPUMPS_CP2 ); // UV on #ifndef _VECTORCAST_ { // TODO - test code to start the fan since we're turning the heater on F32 fanPWM = 0.25; etpwmSetCmpA( etpwmREG6, (U32)( (S32)( ( fanPWM * (F32)(etpwmREG6->TBPRD) ) + FLOAT_TO_INT_ROUNDUP_OFFSET ) ) ); etpwmSetCmpB( etpwmREG6, (U32)( (S32)( ( fanPWM * (F32)(etpwmREG6->TBPRD) ) + FLOAT_TO_INT_ROUNDUP_OFFSET ) ) ); } #endif } /*********************************************************************//** * @brief * The execRecirculateMode function executes the re-circulate mode state machine. * @details Inputs: recircState * @details Outputs: Check water quality, re-circulate mode state machine executed * @return current state *************************************************************************/ U32 execRecirculateMode( void ) { // check inlet water conductivity, temperature, pressure, and RO rejection ratio checkInletWaterConductivity(); checkInletWaterTemperature(); checkInletPressure(); checkRORejectionRatio(); // execute current re-circulate state switch ( recircState ) { case DG_RECIRCULATE_MODE_STATE_START: recircState = DG_RECIRCULATE_MODE_STATE_FLUSH_LINES; break; case DG_RECIRCULATE_MODE_STATE_FLUSH_LINES: recircState = handleFlushLinesState(); break; case DG_RECIRCULATE_MODE_STATE_RECIRC_WATER: recircState = handleRecircWaterState(); break; case DG_RECIRCULATE_MODE_STATE_PAUSE: recircState = handleRecircPauseState(); break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, 0, recircState ) // TODO - add s/w fault enum to 1st data param recircState = DG_RECIRCULATE_MODE_STATE_START; break; } return (U32)recircState; } /*********************************************************************//** * @brief * The handleFlushLinesState function executes the flush lines state of the * re-circulate mode state machine. * @details Inputs: none * @details Outputs: Integrate volume of water moved through line * @return the next state *************************************************************************/ static DG_RECIRCULATE_MODE_STATE_T handleFlushLinesState( void ) { DG_RECIRCULATE_MODE_STATE_T result = DG_RECIRCULATE_MODE_STATE_FLUSH_LINES; 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 ) { setValveState( VDR, VALVE_STATE_RECIRC_C_TO_NC ); setROPumpTargetFlowRate( TARGET_RO_FLOW_RATE_L, TARGET_RO_PRESSURE_PSI ); result = DG_RECIRCULATE_MODE_STATE_RECIRC_WATER; } return result; } /*********************************************************************//** * @brief * The handleRecircWaterState function executes the re-circulate water state * of the re-circulate mode state machine. * @details Inputs: none * @details Outputs: none * @return the next state *************************************************************************/ static DG_RECIRCULATE_MODE_STATE_T handleRecircWaterState( void ) { DG_RECIRCULATE_MODE_STATE_T result = DG_RECIRCULATE_MODE_STATE_RECIRC_WATER; return result; } /*********************************************************************//** * @brief * The handleRecircPauseState function executes the pause state of the * re-circulate mode state machine. * @details Inputs: none * @details Outputs: none * @return the next state *************************************************************************/ static DG_RECIRCULATE_MODE_STATE_T handleRecircPauseState( void ) { DG_RECIRCULATE_MODE_STATE_T result = DG_RECIRCULATE_MODE_STATE_PAUSE; return result; } /*********************************************************************//** * @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 getCurrentRecirculateState function returns the current state of the * re-circulate mode. * @details Inputs: recircState * @details Outputs: none * @return the current state of re-circulate mode *************************************************************************/ DG_RECIRCULATE_MODE_STATE_T getCurrentRecirculateState( void ) { return recircState; } /**@}*/