Index: firmware/App/Modes/ModeStandby.c =================================================================== diff -u -r499e5de29e706d09f79ba22511068990c4044e84 -r0dec8744af40d0c87a6d7cd1923920c1c2bd1d2f --- firmware/App/Modes/ModeStandby.c (.../ModeStandby.c) (revision 499e5de29e706d09f79ba22511068990c4044e84) +++ firmware/App/Modes/ModeStandby.c (.../ModeStandby.c) (revision 0dec8744af40d0c87a6d7cd1923920c1c2bd1d2f) @@ -1,64 +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 DGStandbyMode + * @{ + */ + +// ********** private definitions ********** + +#define WATER_SAMPLE_TIME_MS ( 10 * MS_PER_SECOND ) ///< Duration of water sample state (in ms). + // ********** private data ********** +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 ********** -/************************************************************************* - * @brief initStandbyMode +static DG_STANDBY_MODE_STATE_T handleStandbyIdleState( void ); +static DG_STANDBY_MODE_STATE_T handleStandbySampleWaterState( void ); + +/*********************************************************************//** + * @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 ) { + // 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 DG_STANDBY_MODE_STATE_START: + standbyState = DG_STANDBY_MODE_STATE_IDLE; + break; + 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_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 : + * @return the next state + *************************************************************************/ +static DG_STANDBY_MODE_STATE_T handleStandbySampleWaterState( void ) +{ + DG_STANDBY_MODE_STATE_T result = DG_STANDBY_MODE_STATE_SAMPLE_WATER; + + // 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; + } + + 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; +} + +/**@}*/