/************************************************************************** * * 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 "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 ///< Main primary heater (heater A) max duty cycle (100%). #define HEATERS_MIN_DUTY_CYCLE 0.00 ///< Primary and trimmer heaters minimum duty cycle (0.00%). #define PRIMARY_HEATER_P_COEFFICIENT 0.05 ///< Primary heaters proportional coefficient. #define PRIMARY_HEATER_I_COEFFICIENT 0.00 // TODO remove? ///< Primary heaters integral coefficient. #define TRIMMER_HEATER_P_COEFFICIENT 0.02 // TODO remove ///< Trimmer heater proportional coefficient. #define TRIMMER_HEATER_I_COEFFICIENT 0.001 // TODO remove ///< Trimmer heater integral coefficient. #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 HEATERS_RAMP_STATE_CHECK_INTERVAL_COUNT 20U ///< Heaters ramp check interval count. #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 PRIMARY_HEATERS_THERMAL_POWER_TO_VOLTAGE_SLOPE 0.31644 ///< Primary heaters thermal power to voltage slope. #define PRIMARY_HEATERS_THERMAL_POWER_TO_VOLTAGE_INTERCEPT 0.021 ///< Primary heaters thermal power to voltage intercept. typedef enum Heaters_Exec_States { HEATER_EXEC_STATE_NOT_RUNNING = 0, ///< Heater exec state not running HEATER_EXEC_STATE_RAMP_TO_TARGET, ///< Heater exec state ramp to target HEATER_EXEC_STATE_CONTROL_TO_TARGET, ///< Heater exec state control to target NUM_OF_HEATERS_STATE, ///< Number of heaters state } HEATERS_STATE_T; /// Heaters data structure typedef struct { F32 targetTemp; ///< Heater Target temperature F32 previousTemps[ TEMPERATURES_MOVING_AVG_SIZE ]; ///< Heater Previous temperatures array U32 previousTempsIndex; ///< Heater previous temperatures arrays current index HEATERS_STATE_T state; ///< Heater state TEMPERATURE_SENSORS_T feedbackSensor; ///< Heater feedback sensor for controlling U32 controlTimerCounter; ///< Heater control timer counter BOOL startHeaterSignal; ///< Heater start indication flag BOOL isHeaterOn; ///< Heater on/off status flag 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 F32 dutycycle; ///< Heater duty cycle F32 targetROFlow; ///< Heater target flow U32 rampStateStartTime; ///< Heater ramp state start time U32 heaterOnWithNoFlowTimer; ///< Heater on with no flow timer BOOL isFlowBelowMin; ///< Heater flow below minimum flag indicator PI_CONTROLLER_ID_T controllerID; ///< Heater PI controller ID TODO remove this? F32 initialDutyCycle; ///< Heater initial duty cycle before a hand off } HEATER_STATUS_T; static HEATER_STATUS_T heaterStatus[ 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 heatersOnWithNoFlowTimer; ///< Heaters are on but there is no sufficient flow. TODO remove static BOOL isFlowBelowMin = FALSE; ///< Boolean flag to indicate if the flow is below the minimum. TODo remove static U32 voltageMonitorTimeCounter = 0; ///< Heaters voltage monitor counter. // ********** private function prototypes ********** static HEATERS_STATE_T handleHeaterStateNotRunning( DG_HEATERS_T heater ); static HEATERS_STATE_T handleHeaterStateRampToTarget( DG_HEATERS_T heater ); static HEATERS_STATE_T handleHeaterStateControlToTarget( DG_HEATERS_T heater ); static void setHeaterDutyCycle( DG_HEATERS_T heater, F32 pwm ); static F32 calculateHeaterDutyCycle( F32 targetTemperature, F32 currentTemperature, F32 targetFlow ); 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: isFlowBelowMin, voltageMonitorTimeCounter, heaterStatus * @return none *************************************************************************/ void initHeaters( void ) { DG_HEATERS_T heater; isFlowBelowMin = FALSE; voltageMonitorTimeCounter = 0; for ( heater = DG_PRIMARY_HEATER; heater < NUM_OF_DG_HEATERS; heater++ ) { heaterStatus[ heater ].controlTimerCounter = 0; // The default feedback sensor of the primary heater is TPo but it changes to THd in heat disinfect // The default feedback sensor of the trimmer heater is TDi all the time heaterStatus[ heater ].feedbackSensor = ( DG_PRIMARY_HEATER == heater ? TEMPSENSORS_OUTLET_PRIMARY_HEATER : TEMPSENSORS_INLET_DIALYSATE ); heaterStatus[ heater ].startHeaterSignal = FALSE; heaterStatus[ heater ].tempOutOfRangeTimer = 0; heaterStatus[ heater ].isHeaterTempOutOfRange = FALSE; heaterStatus[ heater ].state = HEATER_EXEC_STATE_NOT_RUNNING; heaterStatus[ heater ].targetTemp = 0.0; heaterStatus[ heater ].dutycycle = 0.0; heaterStatus[ heater ].targetROFlow = 0.0; heaterStatus[ heater ].previousTempsIndex = 0; heaterStatus[ heater ].controllerID = ( DG_PRIMARY_HEATER == heater ? PI_CONTROLLER_ID_PRIMARY_HEATER : PI_CONTROLLER_ID_TRIMMER_HEATER ); heaterStatus[ heater ].initialDutyCycle = 0.0; // Set the array of the previous temperatures to 0.0 memset( &heaterStatus[ heater ].previousTemps, 0.0, sizeof( TEMPERATURES_MOVING_AVG_SIZE ) ); } // Initialize the PI controller for the primary heaters initializePIController( PI_CONTROLLER_ID_PRIMARY_HEATER, HEATERS_MIN_DUTY_CYCLE, PRIMARY_HEATER_P_COEFFICIENT, PRIMARY_HEATER_I_COEFFICIENT, HEATERS_MIN_DUTY_CYCLE, HEATERS_MAX_DUTY_CYCLE ); // Initialize the PI controller for the trimmer heater initializePIController( PI_CONTROLLER_ID_TRIMMER_HEATER, HEATERS_MIN_DUTY_CYCLE, TRIMMER_HEATER_P_COEFFICIENT, TRIMMER_HEATER_I_COEFFICIENT, HEATERS_MIN_DUTY_CYCLE, HEATERS_MAX_DUTY_CYCLE ); } /*********************************************************************//** * @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 ) { if ( ( targetTemperature >= MINIMUM_TARGET_TEMPERATURE ) && ( targetTemperature <= MAXIMUM_TARGET_TEMPERATURE ) ) { heaterStatus[ heater ].targetTemp = targetTemperature; // 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 heaterStatus[ 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 ) { F32 targetTemp = heaterStatus[ heater ].targetTemp; if ( ( targetTemp >= MINIMUM_TARGET_TEMPERATURE ) && ( targetTemp <= MAXIMUM_TARGET_TEMPERATURE ) ) { status = TRUE; heaterStatus[ 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 ) { heaterStatus[ 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 = heaterStatus[ heater ].state; switch( state ) { case HEATER_EXEC_STATE_NOT_RUNNING: heaterStatus[ heater ].state = handleHeaterStateNotRunning( heater ); break; case HEATER_EXEC_STATE_RAMP_TO_TARGET: heaterStatus[ heater ].state = handleHeaterStateRampToTarget( heater ); break; case HEATER_EXEC_STATE_CONTROL_TO_TARGET: heaterStatus[ heater ].state = handleHeaterStateControlToTarget( heater ); break; default: // The heater is in an unknown state. Turn it off and switch to not running state stopHeater( heater ); heaterStatus[ heater ].state = HEATER_EXEC_STATE_NOT_RUNNING; SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_HEATERS_INVALID_EXEC_STATE, heater ); break; } } } /*********************************************************************//** * @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 heaterStatus[ DG_TRIMMER_HEATER ].targetTemp = heaterCmdPtr->targetTemp; heaterStatus[ 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: isTrimmerHeaterOn, mainPrimaryHeaterDutyCycle, * smallPrimaryHeaterDutyCycle, trimmerHeaterDutyCycle, * heatersOnWithNoFlowTimer, isFlowBelowMin * @details Outputs: heatersOnWithNoFlowTimer, isFlowBelowMin * @return none *************************************************************************/ void execHeatersMonitor( void ) { #ifndef IGNORE_HEATERS_MONITOR checkPrimaryHeaterTempSensors(); checkTrimmerHeaterTempSensors(); #endif // Monitor the heaters voltage //monitorHeatersVoltage(); /* * If any of the heaters are on or any of the heaters' PWMs are not zero, check if the flow is below than the minimum value * If the flow is below minimum for the first time, set the variables * If the flow is below minimum for more than the defined time, stop the heaters and raise the alarm * If the flow is in range, reset the variables * This is to make sure that any of the heaters do not stay on while there is no flow * In the monitor, trimmer heater is only monitored if heat disinfect mode is active. Trimmer heater is usually * controlled by HD so checking the DG flow rate to decide whether it should be on or off is not appropriate */ /*BOOL isModeHeat = ( DG_MODE_HEAT == getCurrentOperationMode() ) && ( TRUE == isTrimmerHeaterOn ); BOOL isHeaterOn = ( TRUE == isPrimaryHeaterOn ) || ( TRUE == isModeHeat ); BOOL isPWMNonZero = ( mainPrimaryHeaterDutyCycle > HEATERS_MIN_DUTY_CYCLE ) || ( smallPrimaryHeaterDutyCycle > HEATERS_MIN_DUTY_CYCLE ) || ( trimmerHeaterDutyCycle > HEATERS_MIN_DUTY_CYCLE ); if ( ( TRUE == isHeaterOn ) || ( TRUE == isPWMNonZero ) ) { F32 measuredFlow = getMeasuredROFlowRate(); if ( measuredFlow < MIN_RO_FLOWRATE_LPM ) { // Flow is below minimum for the first time if ( FALSE == isFlowBelowMin ) { isFlowBelowMin = TRUE; heatersOnWithNoFlowTimer = getMSTimerCount(); } // Flow is below minimum for a long time so raise the alarm else if ( TRUE == didTimeout( heatersOnWithNoFlowTimer, HEATERS_ON_NO_FLOW_TIMEOUT_MS ) ) { stopPrimaryHeater(); stopTrimmerHeater(); activateAlarmNoData( ALARM_ID_DG_HEATERS_ON_WITH_NO_FLOW_TIMEOUT ); } } else { isFlowBelowMin = FALSE; heatersOnWithNoFlowTimer = getMSTimerCount(); } }*/ // Check for data publication publishHeatersData(); } /*********************************************************************//** * @brief * The handleHeaterStateNotRunning 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 handleHeaterStateNotRunning( DG_HEATERS_T heater ) { HEATERS_STATE_T state = HEATER_EXEC_STATE_NOT_RUNNING; if ( TRUE == heaterStatus[ heater ].startHeaterSignal ) { heaterStatus[ heater ].isHeaterOn = TRUE; heaterStatus[ heater ].startHeaterSignal = FALSE; heaterStatus[ heater ].targetROFlow = getTargetROPumpFlowRate(); // If the operation mode is heat disinfect and the heater is primary heater, change the feedback sensor to THd if ( ( DG_MODE_HEAT == getCurrentOperationMode() ) && ( DG_PRIMARY_HEATER == heater ) ) { heaterStatus[ heater ].feedbackSensor = TEMPSENSORS_HEAT_DISINFECT; } TEMPERATURE_SENSORS_T sensor = heaterStatus[ heater ].feedbackSensor; F32 feedbackTemperature = getTemperatureValue( (U32)sensor ); F32 targetTemperature = heaterStatus[ heater ].targetTemp; if ( targetTemperature > feedbackTemperature ) { // Set the heater duty cycle to maximum setHeaterDutyCycle( heater, HEATERS_MAX_DUTY_CYCLE ); } else { // Set the heater duty cycle to minimum since we are cooling setHeaterDutyCycle( heater, HEATERS_MIN_DUTY_CYCLE ); } // Once the heater is staring for the first time so no minimum ramp time is needed heaterStatus[ heater ].rampStateStartTime = getMSTimerCount(); // Turn on the heater state = HEATER_EXEC_STATE_RAMP_TO_TARGET; } return state; } /*********************************************************************//** * @brief * The handleHeaterStateRampToTarget function handles the heaters' control * while they are ramping to target temperature. * @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 handleHeaterStateRampToTarget( DG_HEATERS_T heater ) { HEATERS_STATE_T state = HEATER_EXEC_STATE_RAMP_TO_TARGET; TEMPERATURE_SENSORS_T sensor = heaterStatus[ heater ].feedbackSensor; F32 feedbackTemperature = getTemperatureValue( (U32)sensor ); // Continuously get the target temperature in case it was changed dynamically F32 targetTemperature = heaterStatus[ heater ].targetTemp; U32 currentIndex = heaterStatus[ heater ].previousTempsIndex; if ( ++heaterStatus[ heater ].controlTimerCounter > HEATERS_RAMP_STATE_CHECK_INTERVAL_COUNT ) { U32 i; F32 slope; F32 deltaTemperature; F32 deltaDutyCycle; F32 dutyCycle; F32 revDeltaDutyCycle; F32 inletTemperature; F32 measuredFlow; // Set the hand off flag to false BOOL isItHandOffTime = FALSE; // Update the temperature data array as well as the next index heaterStatus[ heater ].previousTemps[ currentIndex ] = feedbackTemperature; heaterStatus[ heater ].previousTempsIndex = INC_WRAP( currentIndex, 0, TEMPERATURES_MOVING_AVG_SIZE - 1 ); // Calculate the running sum of the temperatures for ( i = 0; i < TEMPERATURES_MOVING_AVG_SIZE; i++ ) { slope += heaterStatus[ heater ].previousTemps[ i ]; } // If the heater is the primary heater, if ( DG_PRIMARY_HEATER == heater ) { inletTemperature = getTemperatureValue( (U32)TEMPSENSORS_HEAT_DISINFECT ); } else { // TODO figure out the trimmer strategy } slope = slope / (F32)TEMPERATURES_MOVING_AVG_SIZE; measuredFlow = getMeasuredROFlowRate(); dutyCycle = calculateHeaterDutyCycle( targetTemperature, inletTemperature, measuredFlow ); deltaDutyCycle = ( feedbackTemperature <= targetTemperature ? HEATERS_MAX_DUTY_CYCLE - dutyCycle : dutyCycle ); // TODO add #defines for all these constants revDeltaDutyCycle = ( deltaDutyCycle - 0.0 < NEARLY_ZERO ? 0 : 1.0 / deltaDutyCycle ); revDeltaDutyCycle = ( revDeltaDutyCycle * 0.1 < 1.0 ? revDeltaDutyCycle * 0.1 : 1.0 ); deltaTemperature = ( ( atanf( slope ) * 1.27 ) / measuredFlow ) + revDeltaDutyCycle; if ( fabs( targetTemperature - feedbackTemperature ) < deltaTemperature ) { if ( TRUE == didTimeout( heaterStatus[ heater ].rampStateStartTime, HEATERS_MIN_RAMP_TIME_MS ) ) { isItHandOffTime = TRUE; } } else if ( feedbackTemperature > targetTemperature ) { isItHandOffTime = TRUE; } if ( TRUE == isItHandOffTime ) { setHeaterDutyCycle( heater, dutyCycle ); resetPIController( heaterStatus[ heater ].controllerID, dutyCycle ); // TODO remove? heaterStatus[ heater ].initialDutyCycle = dutyCycle; state = HEATER_EXEC_STATE_CONTROL_TO_TARGET; } heaterStatus[ heater ].controlTimerCounter = 0; } // Check if the heater is requested to be off if ( FALSE == heaterStatus[ heater ].isHeaterOn ) { setHeaterDutyCycle( heater, HEATERS_MIN_DUTY_CYCLE ); state = HEATER_EXEC_STATE_NOT_RUNNING; } return state; } /*********************************************************************//** * @brief * The handleHeaterStateControlToTarget function handles the heaters' control * to target while they have reached to target. * @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 handleHeaterStateControlToTarget( DG_HEATERS_T heater ) { HEATERS_STATE_T state = HEATER_EXEC_STATE_CONTROL_TO_TARGET; F32 targetFlow = getTargetROPumpFlowRate(); BOOL hasFlowChanged = ( fabs( targetFlow - heaterStatus[ heater ].targetROFlow ) > NEARLY_ZERO ? TRUE : FALSE ); if ( TRUE == hasFlowChanged ) { // Moving back from control to target to ramp. heaterStatus[ heater ].rampStateStartTime = getMSTimerCount(); heaterStatus[ heater ].targetROFlow = targetFlow; // Go back to ramp state with 100% duty cycle setHeaterDutyCycle( heater, HEATERS_MAX_DUTY_CYCLE ); state = HEATER_EXEC_STATE_RAMP_TO_TARGET; } else if ( ++heaterStatus[ heater ].controlTimerCounter > HEATERS_CONTROL_STATE_CHECK_INTERVAL_COUNT ) { F32 feedbackTemperature = getTemperatureValue( (U32)heaterStatus[ heater ].feedbackSensor ); F32 targetTemperature = heaterStatus[ heater ].targetTemp; F32 dutyCycle = heaterStatus[ heater ].dutycycle; dutyCycle += ( targetTemperature - feedbackTemperature ) * PRIMARY_HEATER_P_COEFFICIENT * getMeasuredROFlowRate(); F32 deltaDutyCycle = dutyCycle - heaterStatus[ heater ].initialDutyCycle; if ( ( fabs( deltaDutyCycle ) > 0.09 ) && ( deltaDutyCycle < 0.0 ) ) { dutyCycle = heaterStatus[ heater ].initialDutyCycle - 0.09; } else if ( deltaDutyCycle > 0.09 ) { dutyCycle = heaterStatus[ heater ].initialDutyCycle + 0.09; } dutyCycle = ( dutyCycle > HEATERS_MAX_DUTY_CYCLE ? HEATERS_MAX_DUTY_CYCLE : dutyCycle ); dutyCycle = ( dutyCycle < HEATERS_MIN_DUTY_CYCLE ? HEATERS_MIN_DUTY_CYCLE : dutyCycle ); setHeaterDutyCycle( heater, dutyCycle ); heaterStatus[ heater ].controlTimerCounter = 0; } // Check if the heater is requested to be off if ( FALSE == heaterStatus[ heater ].isHeaterOn ) { setHeaterDutyCycle( heater, HEATERS_MIN_DUTY_CYCLE ); state = HEATER_EXEC_STATE_NOT_RUNNING; } 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 ); } heaterStatus[ heater ].dutycycle = pwm; } /*********************************************************************//** * @brief * The calculateHeaterDutyCycle function calculates a heater's duty cycle * by providing the * @details Inputs: none * @details Outputs: primaryHeaterTargetTemperature * @param targetTemp target temperature for the primary heater * @return none *************************************************************************/ static F32 calculateHeaterDutyCycle( F32 targetTemperature, F32 currentTemperature, F32 targetFlow ) { // The power is proportional to square root of deltaT x flow F32 power = sqrt( fabs( targetTemperature - currentTemperature ) * targetFlow ); // PWM = ( power * A - B ) F32 dutyCycle = ( ( power * PRIMARY_HEATERS_THERMAL_POWER_TO_VOLTAGE_SLOPE ) - PRIMARY_HEATERS_THERMAL_POWER_TO_VOLTAGE_INTERCEPT ); // 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 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 = heaterStatus[ 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 = heaterStatus[ DG_PRIMARY_HEATER ].dutycycle * 100.0; data.trimmerHeaterDC = heaterStatus[ DG_TRIMMER_HEATER ].dutycycle * 100.0; data.primaryTargetTemp = heaterStatus[ DG_PRIMARY_HEATER ].targetTemp; data.trimmerTargetTemp = heaterStatus[ DG_TRIMMER_HEATER ].targetTemp; data.primaryHeaterState = heaterStatus[ DG_PRIMARY_HEATER ].state; data.trimmerHeaterState = heaterStatus[ DG_TRIMMER_HEATER ].state; //broadcastHeatersData( &data ); 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: heatersVoltageMonitorTimeCounter * @details Outputs: heatersVoltageMonitorTimeCounter * @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 - heaterStatus[ DG_PRIMARY_HEATER ].dutycycle; F32 smallPri = 1.0 - heaterStatus[ DG_PRIMARY_HEATER ].dutycycle; F32 trimmer = 1.0 - heaterStatus[ 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; } /**@}*/