Index: firmware/App/Modes/ModeStandby.c =================================================================== diff -u -rf43eb1e9e0803776ec7420b16e1db8760b020bd9 -r0dec8744af40d0c87a6d7cd1923920c1c2bd1d2f --- firmware/App/Modes/ModeStandby.c (.../ModeStandby.c) (revision f43eb1e9e0803776ec7420b16e1db8760b020bd9) +++ firmware/App/Modes/ModeStandby.c (.../ModeStandby.c) (revision 0dec8744af40d0c87a6d7cd1923920c1c2bd1d2f) @@ -1,126 +1,240 @@ -/**********************************************************************//** - * - * 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 ModeStandby.c - * - * @date 11-Dec-2019 - * @author L. Baloa - * - * @brief Top-level state machine for the standby mode. - * - **************************************************************************/ +/************************************************************************** +* +* 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 ModeStandby.c +* +* @author (last) Quang Nguyen +* @date (last) 21-Jul-2020 +* +* @author (original) Dara Navaei +* @date (original) 05-Nov-2019 +* +***************************************************************************/ +#include "CPLD.h" +#include "DrainPump.h" +#include "Heaters.h" #include "OperationModes.h" +#include "ROPump.h" +#include "SystemComm.h" +#include "Timers.h" +#include "Valves.h" #include "ModeStandby.h" -#include "CPLD.h" /** - * @addtogroup StandbyMode + * @addtogroup DGStandbyMode * @{ */ // ********** private definitions ********** -/// Enumeration of standby mode states. -typedef enum Standby_Mode_States -{ - STANDBY_MODE_STATE_START = 0, ///< Start standby mode state. - STANDBY_MODE_STATE_IDLE, ///< Idle standby mode state. - NUM_OF_STANDBY_MODE_STATES ///< Number of standby mode states. -} STANDBY_MODE_STATE_T; +#define WATER_SAMPLE_TIME_MS ( 10 * MS_PER_SECOND ) ///< Duration of water sample state (in ms). // ********** private data ********** -static STANDBY_MODE_STATE_T standbyState = STANDBY_MODE_STATE_START; ///< Currently active standby state. +static DG_STANDBY_MODE_STATE_T standbyState = DG_STANDBY_MODE_STATE_START; ///< Currently active standby state. +static BOOL pendingSampleWaterRequest = FALSE; ///< Flag indicating HD has requested a water sample. +static BOOL pendingStartDGRequest = FALSE; ///< Flag indicating HD has requested DG start (go to re-circulate mode). +static U32 waterSampleStartTime = 0; ///< Time stamp for start of water sample state. // ********** private function prototypes ********** -STANDBY_MODE_STATE_T handleStandbyIdleState( void ); +static DG_STANDBY_MODE_STATE_T handleStandbyIdleState( void ); +static DG_STANDBY_MODE_STATE_T handleStandbySampleWaterState( void ); /*********************************************************************//** - * @brief initStandbyMode + * @brief * The initStandbyMode function initializes the Standby Mode module. * @details * Inputs : none * Outputs : Standby Mode module initialized. - * @param none * @return none *************************************************************************/ void initStandbyMode( void ) { + standbyState = DG_STANDBY_MODE_STATE_START; + pendingSampleWaterRequest = FALSE; + pendingStartDGRequest = FALSE; + waterSampleStartTime = 0; } /*********************************************************************//** - * @brief transitionToStandbyMode + * @brief * The transitionToStandbyMode function prepares for transition to standby mode. * @details * Inputs : none * Outputs : - * @param none * @return none *************************************************************************/ void transitionToStandbyMode( void ) { - // reset to start state each time we transition to standby mode - standbyState = STANDBY_MODE_STATE_START; + // re-initialize standby mode each time we transition to standby mode + initStandbyMode(); + + // set initial actuator states + setValveState( VPI, VALVE_STATE_CLOSED ); + setValveState( VSP, VALVE_STATE_CLOSED ); + setValveState( VRC, VALVE_STATE_DRAIN_C_TO_NO ); + setValveState( VDR, VALVE_STATE_DRAIN_C_TO_NO ); + setValveState( VPO, VALVE_STATE_NOFILL_C_TO_NO ); + signalROPumpHardStop(); + signalDrainPumpHardStop(); + stopPrimaryHeater(); + stopTrimmerHeater(); + // conc. pumps off + // UV off } /*********************************************************************//** - * @brief execStandbyMode + * @brief * The execStandbyMode function executes the Standby Mode state machine. * @details * Inputs : none * Outputs : - * @param none - * @return none + * @return current state. *************************************************************************/ -void execStandbyMode( void ) +U32 execStandbyMode( void ) { // execute current Standby state switch ( standbyState ) { - case STANDBY_MODE_STATE_START: - standbyState = STANDBY_MODE_STATE_IDLE; + case DG_STANDBY_MODE_STATE_START: + standbyState = DG_STANDBY_MODE_STATE_IDLE; break; - case STANDBY_MODE_STATE_IDLE: + case DG_STANDBY_MODE_STATE_IDLE: standbyState = handleStandbyIdleState(); break; + case DG_STANDBY_MODE_STATE_SAMPLE_WATER: + standbyState = handleStandbySampleWaterState(); + break; + default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, 0, standbyState ) // TODO - add s/w fault enum to 1st data param - standbyState = STANDBY_MODE_STATE_START; + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, 0, standbyState ) // TODO - add s/w fault enum to 1st data param + standbyState = DG_STANDBY_MODE_STATE_START; break; } + + return standbyState; } /*********************************************************************//** * @brief * The handleStandbyIdleState function executes the Idle state of the \n * Standby Mode state machine. * @details + * Inputs : pendingSampleWaterRequest, pendingStartDGRequest + * Outputs : possibly op mode + * @return the next state + *************************************************************************/ +static DG_STANDBY_MODE_STATE_T handleStandbyIdleState( void ) +{ + DG_STANDBY_MODE_STATE_T result = DG_STANDBY_MODE_STATE_IDLE; + + // go to standby solo mode if HD is turned off or stops communicating. + if ( FALSE == isHDCommunicating() ) + { // TODO if HD comm loss, should we wait an hour or so before going to solo standby? +// requestNewOperationMode( DG_MODE_SOLO ); // TODO - uncomment when solo mode is implemented. + } + // if HD requests water sample, go to water sample state + else if ( TRUE == pendingSampleWaterRequest ) + { + pendingSampleWaterRequest = FALSE; + waterSampleStartTime = getMSTimerCount(); + // TODO - open VPi and VSP valves + result = DG_STANDBY_MODE_STATE_SAMPLE_WATER; + } + else if ( TRUE == pendingStartDGRequest ) + { + pendingStartDGRequest = FALSE; + requestNewOperationMode( DG_MODE_CIRC ); + } + + return result; +} + +/*********************************************************************//** + * @brief + * The handleStandbySampleWaterState function executes the sample water state \n + * of the Standby Mode state machine. + * @details * Inputs : none * Outputs : - * @param none * @return the next state *************************************************************************/ -STANDBY_MODE_STATE_T handleStandbyIdleState( void ) +static DG_STANDBY_MODE_STATE_T handleStandbySampleWaterState( void ) { - STANDBY_MODE_STATE_T result = STANDBY_MODE_STATE_IDLE; + DG_STANDBY_MODE_STATE_T result = DG_STANDBY_MODE_STATE_SAMPLE_WATER; - // go to standby or standby solo mode depending on whether HD is connected -// if ( FALSE == isHDCommunicating() ) // TODO - handle switching between standby and standby-solo modes -// { -// requestNewOperationMode( MODE_SOLO ); -// } + // VPi and VSP valves open for 10 seconds, then close and return to idle state + if ( TRUE == didTimeout( waterSampleStartTime, WATER_SAMPLE_TIME_MS ) ) + { + // TODO - close VPi and VSP valves. + result = DG_STANDBY_MODE_STATE_IDLE; + } - // TODO - what is DG supposed to be doing while in standby mode? + return result; +} +/*********************************************************************//** + * @brief + * The requestWaterSample function handles an HD request to sample water. + * @details + * Inputs : standbyState + * Outputs : pendingSampleWaterRequest + * @return TRUE if request accepted, FALSE if not. + *************************************************************************/ +BOOL requestWaterSample( void ) +{ + BOOL result = FALSE; + + if ( DG_STANDBY_MODE_STATE_IDLE == standbyState ) + { + result = TRUE; + pendingSampleWaterRequest = TRUE; + } + return result; } +/*********************************************************************//** + * @brief + * The requestDGStart function handles an HD request to start (go to re-circulate mode). + * @details + * Inputs : standbyState + * Outputs : pendingSampleWaterRequest + * @return TRUE if request accepted, FALSE if not. + *************************************************************************/ +BOOL requestDGStart( void ) +{ + BOOL result = FALSE; + + if ( DG_STANDBY_MODE_STATE_IDLE == standbyState ) + { + result = TRUE; + pendingStartDGRequest = TRUE; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The getCurrentStandbyState function returns the current state of the \n + * standby mode. + * @details + * Inputs : standbyState + * Outputs : none + * @return the current state of standby mode. + *************************************************************************/ +DG_STANDBY_MODE_STATE_T getCurrentStandbyState( void ) +{ + return standbyState; +} + /**@}*/