/************************************************************************** * * Copyright (c) 2024-2024 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 ModeGenPermeate.c * * @author (last) Michael Garthwaite * @date (last) 28-Feb-2025 * * @author (original) Michael Garthwaite * @date (original) 28-Feb-2025 * ***************************************************************************/ #include "BoostPump.h" #include "DDInterface.h" #include "FPModeStandby.h" #include "FPOperationModes.h" #include "Level.h" #include "MessageSupport.h" #include "Messaging.h" #include "ModeGenPermeate.h" #include "ModePreGenPermeate.h" #include "PermeateTank.h" #include "ROPump.h" #include "TaskGeneral.h" #include "Timers.h" #include "Valves.h" /** * @addtogroup FPGenPermeateMode * @{ */ // ********** private definitions ********** #define PRE_GEN_PERMEATE_DATA_PUBLISH_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the gen permeate mode data published. #define GEN_PERMEATE_BOOST_PUMP_TGT_PSI 25.0F ///< Pressure target in PSI for the boost pump during generate permeate mode. #define GEN_PERMEATE_RO_PUMP_TGT_ML 700 ///< Flow target in ml/min for the ro pump during generate permeate mode. // ********** private data ********** static FP_GENP_MODE_STATE_T genPermeateState; ///< Currently active generate Permeate state. static U32 genPermeateDataPublicationTimerCounter; ///< Used to schedule generate Permeate data publication to CAN bus. static OVERRIDE_U32_T genPermeateDataPublishInterval; ///< Generate permeate mode data publish interval. // ********** private function prototypes ********** static void publishGenPModeData( void ); static FP_GENP_MODE_STATE_T handleGenPTankFullState( void ); static FP_GENP_MODE_STATE_T handleGenPTankFillState( void ); static void setModeGenPTransition( FP_GENP_MODE_STATE_T state ); static U32 getGenPermeateDataPublishInterval( void ); /*********************************************************************//** * @brief * The initGenPermeateMode function initializes the water generation mode unit. * @details \b Inputs: none * @details \b Outputs: Gen water mode unit initialized * @return none *************************************************************************/ void initGenPermeateMode( void ) { genPermeateState = FP_GENP_TANK_FILL_STATE; genPermeateDataPublishInterval.data = PRE_GEN_PERMEATE_DATA_PUBLISH_INTERVAL; genPermeateDataPublishInterval.ovData = PRE_GEN_PERMEATE_DATA_PUBLISH_INTERVAL; genPermeateDataPublishInterval.ovInitData = 0; genPermeateDataPublishInterval.override = OVERRIDE_RESET; genPermeateDataPublicationTimerCounter = 0; } /*********************************************************************//** * @brief * The transitionToGenPermeateMode function prepares for transition to gen * permeate mode. * @details \b Inputs: genPermeateState * @details \b Outputs: none * @return initial state *************************************************************************/ U32 transitionToGenPermeateMode( void ) { initGenPermeateMode(); setCurrentSubState( genPermeateState ); setModeGenPTransition( genPermeateState ); startPermeateTankControl(); setROPumpTargetFlowRateMLPM( GEN_PERMEATE_RO_PUMP_TGT_ML ); return genPermeateState; } /*********************************************************************//** * @brief * The execGenPermeateMode function executes the Gen Permeate mode state machine. * @details \b Inputs: genPermeateState * @details \b Outputs: genPermeateState * @details \b Alarm: ALARM_ID_FP_SOFTWARE_FAULT when wrong gen Permeate state invoked. * @return current state. *************************************************************************/ U32 execGenPermeateMode( void ) { FP_GENP_MODE_STATE_T prevState = genPermeateState; // execute current gen Permeate state switch ( genPermeateState ) { case FP_GENP_TANK_FILL_STATE: genPermeateState = handleGenPTankFillState(); break; case FP_GENP_TANK_FULL_STATE: genPermeateState = handleGenPTankFullState(); break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_FP_SOFTWARE_FAULT, SW_FAULT_ID_FP_INVALID_GENP_STATE, genPermeateState ) break; } if ( prevState != genPermeateState ) { setModeGenPTransition( genPermeateState ); SEND_EVENT_WITH_2_U32_DATA( FP_EVENT_GENP_CHANGE, genPermeateState, prevState ) } //Publish Gen Permeate mode data publishGenPModeData(); return genPermeateState; } /*********************************************************************//** * @brief * The setModeGenPTransition function sets the actuators and variables * for the state transition in generate permeate mode. * @details Inputs: Valve states, Pump speed * @details Outputs: Actuate valves, pumps as desired. * @param state gen permeate state enum * @return none *************************************************************************/ static void setModeGenPTransition( FP_GENP_MODE_STATE_T state ) { // Execute on running state switch( state ) { case FP_GENP_TANK_FILL_STATE: if ( TRUE == isBoostPumpInstalled() ) { setBoostPumpTargetPressure( GEN_PERMEATE_BOOST_PUMP_TGT_PSI ); } break; case FP_GENP_TANK_FULL_STATE: if ( TRUE == isBoostPumpInstalled() ) { signalBoostPumpHardStop(); } break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_FP_SOFTWARE_FAULT, SW_FAULT_ID_FP_INVALID_GENP_STATE, state ) break; } } /*********************************************************************//** * @brief * The handleGenPTankFillState handles the fill state of gen water. * @details \b Inputs: none * @details \b Outputs: none * @return the next state of gen water mode *************************************************************************/ static FP_GENP_MODE_STATE_T handleGenPTankFillState( void ) { FP_GENP_MODE_STATE_T state = FP_GENP_TANK_FILL_STATE; PERMEATE_TANK_STATE_T permemeateTankState = getPermeateTankState(); if ( permemeateTankState == PERMEATE_TANK_FULL_STATE ) { state = FP_GENP_TANK_FULL_STATE; } return state; } /*********************************************************************//** * @brief * The handleGenPTankFullState handles the full state of gen permeate. * @details \b Inputs: stateDelayTime * @details \b Outputs: none * @return the next state of gen permeate mode *************************************************************************/ static FP_GENP_MODE_STATE_T handleGenPTankFullState( void ) { FP_GENP_MODE_STATE_T state = FP_GENP_TANK_FULL_STATE; PERMEATE_TANK_STATE_T permemeateTankState = getPermeateTankState(); if ( permemeateTankState == PERMEATE_TANK_FILL_STATE ) { state = FP_GENP_TANK_FILL_STATE; } return state; } /*********************************************************************//** * @brief * The getCurrentGenPermeateState function returns the current state of the * gen permeate mode. * @details \b Inputs: genPermeateState * @details \b Outputs: genPermeateState * @return the current state of gen permeate mode *************************************************************************/ FP_GENP_MODE_STATE_T getCurrentGenPermeateState( void ) { return genPermeateState; } /*********************************************************************//** * @brief * The getGenPermeateDataPublishInterval function gets the generate water * mode data publish interval. * @details \b Inputs: genPermeateDataPublishInterval * @details \b Outputs: none * @return the interval at generate permeate mode data being published. *************************************************************************/ static U32 getGenPermeateDataPublishInterval( void ) { U32 result = getU32OverrideValue( &genPermeateDataPublishInterval ); return result; } /*********************************************************************//** * @brief * The publishGenPModeData function broadcasts the generate water * mode data at defined interval. * @details \b Inputs: genPermeateDataPublicationTimerCounter * @details \b Outputs: FP generate permeate data broadcast message sent * @details \b Message \Sent: MSG_ID_FP_GEN_WATER_MODE_DATA to publish the * generate water mode data. * @return none *************************************************************************/ static void publishGenPModeData( void ) { if ( ++genPermeateDataPublicationTimerCounter >= getGenPermeateDataPublishInterval() ) { GEN_PERMEATE_MODE_DATA_T data; data.genPermeateExecState = (U32)getCurrentGenPermeateState(); data.setFlowRate = (F32)getDDPermeateFlowRate(); broadcastData( MSG_ID_FP_GEN_WATER_MODE_DATA, COMM_BUFFER_OUT_CAN_FP_BROADCAST, (U08*)&data, sizeof( GEN_PERMEATE_MODE_DATA_T ) ); genPermeateDataPublicationTimerCounter = 0; } } /*********************************************************************//** * @brief * The requestGenWaterStart function handles an DD request to start (go to gen permeate mode). * @details \b Inputs: none * @details \b Outputs: none * @return TRUE if request accepted, FALSE if not. *************************************************************************/ BOOL requestGenWaterStart( void ) { BOOL result = TRUE; requestNewOperationMode( FP_MODE_GENP ); return result; } /*********************************************************************//** * @brief * The requestGenWaterStop function handles an DD request to stop (go to standby mode). * @details \b Inputs: none * @details \b Outputs: none * @return TRUE if request accepted, FALSE if not. *************************************************************************/ BOOL requestGenWaterStop( void ) { BOOL result = TRUE; signalROPumpHardStop(); if ( TRUE == isBoostPumpInstalled() ) { signalBoostPumpHardStop(); } requestNewOperationMode( FP_MODE_STAN ); return result; } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The testGenPermeateDataPublishIntervalOverride function overrides the * DD generate water mode data publish interval. * @details \b Inputs: genPermeateDataPublishInterval * @details \b Outputs: genPermeateDataPublishInterval * @param Override message from Dialin which includes the interval * (in ms) to override the FP generate permeate data publish interval to. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testGenPermeateDataPublishIntervalOverride( MESSAGE_T *message ) { BOOL result = u32BroadcastIntervalOverride( message, &genPermeateDataPublishInterval, TASK_GENERAL_INTERVAL ); return result; } /**@}*/