Index: firmware/App/Controllers/Heaters.c =================================================================== diff -u -rde2ef5ff6293f23a43414c5e2e40630468a68ddf -r54e58f64179ea382d2e2c403c8c3b9a15a612636 --- firmware/App/Controllers/Heaters.c (.../Heaters.c) (revision de2ef5ff6293f23a43414c5e2e40630468a68ddf) +++ firmware/App/Controllers/Heaters.c (.../Heaters.c) (revision 54e58f64179ea382d2e2c403c8c3b9a15a612636) @@ -15,7 +15,7 @@ * ***************************************************************************/ -#include +#include // Used for converting slope to radians and square root // TI PWM driver #include "etpwm.h" @@ -29,6 +29,7 @@ #include "ROPump.h" #include "SafetyShutdown.h" #include "SystemCommMessages.h" +#include "TaskGeneral.h" #include "TaskPriority.h" #include "TemperatureSensors.h" #include "Timers.h" @@ -40,29 +41,22 @@ // ********** private definitions ********** -#define MAIN_PRIMARY_HEATER_MAX_DUTY_CYCLE 1.00 ///< Main primary heater (heater A) max duty cycle (100%). -#define SMALL_PRIMAY_HEATER_MAX_DUTY_CYCLE 1.00 ///< Small Primary heater (heater B) max duty cycle (100%). -#define TRIMMER_HEATER_MAX_DUTY_CYCLE 1.00 ///< Trimmer heater max duty cycle (100%). +#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_HEATERS_CUMULATIVE_DUTY_CYCLE ( MAIN_PRIMARY_HEATER_MAX_DUTY_CYCLE + \ - SMALL_PRIMAY_HEATER_MAX_DUTY_CYCLE ) ///< Primary heaters cumulative duty cycle. -#define PRIMARY_HEATER_INITIAL_DUTY_CYCLE_ESTIMATE_DIVISOR 2.0 ///< Primary heaters initial duty cycle estimation divisor. -#define MAIN_AND_SMALL_PRIMARY_HEATER_DUTY_CYCLE_DIVISOR 2.0 ///< Main and small primary heater duty cycle divisor -#define PRIMARY_HEATERS_P_COEFFICIENT 0.15 ///< Primary heaters proportional coefficient. -#define PRIMARY_HEATERS_I_COEFFICIENT 0.001 ///< Primary heaters integral coefficient. +#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 ///< Trimmer heater proportional coefficient. -#define TRIMMER_HEATER_I_COEFFICIENT 0.001 ///< Trimmer heater 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 CONTROLLER_CHECK_INTERVAL_COUNT 10U ///< Time interval count to check the PI controller. -#define TEMP_SENSORS_INTERVAL_COUNT 10U ///< Temperature sensors interval count. - #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. @@ -71,221 +65,231 @@ #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. -/// Primary heaters exec states -typedef enum primary_heaters_exec_states -{ - PRIMARY_HEATERS_EXEC_STATE_OFF = 0, ///< Primary heaters exec state off - PRIMARY_HEATERS_EXEC_STATE_CONTROL_TO_TARGET, ///< Primary heaters exec state control to target (PI controller state) - NUM_OF_PRIMARY_HEATERS_EXEC_STATES ///< Number of primary heaters exec states -} PRIMARY_HEATERS_EXEC_STATES_T; +#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. -/// Trimmer heater exec states -typedef enum trimmer_heater_exec_states +typedef enum Heaters_Exec_States { - TRIMMER_HEATER_EXEC_STATE_OFF = 0, ///< Trimmer heater exec state off - TRIMMER_HEATER_EXEC_STATE_CONTROL_TO_TARGET, ///< Trimmer heater exec state control to target (PI controller state) - NUM_OF_TRIMMER_HEATER_EXEC_STATES ///< Number of trimmer heater exec states -} TRIMMER_HEATER_EXEC_STATES_T; + 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; -/// Name of the heaters states -typedef enum name_of_heaters +/// Heaters data structure +typedef struct { - PRIMARY_HEATER = 0, ///< Primary heater - TRIMMER_HEATER, ///< Trimmer heater - NUM_OF_HEATERS ///< Number of heaters -} NAME_OF_HEATER_T; + 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; -// ********** private data ********** - -static PRIMARY_HEATERS_EXEC_STATES_T primaryHeatersExecState; ///< Primary heaters exec state. -static TRIMMER_HEATER_EXEC_STATES_T trimmerHeaterExecState; ///< Trimmer heater exec state. - -static F32 primaryHeaterTargetTemperature; ///< Primary heaters target temperature. -static F32 trimmerHeaterTargetTemperature; ///< Trimmer heater target temperature. - -static F32 mainPrimaryHeaterDutyCycle; ///< Main primary heater duty cycle. -static F32 smallPrimaryHeaterDutyCycle; ///< Small primary heater duty cycle. -static F32 trimmerHeaterDutyCycle; ///< Trimmer heater duty cycle. -static U32 primaryHeaterTimerCounter; ///< Primary heater timer counter. -static U32 trimmerHeaterTimerCounter; ///< Trimmer heater timer counter. +static HEATER_STATUS_T heaterStatus[ NUM_OF_DG_HEATERS ]; ///< Heaters status. static U32 dataPublicationTimerCounter; ///< Data publication timer counter. -static BOOL isPrimaryHeaterOn; ///< Flag to show if the primary heater is on. -static BOOL isTrimmerHeaterOn; ///< Flag to show if the trimmer heater is on. - static OVERRIDE_U32_T heatersDataPublishInterval = { HEATERS_DATA_PUBLISH_INTERVAL, HEATERS_DATA_PUBLISH_INTERVAL, 0, 0 }; ///< Heaters data publish time interval. -static BOOL hasStartPrimaryHeaterRequested; ///< Start primary heater request flag. -static BOOL hasStartTrimmerHeaterRequested; ///< Start trimmer heater request flag. -static U32 heatersOnWithNoFlowTimer; ///< Heaters are on but there is no sufficient flow. -static TEMPERATURE_SENSORS_T primaryHeatersFeedbackTempSensor = TEMPSENSORS_OUTLET_PRIMARY_HEATER; ///< Primary heaters feedback temperature sensors. -static TEMPERATURE_SENSORS_T trimmerHeaterFeedbackTempSensor = TEMPSENSORS_INLET_DIALYSATE; ///< Trimmer heater feedback temperature sensors. -static U32 primaryHeaterTempOutTimer = 0; ///< Primary heaters temperature out of range start timer. -static U32 trimmerHeaterTempOutTimer = 0; ///< Trimmer heater internal temperature out of range timer. -static BOOL isPrimaryHeaterTempOutOfRange = FALSE; ///< Boolean flag to indicate if the primary heaters internal temperature out of range. -static BOOL isTrimmerHeaterTempOutOfRange = FALSE; ///< Boolean flag to indicate if the trimmer heater internal temperature out of range. -static BOOL isFlowBelowMin = FALSE; ///< Boolean flag to indicate if the flow is below the minimum. -static U32 heatersVoltageMonitorTimeCounter = 0; ///< Heaters voltage monitor counter. +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 PRIMARY_HEATERS_EXEC_STATES_T handlePrimaryHeaterStateOff( void ); -static PRIMARY_HEATERS_EXEC_STATES_T handlePrimaryHeaterStateControlToTarget( void ); +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 TRIMMER_HEATER_EXEC_STATES_T handleTrimmerHeaterStateOff( void ); -static TRIMMER_HEATER_EXEC_STATES_T handleTrimmerHeaterControlToTarget( void ); +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 resetHeaterState( NAME_OF_HEATER_T heater ); static void publishHeatersData( void ); static void checkPrimaryHeaterTempSensors( void ); static void checkTrimmerHeaterTempSensors( void ); static void monitorHeatersVoltage( void ); /*********************************************************************//** * @brief - * The initHeaters function initializes the variables and the PI controllers - * for the primary and trimmer heaters. + * The initHeaters initializes the heaters driver. * @details Inputs: none - * @details Outputs: Heaters module initialized + * @details Outputs: isFlowBelowMin, voltageMonitorTimeCounter, heaterStatus * @return none *************************************************************************/ void initHeaters( void ) { - primaryHeatersExecState = PRIMARY_HEATERS_EXEC_STATE_OFF; - trimmerHeaterExecState = TRIMMER_HEATER_EXEC_STATE_OFF; - primaryHeaterTargetTemperature = 0.0; - trimmerHeaterTargetTemperature = 0.0; - primaryHeaterTimerCounter = 0; - trimmerHeaterTimerCounter = 5; - dataPublicationTimerCounter = 0; - isPrimaryHeaterOn = FALSE; - isTrimmerHeaterOn = FALSE; - primaryHeatersFeedbackTempSensor = TEMPSENSORS_OUTLET_PRIMARY_HEATER; - trimmerHeaterFeedbackTempSensor = TEMPSENSORS_INLET_DIALYSATE; - primaryHeaterTempOutTimer = 0; - trimmerHeaterTempOutTimer = 0; - isPrimaryHeaterTempOutOfRange = FALSE; - isTrimmerHeaterTempOutOfRange = FALSE; - isFlowBelowMin = FALSE; - heatersVoltageMonitorTimeCounter = 0; + 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_HEATERS_P_COEFFICIENT, PRIMARY_HEATERS_I_COEFFICIENT, - HEATERS_MIN_DUTY_CYCLE, PRIMARY_HEATERS_CUMULATIVE_DUTY_CYCLE ); + 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, TRIMMER_HEATER_MAX_DUTY_CYCLE ); + 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 setPrimaryHeaterTargetTemperature function sets the primary heater - * target temperature. + * The setHeaterTargetTemperature function sets the target temperature of a heater. * @details Inputs: none - * @details Outputs: primaryHeaterTargetTemperature - * @param targetTemp target temperature for the primary heater + * @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 setPrimaryHeaterTargetTemperature( F32 targetTemp ) +void setHeaterTargetTemperature( DG_HEATERS_T heater, F32 targetTemperature ) { - primaryHeaterTargetTemperature = targetTemp; + 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 setTrimmerHeaterTargetTemperature function sets the trimmer heater - * target temperature. + * The getHeaterTargetTemperature function returns the heater target temperature. * @details Inputs: none - * @details Outputs: trimmerHeaterTargetTemperature - * @param targetTemp target temperature for the trimmer heater - * @return none + * @details Outputs: heaterStatus + * @return heater target temperature *************************************************************************/ -void setTrimmerHeaterTargetTemperature( F32 targetTemp ) +F32 getHeaterTargetTemperature( DG_HEATERS_T heater ) { - trimmerHeaterTargetTemperature = targetTemp; + return heaterStatus[ heater ].targetTemp; } /*********************************************************************//** * @brief - * The getPrimaryHeaterTargetTemperature function return the primary heater - * target temperature. - * @details Inputs: primaryHeaterTargetTemperature - * @details Outputs: none - * @return the current primary heater target temperature - *************************************************************************/ -F32 getPrimaryHeaterTargetTemperature( void ) -{ - return primaryHeaterTargetTemperature; -} - -/*********************************************************************//** - * @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 startPrimaryHeater( void ) +BOOL startHeater( DG_HEATERS_T heater ) { BOOL status = FALSE; - if ( ( primaryHeaterTargetTemperature >= MINIMUM_TARGET_TEMPERATURE ) && ( primaryHeaterTargetTemperature <= MAXIMUM_TARGET_TEMPERATURE ) ) + if( heater < NUM_OF_DG_HEATERS ) { - hasStartPrimaryHeaterRequested = TRUE; - status = TRUE; - } + F32 targetTemp = heaterStatus[ heater ].targetTemp; - return status; -} - -/*********************************************************************//** - * @brief - * The startTrimmerHeater function starts the trimmer heater. It resets the - * trimmer heater's state and sets the duty cycle of the trimmer heater. - * @details Inputs: trimmerHeaterTargetTemperature - * @details Outputs: hasStartTrimmerHeaterRequested - * @return status - *************************************************************************/ -BOOL startTrimmerHeater( void ) -{ - BOOL status = FALSE; - - if ( ( trimmerHeaterTargetTemperature >= MINIMUM_TARGET_TEMPERATURE ) && ( trimmerHeaterTargetTemperature <= MAXIMUM_TARGET_TEMPERATURE ) ) + if ( ( targetTemp >= MINIMUM_TARGET_TEMPERATURE ) && ( targetTemp <= MAXIMUM_TARGET_TEMPERATURE ) ) + { + status = TRUE; + heaterStatus[ heater ].startHeaterSignal = TRUE; + } + } + else { - hasStartTrimmerHeaterRequested = TRUE; - status = TRUE; + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_HEATERS_INVALID_HEATER_ID_SELECTED, heater ) } return status; } /*********************************************************************//** * @brief - * The stopPrimaryHeater function stops the primary heater. + * The stopHeater stops the specified heater. * @details Inputs: none - * @details Outputs: Primary heater stops - * @return none + * @details Outputs: heaterStatus + * @param heater: heater ID that is requested to turn on + * @return TRUE if the start was accepted otherwise, FALSE *************************************************************************/ -void stopPrimaryHeater( void ) +void stopHeater( DG_HEATERS_T heater ) { - isPrimaryHeaterOn = FALSE; + heaterStatus[ heater ].isHeaterOn = FALSE; } /*********************************************************************//** * @brief - * The stopTrimmerHeater function stops the trimmer heater. - * @details Inputs: none - * @details Outputs: Trimmer heater stops + * The execHeaters function executes the heaters state machine. + * @details Inputs: heaterStatus + * @details Outputs: heaterStatus * @return none *************************************************************************/ -void stopTrimmerHeater( void ) +void execHeaters( void ) { - isTrimmerHeaterOn = FALSE; + 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; + } + } } /*********************************************************************//** @@ -311,8 +315,8 @@ { cmdResponse.rejected = FALSE; #ifndef DISABLE_HEATERS_AND_TEMPS - trimmerHeaterTargetTemperature = heaterCmdPtr->targetTemp; - hasStartTrimmerHeaterRequested = TRUE; + heaterStatus[ DG_TRIMMER_HEATER ].targetTemp = heaterCmdPtr->targetTemp; + heaterStatus[ DG_TRIMMER_HEATER ].startHeaterSignal = TRUE; #endif } else @@ -323,7 +327,7 @@ else { cmdResponse.rejected = FALSE; - stopTrimmerHeater(); + stopHeater( DG_TRIMMER_HEATER ); } sendCommandResponseMsg( &cmdResponse ); @@ -360,7 +364,7 @@ * 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 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 ); @@ -391,237 +395,268 @@ isFlowBelowMin = FALSE; heatersOnWithNoFlowTimer = getMSTimerCount(); } - } + }*/ // Check for data publication publishHeatersData(); } /*********************************************************************//** * @brief - * The execPrimaryHeaters function executes the primary heaters' state machine. - * @details Inputs: primaryHeatersExecState - * @details Outputs: primaryHeatersExecState - * @return none + * 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 *************************************************************************/ -void execPrimaryHeaters( void ) +static HEATERS_STATE_T handleHeaterStateNotRunning( DG_HEATERS_T heater ) { - switch ( primaryHeatersExecState ) + HEATERS_STATE_T state = HEATER_EXEC_STATE_NOT_RUNNING; + + if ( TRUE == heaterStatus[ heater ].startHeaterSignal ) { - case PRIMARY_HEATERS_EXEC_STATE_OFF: - primaryHeatersExecState = handlePrimaryHeaterStateOff(); - break; + heaterStatus[ heater ].isHeaterOn = TRUE; + heaterStatus[ heater ].startHeaterSignal = FALSE; + heaterStatus[ heater ].targetROFlow = getTargetROPumpFlowRate(); - case PRIMARY_HEATERS_EXEC_STATE_CONTROL_TO_TARGET: - primaryHeatersExecState = handlePrimaryHeaterStateControlToTarget(); - break; + // 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; + } - default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_HEATERS_PRIMARY_HEATER_EXEC_INVALID_STATE, primaryHeatersExecState ); - primaryHeatersExecState = PRIMARY_HEATERS_EXEC_STATE_CONTROL_TO_TARGET; - break; - } -} + TEMPERATURE_SENSORS_T sensor = heaterStatus[ heater ].feedbackSensor; + F32 feedbackTemperature = getTemperatureValue( (U32)sensor ); + F32 targetTemperature = heaterStatus[ heater ].targetTemp; -/*********************************************************************//** - * @brief - * The execTrimmerHeater function executes the trimmer heater's state machine. - * @details Inputs: trimmerHeaterExecState - * @details Outputs: trimmerHeaterExecState - * @return none - *************************************************************************/ -void execTrimmerHeater( void ) -{ - switch ( trimmerHeaterExecState ) - { - case TRIMMER_HEATER_EXEC_STATE_OFF: - trimmerHeaterExecState = handleTrimmerHeaterStateOff(); - break; + 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 ); + } - case TRIMMER_HEATER_EXEC_STATE_CONTROL_TO_TARGET: - trimmerHeaterExecState = handleTrimmerHeaterControlToTarget(); - break; + // Once the heater is staring for the first time so no minimum ramp time is needed + heaterStatus[ heater ].rampStateStartTime = getMSTimerCount(); - default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_HEATERS_TRIMMER_HEATER_EXEC_INVALID_STATE, trimmerHeaterExecState ); - trimmerHeaterExecState = TRIMMER_HEATER_EXEC_STATE_CONTROL_TO_TARGET; - break; + // Turn on the heater + state = HEATER_EXEC_STATE_RAMP_TO_TARGET; } + + return state; } /*********************************************************************//** * @brief - * The handlePrimaryHeaterStateOff function handles the primary heaters at - * off state. - * @details Inputs: hasStartPrimaryHeaterRequested, isPrimaryHeaterOn - * @details Outputs: state (PRIMARY_HEATERS_EXEC_STATES_T), isPrimaryHeaterOn - * @return state (PRIMARY_HEATERS_EXEC_STATES_T) + * 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 PRIMARY_HEATERS_EXEC_STATES_T handlePrimaryHeaterStateOff( void ) +static HEATERS_STATE_T handleHeaterStateRampToTarget( DG_HEATERS_T heater ) { - PRIMARY_HEATERS_EXEC_STATES_T state = PRIMARY_HEATERS_EXEC_STATE_OFF; + HEATERS_STATE_T state = HEATER_EXEC_STATE_RAMP_TO_TARGET; - if ( TRUE == hasStartPrimaryHeaterRequested ) + 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 ) { - resetHeaterState( PRIMARY_HEATER ); - // Once the primary heaters duty cycle is set, it is divided into 2 - // so both heaters will start and both elements are heated up - setMainPrimaryHeaterPWM( mainPrimaryHeaterDutyCycle ); - setSmallPrimaryHeaterPWM( smallPrimaryHeaterDutyCycle ); - isPrimaryHeaterOn = TRUE; - hasStartPrimaryHeaterRequested = FALSE; + U32 i; + F32 slope; + F32 deltaTemperature; + F32 deltaDutyCycle; + F32 dutyCycle; + F32 revDeltaDutyCycle; + F32 inletTemperature; + F32 measuredFlow; - // Check if the operation mode is heat disinfect. If the mode is - // heat disinfect, the feedback sensor will be Thd - if ( DG_MODE_HEAT == getCurrentOperationMode() ) + // 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++ ) { -#ifdef THD_USING_TRO_CONNECTOR - // Set the feedback temperature sensor - // THd uses TRo in V3 - primaryHeatersFeedbackTempSensor = TEMPSENSORS_OUTLET_REDUNDANT; -#else - primaryHeatersFeedbackTempSensor = TEMPSENSORS_HEAT_DISINFECT; -#endif + slope += heaterStatus[ heater ].previousTemps[ i ]; } - state = PRIMARY_HEATERS_EXEC_STATE_CONTROL_TO_TARGET; + // 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 handlePrimaryHeaterStateControlToTarget function handles the primary - * heaters at control state when the heaters are active. - * @details Inputs: primaryHeaterTimerCounter, mainPrimaryHeaterDutyCycle, - * smallPrimaryHeaterDutyCycle, isPrimaryHeaterOn - * @details Outputs: primaryHeaterTimerCounter, mainPrimaryHeaterDutyCycle, - * smallPrimaryHeaterDutyCycle - * @return state (PRIMARY_HEATERS_EXEC_STATES_T) + * 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 PRIMARY_HEATERS_EXEC_STATES_T handlePrimaryHeaterStateControlToTarget( void ) +static HEATERS_STATE_T handleHeaterStateControlToTarget( DG_HEATERS_T heater ) { - PRIMARY_HEATERS_EXEC_STATES_T state = PRIMARY_HEATERS_EXEC_STATE_CONTROL_TO_TARGET; + HEATERS_STATE_T state = HEATER_EXEC_STATE_CONTROL_TO_TARGET; - if ( ++primaryHeaterTimerCounter >= CONTROLLER_CHECK_INTERVAL_COUNT ) + F32 targetFlow = getTargetROPumpFlowRate(); + BOOL hasFlowChanged = ( fabs( targetFlow - heaterStatus[ heater ].targetROFlow ) > NEARLY_ZERO ? TRUE : FALSE ); + + if ( TRUE == hasFlowChanged ) { - // Check if the flow is not below minimum required first - // If the flow is below minimum, send 0.00 to the PWM until the flow comes back to range - // If the flow is within range, run the PI controller to control the heaters normally - if ( FALSE == isFlowBelowMin ) - { - F32 outletTemp = getTemperatureValue( primaryHeatersFeedbackTempSensor ); - mainPrimaryHeaterDutyCycle = runPIController( PI_CONTROLLER_ID_PRIMARY_HEATER, primaryHeaterTargetTemperature, outletTemp ); + // Moving back from control to target to ramp. + heaterStatus[ heater ].rampStateStartTime = getMSTimerCount(); + heaterStatus[ heater ].targetROFlow = targetFlow; - // Once the primary heaters duty cycle is set, it is divided into 2 so both heaters will start and both elements are heated up - smallPrimaryHeaterDutyCycle = mainPrimaryHeaterDutyCycle / MAIN_AND_SMALL_PRIMARY_HEATER_DUTY_CYCLE_DIVISOR; - mainPrimaryHeaterDutyCycle = mainPrimaryHeaterDutyCycle / MAIN_AND_SMALL_PRIMARY_HEATER_DUTY_CYCLE_DIVISOR; + // 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; - setMainPrimaryHeaterPWM( mainPrimaryHeaterDutyCycle ); - setSmallPrimaryHeaterPWM( smallPrimaryHeaterDutyCycle ); + 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; } - // Flow is below the minimum required flow to run the primary heaters - else + else if ( deltaDutyCycle > 0.09 ) { - setMainPrimaryHeaterPWM( HEATERS_MIN_DUTY_CYCLE ); - setSmallPrimaryHeaterPWM( HEATERS_MIN_DUTY_CYCLE ); + dutyCycle = heaterStatus[ heater ].initialDutyCycle + 0.09; } - primaryHeaterTimerCounter = 0; - } + dutyCycle = ( dutyCycle > HEATERS_MAX_DUTY_CYCLE ? HEATERS_MAX_DUTY_CYCLE : dutyCycle ); + dutyCycle = ( dutyCycle < HEATERS_MIN_DUTY_CYCLE ? HEATERS_MIN_DUTY_CYCLE : dutyCycle ); - // If the primary heater is running and another start primary heater request - // is set, reset the primary heater again - if ( TRUE == hasStartPrimaryHeaterRequested ) - { - resetHeaterState( PRIMARY_HEATER ); - hasStartPrimaryHeaterRequested = FALSE; + setHeaterDutyCycle( heater, dutyCycle ); + + heaterStatus[ heater ].controlTimerCounter = 0; } - if ( FALSE == isPrimaryHeaterOn ) + // Check if the heater is requested to be off + if ( FALSE == heaterStatus[ heater ].isHeaterOn ) { - // Switch to off state. Set the duty cycles to 0 - mainPrimaryHeaterDutyCycle = HEATERS_MIN_DUTY_CYCLE; - smallPrimaryHeaterDutyCycle = HEATERS_MIN_DUTY_CYCLE; - setMainPrimaryHeaterPWM( HEATERS_MIN_DUTY_CYCLE ); - setSmallPrimaryHeaterPWM( HEATERS_MIN_DUTY_CYCLE ); - state = PRIMARY_HEATERS_EXEC_STATE_OFF; + setHeaterDutyCycle( heater, HEATERS_MIN_DUTY_CYCLE ); + state = HEATER_EXEC_STATE_NOT_RUNNING; } return state; } /*********************************************************************//** * @brief - * The handleTrimmerHeaterStateOff function handles the trimmer heater at - * off state. - * @details Inputs: hasStartTrimmerHeaterRequested, isTrimmerHeaterOn - * @details Outputs: state (TRIMMER_HEATER_EXEC_STATES_T), isTrimmerHeaterOn - * @return state (TRIMMER_HEATER_EXEC_STATES_T) + * 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 TRIMMER_HEATER_EXEC_STATES_T handleTrimmerHeaterStateOff( void ) +static void setHeaterDutyCycle( DG_HEATERS_T heater, F32 pwm ) { - TRIMMER_HEATER_EXEC_STATES_T state = TRIMMER_HEATER_EXEC_STATE_OFF; - - if ( TRUE == hasStartTrimmerHeaterRequested ) + if ( DG_PRIMARY_HEATER == heater ) { - resetHeaterState( TRIMMER_HEATER ); - isTrimmerHeaterOn = TRUE; - hasStartTrimmerHeaterRequested = FALSE; - setTrimmerHeaterPWM( trimmerHeaterDutyCycle ); - state = TRIMMER_HEATER_EXEC_STATE_CONTROL_TO_TARGET; + setMainPrimaryHeaterPWM( pwm ); + setSmallPrimaryHeaterPWM( pwm ); } + else if ( DG_TRIMMER_HEATER == heater ) + { + setTrimmerHeaterPWM( pwm ); + } - return state; + heaterStatus[ heater ].dutycycle = pwm; } /*********************************************************************//** * @brief - * The handleTrimmerHeaterControlToTarget function handles the trimmer - * heater at control state when the heater is active. - * @details Inputs: trimmerHeaterTimerCounter, trimmerHeaterDutyCycle - * @details Outputs: trimmerHeaterTimerCounter, trimmerHeaterDutyCycle, - * isTrimmerHeaterOn - * @return state (TRIMMER_HEATER_EXEC_STATES_T) + * 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 TRIMMER_HEATER_EXEC_STATES_T handleTrimmerHeaterControlToTarget( void ) +static F32 calculateHeaterDutyCycle( F32 targetTemperature, F32 currentTemperature, F32 targetFlow ) { - TRIMMER_HEATER_EXEC_STATES_T state = TRIMMER_HEATER_EXEC_STATE_CONTROL_TO_TARGET; + // The power is proportional to square root of deltaT x flow + F32 power = sqrt( fabs( targetTemperature - currentTemperature ) * targetFlow ); - if ( ++trimmerHeaterTimerCounter >= CONTROLLER_CHECK_INTERVAL_COUNT ) - { - // Check if the flow is not below minimum required first - // If the flow is below minimum, send 0.00 to the PWM until the flow comes back to range - // If the flow is within range, run the PI controller to control the heaters normally - if ( FALSE == isFlowBelowMin ) - { - F32 outletTemp = getTemperatureValue( trimmerHeaterFeedbackTempSensor ); - trimmerHeaterDutyCycle = runPIController( PI_CONTROLLER_ID_TRIMMER_HEATER, trimmerHeaterTargetTemperature, outletTemp ); - setTrimmerHeaterPWM( trimmerHeaterDutyCycle ); - } - else - { - setTrimmerHeaterPWM( HEATERS_MIN_DUTY_CYCLE ); - } + // PWM = ( power * A - B ) + F32 dutyCycle = ( ( power * PRIMARY_HEATERS_THERMAL_POWER_TO_VOLTAGE_SLOPE ) - PRIMARY_HEATERS_THERMAL_POWER_TO_VOLTAGE_INTERCEPT ); - trimmerHeaterTimerCounter = 0; - } + // 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 ); - if ( TRUE == hasStartTrimmerHeaterRequested ) - { - hasStartTrimmerHeaterRequested = FALSE; - } - - if ( FALSE == isTrimmerHeaterOn ) - { - // Set the duty cycle to 0 and switch to off state - trimmerHeaterDutyCycle = HEATERS_MIN_DUTY_CYCLE; - setTrimmerHeaterPWM( HEATERS_MIN_DUTY_CYCLE ); - state = TRIMMER_HEATER_EXEC_STATE_OFF; - } - - return state; + return dutyCycle; } /*********************************************************************//** @@ -665,51 +700,6 @@ /*********************************************************************//** * @brief - * The resetHeaterState function resets the PI controller of the selected heater. - * @details Inputs: mainPrimaryHeaterDutyCycle, trimmerHeaterDutyCycle - * @details Outputs: mainPrimaryHeaterDutyCycle, trimmerHeaterDutyCycle - * @param heater enumeration of the heater for which the PI controller will be reset - * @return none - *************************************************************************/ -static void resetHeaterState( NAME_OF_HEATER_T heater ) -{ - if ( PRIMARY_HEATER == heater ) - { - // Calculate the delta temperature from the inlet primary heater to target temperature - F32 inletPrimaryHeaterTemp = getTemperatureValue( TEMPSENSORS_INLET_PRIMARY_HEATER ); - F32 deltaTemp = primaryHeaterTargetTemperature - inletPrimaryHeaterTemp; - - // If the delta temperature (target temperature - inlet primary heater temperature) - // Is 0 or negative, the duty cycle is 0.0 - if ( deltaTemp <= 0.0 ) - { - mainPrimaryHeaterDutyCycle = HEATERS_MIN_DUTY_CYCLE; - smallPrimaryHeaterDutyCycle = HEATERS_MIN_DUTY_CYCLE; - } - else - { - // The formula for the initial guess is: (delta temperature * target flow) / 2.0 - F32 targetFlow = getTargetROPumpFlowRate(); - F32 duty = ( targetFlow * deltaTemp ) / PRIMARY_HEATER_INITIAL_DUTY_CYCLE_ESTIMATE_DIVISOR; - // If the duty cycle is greater 200% on the primary and small primary heaters, set it to 200%, otherwise set it to the - // estimated duty cycles - duty = ( duty > PRIMARY_HEATERS_CUMULATIVE_DUTY_CYCLE ? PRIMARY_HEATERS_CUMULATIVE_DUTY_CYCLE : duty ); - mainPrimaryHeaterDutyCycle = duty / MAIN_AND_SMALL_PRIMARY_HEATER_DUTY_CYCLE_DIVISOR; - smallPrimaryHeaterDutyCycle = duty / MAIN_AND_SMALL_PRIMARY_HEATER_DUTY_CYCLE_DIVISOR; - } - - // The PI controller of the primary heater consists of main and small primary heaters duty cycles - resetPIController( PI_CONTROLLER_ID_PRIMARY_HEATER, mainPrimaryHeaterDutyCycle + smallPrimaryHeaterDutyCycle ); - } - else if ( TRIMMER_HEATER == heater ) - { - trimmerHeaterDutyCycle = TRIMMER_HEATER_MAX_DUTY_CYCLE; - resetPIController( PI_CONTROLLER_ID_TRIMMER_HEATER, TRIMMER_HEATER_MAX_DUTY_CYCLE ); - } -} - -/*********************************************************************//** - * @brief * The publishTemperatureData function publishes the heaters data into * at the defined time interval. * @details Inputs: dataPublicationTimerCounter @@ -722,14 +712,20 @@ { HEATERS_DATA_T data; - data.mainPrimayHeaterDC = mainPrimaryHeaterDutyCycle * 100.0; - data.smallPrimaryHeaterDC = smallPrimaryHeaterDutyCycle * 100.0; - data.trimmerHeaterDC = trimmerHeaterDutyCycle * 100.0; - data.primaryTargetTemp = primaryHeaterTargetTemperature; - data.trimmerTargetTemp = trimmerHeaterTargetTemperature; + 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 ); + //broadcastHeatersData( &data ); + broadcastData( MSG_ID_DG_HEATERS_DATA, COMM_BUFFER_OUT_CAN_DG_BROADCAST, (U08*)&data, sizeof( HEATERS_DATA_T ) ); + dataPublicationTimerCounter = 0; } } @@ -760,8 +756,8 @@ 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 ) ) + /*/ If any of the temperatures are above the range + /if ( ( FALSE == isPrimaryHeaterTempOutOfRange ) && ( TRUE == isTempOut ) ) { stopPrimaryHeater(); isPrimaryHeaterTempOutOfRange = TRUE; @@ -773,7 +769,7 @@ { isPrimaryHeaterTempOutOfRange = FALSE; activateSafetyShutdown(); - } + }*/ } /*********************************************************************//** @@ -804,7 +800,7 @@ // If it is above the range for the first time, stop the trimmer heater // and set the variables - if ( ( FALSE == isTrimmerHeaterTempOutOfRange ) && ( TRUE == isTempOut ) ) + /*if ( ( FALSE == isTrimmerHeaterTempOutOfRange ) && ( TRUE == isTempOut ) ) { stopTrimmerHeater(); isTrimmerHeaterTempOutOfRange = TRUE; @@ -815,7 +811,7 @@ ( TRUE == didTimeout( trimmerHeaterTempOutTimer, HEATERS_MAX_ALLOWED_INTERNAL_TEMPERATURE_TIMEOUT_MS ) ) ) { activateSafetyShutdown(); - } + }*/ } /*********************************************************************//** @@ -827,7 +823,7 @@ *************************************************************************/ static void monitorHeatersVoltage( void ) { - if ( ++heatersVoltageMonitorTimeCounter >= HEATERS_VOLTAGE_MONITOR_TIME_INTERVAL ) + 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 @@ -837,9 +833,9 @@ F32 trimmerVoltage = getIntADCVoltageConverted( INT_ADC_TRIMMER_HEATER_24_VOLTS ); // Voltage to PWM is reverse. If PWM = 0 -> V = 24V - F32 mainPri = 1.0 - mainPrimaryHeaterDutyCycle; - F32 smallPri = 1.0 - smallPrimaryHeaterDutyCycle; - F32 trimmer = 1.0 - trimmerHeaterDutyCycle; + 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 @@ -858,7 +854,7 @@ SET_ALARM_WITH_1_F32_DATA( ALARM_ID_DG_TRIMMER_HEATER_VOLTAGE_OUT_OF_RANGE, trimmerVoltage ); } - heatersVoltageMonitorTimeCounter = 0; + voltageMonitorTimeCounter = 0; } }