Index: firmware/App/Controllers/Heaters.c =================================================================== diff -u -r28b5f2e7f757647145a82a39aca0a5f3652c68a1 -rd7be59e36db5e9899b02dd0bfadadc50fed934c0 --- firmware/App/Controllers/Heaters.c (.../Heaters.c) (revision 28b5f2e7f757647145a82a39aca0a5f3652c68a1) +++ firmware/App/Controllers/Heaters.c (.../Heaters.c) (revision d7be59e36db5e9899b02dd0bfadadc50fed934c0) @@ -1,33 +1,39 @@ /************************************************************************** * -* Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. +* Copyright (c) 2020-2022 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * -* @file Heaters.c +* @file Heaters.c * -* @author (last) Quang Nguyen -* @date (last) 14-Sep-2020 +* @author (last) Bill Bracken +* @date (last) 22-Aug-2022 * -* @author (original) Dara Navaei -* @date (original) 23-Apr-2020 +* @author (original) Dara Navaei +* @date (original) 23-Apr-2020 * ***************************************************************************/ -#include +#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 "NVDataMgmt.h" #include "OperationModes.h" -#include "PIControllers.h" +#include "PersistentAlarm.h" +#include "Reservoirs.h" #include "ROPump.h" #include "SafetyShutdown.h" #include "SystemCommMessages.h" +#include "TaskGeneral.h" #include "TaskPriority.h" #include "TemperatureSensors.h" #include "Timers.h" @@ -39,185 +45,169 @@ // ********** private definitions ********** -#define MAIN_PRIMARY_HEATER_MAX_DUTY_CYCLE 0.89 ///< Main primary heater (heater A) max duty cycle (89%). -#define SMALL_PRIMAY_HEATER_MAX_DUTY_CYCLE 0.50 ///< Small Primary heater (heater B) max duty cycle (50%). -#define TRIMMER_HEATER_MAX_DUTY_CYCLE 0.50 ///< Trimmer heater max duty cycle (50%). -#define HEATERS_MIN_DUTY_CYCLE 0.00 ///< Primary and trimmer heaters minimum duty cycle (0.00%). +#define HEATERS_MAX_DUTY_CYCLE 1.00F ///< Heaters max duty cycle (100%). +#define HEATERS_MIN_DUTY_CYCLE 0.00F ///< Heaters minimum duty cycle (0.00%). +#define HEATERS_MIN_HEAT_DISINFECT_DUTY_CYCLE 0.6F ///< Heaters minimum duty cycle during heat disinfect. -#define PRIMARY_HEATERS_P_COEFFICIENT 0.02 ///< Primary heaters proportional coefficient. -#define PRIMARY_HEATERS_I_COEFFICIENT 0.001 ///< Primary heaters integral coefficient. +#define HEATERS_DATA_PUBLISH_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Heaters data publish interval. -#define TRIMMER_HEATER_P_COEFFICIENT 0.02 ///< Trimmer heater proportional coefficient. -#define TRIMMER_HEATER_I_COEFFICIENT 0.001 ///< Trimmer heater integral coefficient. +#define MINIMUM_TARGET_TEMPERATURE 10.0F ///< Minimum allowed target temperature for the heaters. +#define MAXIMUM_TARGET_TEMPERATURE 90.0F ///< Maximum allowed target temperature for the heaters. -#define 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_ON_NO_FLOW_TIMEOUT_MS ( 1 * MS_PER_SECOND ) ///< Heaters on with no flow time out in milliseconds. +#define HEATERS_MAX_OPERATING_VOLTAGE_V 24.0F ///< Heaters max operating voltage in volts. +#define HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS ( 2 * MS_PER_SECOND ) ///< Heaters voltage out of range time out in milliseconds. +#define HEATERS_MAX_VOLTAGE_OUT_OF_RANGE_TOL 0.2F ///< Heaters max voltage out of range tolerance. +#define TRIMMER_HEATER_MAX_POWER_W 66.5F ///< Trimmer heater maximum power in Watts. +#define TRIMMER_HEATER_CONTROL_CHECK_INTERVAL_COUNT ( ( 30 * MS_PER_SECOND ) / TASK_GENERAL_INTERVAL ) ///< Trimmer heater control interval count. -#define HEATERS_DATA_PUBLISH_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Heaters data publish interval. +#define DELTA_TEMPERATURE_TIME_COSNTANT_C 8.6F ///< Delta temperature calculated from time constant. +#define PRIMARY_HEATER_DUTY_CYCLE_PER_TEMPERATURE_C 0.03F ///< Primary heaters duty cycle per temperature in C. +#define DATA_PUBLISH_COUNTER_START_COUNT 70 ///< Data publish counter start count. +#define MIN_RO_HEATER_FLOWRATE_LPM 0.2F ///< Minimum target RO heater flow rate in L/min. -#define SMALL_PRIMARY_AND_TRIMMER_HEATERS_POST_TARGET_TEMPERATURE 40U ///< Small primary and trimmer heaters target temperature during POST. -#define MAIN_PRIMARY_HEATER_POST_TARGET_TEMPERATURE 35U ///< Main primary heater target temperature during POST. -#define HEATERS_POST_HEAT_UP_TIME_SECONDS 50U ///< The time that the heaters are heated up to reach to the target temperature during POST. -#define HEATERS_POST_TEMPERATURE_TOLERANCE 1U ///< Tolerance of the sensors to the target temperature during POST. +static const F32 WATER_SPECIFIC_HEAT_DIVIDED_BY_MINUTES = 4184 / SEC_PER_MIN; ///< Water specific heat in J/KgC / 60. +static const F32 PRIMARY_HEATERS_MAXIMUM_POWER_WATTS = 475 + 237.5F; ///< Primary heaters maximum power (main primary = 475W and small primary = 237.5W). -#define MINIMUM_TARGET_TEMPERATURE 10U ///< Minimum allowed target temperature for the heaters. -#define MAXIMUM_TARGET_TEMPERATURE 90U ///< Maximum allowed target temperature for the heaters. - -#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 120.0 ///< Heaters max allowed internal temperature in degrees C. TODO figure out the max temperature value -#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. - -/// Heaters self-test enums -typedef enum heaters_self_test_states +/// Heaters exec states +typedef enum Heaters_Exec_States { - HEATERS_SELF_TEST_START = 0, ///< Heaters self-test start state - HEATERS_SELF_TEST_SMALL_PRIMARY_AND_TRIMMER_HEATERS, ///< Heaters self-test small primary and trimmer heaters state - HEATERS_SELF_TEST_MAIN_PRIMARY_HEATER, ///< Heaters self-test start main primary state - HEATERS_SELF_TEST_COMPLETE, ///< Heaters self-test complete state - NUM_OF_HEATERS_SELF_TEST_STATES ///< Number of heaters self-test states -} HEATERS_SELF_TEST_STATES_T ; + 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; -/// Primary heaters exec states -typedef enum primary_heaters_exec_states +/// Heaters data structure +typedef struct { - 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; + F32 targetTemp; ///< Heater target temperature. + HEATERS_STATE_T state; ///< Heater state. + BOOL startHeaterSignal; ///< Heater start indication flag. + BOOL isHeaterOn; ///< Heater on/off status flag. + F32 dutycycle; ///< Heater duty cycle. + F32 targetFlow; ///< Heater target flow. + BOOL hasTargetTempChanged; ///< Heater target temperature change flag indicator. + F32 heaterEfficiency; ///< Heater efficiency during the run. + BOOL hasTargetBeenReached; ///< Heater flag to indicate whether the target temperature has been reached. + F32 calculatedTemperature; ///< Heater calculated temperature. + DG_RESERVOIR_ID_T inactiveRsrvr; ///< Heater inactive reservoir. +} HEATER_STATUS_T; -/// Trimmer heater exec states -typedef enum trimmer_heater_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; +static HEATER_STATUS_T heatersStatus[ NUM_OF_DG_HEATERS ]; ///< Heaters status. +static U32 dataPublicationTimerCounter; ///< Data publication timer counter. +static U32 trimmerHeaterControlCounter; ///< Trimmer heater control counter. +static OVERRIDE_U32_T heatersDataPublishInterval = { HEATERS_DATA_PUBLISH_INTERVAL, HEATERS_DATA_PUBLISH_INTERVAL, 0, 0 }; ///< Heaters data publish time interval. -/// Name of the heaters states -typedef enum name_of_heaters -{ - PRIMARY_HEATER = 0, ///< Primary heater - TRIMMER_HEATER, ///< Trimmer heater - NUM_OF_HEATERS ///< Number of heaters -} NAME_OF_HEATER_T; - -// ********** private data ********** - -static SELF_TEST_STATUS_T heatersSelfTestResult; ///< Heaters self-test results. -static HEATERS_SELF_TEST_STATES_T heatersSelfTestState; ///< Heaters self-test state. -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 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 U32 selfTestElapsedTime; ///< Self-test elapsed time variable. -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_OUTLET_REDUNDANT; ///< Trimmer heater feedback temperature sensors. -static U32 primaryHeatersInternalTempOutTimer = 0; ///< Primary heaters internal temperature out of range timer. -static U32 trimmerHeaterInternalTempOutTimer = 0; ///< Trimmer heater internal temperature out of range timer. -static BOOL isPrimaryHeatersTempOutOfRange = 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. - // ********** private function prototypes ********** -static HEATERS_SELF_TEST_STATES_T handleHeatersSelfTestStart( void ); -static HEATERS_SELF_TEST_STATES_T handleHeatersSelfTestSmallPrimaryAndTrimmerHeaters( void ); -static HEATERS_SELF_TEST_STATES_T handleHeatersSelfTestMainPrimaryHeater( void ); +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 PRIMARY_HEATERS_EXEC_STATES_T handlePrimaryHeaterStateOff( void ); -static PRIMARY_HEATERS_EXEC_STATES_T handlePrimaryHeaterStateControlToTarget( void ); +static void setHeaterDutyCycle( DG_HEATERS_T heater, F32 pwm ); +static F32 calculatePrimaryHeaterDutyCycle( F32 targetTemperature, F32 currentTemperature, F32 flow, BOOL checkEfficiency ); +static F32 calculateTrimmerHeaterDutyCycle( F32 targetTemperature, F32 currentTemperature, F32 flow, BOOL checkEfficiency ); +static BOOL haveHeaterControlConditionsChanged( DG_HEATERS_T heater ); -static TRIMMER_HEATER_EXEC_STATES_T handleTrimmerHeaterStateOff( void ); -static TRIMMER_HEATER_EXEC_STATES_T handleTrimmerHeaterControlToTarget( void ); - 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 U32 getPublishHeatersDataInterval( 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: voltageMonitorTimeCounter, heaterStatus, + * hasTreatmentInternalTempBeenSet, dataPublicationTimerCounter, + * trimmerHeaterControlCounter * @return none *************************************************************************/ void initHeaters( void ) { - heatersSelfTestState = HEATERS_SELF_TEST_START; - 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; - selfTestElapsedTime = 0; - primaryHeatersFeedbackTempSensor = TEMPSENSORS_OUTLET_PRIMARY_HEATER; - trimmerHeaterFeedbackTempSensor = TEMPSENSORS_OUTLET_REDUNDANT; - primaryHeatersInternalTempOutTimer = 0; - trimmerHeaterInternalTempOutTimer = 0; - isPrimaryHeatersTempOutOfRange = FALSE; - isTrimmerHeaterTempOutOfRange = FALSE; - isFlowBelowMin = FALSE; + DG_HEATERS_T heater; + dataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; + trimmerHeaterControlCounter = 0; - // 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, MAIN_PRIMARY_HEATER_MAX_DUTY_CYCLE + SMALL_PRIMAY_HEATER_MAX_DUTY_CYCLE ); + for ( heater = DG_PRIMARY_HEATER; heater < NUM_OF_DG_HEATERS; heater++ ) + { + heatersStatus[ heater ].targetTemp = 0.0; + heatersStatus[ heater ].state = HEATER_EXEC_STATE_OFF; + heatersStatus[ heater ].startHeaterSignal = FALSE; + heatersStatus[ heater ].isHeaterOn = FALSE; + heatersStatus[ heater ].dutycycle = 0.0; + heatersStatus[ heater ].targetFlow = 0.0; + heatersStatus[ heater ].hasTargetTempChanged = FALSE; + heatersStatus[ heater ].heaterEfficiency = 1.0; // Assuming 100% efficiency during initialization until it is updated + heatersStatus[ heater ].hasTargetBeenReached = FALSE; + } - // 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 ); + // Initialize the persistent alarms + initPersistentAlarm( ALARM_ID_DG_MAIN_PRIMARY_HEATER_VOLTAGE_OUT_OF_RANGE, HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS, + HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS ); + + initPersistentAlarm( ALARM_ID_DG_SMALL_PRIMARY_HEATER_VOLTAGE_OUT_OF_RANGE, HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS, + HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS ); + + initPersistentAlarm( ALARM_ID_DG_TRIMMER_HEATER_VOLTAGE_OUT_OF_RANGE, HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS, + HEATERS_VOLTAGE_OUT_OF_RANGE_TIMEOUT_MS ); + + initPersistentAlarm( ALARM_ID_DG_FLOW_TOO_LOW_WHILE_HEATER_ON, HEATERS_ON_NO_FLOW_TIMEOUT_MS, HEATERS_ON_NO_FLOW_TIMEOUT_MS ); } /*********************************************************************//** * @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 - * @return none + * @details Outputs: heaterStatus + * @param heater: heater ID that its target temperature is set + * @param targetTemperature: target temperature of that the heater has to + * heat the fluid + * @return TRUE if the temperature was set otherwise, FALSE *************************************************************************/ -void setPrimaryHeaterTargetTemperature( F32 targetTemp ) +BOOL setHeaterTargetTemperature( DG_HEATERS_T heater, F32 targetTemperature ) { - primaryHeaterTargetTemperature = targetTemp; + BOOL result = FALSE; + + if( heater < NUM_OF_DG_HEATERS ) + { + // Assume the target temperature has not changed + heatersStatus[ heater ].hasTargetTempChanged = FALSE; + + // Check if the requested temperature is within the allowed range + if ( ( targetTemperature >= MINIMUM_TARGET_TEMPERATURE ) && ( targetTemperature <= MAXIMUM_TARGET_TEMPERATURE ) ) + { + heatersStatus[ heater ].targetTemp = targetTemperature; + heatersStatus[ heater ].hasTargetTempChanged = TRUE; + result = TRUE; + } + } + else + { + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_HEATERS_INVALID_HEATER_ID_SELECTED, heater ) + } + + return result; } /*********************************************************************//** * @brief - * The 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 heatersStatus[ heater ].targetTemp; } /*********************************************************************//** @@ -228,197 +218,136 @@ * @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 ) { -//#ifndef DISABLE_HEATERS_AND_TEMPS - hasStartPrimaryHeaterRequested = TRUE; -//#endif - status = TRUE; + if ( TRUE == heatersStatus[ heater ].hasTargetTempChanged ) + { + status = TRUE; + heatersStatus[ heater ].startHeaterSignal = TRUE; + } } - - 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 ) ) + else { -#ifndef DISABLE_HEATERS_AND_TEMPS - hasStartTrimmerHeaterRequested = TRUE; -#endif - 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 ) { - setMainPrimaryHeaterPWM( HEATERS_MIN_DUTY_CYCLE ); - setSmallPrimaryHeaterPWM( HEATERS_MIN_DUTY_CYCLE ); - isPrimaryHeaterOn = FALSE; + heatersStatus[ 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 ) { - setTrimmerHeaterPWM( HEATERS_MIN_DUTY_CYCLE ); - isTrimmerHeaterOn = FALSE; -} + DG_HEATERS_T heater; + HEATERS_STATE_T state; -/*********************************************************************//** - * @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 ) + for ( heater = DG_PRIMARY_HEATER; heater < NUM_OF_DG_HEATERS; heater++ ) { - if ( ( MINIMUM_TARGET_TEMPERATURE <= heaterCmdPtr->targetTemp ) && ( heaterCmdPtr->targetTemp <= MAXIMUM_TARGET_TEMPERATURE ) ) + state = heatersStatus[ heater ].state; + + switch( state ) { - cmdResponse.rejected = FALSE; - #ifndef DISABLE_HEATERS_AND_TEMPS - trimmerHeaterTargetTemperature = heaterCmdPtr->targetTemp; - hasStartTrimmerHeaterRequested = TRUE; - #endif + 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; } - else + + // Check if the heater is requested to be off + if ( FALSE == heatersStatus[ heater ].isHeaterOn ) { - cmdResponse.rejectCode = DG_CMD_REQUEST_REJECT_REASON_INVALID_PARAMETER; + setHeaterDutyCycle( heater, HEATERS_MIN_DUTY_CYCLE ); + heatersStatus[ heater ].state = HEATER_EXEC_STATE_OFF; } } - else - { - cmdResponse.rejected = FALSE; - stopTrimmerHeater(); - } - - sendCommandResponseMsg( &cmdResponse ); } /*********************************************************************//** * @brief - * The execHeatersMonitor function turns off the heaters when RO pump is - * not on. - * @details Inputs: none - * @details Outputs: Turns off the heaters when RO pump is not on + * 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 ) { -#ifndef IGNORE_HEATERS_MONITOR - F32 primaryHeatersInternalTemp = getTemperatureValue( TEMPSENSORS_PRIMARY_HEATER_INTERNAL ); - F32 trimmerHeaterInternalTemp = getTemperatureValue( TEMPSENSORS_TRIMMER_HEATER_INTERNAL ); + DG_HEATERS_T heater; - // Check if the primary heaters' internal temperature is above the limit - if ( primaryHeatersInternalTemp > HEATERS_MAX_ALLOWED_INTERNAL_TEMPERATURE_C ) + for ( heater = DG_PRIMARY_HEATER; heater < NUM_OF_DG_HEATERS; heater++ ) { - SET_ALARM_WITH_1_U32_DATA( ALARM_ID_DG_PRIMARY_HEATERS_INTERNAL_TEMP_OUT_OF_RANGE, primaryHeatersInternalTemp ); - - // If it is above the range for the first time, stop the primary heaters - // and set the variables - if ( FALSE == isPrimaryHeatersTempOutOfRange ) + // Check if the heater is on and if it is, check the flow sensor's status + if ( TRUE == heatersStatus[ heater ].isHeaterOn ) { - stopPrimaryHeater(); - isPrimaryHeatersTempOutOfRange = TRUE; - primaryHeatersInternalTempOutTimer = getMSTimerCount(); - } - // If the primary heaters internal temperature was out for more than the define period, activate the safety shutdown - else if ( TRUE == didTimeout( primaryHeatersInternalTempOutTimer, HEATERS_MAX_ALLOWED_INTERNAL_TEMPERATURE_TIMEOUT_MS ) ) - { - activateSafetyShutdown(); - } - } + // 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 ? getMeasuredROFlowRateLPM() : 50.0 ); + F32 minFlow = ( DG_PRIMARY_HEATER == heater ? MIN_RO_HEATER_FLOWRATE_LPM : MIN_RO_HEATER_FLOWRATE_LPM ); + BOOL isFlowLow = ( measFlow < minFlow ? TRUE : FALSE ); - // Check if the trimmer heater internal temperature is above the limit - if ( trimmerHeaterInternalTemp > HEATERS_MAX_ALLOWED_INTERNAL_TEMPERATURE_C ) - { - SET_ALARM_WITH_1_U32_DATA( ALARM_ID_DG_TRIMMER_HEATER_INTERNAL_TEMP_OUT_OF_RANGE, trimmerHeaterInternalTemp ); - - // If it is above the range for the first time, stop the trimmer heater - // and set the variables - if ( FALSE == isTrimmerHeaterTempOutOfRange ) - { - stopTrimmerHeater(); - isTrimmerHeaterTempOutOfRange = TRUE; - trimmerHeaterInternalTempOutTimer = getMSTimerCount(); + checkPersistentAlarm( ALARM_ID_DG_FLOW_TOO_LOW_WHILE_HEATER_ON, isFlowLow, measFlow, minFlow ); } - // If the trimmer heater internal temperature was out for more than the define period, activate the safety shutdown - else if ( TRUE == didTimeout( trimmerHeaterInternalTempOutTimer, HEATERS_MAX_ALLOWED_INTERNAL_TEMPERATURE_TIMEOUT_MS ) ) + else { - activateSafetyShutdown(); + checkPersistentAlarm( ALARM_ID_DG_FLOW_TOO_LOW_WHILE_HEATER_ON, FALSE, 0.0, 0.0 ); } } -#endif - /* - * If any of the heaters are on, 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 - */ - if ( ( TRUE == isPrimaryHeaterOn ) || ( TRUE == isTrimmerHeaterOn ) ) +#ifndef _RELEASE_ + if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_HEATERS_MONITOR ) != SW_CONFIG_ENABLE_VALUE ) +#endif { - F32 measuredFlow = getMeasuredROFlowRate(); - - if ( measuredFlow < MIN_RO_FLOWRATE_LPM ) - { - if ( FALSE == isFlowBelowMin ) - { - isFlowBelowMin = TRUE; - heatersOnWithNoFlowTimer = getMSTimerCount(); - } - else if ( TRUE == didTimeout( heatersOnWithNoFlowTimer, HEATERS_ON_NO_FLOW_TIMEOUT_MS ) ) - { - stopPrimaryHeater(); - stopTrimmerHeater(); - - SET_ALARM_WITH_1_F32_DATA( ALARM_ID_DG_HEATERS_ON_WITH_NO_FLOW_TIMEOUT, measuredFlow ); - } - } - else - { - isFlowBelowMin = FALSE; - heatersOnWithNoFlowTimer = getMSTimerCount(); - } + monitorHeatersVoltage(); } // Check for data publication @@ -427,362 +356,389 @@ /*********************************************************************//** * @brief - * The execHeatersSelfTest function executes the heaters' self-test state machine. - * @details Inputs: heatersSelfTestState - * @details Outputs: heatersSelfTestState - * @return heatersSelfTestState + * The resetHeatersEfficiency function resets the heaters efficiency upon + * the start of a treatment. + * @details Inputs: none + * @details Outputs: heaterStatus + * @return none *************************************************************************/ -SELF_TEST_STATUS_T execHeatersSelfTest( void ) +void resetHeatersEfficiency( void ) { - switch ( heatersSelfTestState ) - { - case HEATERS_SELF_TEST_START: - heatersSelfTestState = handleHeatersSelfTestStart(); - break; + heatersStatus[ DG_PRIMARY_HEATER ].heaterEfficiency = 1.0; + heatersStatus[ DG_TRIMMER_HEATER ].heaterEfficiency = 1.0; +} - case HEATERS_SELF_TEST_SMALL_PRIMARY_AND_TRIMMER_HEATERS: - heatersSelfTestState = handleHeatersSelfTestSmallPrimaryAndTrimmerHeaters(); - break; +/*********************************************************************//** + * @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; - case HEATERS_SELF_TEST_MAIN_PRIMARY_HEATER: - heatersSelfTestState = handleHeatersSelfTestMainPrimaryHeater(); - break; + if ( TRUE == heatersStatus[ heater ].startHeaterSignal ) + { + heatersStatus[ heater ].isHeaterOn = TRUE; + heatersStatus[ heater ].startHeaterSignal = FALSE; - case HEATERS_SELF_TEST_COMPLETE: - // POST is done. Do nothing - break; - - default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_HEATERS_SELF_TEST_INVALID_STATE, heatersSelfTestState ); - heatersSelfTestState = HEATERS_SELF_TEST_COMPLETE; - break; + // 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 heatersSelfTestResult; + return state; } /*********************************************************************//** * @brief - * The execPrimaryHeaters function executes the primary heaters' state machine. - * @details Inputs: primaryHeatersExecState - * @details Outputs: primaryHeatersExecState - * @return none + * 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 *************************************************************************/ -void execPrimaryHeaters( void ) +static HEATERS_STATE_T handleHeaterStatePrimaryRampToTarget( void ) { - switch ( primaryHeatersExecState ) + HEATERS_STATE_T state = HEATER_EXEC_STATE_PRIMARY_RAMP_TO_TARGET; + DG_HEATERS_T heater = DG_PRIMARY_HEATER; + F32 inletTemperature = getTemperatureValue( (U32)TEMPSENSORS_HEAT_DISINFECT ); + F32 targetFlow = 0.0; + F32 dutyCycle = 0.0; + F32 targetTemperature = heatersStatus[ heater ].targetTemp; + DG_OP_MODE_T opMode = getCurrentOperationMode(); + + if ( DG_MODE_FILL == opMode ) { - case PRIMARY_HEATERS_EXEC_STATE_OFF: - primaryHeatersExecState = handlePrimaryHeaterStateOff(); - break; + // If the previous average fill flow rate is 0, use the nominal target RO flow from the RO pump + targetFlow = ( getAvgFillFlowRateLPM() - 0.0 > NEARLY_ZERO ? getAvgFillFlowRateLPM() : getTargetROPumpFlowRateLPM() ); + dutyCycle = calculatePrimaryHeaterDutyCycle( targetTemperature, inletTemperature, targetFlow, TRUE ); + state = HEATER_EXEC_STATE_PRIMARY_CONTROL_TO_TARGET; + } + else if ( ( DG_MODE_GENE == opMode ) || ( DG_MODE_DRAI == opMode ) ) + { + targetTemperature += DELTA_TEMPERATURE_TIME_COSNTANT_C; + // Use target flow rate during Idle and drain + targetFlow = getTargetROPumpFlowRateLPM(); + dutyCycle = calculatePrimaryHeaterDutyCycle( targetTemperature, inletTemperature, targetFlow, FALSE ); + state = HEATER_EXEC_STATE_PRIMARY_CONTROL_TO_TARGET; + } + else if ( ( DG_MODE_HEAT == opMode ) || ( DG_MODE_CHEM == opMode ) ) + { + // If the mode is any of the disinfects, especially heat, use the target flow rate instead of the avg. flow + // Most of the times the heater should be running at 100% duty cycle since the target temperature is 81 C + targetFlow = getTargetROPumpFlowRateLPM(); + dutyCycle = calculatePrimaryHeaterDutyCycle( targetTemperature, inletTemperature, targetFlow, FALSE ); + state = HEATER_EXEC_STATE_CONTROL_TO_DISINFECT_TARGET; + } - case PRIMARY_HEATERS_EXEC_STATE_CONTROL_TO_TARGET: - primaryHeatersExecState = handlePrimaryHeaterStateControlToTarget(); - break; + // Update the calculated target temperature + heatersStatus[ DG_PRIMARY_HEATER ].calculatedTemperature = targetTemperature; + setHeaterDutyCycle( heater, dutyCycle ); - 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; - } + return state; } /*********************************************************************//** * @brief - * The execTrimmerHeater function executes the trimmer heater's state machine. - * @details Inputs: trimmerHeaterExecState - * @details Outputs: trimmerHeaterExecState - * @return none + * 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 *************************************************************************/ -void execTrimmerHeater( void ) +static HEATERS_STATE_T handleHeaterStatePrimaryControlToTarget( void ) { - switch ( trimmerHeaterExecState ) - { - case TRIMMER_HEATER_EXEC_STATE_OFF: - trimmerHeaterExecState = handleTrimmerHeaterStateOff(); - break; + HEATERS_STATE_T state = HEATER_EXEC_STATE_PRIMARY_CONTROL_TO_TARGET; + DG_HEATERS_T heater = DG_PRIMARY_HEATER; - case TRIMMER_HEATER_EXEC_STATE_CONTROL_TO_TARGET: - trimmerHeaterExecState = handleTrimmerHeaterControlToTarget(); - break; - - 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; + if ( TRUE == haveHeaterControlConditionsChanged( heater ) ) + { + state = HEATER_EXEC_STATE_PRIMARY_RAMP_TO_TARGET; } + + return state; } /*********************************************************************//** * @brief - * The handleHeatersSelfTestStart function starts the small primary and - * the trimmer heaters for self-test. - * @details Inputs: heatersSelfTestResult, selfTestElapsedTime - * @details Outputs: heatersSelfTestResult, selfTestElapsedTime - * @return state (HEATERS_SELF_TEST_STATES_T) + * 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_SELF_TEST_STATES_T handleHeatersSelfTestStart( void ) +static HEATERS_STATE_T handleHeaterStateControlToDisinfectTarget( DG_HEATERS_T heater ) { - // Code temporarily disabled since POST is not tested yet - // TODO once POST is tested and implemented, remove #ifndef -#ifndef _VECTORCAST_ - heatersSelfTestResult = SELF_TEST_STATUS_PASSED; - return HEATERS_SELF_TEST_COMPLETE; -#else - heatersSelfTestResult = SELF_TEST_STATUS_IN_PROGRESS; - setSmallPrimaryHeaterPWM( SMALL_PRIMAY_HEATER_MAX_DUTY_CYCLE ); - setTrimmerHeaterPWM( TRIMMER_HEATER_MAX_DUTY_CYCLE ); - selfTestElapsedTime = getMSTimerCount(); + HEATERS_STATE_T state = HEATER_EXEC_STATE_CONTROL_TO_DISINFECT_TARGET; - return HEATERS_SELF_TEST_SMALL_PRIMARY_AND_TRIMMER_HEATERS; -#endif + 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 handleHeatersSelfTestSmallPrimaryAndTrimmerHeaters function checks if - * the time on the small primary and trimmer heaters has elapsed. If time has - * elapsed, it checks the thermocouple temperature shared among the two - * heaters. If they are in range, it sets the main primary heater and transitions - * to the next state. If it fails, it sets an alarm and fails the test. - * @details Inputs: heatersSelfTestResult, selfTestElapsedTime - * @details Outputs: heatersSelfTestResult, selfTestElapsedTime - * @return state (HEATERS_SELF_TEST_STATES_T) + * 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_SELF_TEST_STATES_T handleHeatersSelfTestSmallPrimaryAndTrimmerHeaters( void ) +static HEATERS_STATE_T handleHeaterStateTrimmerRampToTarget( void ) { - HEATERS_SELF_TEST_STATES_T state = HEATERS_SELF_TEST_SMALL_PRIMARY_AND_TRIMMER_HEATERS; + HEATERS_STATE_T state = HEATER_EXEC_STATE_TRIMMER_RAMP_TO_TARGET; + DG_HEATERS_T heater = DG_TRIMMER_HEATER; + F32 currentTemperature = 0.0; + F32 targetFlowLPM = getTargetDialysateFlowLPM(); + F32 dutyCycle = 0.0; + F32 targetTemperature = heatersStatus[ heater ].targetTemp; + DG_OP_MODE_T opMode = getCurrentOperationMode(); - if ( didTimeout( selfTestElapsedTime, HEATERS_POST_HEAT_UP_TIME_SECONDS ) ) + if ( ( DG_MODE_FILL == opMode ) || ( DG_MODE_GENE == opMode ) || ( DG_MODE_DRAI == opMode ) ) { - setSmallPrimaryHeaterPWM( 0 ); - setTrimmerHeaterPWM( 0 ); - - F32 convertedTemperature = getTemperatureValue( TEMPSENSORS_TRIMMER_HEATER_INTERNAL ); - - if ( fabs( convertedTemperature - SMALL_PRIMARY_AND_TRIMMER_HEATERS_POST_TARGET_TEMPERATURE ) > HEATERS_POST_TEMPERATURE_TOLERANCE ) - { - //TODO alarm - // TODO POST failed - heatersSelfTestResult = SELF_TEST_STATUS_FAILED; - state = HEATERS_SELF_TEST_COMPLETE; - } - else - { - setMainPrimaryHeaterPWM( MAIN_PRIMARY_HEATER_MAX_DUTY_CYCLE ); - selfTestElapsedTime = getMSTimerCount(); - state = HEATERS_SELF_TEST_MAIN_PRIMARY_HEATER; - } + currentTemperature = getTrimmerHeaterTargetTemperature(); + dutyCycle = calculateTrimmerHeaterDutyCycle( targetTemperature, currentTemperature, targetFlowLPM, TRUE ); + state = HEATER_EXEC_STATE_TRIMMER_CONTROL_TO_TARGET; } + else if ( DG_MODE_HEAT == opMode ) + { + // If the mode is heat disinfect, use the target flow rate instead of the avg. flow + // Most of the times the heater should be running at 100% duty cycle since the target temperature is 81 C and + // it is far from the inlet temperature. + currentTemperature = getTemperatureValue( (U32)TEMPSENSORS_HEAT_DISINFECT ); + targetFlowLPM = getTargetROPumpFlowRateLPM(); + dutyCycle = calculateTrimmerHeaterDutyCycle( targetTemperature, currentTemperature, targetFlowLPM, FALSE ); + state = HEATER_EXEC_STATE_CONTROL_TO_DISINFECT_TARGET; + } + // Update the calculated target temperature + heatersStatus[ heater ].calculatedTemperature = currentTemperature; + heatersStatus[ heater ].inactiveRsrvr = getInactiveReservoir(); + heatersStatus[ heater ].targetFlow = targetFlowLPM; + trimmerHeaterControlCounter = 0; + setHeaterDutyCycle( heater, dutyCycle ); + return state; } /*********************************************************************//** * @brief - * The handleHeatersSelfTestMainPrimaryHeater function checks if the time - * on the main primary heater has elapsed. When the time has elapsed, it - * checks to ensure the thermocouple temperature is within the tolerance - * of the target temperature. It then transitions to the complete state. - * @details Inputs: heatersSelfTestResult - * @details Outputs: heatersSelfTestResult - * @return state (HEATERS_SELF_TEST_STATES_T) + * The handleHeaterStateTrimmerControlToTarget function handles the trimmer + * heater's control to target state. + * @details Inputs: heaterStatus, trimmerHeaterControlCounter + * @details Outputs: heaterStatus, trimmerHeaterControlCounter + * @return next state of the state machine *************************************************************************/ -static HEATERS_SELF_TEST_STATES_T handleHeatersSelfTestMainPrimaryHeater( void ) +static HEATERS_STATE_T handleHeaterStateTrimmerControlToTarget( void ) { - HEATERS_SELF_TEST_STATES_T state = HEATERS_SELF_TEST_MAIN_PRIMARY_HEATER; + HEATERS_STATE_T state = HEATER_EXEC_STATE_TRIMMER_CONTROL_TO_TARGET; - if ( didTimeout( selfTestElapsedTime, HEATERS_POST_HEAT_UP_TIME_SECONDS ) ) + // If the inactive reservoir has changed from the last run transition to ramp state to recalculate the + // duty cycle for the next delivery + if ( heatersStatus[ DG_TRIMMER_HEATER ].inactiveRsrvr != getInactiveReservoir() ) { - setMainPrimaryHeaterPWM( 0 ); + state = HEATER_EXEC_STATE_TRIMMER_RAMP_TO_TARGET; + } + else if ( ++trimmerHeaterControlCounter > TRIMMER_HEATER_CONTROL_CHECK_INTERVAL_COUNT ) + { + // When the trimmer heater is on, its duty cycle is adjusted at the control interval. For this control check, + // dialysate inlet temperature sensor is used rather than the theoretical calculations. + F32 outletRedundantTemperature = getTemperatureValue( TEMPSENSORS_INLET_DIALYSATE ); // TODO Change to TRo in DVT + F32 targetTemperature = heatersStatus[ DG_TRIMMER_HEATER ].targetTemp; + F32 targetFlowLPM = heatersStatus[ DG_TRIMMER_HEATER ].targetFlow; + F32 dutyCycle = calculateTrimmerHeaterDutyCycle( targetTemperature, outletRedundantTemperature, targetFlowLPM, TRUE ); - F32 convertedTemperature = getTemperatureValue( TEMPSENSORS_PRIMARY_HEATER_INTERNAL ); + trimmerHeaterControlCounter = 0; - if ( fabs( convertedTemperature - MAIN_PRIMARY_HEATER_POST_TARGET_TEMPERATURE ) > HEATERS_POST_TEMPERATURE_TOLERANCE ) - { - //TODO alarm - // TODO POST failed - heatersSelfTestResult = SELF_TEST_STATUS_FAILED; - } - else - { - heatersSelfTestResult = SELF_TEST_STATUS_PASSED; - } - - state = HEATERS_SELF_TEST_COMPLETE; + setHeaterDutyCycle( DG_TRIMMER_HEATER, dutyCycle ); } 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 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 PRIMARY_HEATERS_EXEC_STATES_T handlePrimaryHeaterStateOff( void ) +static void setHeaterDutyCycle( DG_HEATERS_T heater, F32 pwm ) { - PRIMARY_HEATERS_EXEC_STATES_T state = PRIMARY_HEATERS_EXEC_STATE_OFF; - - if ( hasStartPrimaryHeaterRequested ) + // Check if the requested duty cycle is different from what the heater's duty cycle is. + // If the same duty cycle is requested, then it is not needed to send it again. This is to make sure + // the same duty cycle is not sent to the hardware all the time. + if ( fabs( heatersStatus[ heater ].dutycycle - pwm ) > NEARLY_ZERO ) { - resetHeaterState( PRIMARY_HEATER ); - setMainPrimaryHeaterPWM( mainPrimaryHeaterDutyCycle ); - isPrimaryHeaterOn = TRUE; - hasStartPrimaryHeaterRequested = FALSE; - - // 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() ) + if ( DG_PRIMARY_HEATER == heater ) { - // Set the feedback temperature sensor - primaryHeatersFeedbackTempSensor = TEMPSENSORS_OUTLET_REDUNDANT; //TODO change this to Thd sensors once it is installed + setMainPrimaryHeaterPWM( pwm ); + setSmallPrimaryHeaterPWM( pwm ); } + else if ( DG_TRIMMER_HEATER == heater ) + { + setTrimmerHeaterPWM( pwm ); + } - state = PRIMARY_HEATERS_EXEC_STATE_CONTROL_TO_TARGET; + // Updated the heater's information + heatersStatus[ heater ].dutycycle = pwm; } - - 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 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 PRIMARY_HEATERS_EXEC_STATES_T handlePrimaryHeaterStateControlToTarget( void ) +static F32 calculatePrimaryHeaterDutyCycle( F32 targetTemperature, F32 currentTemperature, F32 flow, BOOL checkEfficiency ) { - PRIMARY_HEATERS_EXEC_STATES_T state = PRIMARY_HEATERS_EXEC_STATE_CONTROL_TO_TARGET; + // Get the primary heater's efficiency and the last fill temperature from the ModeFill + F32 heaterEfficiency = heatersStatus[ DG_PRIMARY_HEATER ].heaterEfficiency; - if ( ++primaryHeaterTimerCounter >= CONTROLLER_CHECK_INTERVAL_COUNT ) +#ifndef _RELEASE_ + if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_HEATERS_EFFICIENCY ) != SW_CONFIG_ENABLE_VALUE ) +#endif { - // 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 ) + if ( TRUE == checkEfficiency ) { - F32 outletTemp = getTemperatureValue( primaryHeatersFeedbackTempSensor ); - mainPrimaryHeaterDutyCycle = runPIController( PI_CONTROLLER_ID_PRIMARY_HEATER, primaryHeaterTargetTemperature, outletTemp ); + F32 lastFillTemperature = getLastFillTemperature(); + F32 primaryTargetTemperature = heatersStatus[ DG_PRIMARY_HEATER ].targetTemp; - if ( mainPrimaryHeaterDutyCycle >= MAIN_PRIMARY_HEATER_MAX_DUTY_CYCLE ) + // 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 under-shoots the duty cycle, the efficiency increases the duty cycle for the next fill cycle + if ( lastFillTemperature - primaryTargetTemperature < 0.0F ) { - // The duty cycle from the PI controller was greater than max duty cycle of the main primary - // heater. So subtract the remaining from the max main primary heater duty cycle and set - // the rest to the small primary heater - smallPrimaryHeaterDutyCycle = mainPrimaryHeaterDutyCycle - MAIN_PRIMARY_HEATER_MAX_DUTY_CYCLE; - mainPrimaryHeaterDutyCycle = MAIN_PRIMARY_HEATER_MAX_DUTY_CYCLE; - setMainPrimaryHeaterPWM( mainPrimaryHeaterDutyCycle ); - setSmallPrimaryHeaterPWM( smallPrimaryHeaterDutyCycle ); + heaterEfficiency -= ( lastFillTemperature - primaryTargetTemperature ) * PRIMARY_HEATER_DUTY_CYCLE_PER_TEMPERATURE_C; } else { - setMainPrimaryHeaterPWM( mainPrimaryHeaterDutyCycle ); - smallPrimaryHeaterDutyCycle = HEATERS_MIN_DUTY_CYCLE; - setSmallPrimaryHeaterPWM( smallPrimaryHeaterDutyCycle ); + heaterEfficiency += ( primaryTargetTemperature - lastFillTemperature ) * PRIMARY_HEATER_DUTY_CYCLE_PER_TEMPERATURE_C; } - } - // Flow is below the minimum required flow to run the primary heaters - else - { - setMainPrimaryHeaterPWM( HEATERS_MIN_DUTY_CYCLE ); - setSmallPrimaryHeaterPWM( HEATERS_MIN_DUTY_CYCLE ); - } - primaryHeaterTimerCounter = 0; - } + // If the efficiency is less than 0, set it to 0 but if the efficiency is greater than 100, it is not enforced + // to be 100 since the duty cycle range check will cap the duty cycle to 100 anyways. + heaterEfficiency = MAX( heaterEfficiency, 0.0 ); - if ( isPrimaryHeaterOn != TRUE ) - { - // Switch to off state. Set the duty cycles to 0 - mainPrimaryHeaterDutyCycle = HEATERS_MIN_DUTY_CYCLE; - smallPrimaryHeaterDutyCycle = HEATERS_MIN_DUTY_CYCLE; - state = PRIMARY_HEATERS_EXEC_STATE_OFF; + // Update the heaters efficiency + heatersStatus[ DG_PRIMARY_HEATER ].heaterEfficiency = heaterEfficiency; + } } - return state; + // 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 = MIN( dutyCycle, HEATERS_MAX_DUTY_CYCLE ); + dutyCycle = MAX( dutyCycle, HEATERS_MIN_DUTY_CYCLE ); + + return dutyCycle; } /*********************************************************************//** * @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 calculateTrimmerHeaterDutyCycle function calculates the trimmer + * heater's duty cycle. + * @details Inputs: none + * @details Outputs: none + * @param targetTemperature target temperature of the heater + * @oaram currentTemperature current inlet temperature of the heater + * @param flow current flow + * @param check efficiency flag to indicate whether to consider heater's + * efficiency + * @return calculated duty cycle *************************************************************************/ -static TRIMMER_HEATER_EXEC_STATES_T handleTrimmerHeaterStateOff( void ) +static F32 calculateTrimmerHeaterDutyCycle( F32 targetTemperature, F32 currentTemperature, F32 flow, BOOL checkEfficiency ) { - TRIMMER_HEATER_EXEC_STATES_T state = TRIMMER_HEATER_EXEC_STATE_OFF; + // Get the primary heater's efficiency and the last fill temperature from the ModeFill + F32 heaterEfficiency = heatersStatus[ DG_TRIMMER_HEATER ].heaterEfficiency; + F32 dutyCycle = 0.0; - if ( hasStartTrimmerHeaterRequested ) +#ifndef _RELEASE_ + if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_HEATERS_EFFICIENCY ) != SW_CONFIG_ENABLE_VALUE ) +#endif { - resetHeaterState( TRIMMER_HEATER ); - isTrimmerHeaterOn = TRUE; - hasStartTrimmerHeaterRequested = FALSE; - setTrimmerHeaterPWM( trimmerHeaterDutyCycle ); - - // 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() ) + if ( TRUE == checkEfficiency ) { - // Set the feedback temperature sensor - trimmerHeaterFeedbackTempSensor = TEMPSENSORS_OUTLET_REDUNDANT; //TODO change this to Thd sensors once it is installed + // TODO Do we need efficiency for the trimmer heater? } - - state = TRIMMER_HEATER_EXEC_STATE_CONTROL_TO_TARGET; } - return state; + // Duty cycle = ( 69.73 * flow rate * deltaT / primary heater maximum power ) and multiply the duty cycle to the heater efficiency + dutyCycle = ( ( flow * WATER_SPECIFIC_HEAT_DIVIDED_BY_MINUTES * + ( targetTemperature - currentTemperature ) * heaterEfficiency ) / TRIMMER_HEATER_MAX_POWER_W ) * heaterEfficiency; + + // Check the boundaries of the calculated duty cycle + dutyCycle = MIN( dutyCycle, HEATERS_MAX_DUTY_CYCLE ); + dutyCycle = MAX( dutyCycle, HEATERS_MIN_DUTY_CYCLE ); + + return dutyCycle; } /*********************************************************************//** * @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 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 TRIMMER_HEATER_EXEC_STATES_T handleTrimmerHeaterControlToTarget( void ) +static BOOL haveHeaterControlConditionsChanged( DG_HEATERS_T heater ) { - TRIMMER_HEATER_EXEC_STATES_T state = TRIMMER_HEATER_EXEC_STATE_CONTROL_TO_TARGET; + BOOL status = FALSE; + F32 targetFlow = ( DG_PRIMARY_HEATER == heater ? getTargetROPumpFlowRateLPM() : getTargetDialysateFlowLPM() ); + BOOL hasFlowChanged = ( fabs( targetFlow - heatersStatus[ heater ].targetFlow ) > NEARLY_ZERO ? TRUE : FALSE ); - if ( ++trimmerHeaterTimerCounter >= CONTROLLER_CHECK_INTERVAL_COUNT ) + // Check if the target flow has changed or the target temperature has changed. + if ( ( TRUE == hasFlowChanged ) || ( TRUE == heatersStatus[ heater ].hasTargetTempChanged ) ) { - // 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 ); - } - - trimmerHeaterTimerCounter = 0; + status = TRUE; + heatersStatus[ heater ].targetFlow = targetFlow; + heatersStatus[ heater ].hasTargetTempChanged = FALSE; } - if ( ! isTrimmerHeaterOn ) - { - // Set the duty cycle to 0 and switch to off state - trimmerHeaterDutyCycle = HEATERS_MIN_DUTY_CYCLE; - state = TRIMMER_HEATER_EXEC_STATE_OFF; - } - - return state; + return status; } /*********************************************************************//** @@ -826,69 +782,72 @@ /*********************************************************************//** * @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 + * The publishTemperatureData function publishes the heaters data into + * at the defined time interval. + * @details Inputs: dataPublicationTimerCounter + * @details Outputs: dataPublicationTimerCounter * @return none *************************************************************************/ -static void resetHeaterState( NAME_OF_HEATER_T heater ) +static void publishHeatersData( void ) { - if ( heater == PRIMARY_HEATER ) + if ( ++dataPublicationTimerCounter >= getU32OverrideValue( &heatersDataPublishInterval ) ) { - mainPrimaryHeaterDutyCycle = MAIN_PRIMARY_HEATER_MAX_DUTY_CYCLE; - resetPIController( PI_CONTROLLER_ID_PRIMARY_HEATER, MAIN_PRIMARY_HEATER_MAX_DUTY_CYCLE ); - } - else if ( heater == TRIMMER_HEATER ) - { - trimmerHeaterDutyCycle = TRIMMER_HEATER_MAX_DUTY_CYCLE; - resetPIController( PI_CONTROLLER_ID_TRIMMER_HEATER, TRIMMER_HEATER_MAX_DUTY_CYCLE ); - } -} + HEATERS_DATA_T data; -/*********************************************************************//** - * @brief - * The getPublishHeatersDataInterval function gets the publish interval. - * @details Inputs: heatersDataPublishInterval - * @details Outputs: none - * @return result - *************************************************************************/ -U32 getPublishHeatersDataInterval( void ) -{ - U32 result = heatersDataPublishInterval.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; + data.primaryCalcTargetTemp = heatersStatus[ DG_PRIMARY_HEATER ].calculatedTemperature; + data.trimmerCalcCurrentTemp = heatersStatus[ DG_TRIMMER_HEATER ].calculatedTemperature; - if ( OVERRIDE_KEY == heatersDataPublishInterval.override ) - { - result = heatersDataPublishInterval.ovData; - } + broadcastData( MSG_ID_DG_HEATERS_DATA, COMM_BUFFER_OUT_CAN_DG_BROADCAST, (U08*)&data, sizeof( HEATERS_DATA_T ) ); - return result; + dataPublicationTimerCounter = 0; + } } /*********************************************************************//** * @brief - * The publishTemperatureData function publishes the heaters data into - * at the defined time interval. - * @details Inputs: dataPublicationTimerCounter - * @details Outputs: dataPublicationTimerCounter + * The monitorHeatersVoltage function monitors the heaters' voltages + * @details Inputs: voltageMonitorTimeCounter + * @details Outputs: voltageMonitorTimeCounter * @return none *************************************************************************/ -static void publishHeatersData( void ) +static void monitorHeatersVoltage( void ) { - if ( ++dataPublicationTimerCounter >= getPublishHeatersDataInterval() ) - { - HEATERS_DATA_T data; + 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 ); - data.mainPrimayHeaterDC = mainPrimaryHeaterDutyCycle * 100.0; - data.smallPrimaryHeaterDC = smallPrimaryHeaterDutyCycle * 100.0; - data.trimmerHeaterDC = trimmerHeaterDutyCycle * 100.0; + // Voltage to PWM is reverse. If PWM = 0 -> V = 24V + F32 mainPri = heatersStatus[ DG_PRIMARY_HEATER ].dutycycle > NEARLY_ZERO ? heatersStatus[ DG_PRIMARY_HEATER ].dutycycle : 1.0; + F32 smallPri = heatersStatus[ DG_PRIMARY_HEATER ].dutycycle > NEARLY_ZERO ? heatersStatus[ DG_PRIMARY_HEATER ].dutycycle : 1.0; + F32 trimmer = heatersStatus[ DG_TRIMMER_HEATER ].dutycycle > NEARLY_ZERO ? heatersStatus[ DG_TRIMMER_HEATER ].dutycycle : 1.0; - broadcastHeatersData( &data ); + F32 mainPriVoltageTol = HEATERS_MAX_VOLTAGE_OUT_OF_RANGE_TOL * mainPriVoltage; + F32 smallPriVoltageTol = HEATERS_MAX_VOLTAGE_OUT_OF_RANGE_TOL * smallPriVoltage; + F32 trimmerVoltageTol = HEATERS_MAX_VOLTAGE_OUT_OF_RANGE_TOL * trimmerVoltage; - dataPublicationTimerCounter = 0; - } + BOOL isMainPriOut = ( fabs( ( HEATERS_MAX_OPERATING_VOLTAGE_V * mainPri ) - mainPriVoltage ) > mainPriVoltageTol ? TRUE : FALSE ); + BOOL isSmallPriOut = ( fabs( ( HEATERS_MAX_OPERATING_VOLTAGE_V * smallPri ) - smallPriVoltage ) > smallPriVoltageTol ? TRUE : FALSE ); + BOOL isTrimmerOut = ( fabs( ( HEATERS_MAX_OPERATING_VOLTAGE_V * trimmer ) - trimmerVoltage ) > trimmerVoltageTol ? TRUE : FALSE ); + + //checkPersistentAlarm( ALARM_ID_DG_MAIN_PRIMARY_HEATER_VOLTAGE_OUT_OF_RANGE, isMainPriOut, mainPri, mainPriVoltageTol ); + //checkPersistentAlarm( ALARM_ID_DG_SMALL_PRIMARY_HEATER_VOLTAGE_OUT_OF_RANGE, isSmallPriOut, smallPri, smallPriVoltageTol ); + //checkPersistentAlarm( ALARM_ID_DG_TRIMMER_HEATER_VOLTAGE_OUT_OF_RANGE, isTrimmerOut, trimmer, trimmerVoltageTol ); } + /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ @@ -906,11 +865,11 @@ { BOOL result = FALSE; - if ( isTestingActivated() ) + if ( TRUE == isTestingActivated() ) { U32 interval = value / TASK_PRIORITY_INTERVAL; - result = TRUE; + result = TRUE; heatersDataPublishInterval.ovData = interval; heatersDataPublishInterval.override = OVERRIDE_KEY; } @@ -930,9 +889,9 @@ { BOOL result = FALSE; - if ( isTestingActivated() ) + if ( TRUE == isTestingActivated() ) { - result = TRUE; + result = TRUE; heatersDataPublishInterval.override = OVERRIDE_RESET; heatersDataPublishInterval.ovData = heatersDataPublishInterval.ovInitData; }