Index: firmware/App/Modes/ModeGenIdle.c =================================================================== diff -u -re0cdf49eb0f54239e5d765282e0952cea7ded1bd -r66cc826068b54bc436cde0fae70a05ba6c1ac974 --- firmware/App/Modes/ModeGenIdle.c (.../ModeGenIdle.c) (revision e0cdf49eb0f54239e5d765282e0952cea7ded1bd) +++ firmware/App/Modes/ModeGenIdle.c (.../ModeGenIdle.c) (revision 66cc826068b54bc436cde0fae70a05ba6c1ac974) @@ -9,7 +9,7 @@ * @file ModeGenIdle.c * * @author (last) Dara Navaei -* @date (last) 16-Jan-2022 +* @date (last) 31-Mar-2022 * * @author (original) Quang Nguyen * @date (original) 06-Aug-2021 @@ -21,13 +21,15 @@ #include "DrainPump.h" #include "FPGA.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" @@ -41,25 +43,39 @@ // ********** private definitions ********** -#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 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.3F ///< 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. +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 genIdlePublicationTimerCounter; ///< Used to schedule bad fill sub-states publication to CAN bus. +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, ///< Interval (in ms) at which to publish bad fill sub-states to CAN bus. + BAD_FILL_SUBSTATES_PUB_INTERVAL, + 0, 0 }; // ********** 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 ); // This state has sub-states 1.0 to 1.4 +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleFirstDrainState( void ); // idle 1.0 +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleFlushFillState( void ); // idle 1.1 +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleSecondDrainState( void ); // idle 1.2 +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleRefillState( void ); // idle 1.3 +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleClearAlarmState( void ); // idle 1.4 + +static void publishBadFillSubstates( void ); + /*********************************************************************//** * @brief * The initGenIdleMode function initializes the generation idle mode module. @@ -69,9 +85,11 @@ *************************************************************************/ void initGenIdleMode( void ) { - genIdleState = DG_GEN_IDLE_MODE_STATE_START; - flushLinesVolumeL = 0.0; - hdLostCommStartTime_ms = 0; + genIdleState = DG_GEN_IDLE_MODE_STATE_START; + hdLostCommStartTime_ms = 0; + targetFillVolumeML = 0; + genIdlePublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; + handleBadFillFlag = FALSE; } /*********************************************************************//** @@ -108,7 +126,7 @@ // 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 - setROPumpTargetFlowRateLPM( TARGET_FLUSH_LINES_RO_FLOW_RATE_L, TARGET_RO_PRESSURE_PSI ); + setROPumpTargetFlowRateLPM( TARGET_RO_FLOW_RATE_L, TARGET_RO_PRESSURE_PSI ); setHeaterTargetTemperature( DG_PRIMARY_HEATER, getPrimaryHeaterTargetTemperature() ); startHeater( DG_PRIMARY_HEATER ); #endif @@ -118,6 +136,47 @@ /*********************************************************************//** * @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; +} +/*********************************************************************//** + * @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 @@ -148,95 +207,289 @@ 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; break; } + publishBadFillSubstates(); + return (U32)genIdleState; } /*********************************************************************//** * @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 = getMeasuredROFlowRateLPM(); - 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; + // Execute current bad fill state + switch ( badFillState ) + { + case DG_HANDLE_BAD_FILL_STATE_START: + badFillState = DG_HANDLE_BAD_FILL_STATE_FIRST_DRAIN; + break; - // When enough water volume has flowed to flush the lines, transition to flush water state - if ( flushLinesVolumeL >= getFlushLineVolumeL() ) + case DG_HANDLE_BAD_FILL_STATE_FIRST_DRAIN: // idle 1.0 + badFillState = handleFirstDrainState(); + break; + + case DG_HANDLE_BAD_FILL_STATE_FLUSH_FILL: // idle 1.1 + badFillState = handleFlushFillState(); + break; + + case DG_HANDLE_BAD_FILL_STATE_SECOND_DRAIN: // idle 1.2. + badFillState = handleSecondDrainState(); + break; + + case DG_HANDLE_BAD_FILL_STATE_REFILL: + badFillState = handleRefillState(); // idle 1.3 + break; + + case DG_HANDLE_BAD_FILL_STATE_CLEAR_ALARM: + badFillState = handleClearAlarmState(); // idle 1.4 + 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 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 result = DG_HANDLE_BAD_FILL_STATE_FLUSH_FILL; // after first drain is completed, go to next bad fill sub-state + + requestNewOperationMode( DG_MODE_DRAI ); // go to drain mode to empty bad dialysate because this is a bad fill + + return result; +} + +/*********************************************************************//** + * @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 result = DG_HANDLE_BAD_FILL_STATE_FLUSH_FILL; + + if ( FALSE == isAlarmActive( ALARM_ID_FILL_CONDUCTIVITY_OUT_OF_RANGE ) ) // alarm is no longer active - cleared by user { -#ifndef DISABLE_FLOW_CONTROL_TREATMENT - setROPumpTargetFlowRateLPM( TARGET_RO_FLOW_RATE_L, TARGET_RO_PRESSURE_PSI ); -#endif - result = DG_GEN_IDLE_MODE_STATE_FLUSH_WATER; + targetFillVolumeML = getTargetFillVolumeML(); // save the HD target fill volume before command 1000 mL fill volume + startFillCmd( BAD_FLUSH_FILL_TARGET_VOLUME_ML, getTargetFillFlowRateLPM() ); + + if ( TRUE == handleBadFillFlag ) + { + result = DG_HANDLE_BAD_FILL_STATE_FIRST_DRAIN; // (idle 1.0) + } + else + { + result = DG_HANDLE_BAD_FILL_STATE_SECOND_DRAIN; // (idle 1.2) + } } return result; } /*********************************************************************//** * @brief - * The handleFlushWaterState function executes the flush water state - * of the generation idle mode state machine. + * 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_STATE_T handleFlushWaterState( void ) +static DG_GEN_IDLE_MODE_BAD_FILL_STATE_T handleSecondDrainState( void ) { - return DG_GEN_IDLE_MODE_STATE_FLUSH_WATER; + DG_GEN_IDLE_MODE_BAD_FILL_STATE_T result = DG_HANDLE_BAD_FILL_STATE_REFILL; // after second drain completed, go to refill sub-state (idle 1.3) + + requestNewOperationMode( DG_MODE_DRAI ); // go to drain mode to empty bad dialysate because this is a bad fill + + return result; } +/*********************************************************************//** + * @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 result = DG_HANDLE_BAD_FILL_STATE_CLEAR_ALARM; // (idle 1.4) + + startFillCmd( targetFillVolumeML, getTargetFillFlowRateLPM() ); // refill to the saved target fill volume (~1500 mL) + + return result; +} + +/*********************************************************************//** + * @brief + * The handleClearAlarmState function executes the clear alarm 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 handleClearAlarmState( void ) +{ + DG_GEN_IDLE_MODE_BAD_FILL_STATE_T result = DG_HANDLE_BAD_FILL_STATE_START; + + // clear wait for dialysate alarm condition to allow resume + clearAlarmCondition( ALARM_ID_CREATING_DIALYSATE_PLEASE_WAIT ); // resume option will appear + handleBadFillFlag = FALSE; // set flag to FALSE here so next call to idle exec will move to normal flush water state + genIdleState = DG_GEN_IDLE_MODE_STATE_START; + + return result; +} + +/*********************************************************************//** + * @brief + * The publishBadFillSubstates function publishes idle mode bad fill + * sub-states at the set interval. + * @details Inputs: genIdlePublicationTimerCounter + * @details Outputs: genIdlePublicationTimerCounter + * @return none + *************************************************************************/ +static void publishBadFillSubstates( void ) +{ + // publish bad fill sub-states on interval + if ( ++genIdlePublicationTimerCounter >= getU32OverrideValue( &badFillSubstatesPublishInterval ) ) + { + GEN_IDLE_BAD_FILL_STATE data; + + data.badFillState = (U32)badFillState; + + broadcastData( MSG_ID_DG_BAD_FILL_SUB_STATE, COMM_BUFFER_OUT_CAN_DG_BROADCAST, (U08*)&data, sizeof( GEN_IDLE_BAD_FILL_STATE ) ); + genIdlePublicationTimerCounter = 0; + } +} + + +/************************************************************************* + * 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 testSetBadFillSubstatesPublishIntervalOverride( 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 testResetBadFillSubstatesPublishIntervalOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + badFillSubstatesPublishInterval.override = OVERRIDE_RESET; + badFillSubstatesPublishInterval.ovData = badFillSubstatesPublishInterval.ovInitData; + result = TRUE; + } + + return result; +} + /**@}*/