/************************************************************************** * * 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) Quang Nguyen * @date (last) 13-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 "Timers.h" #include "Valves.h" #include "TemperatureSensors.h" /** * @addtogroup DGFillMode * @{ */ // ********** private definitions ********** #define QUARTER_SECOND 250 #define HALF_SECOND 500 // ********** 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 \n * fill mode. * @details * Inputs : none * Outputs : fillState * @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 : fillState * @return current state. *************************************************************************/ U32 execFillMode( void ) { // check inlet water conductivity, temperature, and pressure checkInletWaterConductivity( fillState ); checkInletWaterTemperature(); checkInletPressure(); // 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 \n * state of the Fill Mode state machine. * @details * Inputs : none * Outputs : * @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 \n * state of the Fill Mode state machine. * @details * Inputs : none * Outputs : * @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 \n * fill mode. * @details * Inputs : fillState * Outputs : none * @return the current state of fill mode. *************************************************************************/ DG_FILL_MODE_STATE_T getCurrentFillState( void ) { return fillState; } /**@}*/