/************************************************************************** * * Copyright (c) 2019-2023 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 ModeDrain.c * * @author (last) Dara Navaei * @date (last) 21-Dec-2022 * * @author (original) Leonardo Baloa * @date (original) 20-Dec-2019 * ***************************************************************************/ #include "ConcentratePumps.h" #include "ConductivitySensors.h" #include "CPLD.h" #include "DrainPump.h" #include "Heaters.h" #include "ModeFill.h" #include "ModeDrain.h" #include "OperationModes.h" #include "Pressures.h" #include "Reservoirs.h" #include "ROPump.h" #include "SystemComm.h" #include "TaskGeneral.h" #include "TemperatureSensors.h" #include "Timers.h" #include "Valves.h" /** * @addtogroup DGDrainMode * @{ */ // ********** private definitions ********** #define TARGET_DRAIN_PUMP_RPM 2400 ///< Target drain pump speed (in RPM). #define DRAIN_WEIGHT_UNCHANGE_TIMEOUT ( 2 * MS_PER_SECOND ) ///< Time period of unchanged weight during draining before timeout. #define DRAIN_EMPTY_TARE_WAIT ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) /// Time period to wait after drain complete and before taring load cells. #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 DELAY_RES_DRAIN_VALVE_MS 1000 ///< Delay reservoir drain valve open by 1 second. #define DELAY_DRAIN_PUMP_MS 2000 ///< Delay drain pump on by 2 seconds. #define DIALYSATE_DRAIN_TIME_OUT ( 2 * SEC_PER_MIN * MS_PER_SECOND ) ///< Dialysate drain time out. #define RINSE_CONCENTRATE_LINES_WAIT ( 25 * MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Time period to wait for concentrate lines to rinse. #define RINSE_SPEED ( ( CONCENTRATE_PUMP_MAX_SPEED - 3.0F ) * -1.0F ) ///< Reserver the concentrate speed to rinse out concentrate lines. #define CONCENTRATION_BOTTLE_LOW_VOLUME_ML 100.0F ///< Concentration bottle low volume in mL. // ********** private data ********** static DG_DRAIN_STATE_T drainState; ///< Currently active drain state. static U32 drainEmptyTareTimerCtr; ///< Timer counter for delay between drain complete and load cell tare. static BOOL rinseConcentrateLines; ///< Flag indicates to rinse concentrate lines. static U32 rinseConcentrateLinesTimerCtr; ///< Timer counter for rinsing concentrate lines. static U32 dialysateDrainStartTime; ///< Dialysate drain start time. // ********** private function prototypes ********** static DG_DRAIN_STATE_T handleDrainStateStart( void ); static DG_DRAIN_STATE_T handleDrainStateDrain( void ); static DG_DRAIN_STATE_T handleDrainStateTare( void ); static DG_DRAIN_STATE_T handleRinseState( void ); /*********************************************************************//** * @brief * The initDrainMode function initializes the drain mode module. * @details Inputs: none * @details Outputs: drainState, drainEmptyTareTimerCtr, dialysateDrainStartTime, * rinseConcentrateLinesTimerCtr * @return none *************************************************************************/ void initDrainMode( void ) { // NOTE: rinseConcentrateLines should not be initialized here. This variable is set by the caller when a drain command is requested. // The caller will specify whether to set the rinse lines or not. drainState = DG_DRAIN_STATE_START; drainEmptyTareTimerCtr = 0; rinseConcentrateLinesTimerCtr = 0; dialysateDrainStartTime = 0; } /*********************************************************************//** * @brief * The transitionToDrainMode function prepares for transition to drain mode. * @details Inputs: none * @details Outputs: Drain mode initialized * @return initial state *************************************************************************/ U32 transitionToDrainMode( void ) { // re-initialize each time we transition to drain mode initDrainMode(); DG_RESERVOIR_ID_T inactiveReservoir = getInactiveReservoir(); if ( DG_RESERVOIR_1 == inactiveReservoir ) { setValveStateDelayed( VRD1, VALVE_STATE_OPEN, DELAY_RES_DRAIN_VALVE_MS ); } else if ( DG_RESERVOIR_2 == inactiveReservoir ) { setValveStateDelayed( VRD2, VALVE_STATE_OPEN, DELAY_RES_DRAIN_VALVE_MS ); } initDrainParameters( inactiveReservoir ); // set initial actuator states setValveState( VDR, VALVE_STATE_DRAIN_C_TO_NO ); setDrainPumpTargetRPMDelayed( TARGET_DRAIN_PUMP_RPM, DELAY_DRAIN_PUMP_MS ); // 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 setROPumpTargetFlowRateLPM( TARGET_RO_FLOW_RATE_L, TARGET_RO_PRESSURE_PSI ); setHeaterTargetTemperature( DG_PRIMARY_HEATER, getPrimaryHeaterTargetTemperature() ); startHeater( DG_PRIMARY_HEATER ); setCPLDCleanLEDColor( CPLD_CLEAN_LED_OFF ); return drainState; } /*********************************************************************//** * @brief * The execDrainMode function executes the drain mode state machine. * @details Inputs: drainState * @details Outputs: Check water quality, drain mode state machine executed * @return current state. *************************************************************************/ U32 execDrainMode( void ) { // check inlet water conductivity, temperature, pressure, and RO rejection ratio checkInletWaterConductivity(); checkInletWaterTemperature(); checkInletWaterPressure(); // execute current drain state switch ( drainState ) { case DG_DRAIN_STATE_START: drainState = handleDrainStateStart(); break; case DG_DRAIN_STATE_DRAIN: drainState = handleDrainStateDrain(); break; case DG_DRAIN_STATE_TARE: drainState = handleDrainStateTare(); break; case DG_DRAIN_STATE_RINSE: drainState = handleRinseState(); break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_DRAIN_MODE_INVALID_EXEC_STATE, drainState ) drainState = DG_DRAIN_STATE_START; break; } return drainState; } /*********************************************************************//** * @brief * The getCurrentDrainState function returns the current state of the drain mode. * @details Inputs: drainState * @details Outputs: none * @return the current state of drain mode. *************************************************************************/ DG_DRAIN_STATE_T getCurrentDrainState( void ) { return drainState; } /*********************************************************************//** * @brief * The signalDrainModeRinseConcentrateLines function sets the flag for drain * mode to rinse concentrate lines. * @details Inputs: none * @details Outputs: rinseConcentrateLines * @return none *************************************************************************/ void signalDrainModeRinseConcentrateLines( BOOL rinse ) { rinseConcentrateLines = rinse; } /*********************************************************************//** * @brief * The handleDrainStateStart function handles the drain start state of * the drain mode state machine. * @details Inputs: none * @details Outputs: dialysateDrainStartTime * @return the next state *************************************************************************/ static DG_DRAIN_STATE_T handleDrainStateStart( void ) { DG_DRAIN_STATE_T state = DG_DRAIN_STATE_START; if ( TRUE == isDrainPumpOn() ) { dialysateDrainStartTime = getMSTimerCount(); state = DG_DRAIN_STATE_DRAIN; } return state; } /*********************************************************************//** * @brief * The handleDrainStateDrain function handles the drain state of the drain * mode state machine. * @details Inputs: none * @details Outputs: none * @return the next state *************************************************************************/ static DG_DRAIN_STATE_T handleDrainStateDrain( void ) { DG_DRAIN_STATE_T result = DG_DRAIN_STATE_DRAIN; DG_RESERVOIR_ID_T inactiveReservoir = getInactiveReservoir(); // if we have reached our target drain to volume (by weight) or cannot drain anymore, we are done draining - go back to generation idle mode if ( TRUE == hasTargetDrainVolumeBeenReached( inactiveReservoir, DRAIN_WEIGHT_UNCHANGE_TIMEOUT ) ) { DG_ACID_CONCENTRATES_RECORD_T acid; F32 acidBottleVolML; DG_BICARB_CONCENTRATES_RECORD_T bicarb; F32 bicarbBottleVolML; getAcidConcentrateCalRecord( &acid ); getBicarbConcentrateCalRecord( &bicarb ); signalDrainPumpHardStop(); acidBottleVolML = acid.acidConcentrate[ CAL_DATA_ACID_CONCENTRATE_1 ].acidFullBottleVolumeML; bicarbBottleVolML = bicarb.bicarbConcentrate[ CAL_DATA_BICARB_CONCENTRATE_1 ].bicarbStartVolumeML; if ( DG_RESERVOIR_1 == inactiveReservoir ) { setValveState( VRD1, VALVE_STATE_CLOSED ); } else if ( DG_RESERVOIR_2 == inactiveReservoir ) { setValveState( VRD2, VALVE_STATE_CLOSED ); } #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_EMPTY_BOTTLES_ALARM ) != SW_CONFIG_ENABLE_VALUE ) #endif { // Detect empty bottles using integrated volumes if ( ( acidBottleVolML - getChemicalUsedVolumeML( ACID ) ) <= CONCENTRATION_BOTTLE_LOW_VOLUME_ML ) // || // SRSDG 437 { resetChemicalUsedVolumeML( ACID ); setThisFisrtFillFlag( TRUE ); // indicates bottles need prime activateAlarmNoData( ALARM_ID_DG_ACID_BOTTLE_LOW_VOLUME ); } if ( ( bicarbBottleVolML - getChemicalUsedVolumeML( BICARB ) ) <= CONCENTRATION_BOTTLE_LOW_VOLUME_ML ) // || // SRSDG 438 { resetChemicalUsedVolumeML( BICARB ); setThisFisrtFillFlag( TRUE ); activateAlarmNoData( ALARM_ID_DG_BICARB_BOTTLE_LOW_VOLUME ); } } if ( TRUE == isReservoirTarePending() ) { // Tare reservoir load cells at empty if requested result = DG_DRAIN_STATE_TARE; } else { requestNewOperationMode( DG_MODE_GENE ); } } else if ( TRUE == didTimeout( dialysateDrainStartTime, DIALYSATE_DRAIN_TIME_OUT ) ) // Drain timed out raise the alarm { activateAlarmNoData( ALARM_ID_DG_DIALYSATE_DRAIN_TIME_OUT ); } return result; } /*********************************************************************//** * @brief * The handleDrainStateTare function handles the tare state of the drain mode * state machine. * @details Inputs: drainEmptyTareTimerCtr * @details Outputs: drainEmptyTareTimerCtr * @return the next state *************************************************************************/ static DG_DRAIN_STATE_T handleDrainStateTare( void ) { DG_DRAIN_STATE_T result = DG_DRAIN_STATE_TARE; DG_RESERVOIR_ID_T inactiveReservoir = getInactiveReservoir(); if ( ++drainEmptyTareTimerCtr > DRAIN_EMPTY_TARE_WAIT ) { drainEmptyTareTimerCtr = 0; tareLoadCellsAtEmpty( inactiveReservoir ); setValveState( VRD1, VALVE_STATE_CLOSED ); setValveState( VRD2, VALVE_STATE_CLOSED ); if ( TRUE == rinseConcentrateLines ) { setConcentratePumpTargetSpeed( CONCENTRATEPUMPS_CP1_ACID, RINSE_SPEED ); setConcentratePumpTargetSpeed( CONCENTRATEPUMPS_CP2_BICARB, RINSE_SPEED ); requestConcentratePumpOn( CONCENTRATEPUMPS_CP1_ACID ); requestConcentratePumpOn( CONCENTRATEPUMPS_CP2_BICARB ); } result = DG_DRAIN_STATE_RINSE; } return result; } /*********************************************************************//** * @brief * The handleRinseState function handles the tare state of the drain mode * state machine. * @details Inputs: drainEmptyTareTimerCtr * @details Outputs: drainEmptyTareTimerCtr * @return the next state *************************************************************************/ static DG_DRAIN_STATE_T handleRinseState( void ) { DG_DRAIN_STATE_T result = DG_DRAIN_STATE_RINSE; if ( TRUE == rinseConcentrateLines ) { if ( ++rinseConcentrateLinesTimerCtr > RINSE_CONCENTRATE_LINES_WAIT ) { rinseConcentrateLinesTimerCtr = 0; requestConcentratePumpOff( CONCENTRATEPUMPS_CP1_ACID, NO_PARK_CONC_PUMPS ); requestConcentratePumpOff( CONCENTRATEPUMPS_CP2_BICARB, NO_PARK_CONC_PUMPS ); requestNewOperationMode( DG_MODE_GENE ); } } else { requestNewOperationMode( DG_MODE_GENE ); } return result; } /**@}*/