Index: firmware/App/Modes/BalancingChamber.c =================================================================== diff -u -r48ca7fd644c67920acc29a80b7af379d0a134d1a -rb6d20d9e5c704a9f7ebceea9bb12731dac61fc2a --- firmware/App/Modes/BalancingChamber.c (.../BalancingChamber.c) (revision 48ca7fd644c67920acc29a80b7af379d0a134d1a) +++ firmware/App/Modes/BalancingChamber.c (.../BalancingChamber.c) (revision b6d20d9e5c704a9f7ebceea9bb12731dac61fc2a) @@ -32,36 +32,36 @@ */ // ********** private definitions ********** -#define BAL_CHAMBER_FILL_VOLUME_ML 30.0 ///< Balancing chamber fill/drain volume per batch operation. -#define FRESH_DIAL_PRESSURE_MIN_PSIG 23.0 ///< Minimum fresh dialysate pressure (PDf) limit for balancing chamber fill operations. -#define FRESH_DIAL_PRESSURE_MAX_PSIG 25.0 ///< Maximum fresh dialysate pressure (PDf) limit for balancing chamber fill operations. -#define SPENT_DIAL_PRESSURE_MIN_PSIG 29.0 ///< Minimum spent dialysate pressure (PDs) limit for balancing chamber fill operations. -#define SPENT_DIAL_PRESSURE_MAX_PSIG 30.0 ///< Maximum spent dialysate pressure (PDs) limit for balancing chamber fill operations. -#define BAL_CHAMBER_DATA_PUBLISH_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the balancing chamber data published. +#define BAL_CHAMBER_FILL_VOLUME_ML 30.0F ///< Balancing chamber fill/drain volume per batch operation. +#define FRESH_DIAL_PRESSURE_MIN_PSIG 23.0F ///< Minimum fresh dialysate pressure (PDf) limit for balancing chamber fill operations. +#define FRESH_DIAL_PRESSURE_MAX_PSIG 25.0F ///< Maximum fresh dialysate pressure (PDf) limit for balancing chamber fill operations. +#define SPENT_DIAL_PRESSURE_MIN_PSIG 29.0F ///< Minimum spent dialysate pressure (PDs) limit for balancing chamber fill operations. +#define SPENT_DIAL_PRESSURE_MAX_PSIG 30.0F ///< Maximum spent dialysate pressure (PDs) limit for balancing chamber fill operations. +#define BAL_CHAMBER_DATA_PUBLISH_INTERVAL ( 250 / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the balancing chamber data published. // ********** private data ********** -static BAL_CHAMBER_EXEC_STATE_T balChamberExecState = BAL_CHAMBER_STATE_START; ///< Current balancing chamber executive state. -static OVERRIDE_F32_T balChamberSwitchingFreq; ///< Balancing chamber switching rate based on the dialysate flow rate ( overrideable) -static U32 balChamberSwitchingPeriod = 0; ///< Periodic balancing chamber switching time in task interval count. -static U32 currentBalChamberSwitchingCounter = 0; ///< Counter (in task interval) to monitor the timing spent during balancing chamber fill/drain operation. -static BOOL isBalChamberFillInProgress = FALSE; ///< Flag indicating balancing chamber fill/drain is in progress. -static BOOL isPressureStalbilizedDuringFill = FALSE; ///< Flag indicating that the pressure is stablized due to fill complete. -static BOOL readyToDeliverDialysate = FALSE; ///< Flag indicating whether ready to deliver dialysate or not. +static BAL_CHAMBER_EXEC_STATE_T balChamberExecState; ///< Current balancing chamber executive state. +static OVERRIDE_F32_T balChamberSwitchingFreq; ///< Balancing chamber switching rate ( switches per min) based on the dialysate flow rate ( overrideable) +static U32 balChamberSwitchingPeriod; ///< Periodic balancing chamber switching time in task interval count. +static U32 currentBalChamberSwitchingCounter; ///< Counter (in task interval) to monitor the timing spent during balancing chamber fill/drain operation. +static BOOL isBalChamberFillInProgress; ///< Flag indicating balancing chamber fill/drain is in progress. +static BOOL isPressureStalbilizedDuringFill; ///< Flag indicating that the pressure is stablized due to fill complete. +static BOOL isDialysateGoodtoDeliver; ///< Flag indicating whether ready to deliver dialysate or not. static OVERRIDE_U32_T isDialDeliveryInProgress; ///< To indicate dialysate started delivering to dialyzer for treatment (overrideable) -static BAL_CHAMBER_SW_STATE_T balChamberSWState = BAL_CHAMBER_SW_STATE1; ///< Current balancing chamber switching state ( state 1 or state 2). +static BAL_CHAMBER_SW_STATE_T balChamberSWState; ///< Current balancing chamber switching state ( state 1 or state 2). static U32 balChamberDataPublicationTimerCounter; ///< Used to schedule balancing chamber data publication to CAN bus. static OVERRIDE_U32_T balChamberDataPublishInterval; ///< Balancing chamber data publish interval. // ********** private function prototypes ********** static BAL_CHAMBER_EXEC_STATE_T handleBalChamberState1FillStart( void ); static BAL_CHAMBER_EXEC_STATE_T handleBalChamberConcentrateControl(void); -static BAL_CHAMBER_EXEC_STATE_T handleBalChamberState1FillEnd(); +static BAL_CHAMBER_EXEC_STATE_T handleBalChamberState1FillEnd(void); static BAL_CHAMBER_EXEC_STATE_T handleBalChamberState2FillStart( void ); -static BAL_CHAMBER_EXEC_STATE_T handleBalChamberState2FillEnd(); -static BAL_CHAMBER_EXEC_STATE_T handleBalChamberDialDeliveryBypassCheck( void ); +static BAL_CHAMBER_EXEC_STATE_T handleBalChamberState2FillEnd(void); static BAL_CHAMBER_EXEC_STATE_T handleBalChamberDialDeliveryPause( void ); +static void balChamberDialDeliveryBypassCheck( void ); static void publishBalChamberData( void ); static U32 getBalChamberDataPublishInterval( void ); @@ -92,7 +92,7 @@ isBalChamberFillInProgress = FALSE; currentBalChamberSwitchingCounter = 0; isPressureStalbilizedDuringFill = FALSE; - readyToDeliverDialysate = FALSE; + isDialysateGoodtoDeliver = FALSE; balChamberDataPublicationTimerCounter = 0; } @@ -132,17 +132,46 @@ /*********************************************************************//** * @brief - * The execBalancingChamber function executes the balancing chamber state machine. + * The execBalancingChamberMonitor function monitors the balancing chamber fill + * operations and alarm if temperature and conductivity is not in range. * @details \b Inputs: balChamberExecState * @details \b Outputs: balChamberExecState * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT when wrong balancing chamber state invoked. * @return current state. *************************************************************************/ -U32 execBalancingChamber( void ) +void execBalancingChamberMonitor( void ) { - // Increment counter during fill time only + // Read temperature and conducitivity + //F32 hydChamberTemperature = getTemperatureValue( TEMPSENSORS_HYDRAULICS_PRIMARY_HEATER ); + //F32 biCarbConductivity = getConductivityValue( CONDUCTIVITYSENSORS_CD1_SENSOR ); + //F32 acidBicarbMixConductivity = getConductivityValue( CONDUCTIVITYSENSORS_CD3_SENSOR ); + + // Monitor critical parameter while balancing chamber fill is in progress if ( TRUE == isBalChamberFillInProgress ) { + // Temperature range check + + // Coductivity range check + + // Also, TD confirms to deliver dialysate + isDialysateGoodtoDeliver = TRUE; + } +} + +/*********************************************************************//** + * @brief + * The execBalancingChamberControl function executes the balancing chamber state machine. + * @details \b Inputs: balChamberExecState + * @details \b Outputs: balChamberExecState + * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT when wrong balancing chamber state invoked. + * @return current state. + *************************************************************************/ +U32 execBalancingChamberControl( void ) +{ + // Increment counter during fill time only, Also check dialysate needs to be bypassed. + if ( TRUE == isBalChamberFillInProgress ) + { + balChamberDialDeliveryBypassCheck(); currentBalChamberSwitchingCounter += 1; } @@ -177,10 +206,6 @@ balChamberExecState = handleBalChamberState2FillEnd(); break; - case BAL_CAHMBER_DIAL_DELVIERY_BYPASS_CHECK: - balChamberExecState = handleBalChamberDialDeliveryBypassCheck(); - break; - case BAL_CHAMBER_DIAL_DELIVERY_PAUSE: balChamberExecState = handleBalChamberDialDeliveryPause(); break; @@ -321,14 +346,13 @@ // Check fresh and spent dialysate pressure in range if ( ( ( freshDialPressure >= FRESH_DIAL_PRESSURE_MIN_PSIG ) && ( freshDialPressure <= FRESH_DIAL_PRESSURE_MAX_PSIG ) ) && - ( ( spentDialPressure >= SPENT_DIAL_PRESSURE_MIN_PSIG ) && ( spentDialPressure <= SPENT_DIAL_PRESSURE_MAX_PSIG ) ) ) + ( ( spentDialPressure >= SPENT_DIAL_PRESSURE_MIN_PSIG ) && ( spentDialPressure <= SPENT_DIAL_PRESSURE_MAX_PSIG ) ) ) { //Valve control for state 1 fill valveControlForBCState1FillStart(); // Update fill status flag to true isBalChamberFillInProgress = TRUE; - currentBalChamberSwitchingCounter += 1; state = BAL_CHAMBER_STATE1_BICARB_ACID_DOSING_CNTRL; } @@ -383,15 +407,15 @@ // Check fresh and spent dialysate pressure back in range to indicate fill complete. if ( ( ( freshDialPressure >= FRESH_DIAL_PRESSURE_MIN_PSIG ) && ( freshDialPressure <= FRESH_DIAL_PRESSURE_MAX_PSIG ) ) && - ( ( spentDialPressure >= SPENT_DIAL_PRESSURE_MIN_PSIG ) && ( spentDialPressure <= SPENT_DIAL_PRESSURE_MAX_PSIG ) ) ) + ( ( spentDialPressure >= SPENT_DIAL_PRESSURE_MIN_PSIG ) && ( spentDialPressure <= SPENT_DIAL_PRESSURE_MAX_PSIG ) ) ) { // stabilized pressure indicating fill is complete isPressureStalbilizedDuringFill = TRUE; } - // Check switching cycle time or pressure check for valve closure + // Swithcing time met or pressure in range, close valves if ( ( currentBalChamberSwitchingCounter >= balChamberSwitchingPeriod ) || - ( TRUE == isPressureStalbilizedDuringFill ) ) + ( TRUE == isPressureStalbilizedDuringFill ) ) { // close the state 1 opened valves valveControlForBCState1FillEnd(); @@ -400,8 +424,15 @@ // On completion of cycle time, transition to next state if ( currentBalChamberSwitchingCounter >= balChamberSwitchingPeriod ) { - isBalChamberFillInProgress = FALSE; - state = BAL_CHAMBER_STATE2_FILL_START; + if ( TRUE != isPressureStalbilizedDuringFill ) + { + // TODO : Alarm handling for switching time expired, but still pressure not in range, + } + else + { + isBalChamberFillInProgress = FALSE; + state = BAL_CHAMBER_STATE2_FILL_START; + } } return state; @@ -428,14 +459,13 @@ // Check fresh and spent dialysate pressure in range if ( ( ( freshDialPressure >= FRESH_DIAL_PRESSURE_MIN_PSIG ) && ( freshDialPressure <= FRESH_DIAL_PRESSURE_MAX_PSIG ) ) && - ( ( spentDialPressure >= SPENT_DIAL_PRESSURE_MIN_PSIG ) && ( spentDialPressure <= SPENT_DIAL_PRESSURE_MAX_PSIG ) ) ) + ( ( spentDialPressure >= SPENT_DIAL_PRESSURE_MIN_PSIG ) && ( spentDialPressure <= SPENT_DIAL_PRESSURE_MAX_PSIG ) ) ) { // Valve control for state 2 fill valveControlForBCState2FillStart(); // Update fill status flag to true isBalChamberFillInProgress = TRUE; - currentBalChamberSwitchingCounter += 1; state = BAL_CHAMBER_STATE2_BICARB_ACID_DOSING_CNTRL; } @@ -463,33 +493,33 @@ // Check fresh and spent dialysate pressure back in range to indicate fill complete. if ( ( ( freshDialPressure >= FRESH_DIAL_PRESSURE_MIN_PSIG ) && ( freshDialPressure <= FRESH_DIAL_PRESSURE_MAX_PSIG ) ) && - ( ( spentDialPressure >= SPENT_DIAL_PRESSURE_MIN_PSIG ) && ( spentDialPressure <= SPENT_DIAL_PRESSURE_MAX_PSIG ) ) ) + ( ( spentDialPressure >= SPENT_DIAL_PRESSURE_MIN_PSIG ) && ( spentDialPressure <= SPENT_DIAL_PRESSURE_MAX_PSIG ) ) ) { // stabilized pressure indicating fill is complete isPressureStalbilizedDuringFill = TRUE; } // Check switching cycle time or pressure check for valve closure if ( ( currentBalChamberSwitchingCounter >= balChamberSwitchingPeriod ) || - ( TRUE == isPressureStalbilizedDuringFill ) ) + ( TRUE == isPressureStalbilizedDuringFill ) ) { // close the valves valveControlForBCState2FillEnd(); } // On completion of cycle time, transition to next state - if ( ( currentBalChamberSwitchingCounter >= balChamberSwitchingPeriod ) && - ( TRUE == getDialDeliveryProgressStatus() ) ) + if ( currentBalChamberSwitchingCounter >= balChamberSwitchingPeriod ) { - // do periodic dialysate delivery - state = BAL_CHAMBER_STATE1_FILL_START; - isBalChamberFillInProgress = FALSE; + if ( TRUE != isPressureStalbilizedDuringFill ) + { + // TODO : Alarm handling for switching time expired, but still pressure not in range, + } + else + { + isBalChamberFillInProgress = FALSE; + state = BAL_CHAMBER_STATE1_FILL_START; + } } - else if( currentBalChamberSwitchingCounter >= balChamberSwitchingPeriod ) - { - state = BAL_CAHMBER_DIAL_DELVIERY_BYPASS_CHECK; - isBalChamberFillInProgress = FALSE; - } return state; } @@ -498,30 +528,32 @@ * @brief * The handleBalChamberDialDeliveryBypassCheck function decides closing the * bypass valve and open up the dialyzer valves for treatment. - * @details \b Inputs: readyToDeliverDialysate - * @details \b Outputs: readyToDeliverDialysate,isDialDeliveryInProgress - * @return next balancing chamber state. + * @details \b Inputs: isDialysateGoodtoDeliver + * @details \b Outputs: isDialysateGoodtoDeliver,isDialDeliveryInProgress + * @return none. *************************************************************************/ -static BAL_CHAMBER_EXEC_STATE_T handleBalChamberDialDeliveryBypassCheck( void ) +static void balChamberDialDeliveryBypassCheck( void ) { - BAL_CHAMBER_EXEC_STATE_T state = BAL_CHAMBER_STATE1_FILL_START; - - // TODO: Make sure the pressure, temperature and conductivity in range - // Also TD confirms the delivery of dialysate. - readyToDeliverDialysate = TRUE; - - if ( TRUE == readyToDeliverDialysate ) + if ( TRUE == isDialysateGoodtoDeliver ) { - // Close By pass valve and Open VDI, VDo valves + // Close bypass valve and Open VDI, VDo valves setValveState( VDB2, VALVE_STATE_CLOSED ); setValveState( VDI, VALVE_STATE_OPEN ); setValveState( VDO, VALVE_STATE_OPEN ); - // update dialysate delivery is in progress. + // update dialysate delivery status. isDialDeliveryInProgress.data = TRUE; } + else + { + // Open bypass valve and raise alarm + setValveState( VDI, VALVE_STATE_CLOSED ); + setValveState( VDO, VALVE_STATE_CLOSED ); + setValveState( VDB2, VALVE_STATE_OPEN ); - return state; + // update dialysate delivery status. + isDialDeliveryInProgress.data = FALSE; + } } /*********************************************************************//** @@ -638,7 +670,7 @@ data.isBalChamberFillInProgress = isBalChamberFillInProgress; data.currentBalChamberSwitchingCounter = currentBalChamberSwitchingCounter; data.isPressureStalbilizedDuringFill = isPressureStalbilizedDuringFill; - data.readyToDeliverDialysate = readyToDeliverDialysate; + data.isDialysateGoodtoDeliver = isDialysateGoodtoDeliver; broadcastData( MSG_ID_DD_BAL_CHAMBER_DATA, COMM_BUFFER_OUT_CAN_DD_BROADCAST, (U08*)&data, sizeof( BAL_CHAMBER_DATA_T ) );