Index: firmware/App/Modes/Dialysis.c =================================================================== diff -u -ra4f5ed3748870d287a4c2c6fcc003fc4d3b9233f -r2acda0ccdd00334bec87bfbe61c4e78e867925de --- firmware/App/Modes/Dialysis.c (.../Dialysis.c) (revision a4f5ed3748870d287a4c2c6fcc003fc4d3b9233f) +++ firmware/App/Modes/Dialysis.c (.../Dialysis.c) (revision 2acda0ccdd00334bec87bfbe61c4e78e867925de) @@ -23,12 +23,13 @@ #include "Dialysis.h" #include "DialInFlow.h" #include "DialOutFlow.h" +#include "ModeTreatment.h" #include "ModeTreatmentParams.h" #include "OperationModes.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" #include "Timers.h" -#include "ModeTreatment.h" +#include "Valves.h" /** * @addtogroup Dialysis @@ -41,11 +42,15 @@ #define MAX_UF_RATE_ACCURACY_ERROR_ML_HR 100.0 ///< Maximum ultrafiltration rate accuracy error in mL/hr over each hour of treatment. #define MAX_UF_RATE_ACCURACY_ERROR_PCT 0.05 ///< Minimum ultrafilteration rate accuracy in percentage of set point (5%) over each hour of treatment. #define MAX_UF_ACCURACY_ERROR_ML 250.0 ///< Maximum ultrafiltration accuracy error in mL over the entire treatment. -/// Ultrafiltration rate accuracy check interval count +/// Saline bolus data broadcast interval (ms/task time) count. +#define SALINE_BOLUS_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) +/// Ultrafiltration rate accuracy check interval count. #define UF_ACCURACY_CHECK_INTERVAL ((1 * MIN_PER_HOUR * SEC_PER_MIN * MS_PER_SECOND) / TASK_GENERAL_INTERVAL) -#define MAX_SALINE_VOLUME_DELIVERED 800.0 ///< Maximum saline volume delivered for a treatment. +#define MAX_SALINE_VOLUME_DELIVERED 800 ///< Maximum saline volume delivered for a treatment. #define SALINE_BOLUS_RATE_ML_MIN 150 ///< Fixed rate for saline bolus delivery. +#define MIN_SALINE_BOLUS_VOLUME_PCT 0.8 ///< Minimum saline bolus volume measured by independent means (as % of target). +#define MAX_SALINE_BOLUS_VOLUME_PCT 1.2 ///< Maximum saline bolus volume measured by independent means (as % of target). // ********** private data ********** @@ -68,12 +73,16 @@ static F32 setUFRate; ///< Currently set ultrafiltration rate (from prescription). static F32 maxUFRateAccuracyError_Ml_hr; ///< Minimum ultrafiltration rate accuracy over 1 hour duration (5% or 100 mL, whichever is greater). +static U32 salineBolusBroadcastTimerCtr; ///< Saline bolus data broadcast timer counter used to schedule when to transmit data. static BOOL salineBolusStartRequested; ///< Flag indicates a saline bolus start has been requested by user. static BOOL salineBolusAbortRequested; ///< Flag indicates a salien bolus abort has been requested by user. static BOOL salineBolusAutoResumeUF; ///< Flag indicates UF should be auto-resumed after saline bolus completes. static F32 totalSalineVolumeDelivered; ///< Volume (mL) in total of saline delivered so far (cumulative for all boluses including current one). static F32 bolusSalineVolumeDelivered; ///< Volume (mL) of current bolus delivered so far. static F32 bolusSalineVolumeDelivered_Safety; ///< Volume (mL) of current bolus delivered so far according to safety monitor. +static U32 bolusSalineRotorCount; ///< Blood pump rotor count during saline bolus to calculate saline bolus volume independently for safety. +static U32 bolusSalineLastRotorCount; ///< Last blood pump count from last saline bolus volume update. +static U32 bolusSalineLastVolumeTimeStamp; ///< Time stamp for last saline volume update. static U32 uFAccuracyCheckTimerCtr; ///< Timer counter to determine when next to check ultrafiltration accuracy. static F32 lastUFVolumeChecked; ///< Starting ultrafiltration volume for accuracy check. @@ -90,12 +99,15 @@ static UF_STATE_T handleUFCompletedState( DIALYSIS_STATE_T *dialysisState ); static SALINE_BOLUS_STATE_T handleSalineBolusIdleState( DIALYSIS_STATE_T *dialysisState ); +static SALINE_BOLUS_STATE_T handleSalineBolusWait4Pumps2Stop( DIALYSIS_STATE_T *dialysisState ); static SALINE_BOLUS_STATE_T handleSalineBolusInProgressState( DIALYSIS_STATE_T *dialysisState ); static SALINE_BOLUS_STATE_T handleSalineBolusMaxDeliveredState( DIALYSIS_STATE_T *dialysisState ); static void checkUFAccuracyAndVolume( void ); static void updateUFVolumes( void ); +static void publishSalineBolusData( void ); + /*********************************************************************//** * @brief * The initDialysis function initializes the Dialysis sub-mode module. @@ -114,8 +126,6 @@ refUFVolume = 0.0; measUFVolume = 0.0; -// resStartVolume[ DG_RESERVOIR_1 ] = 0.0; -// resStartVolume[ DG_RESERVOIR_2 ] = 0.0; resFinalVolume[ DG_RESERVOIR_1 ] = 0.0; resFinalVolume[ DG_RESERVOIR_2 ] = 0.0; measUFVolumeFromPriorReservoirs = 0.0; @@ -129,12 +139,19 @@ setUFRate = 0.0; maxUFRateAccuracyError_Ml_hr = MAX_UF_RATE_ACCURACY_ERROR_ML_HR; - salineBolusStartRequested = FALSE; - salineBolusAbortRequested = FALSE; - salineBolusAutoResumeUF = FALSE; + salineBolusBroadcastTimerCtr = 0; + totalSalineVolumeDelivered = 0.0; uFAccuracyCheckTimerCtr = 0; lastUFVolumeChecked = 0.0; + + // initialize saline bolus variables + salineBolusStartRequested = FALSE; + salineBolusAbortRequested = FALSE; + salineBolusAutoResumeUF = FALSE; + bolusSalineVolumeDelivered = 0.0; + bolusSalineVolumeDelivered_Safety = 0.0; + bolusSalineRotorCount = 0; } /*********************************************************************//** @@ -206,6 +223,7 @@ lastUFTimeStamp = getMSTimerCount(); // send dialysate outlet pump latest UF volumes setDialOutUFVolumes( refUFVolume, measUFVolume ); + // restart pumps #ifndef RUN_PUMPS_OPEN_LOOP setBloodPumpTargetFlowRate( setBloodFlowRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); @@ -271,7 +289,7 @@ { rejReason = REQUEST_REJECT_REASON_SALINE_BOLUS_IN_PROGRESS; } - else if ( totalSalineVolumeDelivered >= MAX_SALINE_VOLUME_DELIVERED ) + else if ( totalSalineVolumeDelivered >= (F32)MAX_SALINE_VOLUME_DELIVERED ) { rejReason = REQUEST_REJECT_REASON_SALINE_MAX_VOLUME_REACHED; } @@ -512,6 +530,9 @@ SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_DIALYSIS_INVALID_STATE, currentDialysisState ) break; } + + // publish saline bolus data at set interval (whether we're delivering one or not) + publishSalineBolusData(); } /*********************************************************************//** @@ -577,6 +598,10 @@ currentSalineBolusState = handleSalineBolusIdleState( &result ); break; + case SALINE_BOLUS_STATE_WAIT_FOR_PUMPS_STOP: + currentSalineBolusState = handleSalineBolusWait4Pumps2Stop( &result ); + break; + case SALINE_BOLUS_STATE_IN_PROGRESS: currentSalineBolusState = handleSalineBolusInProgressState( &result ); break; @@ -809,13 +834,72 @@ if ( TRUE == salineBolusStartRequested ) { salineBolusStartRequested = FALSE; + // cmd all pumps to stop +#ifndef RUN_PUMPS_OPEN_LOOP + setBloodPumpTargetFlowRate( 0, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); + setDialInPumpTargetFlowRate( 0, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); + setDialOutPumpTargetRate( 0, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); +#else + setBloodPumpTargetFlowRate( 0, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); + setDialInPumpTargetFlowRate( 0, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); + setDialOutPumpTargetRate( 0, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); +#endif + // begin saline bolus + result = SALINE_BOLUS_STATE_WAIT_FOR_PUMPS_STOP; } return result; } /*********************************************************************//** * @brief + * The handleSalineBolusWait4Pumps2Stop function handles the wait for pumps + * to stop state of the saline bolus state machine. + * @details Inputs: none + * @details Outputs: + * @param dialysisState next dialysis state + * @return next saline bolus state + *************************************************************************/ +static SALINE_BOLUS_STATE_T handleSalineBolusWait4Pumps2Stop( DIALYSIS_STATE_T *dialysisState ) +{ + SALINE_BOLUS_STATE_T result = SALINE_BOLUS_STATE_WAIT_FOR_PUMPS_STOP; + + if ( ( FALSE == isBloodPumpRunning() ) && ( FALSE == isDialInPumpRunning() ) && ( FALSE == isDialOutPumpRunning() ) ) + { + // reset bolus data before we start + bolusSalineVolumeDelivered = 0.0; + bolusSalineVolumeDelivered_Safety = 0.0; + bolusSalineRotorCount = 0; + bolusSalineLastRotorCount = getBloodPumpRotorCount(); + bolusSalineLastVolumeTimeStamp = getMSTimerCount(); + // TODO - turn off accumulation of dialysate usage + + // bypass dialyzer + setValvePosition( VDI, VALVE_POSITION_C_CLOSE ); + setValvePosition( VDO, VALVE_POSITION_C_CLOSE ); + // switch to saline bag + setValvePosition( VBA, VALVE_POSITION_C_CLOSE ); + // start blood pump at saline bolus rate +#ifndef RUN_PUMPS_OPEN_LOOP + setBloodPumpTargetFlowRate( SALINE_BOLUS_FLOW_RATE, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); +#else + setBloodPumpTargetFlowRate( SALINE_BOLUS_FLOW_RATE, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); +#endif + // start dialysate inlet pump at re-circ rate +#ifndef RUN_PUMPS_OPEN_LOOP + setDialInPumpTargetFlowRate( DIALYSATE_FLOW_RATE_FOR_RECIRC, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); +#else + setDialInPumpTargetFlowRate( DIALYSATE_FLOW_RATE_FOR_RECIRC, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); +#endif + // begin saline bolus + result = SALINE_BOLUS_STATE_IN_PROGRESS; + } + + return result; +} + +/*********************************************************************//** + * @brief * The handleSalineBolusInProgressState function handles the in-progress state of the * saline bolus state machine. * @details Inputs: none @@ -826,7 +910,72 @@ static SALINE_BOLUS_STATE_T handleSalineBolusInProgressState( DIALYSIS_STATE_T *dialysisState ) { SALINE_BOLUS_STATE_T result = SALINE_BOLUS_STATE_IN_PROGRESS; + F32 timeSinceLastVolumeUpdateMin = (F32)calcTimeSince( bolusSalineLastVolumeTimeStamp ) / (F32)( MS_PER_SECOND * SEC_PER_MIN ); + F32 bolusTargetVolume = getTreatmentParameterU32( TREATMENT_PARAM_SALINE_BOLUS_VOLUME ); + F32 bldFlowRate = getMeasuredBloodFlowRate(); // TODO - should I use raw flow instead of filtered here??? + F32 volSinceLastUpdateMl = bldFlowRate * timeSinceLastVolumeUpdateMin; + U32 bldPumpMotorCount = getBloodPumpMotorCount(); + U32 bldPumpMotorDelta = u32DiffWithWrap( bolusSalineLastRotorCount, bldPumpMotorCount ); + // update saline bolus volumes + bolusSalineLastVolumeTimeStamp = getMSTimerCount(); + bolusSalineVolumeDelivered += volSinceLastUpdateMl; + totalSalineVolumeDelivered += volSinceLastUpdateMl; + bolusSalineRotorCount += bldPumpMotorDelta; + bolusSalineLastRotorCount = bldPumpMotorCount; + bolusSalineVolumeDelivered_Safety = ( (F32)bolusSalineRotorCount * VOLUME_PER_BP_MOTOR_REV_ML ); // TODO - include upstream pressure compensation to this calc + + // TODO - check for empty saline bag + + // determine if we've reached maximum saline delivery volume + if ( ( totalSalineVolumeDelivered >= (F32)MAX_SALINE_VOLUME_DELIVERED ) ) + { + result = SALINE_BOLUS_STATE_MAX_DELIVERED; + } + else + { + // determine if bolus is complete + if ( bolusSalineVolumeDelivered >= bolusTargetVolume ) + { + // if safety thinks we've under-delivered the bolus, throw a fault + if ( bolusSalineVolumeDelivered_Safety < ( bolusTargetVolume * MIN_SALINE_BOLUS_VOLUME_PCT ) ) + { + // TODO - fault + } + result = SALINE_BOLUS_STATE_IDLE; + } + // determine if safety thinks we've over-delivered the bolus + else if ( bolusSalineVolumeDelivered_Safety > ( bolusTargetVolume * MAX_SALINE_BOLUS_VOLUME_PCT ) ) + { + // TODO - fault + result = SALINE_BOLUS_STATE_IDLE; + } + } + + // are we stopping the bolus and resuming treatment? + if ( result != SALINE_BOLUS_STATE_IN_PROGRESS ) + { + // hard stop blood and dialysate pumps + signalBloodPumpHardStop(); + signalDialInPumpHardStop(); + // send last saline bolus data + salineBolusBroadcastTimerCtr = SALINE_BOLUS_DATA_PUB_INTERVAL; + publishSalineBolusData(); + // switch back to dialyzer + + // switch back to patient artery + + if ( TRUE == salineBolusAutoResumeUF ) + { + // TODO - resume dialysis + // TODO - resume UF + } + // TODO - resume dialysate usage + + // resume dialysis + startDialysis(); + } + return result; } @@ -843,11 +992,36 @@ { SALINE_BOLUS_STATE_T result = SALINE_BOLUS_STATE_MAX_DELIVERED; + // nothing to be done here - this is a terminal state for a given treatment - no more saline may be delivered to patient + return result; } /*********************************************************************//** * @brief + * The publishSalineBolusData function handles the max saline delivered + * state of the saline bolus state machine. This is a terminal state. + * @details Inputs: none + * @details Outputs: + * @param dialysisState next dialysis state + * @return next saline bolus state + *************************************************************************/ +static void publishSalineBolusData( void ) +{ + if ( ++salineBolusBroadcastTimerCtr >= SALINE_BOLUS_DATA_PUB_INTERVAL ) + { + SALINE_BOLUS_DATA_PAYLOAD_T data; + + data.maxSalineVolumeMl = MAX_SALINE_VOLUME_DELIVERED; + data.cumSalineVolumeMl = totalSalineVolumeDelivered; + data.bolSalineVolumeMl = bolusSalineVolumeDelivered; + broadcastSalineBolusData( data ); + salineBolusBroadcastTimerCtr = 0; + } +} + +/*********************************************************************//** + * @brief * The checkUF function checks ultrafiltration accuracy for the last * hour and checks total UF volume. Triggers an alarm if out of spec. * @details