Index: firmware/App/Controllers/BalancingChamber.c =================================================================== diff -u -r9414a62b7071ca116813ad68eb80892331c536c7 -rcf4b4058736c50efbf955846f37c6bd62ddc7e99 --- firmware/App/Controllers/BalancingChamber.c (.../BalancingChamber.c) (revision 9414a62b7071ca116813ad68eb80892331c536c7) +++ firmware/App/Controllers/BalancingChamber.c (.../BalancingChamber.c) (revision cf4b4058736c50efbf955846f37c6bd62ddc7e99) @@ -25,6 +25,7 @@ #include "FpgaDD.h" #include "Heaters.h" #include "Level.h" +#include "MessagePayloads.h" #include "MessageSupport.h" #include "Messaging.h" #include "MixingControl.h" @@ -49,7 +50,7 @@ #define BAL_CHAMBER_FILL_COMPLETE_MS ( 100 / TASK_GENERAL_INTERVAL ) ///< Time (ms/tasktime) to confirm the balancing chamber fill completed and pressure is within range #define SPENT_FILL_COMPLETE_QD_LOW_RISE_MAX_MLPM 300.0F ///< For Qd <= 300 mL/min (Diener 1000). #define SPENT_FILL_COMPLETE_DP_RISE_PSIG 0.5F ///< Minimum 100 ms spent pressure rise (current minus n-2) to count as a rise hit at low Qd. -#define SPENT_FILL_COMPLETE_DP_RISE_HIGH_QD_PSIG 3.0F ///< Minimum spent pressure rise for Diener 2000 or Qd > 300 mL/min. +#define SPENT_FILL_COMPLETE_DP_RISE_HIGH_QD_PSIG 2.0F ///< Minimum spent pressure rise for Diener 2000 #define SPENT_FILL_SLOPE_SPENT_PRESSURE_HIGH_PSIG 2.0F ///< Above this spent pressure rise, only one rise hit is required. #define SPENT_FILL_SLOPE_MAX_RISE_HITS 2 ///< Rise hits required when Qd <= 150 in the low spent-pressure band. #define SPENT_FILL_DETECT_COUNT_TOL_PCT 1.0F ///< Allowed detection timing tolerance as a fraction of expected BC switching count. @@ -62,13 +63,14 @@ #define BAL_CHAMBER_FILL_TIMEOUT_FACTOR 3.0F ///< Balancing Chamber fill timeout factor (300% of observed fill count) #define COMP_SLOPE -0.000376F ///< Balancing chamber temperature compensation slope factor #define COMP_INTERCEPT 1.007269F ///< Balancing chamber temperature compensation intercept factor -/// Bicarb (D10) pump speed from instantaneous mix volume: speed = 240 - sqrt(230400 - 96000*v)/2 #define BC_BICARB_SPEED_OFFSET_MLPM 240.0F ///< Bicarb speed formula offset (mL/min). #define BC_BICARB_SPEED_DISC_CONST 230400.0F ///< Bicarb speed formula discriminant constant. #define BC_BICARB_SPEED_DISC_VOL_COEF 96000.0F ///< Bicarb speed formula volume coefficient. #define BC_ACID_SPEED_OFFSET_MLPM 160.0F ///< Acid speed formula offset (mL/min). #define BC_ACID_SPEED_DISC_CONSTANT 102400.0F ///< Acid speed formula discriminant constant. #define BC_ACID_SPEED_DISC_VOL_COEF 96000.0F ///< Acid speed formula volume coefficient. +#define BAL_CHAMBER_MAX_SWITCH_TIME_TOL 1.0F ///< Maximum tolerance defined for switching states +#define BAL_CHAMBER_ABSOLUTE_SPENT_PRESSURE 33.0F ///< Maximum spent(D51) pressure before we declare the fill is complete /// Payload record structure for balancing chamber switch only request typedef struct @@ -117,6 +119,14 @@ static BOOL isBalChamberSwitchingPeriodUpdatePending; ///< BC switching period update pending apply at fill end. static U32 bcSwitchingBasedOnClosedPeriodCounter; ///< Valve-close segments remaining before first-cycle relaxations clear after Qd timing apply. static F32 balancingError; ///< Balancing error that has been calculated during balancing chamber switching. +static F32 balChamberTolSwitchTime; ///< Balancing chamber allowed tolerance time before we switch to next state. +static U32 detectTolCount; +static U32 maxDetectCount; +static U32 minDetectCount; +static BOOL isDetectionInCountWindow; +static F32 riseDeltaPsi; +static BOOL riseHit; +static U32 requiredRiseCount; //TODO: remove later once level sensor working static U32 bicarbChamberPeriodicFillCounter; @@ -210,6 +220,7 @@ prevBalChamberState = BAL_CHAMBER_STATE_IDLE; //TODO:remove once level sensor working bicarbChamberPeriodicFillCounter = 0; + balChamberTolSwitchTime = 0.0F; } /*********************************************************************//** @@ -401,8 +412,7 @@ //update the switching period in task interval for balancing chamber fill timeout check balChamberSwitchingPeriod = (U32)( (F32)SEC_PER_MIN / getBalChamberSwitchingFreq() * ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ); - // finish the balancing chamber fill 50 ms prior completing the regular cycle time. - balChamberSwitchingPeriod -= 1; + balChamberTolSwitchTime = (F32)balChamberSwitchingPeriod + ( (F32)balChamberSwitchingPeriod * BAL_CHAMBER_MAX_SWITCH_TIME_TOL ); //Update last td dialysate flow rate lastTdDialysateFlowrate = tdDialysateFlowrate; @@ -418,7 +428,7 @@ //Testing balChamberValveClosePeriod = balChamberSwitchingPeriod; - balChamberValveClosePeriod -= 1; // Close valves prior 50 msecond for testing + balChamberValveClosePeriod -= 2; // Close valves 100 msecond prior to switching to next state currentBalChamberFillCounter = 0; currentBalChamberSwitchingCounter = 0; @@ -842,8 +852,6 @@ isBalChamberFillInProgress = FALSE; isPressureStabilizedDuringFill = FALSE; isPressureDroppedDuringFill = FALSE; - lastPrevSpentDialPressure = 0.0F; - prevSpentDialPressure = 0.0F; currentBalChamberSwitchingCounter = 0; currentBalChamberFillCounter = 0; spentFillRiseHitCount = 0; @@ -864,6 +872,8 @@ freshDialPressure = getFilteredPressure( D18_PRES ); spentDialPressure = getFilteredPressure( D51_PRES ); + lastPrevSpentDialPressure = spentDialPressure; + prevSpentDialPressure = spentDialPressure; if ( getTestConfigStatus( TEST_CONFIG_DD_FP_ENABLE_BETA_1_0_HW ) == TRUE ) { @@ -1156,8 +1166,6 @@ isPressureStabilizedDuringFill = FALSE; isPressureDroppedDuringFill = FALSE; balChamberSWState = BAL_CHAMBER_SW_STATE2; - lastPrevSpentDialPressure = 0.0F; - prevSpentDialPressure = 0.0F; currentBalChamberSwitchingCounter = 0; currentBalChamberFillCounter = 0; spentFillRiseHitCount = 0; @@ -1177,6 +1185,8 @@ } freshDialPressure = getFilteredPressure( D18_PRES ); spentDialPressure = getFilteredPressure( D51_PRES ); + lastPrevSpentDialPressure = spentDialPressure; + prevSpentDialPressure = spentDialPressure; if ( getTestConfigStatus( TEST_CONFIG_DD_FP_ENABLE_BETA_1_0_HW ) == TRUE ) { @@ -1467,14 +1477,13 @@ BOOL state; // Gate slope hits near expected valve-close count (current vs balChamberValveClosePeriod +/- tolerance). - U32 detectTolCount = (U32)( (F32)balChamberValveClosePeriod * SPENT_FILL_DETECT_COUNT_TOL_PCT ); - U32 maxDetectCount = balChamberValveClosePeriod + detectTolCount; - U32 minDetectCount = balChamberValveClosePeriod - detectTolCount; - BOOL isDetectionInCountWindow = ( ( currentBalChamberFillCounter >= minDetectCount ) && - ( currentBalChamberFillCounter <= maxDetectCount ) ) ? TRUE : FALSE; - F32 riseDeltaPsi = spentDialPressure - lastPrevSpentDialPressure; - BOOL riseHit = ( riseDeltaPsi > riseThresholdPsig ) ? TRUE : FALSE; - U32 requiredRiseCount; + detectTolCount = (U32)( (F32)balChamberValveClosePeriod * SPENT_FILL_DETECT_COUNT_TOL_PCT ); + maxDetectCount = balChamberValveClosePeriod + detectTolCount; + minDetectCount = balChamberValveClosePeriod - detectTolCount; + isDetectionInCountWindow = ( ( currentBalChamberFillCounter >= minDetectCount ) && + ( currentBalChamberFillCounter <= maxDetectCount ) ) ? TRUE : FALSE; + riseDeltaPsi = spentDialPressure - lastPrevSpentDialPressure; + riseHit = ( riseDeltaPsi > riseThresholdPsig ) ? TRUE : FALSE; // Mid band between low/high spent pressure rise: require two rise hits. if ( riseDeltaPsi > SPENT_FILL_SLOPE_SPENT_PRESSURE_HIGH_PSIG ) @@ -1548,6 +1557,9 @@ if ( ( TRUE == isSpentFillCompleteDetected ) && ( isSpentFillComplete != TRUE ) ) { + + isSpentFillComplete = TRUE; + // Check spent fill time against the balancing chamber closing period if ( balChamberValveClosePeriod > 0 ) { @@ -1582,11 +1594,11 @@ } } - //Update spent fill is complete - if ( currentBalChamberSwitchingCounter >= balChamberValveClosePeriod ) - { - isSpentFillComplete = TRUE; - } +// //Update spent fill is complete +// if ( currentBalChamberSwitchingCounter >= balChamberValveClosePeriod ) +// { +// isSpentFillComplete = TRUE; +// } } } } @@ -1853,6 +1865,18 @@ data.isPressureStabilizedDuringFill = isPressureStabilizedDuringFill; data.balChamberSWOnlyState = balanceChamberSwitchingOnly; data.isBalChamberSwitchingActive = isBalChamberSwitchingActive; +#ifdef __BAL_CHAMBER_DEBUG__ + data.detectTolCount = detectTolCount; + data.maxDetectCount = maxDetectCount; + data.minDetectCount = minDetectCount; + data.isDetectionInCountWindow = isDetectionInCountWindow; + data.riseDeltaPsi = riseDeltaPsi; + data.riseHit = riseHit; + data.requiredRiseCount = requiredRiseCount; + data.spentFillRiseHitCount = spentFillRiseHitCount; + data.lastPrevSpentDialPressure = lastPrevSpentDialPressure; + data.prevSpentDialPressure = prevSpentDialPressure; +#endif broadcastData( MSG_ID_DD_BAL_CHAMBER_DATA, COMM_BUFFER_OUT_CAN_DD_BROADCAST, (U08*)&data, sizeof( BAL_CHAMBER_DATA_T ) );