Index: firmware/App/Modes/ModeGenIdle.c =================================================================== diff -u -r94a190522ce398399c7b93c59f788d7666ec0060 -ree866f853290962da6811d58888f910f60f3bbe3 --- firmware/App/Modes/ModeGenIdle.c (.../ModeGenIdle.c) (revision 94a190522ce398399c7b93c59f788d7666ec0060) +++ firmware/App/Modes/ModeGenIdle.c (.../ModeGenIdle.c) (revision ee866f853290962da6811d58888f910f60f3bbe3) @@ -1,15 +1,15 @@ /************************************************************************** * -* Copyright (c) 2019-2021 Diality Inc. - All Rights Reserved. +* Copyright (c) 2021-2022 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 ModeGenIdle.c * -* @author (last) Dara Navaei -* @date (last) 06-Nov-2021 +* @author (last) Michael Garthwaite +* @date (last) 21-Nov-2022 * * @author (original) Quang Nguyen * @date (original) 06-Aug-2021 @@ -20,13 +20,18 @@ #include "ConductivitySensors.h" #include "DrainPump.h" #include "FPGA.h" +#include "CPLD.h" +#include "HDDefs.h" #include "Heaters.h" +#include "ModeFill.h" #include "ModeGenIdle.h" #include "NVDataMgmt.h" #include "OperationModes.h" #include "Pressures.h" +#include "Reservoirs.h" #include "ROPump.h" #include "SystemComm.h" +#include "SystemCommMessages.h" #include "TaskGeneral.h" #include "TemperatureSensors.h" #include "Timers.h" @@ -40,25 +45,40 @@ // ********** 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_RO_PRESSURE_PSI 130 ///< Target pressure for RO pump. +#define TARGET_RO_FLOW_RATE_L 0.3F ///< Target flow rate for RO pump. +#define BAD_FLUSH_FILL_TARGET_VOLUME_ML 1000 ///< Target fill volume in the bad flush fill state. +#define HD_LOST_COMM_TIMEOUT_MS (5 * SEC_PER_MIN * MS_PER_SECOND ) ///< The time of HD lost comm before DG transition back to standby. +#define BAD_FILL_SUBSTATES_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the bad fill sub-states is published on the CAN bus. +#define DATA_PUBLISH_COUNTER_START_COUNT 61 ///< Data publish counter start count. -#define TARGET_FLUSH_LINES_RO_FLOW_RATE_L 0.3 ///< Target flow rate for RO pump. - -/// The time of HD lost comm before DG transition back to standby. -#define HD_LOST_COMM_TIMEOUT_MS (5 * SEC_PER_MIN * MS_PER_SECOND ) - // ********** private data ********** -static DG_GEN_IDLE_MODE_STATE_T genIdleState; ///< Currently active generation idle state. -static F32 flushLinesVolumeL = 0.0; ///< Volume of water pumped by RO pump during flush lines state. -static U32 hdLostCommStartTime_ms = 0; ///< Lost communication with HD start time in ms. +static DG_GEN_IDLE_MODE_STATE_T genIdleState; ///< Currently active generation idle state. +// NOTE: the bad fill state must be initialized here and not in the transition function since in case of a bad fill, the transition function is called +// several times to drain and fill and handle a bad fill. +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T badFillState = DG_HANDLE_BAD_FILL_STATE_START; ///< Initialize bad fill sub-state. +static U32 hdLostCommStartTime_ms; ///< Lost communication with HD start time in ms. +static U32 targetFillVolumeML; ///< Save the target fill volume before calling startFillCmd(). +static BOOL handleBadFillFlag; ///< Internal signal flag to handle bad fill. +static OVERRIDE_U32_T badFillSubstatesPublishInterval = { BAD_FILL_SUBSTATES_PUB_INTERVAL, + BAD_FILL_SUBSTATES_PUB_INTERVAL, + 0, 0 }; ///< Interval (in ms) at which to publish bad fill sub-states to CAN bus. // ********** private function prototypes ********** -static DG_GEN_IDLE_MODE_STATE_T handleFlushLinesState( void ); +static DG_GEN_IDLE_MODE_STATE_T handleIdleStartState( void ); static DG_GEN_IDLE_MODE_STATE_T handleFlushWaterState( void ); +static DG_GEN_IDLE_MODE_STATE_T handleBadFillState( void ); +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleStartState( void ); +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleFirstDrainState( void ); +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleFlushFillState( void ); +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleSecondDrainState( void ); +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleRefillState( void ); + +static void publishBadFillSubstates( U32 badFill ); + /*********************************************************************//** * @brief * The initGenIdleMode function initializes the generation idle mode module. @@ -68,8 +88,7 @@ *************************************************************************/ void initGenIdleMode( void ) { - genIdleState = DG_GEN_IDLE_MODE_STATE_START; - flushLinesVolumeL = 0.0; + genIdleState = DG_GEN_IDLE_MODE_STATE_START; hdLostCommStartTime_ms = 0; } @@ -94,6 +113,9 @@ setValveState( VRC, VALVE_STATE_DRAIN_C_TO_NO ); setValveState( VDR, VALVE_STATE_DRAIN_C_TO_NO ); setValveState( VPO, VALVE_STATE_NOFILL_C_TO_NO ); + setValveState( VRF, VALVE_STATE_R1_C_TO_NC ); + setValveState( VRI, VALVE_STATE_R1_C_TO_NO ); + setValveState( VRO, VALVE_STATE_R2_C_TO_NC ); signalDrainPumpHardStop(); requestConcentratePumpOff( CONCENTRATEPUMPS_CP1_ACID ); @@ -106,17 +128,60 @@ // NOTE: The target flow rate should be set prior to setting the start primary heater // because the initial guess in the heaters driver needs the target flow to calculate // the new PWMs for the main and small primary heaters -#ifndef DISABLE_FLOW_CONTROL_TREATMENT - setROPumpTargetFlowRate( TARGET_FLUSH_LINES_RO_FLOW_RATE_L, TARGET_RO_PRESSURE_PSI ); - setHeaterTargetTemperature( DG_PRIMARY_HEATER, 39.0 ); // TODO remove this line. It comes form HD this for testing only + setROPumpTargetFlowRateLPM( TARGET_RO_FLOW_RATE_L, TARGET_RO_PRESSURE_PSI ); + setHeaterTargetTemperature( DG_PRIMARY_HEATER, getPrimaryHeaterTargetTemperature() ); startHeater( DG_PRIMARY_HEATER ); -#endif + setCPLDCleanLEDColor( CPLD_CLEAN_LED_OFF ); + return genIdleState; } /*********************************************************************//** * @brief + * The getCurrentGenIdleState function returns the current state of the + * generation idle mode. + * @details Inputs: genIdleState + * @details Outputs: none + * @return the current state of generation idle mode + *************************************************************************/ +DG_GEN_IDLE_MODE_STATE_T getCurrentGenIdleState( void ) +{ + return genIdleState; +} + +/*********************************************************************//** + * @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 setBadAvgConductivityDetectedFlag function sets a flag to indicate + * that bad average conductivity is detected. + * @details Inputs: none + * @details Outputs: none + * @param flag to TRUE if bad avg conductivity otherwise FALSE + *************************************************************************/ +void setBadAvgConductivityDetectedFlag( BOOL badAvgConducitivyflag ) +{ + handleBadFillFlag = badAvgConducitivyflag; + badFillState = DG_HANDLE_BAD_FILL_STATE_START; // Reset bad fill state too so we start at beginning +} + +/*********************************************************************//** + * @brief * The execGenIdleMode function executes the generation idle mode state machine. * @details Inputs: genIdleState * @details Outputs: Check water quality, generation idle mode state machine executed @@ -127,7 +192,7 @@ // Check inlet water conductivity, temperature, pressure, and RO rejection ratio checkInletWaterConductivity(); checkInletWaterTemperature(); - checkInletPressure(); + checkInletWaterPressure(); checkRORejectionRatio(); // Transition to standby mode when HD is not communicating @@ -147,17 +212,17 @@ switch ( genIdleState ) { case DG_GEN_IDLE_MODE_STATE_START: - genIdleState = DG_GEN_IDLE_MODE_STATE_FLUSH_LINES; + genIdleState = handleIdleStartState(); break; - case DG_GEN_IDLE_MODE_STATE_FLUSH_LINES: - genIdleState = handleFlushLinesState(); - break; - case DG_GEN_IDLE_MODE_STATE_FLUSH_WATER: genIdleState = handleFlushWaterState(); break; + case DG_GEN_IDLE_MODE_STATE_HANDLE_BAD_FILL: + genIdleState = handleBadFillState(); + break; + default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_GEN_IDLE_MODE_INVALID_EXEC_STATE, genIdleState ) genIdleState = DG_GEN_IDLE_MODE_STATE_START; @@ -169,73 +234,274 @@ /*********************************************************************//** * @brief - * The getCurrentGenIdleState function returns the current state of the - * generation idle mode. - * @details Inputs: genIdleState + * The handleIdleStartState function executes the start state of the + * generation idle mode state machine. + * @details Inputs: none * @details Outputs: none - * @return the current state of generation idle mode + * @return the next state *************************************************************************/ -DG_GEN_IDLE_MODE_STATE_T getCurrentGenIdleState( void ) +static DG_GEN_IDLE_MODE_STATE_T handleIdleStartState( void ) { - return genIdleState; + DG_GEN_IDLE_MODE_STATE_T result = DG_GEN_IDLE_MODE_STATE_START; + + if ( TRUE == handleBadFillFlag ) + { + result = DG_GEN_IDLE_MODE_STATE_HANDLE_BAD_FILL; + } + else + { + badFillState = DG_HANDLE_BAD_FILL_STATE_START; + result = DG_GEN_IDLE_MODE_STATE_FLUSH_WATER; + } + + return result; } /*********************************************************************//** * @brief - * The requestDGStop function handles an HD request to stop (return to standby mode). + * The handleFlushWaterState function executes the flush water state + * generation idle mode state machine. * @details Inputs: none - * @details Outputs: DG standby mode requested - * @return TRUE if request accepted, FALSE if not. + * @details Outputs: none + * @return the next state *************************************************************************/ -BOOL requestDGStop( void ) +static DG_GEN_IDLE_MODE_STATE_T handleFlushWaterState( void ) { - BOOL result = TRUE; + DG_GEN_IDLE_MODE_STATE_T result = DG_GEN_IDLE_MODE_STATE_FLUSH_WATER; - requestNewOperationMode( DG_MODE_STAN ); - return result; } /*********************************************************************//** * @brief - * The handleFlushLinesState function executes the flush lines state of the + * The handleBadFillState function executes the bad fill state of the * generation idle mode state machine. - * @details Inputs: flushLinesVolumeL - * @details Outputs: Integrate volume of water moved through line + * @details Inputs: none + * @details Outputs: badFillState * @return the next state *************************************************************************/ -static DG_GEN_IDLE_MODE_STATE_T handleFlushLinesState( void ) +static DG_GEN_IDLE_MODE_STATE_T handleBadFillState( void ) { - DG_GEN_IDLE_MODE_STATE_T result = DG_GEN_IDLE_MODE_STATE_FLUSH_LINES; - F32 waterFlowRate = getMeasuredROFlowRate(); - F32 waterVolume = ( ( waterFlowRate / SEC_PER_MIN ) / ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ); + DG_GEN_IDLE_MODE_STATE_T result = DG_GEN_IDLE_MODE_STATE_HANDLE_BAD_FILL; - // Integrate volume of water moved through line - flushLinesVolumeL += waterVolume; - - // When enough water volume has flowed to flush the lines, transition to flush water state - if ( flushLinesVolumeL >= getFlushLineVolume() ) + // Execute current bad fill state + switch ( badFillState ) { -#ifndef DISABLE_FLOW_CONTROL_TREATMENT - setROPumpTargetFlowRate( TARGET_RO_FLOW_RATE_L, TARGET_RO_PRESSURE_PSI ); -#endif - result = DG_GEN_IDLE_MODE_STATE_FLUSH_WATER; + case DG_HANDLE_BAD_FILL_STATE_START: + badFillState = handleStartState(); + break; + + case DG_HANDLE_BAD_FILL_STATE_FIRST_DRAIN: + badFillState = handleFirstDrainState(); + break; + + case DG_HANDLE_BAD_FILL_STATE_FLUSH_FILL: + badFillState = handleFlushFillState(); + break; + + case DG_HANDLE_BAD_FILL_STATE_SECOND_DRAIN: + badFillState = handleSecondDrainState(); + break; + + case DG_HANDLE_BAD_FILL_STATE_REFILL: + badFillState = handleRefillState(); + break; + + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_GEN_IDLE_MODE_INVALID_EXEC_STATE, genIdleState ) + badFillState = DG_HANDLE_BAD_FILL_STATE_START; + break; } return result; } /*********************************************************************//** * @brief - * The handleFlushWaterState function executes the flush water state - * of the generation idle mode state machine. + * The handleStartState function executes the start state of the handle bad + * fill state machine. * @details Inputs: none * @details Outputs: none * @return the next state *************************************************************************/ -static DG_GEN_IDLE_MODE_STATE_T handleFlushWaterState( void ) +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleStartState( void ) { - return DG_GEN_IDLE_MODE_STATE_FLUSH_WATER; + DG_GEN_IDLE_MODE_BAD_FILL_STATE_T state = DG_HANDLE_BAD_FILL_STATE_FIRST_DRAIN; + + // Publish the state prior to requesting a drain and transitioning to drain mode + publishBadFillSubstates( state ); + + // Drain the bad filled reservoir first + requestNewOperationMode( DG_MODE_DRAI ); + + return state; } +/*********************************************************************//** + * @brief + * The handleFirstDrainState function executes the first drain state of the + * handle bad fill state machine. + * @details Inputs: none + * @details Outputs: none + * @return the next state + *************************************************************************/ +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleFirstDrainState( void ) +{ + DG_GEN_IDLE_MODE_BAD_FILL_STATE_T state = DG_HANDLE_BAD_FILL_STATE_FIRST_DRAIN; + + // Check if the alarm has been cleared by the user and if yes, continue with the fill + if ( FALSE == isAlarmActive( ALARM_ID_FILL_CONDUCTIVITY_OUT_OF_RANGE ) ) + { + // Save the HD target fill volume before command 1000 mL fill volume + targetFillVolumeML = getTargetFillVolumeML(); + + state = DG_HANDLE_BAD_FILL_STATE_FLUSH_FILL; + + // Publish the state prior to transitioning to fill mode + publishBadFillSubstates( state ); + + // Start the flush fill + startFillCmd( BAD_FLUSH_FILL_TARGET_VOLUME_ML, getTargetFillFlowRateLPM() ); + } + else + { + publishBadFillSubstates( state ); + } + + return state; +} + +/*********************************************************************//** + * @brief + * The handleFlushFillState function executes the flush fill state of the + * handle bad fill state machine. + * @details Inputs: none + * @details Outputs: none + * @return the next state + *************************************************************************/ +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleFlushFillState( void ) +{ + DG_GEN_IDLE_MODE_BAD_FILL_STATE_T state = DG_HANDLE_BAD_FILL_STATE_SECOND_DRAIN; + + // Publish the state prior to transitioning to drain mode + publishBadFillSubstates( state ); + + requestNewOperationMode( DG_MODE_DRAI ); + + return state; +} + +/*********************************************************************//** + * @brief + * The handleSecondDrainState function executes the second drain state of the + * handle bad fill state machine. + * @details Inputs: none + * @details Outputs: none + * @return the next state + *************************************************************************/ +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleSecondDrainState( void ) +{ + DG_GEN_IDLE_MODE_BAD_FILL_STATE_T state = DG_HANDLE_BAD_FILL_STATE_REFILL; + + // Publish the state prior to transitioning to fill mode + publishBadFillSubstates( state ); + + // Refill to the saved target fill volume (~1500 mL) + startFillCmd( targetFillVolumeML, getTargetFillFlowRateLPM() ); + + return state; +} + +/*********************************************************************//** + * @brief + * The handleRefillState function executes refill state of the handle bad + * fill state machine. + * @details Inputs: none + * @details Outputs: none + * @return the next state + *************************************************************************/ +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleRefillState( void ) +{ + DG_GEN_IDLE_MODE_BAD_FILL_STATE_T state = DG_HANDLE_BAD_FILL_STATE_START; + + publishBadFillSubstates( state ); + + // Clear wait for dialysate alarm condition to allow resume + clearAlarmCondition( ALARM_ID_CREATING_DIALYSATE_PLEASE_WAIT ); // resume option will appear + // Set flag to FALSE here so next call to idle exec will move to normal flush water state + handleBadFillFlag = FALSE; + + return state; +} + +/*********************************************************************//** + * @brief + * The publishBadFillSubstates function publishes idle mode bad fill + * sub-states at the set interval. + * @details Inputs: none + * @details Outputs: none + * @param badFill the bad fill state that has to be published + * @return none + *************************************************************************/ +static void publishBadFillSubstates( U32 badFill ) +{ + GEN_IDLE_BAD_FILL_STATE data; + + data.badFillState = badFill; + + broadcastData( MSG_ID_DG_BAD_FILL_SUB_STATE, COMM_BUFFER_OUT_CAN_DG_BROADCAST, (U08*)&data, sizeof( GEN_IDLE_BAD_FILL_STATE ) ); +} + + +/************************************************************************* + * TEST SUPPORT FUNCTIONS + *************************************************************************/ + + +/*********************************************************************//** + * @brief + * The testSetBadFillSubstatesPublishIntervalOverride function overrides the + * bad fill sub-states publish interval. + * @details Inputs: badFillSubstatesPublishInterval + * @details Outputs: badFillSubstatesPublishInterval + * @param: value override bad fill sub-states publish interval with (in ms) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetGenIdleSubstatesPublishIntervalOverride( U32 value ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + U32 intvl = value / TASK_GENERAL_INTERVAL; + badFillSubstatesPublishInterval.ovData = intvl; + badFillSubstatesPublishInterval.override = OVERRIDE_KEY; + result = TRUE; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetBadFillSubstatesPublishIntervalOverride function resets the + * override of the bad fill sub-states publish interval. + * @details Inputs: badFillSubstatesPublishInterval + * @details Outputs: badFillSubstatesPublishInterval + * @return TRUE if override reset successful, FALSE if not + *************************************************************************/ +BOOL testResetGenIdleSubstatesPublishIntervalOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + badFillSubstatesPublishInterval.override = OVERRIDE_RESET; + badFillSubstatesPublishInterval.ovData = badFillSubstatesPublishInterval.ovInitData; + result = TRUE; + } + + return result; +} + /**@}*/