/************************************************************************** * * Copyright (c) 2020-2022 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 Heaters.c * * @author (last) Michael Garthwaite * @date (last) 07-Sep-2022 * * @author (original) Dara Navaei * @date (original) 23-Apr-2020 * ***************************************************************************/ #include // Used for converting slope to radians and square root // TI PWM driver #include "etpwm.h" #include "AlarmMgmt.h" #include "DGDefs.h" #include "FlowSensors.h" #include "Heaters.h" #include "MessageSupport.h" #include "ModeFill.h" #include "NVDataMgmt.h" #include "OperationModes.h" #include "PersistentAlarm.h" #include "Reservoirs.h" #include "ROPump.h" #include "SafetyShutdown.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" #include "TaskPriority.h" #include "TemperatureSensors.h" #include "Timers.h" #include "Voltages.h" /** * @addtogroup Heaters * @{ */ // ********** private definitions ********** #define HEATERS_MAX_DUTY_CYCLE 1.00F ///< Heaters max duty cycle (100%). #define HEATERS_MIN_DUTY_CYCLE 0.00F ///< Heaters minimum duty cycle (0.00%). #define HEATERS_MIN_HEAT_DISINFECT_DUTY_CYCLE 0.6F ///< Heaters minimum duty cycle during heat disinfect. #define HEATERS_MIN_EST_GAIN 0.2F ///< Heaters minimum estimation gain. #define HEATERS_MAX_EST_GAIN 5.0F ///< Heaters maximum estimation gain. #define HEATERS_NEUTRAL_EST_GAIN 1.0F ///< Heaters neutral estimation gain. #define HEATERS_DATA_PUBLISH_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Heaters data publish interval. #define MINIMUM_TARGET_TEMPERATURE 10.0F ///< Minimum allowed target temperature for the heaters. #define MAXIMUM_TARGET_TEMPERATURE 90.0F ///< Maximum allowed target temperature for the heaters. #define HEATERS_ON_NO_FLOW_TIMEOUT_MS ( 10 * MS_PER_SECOND ) ///< Heaters on with no flow time out in milliseconds. #define HEATERS_MAX_OPERATING_VOLTAGE_V 24.0F ///< Heaters max operating voltage in volts. #define HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS ( 2 * MS_PER_SECOND ) ///< Heaters voltage out of range time out in milliseconds. #define HEATERS_MAX_VOLTAGE_OUT_OF_RANGE_TOL 0.2F ///< Heaters max voltage out of range tolerance. #define TRIMMER_HEATER_MAX_POWER_W 66.5F ///< Trimmer heater maximum power in Watts. #define TRIMMER_HEATER_CONTROL_CHECK_INTERVAL_COUNT ( ( 30 * MS_PER_SECOND ) / TASK_GENERAL_INTERVAL ) ///< Trimmer heater control interval count. #define DELTA_TEMPERATURE_TIME_COSNTANT_C 8.6F ///< Delta temperature calculated from time constant. #define PRIMARY_HEATER_DUTY_CYCLE_PER_TEMPERATURE_C 0.015F ///< Primary heaters duty cycle per temperature in C. #define DATA_PUBLISH_COUNTER_START_COUNT 70 ///< Data publish counter start count. #define MIN_RO_HEATER_FLOWRATE_LPM 0.2F ///< Minimum target RO heater flow rate in L/min. static const F32 WATER_SPECIFIC_HEAT_DIVIDED_BY_MINUTES = 4184 / SEC_PER_MIN; ///< Water specific heat in J/KgC / 60. static const F32 PRIMARY_HEATERS_MAXIMUM_POWER_WATTS = 475 + 237.5F; ///< Primary heaters maximum power (main primary = 475W and small primary = 237.5W). static const F32 HEATERS_VOLTAGE_TOLERANCE_V = HEATERS_MAX_OPERATING_VOLTAGE_V * HEATERS_MAX_VOLTAGE_OUT_OF_RANGE_TOL; ///< Heaters voltage tolerance in volts. /// Heaters data structure typedef struct { F32 targetTemp; ///< Heater target temperature. HEATERS_STATE_T state; ///< Heater state. BOOL startHeaterSignal; ///< Heater start indication flag. BOOL isHeaterOn; ///< Heater on/off status flag. F32 dutyCycle; ///< Heater duty cycle. F32 targetFlow; ///< Heater target flow. BOOL hasTargetTempChanged; ///< Heater target temperature change flag indicator. F32 heaterEstGain; ///< Heater estimation gain during the run. BOOL hasTargetBeenReached; ///< Heater flag to indicate whether the target temperature has been reached. F32 calculatedTemperature; ///< Heater calculated temperature. DG_RESERVOIR_ID_T inactiveRsrvr; ///< Heater inactive reservoir. } HEATER_STATUS_T; static HEATER_STATUS_T heatersStatus[ NUM_OF_DG_HEATERS ]; ///< Heaters status. static U32 dataPublicationTimerCounter; ///< Data publication timer counter. static U32 trimmerHeaterControlCounter; ///< Trimmer heater control counter. static OVERRIDE_U32_T heatersDataPublishInterval = { HEATERS_DATA_PUBLISH_INTERVAL, HEATERS_DATA_PUBLISH_INTERVAL, 0, 0 }; ///< Heaters data publish time interval. // ********** private function prototypes ********** static HEATERS_STATE_T handleHeaterStateOff( DG_HEATERS_T heater ); static HEATERS_STATE_T handleHeaterStatePrimaryRampToTarget( void ); static HEATERS_STATE_T handleHeaterStatePrimaryControlToTarget( void ); static HEATERS_STATE_T handleHeaterStateControlToDisinfectTarget( DG_HEATERS_T heater ); static HEATERS_STATE_T handleHeaterStateTrimmerRampToTarget( void ); static HEATERS_STATE_T handleHeaterStateTrimmerControlToTarget( void ); static void setHeaterDutyCycle( DG_HEATERS_T heater, F32 pwm ); static F32 calculatePrimaryHeaterDutyCycle( F32 targetTemperature, F32 currentTemperature, F32 flow, BOOL checkEfficiency ); static F32 calculateTrimmerHeaterDutyCycle( F32 targetTemperature, F32 currentTemperature, F32 flow, BOOL checkEfficiency ); static BOOL haveHeaterControlConditionsChanged( DG_HEATERS_T heater ); static void setMainPrimaryHeaterPWM( F32 pwm ); static void setSmallPrimaryHeaterPWM( F32 pwm ); static void setTrimmerHeaterPWM( F32 pwm ); static void publishHeatersData( void ); static void monitorHeatersVoltage( void ); /*********************************************************************//** * @brief * The initHeaters initializes the heaters driver. * @details Inputs: none * @details Outputs: voltageMonitorTimeCounter, heaterStatus, * hasTreatmentInternalTempBeenSet, dataPublicationTimerCounter, * trimmerHeaterControlCounter * @return none *************************************************************************/ void initHeaters( void ) { DG_HEATERS_T heater; dataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; trimmerHeaterControlCounter = 0; for ( heater = DG_PRIMARY_HEATER; heater < NUM_OF_DG_HEATERS; heater++ ) { heatersStatus[ heater ].targetTemp = 0.0F; heatersStatus[ heater ].state = HEATER_EXEC_STATE_OFF; heatersStatus[ heater ].startHeaterSignal = FALSE; heatersStatus[ heater ].isHeaterOn = FALSE; heatersStatus[ heater ].dutyCycle = 0.0F; heatersStatus[ heater ].targetFlow = 0.0F; heatersStatus[ heater ].hasTargetTempChanged = FALSE; heatersStatus[ heater ].heaterEstGain = HEATERS_NEUTRAL_EST_GAIN; heatersStatus[ heater ].hasTargetBeenReached = FALSE; } // Initialize the persistent alarms initPersistentAlarm( ALARM_ID_DG_MAIN_PRIMARY_HEATER_VOLTAGE_OUT_OF_RANGE, HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS, HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS ); initPersistentAlarm( ALARM_ID_DG_SMALL_PRIMARY_HEATER_VOLTAGE_OUT_OF_RANGE, HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS, HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS ); initPersistentAlarm( ALARM_ID_DG_TRIMMER_HEATER_VOLTAGE_OUT_OF_RANGE, HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS, HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS ); initPersistentAlarm( ALARM_ID_RO_FLOW_TOO_LOW_WHILE_PRIMARY_HEATER_IS_ON, HEATERS_ON_NO_FLOW_TIMEOUT_MS, HEATERS_ON_NO_FLOW_TIMEOUT_MS ); initPersistentAlarm( ALARM_ID_DIALYSATE_FLOW_TOO_LOW_WHILE_TRIMMER_HEATER_IS_ON, HEATERS_ON_NO_FLOW_TIMEOUT_MS, HEATERS_ON_NO_FLOW_TIMEOUT_MS ); } /*********************************************************************//** * @brief * The setHeaterTargetTemperature function sets the target temperature of a heater. * @details Inputs: none * @details Outputs: heaterStatus * @param heater: heater ID that its target temperature is set * @param targetTemperature: target temperature of that the heater has to * heat the fluid * @return TRUE if the temperature was set otherwise, FALSE *************************************************************************/ BOOL setHeaterTargetTemperature( DG_HEATERS_T heater, F32 targetTemperature ) { BOOL result = FALSE; if( heater < NUM_OF_DG_HEATERS ) { // Assume the target temperature has not changed heatersStatus[ heater ].hasTargetTempChanged = FALSE; // Check if the requested temperature is within the allowed range if ( ( targetTemperature >= MINIMUM_TARGET_TEMPERATURE ) && ( targetTemperature <= MAXIMUM_TARGET_TEMPERATURE ) ) { heatersStatus[ heater ].targetTemp = targetTemperature; heatersStatus[ heater ].hasTargetTempChanged = TRUE; result = TRUE; } } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_HEATERS_INVALID_HEATER_ID_SELECTED, heater ) } return result; } /*********************************************************************//** * @brief * The getHeaterTargetTemperature function returns the heater target temperature. * @details Inputs: none * @details Outputs: heaterStatus * @return heater target temperature *************************************************************************/ F32 getHeaterTargetTemperature( DG_HEATERS_T heater ) { return heatersStatus[ heater ].targetTemp; } /*********************************************************************//** * @brief * The startPrimaryHeater function starts the primary heaters. It resets * the primary heaters state and sets the main primary heater duty cycle. * @details Inputs: primaryHeaterTargetTemperature * @details Outputs: hasStartPrimaryHeaterRequested * @return status *************************************************************************/ BOOL startHeater( DG_HEATERS_T heater ) { BOOL status = FALSE; if( heater < NUM_OF_DG_HEATERS ) { if ( HEATER_EXEC_STATE_OFF == heatersStatus[ heater ].state ) { status = TRUE; heatersStatus[ heater ].startHeaterSignal = TRUE; } } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_HEATERS_INVALID_HEATER_ID_SELECTED, heater ) } return status; } /*********************************************************************//** * @brief * The stopHeater stops the specified heater. * @details Inputs: none * @details Outputs: heaterStatus * @param heater: heater ID that is requested to turn on * @return TRUE if the start was accepted otherwise, FALSE *************************************************************************/ void stopHeater( DG_HEATERS_T heater ) { heatersStatus[ heater ].isHeaterOn = FALSE; } /*********************************************************************//** * @brief * The execHeaters function executes the heaters state machine. * @details Inputs: heaterStatus * @details Outputs: heaterStatus * @return none *************************************************************************/ void execHeaters( void ) { DG_HEATERS_T heater; HEATERS_STATE_T state; for ( heater = DG_PRIMARY_HEATER; heater < NUM_OF_DG_HEATERS; heater++ ) { state = heatersStatus[ heater ].state; switch( state ) { case HEATER_EXEC_STATE_OFF: heatersStatus[ heater ].state = handleHeaterStateOff( heater ); break; case HEATER_EXEC_STATE_PRIMARY_RAMP_TO_TARGET: heatersStatus[ heater ].state = handleHeaterStatePrimaryRampToTarget(); break; case HEATER_EXEC_STATE_PRIMARY_CONTROL_TO_TARGET: heatersStatus[ heater ].state = handleHeaterStatePrimaryControlToTarget(); break; case HEATER_EXEC_STATE_CONTROL_TO_DISINFECT_TARGET: heatersStatus[ heater ].state = handleHeaterStateControlToDisinfectTarget( heater ); break; case HEATER_EXEC_STATE_TRIMMER_RAMP_TO_TARGET: heatersStatus[ heater ].state = handleHeaterStateTrimmerRampToTarget(); break; case HEATER_EXEC_STATE_TRIMMER_CONTROL_TO_TARGET: heatersStatus[ heater ].state = handleHeaterStateTrimmerControlToTarget(); break; default: // The heater is in an unknown state. Turn it off and switch to not running state stopHeater( heater ); heatersStatus[ heater ].state = HEATER_EXEC_STATE_OFF; SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_HEATERS_INVALID_EXEC_STATE, heater ); break; } // Check if the heater is requested to be off if ( FALSE == heatersStatus[ heater ].isHeaterOn ) { setHeaterDutyCycle( heater, HEATERS_MIN_DUTY_CYCLE ); heatersStatus[ heater ].state = HEATER_EXEC_STATE_OFF; } } } /*********************************************************************//** * @brief * The execHeatersMonitor function monitors the status of the heaters. * The internal temperature sensors and the voltages of the heaters are * monitored. The flow is continuously checked and if there is no flow * for a period of time, the heaters are turned off. * @details Inputs: heaterStatus * @details Outputs: heaterStatus * @return none *************************************************************************/ void execHeatersMonitor( void ) { DG_HEATERS_T heater; for ( heater = DG_PRIMARY_HEATER; heater < NUM_OF_DG_HEATERS; heater++ ) { // Check if the heater is on and if it is, check the flow sensor's status if ( TRUE == heatersStatus[ heater ].isHeaterOn ) { ALARM_ID_T alarm; F32 measFlow; F32 minFlow; BOOL isFlowLow; if ( DG_PRIMARY_HEATER == heater ) { alarm = ALARM_ID_RO_FLOW_TOO_LOW_WHILE_PRIMARY_HEATER_IS_ON; measFlow = getMeasuredFlowRateLPM( RO_FLOW_SENSOR ); minFlow = MIN_RO_FLOWRATE_LPM; } else { alarm = ALARM_ID_DIALYSATE_FLOW_TOO_LOW_WHILE_TRIMMER_HEATER_IS_ON; measFlow = getMeasuredFlowRateLPM( DIALYSATE_FLOW_SENSOR ); minFlow = MIN_DIALYSATE_FLOWRATE_LPM; } isFlowLow = ( measFlow < minFlow ? TRUE : FALSE ); checkPersistentAlarm( alarm, isFlowLow, measFlow, minFlow ); } else { checkPersistentAlarm( ALARM_ID_RO_FLOW_TOO_LOW_WHILE_PRIMARY_HEATER_IS_ON, FALSE, 0.0, 0.0 ); checkPersistentAlarm( ALARM_ID_DIALYSATE_FLOW_TOO_LOW_WHILE_TRIMMER_HEATER_IS_ON, FALSE, 0.0, 0.0 ); } } #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_HEATERS_MONITOR ) != SW_CONFIG_ENABLE_VALUE ) #endif { monitorHeatersVoltage(); } // Check for data publication publishHeatersData(); } /*********************************************************************//** * @brief * The resetHeatersEstimationGain function resets the heaters estimation gain upon * the start of a treatment. * @details Inputs: none * @details Outputs: heaterStatus * @return none *************************************************************************/ void resetHeatersEstimationGain( void ) { heatersStatus[ DG_PRIMARY_HEATER ].heaterEstGain = HEATERS_NEUTRAL_EST_GAIN; heatersStatus[ DG_TRIMMER_HEATER ].heaterEstGain = HEATERS_NEUTRAL_EST_GAIN; } /*********************************************************************//** * @brief * The calculateHeaterEstimationGain function calculates the heater estimation * gain. * @details Inputs: none * @details Outputs: heatersStatus * @return none *************************************************************************/ void calculateHeaterEstimationGain( DG_HEATERS_T heater ) { #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_HEATERS_EFFICIENCY ) != SW_CONFIG_ENABLE_VALUE ) #endif { F32 heaterEstGain = heatersStatus[ heater ].heaterEstGain; F32 heaterDutyCycle = heatersStatus[ heater ].dutyCycle; F32 lastFillTemperature = getAvgFillTemperature(); F32 primaryTargetTemperature = heatersStatus[ heater ].targetTemp; BOOL isTempUnderTarget = ( lastFillTemperature < primaryTargetTemperature ? TRUE : FALSE ); if ( TRUE == isTempUnderTarget ) { if ( heaterDutyCycle < HEATERS_MAX_DUTY_CYCLE ) { heaterEstGain += ( primaryTargetTemperature - lastFillTemperature ) * PRIMARY_HEATER_DUTY_CYCLE_PER_TEMPERATURE_C; } } else { if ( heaterDutyCycle > HEATERS_MIN_DUTY_CYCLE ) { heaterEstGain -= ( lastFillTemperature - primaryTargetTemperature ) * PRIMARY_HEATER_DUTY_CYCLE_PER_TEMPERATURE_C; } } heaterEstGain = MAX( heaterEstGain, HEATERS_MIN_EST_GAIN ); heaterEstGain = MIN( heaterEstGain, HEATERS_MAX_EST_GAIN ); heatersStatus[ heater ].heaterEstGain = heaterEstGain; } } /*********************************************************************//** * @brief * The handleHeaterStateOff function handles the heater not running state. * @details Inputs: heaterStatus * @details Outputs: heaterStatus * @param heater: The heater Id that its not running state is handled * @return next state of the state machine *************************************************************************/ static HEATERS_STATE_T handleHeaterStateOff( DG_HEATERS_T heater ) { HEATERS_STATE_T state = HEATER_EXEC_STATE_OFF; if ( TRUE == heatersStatus[ heater ].startHeaterSignal ) { heatersStatus[ heater ].isHeaterOn = TRUE; heatersStatus[ heater ].startHeaterSignal = FALSE; // Depending on which heater is called, go to different states state = ( heater == DG_PRIMARY_HEATER ? HEATER_EXEC_STATE_PRIMARY_RAMP_TO_TARGET : HEATER_EXEC_STATE_TRIMMER_RAMP_TO_TARGET ); } return state; } /*********************************************************************//** * @brief * The handleHeaterStatePrimaryRampToTarget function handles the primary heaters' * control while they are ramping to target temperature. * @details Inputs: heaterStatus * @details Outputs: heaterStatus * @return next state of the state machine *************************************************************************/ static HEATERS_STATE_T handleHeaterStatePrimaryRampToTarget( void ) { HEATERS_STATE_T state = HEATER_EXEC_STATE_PRIMARY_RAMP_TO_TARGET; DG_HEATERS_T heater = DG_PRIMARY_HEATER; F32 inletTemperature = getTemperatureValue( (U32)TEMPSENSORS_HEAT_DISINFECT ); F32 targetFlow = 0.0; F32 dutyCycle = 0.0; F32 targetTemperature = heatersStatus[ heater ].targetTemp; DG_OP_MODE_T opMode = getCurrentOperationMode(); if ( DG_MODE_FILL == opMode ) { // If the previous average fill flow rate is 0, use the nominal target RO flow from the RO pump targetFlow = ( getAvgFillFlowRateLPM() - 0.0 > NEARLY_ZERO ? getAvgFillFlowRateLPM() : getTargetROPumpFlowRateLPM() ); dutyCycle = calculatePrimaryHeaterDutyCycle( targetTemperature, inletTemperature, targetFlow, TRUE ); state = HEATER_EXEC_STATE_PRIMARY_CONTROL_TO_TARGET; } else if ( ( DG_MODE_GENE == opMode ) || ( DG_MODE_DRAI == opMode ) ) { targetTemperature += DELTA_TEMPERATURE_TIME_COSNTANT_C; targetFlow = getTargetROPumpFlowRateLPM(); dutyCycle = calculatePrimaryHeaterDutyCycle( targetTemperature, inletTemperature, targetFlow, FALSE ); state = HEATER_EXEC_STATE_PRIMARY_CONTROL_TO_TARGET; } else if ( ( DG_MODE_HEAT == opMode ) || ( DG_MODE_CHEM == opMode ) ) { // If the mode is any of the disinfects, especially heat, use the target flow rate instead of the avg. flow // Most of the times the heater should be running at 100% duty cycle since the target temperature is 81 C targetFlow = getTargetROPumpFlowRateLPM(); dutyCycle = calculatePrimaryHeaterDutyCycle( targetTemperature, inletTemperature, targetFlow, FALSE ); state = HEATER_EXEC_STATE_CONTROL_TO_DISINFECT_TARGET; } else { // No other modes are using the heaters // TODO software fault } // Update the calculated target temperature heatersStatus[ DG_PRIMARY_HEATER ].calculatedTemperature = targetTemperature; setHeaterDutyCycle( heater, dutyCycle ); return state; } /*********************************************************************//** * @brief * The handleHeaterStatePrimaryControlToTarget function handles the primary * heaters' control to target while the heater is targeting to reach to temperature. * @details Inputs: heaterStatus * @details Outputs: heaterStatus * @return next state of the state machine *************************************************************************/ static HEATERS_STATE_T handleHeaterStatePrimaryControlToTarget( void ) { HEATERS_STATE_T state = HEATER_EXEC_STATE_PRIMARY_CONTROL_TO_TARGET; DG_HEATERS_T heater = DG_PRIMARY_HEATER; if ( TRUE == haveHeaterControlConditionsChanged( heater ) ) { state = HEATER_EXEC_STATE_PRIMARY_RAMP_TO_TARGET; } return state; } /*********************************************************************//** * @brief * The handleHeaterStateControlToDisinfectTarget function handles the * heaters' control to target while the operation mode is heat or chemical * disinfects. * @details Inputs: heaterStatus * @details Outputs: heaterStatus * @param heater: The heater Id that its on state is handled * @return next state of the state machine *************************************************************************/ static HEATERS_STATE_T handleHeaterStateControlToDisinfectTarget( DG_HEATERS_T heater ) { HEATERS_STATE_T state = HEATER_EXEC_STATE_CONTROL_TO_DISINFECT_TARGET; F32 heatDisinfectSensorTemp = getTemperatureValue( TEMPSENSORS_HEAT_DISINFECT ); // Check if the heaters control conditions have changed, if yes, switch back to ramp to target if ( TRUE == haveHeaterControlConditionsChanged( heater ) ) { state = HEATER_EXEC_STATE_PRIMARY_RAMP_TO_TARGET; } // If the heat disinfect sensor indicates that the temperature is below target temperature but the target temperature had been reached // before turn on the heaters with 100% power if ( ( heatDisinfectSensorTemp <= heatersStatus[ heater ].targetTemp ) && ( TRUE == heatersStatus[ heater ].hasTargetBeenReached ) ) { heatersStatus[ heater ].hasTargetBeenReached = FALSE; setHeaterDutyCycle( heater, HEATERS_MAX_DUTY_CYCLE ); } // If we have reached to target temperature, turn off the heaters if ( heatDisinfectSensorTemp > heatersStatus[ heater ].targetTemp ) { // Set the flag to true for the next run heatersStatus[ heater ].hasTargetBeenReached = TRUE; // The primary heater are not turned off but it is set to a minimum duty cycle so the temperature is kept // above the target setHeaterDutyCycle( heater, HEATERS_MIN_HEAT_DISINFECT_DUTY_CYCLE ); } return state; } /*********************************************************************//** * @brief * The handleHeaterStateTrimmerRampToTarget function handles the trimmer * heater's ramp to target. * @details Inputs: heaterStatus * @details Outputs: heaterStatus * @return next state of the state machine *************************************************************************/ static HEATERS_STATE_T handleHeaterStateTrimmerRampToTarget( void ) { HEATERS_STATE_T state = HEATER_EXEC_STATE_TRIMMER_RAMP_TO_TARGET; DG_HEATERS_T heater = DG_TRIMMER_HEATER; F32 currentTemperature = 0.0; F32 targetFlowLPM = getTargetDialysateFlowLPM(); F32 dutyCycle = 0.0; F32 targetTemperature = heatersStatus[ heater ].targetTemp; DG_OP_MODE_T opMode = getCurrentOperationMode(); if ( ( DG_MODE_FILL == opMode ) || ( DG_MODE_GENE == opMode ) || ( DG_MODE_DRAI == opMode ) ) { currentTemperature = getReservoirCurrentTemperature(); dutyCycle = calculateTrimmerHeaterDutyCycle( targetTemperature, currentTemperature, targetFlowLPM, TRUE ); state = HEATER_EXEC_STATE_TRIMMER_CONTROL_TO_TARGET; } else if ( DG_MODE_HEAT == opMode ) { // If the mode is heat disinfect, use the target flow rate instead of the avg. flow // Most of the times the heater should be running at 100% duty cycle since the target temperature is 81 C and // it is far from the inlet temperature. currentTemperature = getTemperatureValue( (U32)TEMPSENSORS_HEAT_DISINFECT ); targetFlowLPM = getTargetROPumpFlowRateLPM(); dutyCycle = calculateTrimmerHeaterDutyCycle( targetTemperature, currentTemperature, targetFlowLPM, FALSE ); state = HEATER_EXEC_STATE_CONTROL_TO_DISINFECT_TARGET; } // Update the calculated target temperature // Reset the duty cycle since the reservoir has been switched heatersStatus[ heater ].calculatedTemperature = currentTemperature; heatersStatus[ heater ].inactiveRsrvr = getInactiveReservoir(); heatersStatus[ heater ].targetFlow = targetFlowLPM; heatersStatus[ heater ].dutyCycle = 0.0F; trimmerHeaterControlCounter = 0; // Cap the minimum duty cycle. So if it is calculated to negative, set it to 0 dutyCycle = MAX( dutyCycle, HEATERS_MIN_DUTY_CYCLE ); setHeaterDutyCycle( heater, dutyCycle ); return state; } /*********************************************************************//** * @brief * The handleHeaterStateTrimmerControlToTarget function handles the trimmer * heater's control to target state. * @details Inputs: heaterStatus, trimmerHeaterControlCounter * @details Outputs: heaterStatus, trimmerHeaterControlCounter * @return next state of the state machine *************************************************************************/ static HEATERS_STATE_T handleHeaterStateTrimmerControlToTarget( void ) { HEATERS_STATE_T state = HEATER_EXEC_STATE_TRIMMER_CONTROL_TO_TARGET; F32 tempDutyCycle = 0.0; // If the inactive reservoir has changed from the last run transition to ramp state to recalculate the // duty cycle for the next delivery if ( heatersStatus[ DG_TRIMMER_HEATER ].inactiveRsrvr != getInactiveReservoir() ) { state = HEATER_EXEC_STATE_TRIMMER_RAMP_TO_TARGET; } else if ( ++trimmerHeaterControlCounter > TRIMMER_HEATER_CONTROL_CHECK_INTERVAL_COUNT ) { // When the trimmer heater is on, its duty cycle is adjusted at the control interval. For this control check, // dialysate inlet temperature sensor is used rather than the theoretical calculations. F32 outletRedundantTemperature = getTemperatureValue( TEMPSENSORS_OUTLET_REDUNDANT ); F32 targetTemperature = heatersStatus[ DG_TRIMMER_HEATER ].targetTemp; F32 targetFlowLPM = heatersStatus[ DG_TRIMMER_HEATER ].targetFlow; F32 dutyCycle = calculateTrimmerHeaterDutyCycle( targetTemperature, outletRedundantTemperature, targetFlowLPM, TRUE ); trimmerHeaterControlCounter = 0; tempDutyCycle = heatersStatus[ DG_TRIMMER_HEATER ].dutyCycle + dutyCycle; tempDutyCycle = MIN( tempDutyCycle, HEATERS_MAX_DUTY_CYCLE ); tempDutyCycle = MAX( tempDutyCycle, HEATERS_MIN_DUTY_CYCLE ); setHeaterDutyCycle( DG_TRIMMER_HEATER, tempDutyCycle ); } return state; } /*********************************************************************//** * @brief * The setHeaterDutyCycle function sets the duty cycle of a heater. * @details Inputs: none * @details Outputs: none * @param heater: The heater Id that its duty cycle is set * @param pwm: The PWM that is set * @return none *************************************************************************/ static void setHeaterDutyCycle( DG_HEATERS_T heater, F32 pwm ) { // Check if the requested duty cycle is different from what the heater's duty cycle is. // If the same duty cycle is requested, then it is not needed to send it again. This is to make sure // the same duty cycle is not sent to the hardware all the time. if ( fabs( heatersStatus[ heater ].dutyCycle - pwm ) > NEARLY_ZERO ) { if ( DG_PRIMARY_HEATER == heater ) { setMainPrimaryHeaterPWM( pwm ); setSmallPrimaryHeaterPWM( pwm ); } else if ( DG_TRIMMER_HEATER == heater ) { setTrimmerHeaterPWM( pwm ); } // Updated the heater's information heatersStatus[ heater ].dutyCycle = pwm; } } /*********************************************************************//** * @brief * The calculatePrimaryHeaterDutyCycle function calculates the primary * heater's duty cycle. * @details Inputs: none * @details Outputs: none * @param targetTemperature target temperature of the heater * @oaram currentTemperature current inlet temperature of the heater * @param flow current flow * @return calculated duty cycle *************************************************************************/ static F32 calculatePrimaryHeaterDutyCycle( F32 targetTemperature, F32 currentTemperature, F32 flow, BOOL checkEfficiency ) { // Duty cycle = ( 69.73 * flow rate * deltaT / primary heater maximum power ) ^ 1/2 // Multiply the duty cycle to the heater efficiency F32 dutyCycle = sqrt( ( WATER_SPECIFIC_HEAT_DIVIDED_BY_MINUTES * fabs( targetTemperature - currentTemperature ) * flow ) / PRIMARY_HEATERS_MAXIMUM_POWER_WATTS ); dutyCycle *= heatersStatus[ DG_PRIMARY_HEATER ].heaterEstGain; dutyCycle = MIN( dutyCycle, HEATERS_MAX_DUTY_CYCLE ); dutyCycle = MAX( dutyCycle, HEATERS_MIN_DUTY_CYCLE ); return dutyCycle; } /*********************************************************************//** * @brief * The calculateTrimmerHeaterDutyCycle function calculates the trimmer * heater's duty cycle. * @details Inputs: none * @details Outputs: none * @param targetTemperature target temperature of the heater * @oaram currentTemperature current inlet temperature of the heater * @param flow current flow * @param check efficiency flag to indicate whether to consider heater's * efficiency * @return calculated duty cycle *************************************************************************/ static F32 calculateTrimmerHeaterDutyCycle( F32 targetTemperature, F32 currentTemperature, F32 flow, BOOL checkEfficiency ) { // Get the primary heater's efficiency and the last fill temperature from the ModeFill F32 heaterEstGain = heatersStatus[ DG_TRIMMER_HEATER ].heaterEstGain; F32 dutyCycle = 0.0F; #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_HEATERS_EFFICIENCY ) != SW_CONFIG_ENABLE_VALUE ) #endif { if ( TRUE == checkEfficiency ) { // TODO Do we need efficiency for the trimmer heater? } } // Duty cycle = ( 69.73 * flow rate * deltaT / primary heater maximum power ) and multiply the duty cycle to the heater efficiency dutyCycle = ( ( flow * WATER_SPECIFIC_HEAT_DIVIDED_BY_MINUTES * ( targetTemperature - currentTemperature ) ) / TRIMMER_HEATER_MAX_POWER_W ) * heaterEstGain; dutyCycle = MIN( dutyCycle, HEATERS_MAX_DUTY_CYCLE ); dutyCycle = MAX( dutyCycle, ( HEATERS_MAX_DUTY_CYCLE * -1.0F ) ); return dutyCycle; } /*********************************************************************//** * @brief * The haveHeaterControlConditionsChanged function checks whether the heater * control conditions have changed or not. If the control conditions have * changed it sets the changes the control parameters accordingly. * @details Inputs: heaterStatus, operationMode * @details Outputs: heaterStatus, operationMode * @param heater: The heater Id that its on state is handled * @return TRUE if the control conditions have changed otherwise, FALSE *************************************************************************/ static BOOL haveHeaterControlConditionsChanged( DG_HEATERS_T heater ) { BOOL status = FALSE; F32 targetFlow = ( DG_PRIMARY_HEATER == heater ? getTargetROPumpFlowRateLPM() : getTargetDialysateFlowLPM() ); BOOL hasFlowChanged = ( fabs( targetFlow - heatersStatus[ heater ].targetFlow ) > NEARLY_ZERO ? TRUE : FALSE ); // Check if the target flow has changed or the target temperature has changed. if ( ( TRUE == hasFlowChanged ) || ( TRUE == heatersStatus[ heater ].hasTargetTempChanged ) ) { status = TRUE; heatersStatus[ heater ].targetFlow = targetFlow; heatersStatus[ heater ].hasTargetTempChanged = FALSE; } return status; } /*********************************************************************//** * @brief * The setMainPrimaryHeaterPWM function sets the PWM of the main primary heater. * @details Inputs: none * @details Outputs: Sets the PWM duty cycle for the main primary heater * @param pwm PWM duty cycle to set for 1st primary heater element * @return none *************************************************************************/ static void setMainPrimaryHeaterPWM( F32 pwm ) { etpwmSetCmpA( etpwmREG1, (U32)( (S32)( ( pwm * (F32)(etpwmREG1->TBPRD) ) + FLOAT_TO_INT_ROUNDUP_OFFSET ) ) ); } /*********************************************************************//** * @brief * The setSmallPrimaryHeaterPWM function sets the PWM of the small primary heater. * @details Inputs: none * @details Outputs: Sets the PWM duty cycle for the small primary heater * @param pwm PWM duty cycle to set for 2nd primary heater element * @return none *************************************************************************/ static void setSmallPrimaryHeaterPWM( F32 pwm ) { etpwmSetCmpB( etpwmREG1, (U32)( (S32)( ( pwm * (F32)(etpwmREG1->TBPRD) ) + FLOAT_TO_INT_ROUNDUP_OFFSET ) ) ); } /*********************************************************************//** * @brief * The setTrimmerHeaterPWM function sets the PWM of the trimmer heater. * @details Inputs: none * @details Outputs: Sets the PWM duty cycle for the trimmer heater * @param pwm PWM duty cycle to set for trimmer heater * @return none *************************************************************************/ static void setTrimmerHeaterPWM( F32 pwm ) { etpwmSetCmpA( etpwmREG3, (U32)( (S32)( ( pwm * (F32)(etpwmREG3->TBPRD) ) + FLOAT_TO_INT_ROUNDUP_OFFSET ) ) ); } /*********************************************************************//** * @brief * The publishTemperatureData function publishes the heaters data into * at the defined time interval. * @details Inputs: dataPublicationTimerCounter * @details Outputs: dataPublicationTimerCounter * @return none *************************************************************************/ static void publishHeatersData( void ) { if ( ++dataPublicationTimerCounter >= getU32OverrideValue( &heatersDataPublishInterval ) ) { HEATERS_DATA_T data; data.mainPrimayHeaterDC = heatersStatus[ DG_PRIMARY_HEATER ].dutyCycle * 100.0; // The duty cycle of the primary heater is divided into 2 parts and is applied to main // and small primary heaters. So they are always the same. data.smallPrimaryHeaterDC = heatersStatus[ DG_PRIMARY_HEATER ].dutyCycle * 100.0; data.trimmerHeaterDC = heatersStatus[ DG_TRIMMER_HEATER ].dutyCycle * 100.0; data.primaryTargetTemp = heatersStatus[ DG_PRIMARY_HEATER ].targetTemp; data.trimmerTargetTemp = heatersStatus[ DG_TRIMMER_HEATER ].targetTemp; data.primaryHeaterState = heatersStatus[ DG_PRIMARY_HEATER ].state; data.trimmerHeaterState = heatersStatus[ DG_TRIMMER_HEATER ].state; data.primaryEfficiency = heatersStatus[ DG_PRIMARY_HEATER ].heaterEstGain * 100; data.primaryCalcTargetTemp = heatersStatus[ DG_PRIMARY_HEATER ].calculatedTemperature; data.trimmerCalcCurrentTemp = heatersStatus[ DG_TRIMMER_HEATER ].calculatedTemperature; dataPublicationTimerCounter = 0; broadcastData( MSG_ID_DG_HEATERS_DATA, COMM_BUFFER_OUT_CAN_DG_BROADCAST, (U08*)&data, sizeof( HEATERS_DATA_T ) ); } } /*********************************************************************//** * @brief * The monitorHeatersVoltage function monitors the heaters' voltages * @details Inputs: voltageMonitorTimeCounter * @details Outputs: voltageMonitorTimeCounter * @return none *************************************************************************/ static void monitorHeatersVoltage( void ) { // NOTE: Default to using Primary heater voltage from FPGA F32 mainPriVoltage = getMonitoredLineLevel( MONITORED_LINE_24V_PRIM_HTR_GND_V ); #ifndef _RELEASE_ if ( SW_CONFIG_ENABLE_VALUE == getSoftwareConfigStatus( SW_CONFIG_ENABLE_V3_SYSTEM ) ) { // V3 use CPU based value for Primary, same as Secondary mainPriVoltage = getMonitoredLineLevel( MONITORED_LINE_24V_SEC_HTR_V ); } #endif F32 smallPriVoltage = getMonitoredLineLevel( MONITORED_LINE_24V_SEC_HTR_V ); F32 trimmerVoltage = getMonitoredLineLevel( MONITORED_LINE_24V_TRIM_HTR_V ); // Voltage to PWM is reverse. If PWM = 0 -> V = 24V F32 mainPriDC = heatersStatus[ DG_PRIMARY_HEATER ].dutyCycle; F32 smallPriDC = heatersStatus[ DG_PRIMARY_HEATER ].dutyCycle; F32 trimmerDC = heatersStatus[ DG_TRIMMER_HEATER ].dutyCycle; F32 mainPriExpectedVoltage = HEATERS_MAX_OPERATING_VOLTAGE_V * ( 1.0F - mainPriDC ); F32 smallPriExpectedVoltage = HEATERS_MAX_OPERATING_VOLTAGE_V * ( 1.0F - smallPriDC ); F32 trimmerExpectedVoltage = HEATERS_MAX_OPERATING_VOLTAGE_V * ( 1.0F - trimmerDC ); BOOL isMainPriOut = ( fabs( mainPriExpectedVoltage - mainPriVoltage ) > HEATERS_VOLTAGE_TOLERANCE_V ? TRUE : FALSE ); BOOL isSmallPriOut = ( fabs( smallPriExpectedVoltage - smallPriVoltage ) > HEATERS_VOLTAGE_TOLERANCE_V ? TRUE : FALSE ); BOOL isTrimmerOut = ( fabs( trimmerExpectedVoltage - trimmerVoltage ) > HEATERS_VOLTAGE_TOLERANCE_V ? TRUE : FALSE ); if ( getCurrentOperationMode() != DG_MODE_INIT ) { checkPersistentAlarm( ALARM_ID_DG_MAIN_PRIMARY_HEATER_VOLTAGE_OUT_OF_RANGE, isMainPriOut, mainPriDC, HEATERS_VOLTAGE_TOLERANCE_V ); checkPersistentAlarm( ALARM_ID_DG_SMALL_PRIMARY_HEATER_VOLTAGE_OUT_OF_RANGE, isSmallPriOut, smallPriDC, HEATERS_VOLTAGE_TOLERANCE_V ); checkPersistentAlarm( ALARM_ID_DG_TRIMMER_HEATER_VOLTAGE_OUT_OF_RANGE, isTrimmerOut, trimmerDC, HEATERS_VOLTAGE_TOLERANCE_V ); } } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The testSetHeatersPublishIntervalOverride function overrides the heaters * publish data time interval. * @details Inputs: heatersDataPublishInterval * @details Outputs: heatersDataPublishInterval * @return result *************************************************************************/ BOOL testSetHeatersPublishIntervalOverride( U32 value ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { U32 interval = value / TASK_PRIORITY_INTERVAL; result = TRUE; heatersDataPublishInterval.ovData = interval; heatersDataPublishInterval.override = OVERRIDE_KEY; } return result; } /*********************************************************************//** * @brief * The testResetHeatersPublishIntervalOverride function resets the heaters * publish time interval to its previous time interval. * @details Inputs: heatersDataPublishInterval * @details Outputs: heatersDataPublishInterval * @return result *************************************************************************/ BOOL testResetHeatersPublishIntervalOverride( void ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { result = TRUE; heatersDataPublishInterval.override = OVERRIDE_RESET; heatersDataPublishInterval.ovData = heatersDataPublishInterval.ovInitData; } return result; } /**@}*/