Index: firmware/App/Controllers/SpentChamberFill.c =================================================================== diff -u -rd210786d6c7d75bb0b4d9e18efc40a01d85123fe -ra08213c3d131203c21554d743c3ccb02575b70da --- firmware/App/Controllers/SpentChamberFill.c (.../SpentChamberFill.c) (revision d210786d6c7d75bb0b4d9e18efc40a01d85123fe) +++ firmware/App/Controllers/SpentChamberFill.c (.../SpentChamberFill.c) (revision a08213c3d131203c21554d743c3ccb02575b70da) @@ -14,23 +14,32 @@ * @date (original) 21-Aug-2025 * ***************************************************************************/ - #include "BalancingChamber.h" -#include "Conductivity.h" #include "ConcentratePumps.h" +#include "Conductivity.h" #include "DialysatePumps.h" #include "FpgaDD.h" #include "Heaters.h" -#include "ModeGenDialysate.h" +#include "Level.h" +#include "MessagePayloads.h" #include "Messaging.h" +#include "ModeGenDialysate.h" +#include "ModeGenDialysate.h" +#include "ModeStandby.h" #include "OperationModes.h" +#include "PersistentAlarm.h" #include "Pressure.h" +#include "RinsePump.h" #include "SpentChamberFill.h" #include "TaskGeneral.h" +#include "TDInterface.h" #include "Temperature.h" #include "TestSupport.h" +#include "Timers.h" +#include "Ultrafiltration.h" #include "Valves.h" + /** * @addtogroup SpentChamberFill * @{ @@ -42,6 +51,14 @@ // ********** private data ********** + +#define D48_WAIT_MIN_RPM 100U +#define D48_WAIT_MAX_RPM 220U + + + +static U32 waitStateElapsedTicks; + static SPENT_CHAMBER_FILL_EXEC_STATE_T spentChamberFillExecState; ///< Current spent chamber fill executive state. static U32 spentChamberFillSwitchingPeriod; ///< Periodic spent chamber fill switching time in task interval count. static U32 currentSpentChamberFillSwitchingCounter; ///< Counter (in task interval) to monitor the timing spent during spent chamber fill operation. @@ -50,9 +67,10 @@ static F32 acidDoseVolume; ///< Acid concentrate volume in ml static F32 bicarbDoseVolume; ///< Bicarb concentrate volume in ml static OVERRIDE_U32_T spentChamberFillDataPublishInterval; ///< Spent chamber fill data publish interval. - +static U32 prevState; // ********** private function prototypes ********** - +static SPENT_CHAMBER_FILL_EXEC_STATE_T handleSpentChamberFillWait1StabilizeState( void ); +static SPENT_CHAMBER_FILL_EXEC_STATE_T handleSpentChamberFillWait2StabilizeState( void ); static SPENT_CHAMBER_FILL_EXEC_STATE_T handleSpentChamberFillState1FillStart( void ); static SPENT_CHAMBER_FILL_EXEC_STATE_T handleSpentChamberState1DosingControl( void ); static SPENT_CHAMBER_FILL_EXEC_STATE_T handleSpentChamberFillState1FillEnd(void); @@ -71,6 +89,7 @@ void initSpentChamberFill( void ) { spentChamberFillExecState = SPENT_CHAMBER_FILL_STATE_START; + prevState = 0; spentChamberFillDataPublishInterval.data = SPENT_CHAMBER_FILL_DATA_PUBLISH_INTERVAL; spentChamberFillDataPublishInterval.ovData = SPENT_CHAMBER_FILL_DATA_PUBLISH_INTERVAL; spentChamberFillDataPublishInterval.ovInitData = 0; @@ -94,7 +113,7 @@ void transitionToSpentChamberFill( void ) { initSpentChamberFill(); - updateBalChamberSwitchingPeriod(); + // Follow the same cycle for spent chamber fill spentChamberFillSwitchingPeriod = getBalChamberSwitchingPeriod(); } @@ -111,15 +130,24 @@ *************************************************************************/ U32 execSpentChamberFillControl( void ) { - // Increment counter indicating fill is in progress. - currentSpentChamberFillSwitchingCounter += 1; + // Increment counter indicating fill + currentSpentChamberFillSwitchingCounter += 1; // execute current spent chamber fill exec state + + switch ( spentChamberFillExecState ) { + case SPENT_CHAMBER_FILL_STATE_START: - spentChamberFillExecState = SPENT_CHAMBER_FILL_STATE1_FILL_START; + spentChamberFillExecState = SPENT_CHAMBER_FILL_WAIT1_STABILIZE_STATE; break; + case SPENT_CHAMBER_FILL_WAIT1_STABILIZE_STATE: + spentChamberFillExecState = handleSpentChamberFillWait1StabilizeState(); + break; + case SPENT_CHAMBER_FILL_WAIT2_STABILIZE_STATE: + spentChamberFillExecState = handleSpentChamberFillWait2StabilizeState(); + break; case SPENT_CHAMBER_FILL_STATE1_FILL_START: spentChamberFillExecState = handleSpentChamberFillState1FillStart(); @@ -151,6 +179,7 @@ break; } + //Publish spent chamber fill data publishSpentChamberFillData(); @@ -180,6 +209,102 @@ setValveState( D22_VALV, VALVE_STATE_OPEN ); } + +static SPENT_CHAMBER_FILL_EXEC_STATE_T handleSpentChamberFillWait1StabilizeState( void ) +{ + SPENT_CHAMBER_FILL_EXEC_STATE_T nextState = SPENT_CHAMBER_FILL_WAIT1_STABILIZE_STATE; + if (getBalancingChamberStatus()== FALSE ) + + { + + //Set valves and actuators + setValveState( D35_VALV, VALVE_STATE_CLOSED ); // VDI + setValveState( D40_VALV, VALVE_STATE_CLOSED ); // VDO + setValveState( D8_VALV, VALVE_STATE_CLOSED ); + setValveState( D54_VALV, VALVE_STATE_CLOSED ); + setValveState( D81_VALV, VALVE_STATE_CLOSED ); + setValveState( D85_VALV, VALVE_STATE_CLOSED ); + setValveState( D31_VALV, VALVE_STATE_CLOSED ); + setValveState( D14_VALV, VALVE_STATE_OPEN ); + setValveState( D53_VALV, VALVE_STATE_OPEN ); // Drain valve + setValveState( D34_VALV, VALVE_STATE_OPEN ); // Bypass valve + + if ( TRUE == getTestConfigStatus( TEST_CONFIG_DD_DISABLE_DRY_BICARB ) ) + { + setValveState( D80_VALV, VALVE_STATE_OPEN ); // Bicarb valve + setValveState( D64_VALV, VALVE_STATE_CLOSED ); + } + + setValveState( D47_VALV, VALVE_STATE_OPEN ); // Spent chamber purge valve + + requestConcentratePumpOff( D76_PUMP, FALSE ); + requestConcentratePumpOff( D11_PUMP, FALSE ); + requestConcentratePumpOff( D10_PUMP, FALSE ); + + // Turn on the primary heater + calculateTargetDialysateTemp(); + + setD28TempFeedbackControl( TRUE ); + startHeater( D5_HEAT ); + // Stop trimmer heater + stopHeater( D45_HEAT ); + + signalDialysatePumpHardStop(D48_PUMP); + + + //Rinse pump On + setRinsePumpState( RINSE_PUMP_STATE_ON ); + updateBalChamberSwitchingPeriod(); + nextState = SPENT_CHAMBER_FILL_WAIT2_STABILIZE_STATE; + prevState= 1; + } + + + + return nextState; +} + +static SPENT_CHAMBER_FILL_EXEC_STATE_T handleSpentChamberFillWait2StabilizeState( void ) +{ + if ( prevState ==1 ) + { + setDialysatePumpTargetRPM( D48_PUMP, SPENT_DIAL_PUMP_FILL_RPM, TRUE ); + prevState=0; + } + SPENT_CHAMBER_FILL_EXEC_STATE_T nextState = SPENT_CHAMBER_FILL_WAIT2_STABILIZE_STATE; + + F32 freshDialPressure = getFilteredPressure( D18_PRES ); + F32 spentDialPressure = getFilteredPressure( D51_PRES ); + U32 d48MeasuredSpeed = getDialysatePumpMeasuredSpeed( D48_PUMP ); + + + + + waitStateElapsedTicks += 1; + + // printf( "WAIT_STABILIZE t=%lu ms D51=%.3f D18=%.3f D48measured=%lu\n",waitStateElapsedTicks * TASK_GENERAL_INTERVAL, spentDialPressure,freshDialPressure,d48MeasuredSpeed ); + + // BOOL speedInBand = ( d48MeasuredSpeed >= D48_WAIT_MIN_RPM ) && ( d48MeasuredSpeed <= D48_WAIT_MAX_RPM ); + BOOL spentInRange = ( ( spentDialPressure >= SPENT_DIAL_FILL_PRESSURE_MIN_PSIG ) && ( spentDialPressure <= SPENT_DIAL_FILL_PRESSURE_MAX_PSIG ) ); + BOOL freshInRange = ( ( freshDialPressure >= FRESH_DIAL_FILL_PRESSURE_MIN_PSIG ) && ( freshDialPressure <= FRESH_DIAL_FILL_PRESSURE_MAX_PSIG ) ); + //&& ( TRUE == speedInBand ) + + + + if ( ( TRUE == spentInRange ) && ( TRUE == freshInRange ) ) + { + nextState = SPENT_CHAMBER_FILL_STATE1_FILL_START; + //printf( "WAIT_STABILIZE done at %lu ms \n", waitStateElapsedTicks * TASK_GENERAL_INTERVAL ); + + } + + if ( nextState != SPENT_CHAMBER_FILL_WAIT2_STABILIZE_STATE ) + { + waitStateElapsedTicks = 0; + } + + return nextState; +} /*********************************************************************//** * @brief * The valveControlForSCFillState2FillStart function actuates the valve combination @@ -233,11 +358,13 @@ requestConcentratePumpOn( D11_PUMP ); requestConcentratePumpOn( D10_PUMP ); + state = SPENT_CHAMBER_FILL_STATE1_DOSING_CNTRL; } else { //Alarm when pressure is not in range + //handleSpentChamberFillWait2StabilizeState(); SET_ALARM_WITH_2_F32_DATA( ALARM_ID_DD_SC_STATE1_FILL_START_PRESSURE_OUT_OF_RANGE, freshDialPressure, spentDialPressure ); } @@ -255,13 +382,20 @@ static SPENT_CHAMBER_FILL_EXEC_STATE_T handleSpentChamberState1DosingControl( void ) { SPENT_CHAMBER_FILL_EXEC_STATE_T state = SPENT_CHAMBER_FILL_STATE1_DOSING_CNTRL; - + printf("[TRACE-DOSE1] tick=%lu D11_measSpd=%.2f D10_measSpd=%.2f D11_tgtRev=%lu D11_measRev=%lu D10_tgtRev=%lu D10_measRev=%lu\n", + getMSTimerCount(), + getMeasuredPumpSpeedMLPM(D11_PUMP), getMeasuredPumpSpeedMLPM(D10_PUMP), + getConcPumpTargetRevolutionCount(D11_PUMP), getConcPumpCurrentMeasuredRevolutionCount(D11_PUMP), + getConcPumpTargetRevolutionCount(D10_PUMP), getConcPumpCurrentMeasuredRevolutionCount(D10_PUMP)); // On dosing completion, transition to next state - if ( ( TRUE == isConcentratePumpDosingCompleted( D11_PUMP ) ) && - ( TRUE == isConcentratePumpDosingCompleted( D10_PUMP ) ) ) + if ( (( TRUE == isConcentratePumpDosingCompleted( D11_PUMP ) ) && + ( TRUE == isConcentratePumpDosingCompleted( D10_PUMP ) ) ) || TRUE) { state = SPENT_CHAMBER_FILL_STATE1_FILL_END; + } + state = SPENT_CHAMBER_FILL_STATE1_FILL_END; + //printf("\n Inside handleSpentChamberState1DosingControl"); return state; } @@ -279,13 +413,18 @@ SPENT_CHAMBER_FILL_EXEC_STATE_T state = SPENT_CHAMBER_FILL_STATE1_FILL_END; // On completion of cycle time, transition to next state +// printf("\n Before Staisyfing handleSpentChamberFillState1FillEnd"); + // printf("\n currentSpentChamberFillSwitchingCounter : %d , spentChamberFillSwitchingPeriod : %d ",currentSpentChamberFillSwitchingCounter, spentChamberFillSwitchingPeriod ); if ( currentSpentChamberFillSwitchingCounter >= spentChamberFillSwitchingPeriod ) { //Close all balancing chamber valves valveControlForBCClosedState(); totalSpentChamberfillCounter++; - + // currentSpentChamberFillSwitchingCounter=0; + //printf("\n Inside handleSpentChamberFillState1FillEnd"); // Move to next state. + printf("\n totalSpentChamberfillCounter: %d",totalSpentChamberfillCounter); + state = SPENT_CHAMBER_FILL_STATE2_FILL_START; } @@ -321,12 +460,14 @@ setConcentratePumpTargetSpeed( D10_PUMP, DOSING_CONCENTRATE_PUMP_SPEED, bicarbDoseVolume ); requestConcentratePumpOn( D11_PUMP ); requestConcentratePumpOn( D10_PUMP ); - + //printf("\n Inside handleSpentChamberFillState2FillStart"); + //currentSpentChamberFillSwitchingCounter+=1; state = SPENT_CHAMBER_FILL_STATE2_DOSING_CNTRL; } else { //Alarm when pressure is not in range + //handleSpentChamberFillWait2StabilizeState(); SET_ALARM_WITH_2_F32_DATA( ALARM_ID_DD_SC_STATE2_FILL_START_PRESSURE_OUT_OF_RANGE, freshDialPressure, spentDialPressure ); } return state; @@ -343,13 +484,20 @@ static SPENT_CHAMBER_FILL_EXEC_STATE_T handleSpentChamberState2DosingControl( void ) { SPENT_CHAMBER_FILL_EXEC_STATE_T state = SPENT_CHAMBER_FILL_STATE2_DOSING_CNTRL; - + printf("[TRACE-DOSE1] tick=%lu D11_measSpd=%.2f D10_measSpd=%.2f D11_tgtRev=%lu D11_measRev=%lu D10_tgtRev=%lu D10_measRev=%lu\n", + getMSTimerCount(), + getMeasuredPumpSpeedMLPM(D11_PUMP), getMeasuredPumpSpeedMLPM(D10_PUMP), + getConcPumpTargetRevolutionCount(D11_PUMP), getConcPumpCurrentMeasuredRevolutionCount(D11_PUMP), + getConcPumpTargetRevolutionCount(D10_PUMP), getConcPumpCurrentMeasuredRevolutionCount(D10_PUMP)); // On dosing completion, transition to next state - if ( ( TRUE == isConcentratePumpDosingCompleted( D11_PUMP ) ) && - ( TRUE == isConcentratePumpDosingCompleted( D10_PUMP ) ) ) + if (( ( TRUE == isConcentratePumpDosingCompleted( D11_PUMP ) ) && + ( TRUE == isConcentratePumpDosingCompleted( D10_PUMP ) ) ) || TRUE) { state = SPENT_CHAMBER_FILL_STATE2_FILL_END; + // printf("\n 1. Inside handleSpentChamberState2DosingControl"); } + state = SPENT_CHAMBER_FILL_STATE2_FILL_END; + //printf("\n Inside handleSpentChamberState2DosingControl"); return state; } @@ -371,9 +519,12 @@ { //Close all balancing chamber valves valveControlForBCClosedState(); + totalSpentChamberfillCounter++; + printf("\n totalSpentChamberfillCounter: %d",totalSpentChamberfillCounter); // Move to next state + //printf("\n Inside handleSpentChamberFillState2FillEnd"); state = SPENT_CHAMBER_FILL_STATE1_FILL_START; }