Index: firmware/App/Controllers/DGInterface.c =================================================================== diff -u -r1a685471524555a374854c0c9ec8e208e71fe2df -ra2bc96881a5fc3d8f779246b2abebf15a8de9384 --- firmware/App/Controllers/DGInterface.c (.../DGInterface.c) (revision 1a685471524555a374854c0c9ec8e208e71fe2df) +++ firmware/App/Controllers/DGInterface.c (.../DGInterface.c) (revision a2bc96881a5fc3d8f779246b2abebf15a8de9384) @@ -53,13 +53,13 @@ // DG status static DG_OP_MODE_T dgCurrentOpMode = DG_MODE_INIT; ///< Current DG operation mode. static U32 dgSubMode = 0; ///< Current state (sub-mode) of current DG operation mode. -static BOOL dgStarted = FALSE; ///< Flag indicates whether we've commanded the DG to start or stop. -static BOOL dgTrimmerHeaterOn = FALSE; ///< Flag indicates whether we've commanded the DG to start or stop the trimmer heater. -static BOOL dgWaterSampled = FALSE; ///< Flag indicates whether we've commanded the DG to sample water. +static BOOL dgStarted = FALSE; ///< Flag indicates whether we have commanded the DG to start or stop. +static BOOL dgTrimmerHeaterOn = FALSE; ///< Flag indicates whether we have commanded the DG to start or stop the trimmer heater. +static BOOL dgWaterSampled = FALSE; ///< Flag indicates whether we have commanded the DG to sample water. -// state machine states +// State machine states static TREATMENT_RESERVOIR_MGMT_STATE_T currentTrtResMgmtState = TREATMENT_RESERVOIR_MGMT_START_STATE; ///< Current state of treatment mode reservoir management. -static U32 resMgmtTimer = 0; ///< used for keeping state time. +static U32 resMgmtTimer = 0; ///< Used for keeping state time. // DG sensor data static F32 dgPressures[ NUM_OF_DG_PRESSURE_SENSORS ]; ///< Latest pressures reported by the DG. @@ -75,15 +75,16 @@ static U32 dgROPumpPressureSetPtPSI = 0; ///< Latest RO pump target pressure reported by the DG. static U32 dgDrainPumpSpeedSetPtRPM = 0; ///< Latest Drain pump target speed reported by the DG. -// reservoir data +// Reservoir data static DG_RESERVOIR_ID_T dgActiveReservoir = DG_RESERVOIR_2; ///< Latest active reservoir reported by the DG. static DG_RESERVOIR_ID_T dgActiveReservoirSet = DG_RESERVOIR_2; ///< Active reservoir commanded. static U32 dgReservoirFillVolumeTarget = 0; ///< Latest fill-to volume reported by the DG. static U32 dgReservoirFillVolumeTargetSet = 0; ///< Fill-to volume commanded. static U32 dgReservoirDrainVolumeTarget = 0; ///< Latest drain-to volume reported by the DG. static U32 dgReservoirDrainVolumeTargetSet = 0; ///< Drain-to volume commanded. -static U32 resUseTimer = 0; ///< used to track time pumping from active reservoir (for volume used calculation). -static F32 resUseVolumeMl = 0.0; ///< Accumulated volume used from active reservoir. +static U32 resUseTimer = 0; ///< Used to track time pumping from active reservoir (for volume used calculation). +static F32 resUseVolumeMl = 0.0; ///< Accumulated volume used from active reservoir. +static BOOL resHasBeenTared[ NUM_OF_DG_RESERVOIRS ]; ///< Flags indicate whether the reservoir has been tared for this treatment. // ********** private function prototypes ********** @@ -104,9 +105,26 @@ dgTrimmerTempSet = 0.0; dgActiveReservoirSet = DG_RESERVOIR_2; dgReservoirFillVolumeTargetSet = 0; - dgReservoirDrainVolumeTargetSet = 0; + dgReservoirDrainVolumeTargetSet = 0; + initPreTreatmentReservoirMgmt(); } +/*********************************************************************//** + * @brief + * The initPreTreatmentReservoirMgmt function initializes the pre-treatment + * reservoir management state machine. + * @details Inputs: none + * @details Outputs: pre-treatment reservoir management state machine initialized. + * @return none + *************************************************************************/ +void initPreTreatmentReservoirMgmt( void ) +{ + // TODO - currentPreTrtResMgmtState = TREATMENT_RESERVOIR_MGMT_START_STATE; + resMgmtTimer = 0; + resHasBeenTared[ DG_RESERVOIR_1 ] = FALSE; + resHasBeenTared[ DG_RESERVOIR_2 ] = FALSE; +} + /*********************************************************************//** * @brief * The initTreatmentReservoirMgmt function initializes the treatment reservoir @@ -123,6 +141,18 @@ resUseVolumeMl = 0.0; } +/*********************************************************************//** + * @brief + * The execPreTreatmentReservoirMgmt function executes the state machine for the + * reservoir management during pre-treatment mode. + * @details Inputs: none + * @details Outputs: DG reservoirs (drains & fills) managed. + * @return none + *************************************************************************/ +void execPreTreatmentReservoirMgmt( void ) +{ +} + /*********************************************************************//** * @brief * The execTreatmentReservoirMgmt function executes the state machine for the @@ -138,14 +168,14 @@ U32 msSinceLastVolumeCalc = calcTimeSince( resUseTimer ); F32 flowRateMlPerMs = (F32)getTargetDialInFlowRate() / (F32)( MS_PER_SECOND * SEC_PER_MIN ); - // calculate volume used from active reservoir - do not accumulate if saline bolus in progress + // Calculate volume used from active reservoir - do not accumulate if saline bolus in progress if ( SALINE_BOLUS_STATE_IN_PROGRESS != getSalineBolusState() ) { resUseVolumeMl += ( flowRateMlPerMs * msSinceLastVolumeCalc ); // TODO - should this calc be done and kept by Dialysis sub-mode? } resUseTimer = getMSTimerCount(); - // treatment reservoir mgmt. state machine + // Treatment reservoir mgmt. state machine switch ( currentTrtResMgmtState ) { case TREATMENT_RESERVOIR_MGMT_START_STATE: @@ -178,7 +208,7 @@ break; case TREATMENT_RESERVOIR_MGMT_WAIT_TO_FILL_STATE: - // delay fill start if we've paused treatment? + // Delay fill start if we have paused treatment? if ( getTreatmentState() == TREATMENT_DIALYSIS_STATE ) { if ( DG_MODE_CIRC == dgOpMode ) @@ -215,22 +245,22 @@ break; case TREATMENT_RESERVOIR_MGMT_WAIT_FOR_RES_SWITCH_STATE: - // reservoir switch during treatment should only occur in this state (i.e. when DG is ready). - // switch reservoirs when active reservoir is spent (i.e. we've pumped fill volume through dialyzer) and DG ready + // Reservoir switch during treatment should only occur in this state (i.e. when DG is ready). + // Switch reservoirs when active reservoir is spent (i.e. we have pumped fill volume through dialyzer) and DG ready if ( ( DG_MODE_CIRC == getDGOpMode() ) && ( DG_RECIRCULATE_MODE_STATE_RECIRC_WATER == getDGSubMode() ) && ( resUseVolumeMl >= (F32)dgReservoirFillVolumeTargetSet ) ) { DG_RESERVOIR_ID_T activeRes = dgActiveReservoirSet; DG_RESERVOIR_ID_T inactiveRes = ( activeRes == DG_RESERVOIR_1 ? DG_RESERVOIR_2 : DG_RESERVOIR_1 ); - // signal dialysis sub-mode to capture baseline volume for next reservoir. + // Signal dialysis sub-mode to capture baseline volume for next reservoir. setStartReservoirVolume(); - // command DG to switch reservoirs + // Command DG to switch reservoirs cmdSetDGActiveReservoir( inactiveRes ); - // signal dialysis sub-mode to switch reservoirs + // Signal dialysis sub-mode to switch reservoirs signalReservoirsSwitched(); resUseVolumeMl = 0.0; - // wait for used reservoir to settle + // Wait for used reservoir to settle resMgmtTimer = getMSTimerCount(); currentTrtResMgmtState = TREATMENT_RESERVOIR_MGMT_WAIT_FOR_SWITCH_SETTLE_STATE; } @@ -239,9 +269,9 @@ case TREATMENT_RESERVOIR_MGMT_WAIT_FOR_SWITCH_SETTLE_STATE: if ( TRUE == didTimeout( resMgmtTimer, RESERVOIR_SETTLE_TIME_MS ) ) { - // signal dialysis sub-mode to capture final volume of prior reservoir after settling. + // Signal dialysis sub-mode to capture final volume of prior reservoir after settling. setFinalReservoirVolume(); - // reset to start state to restart drain, fill, switch process. + // Reset to start state to restart drain, fill, switch process. currentTrtResMgmtState = TREATMENT_RESERVOIR_MGMT_START_STATE; } break; @@ -289,6 +319,20 @@ return dgActiveReservoirSet; } +/*********************************************************************//** + * @brief + * The getDGInactiveReservoir function gets the currently inactive reservoir. + * @details Inputs: dgActiveReservoirSet + * @details Outputs: none + * @return Currently commanded inactive reservoir. + *************************************************************************/ +DG_RESERVOIR_ID_T getDGInactiveReservoir( void ) +{ + DG_RESERVOIR_ID_T inactiveRes = ( DG_RESERVOIR_2 == dgActiveReservoirSet ? DG_RESERVOIR_1 : DG_RESERVOIR_2 ); + + return inactiveRes; +} + /*********************************************************************//** * @brief * The getDGPressure function gets the latest pressure reported by the DG @@ -600,13 +644,18 @@ * The cmdStartDGDrain function sends a drain command message to the DG. * @details Inputs: none * @details Outputs: drain command sent to DG. - * @param drainToVolMl volume (in mL) to drain inactive reservoir to + * @param drainToVolMl volume (in mL) to drain inactive reservoir to * @return none *************************************************************************/ void cmdStartDGDrain( U32 drainToVolMl ) -{ - dgReservoirDrainVolumeTargetSet = drainToVolMl; - sendDGDrainCommand( drainToVolMl ); +{ + DRAIN_RESERVOIR_CMD_PAYLOAD_T payload; + DG_RESERVOIR_ID_T inactiveRes = getDGInactiveReservoir(); + + dgReservoirDrainVolumeTargetSet = drainToVolMl; + payload.drainToVolumeML = drainToVolMl; + payload.tareLoadCells = ( FALSE == resHasBeenTared[ inactiveRes ] ? TRUE : FALSE ); + sendDGDrainCommand( &payload ); } /*********************************************************************//**