Index: firmware/App/Modes/Dialysis.c =================================================================== diff -u -rde5a0d43bdef611d963d11855bc958a8d8899a09 -rd3142d47c87c30968bda8f2a0a6759b40cc72711 --- firmware/App/Modes/Dialysis.c (.../Dialysis.c) (revision de5a0d43bdef611d963d11855bc958a8d8899a09) +++ firmware/App/Modes/Dialysis.c (.../Dialysis.c) (revision d3142d47c87c30968bda8f2a0a6759b40cc72711) @@ -15,6 +15,8 @@ * ***************************************************************************/ +#include + #include "BloodFlow.h" #include "Buttons.h" #include "DGInterface.h" @@ -33,14 +35,17 @@ // ********** private definitions ********** -#define MAX_UF_RATE_ML_PER_MIN 45.83 ///< Maximum ultrafiltration rate in mL/min (2750 mL/hr). -#define MAX_UF_ACCURACY_ERROR_ML 25.0 ///< Maximum ultrafiltration accuracy error in mL over the entire treatment. -#define UF_ACCURACY_CHECK_INTERVAL ((1 * SEC_PER_MIN * MS_PER_SECOND) / TASK_GENERAL_INTERVAL) ///< Ultrafiltration rate accuracy check interval count +#define MAX_UF_RATE_ML_PER_HOUR 2750.0 ///< Maximum ultrafiltration rate in mL/hour +#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. +#define UF_ACCURACY_CHECK_INTERVAL ((1 * MIN_PER_HOUR * SEC_PER_MIN * MS_PER_SECOND) / TASK_GENERAL_INTERVAL) ///< Ultrafiltration rate accuracy check interval count // ********** private data ********** static DIALYSIS_STATE_T currentDialysisState; ///< Current state of the dialysis sub-mode state machine. static UF_STATE_T currentUFState; ///< Current state of the ultrafiltration state machine. +static SALINE_BOLUS_STATE_T currentSalineBolusState; ///< Current state of the saline bolus state machine. static F32 refUFVolume; ///< Current reference volume for ultrafiltration (Where should we be w/r/t ultrafiltration). static F32 measUFVolume; ///< Current total measured volume for ultrafiltration (Where are we w/r/t ultrafiltration). @@ -55,14 +60,15 @@ static U32 setDialysateFlowRate; ///< Currently set dialysate flow rate (from prescription). static F32 maxUFVolumeML; ///< Currently set total ultrafiltration volume for treatment (from prescription). 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 uFAccuracyCheckTimerCtr; ///< Timer counter to determine when next to check ultrafiltration accuracy. static F32 lastUFVolumeChecked; ///< Starting ultrafiltration volume for accuracy check. // ********** private function prototypes ********** static DIALYSIS_STATE_T handleDialysisUltrafiltrationState( void ); -static DIALYSIS_STATE_T handleDialysisSolutionInfusionState( void ); +static DIALYSIS_STATE_T handleDialysisSalineBolusState( void ); static UF_STATE_T handleUFStartState( void ); static UF_STATE_T handleUFPausedState( void ); @@ -75,8 +81,8 @@ /*********************************************************************//** * @brief - * The initDialysis function initializes the Dialysis sub-mode module. \n - * Calling this function will reset dialysis and therefore should only \n + * The initDialysis function initializes the Dialysis sub-mode module. + * Calling this function will reset dialysis and therefore should only * be called when a new treatment is due to begin. * @details * Inputs : none @@ -87,6 +93,7 @@ { currentDialysisState = DIALYSIS_START_STATE; currentUFState = UF_START_STATE; + currentSalineBolusState = SALINE_BOLUS_STATE_IDLE; refUFVolume = 0.0; measUFVolume = 0.0; @@ -103,17 +110,18 @@ setDialysateFlowRate = 0; maxUFVolumeML = 0.0; setUFRate = 0.0; + maxUFRateAccuracyError_Ml_hr = MAX_UF_RATE_ACCURACY_ERROR_ML_HR; uFAccuracyCheckTimerCtr = 0; lastUFVolumeChecked = 0.0; } /*********************************************************************//** * @brief - * The transitionToDialysis function prepares for transition to dialysis sub-mode. \n - * This function will reset anything required for resuming dialysis in a \n - * treatment that has already begun. It does not reset everything as dialysis \n - * may be stopped and resumed multiple times due to alarms or user intervention \n + * The transitionToDialysis function prepares for transition to dialysis sub-mode. + * This function will reset anything required for resuming dialysis in a + * treatment that has already begun. It does not reset everything as dialysis + * may be stopped and resumed multiple times due to alarms or user intervention * and we don't want to start the treatment all over again. * @details * Inputs : none @@ -127,16 +135,16 @@ /*********************************************************************//** * @brief - * The setDialysisParams function sets the dialysis treatment parameters. \n - * This function should be called prior to beginning dialysis treatment \n + * The setDialysisParams function sets the dialysis treatment parameters. + * This function should be called prior to beginning dialysis treatment * and when the user changes one or more parameters during treatment. * @details * Inputs : none * Outputs : dialysis treatment parameters are set. - * @param bPFlow : target blood pump flow rate (in mL/min). - * @param dPFlow : target dialysate inlet pump flow rate (in mL/min). - * @param maxUFVol : maximum ultrafiltration volume (in mL). - * @param uFRate : target ultrafiltration rate (in mL/min). + * @param bPFlow target blood pump flow rate (in mL/min) + * @param dPFlow target dialysate inlet pump flow rate (in mL/min) + * @param maxUFVol maximum ultrafiltration volume (in mL) + * @param uFRate target ultrafiltration rate (in mL/min) * @return none *************************************************************************/ void setDialysisParams( U32 bPFlow, U32 dPFlow, F32 maxUFVol, F32 uFRate ) @@ -145,17 +153,26 @@ setDialysateFlowRate = dPFlow; maxUFVolumeML = maxUFVol; setUFRate = uFRate; + maxUFRateAccuracyError_Ml_hr = MAX_UF_RATE_ACCURACY_ERROR_PCT * uFRate * (F32)MIN_PER_HOUR; + maxUFRateAccuracyError_Ml_hr = MAX( maxUFRateAccuracyError_Ml_hr, MAX_UF_RATE_ACCURACY_ERROR_ML_HR ); - setBloodPumpTargetFlowRate( setBloodFlowRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); // TODO - test code - remove later - setDialInPumpTargetFlowRate( setDialysateFlowRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); -// setBloodPumpTargetFlowRate( setBloodFlowRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); // TODO - restore these later -// setDialInPumpTargetFlowRate( setDialysateFlowRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); + if ( TREATMENT_DIALYSIS_STATE == getTreatmentState() ) + { +#ifndef RUN_PUMPS_OPEN_LOOP + setBloodPumpTargetFlowRate( setBloodFlowRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); + setDialInPumpTargetFlowRate( setDialysateFlowRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); +#else + setBloodPumpTargetFlowRate( setBloodFlowRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); + setDialInPumpTargetFlowRate( setDialysateFlowRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); +#endif + setDialOutPumpTargetRate( setDialysateFlowRate + (S32)setUFRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); + } } /*********************************************************************//** * @brief - * The startDialysis function starts/resumes dialysis. This function \n - * will be called by Treatment Mode when beginning or resuming dialysis \n + * The startDialysis function starts/resumes dialysis. This function + * will be called by Treatment Mode when beginning or resuming dialysis * treatment. * @details * Inputs : none @@ -164,18 +181,28 @@ *************************************************************************/ void startDialysis( void ) { + // set last UF timestamp so UF ref is resumed from this time 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 ); + setDialInPumpTargetFlowRate( setDialysateFlowRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); +#else setBloodPumpTargetFlowRate( setBloodFlowRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); setDialInPumpTargetFlowRate( setDialysateFlowRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); +#endif setDialOutPumpTargetRate( setDialysateFlowRate + (S32)setUFRate, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); // TODO - Heparin pump + // tell DG to start heating dialysate + cmdStartDGTrimmerHeater(); } /*********************************************************************//** * @brief - * The stopDialysis function stops dialysis. This function will be called \n - * by Treatment Mode when an alarm occurs or the user pressed the stop button. \n + * The stopDialysis function stops dialysis. This function will be called + * by Treatment Mode when an alarm occurs or the user pressed the stop button. * Dialysis may be resumed later. * @details * Inputs : none @@ -184,9 +211,13 @@ *************************************************************************/ void stopDialysis( void ) { - setBloodPumpTargetFlowRate( 0, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); - setDialInPumpTargetFlowRate( 0, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); + // stop pumps + 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 ); + // TODO - stop Heparin pump + // tell DG to stop heating dialysate + cmdStopDGTrimmerHeater(); } /*********************************************************************//** @@ -217,7 +248,20 @@ /*********************************************************************//** * @brief - * The getUltrafiltrationVolumeCollected function gets the current ultrafiltration \n + * The getSalineBolusState function gets the current saline bolus state. + * @details + * Inputs : currentSalineBolusState + * Outputs : none + * @return currentSalineBolusState + *************************************************************************/ +SALINE_BOLUS_STATE_T getSalineBolusState( void ) +{ + return currentSalineBolusState; +} + +/*********************************************************************//** + * @brief + * The getUltrafiltrationVolumeCollected function gets the current ultrafiltration * volume collected so far for current treatment. * @details * Inputs : measUFVolume, measUFVolumeFromPriorReservoirs @@ -355,19 +399,19 @@ currentDialysisState = handleDialysisUltrafiltrationState(); break; - case DIALYSIS_SOLUTION_INFUSION_STATE: - currentDialysisState = handleDialysisSolutionInfusionState(); + case DIALYSIS_SALINE_BOLUS_STATE: + currentDialysisState = handleDialysisSalineBolusState(); break; default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_DIALYSIS_INVALID_STATE, currentDialysisState ) + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_DIALYSIS_INVALID_STATE, currentDialysisState ) break; } } /*********************************************************************//** * @brief - * The handleDialysisUltrafiltrationState function handles the ultrafiltration \n + * The handleDialysisUltrafiltrationState function handles the ultrafiltration * state of the Dialysis state machine. * @details * Inputs : currentUFState @@ -401,7 +445,7 @@ break; default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_DIALYSIS_INVALID_UF_STATE, currentUFState ) + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_DIALYSIS_INVALID_UF_STATE, currentUFState ) break; } @@ -410,16 +454,16 @@ /*********************************************************************//** * @brief - * The handleDialysisSolutionInfusionState function handles the solution \n + * The handleDialysisSolutionInfusionState function handles the solution * infustion state of the Dialysis state machine. * @details - * Inputs : TBD - * Outputs : TBD + * Inputs : currentSalineBolusState + * Outputs : currentSalineBolusState * @return next Dialysis state. *************************************************************************/ -static DIALYSIS_STATE_T handleDialysisSolutionInfusionState( void ) +static DIALYSIS_STATE_T handleDialysisSalineBolusState( void ) { - DIALYSIS_STATE_T result = DIALYSIS_SOLUTION_INFUSION_STATE; + DIALYSIS_STATE_T result = DIALYSIS_SALINE_BOLUS_STATE; // TODO - @@ -428,7 +472,7 @@ /*********************************************************************//** * @brief - * The handleUFStartState function handles the Start state of the \n + * The handleUFStartState function handles the Start state of the * ultrafiltration state machine. * @details * Inputs : maxUFVolumeML @@ -456,7 +500,7 @@ /*********************************************************************//** * @brief - * The handleUFPausedState function handles the Paused state of the \n + * The handleUFPausedState function handles the Paused state of the * ultrafiltration state machine. * @details * Inputs : none @@ -482,11 +526,11 @@ /*********************************************************************//** * @brief - * The handleUFRunningState function handles the Running state of the \n + * The handleUFRunningState function handles the Running state of the * ultrafiltration state machine. * @details * Inputs : ms timer, lastUFTimeStamp - * Outputs : UF timer incremented, UF volumes updated and provided to DPo \n + * Outputs : UF timer incremented, UF volumes updated and provided to DPo * pump controller. * @return next ultrafiltration state. *************************************************************************/ @@ -524,7 +568,7 @@ /*********************************************************************//** * @brief - * The handleUFCompletedOrOffState function handles the UF Off state \n + * The handleUFCompletedOrOffState function handles the UF Off state * of the ultrafiltration state machine. * @details * Inputs : none @@ -543,7 +587,7 @@ /*********************************************************************//** * @brief - * The handleUFCompletedState function handles the UF Completed \n + * The handleUFCompletedState function handles the UF Completed * state of the ultrafiltration state machine. This is a terminal state. * @details * Inputs : none @@ -562,46 +606,55 @@ /*********************************************************************//** * @brief - * The checkUF function checks ultrafiltration accuracy for the last \n - * minute and checks total UF volume. Triggers an alarm if out of spec. + * The checkUF function checks ultrafiltration accuracy for the last + * hour and checks total UF volume. Triggers an alarm if out of spec. * @details * Inputs : uFAccuracyCheckTimerCtr, lastUFVolumeChecked, measUFVolume * Outputs : uFAccuracyCheckTimerCtr, lastUFVolumeChecked * @return none *************************************************************************/ static void checkUFAccuracyAndVolume( void ) { - // check UF accuracy at 1 minute intervals - if ( ++uFAccuracyCheckTimerCtr >= UF_ACCURACY_CHECK_INTERVAL ) - { - F32 uFMeasRate = measUFVolume - lastUFVolumeChecked; + F32 uFMeasRate = measUFVolume - lastUFVolumeChecked; - // check UF max rate - if ( uFMeasRate > MAX_UF_RATE_ML_PER_MIN ) - { + // check max UF rate in last hour // TODO - is this check necessary considering next check below is a tighter check on this? +// if ( uFMeasRate > MAX_UF_RATE_ML_PER_HOUR ) +// { +//#ifndef DISABLE_UF_ALARMS +// SET_ALARM_WITH_1_F32_DATA( ALARM_ID_UF_RATE_TOO_HIGH_ERROR, uFMeasRate ); +//#endif +// } + + // check UF rate diff from target in last hour + if ( uFMeasRate > maxUFRateAccuracyError_Ml_hr ) + { #ifndef DISABLE_UF_ALARMS - SET_ALARM_WITH_1_F32_DATA( ALARM_ID_UF_RATE_TOO_HIGH_ERROR, uFMeasRate ); + SET_ALARM_WITH_1_F32_DATA( ALARM_ID_UF_RATE_TOO_HIGH_ERROR, uFMeasRate ); #endif + } + // if actively performing ultrafiltration right now, increment timer and see if time to start another 1 hour check period + if ( UF_RUNNING_STATE == currentUFState ) + { + if ( ++uFAccuracyCheckTimerCtr >= UF_ACCURACY_CHECK_INTERVAL ) + { + // reset for next check interval + lastUFVolumeChecked = measUFVolume; + uFAccuracyCheckTimerCtr = 0; } - // reset for next check - lastUFVolumeChecked = measUFVolume; - uFAccuracyCheckTimerCtr = 0; } // check total UF volume error - if ( ( FABS( refUFVolume - measUFVolume ) ) >= (F32)MAX_UF_ACCURACY_ERROR_ML ) + if ( ( fabs( refUFVolume - measUFVolume ) ) >= (F32)MAX_UF_ACCURACY_ERROR_ML ) { -#ifndef DISABLE_UF_ALARMS SET_ALARM_WITH_2_F32_DATA( ALARM_ID_UF_VOLUME_ACCURACY_ERROR, refUFVolume, measUFVolume ); -#endif } } /*********************************************************************//** * @brief - * The updateUFVolumes function updates the ultrafiltration volumes based on \n - * set UF rate, latest UF elapsed time, and the latest load cell weight for the \n - * currently used reservoir. Updated UF volumes are then sent to the dialysate \n + * The updateUFVolumes function updates the ultrafiltration volumes based on + * set UF rate, latest UF elapsed time, and the latest load cell weight for the + * currently used reservoir. Updated UF volumes are then sent to the dialysate * outlet pump controller. * @details * Inputs : setUFRate, uFTimeMS, load cell weight @@ -631,8 +684,8 @@ /*********************************************************************//** * @brief - * The setStartReservoirVolume function updates the baseline volume of the \n - * next reservoir to be drawn from before it is switched to (i.e. while it \n + * The setStartReservoirVolume function updates the baseline volume of the + * next reservoir to be drawn from before it is switched to (i.e. while it * is the inactive reservoir) in order to get a more stable volume. * @details * Inputs : active reservoir, load cell reading from inactive reservoir @@ -663,8 +716,8 @@ /*********************************************************************//** * @brief - * The signalReservoirsSwitched function informs this module that the \n - * reservoirs have been switched. The UF volume from prior reservoirs is \n + * The signalReservoirsSwitched function informs this module that the + * reservoirs have been switched. The UF volume from prior reservoirs is * tentatively added to with a load cell reading of the inactive reservoir. * @details * Inputs : resFinalVolume[], resStartVolume[] @@ -682,8 +735,8 @@ /*********************************************************************//** * @brief - * The setFinalReservoirVolume function updates the UF volume from prior reservoirs \n - * with a more stable final reservoir volume of the prior reservoir after it's \n + * The setFinalReservoirVolume function updates the UF volume from prior reservoirs + * with a more stable final reservoir volume of the prior reservoir after it's * had a moment to settle. * @details * Inputs : active reservoir, load cell reading from inactive reservoir