/************************************************************************** * * 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 ModeFill.c * * @author (last) Sean Nash * @date (last) 19-Aug-2020 * * @author (original) Leonardo Baloa * @date (original) 19-Nov-2019 * ***************************************************************************/ #include "ConductivitySensors.h" #include "FPGA.h" #include "LoadCell.h" #include "ModeFill.h" #include "OperationModes.h" #include "Pressures.h" #include "Reservoirs.h" #include "TemperatureSensors.h" #include "Timers.h" #include "Valves.h" /** * @addtogroup DGFillMode * @{ */ // ********** private definitions ********** // ********** private data ********** static DG_FILL_MODE_STATE_T fillState; ///< Currently active fill state. // ********** private function prototypes ********** static DG_FILL_MODE_STATE_T handleDialysateProductionState( void ); static DG_FILL_MODE_STATE_T handleDeliverDialysateState( void ); /*********************************************************************//** * @brief initFillMode * The initFillMode function initializes the fill mode module. * @details * Inputs : none * Outputs : Fill mode module initialized * @return none *************************************************************************/ void initFillMode( void ) { fillState = DG_FILL_MODE_STATE_START; } /*********************************************************************//** * @brief transitionToFillMode * The transitionToFillMode function prepares for transition to fill mode. * @details * Inputs : none * Outputs : Re-initialized fill mode * @return none *************************************************************************/ void transitionToFillMode( void ) { // re-initialize fill mode each time we transition to fill mode initFillMode(); // set initial actuator states setValveState( VDR, VALVE_STATE_DRAIN_C_TO_NO ); setValveState( VPO, VALVE_STATE_NOFILL_C_TO_NO ); // Conc. pumps on } /*********************************************************************//** * @brief execFillMode * The execFillMode function executes the fill mode state machine. * @details * Inputs : fillState * Outputs : Check water quality, fill mode state machine executed * @return current state. *************************************************************************/ U32 execFillMode( void ) { // check inlet water conductivity, temperature, pressure, and RO rejection ratio checkInletWaterConductivity(); checkInletWaterTemperature(); checkInletPressure(); checkRORejectionRatio(); // execute current Fill state switch ( fillState ) { case DG_FILL_MODE_STATE_START: fillState = DG_FILL_MODE_STATE_DIALYSATE_PRODUCTION; break; case DG_FILL_MODE_STATE_DIALYSATE_PRODUCTION: fillState = handleDialysateProductionState(); break; case DG_FILL_MODE_STATE_DELIVER_DIALYSATE: fillState = handleDeliverDialysateState(); break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, 0, fillState ) // TODO - add s/w fault enum to 1st data param fillState = DG_FILL_MODE_STATE_START; break; } return fillState; } /*********************************************************************//** * @brief * The handleDialysateProductionState function executes the Dialysate production * state of the fill mode state machine. * @details * Inputs : none * Outputs : none * @return the next state *************************************************************************/ static DG_FILL_MODE_STATE_T handleDialysateProductionState( void ) { DG_FILL_MODE_STATE_T result = DG_FILL_MODE_STATE_DIALYSATE_PRODUCTION; // TODO - transition when temperature and mix is in range if ( 1 ) { setValveState( VPO, VALVE_STATE_FILL_C_TO_NC ); result = DG_FILL_MODE_STATE_DELIVER_DIALYSATE; } return result; } /*********************************************************************//** * @brief * The handleDeliverDialysateState function executes the deliver Dialysate * state of the fill mode state machine. * @details * Inputs : none * Outputs : Deliver Dialysate * @return the next state *************************************************************************/ static DG_FILL_MODE_STATE_T handleDeliverDialysateState( void ) { DG_FILL_MODE_STATE_T result = DG_FILL_MODE_STATE_DELIVER_DIALYSATE; LOAD_CELL_ID_T fillWeightLoadCell = LOAD_CELL_A1; // TODO - transition back when temperature or mix out of range if ( 0 ) { setValveState( VPO, VALVE_STATE_NOFILL_C_TO_NO ); result = DG_FILL_MODE_STATE_DIALYSATE_PRODUCTION; } // determine which load cell to use for fill volume - we want weight of inactive reservoir if ( RESERVOIR_1 == getActiveReservoir() ) { fillWeightLoadCell = LOAD_CELL_B1; } // if we've reached our target fill to volume (by weight), we're done filling - go back to re-circ mode if ( getReservoirFillVolumeTargetMl() <= getLoadCellFilteredWeight( fillWeightLoadCell ) ) { requestNewOperationMode( DG_MODE_CIRC ); } return result; } /*********************************************************************//** * @brief * The getCurrentFillState function returns the current state of the fill mode. * @details * Inputs : fillState * Outputs : none * @return current state of fill mode *************************************************************************/ DG_FILL_MODE_STATE_T getCurrentFillState( void ) { return fillState; } /**@}*/