/**********************************************************************//** * * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * @file TemperatureSensors.c * * @date 7-Apr-2020 * @author Dara Navaei * * @brief DG temperature sensors controller * **************************************************************************/ #include // For temperature calculation #include "TemperatureSensors.h" #include "FPGA.h" #include "SystemCommMessages.h" #include "Timers.h" #include "TaskPriority.h" /** * @addtogroup TemperatureSensors * @{ */ // Private defines #define PRIMARY_HEATER_EXT_TEMP_SENSORS_GAIN 16U ///< Primary heater external temperature sensors gain #define PRIMARY_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE 19600U ///< Primary heater external temperature sensors reference resistance #define PRIMARY_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE 1000U ///< Primary heater external temperature sensors zero degree resistance #define COND_SENSORS_TEMP_SENSOR_GAIN 16U ///< Conductivity sensor gain #define COND_SENSORS_TEMP_SENSOR_REF_RESISTANCE 19600U ///< Conductivity sensor reference resistance #define COND_SENSORS_TEMP_SENSOR_0_DEGREE_RESISTANCE 1000U ///< Conductivity sensor zero degree resistance #define TRIMMER_HEATER_EXT_TEMP_SENSORS_GAIN 32U ///< Trimmer heater external temperature sensors gain #define TRIMMER_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE 5110U ///< Trimmer heater external temperature sensors reference resistance #define TRIMMER_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE 100U ///< Trimmer heater external temperature sensors zero degree resistance #define TEMP_SENSORS_ADC_BITS 24U ///< External temperature sensors ADC bits #define TEMP_SENSORS_ADC_MAX_COUNT (pow(2,TEMP_SENSORS_ADC_BITS)) ///< Temperature sensors max ADC count #define TEMP_EQUATION_COEFF_A (3.9083 * pow(10,-3)) ///< ADC to temperature conversion coefficient A #define TEMP_EQUATION_COEFF_B (-5.775 * pow(10,-7)) ///< ADC to temperature conversion coefficient B #define MAX_NUM_OF_RAW_ADC_SAMPLES 20U ///< Number of ADC reads for moving average calculations #define ADC_READ_FIRST_READ_INDEX 0U ///< ADC array first ADC read index #define ADC_READ_NEXT_INDEX_INDEX 0U ///< ADC array next insertion index #define ADC_READ_RUNNING_SUM_INDEX 1U ///< ADC array running sum index #define ADC_READ_GAIN_INDEX 0U ///< ADC array gain index #define ADC_READ_REF_RESISTANCE_INDEX 1U ///< ADC array reference resistances index #define ADC_READ_0_DEG_RESISTANCE_INDEX 2U ///< ADC array zero degree resistance index #define READ_AND_ERROR_PREV_FPGA_COUNT_INDEX 0U ///< Read and error previous FPGA count index #define READ_AND_ERROR_INTERNAL_READ_COUNT_INDEX 1U ///< Read and error internal read count index #define READ_AND_ERROR_INTERNAL_ERROR_COUNT_INDEX 2U ///< Read and error internal error count index #define READ_AND_ERROR_PREV_FPGA_ERROR_INDEX 3U ///< Read and error previous FPGA error index #define NUM_OF_READ_AND_ERROR_ARRAY_COLUMNS 4U ///< Number of read and error array columns #define MAX_ALLOWED_TEMP_DELTA_BETWEEN_SENSORS 2U ///< Maximum allowed temperature delta between sensors #define NUM_OF_RUNNING_SUM_AND_INDEX_ARRAY_COLUMNS 2U ///< Number of columns in running sum and index array #define NUM_OF_TEMP_SENSORS_CONSTANTS_ARRAY_COLUMNS 3U ///< Number of columns in temperature sensors constants #define MAX_ALLOWED_UNCHANGED_ADC_READS 4U ///< Maximum number of times that the read of a sensor cannot change #define EXTERNAL_TEMP_SENSORS_ERROR_VALUE 0x80 ///< External temperature sensors error value #define HEATERS_INTERNAL_TEMP_SENSOR_FAULT 0x01 ///< Heaters internal temperature sensor fault #define SHIFT_BITS_BY_2 2U ///< Shift bits by 2 #define MIN_WATER_INPUT_TEMPERATURE 10U ///< Minimum water input temperature #define MAX_WATER_INPUT_TEMPERATURE 40U ///< Maximum water input temperature #define ADC_FPGA_READ_DELAY 30U ///< Delay in ms before reading the ADC values from FPGA #define INPUT_WATER_TEMPERATURE_CHECK_TIME_INTERVAL 100U ///< Time interval that is used to check the input water #define HEATERS_INTERNAL_TEMPERTURE_CALCULATION_INTERVAL 20U ///< Time interval that is used to calculate the heaters internal temperature #define HEATERS_INTERNAL_ADC_TO_TEMP_CONVERSION_COEFF 0.25 ///< Heaters internal temperature sensors ADC to temperature conversion coefficient #define HEATERS_COLD_JUNCTION_ADC_TO_TEMP_CONVERSION_COEFF 0.0625 ///< Heaters cold junction temperatur sensors ADC to temperature conversion coefficient // todo ADD TO AE TO THE BOTTOM #define TEMP_SENSORS_DATA_PUBLISH_INTERVAL (5000 / TASK_PRIORITY_INTERVAL) ///< Temperature sensors publish data time interval #define K_THERMOCOUPLE_TEMP_2_MILLI_VOLT_CONVERSION_COEFF 0.041276 ///< K thermocouple temperature to millivolt conversion coefficient #define THERMOCOUPLE_POSITIVE_TEMP_C_0 (-0.176004136860 * pow(10,-1)) ///< K TC positive temperature C0 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_C_1 (0.389212049750 * pow(10,-1)) ///< K TC positive temperature C1 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_C_2 (0.185587700320 * pow(10,-4)) ///< K TC positive temperature C2 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_C_3 (-0.994575928740 * pow(10,-7)) ///< K TC positive temperature C3 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_C_4 (0.318409457190 * pow(10,-9)) ///< K TC positive temperature C4 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_C_5 (-0.560728448890 * pow(10,-12)) ///< K TC positive temperature C5 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_C_6 (0.560750590590 * pow(10,-15)) ///< K TC positive temperature C6 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_C_7 (-0.320207200030 * pow(10,-18)) ///< K TC positive temperature C7 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_C_8 (0.971511471520 * pow(10,-22)) ///< K TC positive temperature C8 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_C_9 (-0.121047212750 * pow(10,-25)) ///< K TC positive temperature C9 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_EXP_A_0 (0.118597600000 * pow(10,0)) ///< K TC positive temperature exponent coefficient A0 #define THERMOCOUPLE_POSITIVE_TEMP_EXP_A_1 (-0.118343200000 * pow(10,-3)) ///< K TC positive temperature exponent coefficient A1 #define THERMOCOUPLE_POSITIVE_TEMP_EXP_A_2 (0.126968600000 * pow(10,3)) ///< K TC positive temperature exponent coefficient A2 #define THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_0 0 ///< K TC positive temperature inverse D0 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_1 (2.508355 * pow(10,1)) ///< K TC positive temperature inverse D1 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_2 (7.860106 * pow(10,-2)) ///< K TC positive temperature inverse D2 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_3 (-2.503131 * pow(10,-1)) ///< K TC positive temperature inverse D3 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_4 (8.315270 * pow(10,-2)) ///< K TC positive temperature inverse D4 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_5 (-1.228034 * pow(10,-2)) ///< K TC positive temperature inverse D5 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_6 (9.804036 * pow(10,-4)) ///< K TC positive temperature inverse D6 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_7 (-4.413030 * pow(10,-5)) ///< K TC positive temperature inverse D7 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_8 (1.057734 * pow(10,-6)) ///< K TC positive temperature inverse D8 coefficient #define THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_9 (-1.052755 * pow(10,-8)) ///< K TC positive temperature inverse D9 coefficient /// Temperature sensor self test states typedef enum tempSensors_Self_Test_States { TEMPSENSORS_SELF_TEST_START = 0, ///< Temperature sensors self test start TEMPSENSORS_SELF_TEST_ADC_CHECK, ///< Temperature sensors self ADC check TEMPSENSORS_SELF_TEST_CONSISTENCY_CHECK, ///< Temperature sensors self test consistency check TEMPSENSORS_SELF_TEST_COMPLETE, ///< Temperature sensors self test complete NUM_OF_TEMPSENSORS_SELF_TEST_STATES ///< Total number of self test states } TEMPSENSORS_SELF_TEST_STATES_T; /// Temperature sensor exec states typedef enum tempSensors_Exec_States { TEMPSENSORS_EXEC_STATE_START = 0, ///< Temperature sensors exec start TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES, ///< Temperature sensors exec get ADC values NUM_OF_TEMPSENSORS_EXEC_STATES, ///< Total number of exec states } TEMPSENSORS_EXEC_STATES_T; // Private variables static SELF_TEST_STATUS_T tempSensorsSelfTestResult; ///< Self test result of the TemperatureSensors module static TEMPSENSORS_SELF_TEST_STATES_T tempSensorsSelfTestState; ///< TemperatureSensor self test state static TEMPSENSORS_EXEC_STATES_T tempSensorsExecState; ///< TemperatureSensor exec state static U32 sampleCount; ///< Initial ADC read index until the array if filled up for the first time static S32 rawADCReads [ NUM_OF_TEMPERATURE_SENSORS ] [ MAX_NUM_OF_RAW_ADC_SAMPLES ]; ///< Raw ADC reads array static S32 runningSumAndIndex [ NUM_OF_TEMPERATURE_SENSORS ] [ NUM_OF_RUNNING_SUM_AND_INDEX_ARRAY_COLUMNS ]; ///< Running sum and next ADC index array static U32 tempSensorsConstants [ NUM_OF_TEMPERATURE_SENSORS ] [ NUM_OF_TEMP_SENSORS_CONSTANTS_ARRAY_COLUMNS ]; ///< Temperature sensors constants array static U32 readAndErrorCounts [ NUM_OF_TEMPERATURE_SENSORS ] [ NUM_OF_READ_AND_ERROR_ARRAY_COLUMNS ]; ///< Read and error counts from FPGA array //static F32 temperatureValues [ NUM_OF_TEMPERATURE_SENSORS ]; ///< Temperature values array //TODO Remove static OVERRIDE_F32_T temperatureValues [ NUM_OF_TEMPERATURE_SENSORS ]; ///< Temperature values array static U32 elapsedTime; ///< Elapsed time variable static U32 internalHeatersConversionTimer; ///< Conversion timer variable to calculate the heaters internal temperature static OVERRIDE_U32_T tempSensorsPublishInterval = { TEMP_SENSORS_DATA_PUBLISH_INTERVAL, TEMP_SENSORS_DATA_PUBLISH_INTERVAL, 0, 0 }; //TODO add to AE static U32 dataPublicationTimerCounter; // TODO add to AE static F32 tempValuesForPublication [ NUM_OF_TEMPERATURE_SENSORS ]; //TODO add to AE // Private functions prototypes static TEMPSENSORS_SELF_TEST_STATES_T handleSelfTestStart ( void ); static TEMPSENSORS_SELF_TEST_STATES_T handleSelfTestADCCheck ( void ); static TEMPSENSORS_SELF_TEST_STATES_T handleSelfTestConsistencyCheck ( void ); static TEMPSENSORS_EXEC_STATES_T handleExecStart ( void ); static TEMPSENSORS_EXEC_STATES_T handleExecGetADCValues ( void ); static F32 getADC2TempConversion ( F32 avgADC, U32 gain, U32 refResistance, U32 zeroDegResistance ); static void getHeaterInternalTemp ( U32 TCIndex, U32 CJIndex ); static void processADCRead ( U32 sensorIndex, U32 adc, U32 fpgaError, U32 fpgaCount ); static void publishTemperatureSensorsData ( void ); //TODO add to AE static DATA_GET_PROTOTYPE( U32, getPublishTemperatureSensorsDataInterval ); //TODO Add to AE // Public functions /************************************************************************* * @brief * The initTemperatureSensors function initializes the module * @details * Inputs : none * Outputs : tempSensorsSelfTestResult, tempSensorsSelfTestState, * tempSensorsExecState, sampleCount, elapsedTime, tempSensorsConstants * @param none * @return none *************************************************************************/ void initTemperatureSensors ( void ) { U08 i; tempSensorsSelfTestState = TEMPSENSORS_SELF_TEST_START; tempSensorsExecState = TEMPSENSORS_EXEC_STATE_START; sampleCount = 0; elapsedTime = 0; internalHeatersConversionTimer = 0; dataPublicationTimerCounter = 0; // Initialize TPi and TPo constants for ( i = TEMPSENSORS_INLET_PRIMARY_HEATER_TEMP_SENSOR; i < TEMPSENSORS_CONDUCTIVITY_SENSOR_1_TEMP_SENSOR; i++ ) { tempSensorsConstants [ i ] [ ADC_READ_GAIN_INDEX ] = PRIMARY_HEATER_EXT_TEMP_SENSORS_GAIN; tempSensorsConstants [ i ] [ ADC_READ_REF_RESISTANCE_INDEX ] = PRIMARY_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE; tempSensorsConstants [ i ] [ ADC_READ_0_DEG_RESISTANCE_INDEX ] = PRIMARY_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE; } // Initialize TD1 and TD2 constants for ( i = TEMPSENSORS_CONDUCTIVITY_SENSOR_1_TEMP_SENSOR; i < TEMPSENSORS_OUTLET_REDUNDANCY_TEMP_SENSOR; i++ ) { tempSensorsConstants [ i ] [ ADC_READ_GAIN_INDEX ] = COND_SENSORS_TEMP_SENSOR_GAIN; tempSensorsConstants [ i ] [ ADC_READ_REF_RESISTANCE_INDEX ] = COND_SENSORS_TEMP_SENSOR_REF_RESISTANCE; tempSensorsConstants [ i ] [ ADC_READ_0_DEG_RESISTANCE_INDEX ] = COND_SENSORS_TEMP_SENSOR_0_DEGREE_RESISTANCE; } // Initialize TRo and TDi constants for ( i = TEMPSENSORS_OUTLET_REDUNDANCY_TEMP_SENSOR; i < TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR; i++ ) { tempSensorsConstants [ i ] [ ADC_READ_GAIN_INDEX ] = TRIMMER_HEATER_EXT_TEMP_SENSORS_GAIN; tempSensorsConstants [ i ] [ ADC_READ_REF_RESISTANCE_INDEX ] = TRIMMER_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE; tempSensorsConstants [ i ] [ ADC_READ_0_DEG_RESISTANCE_INDEX ] = TRIMMER_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE; } // Initialize primary heater internal temperature sensor // The heaters do not need gain, reference resistance, and zero degree resistance tempSensorsConstants [ TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR ] [ ADC_READ_GAIN_INDEX ] = 0; tempSensorsConstants [ TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR ] [ ADC_READ_REF_RESISTANCE_INDEX ] = 0; tempSensorsConstants [ TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR ] [ ADC_READ_0_DEG_RESISTANCE_INDEX ] = 0; // Initialize trimmer heater internal temperature sensor tempSensorsConstants [ TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE_TEMP_SESNOR ] [ ADC_READ_GAIN_INDEX ] = 0; tempSensorsConstants [ TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE_TEMP_SESNOR ] [ ADC_READ_REF_RESISTANCE_INDEX ] = 0; tempSensorsConstants [ TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE_TEMP_SESNOR ] [ ADC_READ_0_DEG_RESISTANCE_INDEX ] = 0; } /************************************************************************* * @brief * The execTemperatureSensorsSelfTest function runs the TemperatureSensors * POST during the self test * @details * Inputs : tempSensorsSelfTestState * Outputs : tempSensorsSelfTestState * @param none * @return tempSensorsSelfTestState *************************************************************************/ SELF_TEST_STATUS_T execTemperatureSensorsSelfTest ( void ) { switch ( tempSensorsSelfTestState ) { case TEMPSENSORS_SELF_TEST_START: tempSensorsSelfTestState = handleSelfTestStart(); break; case TEMPSENSORS_SELF_TEST_ADC_CHECK: tempSensorsSelfTestState = handleSelfTestADCCheck(); break; case TEMPSENSORS_SELF_TEST_CONSISTENCY_CHECK: tempSensorsSelfTestState = handleSelfTestConsistencyCheck(); break; case TEMPSENSORS_SELF_TEST_COMPLETE: // Done with self test, do nothing break; default: SET_ALARM_WITH_2_U32_DATA ( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_TEMPERATURE_SENSORS_INVALID_SELF_TEST_STATE, tempSensorsSelfTestState ); tempSensorsSelfTestState = TEMPSENSORS_SELF_TEST_COMPLETE; break; } return tempSensorsSelfTestResult; } /************************************************************************* * @brief * The execTemperatureSensors function runs the TemperatureSensors main * tasks * @details * Inputs : tempSensorsExecState * Outputs : tempSensorsExecState * @param none * @return none *************************************************************************/ void execTemperatureSensors ( void ) { // Read the sensors all the time switch ( tempSensorsExecState ) { case TEMPSENSORS_SELF_TEST_START: tempSensorsExecState = handleExecStart(); break; case TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES: tempSensorsExecState = handleExecGetADCValues(); break; default: SET_ALARM_WITH_2_U32_DATA ( ALARM_ID_SOFTWARE_FAULT, SW_FAULT_ID_TEMPERATURE_SENSORS_EXEC_INVALID_STATE, tempSensorsExecState ); tempSensorsExecState = TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES; break; } } /************************************************************************* * @brief * The getTemperatureValue function gets the enum of the requested * temperature sensor and returns the temperature * @details * Inputs : none * Outputs : none * @param sensor * @return temperature *************************************************************************/ F32 getTemperatureValue ( U32 sensor ) { F32 temperature; if ( temperatureValues [ sensor ].override == OVERRIDE_KEY ) { temperature = temperatureValues [ sensor ].ovData; } else { temperature = temperatureValues [ sensor ].data; } return temperature; } // Private functions /************************************************************************* * @brief * The getADC2TempConversion function calculates the temperature from * ADC read from FPGA * @details * Inputs : none * Outputs : temperatureValues * @param avgADC, gain, refResistance, zeroDegResistance * @return temperature *************************************************************************/ static F32 getADC2TempConversion ( F32 avgADC, U32 gain, U32 refResistance, U32 zeroDegResistance ) { //R(RTD) = R(ref) * (adc – 2^N-1) / (G *2^N-1); F32 resistance = (refResistance * (avgADC - pow(2,(TEMP_SENSORS_ADC_BITS - 1)))) / (gain * pow(2,(TEMP_SENSORS_ADC_BITS - 1))); //T=(-A+√(A^2-4B(1-R_T/R_0 )))/2B F32 secondSqrtPart = 4 * TEMP_EQUATION_COEFF_B * (1 - (resistance / zeroDegResistance)); F32 temperature = (-TEMP_EQUATION_COEFF_A + sqrt(pow(TEMP_EQUATION_COEFF_A, 2) - secondSqrtPart)) / (2 * TEMP_EQUATION_COEFF_B); return temperature; } /************************************************************************* * @brief * The getHeaterInternalTemp function calculates the internal temperature * of the heaters * @details * Inputs : temperatureValues * Outputs : temperatureValues * @param TCIndex, CJIndex * @return none *************************************************************************/ static void getHeaterInternalTemp ( U32 TCIndex, U32 CJIndex ) { F32 temperature; F32 equiVoltage; F32 correctedVoltage; F32 TCTemp = temperatureValues [ TCIndex ].data; //F32 CJTemp = temperatureValues [ CJIndex ].data; // TODO For testing only REMOVE F32 CJTemp = 29; // Remove after testing // Value in milli-volts F32 rawVoltage = ( TCTemp - CJTemp ) * K_THERMOCOUPLE_TEMP_2_MILLI_VOLT_CONVERSION_COEFF; // TODO add positive and negative check equiVoltage = THERMOCOUPLE_POSITIVE_TEMP_C_0 * (pow(CJTemp, 0)) + THERMOCOUPLE_POSITIVE_TEMP_C_1 * (pow(CJTemp, 1)) + THERMOCOUPLE_POSITIVE_TEMP_C_2 * (pow(CJTemp, 2)) + THERMOCOUPLE_POSITIVE_TEMP_C_3 * (pow(CJTemp, 3)) + THERMOCOUPLE_POSITIVE_TEMP_C_4 * (pow(CJTemp, 4)) + THERMOCOUPLE_POSITIVE_TEMP_C_5 * (pow(CJTemp, 5)) + THERMOCOUPLE_POSITIVE_TEMP_C_6 * (pow(CJTemp, 6)) + THERMOCOUPLE_POSITIVE_TEMP_C_7 * (pow(CJTemp, 7)) + THERMOCOUPLE_POSITIVE_TEMP_C_8 * (pow(CJTemp, 8)) + THERMOCOUPLE_POSITIVE_TEMP_C_9 * (pow(CJTemp, 9)) + THERMOCOUPLE_POSITIVE_TEMP_EXP_A_0 * (exp(THERMOCOUPLE_POSITIVE_TEMP_EXP_A_1 * pow((CJTemp - THERMOCOUPLE_POSITIVE_TEMP_EXP_A_2),2))); correctedVoltage = rawVoltage + equiVoltage; temperature = THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_0 + THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_1 * correctedVoltage + THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_2 * (pow(correctedVoltage, 2)) + THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_3 * (pow(correctedVoltage, 3)) + THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_4 * (pow(correctedVoltage, 4)) + THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_5 * (pow(correctedVoltage, 5)) + THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_6 * (pow(correctedVoltage, 6)) + THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_7 * (pow(correctedVoltage, 7)) + THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_8 * (pow(correctedVoltage, 8)) + THERMOCOUPLE_POSITIVE_TEMP_INVERSE_D_9 * (pow(correctedVoltage, 9)); if ( TCIndex == TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR ) { temperatureValues [ TEMPSENSORS_PRIMARY_HEATER_INTERNAL_TEMP ].data = temperature; } else if ( TCIndex == TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE_TEMP_SESNOR ) { temperatureValues [ TEMPSENSORS_TRIMMER_HEATER_INTERNAL_TEMP ].data = temperature; } } // TODO break this function /************************************************************************* * @brief * The processADCRead function receives the ADC value and the sensor * index and calculates the running sum and the moving average of the ADCs * The temperatureSensorsADCRead and tempSensorsAvgADCValues are updated * @details * Inputs : U32 (sensorIndex, adc) * Outputs : none * @param sensorIndex, adc, fpgaError, fpgaCount * @return none *************************************************************************/ static void processADCRead (U32 sensorIndex, U32 adc, U32 fpgaError, U32 fpgaCount ) { U32 error; BOOL isADCValid = TRUE; F32 temperature; S32 convertedADC; /* * check if the index is not the heaters * Mask the values accordingly * if error is 0x80 for the external sensors, increment * if count has not changed, update the previous count, increment the internal error count * if the internal error count is above the threshold, set alarm, set the bool avGCalc to false * if count is greater than previous, update the previous and reset the internal count * set the avgCalc to true * if the avgCalc in true, calculate all the steps and immediately convert to temperature */ //TODO clean up if ( sensorIndex != TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR && sensorIndex != TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE_TEMP_SESNOR && sensorIndex != TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION_TEMP_SENSOR && sensorIndex != TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION_TEMP_SENSOR ) { error = 0; //TODO non-zero error convertedADC = (S32)(adc & MASK_OFF_U32_MSB); /* * what if the fpga channel error is not zero? * what if the fpga channel error is zero but the individual is not? */ if ( fpgaError != 0 ) { if ( error == EXTERNAL_TEMP_SENSORS_ERROR_VALUE ) { // TODO: alarm? } //isADCValid = FALSE; } if ( error == EXTERNAL_TEMP_SENSORS_ERROR_VALUE ) { //tODO: FILL UP //isADCValid = FALSE; } } // It is a heaters temperature sensor else if ( sensorIndex == TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR || sensorIndex == TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE_TEMP_SESNOR ) { // Cast the adc from U32 to U16 and shit it to left by 2 U16 adcConv = ( (U16)adc ) << SHIFT_BITS_BY_2; // Cast from U16 to S16 and shift the bits to right by 2 // so if the sign bit is 1, the sign bit is extended convertedADC = ( (S16)adcConv ) >> SHIFT_BITS_BY_2; } else if ( sensorIndex == TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION_TEMP_SENSOR || sensorIndex == TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION_TEMP_SENSOR ) { // Cast the adc from U32 to U16 and shift it by 4 U16 rawADC = ( (U16)adc ) << SHIFT_BITS_BY_4; // Cast from U16 to S16 and shift the bits to right by 4 // so if the sign bit is 1, the sign bit is extended convertedADC = ( (S16)rawADC ) >> SHIFT_BITS_BY_4; } U32 previousReadCount = readAndErrorCounts [ sensorIndex ] [ READ_AND_ERROR_PREV_FPGA_COUNT_INDEX ]; if ( fpgaCount == previousReadCount ) { U32 internalErrorCount = readAndErrorCounts [ sensorIndex ] [ READ_AND_ERROR_INTERNAL_ERROR_COUNT_INDEX ]; if ( internalErrorCount > MAX_ALLOWED_UNCHANGED_ADC_READS ) { // TODO alarm } else { readAndErrorCounts [ sensorIndex ] [ READ_AND_ERROR_PREV_FPGA_COUNT_INDEX ] = fpgaCount; readAndErrorCounts [ sensorIndex ] [ READ_AND_ERROR_INTERNAL_ERROR_COUNT_INDEX ] = internalErrorCount++; } //isADCValid = FALSE; } else if ( fpgaCount > previousReadCount ) { readAndErrorCounts [ sensorIndex ] [ READ_AND_ERROR_PREV_FPGA_COUNT_INDEX ] = fpgaCount; readAndErrorCounts [ sensorIndex ] [ READ_AND_ERROR_INTERNAL_ERROR_COUNT_INDEX ] = 0; isADCValid = TRUE; } if ( isADCValid ) { // Update the values in the folders U32 index = runningSumAndIndex [ sensorIndex ] [ ADC_READ_NEXT_INDEX_INDEX ]; S32 runningSum = runningSumAndIndex [ sensorIndex ] [ ADC_READ_RUNNING_SUM_INDEX ]; S32 indexValue = rawADCReads [ sensorIndex ] [ index ]; U32 nextIndex = INC_WRAP( index, ADC_READ_FIRST_READ_INDEX, MAX_NUM_OF_RAW_ADC_SAMPLES - 1 ); runningSum = runningSum - indexValue + convertedADC; rawADCReads [ sensorIndex ] [ index ] = convertedADC; runningSumAndIndex [ sensorIndex ] [ ADC_READ_NEXT_INDEX_INDEX ] = nextIndex; runningSumAndIndex [ sensorIndex ] [ ADC_READ_RUNNING_SUM_INDEX ] = runningSum; // If the buffer array is being filled for the first time, the number of samples // are changing. When the array is filled up completely, max number of samples are used if ( sampleCount < MAX_NUM_OF_RAW_ADC_SAMPLES ) { sampleCount++; } else { sampleCount = MAX_NUM_OF_RAW_ADC_SAMPLES; } // Calculate average F32 avgADCReads = runningSum / sampleCount; // The external temperature sensors have gain and other parameters for temperature // calculations. The heaters internal temperature sensors do not have any parameters // this is used to decide whether to call the quadratic equation or not if ( tempSensorsConstants [ sensorIndex ] [ ADC_READ_GAIN_INDEX ] != 0 ) { temperature = getADC2TempConversion ( avgADCReads, tempSensorsConstants [ sensorIndex ] [ ADC_READ_GAIN_INDEX ], tempSensorsConstants [ sensorIndex ] [ ADC_READ_REF_RESISTANCE_INDEX ], tempSensorsConstants [ sensorIndex ] [ ADC_READ_0_DEG_RESISTANCE_INDEX ] ); } if ( sensorIndex == TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR || sensorIndex == TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE_TEMP_SESNOR ) { temperature = avgADCReads * HEATERS_INTERNAL_ADC_TO_TEMP_CONVERSION_COEFF; } if ( sensorIndex == TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION_TEMP_SENSOR || sensorIndex == TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION_TEMP_SENSOR ) { temperature = avgADCReads * HEATERS_COLD_JUNCTION_ADC_TO_TEMP_CONVERSION_COEFF; } temperatureValues [ sensorIndex ].data = temperature; } } /************************************************************************* * @brief * The handleSelfTestStart function transitions the self test state to * check ADC * @details * Inputs : none * Outputs : none * @param none * @return state (TEMPSENSORS_SELF_TEST_STATES_T) *************************************************************************/ static TEMPSENSORS_SELF_TEST_STATES_T handleSelfTestStart ( void ) { tempSensorsSelfTestResult = SELF_TEST_STATUS_IN_PROGRESS; return TEMPSENSORS_SELF_TEST_ADC_CHECK; } /************************************************************************* * @brief * The handleSelfTestADCCheck function checks whether the ADC reads. If the * reads are above the maximum 24bit ADC count, it will throw an alarm and * switches to the next state * @details * Inputs : none * Outputs : none * @param none * @return state (TEMPSENSORS_SELF_TEST_STATES_T) *************************************************************************/ static TEMPSENSORS_SELF_TEST_STATES_T handleSelfTestADCCheck ( void ) { TEMPSENSORS_SELF_TEST_STATES_T state = TEMPSENSORS_SELF_TEST_CONSISTENCY_CHECK; // TODo Remove F32 avgADCReads [ NUM_OF_TEMPERATURE_SENSORS ]; //TODO remove U08 i; for ( i = 0; i < sizeof(avgADCReads); i++ ) { U32 avgADC = avgADCReads [ i ]; if ( avgADC > TEMP_SENSORS_ADC_MAX_COUNT ) { SET_ALARM_WITH_1_U32_DATA ( ALARM_ID_TEMPERATURE_SENSORS_OUT_OF_RANGE, TEMPSENSORS_SELF_TEST_ADC_CHECK ); } } return state; } /************************************************************************* * @brief * The handleSelfTestConsistencyCheck function checks the values of the * sensors to make sure they are within the allowed range from each other * @details * Inputs : none * Outputs : none * @param none * @return state (TEMPSENSORS_SELF_TEST_STATES_T) *************************************************************************/ static TEMPSENSORS_SELF_TEST_STATES_T handleSelfTestConsistencyCheck ( void ) { //TODO Consider edge cases for the consistency check TEMPSENSORS_SELF_TEST_STATES_T state = TEMPSENSORS_SELF_TEST_COMPLETE; U32 largestDelta; U08 i, j; // Check if all the sensors are within a certain degree c // from each other for ( i = 0; i < sizeof(temperatureValues); i++ ) { for ( j = 0; j < sizeof(temperatureValues); j++ ) { if ( i != j ) { largestDelta = MAX( largestDelta, fabs(temperatureValues [ i ].data - temperatureValues [ j ].data) ); } if ( largestDelta > MAX_ALLOWED_TEMP_DELTA_BETWEEN_SENSORS ) { SET_ALARM_WITH_1_U32_DATA ( ALARM_ID_TEMPERATURE_SENSORS_INCONSISTENT, TEMPSENSORS_SELF_TEST_CONSISTENCY_CHECK ); } } } return state; } /************************************************************************* * @brief * The handleExecStart function waits for a period of time and switches to * the state that reads the ADC values from FPGA * @details * Inputs : none * Outputs : elapsedTime * @param none * @return state (TEMPSENSORS_EXEC_STATES_T) *************************************************************************/ static TEMPSENSORS_EXEC_STATES_T handleExecStart ( void ) { TEMPSENSORS_EXEC_STATES_T state = TEMPSENSORS_EXEC_STATE_START; if ( elapsedTime == 0 ) { elapsedTime = getMSTimerCount(); } else if ( didTimeout( elapsedTime, ADC_FPGA_READ_DELAY ) ) { elapsedTime = 0; state = TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES; } return state; } /************************************************************************* * @brief * The handleExecGetADCValues function reads the ADC values from FPGA and * at the specified time intervals, checks the temperature range of the * inlet water and calls other functions to calculate the internal temperature * of the heaters * @details * Inputs : none * Outputs : internalHeatersConversionTimer, elapsedTime, temperatureValues * @param none * @return state (TEMPSENSORS_EXEC_STATES_T) *************************************************************************/ static TEMPSENSORS_EXEC_STATES_T handleExecGetADCValues ( void ) { TEMPSENSORS_EXEC_STATES_T state = TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES; // Look at the error counter and the specific error flag to make sure the error is a temp sensor // Add a byte array to have bits for each sensor to find out exactly what sensor failed processADCRead( TEMPSENSORS_INLET_PRIMARY_HEATER_TEMP_SENSOR, getFPGATPiTemp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); processADCRead( TEMPSENSORS_OUTLET_PRIMARY_HEATER_TEMP_SENSOR, getFPGATPoTemp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); processADCRead( TEMPSENSORS_CONDUCTIVITY_SENSOR_1_TEMP_SENSOR, getFPGACD1Temp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); processADCRead( TEMPSENSORS_CONDUCTIVITY_SENSOR_2_TEMP_SENSOR, getFPGACD2Temp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); processADCRead( TEMPSENSORS_OUTLET_REDUNDANCY_TEMP_SENSOR, getFPGATHDoTemp(), getFPGATHDoErrorCount(), getFPGATHDoReadCount() ); processADCRead( TEMPSENSORS_INLET_DIALYSATE_TEMP_SENSOR, getFPGATDiTemp(), getFPGATDiErrorCount(), getFPGATDiReadCount() ); processADCRead( TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR, getFPGAPrimaryHeaterTemp(), getFPGAPrimaryHeaterFlags(), getFPGAPrimaryHeaterReadCount() ); processADCRead( TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE_TEMP_SESNOR, getFPGATrimmerHeaterTemp(), getFPGATrimmerHeaterFlags(), getFPGATrimmerHeaterReadCount() ); processADCRead( TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION_TEMP_SENSOR, getFPGAPRimaryColdJunctionTemp(), getFPGATrimmerHeaterFlags(), getFPGAPrimaryHeaterReadCount() ); processADCRead( TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION_TEMP_SENSOR, getFPGATrimmerColdJunctionTemp(), getFPGATrimmerHeaterFlags(), getFPGATrimmerHeaterReadCount() ); // Check the inlet water is within temperature range at the desired time interval if ( elapsedTime == 0 ) { elapsedTime = getMSTimerCount(); } else if ( didTimeout( elapsedTime, INPUT_WATER_TEMPERATURE_CHECK_TIME_INTERVAL ) && temperatureValues [ TEMPSENSORS_INLET_PRIMARY_HEATER_TEMP_SENSOR ].override != OVERRIDE_KEY ) { elapsedTime = 0; F32 inletTemperature = temperatureValues [ TEMPSENSORS_INLET_PRIMARY_HEATER_TEMP_SENSOR ].data; if ( inletTemperature < MIN_WATER_INPUT_TEMPERATURE || inletTemperature >= MAX_WATER_INPUT_TEMPERATURE ) { SET_ALARM_WITH_1_U32_DATA ( ALARM_ID_INLET_WATER_TEMPERATURE_OUT_OF_RANGE, inletTemperature ); } } if ( internalHeatersConversionTimer >= HEATERS_INTERNAL_TEMPERTURE_CALCULATION_INTERVAL ) { getHeaterInternalTemp ( TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR, TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION_TEMP_SENSOR); getHeaterInternalTemp ( TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE_TEMP_SESNOR, TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION_TEMP_SENSOR); internalHeatersConversionTimer = 0; } else { internalHeatersConversionTimer++; } publishTemperatureSensorsData(); return state; } /************************************************************************* * @brief * The getPublishTemperatureSensorsDataInterval function returns the data * publication interval either from the data or from the override * @details * Inputs : tempSensorsPublishInterval * Outputs : none * @param none * @return result *************************************************************************/ U32 getPublishTemperatureSensorsDataInterval ( void ) { U32 result = tempSensorsPublishInterval.data; if ( OVERRIDE_KEY == tempSensorsPublishInterval.override ) { result = tempSensorsPublishInterval.ovData; } return result; } /************************************************************************* * @brief * The publishTemperatureSensorsData function broadcasts the temperature * sensors data at the publication interval * @details * Inputs : dataPublicationTimerCounter, tempValuesForPublication * Outputs : dataPublicationTimerCounter, tempValuesForPublication * @param none * @return none *************************************************************************/ static void publishTemperatureSensorsData ( void ) { if ( ++dataPublicationTimerCounter >= getPublishTemperatureSensorsDataInterval() ) { U08 i; for ( i = 0; i < NUM_OF_TEMPERATURE_SENSORS; i++ ) { tempValuesForPublication [ i ] = getTemperatureValue ( i ); } broadcastTemperatureSensorsData ( (U08*)&tempValuesForPublication, sizeof(tempValuesForPublication) ); } } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /************************************************************************* * @brief * The testSetMeasuredTemperatureOverride function sets the override value * for a specific temperature sensor * @details * Inputs : temperatureValues * Outputs : temperatureValues * @param sensor, temperature * @return result *************************************************************************/ BOOL testSetMeasuredTemperatureOverride ( U32 sensor, F32 temperature ) { BOOL result = FALSE; if ( sensor < NUM_OF_TEMPERATURE_SENSORS ) { if ( isTestingActivated() ) { result = TRUE; temperatureValues [ sensor ].ovData = temperature; temperatureValues [ sensor ].override = OVERRIDE_KEY; } } return result; } /************************************************************************* * @brief * The testSetMeasuredTemperatureOverride function resets the override value * of a specified temperature sensor * @details * Inputs : temperatureValues * Outputs : temperatureValues * @param sensor * @return result *************************************************************************/ BOOL testResetMeasuredTemperatureOverride ( U32 sensor ) { BOOL result = FALSE; if ( sensor < NUM_OF_TEMPERATURE_SENSORS ) { if ( isTestingActivated() ) { result = TRUE; temperatureValues [ sensor ].override = OVERRIDE_RESET; temperatureValues [ sensor ].ovData = temperatureValues [ sensor ].ovInitData; } } return result; } /************************************************************************* * @brief * The testSetTemperatureSensorsPublishIntervalOverride function overrides * the temperature sensors publish data interval * @details * Inputs : tempSensorsPublishInterval * Outputs : tempSensorsPublishInterval * @param value * @return result *************************************************************************/ BOOL testSetTemperatureSensorsPublishIntervalOverride ( U32 value ) { BOOL result = FALSE; if ( isTestingActivated() ) { U32 interval = value / TASK_PRIORITY_INTERVAL; result = TRUE; tempSensorsPublishInterval.ovData = interval; tempSensorsPublishInterval.override = OVERRIDE_KEY; } return result; } /************************************************************************* * @brief * The testResetTemperatureSensorsPublishIntervalOverride function resets * the override value of temperature sensors publish data interval * @details * Inputs : tempSensorsPublishInterval * Outputs : tempSensorsPublishInterval * @param none * @return result *************************************************************************/ BOOL testResetTemperatureSensorsPublishIntervalOverride ( void ) { BOOL result = FALSE; if ( isTestingActivated() ) { tempSensorsPublishInterval.override = OVERRIDE_RESET; tempSensorsPublishInterval.ovData = tempSensorsPublishInterval.ovInitData; } return result; } /**@}*/