/**********************************************************************//** * * 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. * **************************************************************************/ #include "CPLD.h" #include "OperationModes.h" #include "SystemComm.h" #include "Timers.h" #include "ModeStandby.h" /** * @addtogroup StandbyMode * @{ */ // ********** private definitions ********** #define WATER_SAMPLE_TIME_MS ( 10 * MS_PER_SECOND ) ///< Duration of water sample state (in ms). /// 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. STANDBY_MODE_STATE_SAMPLE_WATER, ///< Sample water standby mode state. NUM_OF_STANDBY_MODE_STATES ///< Number of standby mode states. } STANDBY_MODE_STATE_T; // ********** private data ********** static STANDBY_MODE_STATE_T standbyState = 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 ********** static STANDBY_MODE_STATE_T handleStandbyIdleState( void ); static STANDBY_MODE_STATE_T handleStandbySampleWaterState( void ); /*********************************************************************//** * @brief * The initStandbyMode function initializes the Standby Mode module. * @details * Inputs : none * Outputs : Standby Mode module initialized. * @return none *************************************************************************/ void initStandbyMode( void ) { standbyState = STANDBY_MODE_STATE_START; pendingSampleWaterRequest = FALSE; pendingStartDGRequest = FALSE; waterSampleStartTime = 0; } /*********************************************************************//** * @brief * The transitionToStandbyMode function prepares for transition to standby mode. * @details * Inputs : none * Outputs : * @return none *************************************************************************/ void transitionToStandbyMode( void ) { // re-initialize standby mode each time we transition to standby mode initStandbyMode(); // TODO - set initial actuator states } /*********************************************************************//** * @brief * The execStandbyMode function executes the Standby Mode state machine. * @details * Inputs : none * Outputs : * @return none *************************************************************************/ void execStandbyMode( void ) { // execute current Standby state switch ( standbyState ) { case STANDBY_MODE_STATE_START: standbyState = STANDBY_MODE_STATE_IDLE; break; case STANDBY_MODE_STATE_IDLE: standbyState = handleStandbyIdleState(); break; case 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; break; } } /*********************************************************************//** * @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 STANDBY_MODE_STATE_T handleStandbyIdleState( void ) { STANDBY_MODE_STATE_T result = 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( MODE_SOLO ); } // 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 = STANDBY_MODE_STATE_SAMPLE_WATER; } else if ( TRUE == pendingStartDGRequest ) { pendingStartDGRequest = FALSE; requestNewOperationMode( 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 STANDBY_MODE_STATE_T handleStandbySampleWaterState( void ) { STANDBY_MODE_STATE_T result = 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 = 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 ( 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 ( STANDBY_MODE_STATE_IDLE == standbyState ) { result = TRUE; pendingStartDGRequest = TRUE; } return result; } /**@}*/