Index: firmware/App/Controllers/Ultrafiltration.c =================================================================== diff -u -r387ea33c2e45705550905bc1a97e13db1cc95bf8 -r7278aeac8fdd894067a05c3bd3a177d38c0b0803 --- firmware/App/Controllers/Ultrafiltration.c (.../Ultrafiltration.c) (revision 387ea33c2e45705550905bc1a97e13db1cc95bf8) +++ firmware/App/Controllers/Ultrafiltration.c (.../Ultrafiltration.c) (revision 7278aeac8fdd894067a05c3bd3a177d38c0b0803) @@ -1,20 +1,21 @@ /************************************************************************** * -* Copyright (c) 2024-2025 Diality Inc. - All Rights Reserved. +* Copyright (c) 2025-2026 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * @file Ultrafiltration.c * -* @author (last) Vinayakam Mani -* @date (last) 23-Jul-2025 +* @author (last) Santhosh Reddy +* @date (last) 06-Jul-2026 * * @author (original) Vinayakam Mani -* @date (original) 23-Jul-2025 +* @date (original) 28-Jul-2025 * ***************************************************************************/ +#include "BalancingChamber.h" #include "ConcentratePumps.h" #include "Messaging.h" #include "OperationModes.h" @@ -34,13 +35,10 @@ #define UF_DATA_PUBLISH_INTERVAL ( 1000 / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the ultrafiltration data published. #define UF_COMPENSATION_PERIOD ( 2 * SEC_PER_MIN * MS_PER_SECOND ) ///< Interval at which the ultrafiltration compenstaion executed. #define UF_COMP_INTERVAL ( UF_COMPENSATION_PERIOD / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the peroidic ultrafiltration compensation calculated. -#define COMP_SLOPE -0.000376F ///< UF Temperature compensation slope factor -#define COMP_INTERCEPT 1.007269F ///< UF temperature compensation intercept factor #define ZERO_RATE 0.0F ///< Zero value. // ********** private data ********** -static UF_EXEC_STATE_T ufExecState; ///< Current ultrafiltration executive state. static BOOL isUltrafiltrationRequested; ///< Flag indicating ultrafiltration request. static U32 currentUFCompCounter; ///< Counter (in task interval) to initiate the periodic ultrafiltration temperature compensation. static F32 compUFrate; ///< compensated UF rate @@ -50,10 +48,7 @@ // ********** private function prototypes ********** -static UF_EXEC_STATE_T handleUFRunningState( void ); -static UF_EXEC_STATE_T handleUFPausedState( void ); static void updateUFRequest( void ); -static void UpdateUFCompensation( void ); static void publishUltrafiltrationData( void ); /*********************************************************************//** @@ -65,7 +60,6 @@ *************************************************************************/ void initUltrafiltration( void ) { - ufExecState = DD_UF_PAUSED; ufDataPublishInterval.data = UF_DATA_PUBLISH_INTERVAL; ufDataPublishInterval.ovData = UF_DATA_PUBLISH_INTERVAL; ufDataPublishInterval.ovInitData = 0; @@ -74,7 +68,7 @@ currentUFCompCounter = 0; compUFrate = getTDUFRate(); ufDataPublicationTimerCounter = 0; - isUFRateUpdated = FALSE; + isUFRateUpdated = TRUE; } /*********************************************************************//** @@ -92,113 +86,87 @@ /*********************************************************************//** * @brief - * The execUFControl function executes the ultrafiltration state machine. - * @details \b Inputs: ufExecState - * @details \b Outputs: ufExecState - * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT when wrong ultrafiltration - * state invoked. + * The handleUFControl function handles the ultrafiltration run/stop states + * and calcualtes the final ultrafilration rate if the temp compensation is enbaled. + * @details \b Inputs: balancing error, TD UF rate + * @details \b Outputs: calculated UF rate + * @details Warning: The compensated UF should be caclulated first and then + * call 'updateUFRequest' function. * @return current state. *************************************************************************/ -U32 execUFControl( void ) +void handleUFControl( void ) { - // Calculate UF volume and determine UF pause/run - updateUFRequest(); + F32 balancingError; - // Compensate balancing error at defined interval - UpdateUFCompensation(); - - // execute current ultrafiltration exec state - switch ( ufExecState ) + if ( getTestConfigStatus( TEST_CONFIG_DD_DISABLE_UF_TEMP_COMPENSATION ) != TRUE ) { - case DD_UF_PAUSED: - ufExecState = handleUFPausedState(); - break; + if ( ( ++currentUFCompCounter >= UF_COMP_INTERVAL ) || ( TRUE == isUFRateUpdated ) ) + { + // Find the offset + balancingError = getBalancingChamberError(); + // Update compensate UF rate with the balancing chamber error + compUFrate = getTDUFRate() + balancingError; - case DD_UF_RUNNING: - ufExecState = handleUFRunningState(); - break; - - default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DD_SOFTWARE_FAULT, SW_FAULT_ID_UF_INVALID_EXEC_STATE, ufExecState ) - ufExecState = DD_UF_PAUSED; - break; + currentUFCompCounter = 0; + isUFRateUpdated = FALSE; + } } + else if ( TRUE == isUFRateUpdated ) + { + // Get updated UF rate + compUFrate = getTDUFRate(); + // Reset the flag + isUFRateUpdated = FALSE; + } - //Publish ultrafiltration data - publishUltrafiltrationData(); + // Calculate UF volume and determine UF pause/run + updateUFRequest(); - return ufExecState; -} - -/*********************************************************************//** - * @brief - * The handleUFPausedState function handles the ultrafiltration - * paused state. - * @details \b Inputs:isUltrafiltrationRequested - * @details \b Outputs: UF state - * @return next UF state. - *************************************************************************/ -static UF_EXEC_STATE_T handleUFPausedState( void ) -{ - UF_EXEC_STATE_T state = DD_UF_PAUSED; - + // UF pump run or stop based on the flag if ( TRUE == isUltrafiltrationRequested ) { - // start with TD UF rate - compUFrate = getTDUFRate(); - currentUFCompCounter = 0; - setConcentratePumpTargetSpeed( D76_PUMP, compUFrate, DOSING_CONT_VOLUME ); requestConcentratePumpOn( D76_PUMP ); - - //Tranistion to run state - state = DD_UF_RUNNING; } - - return state; -} - -/*********************************************************************//** - * @brief - * The handleUFPausedState function handles the ultrafiltration - * running state. - * @details \b Inputs:isUltrafiltrationRequested - * @details \b Outputs: UF state - * @return next UF state. - *************************************************************************/ -static UF_EXEC_STATE_T handleUFRunningState( void ) -{ - UF_EXEC_STATE_T state = DD_UF_RUNNING; - - if ( TRUE != isUltrafiltrationRequested ) + else { requestConcentratePumpOff( D76_PUMP, FALSE ); - state = DD_UF_PAUSED; } - return state; + //Publish ultrafiltration data + publishUltrafiltrationData(); } /*********************************************************************//** * @brief - * The updateUFRequest function updates the ultrafiltration rate per iteration - * and of the ultrafiltration. - * @details \b Inputs: TD Uf rate, TD Dialysate flow rate and bypass flag - * @details \b Outputs: ufVolumeperIteration , isUltrafiltrationRequested + * The updateUFRequest function updates the ultrafiltration requested + * flag to true or false based on the compensated UF rate. + * @details \b Inputs: compensated UF + * @details \b Outputs: isUltrafiltrationRequested * @return none. *************************************************************************/ static void updateUFRequest( void ) { + F32 qd = getTDDialysateFlowrate(); + BOOL bypass = getTDDialyzerBypass(); + BOOL prevUFState = isUltrafiltrationRequested; + // update latest UF run/pause request - if ( ( getTDUFRate() > ZERO_RATE ) && ( TRUE != getTDDialyzerBypass() ) && - ( getTDDialysateFlowrate() > ZERO_RATE ) ) + // if qd is zero or dialyzer is bypassed, UF pump should be turn off + if ( ( compUFrate > ZERO_RATE ) && ( qd > ZERO_RATE ) && ( FALSE == bypass ) ) { isUltrafiltrationRequested = TRUE; } else { isUltrafiltrationRequested = FALSE; } + + // event update on state switch + if( prevUFState != isUltrafiltrationRequested ) + { + SEND_EVENT_WITH_2_U32_DATA( DD_EVENT_UF_PUMP_ON_OFF, prevUFState, isUltrafiltrationRequested ); + } } /*********************************************************************//** @@ -216,48 +184,6 @@ /*********************************************************************//** * @brief - * The UpdateUFCompensation function updates the ultrafiltration rate - * based on the dialysate temperature compensation. - * @details \b Inputs: D4 and D50 temperature - * @details \b Outputs: updated UF rate - * @return none. - *************************************************************************/ -static void UpdateUFCompensation( void ) -{ - if ( ( ++currentUFCompCounter >= UF_COMP_INTERVAL ) || ( TRUE == isUFRateUpdated ) ) - { - F32 freshDensity = ( COMP_SLOPE * getD4AverageTemperature() ) + COMP_INTERCEPT; // Fresh side dialysate density - F32 spentDensity = ( COMP_SLOPE * getD50AverageTemperature() ) + COMP_INTERCEPT; // spent side dialysate density - F32 compFreshFlowrate = getTDDialysateFlowrate() * freshDensity; // Qd * fresh density - F32 compSpentFlowrate = getTDDialysateFlowrate() * spentDensity; // Qd * spent density - F32 balancingError = compFreshFlowrate - compSpentFlowrate; // Error in g/min - - //Update compensate UF rate with the balancing chamber error - compUFrate = getTDUFRate() + balancingError; - - // Update UF rate - setConcentratePumpTargetSpeed( D76_PUMP, compUFrate, DOSING_CONT_VOLUME ); - - currentUFCompCounter = 0; - isUFRateUpdated = FALSE; - } -} - -/*********************************************************************//** - * @brief - * The getCurrentUFExecState function returns the current state - * of the ultrafiltration. - * @details \b Inputs: ufExecState - * @details \b Outputs: none - * @return the current state of UF execution state. - *************************************************************************/ -UF_EXEC_STATE_T getCurrentUFExecState( void ) -{ - return ufExecState; -} - -/*********************************************************************//** - * @brief * The publishUltrafiltrationData function broadcasts the ultrafiltration * data at defined interval. * @details \b Inputs: ufDataPublicationTimerCounter @@ -272,7 +198,6 @@ { UF_DATA_T data; - data.ufExecState = (U32)ufExecState; data.ufRate = getTDUFRate(); data.compUFrate = compUFrate; data.isUFRequested = (U32)isUltrafiltrationRequested;