Index: firmware/App/Controllers/DryBiCart.c =================================================================== diff -u -rb50acaa8dda74292fb41110440eaaa40f2959fb7 -r2fb473f4574f9c150849bb39991a31bcadaae693 --- firmware/App/Controllers/DryBiCart.c (.../DryBiCart.c) (revision b50acaa8dda74292fb41110440eaaa40f2959fb7) +++ firmware/App/Controllers/DryBiCart.c (.../DryBiCart.c) (revision 2fb473f4574f9c150849bb39991a31bcadaae693) @@ -15,11 +15,16 @@ * ***************************************************************************/ +#include "BalancingChamber.h" #include "Common.h" +#include "ConcentratePumps.h" +#include "Conductivity.h" +#include "DialysatePumps.h" #include "DDDefs.h" #include "DryBiCart.h" #include "Level.h" #include "Messaging.h" +#include "ModeGenDialysate.h" #include "OperationModes.h" #include "PressureSensor.h" #include "PersistentAlarm.h" @@ -49,6 +54,13 @@ //TODO: uncomment later based on the testing results //#define VALV_D80_MAX_OPEN_TIME_MS ( 10 * MS_PER_SECOND ) ///< Max time allowed for opening D80 valve during bicarb chamber (F) fill. #define VALV_D80_MAX_OPEN_TIME_MS ( 500 ) ///< Max time allowed for opening D80 valve during bicarb chamber (F) fill. +#define DRY_BICART_MAX_DRAIN_TIME_MS ( 240 * MS_PER_SECOND ) ///< Max drain time for bicart chamber in ms. +#define DRY_BICART_DRAIN_COND_SAMPLE_PERIOD_MS 50 ///< Conductivity sample period +#define DRY_BICART_DRAIN_COND_ZERO_THRESH 0.05F ///< "Zero" conductivity threshold +#define DRY_BICART_DRAIN_COND_STABLE_SAMPLES 10U ///< Debounce samples (10*50ms=500ms) +#define DRY_BICART_DRAIN_FLUID_PHASE_TIME_MS ( 3 * MS_PER_SECOND ) ///< Time to stay in fluid drain per cycle +#define DRY_BICART_DRAIN_VENT_PHASE_TIME_MS 1500 ///< Time to keep D64 open per vent cycle +#define DRY_BICART_DRAIN_COND_STABLE_COUNT_MAX 0xFFFFU /// Payload record structure for dry bicart fill request typedef struct @@ -60,16 +72,22 @@ static DRY_BICART_FILL_EXEC_STATE_T dryBiCartFillExecState; ///< Current state of dry bicart fill executive state machine. static BICARB_CHAMBER_FILL_EXEC_STATE_T bicarbChamberFillExecState; ///< Current state of bicarb chamber fill executive state machine. +static DRY_BICART_DRAIN_EXEC_STATE_T dryBiCartDrainExecState; ///< Current state of dry bicart drain executive state machine static U32 dryBiCartFillStartTime; ///< Dry bicart fill start time. static U32 biCartFillCycleCounter; ///< Number of drybicart fill cycle static OVERRIDE_U32_T biCartMaxFillCycleCount; ///< Maximum number of drybicart fill cycle ( overrideable) static U32 lastFillDurationInMS; ///< Previous time duration to fill the bicart fill. static U32 currentFillDurationInMS; ///< Current time duartion to fill the bicart fill. static OVERRIDE_U32_T dryBiCartFillRequested; ///< Start/stop Dry bicart fill. +static OVERRIDE_U32_T dryBiCartDrainRequested; ///< Start/stop Dry bicart drain. static OVERRIDE_U32_T dryBiCartDataPublishInterval; ///< Dry bicart data publish interval. static U32 dryBiCartDataPublicationTimerCounter; ///< Used to schedule drybicart data publication to CAN bus. static U32 dryBiCarbSupplyStartTime; ///< Bicarb supply start time from bicart to Chamber F. static OVERRIDE_U32_T bicarbChamberFillRequested; ///< Start/stop Bicarb chamber fill. +static U32 dryBiCartDrainStartTime; ///< Drain overall start time (for max timeout) +static U32 dryBiCartDrainPhaseStartTime; ///< Phase start time (fluid/vent cycling) +static U32 dryBiCartDrainLastCondSampleTime; ///< Last time conductivity was sampled +static U32 dryBiCartDrainCondStableCount; ///< Debounce counter for "zero" conductivity // ********** private function prototypes ********** @@ -83,6 +101,10 @@ static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberCheckLevelState( void ); static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberFillState( void ); static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberPressureCheckState( void ); +static DRY_BICART_DRAIN_EXEC_STATE_T handleDryBicartFluidDrainState( void ); +static DRY_BICART_DRAIN_EXEC_STATE_T handleDryBicartPressureEquillibriumState( void ); +static DRY_BICART_DRAIN_EXEC_STATE_T handleDryBicartFluidDrainDurationCheckState( void ); +static DRY_BICART_DRAIN_EXEC_STATE_T handleDryBicartFluidDrainEndState( void ); static void publishDryBicartData( void ); static U32 getDryBicartFillDataPublishInterval( void ); @@ -120,6 +142,11 @@ dryBiCartDataPublicationTimerCounter = 0; biCartFillCycleCounter = 0; dryBiCarbSupplyStartTime = 0; + dryBiCartDrainExecState = DRY_BICART_DRAIN_START_STATE; + dryBiCartDrainStartTime = 0; + dryBiCartDrainPhaseStartTime = 0; + dryBiCartDrainLastCondSampleTime = 0; + dryBiCartDrainCondStableCount = 0; } /*********************************************************************//** @@ -152,6 +179,9 @@ //Fill Bicarb chamber F execBicarbChamberFillMode(); + //Drain Bicart Chamber exec + execDryBicartDrainMode(); + //Publish dry bicart data publishDryBicartData(); } @@ -548,6 +578,207 @@ /*********************************************************************//** * @brief + * The execDryBicartDrainMode function executes the dry bicart drain state machine. + * @details \b Inputs: dryBiCartDrainExecState + * @details \b Outputs: dryBiCartDrainExecState + * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT when wrong dry bicart drain state + * invoked. + * @return current state. + *************************************************************************/ +U32 execDryBicartDrainMode( void ) +{ + // execute drybicart drain state machine + switch ( dryBiCartDrainExecState ) + { + case DRY_BICART_DRAIN_START_STATE: + if ( TRUE == getU32OverrideValue( &dryBiCartDrainRequested ) ) + { + dryBiCartDrainStartTime = getMSTimerCount(); + dryBiCartDrainPhaseStartTime = dryBiCartDrainStartTime; + dryBiCartDrainLastCondSampleTime = dryBiCartDrainStartTime; + dryBiCartDrainCondStableCount = 0; + dryBiCartDrainExecState = DRY_BICART_FLUID_DRAIN_STATE; + } + break; + + case DRY_BICART_FLUID_DRAIN_STATE: + dryBiCartDrainExecState = handleDryBicartFluidDrainState(); + break; + + case DRY_BICART_PRESSURE_EQUILLIBRIUM_STATE: + dryBiCartDrainExecState = handleDryBicartPressureEquillibriumState(); + break; + + case DRY_BICART_FLUID_DRAIN_END_STATE: + dryBiCartDrainExecState = handleDryBicartFluidDrainEndState(); + break; + + case DRY_BICART_FLUID_DRAIN_DURATION_CHECK_STATE: + dryBiCartDrainExecState = handleDryBicartFluidDrainDurationCheckState(); + break; + + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DD_SOFTWARE_FAULT, SW_FAULT_ID_DRY_BICART_DRAIN_INVALID_EXEC_STATE, dryBiCartDrainExecState ); + break; + } + + return dryBiCartDrainExecState; +} + +/*********************************************************************//** + * @brief + * The handleDryBicartFluidDrainState function initiates the water drain + * from dry bicarbonate cartridge. + * @details \b Inputs: none + * @details \b Outputs: valve states + * @return the next drybicart drain state. + *************************************************************************/ +static DRY_BICART_DRAIN_EXEC_STATE_T handleDryBicartFluidDrainState( void ) +{ + DRY_BICART_DRAIN_EXEC_STATE_T state = DRY_BICART_FLUID_DRAIN_STATE; + + // Open vent valves and close descaling valve + setValveState( D80_VALV, VALVE_STATE_OPEN ); + setValveState( D81_VALV, VALVE_STATE_CLOSED ); + setValveState( D85_VALV, VALVE_STATE_OPEN ); + + // Open balancing chamber pressure valves + valveControlForBCOpenState(); + + //Close D14 valve + setValveState( D14_VALV, VALVE_STATE_CLOSED ); + + // RUn D10 IN OPpen loop mode(max speed) + setConcentratePumpTargetSpeed( D10_PUMP, CONCENTRATE_PUMP_MAX_SPEED, 0xFFFF ); + requestConcentratePumpOn( D10_PUMP ); + + // Run D48 + setDialysatePumpTargetRPM( D48_PUMP, SPENT_DIAL_PUMP_INITIAL_RPM, TRUE ); + + // After a fluid drain window, go to duration/cond check (which alternates phases) + if ( TRUE == didTimeout( dryBiCartDrainPhaseStartTime, DRY_BICART_DRAIN_FLUID_PHASE_TIME_MS ) ) + { + dryBiCartDrainPhaseStartTime = getMSTimerCount(); + state = DRY_BICART_PRESSURE_EQUILLIBRIUM_STATE; + } + + return state; +} + +/*********************************************************************//** + * @brief + * The handleDryBicartPressureEquillibriumState function performs pressure + * equilibration by opening D64 while D80/D85 are open. + * @details \b Inputs: none + * @details \b Outputs: valve state + * @return the next drybicart drain state. + *************************************************************************/ +static DRY_BICART_DRAIN_EXEC_STATE_T handleDryBicartPressureEquillibriumState( void ) +{ + DRY_BICART_DRAIN_EXEC_STATE_T state = DRY_BICART_PRESSURE_EQUILLIBRIUM_STATE; + + // Goal is to have D64 open at the same time as D80/D85 + setValveState( D80_VALV, VALVE_STATE_OPEN ); + setValveState( D85_VALV, VALVE_STATE_OPEN ); + + // Open D64 valve (vent/equil) + setValveState( D64_VALV, VALVE_STATE_OPEN ); + + // After vent window, close and go to duration/cond check + if ( TRUE == didTimeout( dryBiCartDrainPhaseStartTime, DRY_BICART_DRAIN_VENT_PHASE_TIME_MS ) ) + { + setValveState( D64_VALV, VALVE_STATE_CLOSED ); + dryBiCartDrainPhaseStartTime = getMSTimerCount(); + state = DRY_BICART_FLUID_DRAIN_DURATION_CHECK_STATE; + } + + return state; +} + +/*********************************************************************//** + * @brief + * The handleDryBicartFluidDrainDurationCheckState function monitors conductivity + * through D17 and D74 and determines drain completion, otherwise alternates phases. + * @details \b Inputs: P17_COND, P74_COND + * @details \b Outputs: next state decision + * @return the next drybicart drain state. + *************************************************************************/ +static DRY_BICART_DRAIN_EXEC_STATE_T handleDryBicartFluidDrainDurationCheckState( void ) +{ + DRY_BICART_DRAIN_EXEC_STATE_T state = DRY_BICART_FLUID_DRAIN_DURATION_CHECK_STATE; + + // Max drain time fallback + if ( TRUE == didTimeout( dryBiCartDrainStartTime, DRY_BICART_MAX_DRAIN_TIME_MS ) ) + { + return DRY_BICART_FLUID_DRAIN_END_STATE; + } + + // Sample conductivity periodically + if ( TRUE == didTimeout( dryBiCartDrainLastCondSampleTime, DRY_BICART_DRAIN_COND_SAMPLE_PERIOD_MS ) ) + { + F32 cond17 = getFilteredConductivity( D17_COND ); + F32 cond74 = getFilteredConductivity( D74_COND ); + + dryBiCartDrainLastCondSampleTime = getMSTimerCount(); + + // Debounce "zero conductivity" detection + if ( ( cond17 <= DRY_BICART_DRAIN_COND_ZERO_THRESH ) && + ( cond74 <= DRY_BICART_DRAIN_COND_ZERO_THRESH ) ) + { + if ( dryBiCartDrainCondStableCount < DRY_BICART_DRAIN_COND_STABLE_COUNT_MAX ) + { + dryBiCartDrainCondStableCount++; + } + } + else + { + dryBiCartDrainCondStableCount = 0; + } + + if ( dryBiCartDrainCondStableCount >= DRY_BICART_DRAIN_COND_STABLE_SAMPLES ) + { + return DRY_BICART_FLUID_DRAIN_END_STATE; + } + } + + dryBiCartDrainPhaseStartTime = getMSTimerCount(); + state = DRY_BICART_FLUID_DRAIN_STATE; + + return state; +} + +/*********************************************************************//** + * @brief + * The handleDryBicartFluidDrainEndState function handles the end state for + * draining of the fluid. + * @details \b Inputs: none + * @details \b Outputs: valve state / pump control + * @return the next drybicart drain state. + *************************************************************************/ +static DRY_BICART_DRAIN_EXEC_STATE_T handleDryBicartFluidDrainEndState( void ) +{ + DRY_BICART_DRAIN_EXEC_STATE_T state = DRY_BICART_FLUID_DRAIN_END_STATE; + + // Stop pumps + requestConcentratePumpOff( D10_PUMP, TRUE ); + setDialysatePumpTargetRPM( D48_PUMP, 0, TRUE ); + + // Close valves used for drain + setValveState( D64_VALV, VALVE_STATE_CLOSED ); + setValveState( D80_VALV, VALVE_STATE_CLOSED ); + setValveState( D85_VALV, VALVE_STATE_CLOSED ); + + // Go idle when request cleared + if ( FALSE == getU32OverrideValue( &dryBiCartDrainRequested ) ) + { + state = DRY_BICART_DRAIN_START_STATE; + } + + return state; +} + +/*********************************************************************//** + * @brief * The setBicarbChamberFillRequested function sets the chmaberFillRequest * flag value to be True. * @details \b Inputs: none