/************************************************************************** * * Copyright (c) 2019-2020 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) Quang Nguyen * @date (last) 14-Sep-2020 * * @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 "Heaters.h" #include "InternalADC.h" #include "MessageSupport.h" #include "ModeFill.h" #include "OperationModes.h" #include "PIControllers.h" #include "ROPump.h" #include "SafetyShutdown.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" #include "TaskPriority.h" #include "TemperatureSensors.h" #include "Timers.h" /** * @addtogroup Heaters * @{ */ // ********** private definitions ********** #define HEATERS_MAX_DUTY_CYCLE 1.00 ///< Heaters max duty cycle (100%). #define HEATERS_MIN_DUTY_CYCLE 0.00 ///< Heaters minimum duty cycle (0.00%). #define HEATERS_MIN_HEAT_DISINFECT_DUTY_CYCLE 0.6 ///< Heaters minimum duty cycle during heat disinfect. #define HEATERS_DATA_PUBLISH_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Heaters data publish interval. #define MINIMUM_TARGET_TEMPERATURE 10.0 ///< Minimum allowed target temperature for the heaters. #define MAXIMUM_TARGET_TEMPERATURE 90.0 ///< Maximum allowed target temperature for the heaters. #define MAXIMUM_IDLE_DRAIN_TARGET_TEMPERATURE 58.0 ///< Maximum allowed target temperature for the idle and drain modes. #define HEATERS_CONTROL_STATE_CHECK_INTERVAL_COUNT ( ( 10 * MS_PER_SECOND ) / TASK_GENERAL_INTERVAL ) ///< Temperature sensors interval count. #define HEATERS_ON_WITH_NO_FLOW_TIMEOUT_COUNT ( ( 3 * MS_PER_SECOND ) / TASK_PRIORITY_INTERVAL ) ///< Heaters are on but there is no sufficient flow timeout in counts. #define HEATERS_MAX_ALLOWED_INTERNAL_TEMPERATURE_C 170.0 ///< Heaters max allowed internal temperature in C. #define HEATERS_MAX_ALLOWED_COLD_JUNCTION_TEMPERATURE_C 80.0 ///< Heaters max allowed cold junction temperature in C. #define HEATERS_MAX_ALLOWED_INTERNAL_TEMPERATURE_TIMEOUT_MS ( 2 * SEC_PER_MIN * MS_PER_SECOND ) ///< Heaters max allowed internal temperature timeout in milliseconds. #define HEATERS_ON_NO_FLOW_TIMEOUT_MS ( 2 * SEC_PER_MIN * MS_PER_SECOND ) ///< Heaters on with no flow time out in milliseconds. #define HEATERS_MAX_OPERATING_VOLTAGE_V 24.0 ///< Heaters max operating voltage in volts. #define HEATERS_VOLTAGE_MONITOR_TIME_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Heaters voltage monitor timer interval. #define HEATERS_MAX_VOLTAGE_OUT_OF_RANGE_TOL 0.2 ///< Heaters max voltage out of range tolerance. #define HEATERS_MIN_RAMP_TIME_MS ( 6 * MS_PER_SECOND ) ///< Heaters minimum time that they have to stay in the ramp state in milliseconds. #define TEMPERATURES_MOVING_AVG_SIZE 3U ///< Heaters ramp state temperatures moving average size. #define DELTA_TEMPERATURE_TIME_COSNTANT_C 8.6 ///< Delta temperature calculated from time constant. #define MAXIMUM_ALLOWED_TARGET_TEMPERATURE_DEVIATION_C 0.25 ///< Maximum allowed temperature deviation from target temperature in C. #define PRIMARY_HEATER_DUTY_CYCLE_PER_TEMPERATURE_C 0.03 ///< Primary heaters duty cycle per temperature in C. 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.5; ///< Primary heaters maximum power (main primary = 475W and small primary = 237.5W). /// Heaters exec states typedef enum Heaters_Exec_States { HEATER_EXEC_STATE_OFF = 0, ///< Heater exec state off. HEATER_EXEC_STATE_PRIMARY_RAMP_TO_TARGET, ///< Heater exec state primary ramp to target. HEATER_EXEC_STATE_PRIMARY_CONTROL_TO_TARGET, ///< Heater exec state primary control to target. HEATER_EXEC_STATE_CONTROL_TO_DISINFECT_TARGET, ///< Heater exec state control to disinfect (heat or chemical) target. HEATER_EXEC_STATE_TRIMMER_RAMP_TO_TARGET, ///< Heater exec state trimmer ramp to target. HEATER_EXEC_STATE_TRIMMER_CONTROL_TO_TARGET, ///< Heater exec state trimmer control to target. NUM_OF_HEATERS_STATE, ///< Number of heaters state. } HEATERS_STATE_T; /// 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 targetROFlow; ///< Heater target flow. U32 heaterOnWithNoFlowTimer; // TODO remove ///< Heater on with no flow timer. BOOL isFlowBelowMin; ///< Heater flow below minimum flag indicator. BOOL hasTargetTempChanged; ///< Heater target temperature change flag indicator. F32 heaterEfficiency; ///< Heater efficiency during the run. BOOL hasTargetBeenReached; ///< Heater flag to indicate whether the target temperature has been reached. U32 tempOutOfRangeTimer; ///< Heater temperature out of range timer TODO remove once the mechanical thermal cutoff was implemented BOOL isHeaterTempOutOfRange; ///< Heater temperature out of range flag indicator TODO remove once the mechanical thermal cutoff was implemented } HEATER_STATUS_T; static HEATER_STATUS_T heatersStatus[ NUM_OF_DG_HEATERS ]; ///< Heaters status. static U32 dataPublicationTimerCounter; ///< Data publication timer counter. static OVERRIDE_U32_T heatersDataPublishInterval = { HEATERS_DATA_PUBLISH_INTERVAL, HEATERS_DATA_PUBLISH_INTERVAL, 0, 0 }; ///< Heaters data publish time interval. static U32 voltageMonitorTimeCounter = 0; ///< Heaters voltage monitor counter. static U32 operationMode = 0; // ********** 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 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 checkPrimaryHeaterTempSensors( void ); static void checkTrimmerHeaterTempSensors( void ); static void monitorHeatersVoltage( void ); /*********************************************************************//** * @brief * The initHeaters initializes the heaters driver. * @details Inputs: none * @details Outputs: voltageMonitorTimeCounter, heaterStatus, * hasTreatmentInternalTempBeenSet * @return none *************************************************************************/ void initHeaters( void ) { DG_HEATERS_T heater; voltageMonitorTimeCounter = 0; operationMode = 0; for ( heater = DG_PRIMARY_HEATER; heater < NUM_OF_DG_HEATERS; heater++ ) { heatersStatus[ heater ].startHeaterSignal = FALSE; heatersStatus[ heater ].tempOutOfRangeTimer = 0; heatersStatus[ heater ].isHeaterTempOutOfRange = FALSE; heatersStatus[ heater ].state = HEATER_EXEC_STATE_OFF; heatersStatus[ heater ].targetTemp = 0.0; heatersStatus[ heater ].dutycycle = 0.0; heatersStatus[ heater ].targetROFlow = 0.0; heatersStatus[ heater ].hasTargetTempChanged = FALSE; heatersStatus[ heater ].heaterEfficiency = 1.0; // Assuming 100% efficiency during initialization until it is updated heatersStatus[ heater ].hasTargetBeenReached = FALSE; } } /*********************************************************************//** * @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 none *************************************************************************/ void setHeaterTargetTemperature( DG_HEATERS_T heater, F32 targetTemperature ) { 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; // TODO alarm if temperature if out of range or just reject? } } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_HEATERS_INVALID_HEATER_ID_SELECTED, heater ) } } /*********************************************************************//** * @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 ( TRUE == heatersStatus[ heater ].hasTargetTempChanged ) { 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 handleTrimmerHeaterCmd handles a start trimmer heater command from the HD. * It resets the trimmer heater's state and sets the duty cycle of the trimmer heater. * @details Inputs: none * @details Outputs: process command and send back response * @param heaterCmdPtr pointer to heater command data record * @return status *************************************************************************/ void handleTrimmerHeaterCmd( TRIMMER_HEATER_CMD_T *heaterCmdPtr ) { DG_CMD_RESPONSE_T cmdResponse; cmdResponse.commandID = DG_CMD_START_TRIMMER_HEATER; cmdResponse.rejected = TRUE; cmdResponse.rejectCode = DG_CMD_REQUEST_REJECT_REASON_NONE; if ( TRUE == heaterCmdPtr->startHeater ) { if ( ( MINIMUM_TARGET_TEMPERATURE <= heaterCmdPtr->targetTemp ) && ( heaterCmdPtr->targetTemp <= MAXIMUM_TARGET_TEMPERATURE ) ) { cmdResponse.rejected = FALSE; #ifndef DISABLE_HEATERS_AND_TEMPS heatersStatus[ DG_TRIMMER_HEATER ].targetTemp = heaterCmdPtr->targetTemp; heatersStatus[ DG_TRIMMER_HEATER ].startHeaterSignal = TRUE; #endif } else { cmdResponse.rejectCode = DG_CMD_REQUEST_REJECT_REASON_INVALID_PARAMETER; } } else { cmdResponse.rejected = FALSE; stopHeater( DG_TRIMMER_HEATER ); } sendCommandResponseMsg( &cmdResponse ); } /*********************************************************************//** * @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 ) { #ifdef 0 // This code is disabled to prevent any heaters monitoring while the driver in in development. DG_HEATERS_T heater; #ifndef IGNORE_HEATERS_MONITOR checkPrimaryHeaterTempSensors(); checkTrimmerHeaterTempSensors(); #endif for ( heater = DG_PRIMARY_HEATER; heater < NUM_OF_DG_HEATERS; heater++ ) { // Check if a heater is on and whether is duty cycle is not zero if ( ( TRUE == heatersStatus[ heater ].isHeaterOn ) && ( ( heatersStatus[ heater ].dutycycle - HEATERS_MIN_DUTY_CYCLE ) > NEARLY_ZERO ) ) { // TODO add the function that gets the flow of the new flow sensor for DG. For now it is assumed that trimmer heater flow sensor // is not 0 so the heater can run if needed F32 measFlow = ( DG_PRIMARY_HEATER == heater ? getMeasuredROFlowRate() : 50.0 ); // TODO get the minimum new flow sensor flow sensor F32 minFlow = ( DG_PRIMARY_HEATER == heater ? MIN_RO_FLOWRATE_LPM : MIN_RO_FLOWRATE_LPM ); BOOL isFlowLow = ( measFlow < minFlow ? TRUE : FALSE ); if ( TRUE == isFlowLow ) { // Check if the flow of the heater is below minimum for the first time if ( FALSE == heatersStatus[ heater ].isFlowBelowMin ) { heatersStatus[ heater ].isFlowBelowMin = TRUE; heatersStatus[ heater ].heaterOnWithNoFlowTimer = getMSTimerCount(); } else if ( TRUE == didTimeout( heatersStatus[ heater ].heaterOnWithNoFlowTimer, HEATERS_ON_NO_FLOW_TIMEOUT_MS ) ) { // Heater has been on with no flow time out stopHeater( heater ); ALARM_ID_T alarm = ( DG_PRIMARY_HEATER == heater ? ALARM_ID_DG_PRIMARY_HEATER_ON_WITH_NO_FLOW_TIMEOUT : ALARM_ID_DG_TRIMMER_HEATER_ON_WITH_NO_FLOW_TIMEOUT ); activateAlarmNoData( alarm ); } } else { heatersStatus[ heater ].isFlowBelowMin = FALSE; heatersStatus[ heater ].heaterOnWithNoFlowTimer = getMSTimerCount(); } } } monitorHeatersVoltage(); #endif // Check for data publication publishHeatersData(); } /*********************************************************************//** * @brief * The resetHeatersEfficiency function resets the heaters efficiency upon * the start of a treatment. * @details Inputs: none * @details Outputs: heaterStatus * @return none *************************************************************************/ void resetHeatersEfficiency( void ) { heatersStatus[ DG_PRIMARY_HEATER ].heaterEfficiency = 1.0; heatersStatus[ DG_TRIMMER_HEATER ].heaterEfficiency = 1.0; } /*********************************************************************//** * @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; if ( DG_MODE_FILL == getCurrentOperationMode() ) { // If the previous average fill flow rate is 0, use the nominal target RO flow from the RO pump targetFlow = ( getAvgFillFlowRate() - 0.0 > NEARLY_ZERO ? getAvgFillFlowRate() : getTargetROPumpFlowRate() ); dutyCycle = calculatePrimaryHeaterDutyCycle( targetTemperature, inletTemperature, targetFlow, TRUE ); state = HEATER_EXEC_STATE_PRIMARY_CONTROL_TO_TARGET; } else if ( ( DG_MODE_GENE == getCurrentOperationMode() ) || ( DG_MODE_DRAI == getCurrentOperationMode() ) ) { targetTemperature += DELTA_TEMPERATURE_TIME_COSNTANT_C; // Use target flow rate during Idle and drain targetFlow = getTargetROPumpFlowRate(); dutyCycle = calculatePrimaryHeaterDutyCycle( targetTemperature, inletTemperature, targetFlow, FALSE ); state = HEATER_EXEC_STATE_PRIMARY_CONTROL_TO_TARGET; } else if ( ( DG_MODE_HEAT == getCurrentOperationMode() ) || ( DG_MODE_CHEM == getCurrentOperationMode() ) ) { // 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 = getTargetROPumpFlowRate(); dutyCycle = calculatePrimaryHeaterDutyCycle( targetTemperature, inletTemperature, targetFlow, FALSE ); state = HEATER_EXEC_STATE_CONTROL_TO_DISINFECT_TARGET; } 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 disifect 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; return state; } /*********************************************************************//** * @brief * The handleHeaterStateTrimmerControlToTarget function handles the trimmer * heater's control to target. * @details Inputs: heaterStatus * @details Outputs: heaterStatus * @return next state of the state machine *************************************************************************/ static HEATERS_STATE_T handleHeaterStateTrimmerControlToTarget( void ) { HEATERS_STATE_T state = HEATER_EXEC_STATE_TRIMMER_CONTROL_TO_TARGET; 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 ) { if ( DG_PRIMARY_HEATER == heater ) { setMainPrimaryHeaterPWM( pwm ); setSmallPrimaryHeaterPWM( pwm ); } else if ( DG_TRIMMER_HEATER == heater ) { setTrimmerHeaterPWM( pwm ); } 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 ) { // Get the primary heater's efficiency and the last fill temperature from the ModeFill F32 heaterEfficiency = heatersStatus[ DG_PRIMARY_HEATER ].heaterEfficiency; if ( TRUE == checkEfficiency ) { F32 lastFillTemperature = getLastFillTemperature(); // If the last fill temperature > target temperature, it means the primary heater overshot the duty cycle // so with its efficiency is toned down for the next fill cycle // If the heater undershoots the duty cycle, the efficiency increases the duty cycle for the next fill cycle if ( lastFillTemperature - targetTemperature > MAXIMUM_ALLOWED_TARGET_TEMPERATURE_DEVIATION_C ) { heaterEfficiency -= ( lastFillTemperature - targetTemperature ) * PRIMARY_HEATER_DUTY_CYCLE_PER_TEMPERATURE_C; } else if ( lastFillTemperature - targetTemperature <= MAXIMUM_ALLOWED_TARGET_TEMPERATURE_DEVIATION_C ) { heaterEfficiency += ( lastFillTemperature - targetTemperature ) * PRIMARY_HEATER_DUTY_CYCLE_PER_TEMPERATURE_C; } // Update the heaters efficiency heatersStatus[ DG_PRIMARY_HEATER ].heaterEfficiency = heaterEfficiency; } // 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 ) * heaterEfficiency; // Check the boundaries of the calculated duty cycle dutyCycle = ( dutyCycle > HEATERS_MAX_DUTY_CYCLE ? HEATERS_MAX_DUTY_CYCLE : dutyCycle ); dutyCycle = ( dutyCycle < HEATERS_MIN_DUTY_CYCLE ? HEATERS_MIN_DUTY_CYCLE : dutyCycle ); 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 = getTargetROPumpFlowRate(); BOOL hasFlowChanged = ( fabs( targetFlow - heatersStatus[ heater ].targetROFlow ) > 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; // Moving back from control to target to ramp. heatersStatus[ heater ].targetROFlow = 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 ].heaterEfficiency * 100; broadcastData( MSG_ID_DG_HEATERS_DATA, COMM_BUFFER_OUT_CAN_DG_BROADCAST, (U08*)&data, sizeof( HEATERS_DATA_T ) ); dataPublicationTimerCounter = 0; } } /*********************************************************************//** * @brief * The checkPrimaryHeaterTempSensors function checks the primary heater's * thermocouple and cold junction temperature sensors. * @details Inputs: isPrimaryHeaterTempOutOfRange * @details Outputs: isPrimaryHeaterTempOutOfRange * @return none *************************************************************************/ static void checkPrimaryHeaterTempSensors( void ) { F32 primaryHeaterInternalTemp = getTemperatureValue( TEMPSENSORS_PRIMARY_HEATER_INTERNAL ); F32 primaryHeaterColdJunctionTemp = getTemperatureValue( TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION ); BOOL isTempOut = FALSE; // Check if the primary heater's internal temperature or the cold junction temperature is above the allowed limit if ( primaryHeaterInternalTemp > HEATERS_MAX_ALLOWED_INTERNAL_TEMPERATURE_C ) { isTempOut = TRUE; SET_ALARM_WITH_1_U32_DATA( ALARM_ID_DG_PRIMARY_HEATER_INTERNAL_TEMP_OUT_OF_RANGE, primaryHeaterInternalTemp ); } else if ( primaryHeaterColdJunctionTemp > HEATERS_MAX_ALLOWED_COLD_JUNCTION_TEMPERATURE_C ) { isTempOut = TRUE; SET_ALARM_WITH_1_U32_DATA( ALARM_ID_DG_PRIMARY_HEATER_CJ_TEMP_OUT_OF_RANGE, primaryHeaterInternalTemp ); } /*/ If any of the temperatures are above the range /if ( ( FALSE == isPrimaryHeaterTempOutOfRange ) && ( TRUE == isTempOut ) ) { stopPrimaryHeater(); isPrimaryHeaterTempOutOfRange = TRUE; primaryHeaterTempOutTimer = getMSTimerCount(); } // If the primary heaters internal temperature was out for more than the define period, activate the safety shutdown else if ( ( TRUE == isPrimaryHeaterTempOutOfRange ) && ( TRUE == didTimeout( primaryHeaterTempOutTimer, HEATERS_MAX_ALLOWED_INTERNAL_TEMPERATURE_TIMEOUT_MS ) ) ) { isPrimaryHeaterTempOutOfRange = FALSE; activateSafetyShutdown(); }*/ } /*********************************************************************//** * @brief * The checkTrimmerHeaterTempSensors function checks the trimmer heater's * thermocouple and cold junction temperature sensors. * @details Inputs: isTrimmerHeaterTempOutOfRange * @details Outputs: isTrimmerHeaterTempOutOfRange * @return none *************************************************************************/ static void checkTrimmerHeaterTempSensors( void ) { F32 trimmerHeaterInternalTemp = getTemperatureValue( TEMPSENSORS_TRIMMER_HEATER_INTERNAL ); F32 trimmerHeaterColdJunctionTemp = getTemperatureValue( TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION ); BOOL isTempOut = FALSE; // Check if the primary heater's internal temperature or the cold junction temperature is above the allowed limit if ( trimmerHeaterInternalTemp > HEATERS_MAX_ALLOWED_INTERNAL_TEMPERATURE_C ) { isTempOut = TRUE; SET_ALARM_WITH_1_U32_DATA( ALARM_ID_DG_TRIMMER_HEATER_INTERNAL_TEMP_OUT_OF_RANGE, trimmerHeaterInternalTemp ); } else if ( trimmerHeaterColdJunctionTemp > HEATERS_MAX_ALLOWED_COLD_JUNCTION_TEMPERATURE_C ) { isTempOut = TRUE; SET_ALARM_WITH_1_U32_DATA( ALARM_ID_DG_TRIMMER_HEATER_CJ_TEMP_OUT_OF_RANGE, trimmerHeaterColdJunctionTemp ); } // If it is above the range for the first time, stop the trimmer heater // and set the variables /*if ( ( FALSE == isTrimmerHeaterTempOutOfRange ) && ( TRUE == isTempOut ) ) { stopTrimmerHeater(); isTrimmerHeaterTempOutOfRange = TRUE; trimmerHeaterTempOutTimer = getMSTimerCount(); } // If the trimmer heater internal temperature was out for more than the define period, activate the safety shutdown else if ( ( TRUE == isTrimmerHeaterTempOutOfRange ) && ( TRUE == didTimeout( trimmerHeaterTempOutTimer, HEATERS_MAX_ALLOWED_INTERNAL_TEMPERATURE_TIMEOUT_MS ) ) ) { activateSafetyShutdown(); }*/ } /*********************************************************************//** * @brief * The monitorHeatersVoltage function monitors the heaters' voltages * @details Inputs: voltageMonitorTimeCounter * @details Outputs: voltageMonitorTimeCounter * @return none *************************************************************************/ static void monitorHeatersVoltage( void ) { if ( ++voltageMonitorTimeCounter >= HEATERS_VOLTAGE_MONITOR_TIME_INTERVAL ) { F32 mainPriVoltage = getIntADCVoltageConverted( INT_ADC_PRIMARY_HEATER_24_VOLTS ); // TODO it is assumed that the main and small primary heaters have equal voltage since the PWMs are divided into 2 // before applying the PWMs to the heaters. Right now, there is no ADC channel available for the small primary // heater so the main primary heater's ADC channel is used for the small primary heater as well. F32 smallPriVoltage = getIntADCVoltageConverted( INT_ADC_PRIMARY_HEATER_24_VOLTS ); F32 trimmerVoltage = getIntADCVoltageConverted( INT_ADC_TRIMMER_HEATER_24_VOLTS ); // Voltage to PWM is reverse. If PWM = 0 -> V = 24V F32 mainPri = 1.0 - heatersStatus[ DG_PRIMARY_HEATER ].dutycycle; F32 smallPri = 1.0 - heatersStatus[ DG_PRIMARY_HEATER ].dutycycle; F32 trimmer = 1.0 - heatersStatus[ DG_TRIMMER_HEATER ].dutycycle; // Check main primary heater's voltage // The corresponding voltage of the current PWM must be close to the sensed voltage if ( fabs( ( HEATERS_MAX_OPERATING_VOLTAGE_V * mainPri ) - mainPriVoltage ) > HEATERS_MAX_VOLTAGE_OUT_OF_RANGE_TOL * mainPriVoltage ) { SET_ALARM_WITH_1_F32_DATA( ALARM_ID_DG_MAIN_PRIMARY_HEATER_VOLTAGE_OUT_OF_RANGE, mainPriVoltage ); } // Check small primary heater's voltage if ( fabs( ( HEATERS_MAX_OPERATING_VOLTAGE_V * smallPri ) - smallPriVoltage ) > HEATERS_MAX_VOLTAGE_OUT_OF_RANGE_TOL * smallPriVoltage ) { SET_ALARM_WITH_1_F32_DATA( ALARM_ID_DG_SMALL_PRIMARY_HEATER_VOLTAGE_OUT_OF_RANGE, smallPriVoltage ); } // Check trimmer heater's voltage if ( fabs( ( HEATERS_MAX_OPERATING_VOLTAGE_V * trimmer ) - trimmerVoltage ) > HEATERS_MAX_VOLTAGE_OUT_OF_RANGE_TOL * trimmerVoltage ) { SET_ALARM_WITH_1_F32_DATA( ALARM_ID_DG_TRIMMER_HEATER_VOLTAGE_OUT_OF_RANGE, trimmerVoltage ); } voltageMonitorTimeCounter = 0; } } /************************************************************************* * 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; } /**@}*/