Index: firmware/App/Controllers/DryBiCart.c =================================================================== diff -u -rb4e65bc0026ff22f0bb77e4e066724368ef1cfd5 -r788a0b1a638d815df1e83af80cba7be03af2020c --- firmware/App/Controllers/DryBiCart.c (.../DryBiCart.c) (revision b4e65bc0026ff22f0bb77e4e066724368ef1cfd5) +++ firmware/App/Controllers/DryBiCart.c (.../DryBiCart.c) (revision 788a0b1a638d815df1e83af80cba7be03af2020c) @@ -8,7 +8,7 @@ * @file DryBiCart.c * * @author (last) Sameer Kalliadan Poyil -* @date (last) 30-Apr-2026 +* @date (last) 25-Jun-2026 * * @author (original) Vinayakam Mani * @date (original) 19-Nov-2025 @@ -25,9 +25,9 @@ #include "Level.h" #include "Messaging.h" #include "MessageSupport.h" +#include "MixingControl.h" #include "ModeGenDialysate.h" #include "OperationModes.h" -#include "PIControllers.h" #include "PressureSensor.h" #include "PersistentAlarm.h" #include "Pressure.h" @@ -43,82 +43,47 @@ // ********** private definitions ********** -#define DRY_BICART_DATA_PUBLISH_INTERVAL ( 250 / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the balancing chamber data published. +#define DRY_BICART_DATA_PUBLISH_INTERVAL ( 250 / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the drybicart data published. +#define WCDI2_PRESSURE_OFFSET_PSI 7.0F ///< The difference between the actual pressure measured at bicart assembly and the measured D66 pressure based on the placement of the sensor for WCID1 +#define WCID1_PRESSURE_OFFSET_PSI 1.0F ///< The difference between the actual pressure measured at bicart assembly and the measured D66 pressure based on the placement of the sensor for WCID2 +#define DRY_BICART_PERSISTENCE_DURATION_MS 200 ///< Fill persistence duration to overcome the pressure overshoot/ drop for beta 1.9 and above. // Dry Bicart Fill -#define DRY_BICART_FILL_PERSISTANCE_DURATION_MS 200 ///< Fill persistence duration to overcome the pressure over shoot for beta 1.9 and beta 2.0. -#define DRY_BICART_FILL_DURATION_MIN_MS ( ( 2 * MS_PER_SECOND ) + \ - DRY_BICART_FILL_PERSISTANCE_DURATION_MS ) ///< Minimum fill duration to be met to end the fill cycle. -#define DRY_BICART_FILL_COMPLETE_TIME_MS ( 1 * MS_PER_SECOND ) ///< Wait time to reset the request flag after fill complete -#define DRY_BICART_FILL_DURATION_DIFF_MS 750 ///< Fill duration difference between last and current fill cycle. -#define PRESSURE_OFFSET_PSI 1.0F ///< Dry bicart pressure offset. -#define DRY_BICART_FILL_COMPLETE_PRESSURE ( 10.0F + PRESSURE_OFFSET_PSI ) ///< Maximum pressure reached to indicate the dry bicart fill being completed. -#define DRY_BICART_FILL_INITIATE_PRESSURE 1.5F ///< Minimum pressure required to initiate the dry bicart fill process. -#define DRY_BICART_FILL_COMPLETE_SUPPLY_PRESSURE 6.0F ///< Maximum pressure allowed at bicart fill during supply process/state -#define DRY_BICART_FILL_INITIATE_SUPPLY_PRESSURE 3.0F ///< Minimum pressure required to initiate the dry bicart fill during supply process/state -#define MAX_DRY_BICART_FILL_COMPLETE_PRESSURE 15.0F ///< Very max dry bicart fill complete pressure for persistence on d66 - -#define DRY_BICART_DEFAULT_MAX_FILL_CYCLE_CNT 10 ///< Default max fill cycle allowed for dry bicart fill/mix with water. -#define DRY_BICART_MAX_FILL_CYCLE_CNT 30 ///< Max fill cycle allowed (by override) for dry bicart fill/mix with water. +#define DRY_BICART_FILL_COMPLETE_TIME_MS ( 1 * MS_PER_SECOND ) ///< Wait time to reset the request flag after fill complete +#define DRY_BICART_WCID2_FILL_COMPLETE_PRESSURE_PSI ( 10.0F + WCDI2_PRESSURE_OFFSET_PSI ) ///< Maximum pressure reached to indicate the dry bicart fill being completed for WCID2 +#define DRY_BICART_WCID1_FILL_COMPLETE_PRESSURE_PSI ( 10.0F + WCID1_PRESSURE_OFFSET_PSI ) ///< Maximum pressure reached to indicate the dry bicart fill being completed for WCID1 +#define GET_FILL_COMPLETE_PRESSURE_PSI (getTestConfigStatus( TEST_CONFIG_DD_ENABLE_WCID_1 ) != TRUE ? \ + DRY_BICART_WCID2_FILL_COMPLETE_PRESSURE_PSI : DRY_BICART_WCID1_FILL_COMPLETE_PRESSURE_PSI ) ///< Get fill complete pressure based on the WCID configuration +#define DRY_BICART_FILL_INITIATE_PRESSURE_PSI 1.5F ///< Minimum pressure required to initiate the dry bicart fill process. +#define DRY_BICART_FILL_COMPLETE_SUPPLY_PRESSURE_PSI 10.0F ///< Maximum pressure allowed at bicart fill during supply process/state +#define DRY_BICART_FILL_INITIATE_SUPPLY_PRESSURE_PSI 3.0F ///< Minimum pressure required to initiate the dry bicart fill during supply process/state +#define DRY_BICART_MAX_FILL_CYCLE_CNT 10 ///< Max fill cycle allowed (by override) for dry bicart fill/mix with water. +#define DRY_BICART_MINIMUM_FILL_CYCLE_CNT 3 ///< Minimum fill cycle count for filling dry bicart // Dry Bicart Fill Vent -#define DRY_BICART_FILL_VENT_TIME_MS ( 1 * MS_PER_SECOND ) ///< Wait time to vent dry bicart gas before actuating Bicarb chamber(F) venting. -#define DRY_BICART_FILL_VENT_MAX_TIME_MS ( 10 * MS_PER_SECOND ) ///< Max time to vent both dry bicart and Chamber F. -#define DRY_BICART_FILL_VENT_COMPLETE_PRESSURE 1.5F ///< Pressure reached to indicate the dry bicart venting being completed. +#define DRY_BICART_FILL_VENT_TIME_MS ( 1 * MS_PER_SECOND ) ///< Wait time to vent dry bicart gas before actuating Bicarb chamber(F) venting. +#define DRY_BICART_FILL_VENT_MAX_TIME_MS ( 3 * MS_PER_SECOND ) ///< Max time to vent both dry bicart and Chamber F. +#define DRY_BICART_FILL_VENT_COMPLETE_PRESSURE_PSI 1.5F ///< Pressure reached to indicate the dry bicart venting being completed. // Bicarb chamber fill/Supply #ifdef CONDUCTIVE_LEVEL_SENSOR_ENABLED -#define DRY_BICART_SUPPLY_VALVE_D80_OPEN_TIME_MS ( 10 * MS_PER_SECOND ) ///< Max time allowed for supply (opening D80 valve) during bicarb chamber (F) fill using level sensor +#define DRY_BICART_SUPPLY_VALVE_D80_OPEN_TIME_MS ( 10 * MS_PER_SECOND ) ///< Max time allowed for supply (opening D80 valve) during bicarb chamber (F) fill using level sensor #else -#define DRY_BICART_SUPPLY_VALVE_D80_OPEN_TIME_MS ( 3 * MS_PER_SECOND ) ///< Max time allowed for supply (opening D80 valve) during bicarb chamber (F) fill time based fill +#define DRY_BICART_SUPPLY_VALVE_D80_OPEN_TIME_MS ( 3 * MS_PER_SECOND ) ///< Max time allowed for supply (opening D80 valve) during bicarb chamber (F) fill time based fill #endif -#define DRY_BICART_SUPPLY_VENT_TIME_MS ( 1 * MS_PER_SECOND ) ///< Wait time to vent dry bicart gas before actuating Bicarb chamber(F) venting. -#define DRY_BICART_SUPPLY_VENT_MAX_TIME_MS ( 1 * MS_PER_SECOND ) ///< Max time to vent both dry bicart and Chamber F. -#define DRY_BICART_SUPPLY_COMPLETE_TIME_MS ( 2 * MS_PER_SECOND ) ///< Wait time to close the D80 valve after D65 is closed +#define DRY_BICART_SUPPLY_VENT_TIME_MS ( 1 * MS_PER_SECOND ) ///< Wait time to vent dry bicart gas before actuating Bicarb chamber(F) venting. +#define DRY_BICART_SUPPLY_VENT_MAX_TIME_MS ( 1 * MS_PER_SECOND ) ///< Max time to vent both dry bicart and Chamber F. +#define DRY_BICART_SUPPLY_COMPLETE_TIME_MS ( 2 * MS_PER_SECOND ) ///< Wait time to close the D80 valve after D65 is closed // Dry Bicart Drain -#define LARGE_DRY_BICART_MAX_DRAIN_TIME_MS ( 8 * SEC_PER_MIN * MS_PER_SECOND ) ///< Max drain time for large dry bicart in ms. -#define SMALL_DRY_BICART_MAX_DRAIN_TIME_MS ( 4 * SEC_PER_MIN * MS_PER_SECOND ) ///< Max drain time for small dry bicart in ms. -#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 ( 8 * MS_PER_SECOND ) ///< Time to keep D64 open per vent cycle -#define DRY_BICART_DRAIN_COND_SAMPLE_PERIOD_MS 500 ///< 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 LARGE_DRY_BICART_MAX_DRAIN_TIME_MS ( 8 * SEC_PER_MIN * MS_PER_SECOND ) ///< Max drain time for large dry bicart in ms. +#define SMALL_DRY_BICART_MAX_DRAIN_TIME_MS ( 4 * SEC_PER_MIN * MS_PER_SECOND ) ///< Max drain time for small dry bicart in ms. +#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 ( 8 * MS_PER_SECOND ) ///< Time to keep D64 open per vent cycle +#define DRY_BICART_DRAIN_COND_SAMPLE_PERIOD_MS 500 ///< Conductivity sample period +#define DRY_BICART_DRAIN_COND_ZERO_THRESH 0.05F ///< Zero conductivity threshold +#define DRY_BICART_DRAIN_COND_STABLE_SAMPLES 10U ///< De-bounce samples (10*50ms=500ms) -// drybicarb mixing -#define BICARB_VOL_CONTROL_P_COEFFICIENT ( 0.00008484 * 2 ) ///< Bicarb proportional gain (kp) -#define BICARB_VOL_CONTROL_I_COEFFICIENT ( 0.00033936 / 5 ) ///< Bicarb integral gain. (ki) -#define MIN_BICARB_VOLUME_ML 0.868686869 ///< Minimum bicarb volume in mL -#define MAX_BICARB_VOLUME_ML 1.8 ///< Maximum bicarb volume in mL - -#define ACID_VOL_CONTROL_P_COEFFICIENT ( 0.00000997 / 2 ) ///< Acid proportional gain (kp) -#define ACID_VOL_CONTROL_I_COEFFICIENT ( 0.00003988 / 2 ) ///< Acid integral gain. (ki) -#define MIN_ACID_VOLUME_ML 0.3 ///< Minimum acid volume in mL -#define MAX_ACID_VOLUME_ML 1.0 ///< Maximum acid volume in mL/min. - -#define DRY_BICARB_TARGET_CONDUCTIVITY 2714.0F ///< Target bicarb conductivity -#define DRY_BICARB_DELTA_CONDUCTIVITY 500.0F ///< Delta bicarb conductivity - -#define DRY_ACID_BICARB_TARGET_CONDUCTIVITY 13734.87F ///< Target acid bicarb mix conductivity -#define DRY_ACID_BICARB_DELTA_CONDUCTIVITY 700.0F ///< Delta acid bicarb mix conductivity - -#define ACID_TYPE_1K_2_5_CA 11192.55F ///< standard acid conductivity for 1K -#define ACID_TYPE_2K_2_5_CA 11313.62F ///< standard acid conductivity for 2K -#define ACID_TYPE_3K_2_5_CA 11435.68F ///< standard acid conductivity for 3K - -#define MIN_BICARB_CONDUCTIVITY 2000 ///< Minimum Bicarb conductivity limit -#define MAX_BICARB_CONDUCTIVITY 4000 ///< Maximum Bicarb conductivity limit - -#define MIX_NO_FEED_FORWARD 0.0F ///< Feedforward term for dialysate closed loop control - -#define MIX_CONTROL_INTERVAL_MS ( 15 * MS_PER_SECOND ) ///< Dialysate mixing control interval in ms -#define BICARB_MIX_CONTROL_INTERVAL ( 3 * MS_PER_SECOND /\ - TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the dialysate mix is controlled. -#define ACID_MIX_CONTROL_INTERVAL ( MIX_CONTROL_INTERVAL_MS /\ - TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the dialysate mix is controlled. -#define BICARB_DEADBAND_CONTROL 50.0F ///< Dry Bicarb dead band control -#define ACID_DEADBAND_CONTROL 100.0F ///< Acid dead band control - /// Payload record structure for dry bicart fill request typedef struct { @@ -132,39 +97,16 @@ DRY_BICART_INITIAL_FILL, ///< Dry bicart initial fill DRY_BICART_SUPPLY, ///< Dry bicart chamber fill DRY_BICART_DRAIN, ///< Dry bicart drain + DRY_BICART_DEPRESSURIZE, ///< Dry bicart depressurize NUM_OF_DRY_BICART_OPERATION ///< Number of dry bicart operation } DRY_BICART_OPERATION_T; -/// Enumeration of dialysate Mixing id. -typedef enum DialysateMixingID -{ - BICARB_MIX_ID = 0, ///< Bicarb mixing id - DIALYSATE_MIX_ID_FIRST = BICARB_MIX_ID, ///< First dialysate mixing in list - ACID_MIX_ID, ///< Acid mix id - NUM_OF_DIALYSATE_MIXING_ID ///< Number of dialysate mix ids -} DIALYSATE_MIXING_ID_T; - -/// Enumeration of dialysate Mixing states. -typedef enum DialysateMixing_States -{ - DIALYSATE_MIXING_OPEN_LOOP_STATE = 0, ///< Dialysate open loop state - DIALYSATE_MIXING_RAMP_UP_STATE, ///< Dialysate mixing ramp up state - DIALYSATE_MIXING_CONTROL_TO_TARGET_STATE, ///< Dialysate mixing control to target state - NUM_OF_DIALYSATE_MIXING_STATES ///< Number of dialysate mixing states -} DIALYSATE_MIXING_STATE_T; - -/// Dialysate Mixing state machine data structure -typedef struct -{ - U32 controlTimerCounter; ///< Timer counter to perform control on dialysate mixing. - DIALYSATE_MIXING_STATE_T dialysateMixingState; ///< Current state of dialysate mixing controller state machine. -} DIALYSATE_MIXING_DATA_T; - // ********** private data ********** 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 DRY_BICART_DEPRESSURIZE_EXEC_STATE_T dryBiCartDepressurizeExecState; ///< Current state of dry bicart depressurize executive state machine // fill static U32 dryBiCartFillStartTime; ///< Dry bicart fill start time. static U32 biCartFillCycleCounter; ///< Number of drybicart fill cycle @@ -173,11 +115,13 @@ static OVERRIDE_U32_T biCartMaxFillCycleCount; ///< Maximum number of drybicart fill cycle ( overrideable) static OVERRIDE_F32_T dryBiCartUpperCartPressure; ///< Upper cart pressure static OVERRIDE_F32_T dryBiCartLowerCartPressure; ///< Lower cart pressure +static BOOL dryBiCartFillVentTimeOut; ///< Vent time out flag // initiate the fill/supply/drain static OVERRIDE_U32_T dryBiCartFillRequested; ///< Start/stop dry bicart fill. static OVERRIDE_U32_T bicarbChamberFillRequested; ///< Start/stop bicarb chamber fill. static OVERRIDE_U32_T dryBiCartDrainRequested; ///< Start/stop dry bicart drain. +static OVERRIDE_U32_T dryBiCartDepressurizeRequested; ///< Start/stop dry bicart depressurize // publish static OVERRIDE_U32_T dryBiCartDataPublishInterval; ///< Dry bicart data publish interval. static U32 dryBiCartDataPublicationTimerCounter; ///< Used to schedule drybicart data publication to CAN bus. @@ -193,7 +137,8 @@ static U32 dryBiCartDrainCondStableCount; ///< Debounce counter for "zero" conductivity static U32 dryBiCartDrainTimePeriod; ///< Dry bicart drain time period -static U32 dryBiCartPersistanceStartTime; ///< Dry bicart fill start time. +static U32 dryBiCartPersistenceStartTime; ///< Dry bicart Persistence time. +static U32 drybicartPersistenceOnLowercartPressureStartTime; ///< Dry bicart Persistence on lower cart pressure start time. static BOOL dryBiCartPressureDecayStartTimeFlag; ///< Dry bicart d66 pressure decay start timer. static DRY_BICART_OPERATION_T dryBicartStartRequest; ///< Dry bicart operation, fill or supply or drain request @@ -237,8 +182,7 @@ static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberStartState( void ); static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberCartridgeFillWaterStartState( void ); static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberCartridgeFillWaterEndState( void ); -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 handleBicarbChamberSupplyStart( void ); static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberPressureCheckState( void ); static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberSupplyVentStartState(void); static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberSupplyVentEndState(void); @@ -248,24 +192,12 @@ static DRY_BICART_DRAIN_EXEC_STATE_T handleDryBicartFluidDrainDurationCheckState( void ); static DRY_BICART_DRAIN_EXEC_STATE_T handleDryBicartFluidDrainEndState( void ); -// Mixing control loop -static DIALYSATE_MIXING_STATE_T handleDialysateMixOpenLoopState( DIALYSATE_MIXING_ID_T mixId ); -static DIALYSATE_MIXING_STATE_T handleDialysateMixRampToTargetState( DIALYSATE_MIXING_ID_T mixId ); -static DIALYSATE_MIXING_STATE_T handleDialysateMixControlToTargetState( DIALYSATE_MIXING_ID_T mixId ); +static DRY_BICART_DEPRESSURIZE_EXEC_STATE_T handleDryBicartDepressurizeStartState( void ); +static DRY_BICART_DEPRESSURIZE_EXEC_STATE_T handleDryBicartDepressurizeVentStartState( void ); +static DRY_BICART_DEPRESSURIZE_EXEC_STATE_T handleDryBicartDepressurizeVentEndState( void ); -static void setBicarbMixVol( F32 targetValue ); -static void setAcidMixVol( F32 targetValue ); -static F32 getBicarbDeltaConductivity( void ); -static F32 getBicarbTargetConductivity( void ); -static F32 getAcidBicarbDeltaConductivity( void ); -static F32 getAcidBicarbTargetConductivity( void ); -static F32 getBicarbKpGainCoefficient( void ); -static F32 getBicarbKiGainCoefficient( void ); -static F32 getAcidKpGainCoefficient( void ); -static F32 getAcidKiGainCoefficient( void ); static F32 getDryBicartLowerCartPressure( void ); static F32 getDryBicartUpperCartPressure( void ); -static U32 getDryBicartMixControlInterval( void ); // publish the status static void publishDryBicartData( void ); @@ -281,26 +213,18 @@ *************************************************************************/ void initDryBiCart( void ) { - DIALYSATE_MIXING_ID_T mixId; - dryBiCartFillExecState = DRY_BICART_START_STATE; bicarbChamberFillExecState = BICARB_CHAMBER_START_STATE; dryBiCartDrainExecState = DRY_BICART_DRAIN_START_STATE; dryBicartStartRequest = DRY_BICART_IDLE; - for ( mixId = DIALYSATE_MIX_ID_FIRST; mixId < NUM_OF_DIALYSATE_MIXING_ID; mixId++ ) - { - dialysateMix[ mixId ].dialysateMixingState = DIALYSATE_MIXING_OPEN_LOOP_STATE; - dialysateMix[ mixId ].controlTimerCounter = 0; - } - dryBiCartDataPublishInterval.data = DRY_BICART_DATA_PUBLISH_INTERVAL; dryBiCartDataPublishInterval.ovData = DRY_BICART_DATA_PUBLISH_INTERVAL; dryBiCartDataPublishInterval.ovInitData = 0; dryBiCartDataPublishInterval.override = OVERRIDE_RESET; - biCartMaxFillCycleCount.data = DRY_BICART_DEFAULT_MAX_FILL_CYCLE_CNT; - biCartMaxFillCycleCount.ovData = DRY_BICART_DEFAULT_MAX_FILL_CYCLE_CNT; + biCartMaxFillCycleCount.data = DRY_BICART_MAX_FILL_CYCLE_CNT; + biCartMaxFillCycleCount.ovData = DRY_BICART_MAX_FILL_CYCLE_CNT; biCartMaxFillCycleCount.ovInitData = 0; biCartMaxFillCycleCount.override = OVERRIDE_RESET; @@ -324,72 +248,18 @@ dryBiCartType.ovInitData = 0; dryBiCartType.override = OVERRIDE_RESET; - dryBiCartAcidMixVolume.data = DEFAULT_ACID_VOLUME_ML; - dryBiCartAcidMixVolume.ovData = DEFAULT_ACID_VOLUME_ML; - dryBiCartAcidMixVolume.ovInitData = 0.0F; - dryBiCartAcidMixVolume.override = OVERRIDE_RESET; - - dryBiCartBicarbMixVolume.data = DEFAULT_BICARB_VOLUME_ML; - dryBiCartBicarbMixVolume.ovData = DEFAULT_BICARB_VOLUME_ML; - dryBiCartBicarbMixVolume.ovInitData = 0.0F; - dryBiCartBicarbMixVolume.override = OVERRIDE_RESET; - - dryBiCartAcidMixVolumeKpGain.data = ACID_VOL_CONTROL_P_COEFFICIENT; - dryBiCartAcidMixVolumeKpGain.ovData = ACID_VOL_CONTROL_P_COEFFICIENT; - dryBiCartAcidMixVolumeKpGain.ovInitData = 0.0F; - dryBiCartAcidMixVolumeKpGain.override = OVERRIDE_RESET; - - dryBiCartAcidMixVolumeKiGain.data = ACID_VOL_CONTROL_I_COEFFICIENT; - dryBiCartAcidMixVolumeKiGain.ovData = ACID_VOL_CONTROL_I_COEFFICIENT; - dryBiCartAcidMixVolumeKiGain.ovInitData = 0.0F; - dryBiCartAcidMixVolumeKiGain.override = OVERRIDE_RESET; - - dryBiCartBicarbMixVolumeKpGain.data = BICARB_VOL_CONTROL_P_COEFFICIENT; - dryBiCartBicarbMixVolumeKpGain.ovData = BICARB_VOL_CONTROL_P_COEFFICIENT; - dryBiCartBicarbMixVolumeKpGain.ovInitData = 0.0F; - dryBiCartBicarbMixVolumeKpGain.override = OVERRIDE_RESET; - - dryBiCartBicarbMixVolumeKiGain.data = BICARB_VOL_CONTROL_I_COEFFICIENT; - dryBiCartBicarbMixVolumeKiGain.ovData = BICARB_VOL_CONTROL_I_COEFFICIENT; - dryBiCartBicarbMixVolumeKiGain.ovInitData = 0.0F; - dryBiCartBicarbMixVolumeKiGain.override = OVERRIDE_RESET; - - dryBiCartBicarbTargetConductivity.data = DRY_BICARB_TARGET_CONDUCTIVITY; - dryBiCartBicarbTargetConductivity.ovData = DRY_BICARB_TARGET_CONDUCTIVITY; - dryBiCartBicarbTargetConductivity.ovInitData = 0.0F; - dryBiCartBicarbTargetConductivity.override = OVERRIDE_RESET; - - dryBiCartBicarbDeltaConductivity.data = DRY_BICARB_DELTA_CONDUCTIVITY; - dryBiCartBicarbDeltaConductivity.ovData = DRY_BICARB_DELTA_CONDUCTIVITY; - dryBiCartBicarbDeltaConductivity.ovInitData = 0.0F; - dryBiCartBicarbDeltaConductivity.override = OVERRIDE_RESET; - - dryBiCartDialysateTargetConductivity.data = DRY_ACID_BICARB_TARGET_CONDUCTIVITY; - dryBiCartDialysateTargetConductivity.ovData = DRY_ACID_BICARB_TARGET_CONDUCTIVITY; - dryBiCartDialysateTargetConductivity.ovInitData = 0.0F; - dryBiCartDialysateTargetConductivity.override = OVERRIDE_RESET; - - dryBiCartDialysateDeltaConductivity.data = DRY_ACID_BICARB_DELTA_CONDUCTIVITY; - dryBiCartDialysateDeltaConductivity.ovData = DRY_ACID_BICARB_DELTA_CONDUCTIVITY; - dryBiCartDialysateDeltaConductivity.ovInitData = 0.0F; - dryBiCartDialysateDeltaConductivity.override = OVERRIDE_RESET; - - dryBiCartUpperCartPressure.data = DRY_BICART_FILL_COMPLETE_SUPPLY_PRESSURE; - dryBiCartUpperCartPressure.ovData = DRY_BICART_FILL_COMPLETE_SUPPLY_PRESSURE; + dryBiCartUpperCartPressure.data = DRY_BICART_FILL_COMPLETE_SUPPLY_PRESSURE_PSI; + dryBiCartUpperCartPressure.ovData = DRY_BICART_FILL_COMPLETE_SUPPLY_PRESSURE_PSI; dryBiCartUpperCartPressure.ovInitData = 0.0F; dryBiCartUpperCartPressure.override = OVERRIDE_RESET; - dryBiCartLowerCartPressure.data = DRY_BICART_FILL_INITIATE_SUPPLY_PRESSURE; - dryBiCartLowerCartPressure.ovData = DRY_BICART_FILL_INITIATE_SUPPLY_PRESSURE; + dryBiCartLowerCartPressure.data = DRY_BICART_FILL_INITIATE_SUPPLY_PRESSURE_PSI; + dryBiCartLowerCartPressure.ovData = DRY_BICART_FILL_INITIATE_SUPPLY_PRESSURE_PSI; dryBiCartLowerCartPressure.ovInitData = 0.0F; dryBiCartLowerCartPressure.override = OVERRIDE_RESET; - dryBiCartMixControlInterval.data = BICARB_MIX_CONTROL_INTERVAL; - dryBiCartMixControlInterval.ovData = BICARB_MIX_CONTROL_INTERVAL; - dryBiCartMixControlInterval.ovInitData = 0; - dryBiCartMixControlInterval.override = OVERRIDE_RESET; - dryBiCartPressureDecayStartTimeFlag = FALSE; + dryBiCartFillVentTimeOut = FALSE; dryBiCartFillStartTime = 0; lastFillDurationInMS = 0; currentFillDurationInMS = 0; @@ -402,15 +272,8 @@ dryBiCartDrainCondStableCount = 0; dryBiCartDrainTimePeriod = 0; dryBiCartPersistanceStartTime = 0; + drybicartPersistenceOnLowercartPressureStartTime = 0; prevDryBicartState = DRY_BICART_IDLE; - - initializePIController( PI_CONTROLLER_ID_BICARB_VOL, 0.0F,\ - getBicarbKpGainCoefficient(), getBicarbKiGainCoefficient(),\ - MIN_BICARB_VOLUME_ML, MAX_BICARB_VOLUME_ML, FALSE, MIX_NO_FEED_FORWARD ); - - initializePIController( PI_CONTROLLER_ID_ACID_VOL, 0.0F, \ - getAcidKpGainCoefficient(), getAcidKiGainCoefficient(),\ - MIN_ACID_VOLUME_ML, MAX_ACID_VOLUME_ML, FALSE, MIX_NO_FEED_FORWARD ); } /*********************************************************************//** @@ -428,73 +291,24 @@ /*********************************************************************//** * @brief - * The execDialysateCompositionMixingController function executes the dialysate mixing controller. - * @details \b Inputs: dialysateMix[ mixId ].dialysateMixingState - * @details \b Outputs:dialysateMix[ mixId ].dialysateMixingState - * @details \b Alarms: ALARM_ID_DD_SOFTWARE_FAULT if invalid dialysate mixing - * state machine found. - * @return none - *************************************************************************/ -void execDialysateCompositionMixingController( void ) -{ - DIALYSATE_MIXING_ID_T mixId; - - for ( mixId = DIALYSATE_MIX_ID_FIRST; mixId < NUM_OF_DIALYSATE_MIXING_ID; mixId++ ) - { - switch ( dialysateMix[ mixId ].dialysateMixingState ) - { - case DIALYSATE_MIXING_OPEN_LOOP_STATE: - dialysateMix[ mixId ].dialysateMixingState = handleDialysateMixOpenLoopState( mixId ); - break; - - case DIALYSATE_MIXING_RAMP_UP_STATE: - dialysateMix[ mixId ].dialysateMixingState = handleDialysateMixRampToTargetState( mixId ); - break; - - case DIALYSATE_MIXING_CONTROL_TO_TARGET_STATE: - dialysateMix[ mixId ].dialysateMixingState = handleDialysateMixControlToTargetState( mixId ); - break; - - default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DD_SOFTWARE_FAULT, SW_FAULT_ID_DIALYSATE_MIX_EXEC_INVALID_STATE, mixId ) - dialysateMix[ mixId ].dialysateMixingState = DIALYSATE_MIXING_OPEN_LOOP_STATE; - break; - } - } -} - -/*********************************************************************//** - * @brief * The execDryBicart function executes the set of dry bicarbonate cartridge fill, * bicarbonate chamber fill and bicarbonate cartridge drain related state machines one at time - * @details \b Inputs: dryBicartStartRequest - * @details \b Outputs: none + * @details \b Inputs: dryBicartStartRequest, prevDryBicartState + * @details \b Outputs: prevDryBicartState + * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT when wrong dry bicart state machine is + * invoked. * @return none. *************************************************************************/ void execDryBicart( void ) { - //( TRUE == getBicarbChamberFillRequested() ) - //supply dryBicartStartRequest = DRY_BICART_SUPPLY ; - //( TRUE == getBicartFillRequested() ) - //fill dryBicartStartRequest = DRY_BICART_INITIAL_FILL; - //( TRUE == getBicartDrainRequested() ) - //drain dryBicartStartRequest = DRY_BICART_DRAIN; - //else idle dryBicartStartRequest = DRY_BICART_IDLE; - updateDrybicartOperation(); - - if( getCurrentBalancingChamberExecState() > BAL_CHAMBER_STATE_IDLE ) - { - // closed loop bicarb and acid mixing controller - execDialysateCompositionMixingController(); - } - // top level state machine selection DRY_BICART_OPERATION_T dryBicartState = dryBicartStartRequest; switch ( dryBicartState ) { case DRY_BICART_IDLE: // run as idle state + updateDrybicartOperation(); break; case DRY_BICART_INITIAL_FILL: @@ -512,6 +326,11 @@ execDryBicartDrainMode(); break; + case DRY_BICART_DEPRESSURIZE: + // Dry bicart depressurize + execDryBicartDepressurize(); + break; + default: // software fault, dryBicartStartRequest = DRY_BICART_IDLE; @@ -610,14 +429,10 @@ bicarbChamberFillExecState = handleBicarbChamberCartridgeFillWaterEndState(); break; - case BICARB_CHAMBER_CHECK_LEVEL_STATE: - bicarbChamberFillExecState = handleBicarbChamberCheckLevelState(); + case BICARB_CHAMBER_SUPPLY_STATE: + bicarbChamberFillExecState = handleBicarbChamberSupplyStart(); break; - case BICARB_CHAMBER_FILL_STATE: - bicarbChamberFillExecState = handleBicarbChamberFillState(); - break; - case BICARB_CHAMBER_PRESSURE_CHECK_STATE: bicarbChamberFillExecState = handleBicarbChamberPressureCheckState(); break; @@ -678,44 +493,53 @@ /*********************************************************************//** * @brief - * TThe getBicarbMixVol function gets the bicarb mix volume provided by the controller - * @details \b Inputs: dryBiCartBicarbMixVolume - * @details \b Outputs: none - * @return TRUE if successful, FALSE if not. + * The execDryBicartDepressurize function executes the dry bicart depressurize state machine. + * @details \b Inputs: dryBiCartDepressurizeExecState + * @details \b Outputs: dryBiCartDepressurizeExecState + * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT when wrong dry bicart depressurize state + * invoked. + * @return current state. *************************************************************************/ -F32 getBicarbMixVol( void ) +U32 execDryBicartDepressurize( void ) { - F32 result = getF32OverrideValue( &dryBiCartBicarbMixVolume ); + // execute drybicart depressurize state machines + switch ( dryBiCartDepressurizeExecState ) + { + case DRY_BICART_DEPRESSURIZE_START_STATE: + dryBiCartDepressurizeExecState = handleDryBicartDepressurizeStartState(); + break; - return result; -} + case DRY_BICART_DEPRESSURIZE_VENT_START_STATE: + dryBiCartDepressurizeExecState = handleDryBicartDepressurizeVentStartState(); + break; -/*********************************************************************//** - * @brief - * The getAcidMixVol function gets acid mix volume provided by the controller - * @details \b Inputs: dryBiCartAcidMixVolume - * @details \b Outputs: none - * @return TRUE if successful, FALSE if not. - *************************************************************************/ -F32 getAcidMixVol( void ) -{ - F32 result = getF32OverrideValue( &dryBiCartAcidMixVolume ); + case DRY_BICART_DEPRESSURIZE_VENT_END_STATE: + dryBiCartDepressurizeExecState = handleDryBicartDepressurizeVentEndState(); + break; - return result; + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DD_SOFTWARE_FAULT, SW_FAULT_ID_DRY_BICART_DEPRESURIZE_INVALID_EXEC_STATE, dryBiCartDepressurizeExecState ); + break; + } + + return dryBiCartDepressurizeExecState; } /*********************************************************************//** * @brief * The setBicarbChamberFillRequested function sets the chmaberFillRequest * flag value to be True. - * @details \b Inputs: dryBiCartFillRequested, dryBiCartDrainRequested + * @details \b Inputs: dryBiCartFillRequested, dryBiCartDrainRequested, + * dryBiCartDepressurizeRequested * @details \b Outputs: bicarbChamberFillRequested * @return TRUE if successful, FALSE if not. *************************************************************************/ BOOL setBicarbChamberFillRequested( void ) { BOOL result = FALSE; - BOOL requestStatus = getU32OverrideValue( &dryBiCartFillRequested ) || getU32OverrideValue( &dryBiCartDrainRequested ); + BOOL requestStatus = getU32OverrideValue( &dryBiCartFillRequested ) ||\ + getU32OverrideValue( &dryBiCartDrainRequested ) || \ + getU32OverrideValue( &dryBiCartDepressurizeRequested ); // ensure that there is no other operation in progress before new request set to true if ( FALSE == requestStatus ) @@ -751,14 +575,17 @@ * @brief * The setBicartFillRequested function sets the bicart fill request * flag value to be True. - * @details \b Inputs: bicarbChamberFillRequested, dryBiCartDrainRequested + * @details \b Inputs: bicarbChamberFillRequested, dryBiCartDrainRequested, + * dryBiCartDepressurizeRequested * @details \b Outputs: dryBiCartFillRequested * @return TRUE if successful, FALSE if not. *************************************************************************/ BOOL setBicartFillRequested( void ) { BOOL result = FALSE; - BOOL requestStatus = getU32OverrideValue( &bicarbChamberFillRequested ) || getU32OverrideValue( &dryBiCartDrainRequested ); + BOOL requestStatus = getU32OverrideValue( &bicarbChamberFillRequested ) \ + || getU32OverrideValue( &dryBiCartDrainRequested ) || \ + getU32OverrideValue( &dryBiCartDepressurizeRequested ) ; //ensure that there is no other operation in progress before new request set to true if ( FALSE == requestStatus ) @@ -794,14 +621,17 @@ * @brief * The setBicartDrainRequested function sets the bicart drain request * flag value to be True. - * @details \b Inputs: bicarbChamberFillRequested,dryBiCartFillRequested + * @details \b Inputs: bicarbChamberFillRequested,dryBiCartFillRequested, + * dryBiCartDepressurizeRequested * @details \b Outputs: dryBiCartDrainRequested * @return TRUE if successful, FALSE if not. *************************************************************************/ BOOL setBicartDrainRequested( void ) { BOOL result = FALSE; - BOOL requestStatus = getU32OverrideValue( &bicarbChamberFillRequested ) || getU32OverrideValue( &dryBiCartFillRequested ); + BOOL requestStatus = getU32OverrideValue( &bicarbChamberFillRequested ) || \ + getU32OverrideValue( &dryBiCartFillRequested ) || \ + getU32OverrideValue( &dryBiCartDepressurizeRequested ); //ensure that there is no other operation in progress before new request set to true if ( FALSE == requestStatus ) @@ -835,162 +665,64 @@ /*********************************************************************//** * @brief - * The getCurrentDryBiCartFillExecState function returns the current state - * of the dry bicart fill state. - * @details \b Inputs: dryBiCartFillExecState - * @details \b Outputs: none - * @return the current state of dry bicart fill exec state. - *************************************************************************/ -DRY_BICART_FILL_EXEC_STATE_T getCurrentDryBiCartFillExecState( void ) -{ - return dryBiCartFillExecState; -} - -/*********************************************************************//** - * @brief - * The setAcidMixVol function sets acid mix volume provided by the controller + * The setBicartDepressurizeRequested function sets the bicart depressurize request * flag value to be True. - * @details \b Inputs: none - * @details \b Outputs: dryBiCartAcidMixVolume - * @param targetValue acid mix volume + * @details \b Inputs: bicarbChamberFillRequested,dryBiCartFillRequested,dryBiCartDrainRequested + * @details \b Outputs: dryBiCartDepressurizeRequested * @return TRUE if successful, FALSE if not. *************************************************************************/ -static void setAcidMixVol( F32 targetValue ) +BOOL setBicartDepressurizeRequested( void ) { - dryBiCartAcidMixVolume.data = targetValue; -} + BOOL result = FALSE; + BOOL requestStatus = getU32OverrideValue( &bicarbChamberFillRequested ) || \ + getU32OverrideValue( &dryBiCartFillRequested ) || \ + getU32OverrideValue( &dryBiCartDrainRequested ); -/*********************************************************************//** - * @brief - * The setBicarbMixVol function sets bicarb Mix volume provided by the controller - * @details \b Inputs: none - * @details \b Outputs: dryBiCartBicarbMixVolume - * @param targetValue bicarb Mix volume - * @return TRUE if successful, FALSE if not. - *************************************************************************/ -static void setBicarbMixVol( F32 targetValue ) -{ - dryBiCartBicarbMixVolume.data = targetValue; -} + //ensure that there is no other operation in progress before new request set to true + if ( FALSE == requestStatus ) + { + dryBiCartDepressurizeRequested.data = TRUE; + result = TRUE; + } -/*********************************************************************//** - * @brief - * The getBicarbDeltaConductivity function gets the delta target conductivity - * @details \b Inputs: dryBiCartBicarbDeltaConductivity - * @details \b Outputs: none - * @return TRUE if successful, FALSE if not. - *************************************************************************/ -static F32 getBicarbDeltaConductivity( void ) -{ - F32 result = getF32OverrideValue( &dryBiCartBicarbDeltaConductivity ); - return result; } /*********************************************************************//** * @brief - * The getBicarbTargetConductivity function gets the target conductivity - * @details \b Inputs: dryBiCartBicarbTargetConductivity + * The getBicartDepressurizeRequested function gets the bicart depressurize request + * flag value + * @details \b Inputs: dryBiCartDepressurizeRequested * @details \b Outputs: none * @return TRUE if successful, FALSE if not. *************************************************************************/ -static F32 getBicarbTargetConductivity( void ) +BOOL getBicartDepressurizeRequested( void ) { - F32 result = getF32OverrideValue( &dryBiCartBicarbTargetConductivity ); + BOOL result = FALSE; - return result; -} + if ( ( TRUE == getU32OverrideValue( &dryBiCartDepressurizeRequested ) ) ) + { + result = TRUE; + } -/*********************************************************************//** - * @brief - * The getAcidBicarbDeltaConductivity function gets the acid bicarb delta target conductivity - * @details \b Inputs: dryBiCartAcidBicarbMixDeltaConductivity - * @details \b Outputs: none - * @return TRUE if successful, FALSE if not. - *************************************************************************/ -static F32 getAcidBicarbDeltaConductivity( void ) -{ - F32 result = getF32OverrideValue( &dryBiCartDialysateDeltaConductivity ); - return result; } /*********************************************************************//** * @brief - * The getAcidBicarbTargetConductivity function gets the acid bicarb delta target conductivity - * @details \b Inputs: dryBiCartAcidBicarbMixTargetConductivity + * The getCurrentDryBiCartFillExecState function returns the current state + * of the dry bicart fill state. + * @details \b Inputs: dryBiCartFillExecState * @details \b Outputs: none - * @return TRUE if successful, FALSE if not. + * @return the current state of dry bicart fill exec state. *************************************************************************/ -static F32 getAcidBicarbTargetConductivity( void ) +DRY_BICART_FILL_EXEC_STATE_T getCurrentDryBiCartFillExecState( void ) { - F32 result = getF32OverrideValue( &dryBiCartDialysateTargetConductivity ); - - return result; + return dryBiCartFillExecState; } /*********************************************************************//** * @brief - * The getBicarbKpGainCoefficient function gets the Kp Gain for closed loop - * bicarb mix volume control - * @details \b Inputs: dryBiCartBicarbMixVolumeKpGain - * @details \b Outputs: none - * @return TRUE if successful, FALSE if not. - *************************************************************************/ -static F32 getBicarbKpGainCoefficient( void ) -{ - F32 result = getF32OverrideValue( &dryBiCartBicarbMixVolumeKpGain ); - - return result; -} - -/*********************************************************************//** - * @brief - * The getBicarbKiGainCoefficient function gets the Kp Gain for closed loop - * bicarb mix volume control - * @details \b Inputs: dryBiCartBicarbMixVolumeKiGain - * @details \b Outputs: none - * @return TRUE if successful, FALSE if not. - *************************************************************************/ -static F32 getBicarbKiGainCoefficient( void ) -{ - F32 result = getF32OverrideValue( &dryBiCartBicarbMixVolumeKiGain ); - - return result; -} - -/*********************************************************************//** - * @brief - * The getAcidKpGainCoefficient function gets the Kp Gain for closed loop - * acid mix volume control - * @details \b Inputs: dryBiCartAcidMixVolumeKpGain - * @details \b Outputs: none - * @return TRUE if successful, FALSE if not. - *************************************************************************/ -static F32 getAcidKpGainCoefficient( void ) -{ - F32 result = getF32OverrideValue( &dryBiCartAcidMixVolumeKpGain ); - - return result; -} - -/*********************************************************************//** - * @brief - * The getAcidKiGainCoefficient function gets the Ki Gain for closed loop - * acid mix volume control - * @details \b Inputs: dryBiCartAcidMixVolumeKiGain - * @details \b Outputs: none - * @return TRUE if successful, FALSE if not. - *************************************************************************/ -static F32 getAcidKiGainCoefficient( void ) -{ - F32 result = getF32OverrideValue( &dryBiCartAcidMixVolumeKiGain ); - - return result; -} - -/*********************************************************************//** - * @brief * The getDryBicartUpperCartPressure function gets the bicart upper threshold pressure * @details \b Inputs: dryBiCartUpperCartPressure * @details \b Outputs: none @@ -1019,165 +751,8 @@ /*********************************************************************//** * @brief - * The getDryBicartMixControlInterval function gets the bicart mix control interval - * @details \b Inputs: dryBiCartMixControlInterval - * @details \b Outputs: none - * @return TRUE if successful, FALSE if not. - *************************************************************************/ -static U32 getDryBicartMixControlInterval( void ) -{ - U32 result = getU32OverrideValue( &dryBiCartMixControlInterval ); - - return result; -} - -/*********************************************************************//** - * @brief - * The handleDialysateOpenLoopState function handles the dialysate open loop state of - * the dialysate mix controller state machine. - * @details \b Inputs: D17_COND, D29_COND - * @details \b Outputs: none - * @param mixId dialysate mix id to run the dialysate mix - * @return next state for the controller state machine - *************************************************************************/ -static DIALYSATE_MIXING_STATE_T handleDialysateMixOpenLoopState( DIALYSATE_MIXING_ID_T mixId ) -{ - DIALYSATE_MIXING_STATE_T result; - - if ( BICARB_MIX_ID == mixId ) - { - F32 measuredBicarbConductivity = getFilteredConductivity( D17_COND ); - - result = DIALYSATE_MIXING_OPEN_LOOP_STATE; - - if ( TRUE != getTestConfigStatus( TEST_CONFIG_DD_ENABLE_DOSING_OPEN_LOOP_CONTROL ) ) - { - // if measured conductivity crossed (targetConductivity- deltaConuctivity) , switch to closed loop - if ( measuredBicarbConductivity >= ( getBicarbTargetConductivity() - getBicarbDeltaConductivity() ) ) - { - result = DIALYSATE_MIXING_RAMP_UP_STATE; - } - } - } - else if ( ACID_MIX_ID == mixId ) - { - F32 measuredAcidBicarbMixConductivity = getFilteredConductivity( D27_COND ); - - result = DIALYSATE_MIXING_OPEN_LOOP_STATE; - - if ( TRUE != getTestConfigStatus( TEST_CONFIG_DD_ENABLE_DOSING_OPEN_LOOP_CONTROL ) ) - { - // if measured conductivity crossed (targetConductivity- deltaConuctivity) , switch to closed loop - if ( measuredAcidBicarbMixConductivity >= ( getAcidBicarbTargetConductivity() - getAcidBicarbDeltaConductivity() ) ) - { - result = DIALYSATE_MIXING_RAMP_UP_STATE; - } - } - } - - return result; -} - -/*********************************************************************//** - * @brief - * The handleDialysatePumpRampToTargetState function handles the dialysate ramp to target state of - * the dialysate mix controller state machine. - * @details \b Inputs: none - * @details \b Outputs: none - * @param mixId mix id to - * @return next state for the controller state machine - *************************************************************************/ -static DIALYSATE_MIXING_STATE_T handleDialysateMixRampToTargetState( DIALYSATE_MIXING_ID_T mixId ) -{ - DIALYSATE_MIXING_STATE_T state = DIALYSATE_MIXING_RAMP_UP_STATE; - - if ( BICARB_MIX_ID == mixId ) - { - resetPIController( PI_CONTROLLER_ID_BICARB_VOL, DEFAULT_BICARB_VOLUME_ML, MIX_NO_FEED_FORWARD ); - - state = DIALYSATE_MIXING_CONTROL_TO_TARGET_STATE; - } - else if ( ACID_MIX_ID == mixId ) - { - resetPIController( PI_CONTROLLER_ID_ACID_VOL, DEFAULT_ACID_VOLUME_ML, MIX_NO_FEED_FORWARD ); - - state = DIALYSATE_MIXING_CONTROL_TO_TARGET_STATE; - } - - return state; -} - -/*********************************************************************//** - * @brief - * The handleDialysatePumpControlToTargetState function handles the control to - * target state of the dialysate mix controller state machine.. - * @details \b Inputs: D17_COND, D29_COND, controlTimerCounter - * @details \b Outputs:none - * @return next state of the controller state machine - *************************************************************************/ -static DIALYSATE_MIXING_STATE_T handleDialysateMixControlToTargetState( DIALYSATE_MIXING_ID_T mixId ) -{ - DIALYSATE_MIXING_STATE_T state = DIALYSATE_MIXING_CONTROL_TO_TARGET_STATE; - - if ( BICARB_MIX_ID == mixId ) - { - // control interval for bicarb - if ( ( ++dialysateMix[ mixId ].controlTimerCounter >= getDryBicartMixControlInterval() ) ) - { - F32 bicarbMixVol; - - dialysateMix[ mixId ].controlTimerCounter = 0; - - // Control based on the measured and target conductivity - PI_CONTROLLER_SIGNALS_DATA debugBicarbControl; - F32 measuredBicarbConductivity; - - measuredBicarbConductivity = getFilteredConductivity( D17_COND ); - - if ( fabs ( getBicarbTargetConductivity() - measuredBicarbConductivity ) > BICARB_DEADBAND_CONTROL ) - { - bicarbMixVol = runPIController( PI_CONTROLLER_ID_BICARB_VOL, getBicarbTargetConductivity(), measuredBicarbConductivity ); - //Set bicarb mix vol only if its above dead band threshold, else use the previous set vlaue - setBicarbMixVol( bicarbMixVol ); - } - - debugBicarbControl = getDebugPIControllerSignals( PI_CONTROLLER_ID_BICARB_VOL ); - - memcpy( (void*)&bicarbControlSignals, (void*)&debugBicarbControl, sizeof(PI_CONTROLLER_SIGNALS_DATA) ); - } - } - - if ( ACID_MIX_ID == mixId ) - { - // control interval for acid - if ( ( ++dialysateMix[ mixId ].controlTimerCounter >= ACID_MIX_CONTROL_INTERVAL ) ) - { - F32 acidMixVol; - - dialysateMix[ mixId ].controlTimerCounter = 0; - - F32 measuredAcidBicarbMixConductivity = getFilteredConductivity( D27_COND ); - PI_CONTROLLER_SIGNALS_DATA debugAcidControl; - - if ( fabs ( getAcidBicarbTargetConductivity() - measuredAcidBicarbMixConductivity ) > ACID_DEADBAND_CONTROL ) - { - acidMixVol = runPIController( PI_CONTROLLER_ID_ACID_VOL, getAcidBicarbTargetConductivity(), measuredAcidBicarbMixConductivity ); - // set acid mix volume if its above dead band threshold , else use previous set value - setAcidMixVol( acidMixVol ); - } - - debugAcidControl = getDebugPIControllerSignals( PI_CONTROLLER_ID_ACID_VOL ); - - memcpy( (void*)&acidControlSignals, (void*)&debugAcidControl, sizeof(PI_CONTROLLER_SIGNALS_DATA) ); - } - } - - return state; -} - -/*********************************************************************//** - * @brief - * The updateDrybicartOperation function initiates the water fill, bicarbonate supply or bicarbonate drain + * The updateDrybicartOperation function initiates bicart fill water, bicarbonate supply, + * bicart drain or bicart depressurize * operation * @details \b Inputs: none * @details \b Outputs: dryBicartStartRequest @@ -1200,6 +775,10 @@ //drain dryBicartStartRequest = DRY_BICART_DRAIN; } + else if ( TRUE == getBicartDepressurizeRequested() ) + { + dryBicartStartRequest = DRY_BICART_DEPRESSURIZE; + } else { // idle @@ -1240,7 +819,7 @@ * The handleDryBicartFillWaterStartState function initiates the water fill * into dry bicarbonate cartridge. * @details \b Inputs: none - * @details \b Outputs: valve states + * @details \b Outputs: valve states, dryBiCartFillStartTime * @return the next drybicart fill water end state. *************************************************************************/ static DRY_BICART_FILL_EXEC_STATE_T handleDryBicartFillWaterStartState( void ) @@ -1260,6 +839,7 @@ //Initiate timer to measure length of fill dryBiCartFillStartTime = getMSTimerCount(); + state = DRY_BICART_FILL_WATER_END_STATE; return state; @@ -1269,51 +849,44 @@ * @brief * The handleDryBicartFillWaterEndState function closes the valve opened * for Dry bicart fill. - * @details \b Inputs: D66 pressure sensor reading - * @details \b Outputs: valve states + * @details \b Inputs: D66 pressure sensor reading, dryBiCartPersistenceStartTime + * @details \b Outputs: valve states, lastFillDurationInMS, currentFillDurationInMS, + * biCartFillCycleCounter * @return the next drybicart degas start state. *************************************************************************/ static DRY_BICART_FILL_EXEC_STATE_T handleDryBicartFillWaterEndState( void ) { DRY_BICART_FILL_EXEC_STATE_T state = DRY_BICART_FILL_WATER_END_STATE; F32 d66Pressure = getFilteredPressure( D66_PRES ); + F32 fillCompletePressure = GET_FILL_COMPLETE_PRESSURE_PSI; - // Close dry bicart inlet water if D66 pressure is 11 PSIr - if ( d66Pressure >= DRY_BICART_FILL_COMPLETE_PRESSURE ) + // Close dry bicart inlet water if D66 pressure is 17 PSI + if ( d66Pressure >= fillCompletePressure ) { - // start the persistance timer once d66 reaches 11 PSI - if ( 0 == dryBiCartPersistanceStartTime ) + // start the persistence timer once d66 reaches 17 PSI + if ( 0 == dryBiCartPersistenceStartTime ) { - dryBiCartPersistanceStartTime = getMSTimerCount(); + dryBiCartPersistenceStartTime = getMSTimerCount(); } - //check the the maximum cart pressure during persistence on D66 - if ( d66Pressure >= MAX_DRY_BICART_FILL_COMPLETE_PRESSURE ) - { - setValveState( D65_VALV, VALVE_STATE_CLOSED ); - lastFillDurationInMS = currentFillDurationInMS; - currentFillDurationInMS = calcTimeSince( dryBiCartFillStartTime ); - //Increment number of fill cycle - biCartFillCycleCounter++; - dryBiCartPersistanceStartTime = 0; - - state = DRY_BICART_DEGAS_START_STATE; - } // 200 ms persistence on D66 pressure since pressure overshoot just after D65 opening - if ( TRUE == didTimeout( dryBiCartPersistanceStartTime, DRY_BICART_FILL_PERSISTANCE_DURATION_MS ) ) + if ( TRUE == didTimeout( dryBiCartPersistenceStartTime, DRY_BICART_PERSISTENCE_DURATION_MS ) ) { setValveState( D65_VALV, VALVE_STATE_CLOSED ); lastFillDurationInMS = currentFillDurationInMS; currentFillDurationInMS = calcTimeSince( dryBiCartFillStartTime ); //Increment number of fill cycle biCartFillCycleCounter++; - dryBiCartPersistanceStartTime = 0; + dryBiCartPersistenceStartTime = 0; state = DRY_BICART_DEGAS_START_STATE; } } - + else + { + dryBiCartPersistenceStartTime = 0; + } //TODO:Handle timeout alarm for opening D65 valve too long return state; @@ -1324,7 +897,7 @@ * The handleDryBicartDegasStartState function actuates the vent valve * present in the Bicart assembly. * @details \b Inputs: none - * @details \b Outputs: valve states + * @details \b Outputs: valve states, dryBiCartFillStartTime, dryBiCartPersistenceStartTime * @return the next drybicart degas end state. *************************************************************************/ static DRY_BICART_FILL_EXEC_STATE_T handleDryBicartDegasStartState( void ) @@ -1333,23 +906,24 @@ //Close D64 setValveState( D64_VALV, VALVE_STATE_CLOSED); - //Vent bicart to chamber F setValveState( D85_VALV, VALVE_STATE_OPEN ); - //Initiate timer to measure length of bicart vent - dryBiCartFillStartTime = getMSTimerCount(); - state = DRY_BICART_DEGAS_END_STATE; + dryBiCartFillStartTime = getMSTimerCount(); + dryBiCartPersistenceStartTime = 0; + state = DRY_BICART_DEGAS_END_STATE; + return state; } /*********************************************************************//** * @brief * The handleDryBicartDegasEndState function open vent valve present in * the F Chamber (Bicarb) and closes the vent valve once pressure is met. - * @details \b Inputs: D66 pressure sensor reading, dryBiCartFillStartTime - * @details \b Outputs: valve states + * @details \b Inputs: D66 pressure sensor reading, dryBiCartFillStartTime, + * dryBiCartPersistenceStartTime + * @details \b Outputs: valve states, dryBiCartFillVentTimeOut * @return the next drybicart fill duration check state. *************************************************************************/ static DRY_BICART_FILL_EXEC_STATE_T handleDryBicartDegasEndState( void ) @@ -1361,17 +935,37 @@ { //Vent chamber F to atmosphere setValveState( D64_VALV, VALVE_STATE_OPEN ); - } - // D66 pressure drops to below 1.5 PSI or time out - if ( ( d66Pressure <= DRY_BICART_FILL_VENT_COMPLETE_PRESSURE ) || - ( TRUE == didTimeout( dryBiCartFillStartTime, DRY_BICART_FILL_VENT_MAX_TIME_MS ) ) ) - { - setValveState( D64_VALV, VALVE_STATE_OPEN ); - setValveState( D85_VALV, VALVE_STATE_CLOSED ); - state = DRY_BICART_FILL_DURATION_CHECK_STATE; - } + // D66 pressure drops to below 1.5 PSI + if ( ( d66Pressure <= DRY_BICART_FILL_VENT_COMPLETE_PRESSURE_PSI ) ) + { + // start the persistence timer once d66 reaches 1.5 PSI + if ( 0 == dryBiCartPersistenceStartTime ) + { + dryBiCartPersistenceStartTime = getMSTimerCount(); + } + // 200 ms persistence on D66 pressure since pressure drop below 1.5 PSI after opening D64 + if ( TRUE == didTimeout( dryBiCartPersistenceStartTime, DRY_BICART_PERSISTENCE_DURATION_MS ) ) + { + setValveState( D85_VALV, VALVE_STATE_CLOSED ); + dryBiCartPersistenceStartTime = 0; + state = DRY_BICART_FILL_DURATION_CHECK_STATE; + } + } + else if ( ( TRUE == didTimeout( dryBiCartFillStartTime, DRY_BICART_FILL_VENT_MAX_TIME_MS ) ) ) + { + setValveState( D85_VALV, VALVE_STATE_CLOSED ); + dryBiCartPersistenceStartTime = 0; + dryBiCartFillVentTimeOut = TRUE; + + state = DRY_BICART_FILL_DURATION_CHECK_STATE; + } + else + { + dryBiCartPersistenceStartTime = 0; + } + } //TODO: Alarm when vent timeout exceeded, but pressure not dropped below expected PSI (0 psi) return state; @@ -1382,22 +976,20 @@ * The handleDryBicartFillDurationCheckState function checks the fill duration * between last and current fill and exits if the duration is reduced over period * or number of fill cycle done. - * @details \b Inputs: currentFillDurationInMS, lastFillDurationInMS, biCartMaxFillCycleCount + * @details \b Inputs: biCartMaxFillCycleCount, biCartFillCycleCounter * D66 pressure, biCartFillCycleCounter, dryBiCartFillRequested - * @details \b Outputs: valve states + * @details \b Outputs: valve states, dryBiCartFillStartTime, dryBiCartFillVentTimeOut * @return the next drybicart fill state. *************************************************************************/ static DRY_BICART_FILL_EXEC_STATE_T handleDryBicartFillDurationCheckState( void ) { DRY_BICART_FILL_EXEC_STATE_T state = DRY_BICART_FILL_DURATION_CHECK_STATE; - U32 diffInFillDuration = abs( currentFillDurationInMS - lastFillDurationInMS ); U32 dryBiCartMaxFillCount = getU32OverrideValue( &biCartMaxFillCycleCount ); F32 d66Pressure = getFilteredPressure( D66_PRES ); - // Fill time shortens over the period or number of fill cycle exceeded - if ( ( ( currentFillDurationInMS <= DRY_BICART_FILL_DURATION_MIN_MS ) && - ( diffInFillDuration <= DRY_BICART_FILL_DURATION_DIFF_MS ) ) || - ( biCartFillCycleCounter >= dryBiCartMaxFillCount ) ) + // Fill for a minimum 3 cycle count and vent time out or number of fill cycle exceeded max fill cycle count + if ( ( ( biCartFillCycleCounter >= DRY_BICART_MINIMUM_FILL_CYCLE_CNT ) && ( TRUE == dryBiCartFillVentTimeOut ) ) || + ( biCartFillCycleCounter >= dryBiCartMaxFillCount ) ) { state = DRY_BICART_FILL_COMPLETE_STATE; @@ -1412,18 +1004,10 @@ } else { - // Stop drycart fill if fill request flag is set to false. - if ( FALSE == getU32OverrideValue( &dryBiCartFillRequested ) ) - { - dryBiCartFillRequested.data = FALSE; + dryBiCartFillVentTimeOut = FALSE; - state = DRY_BICART_START_STATE; - } - else - { - // transition to next fill cycle and degas - state = DRY_BICART_FILL_WATER_START_STATE; - } + // transition to next fill cycle and degas + state = DRY_BICART_FILL_WATER_START_STATE; } return state; @@ -1433,27 +1017,33 @@ * @brief * The handleDryBicartFillCompleteState function terminal for the dry bicart * fill states. - * @details \b Inputs: dryBiCartFillRequested - * @details \b Outputs: valve states + * @details \b Inputs: dryBiCartFillRequested, dryBiCartFillStartTime + * @details \b Outputs: valve states, lastFillDurationInMS, biCartFillCycleCounter, + * dryBiCartFillVentTimeOut, dryBiCartFillRequested * @return the next drybicart start state. *************************************************************************/ static DRY_BICART_FILL_EXEC_STATE_T handleDryBicartFillCompleteState( void ) { // stay in this state till bicart request is cleared so that fill complete status can be checked DRY_BICART_FILL_EXEC_STATE_T state = DRY_BICART_FILL_COMPLETE_STATE; - // TODO: check whether its required or not, right now in pre-gen need to check the fill complete status - // TODO: implement the broadcast message for fill complete + // TODO: in pre-gen, need to check the fill complete status if ( TRUE == didTimeout( dryBiCartFillStartTime, DRY_BICART_FILL_COMPLETE_TIME_MS ) ) { - currentFillDurationInMS = 0; - lastFillDurationInMS = 0; - biCartFillCycleCounter = 0; + currentFillDurationInMS = 0; + lastFillDurationInMS = 0; + biCartFillCycleCounter = 0; + dryBiCartFillVentTimeOut = FALSE; // Go idle dryBiCartFillRequested.data = FALSE; // for this request override also cleared dryBiCartFillRequested.ovData = FALSE; + + // when fill complete , need to close the d3 valve + hydChamberWaterInletControl(); + state = DRY_BICART_START_STATE; + dryBicartStartRequest = DRY_BICART_IDLE; } return state; @@ -1498,7 +1088,7 @@ * @brief * The handleBicarbChamberCartridgeFillWaterStartState function fill water in the dry bicart * @details \b Inputs: none - * @details \b Outputs: valve states + * @details \b Outputs: valve states, dryBiCarbSupplyStartTime, dryBiCartPersistenceStartTime * @return the next bicarb cartridge fill water end state. *************************************************************************/ static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberCartridgeFillWaterStartState( void ) @@ -1515,61 +1105,61 @@ setValveState( D64_VALV, VALVE_STATE_OPEN ); dryBiCarbSupplyStartTime = getMSTimerCount(); - dryBiCartPersistanceStartTime = 0; + dryBiCartPersistenceStartTime = 0; return state; } /*********************************************************************//** * @brief * The handleBicarbChamberCartridgeFillWaterStartState function stop water in the dry bicart - * @details \b Inputs: D66 pressure + * @details \b Inputs: D66 pressure, dryBiCartPersistenceStartTime * @details \b Outputs: valve states * @return the next bicarb chamber check level state. *************************************************************************/ static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberCartridgeFillWaterEndState( void ) { BICARB_CHAMBER_FILL_EXEC_STATE_T state = BICARB_CARTRIDGE_FILL_WATER_END_STATE; F32 d66Pressure = getFilteredPressure( D66_PRES ); + F32 fillCompletePressure = GET_FILL_COMPLETE_PRESSURE_PSI; - // check D66 is 11 PSI - if ( d66Pressure >= DRY_BICART_FILL_COMPLETE_PRESSURE ) + // check D66 pressure greater than or equal to 17 PSI + if ( d66Pressure >= fillCompletePressure ) { - // persistence - // check the the maximum cart pressure during persistence on D66 - if ( d66Pressure >= MAX_DRY_BICART_FILL_COMPLETE_PRESSURE ) + // start the persistence timer once d66 reaches 17 PSI + if ( 0 == dryBiCartPersistenceStartTime ) { - // Close water inlet valve as D66 pressure reaches max PSI(14-15) after persistence - setValveState( D65_VALV, VALVE_STATE_CLOSED ); - dryBiCartPersistanceStartTime = 0; - - state = BICARB_CHAMBER_CHECK_LEVEL_STATE; + dryBiCartPersistenceStartTime = getMSTimerCount(); } + // 200 ms persistence on D66 pressure since pressure overshoot just after D65 opening - if ( TRUE == didTimeout( dryBiCartPersistanceStartTime, DRY_BICART_FILL_PERSISTANCE_DURATION_MS ) ) + if ( TRUE == didTimeout( dryBiCartPersistenceStartTime, DRY_BICART_PERSISTENCE_DURATION_MS ) ) { // Close water inlet valve as D66 pressure reaches 11 PSI after persistence setValveState( D65_VALV, VALVE_STATE_CLOSED ); - dryBiCartPersistanceStartTime = 0; + dryBiCartPersistenceStartTime = 0; - state = BICARB_CHAMBER_CHECK_LEVEL_STATE; + state = BICARB_CHAMBER_SUPPLY_STATE; } } + else + { + dryBiCartPersistenceStartTime = 0; + } return state; } /*********************************************************************//** * @brief - * The handleBicarbChamberCheckLevelState function checks the bicarb level(D63) - * in the chamber F and decides to open the bicarb inlet valve (D80). - * @details \b Inputs: D63 level + * The handleBicarbChamberSupplyStart function open the bicarb inlet valve (D80). + * @details \b Inputs: none * @details \b Outputs: valve states, dryBiCarbSupplyStartTime * @return the next bicarb chamber fill state. *************************************************************************/ -static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberCheckLevelState( void ) +static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberSupplyStart( void ) { - BICARB_CHAMBER_FILL_EXEC_STATE_T state = BICARB_CHAMBER_CHECK_LEVEL_STATE; + BICARB_CHAMBER_FILL_EXEC_STATE_T state = BICARB_CHAMBER_PRESSURE_CHECK_STATE; // TODO: set supply in progress to inform BC control to disable alarm setValveState( D64_VALV, VALVE_STATE_CLOSED ); @@ -1579,52 +1169,18 @@ // Start a timer when bicarb supply started to chamber F dryBiCarbSupplyStartTime = getMSTimerCount(); - state = BICARB_CHAMBER_FILL_STATE; + // TODO: alarm - // TODO: alarm - return state; } /*********************************************************************//** * @brief - * The handleBicarbChamberFillState function checks the D66 pressure & - * decides to open D65 and Close the inlet valve (D80) once level reached - * @details \b Inputs: D66 pressure sensor reading, D63 level - * @details \b Outputs: valve states - * @return the next bicarb chamber pressure check state. - *************************************************************************/ -static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberFillState( void ) -{ - BICARB_CHAMBER_FILL_EXEC_STATE_T state = BICARB_CHAMBER_FILL_STATE; - F32 d66Pressure = getFilteredPressure( D66_PRES ); - LVL_STATE_T bicarbChamberLevel = getBicarbChamberLevelStatus(); - - // Once level reached, close the valve - if ( LVL_STATE_HIGH == bicarbChamberLevel ) - { - setValveState( D80_VALV, VALVE_STATE_CLOSED ); - state = BICARB_CHAMBER_PRESSURE_CHECK_STATE; - } - // Open water inlet valve to fill the bicart - else if ( d66Pressure <= getDryBicartLowerCartPressure() ) - { - setValveState( D65_VALV, VALVE_STATE_OPEN ); - state = BICARB_CHAMBER_PRESSURE_CHECK_STATE; - } - else - { - state = BICARB_CHAMBER_PRESSURE_CHECK_STATE; - } - - return state; -} - -/*********************************************************************//** - * @brief * The handleBicarbChamberPressureCheckStartState function actuates the vent valve * present in the Bicart assembly. - * @details \b Inputs: D66 Pressure, D63 level, dryBiCarbSupplyStartTime + * @details \b Inputs: D66 Pressure, D63 level, dryBiCarbSupplyStartTime, + * dryBiCarbSypplyVentStartTime, dryBiCartPersistenceStartTime, bicarbChamberLevel, + * dryBiCartPressureDecayStartTimeFlag, drybicartPersistenceOnLowercartPressureStartTime * @details \b Outputs: dryBiCarbSypplyVentStartTime * @return the next bicarb chamber Supply Vent Start state. *************************************************************************/ @@ -1634,8 +1190,8 @@ F32 d66Pressure = getFilteredPressure( D66_PRES ); LVL_STATE_T bicarbChamberLevel = getBicarbChamberLevelStatus(); - // Once level reached, close the valve, timeout is for safety in case level sensor didn't work, or else its a time based filling - if ( ( LVL_STATE_HIGH == bicarbChamberLevel ) || + // Once upper level reached high , close the valve, timeout is for safety in case level sensor didn't work(10 sec), or else its a time based filling (3 sec) + if ( ( LVL_STATE_HIGH == bicarbChamberLevel ) || ( TRUE == didTimeout( dryBiCarbSupplyStartTime, DRY_BICART_SUPPLY_VALVE_D80_OPEN_TIME_MS ) ) ) { setValveState( D65_VALV, VALVE_STATE_CLOSED ); @@ -1646,14 +1202,15 @@ dryBiCarbSupplyStartTime = getMSTimerCount(); dryBiCartPressureDecayStartTimeFlag = TRUE; } - // do not close the D80 valve, wait till d66 decays to 3 PSI if ( TRUE == didTimeout( dryBiCarbSupplyStartTime, DRY_BICART_SUPPLY_COMPLETE_TIME_MS ) ) { if ( d66Pressure <= getDryBicartLowerCartPressure() ) { setValveState( D80_VALV, VALVE_STATE_CLOSED ); setValveState( D85_VALV, VALVE_STATE_OPEN ); + + dryBiCarbSupplyStartTime = getMSTimerCount(); dryBiCarbSypplyVentStartTime = getMSTimerCount(); state = BICARB_SUPPLY_VENT_START_STATE; @@ -1662,29 +1219,39 @@ } else if ( d66Pressure <= getDryBicartLowerCartPressure() ) { + // start persistence timer + if ( 0 == drybicartPersistenceOnLowercartPressureStartTime ) + { + drybicartPersistenceOnLowercartPressureStartTime = getMSTimerCount(); + } + // 200 ms persistence on D66 pressure + if ( TRUE == didTimeout( drybicartPersistenceOnLowercartPressureStartTime, DRY_BICART_PERSISTENCE_DURATION_MS ) ) + { setValveState( D65_VALV, VALVE_STATE_OPEN ); + drybicartPersistenceOnLowercartPressureStartTime = 0; + } } // Close water inlet valve as fill is complete. else if ( d66Pressure >= getDryBicartUpperCartPressure() ) { - // start persistance timer - if ( 0 == dryBiCartPersistanceStartTime ) + // start persistence timer + if ( 0 == dryBiCartPersistenceStartTime ) { - dryBiCartPersistanceStartTime = getMSTimerCount(); + dryBiCartPersistenceStartTime = getMSTimerCount(); } - //check the the maximum cart pressure during persistence on D66 - if ( d66Pressure >= MAX_DRY_BICART_FILL_COMPLETE_PRESSURE ) - { - setValveState( D65_VALV, VALVE_STATE_CLOSED ); - dryBiCartPersistanceStartTime = 0; - } + // 200 ms persistence on D66 pressure since pressure overshoot just after D65 opening - if ( TRUE == didTimeout( dryBiCartPersistanceStartTime, DRY_BICART_FILL_PERSISTANCE_DURATION_MS ) ) + if ( TRUE == didTimeout( dryBiCartPersistenceStartTime, DRY_BICART_PERSISTENCE_DURATION_MS ) ) { setValveState( D65_VALV, VALVE_STATE_CLOSED ); - dryBiCartPersistanceStartTime = 0; + dryBiCartPersistenceStartTime = 0; } } + else + { + dryBiCartPersistenceStartTime = 0; + drybicartPersistenceOnLowercartPressureStartTime = 0; + } return state; } @@ -1693,7 +1260,7 @@ * @brief * The handleBicarbChamberSupplyVentStartState function actuates the vent valve * present in the Bicart assembly. - * @details \b Inputs: dryBiCarbSypplyVentStartTime + * @details \b Inputs: dryBiCarbSypplyVentStartTime, dryBiCartPersistenceStartTime * @details \b Outputs: dryBiCarbSypplyVentStartTime * @return the next bicarb chamber Supply Vent End state. *************************************************************************/ @@ -1702,20 +1269,40 @@ BICARB_CHAMBER_FILL_EXEC_STATE_T state = BICARB_SUPPLY_VENT_START_STATE; F32 d66Pressure = getFilteredPressure( D66_PRES ); - // open D64 after 1 sec - if ( TRUE == didTimeout( dryBiCarbSypplyVentStartTime, DRY_BICART_SUPPLY_VENT_MAX_TIME_MS ) ) + if ( TRUE == didTimeout( dryBiCarbSypplyVentStartTime, DRY_BICART_SUPPLY_VENT_TIME_MS ) ) { + //Vent chamber F to atmosphere setValveState( D64_VALV, VALVE_STATE_OPEN ); - } - // D66 pressure drops to 1.5 or time out - if ( ( d66Pressure <= DRY_BICART_FILL_VENT_COMPLETE_PRESSURE ) || - ( TRUE == didTimeout( dryBiCarbSypplyVentStartTime, DRY_BICART_FILL_VENT_MAX_TIME_MS ) ) ) - { + // D66 pressure drops to below 1.5 PSI or time out + if ( ( d66Pressure <= DRY_BICART_FILL_VENT_COMPLETE_PRESSURE_PSI ) ) + { + // start the persistence timer once d66 reaches if it drops below 1.5 + if ( 0 == dryBiCartPersistenceStartTime ) + { + dryBiCartPersistenceStartTime = getMSTimerCount(); + } + // 200 ms persistence on D66 pressure since pressure overshoot just after D65 opening + if ( TRUE == didTimeout( dryBiCartPersistenceStartTime, DRY_BICART_PERSISTENCE_DURATION_MS ) ) + { + setValveState( D85_VALV, VALVE_STATE_CLOSED ); + dryBiCartPersistenceStartTime = 0; - setValveState( D85_VALV, VALVE_STATE_CLOSED ); + state = BICARB_SUPPLY_VENT_END_STATE; + } + } + else if ( ( TRUE == didTimeout( dryBiCarbSypplyVentStartTime, DRY_BICART_FILL_VENT_MAX_TIME_MS ) ) ) + { + setValveState( D85_VALV, VALVE_STATE_CLOSED ); + dryBiCartPersistenceStartTime = 0; + dryBiCartFillVentTimeOut = TRUE; - state = BICARB_SUPPLY_VENT_END_STATE; + state = BICARB_SUPPLY_VENT_END_STATE; + } + else + { + dryBiCartPersistenceStartTime = 0; + } } return state; @@ -1726,7 +1313,7 @@ * The handleBicarbChamberSupplyVentEndState function actuates the vent valve * present in the Bicart assembly. * @details \b Inputs: dryBiCarbSypplyVentStartTime - * @details \b Outputs: none + * @details \b Outputs: bicarbChamberFillRequested * @return the next bicarb chamber start state. *************************************************************************/ static BICARB_CHAMBER_FILL_EXEC_STATE_T handleBicarbChamberSupplyVentEndState(void) @@ -1741,6 +1328,8 @@ state = BICARB_CHAMBER_START_STATE; + dryBicartStartRequest = DRY_BICART_IDLE; + return state; } @@ -1768,28 +1357,6 @@ dryBiCartDrainTimePeriod = SMALL_DRY_BICART_MAX_DRAIN_TIME_MS; } - // open bypass valve - setValveState( D34_VALV, VALVE_STATE_OPEN ); - - // TODO: open DD drain - setValveState( D53_VALV, VALVE_STATE_OPEN ); - - // TODO: open iofp drain valve - setValveState( M12_VALV, VALVE_STATE_OPEN ); - - // Open balancing chamber valves - valveControlForBCOpenState(); - - //Close D14 valve - setValveState( D14_VALV, VALVE_STATE_CLOSED ); - - // Run D10 In Open loop mode(max speed 200ml/min) - setConcentratePumpTargetSpeed( D10_PUMP, DRAIN_BICART_PUMP_SPEED, DOSING_CONT_VOLUME ); - requestConcentratePumpOn( D10_PUMP ); - - // Run D48 at 1000 rpm,There is no improvement over above 1000 rpm for drain duration. - setDialysatePumpTargetRPM( D48_PUMP, DIAL_PUMP_DRAIN_RPM, TRUE ); - dryBiCartDrainStartTime = getMSTimerCount(); dryBiCartDrainLastCondSampleTime = dryBiCartDrainStartTime; dryBiCartDrainCondStableCount = 0; @@ -1805,20 +1372,40 @@ * The handleDryBicartFluidDrainState function initiates the water drain * from dry bicarbonate cartridge. * @details \b Inputs: none - * @details \b Outputs: none + * @details \b Outputs: valve states * @return the next drybicart drain duration check state. *************************************************************************/ static DRY_BICART_DRAIN_EXEC_STATE_T handleDryBicartFluidDrainState( void ) { DRY_BICART_DRAIN_EXEC_STATE_T state = DRY_BICART_FLUID_DRAIN_STATE; + //Close D14 valve + setValveState( D14_VALV, VALVE_STATE_CLOSED ); + setValveState( D65_VALV, VALVE_STATE_CLOSED ); + setValveState( D35_VALV, VALVE_STATE_CLOSED ); + setValveState( D40_VALV, VALVE_STATE_CLOSED ); + setValveState( D91_VALV, VALVE_STATE_CLOSED ); + setValveState( D81_VALV, VALVE_STATE_CLOSED ); + // 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 D64 valve (vent for equilibrium) setValveState( D64_VALV, VALVE_STATE_OPEN ); + // open bypass valve + setValveState( D34_VALV, VALVE_STATE_OPEN ); + // TODO: open iofp drain valve, remove, this should be handled in IO/FP module. + setValveState( M12_VALV, VALVE_STATE_OPEN ); + // Open balancing chamber valves + valveControlForBCOpenState(); + + // Run D10 In Open loop mode(max speed 200ml/min) + setConcentratePumpTargetSpeed( D10_PUMP, DRAIN_BICART_PUMP_SPEED, DOSING_CONT_VOLUME ); + requestConcentratePumpOn( D10_PUMP ); + + // Run D48 at 1000 rpm,There is no improvement over above 1000 rpm for drain duration. + setDialysatePumpTargetRPM( D48_PUMP, DIAL_PUMP_DRAIN_RPM, TRUE ); + state = DRY_BICART_FLUID_DRAIN_DURATION_CHECK_STATE; return state; @@ -1853,14 +1440,10 @@ setValveState( D64_VALV, VALVE_STATE_CLOSED ); setValveState( D80_VALV, VALVE_STATE_CLOSED ); setValveState( D85_VALV, VALVE_STATE_CLOSED ); - // close bypass valve setValveState( D34_VALV, VALVE_STATE_CLOSED ); - // TODO: close DD drain - setValveState( D53_VALV, VALVE_STATE_CLOSED ); - - // TODO : close iofp drain valve + // TODO : close iofp drain valve, remove this should be handled in IO/FP module. setValveState( M12_VALV, VALVE_STATE_CLOSED ); //close all balancing chamber valves @@ -1913,12 +1496,104 @@ // for this request override also cleared dryBiCartDrainRequested.ovData = FALSE; state = DRY_BICART_DRAIN_START_STATE; + dryBicartStartRequest = DRY_BICART_IDLE; return state; } /*********************************************************************//** * @brief + * The handleDryBicartDepressurizeStartState function set the one time actuation + * for drybicart de-pressurise request. + * @details \b Inputs: dryBiCartDepressurizeRequested + * @details \b Outputs: none + * @return the next drybicart depressurize state. + *************************************************************************/ +static DRY_BICART_DEPRESSURIZE_EXEC_STATE_T handleDryBicartDepressurizeStartState( void ) +{ + DRY_BICART_DEPRESSURIZE_EXEC_STATE_T state = DRY_BICART_DEPRESSURIZE_START_STATE; + + if ( TRUE == getU32OverrideValue( &dryBiCartDepressurizeRequested ) ) + { + setValveState( D85_VALV, VALVE_STATE_OPEN ); + + state = DRY_BICART_DEPRESSURIZE_VENT_START_STATE; + } + + return state; +} + +/*********************************************************************//** + * @brief + * The handleDryBicartDepressurizeVentStartState function handles the vent start state for + * drybicart de-pressurise + * @details \b Inputs: dryBiCarbSypplyVentStartTime, dryBiCartPersistenceStartTime + * @details \b Outputs: none + * @return the next drybicart depressurize state. + *************************************************************************/ +static DRY_BICART_DEPRESSURIZE_EXEC_STATE_T handleDryBicartDepressurizeVentStartState( void ) +{ + DRY_BICART_DEPRESSURIZE_EXEC_STATE_T state = DRY_BICART_DEPRESSURIZE_VENT_START_STATE; + + F32 d66Pressure = getFilteredPressure( D66_PRES ); + + if ( TRUE == didTimeout( dryBiCarbSypplyVentStartTime, DRY_BICART_SUPPLY_VENT_TIME_MS ) ) + { + //Vent chamber F to atmosphere + setValveState( D64_VALV, VALVE_STATE_OPEN ); + + // D66 pressure drops to below 1.5 PSI or time out + if ( ( d66Pressure <= DRY_BICART_FILL_VENT_COMPLETE_PRESSURE_PSI ) ) + { + // start the persistence timer once d66 reaches if it drops below 1.5 + if ( 0 == dryBiCartPersistenceStartTime ) + { + dryBiCartPersistenceStartTime = getMSTimerCount(); + } + // 200 ms persistence on D66 pressure since pressure overshoot just after D65 opening + if ( TRUE == didTimeout( dryBiCartPersistenceStartTime, DRY_BICART_PERSISTENCE_DURATION_MS ) ) + { + setValveState( D85_VALV, VALVE_STATE_CLOSED ); + dryBiCartPersistenceStartTime = 0; + + state = DRY_BICART_DEPRESSURIZE_VENT_END_STATE; + } + } + else + { + dryBiCartPersistenceStartTime = 0; + } + } + + return state; +} + +/*********************************************************************//** + * @brief + * The handleDryBicartDepressurizeVentEndState function handles the vent end state for + * drybicart de-pressurise + * @details \b Inputs: none + * @details \b Outputs: dryBiCartDepressurizeRequested + * @return the next drybicart depressurize state. + *************************************************************************/ +static DRY_BICART_DEPRESSURIZE_EXEC_STATE_T handleDryBicartDepressurizeVentEndState( void ) +{ + DRY_BICART_DEPRESSURIZE_EXEC_STATE_T state = DRY_BICART_DEPRESSURIZE_VENT_END_STATE; + + // Go idle + dryBiCartDepressurizeRequested.data = FALSE; + // for this request override also cleared + dryBiCartDepressurizeRequested.ovData = FALSE; + + state = DRY_BICART_DEPRESSURIZE_START_STATE; + + dryBicartStartRequest = DRY_BICART_IDLE; + + return state; +} + +/*********************************************************************//** + * @brief * The publishDryBicartData function broadcasts the dry bicart * data at defined interval. * @details \b Inputs: dryBiCartDataPublicationTimerCounter @@ -1946,22 +1621,6 @@ data.dryBiCartType = getU32OverrideValue( &dryBiCartType ); data.dryBiCartDrainTimePeriod = dryBiCartDrainTimePeriod; - // TODO: remove after feature testing - memcpy( &data.dryBiCartBicarbMixingControl, &bicarbControlSignals, sizeof( PI_CONTROLLER_SIGNALS_DATA ) ); - // TODO: remove after feature testing - memcpy( &data.dryBiCartAcidMixingControl , &acidControlSignals , sizeof( PI_CONTROLLER_SIGNALS_DATA ) ); - - data.dryBiCartAcidMixVolume = getAcidMixVol(); - data.dryBiCartBicarbMixVolume = getBicarbMixVol(); - - // TODO: remove after feature testing - data.dryBiCartBicarbMixingKPgain = getBicarbKpGainCoefficient(); - // TODO: remove after feature testing - data.dryBiCartBicarbMixingKIgain = getBicarbKiGainCoefficient(); - - data.dryBiCartAcidMixingKPgain = getAcidKpGainCoefficient(); - data.dryBiCartAcidMixingKIgain = getAcidKiGainCoefficient(); - broadcastData( MSG_ID_DD_DRY_BICART_DATA, COMM_BUFFER_OUT_CAN_DD_BROADCAST, (U08*)&data, sizeof( DRY_BICART_DATA_T ) ); dryBiCartDataPublicationTimerCounter = 0; @@ -2061,208 +1720,38 @@ /*********************************************************************//** * @brief - * The testDryBiCartTypeOverride function selects small/large cartridge type - * @details \b Inputs: dryBiCartType - * @details \b Outputs: dryBiCartType - * @param message set message from Dialin for small/large bicart type + * The testDryBiCartDepressurizeRequestOverride function starts/stops the bicart + * depressurize request + * @details \b Inputs: dryBiCartDepressurizeRequested + * @details \b Outputs: dryBiCartDepressurizeRequested + * @param message set message from Dialin which includes the bicart + * depressurize request start/stop. * @return TRUE if set request is successful, FALSE if not *************************************************************************/ -BOOL testDryBiCartTypeOverride( MESSAGE_T *message ) +BOOL testDryBiCartDepressurizeRequestOverride( MESSAGE_T *message ) { - BOOL result = u32Override( message, &dryBiCartType, FALSE, TRUE ); + BOOL result = u32Override( message, &dryBiCartDepressurizeRequested, FALSE, TRUE ); return result; } /*********************************************************************//** * @brief - * The testDryBiCartAcidMixVolumeOverride function sets the override value - * of the acid concentrate mixing volume. - * @details Inputs: dryBiCartAcidMixVolume - * @details Outputs: dryBiCartAcidMixVolume - * @param message Override message from Dialin which includes the override - * value to override the acid concentrate mixing volume. - * @return TRUE if override successful, FALSE if not + * The testDryBiCartTypeOverride function selects small/large cartridge type + * @details \b Inputs: dryBiCartType + * @details \b Outputs: dryBiCartType + * @param message set message from Dialin for small/large bicart type + * @return TRUE if set request is successful, FALSE if not *************************************************************************/ -BOOL testDryBiCartAcidMixVolumeOverride( MESSAGE_T *message ) +BOOL testDryBiCartTypeOverride( MESSAGE_T *message ) { - BOOL result = f32Override( message, &dryBiCartAcidMixVolume ); + BOOL result = u32Override( message, &dryBiCartType, FALSE, TRUE ); return result; } /*********************************************************************//** * @brief - * The testDryBiCartBicarbMixVolumeOverride function sets the override value - * of the bicarb concentrate mixing volume. - * @details Inputs: dryBiCartBicarbMixVolume - * @details Outputs: dryBiCartBicarbMixVolume - * @param message Override message from Dialin which includes the override - * value to override the bicarb concentrate mixing volume. - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testDryBiCartBicarbMixVolumeOverride( MESSAGE_T *message ) -{ - BOOL result = f32Override( message, &dryBiCartBicarbMixVolume ); - - return result; -} - -/*********************************************************************//** - * @brief - * The testDryBiCartBicarbMixVolControlKpGainOverride function sets the override value - * of the Kp gain coefficient for closed loop bicarb mixing volume control - * @details Inputs: dryBiCartBicarbMixVolumeKpGain - * @details Outputs: dryBiCartBicarbMixVolumeKpGain - * @param message Override message from Dialin which includes the override - * value to override the bicarb concentrate mixing volume kp gain. - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testDryBiCartBicarbMixVolControlKpGainOverride( MESSAGE_T *message ) -{ - BOOL result = f32Override( message, &dryBiCartBicarbMixVolumeKpGain ); - - initializePIController( PI_CONTROLLER_ID_BICARB_VOL, 0.0F,\ - getBicarbKpGainCoefficient(), getBicarbKiGainCoefficient(),\ - MIN_BICARB_VOLUME_ML, MAX_BICARB_VOLUME_ML, FALSE, MIX_NO_FEED_FORWARD ); - - return result; -} - -/*********************************************************************//** - * @brief - * The testDryBiCartBicarbMixVolControlKiGainOverride function sets the override value - * of the Ki gain coefficient for closed loop bicarb concentrate mixing volume control - * @details Inputs: dryBiCartBicarbMixVolumeKiGain - * @details Outputs: dryBiCartBicarbMixVolumeKiGain - * @param message Override message from Dialin which includes the override - * value to override the bicarb concentrate mixing volume ki gain. - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testDryBiCartBicarbMixVolControlKiGainOverride( MESSAGE_T *message ) -{ - BOOL result = f32Override( message, &dryBiCartBicarbMixVolumeKiGain ); - - initializePIController( PI_CONTROLLER_ID_BICARB_VOL, 0.0F,\ - getBicarbKpGainCoefficient(), getBicarbKiGainCoefficient(),\ - MIN_BICARB_VOLUME_ML, MAX_BICARB_VOLUME_ML, FALSE, MIX_NO_FEED_FORWARD ); - - return result; -} - -/*********************************************************************//** - * @brief - * The testDryBiCartAcidMoxVolControlKpGainOverride function sets the override value - * of the Kp gain coefficient for closed loop acid concentrate mixing volume control - * @details Inputs: dryBiCartAcidMixVolumeKpGain - * @details Outputs: dryBiCartAcidMixVolumeKpGain - * @param message Override message from Dialin which includes the override - * value to override the acid concentrate mixing volume kp gain. - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testDryBiCartAcidMixVolControlKpGainOverride( MESSAGE_T *message ) -{ - BOOL result = f32Override( message, &dryBiCartAcidMixVolumeKpGain ); - - initializePIController( PI_CONTROLLER_ID_ACID_VOL, 0.0F, \ - getAcidKpGainCoefficient(), getAcidKiGainCoefficient(),\ - MIN_ACID_VOLUME_ML, MAX_ACID_VOLUME_ML, FALSE, MIX_NO_FEED_FORWARD ); - - return result; -} - -/*********************************************************************//** - * @brief - * The testDryBiCartAcidMixVolControlKiGainOverride function sets the override value - * of the Ki gain coefficient for closed loop acid concentrate mixing volume control - * @details Inputs: dryBiCartAcidMixVolumeKiGain - * @details Outputs: dryBiCartAcidMixVolumeKiGain - * @param message Override message from Dialin which includes the override - * value to override the bicarb concentrate mixing volume ki gain. - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testDryBiCartAcidMixVolControlKiGainOverride( MESSAGE_T *message ) -{ - BOOL result = f32Override( message, &dryBiCartAcidMixVolumeKiGain ); - - initializePIController( PI_CONTROLLER_ID_ACID_VOL, 0.0F, \ - getAcidKpGainCoefficient(), getAcidKiGainCoefficient(),\ - MIN_ACID_VOLUME_ML, MAX_ACID_VOLUME_ML, FALSE, MIX_NO_FEED_FORWARD ); - - return result; -} - -/*********************************************************************//** - * @brief - * The testDryBiCartTargetConductivityOverride function sets the override value - * of target conductivity for closed loop control - * @details Inputs: dryBiCartTargetConductivity - * @details Outputs: dryBiCartTargetConductivity - * @param message Override message from Dialin which includes the override - * value to override the bicarb concentrate target conductivity. - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testDryBiCartTargetConductivityOverride( MESSAGE_T *message ) -{ - BOOL result = f32Override( message, &dryBiCartBicarbTargetConductivity ); - - return result; -} - -/*********************************************************************//** - * @brief - * The testDryBiCartDeltaConductivityOverride function sets the override value - * of delta target conductivity for closed loop control - * @details Inputs: dryBiCartDeltaConductivity - * @details Outputs: dryBiCartDeltaConductivity - * @param message Override message from Dialin which includes the override - * value to override the bicarb concentrate delta target conductivity. - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testDryBiCartDeltaConductivityOverride( MESSAGE_T *message ) -{ - BOOL result = f32Override( message, &dryBiCartBicarbDeltaConductivity ); - - return result; -} - -/*********************************************************************//** - * @brief - * The testDryBiCartAcidBicarbTargetConductivityOverride function sets the override value - * of acid bicarb mix target conductivity for closed loop control - * @details Inputs: dryBiCartAcidBicarbMixTargetConductivity - * @details Outputs: dryBiCartAcidBicarbMixTargetConductivity - * @param message Override message from Dialin which includes the override - * value to override the acid bicarb mix target conductivity. - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testDryBiCartAcidBicarbTargetConductivityOverride( MESSAGE_T *message ) -{ - BOOL result = f32Override( message, &dryBiCartDialysateTargetConductivity ); - - return result; -} - -/*********************************************************************//** - * @brief - * The testDryBiCartAcidBicarbDeltaConductivityOverride function sets the override value - * of delta target conductivity for acid bicarb mix closed loop control - * @details Inputs: dryBiCartDeltaConductivity - * - * @details Outputs: dryBiCartDeltaConductivity - * @param message Override message from Dialin which includes the override - * value to override the acid bicarb mix target delta conductivity. - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testDryBiCartAcidBicarbDeltaConductivityOverride( MESSAGE_T *message ) -{ - BOOL result = f32Override( message, &dryBiCartDialysateDeltaConductivity ); - - return result; -} - -/*********************************************************************//** - * @brief * The testDryBiCartUpperCartPressureOverride function sets the override value * of bicart upper cart pressure * @details Inputs: dryBiCartUpperCartPressure @@ -2295,21 +1784,4 @@ return result; } -/*********************************************************************//** - * @brief - * The testDryBiCartMixControlIntervalOverride function sets the override value - * of mix control interval - * @details Inputs: dryBiCartMixControlInterval - * @details Outputs: dryBiCartMixControlInterval - * @param message Override message from Dialin which includes the override - * value to override the bicart lower pressure. - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testDryBiCartMixControlIntervalOverride( MESSAGE_T *message ) -{ - BOOL result = u32Override( message, &dryBiCartMixControlInterval, 0, BICARB_MIX_CONTROL_INTERVAL ); - - return result; -} - /**@}*/