Index: firmware/App/Controllers/TemperatureSensors.c =================================================================== diff -u -re83f47ec5c3d458701a32069dba4fe81aae6fd67 -r4df3087b3b1944c60cd33c752a81c3fdba19189f --- firmware/App/Controllers/TemperatureSensors.c (.../TemperatureSensors.c) (revision e83f47ec5c3d458701a32069dba4fe81aae6fd67) +++ firmware/App/Controllers/TemperatureSensors.c (.../TemperatureSensors.c) (revision 4df3087b3b1944c60cd33c752a81c3fdba19189f) @@ -56,6 +56,7 @@ #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 ADC_READ_2_TEMP_CONVERSION_COEFFICIENT 3U ///< ADC array ADC to temperature conversion coefficient (will be used for heaters internal sensors) #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 @@ -66,7 +67,7 @@ #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 NUM_OF_TEMP_SENSORS_CONSTANTS_ARRAY_COLUMNS 4U ///< 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 @@ -79,8 +80,8 @@ #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 +#define HEATERS_INTERNAL_TC_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 temperature 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 @@ -141,18 +142,17 @@ [ 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 ] +static F32 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 + [ NUM_OF_READ_AND_ERROR_ARRAY_COLUMNS ]; ///< Read and error counts from FPGA array +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 + TEMP_SENSORS_DATA_PUBLISH_INTERVAL, 0, 0 }; ///< Temperature sensors publish time interval override //TODO add to AE +static U32 dataPublicationTimerCounter; ///< Temperature sensors data publish timer counter // TODO add to AE +static F32 tempValuesForPublication [ NUM_OF_TEMPERATURE_SENSORS ]; ///< Temperature sensors data publication array //TODO add to AE // Private functions prototypes @@ -163,9 +163,14 @@ 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 F32 getADC2TempConversion ( F32 avgADC, U32 gain, U32 refResistance, U32 zeroDegResistance, F32 adcConversionCoeff ); static void getHeaterInternalTemp ( U32 TCIndex, U32 CJIndex ); -static void processADCRead ( U32 sensorIndex, U32 adc, U32 fpgaError, U32 fpgaCount ); +static void processADCRead_old ( U32 sensorIndex, U32 adc, U32 fpgaError, U32 fpgaCount ); //TODO remove the function and update AE + +static void processTempSnsrsADCRead ( U32 sensorIndex, U32 adc, U32 fpgaError, U32 fpgaCount ); //TODO add to AE +static void processHtrsTempSnsrsADCRead ( U32 sensorIndex, U32 adc, U32 fpgaError, U32 fpgaCount ); // TODO add to AE +static BOOL isADCReadValid ( U32 sensorIndex, U32 fpgaError, U32 fpgaCount ); // TODO add to AE +static void processADCRead ( U32 sensorIndex, S32 adc ); static void publishTemperatureSensorsData ( void ); //TODO add to AE static DATA_GET_PROTOTYPE( U32, getPublishTemperatureSensorsDataInterval ); //TODO Add to AE @@ -191,37 +196,61 @@ internalHeatersConversionTimer = 0; dataPublicationTimerCounter = 0; + /* NOTE: The temperature sensors do not have conversion coefficient. + * The conversion coefficients are used for the heaters internal temperature sensors and + * the temperature will be calculated using a quadratic equation + * The internal thermocouple has 0.25 conversion coefficient + * The heaters cold junction sensor has 0.0625 conversion coefficient + * The conversion coefficient will be set to 0 for the temperature sensors + */ // 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; + 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; + tempSensorsConstants [ i ] [ ADC_READ_2_TEMP_CONVERSION_COEFFICIENT ] = 0; } // 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; + 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; + tempSensorsConstants [ i ] [ ADC_READ_2_TEMP_CONVERSION_COEFFICIENT ] = 0; } // 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; + 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; + tempSensorsConstants [ i ] [ ADC_READ_2_TEMP_CONVERSION_COEFFICIENT ] = 0; } - - // 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; + // Initialize the heaters internal thermocouples constants + for ( i = TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR; i < TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION_TEMP_SENSOR; i++ ) + { + tempSensorsConstants [ i ] [ ADC_READ_GAIN_INDEX ] = 0; + tempSensorsConstants [ i ] [ ADC_READ_REF_RESISTANCE_INDEX ] = 0; + tempSensorsConstants [ i ] [ ADC_READ_0_DEG_RESISTANCE_INDEX ] = 0; + tempSensorsConstants [ i ] [ ADC_READ_2_TEMP_CONVERSION_COEFFICIENT ] = HEATERS_INTERNAL_TC_ADC_TO_TEMP_CONVERSION_COEFF; + } + // Initialize the heaters cold junction constants + for ( i = TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION_TEMP_SENSOR; i < TEMPSENSORS_PRIMARY_HEATER_INTERNAL_TEMP; i++ ) + { + tempSensorsConstants [ i ] [ ADC_READ_GAIN_INDEX ] = 0; + tempSensorsConstants [ i ] [ ADC_READ_REF_RESISTANCE_INDEX ] = 0; + tempSensorsConstants [ i ] [ ADC_READ_0_DEG_RESISTANCE_INDEX ] = 0; + tempSensorsConstants [ i ] [ ADC_READ_2_TEMP_CONVERSION_COEFFICIENT ] = HEATERS_COLD_JUNCTION_ADC_TO_TEMP_CONVERSION_COEFF; + } + // Initialize the heaters calculated internal temperature sensors. The constants are zero since they will not be used for conversion + for ( i = TEMPSENSORS_PRIMARY_HEATER_INTERNAL_TEMP; i < NUM_OF_TEMPERATURE_SENSORS; i++ ) + { + tempSensorsConstants [ i ] [ ADC_READ_GAIN_INDEX ] = 0; + tempSensorsConstants [ i ] [ ADC_READ_REF_RESISTANCE_INDEX ] = 0; + tempSensorsConstants [ i ] [ ADC_READ_0_DEG_RESISTANCE_INDEX ] = 0; + tempSensorsConstants [ i ] [ ADC_READ_2_TEMP_CONVERSION_COEFFICIENT ] = 0; + } } /************************************************************************* @@ -334,14 +363,23 @@ * @param avgADC, gain, refResistance, zeroDegResistance * @return temperature *************************************************************************/ -static F32 getADC2TempConversion ( F32 avgADC, U32 gain, U32 refResistance, U32 zeroDegResistance ) +static F32 getADC2TempConversion ( F32 avgADC, U32 gain, U32 refResistance, U32 zeroDegResistance, F32 adcConversionCoeff ) { - //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); + F32 temperature; + if ( adcConversionCoeff == 0.0 ) + { + //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)); + temperature = (-TEMP_EQUATION_COEFF_A + sqrt(pow(TEMP_EQUATION_COEFF_A, 2) - secondSqrtPart)) / (2 * TEMP_EQUATION_COEFF_B); + } + else + { + temperature = avgADC * adcConversionCoeff; + } + return temperature; } @@ -361,9 +399,9 @@ F32 equiVoltage; F32 correctedVoltage; F32 TCTemp = temperatureValues [ TCIndex ].data; - //F32 CJTemp = temperatureValues [ CJIndex ].data; + F32 CJTemp = temperatureValues [ CJIndex ].data; // TODO For testing only REMOVE - F32 CJTemp = 29; + //F32 CJTemp = 29; // Remove after testing // Value in milli-volts @@ -406,20 +444,170 @@ } } -// TODO break this function /************************************************************************* * @brief + * The processTemperatureSensorsADCRead function masks the MSB of the ADC + * read from FPGA and converts it to an S32. Then it calls another function + * to check if the read ADC is valid or not and if it is, it calls another + * function to process the ADC value and covert it to temperature + * @details + * Inputs : none + * Outputs : none + * @param sensorIndex, adc, fpgaError, fpgaCount + * @return none + *************************************************************************/ +static void processTempSnsrsADCRead ( U32 sensorIndex, U32 adc, U32 fpgaError, U32 fpgaCount ) +{ + S32 convertedADC = (S32)(adc & MASK_OFF_U32_MSB); + + if ( isADCReadValid ( sensorIndex, fpgaError, fpgaCount ) ) + { + processADCRead ( sensorIndex, convertedADC ); + } +} + +/************************************************************************* + * @brief + * The processHeatersInternalSensorsADCRead function checks whether the provided + * sensor is the heaters thermocouple or cold junction sensors and performs + * different bit shifts on them accordingly. Then it call another function to + * check if the read ADC is valid and if it is, the function calls another function + * process the ADC and convert it to temperature + * @details + * Inputs : none + * Outputs : none + * @param sensorIndex, adc, fpgaError, fpgaCount + * @return none + *************************************************************************/ +static void processHtrsTempSnsrsADCRead ( U32 sensorIndex, U32 adc, U32 fpgaError, U32 fpgaCount ) +{ + U16 adcConv; + S16 convertedADC; + + 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 + 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 + adcConv = ( (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)adcConv ) >> SHIFT_BITS_BY_4; + } + + if ( isADCReadValid ( sensorIndex, fpgaError, fpgaCount ) ) + { + processADCRead ( sensorIndex, convertedADC ); + } +} + +/************************************************************************* + * @brief + * The isADCReadValid function checks if there is an FPGA error and FPGA + * count. If there is any error in the FPGA error, it raises and alarm. + * If the count has changed and the ADC value is not the same as the previous + * ADC read, it returns a TRUE, signaling that the ADC is valid to be processed + * @details + * Inputs : readAndErrorCounts + * Outputs : readAndErrorCounts + * @param sensorIndex, fpgaError, fpgaCount + * @return isADCValid (BOOL) + *************************************************************************/ +static BOOL isADCReadValid ( U32 sensorIndex, U32 fpgaError, U32 fpgaCount ) +{ + BOOL isADCValid = FALSE; + U32 previousReadCount = readAndErrorCounts [ sensorIndex ] [ READ_AND_ERROR_PREV_FPGA_COUNT_INDEX ]; + + if ( fpgaError != 0 ) + { + /*if ( error == EXTERNAL_TEMP_SENSORS_ERROR_VALUE ) + { + // TODO: alarm? + }*/ + } + else if ( fpgaCount == previousReadCount ) + { + U32 internalErrorCount = readAndErrorCounts [ sensorIndex ] [ READ_AND_ERROR_INTERNAL_ERROR_COUNT_INDEX ]; + + if ( internalErrorCount > MAX_ALLOWED_UNCHANGED_ADC_READS ) + { + // TODO alarm + } + else + { + internalErrorCount = internalErrorCount + 1; + readAndErrorCounts [ sensorIndex ] [ READ_AND_ERROR_PREV_FPGA_COUNT_INDEX ] = fpgaCount; + readAndErrorCounts [ sensorIndex ] [ READ_AND_ERROR_INTERNAL_ERROR_COUNT_INDEX ] = internalErrorCount; + } + } + 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; + } + + return isADCValid; +} + +/************************************************************************* + * @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 + * Inputs : runningSumAndIndex, rawADCReads, sampleCount, temperatureValues + * Outputs : runningSumAndIndex, rawADCReads, sampleCount, temperatureValues + * @param sensorIndex, adc * @return none *************************************************************************/ -static void processADCRead (U32 sensorIndex, U32 adc, U32 fpgaError, U32 fpgaCount ) +static void processADCRead ( U32 sensorIndex, S32 adc ) { + // 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 + adc; + + rawADCReads [ sensorIndex ] [ index ] = adc; + 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; + + F32 temperature = getADC2TempConversion ( avgADCReads, + (U32)tempSensorsConstants [ sensorIndex ] [ ADC_READ_GAIN_INDEX ], + (U32)tempSensorsConstants [ sensorIndex ] [ ADC_READ_REF_RESISTANCE_INDEX ], + (U32)tempSensorsConstants [ sensorIndex ] [ ADC_READ_0_DEG_RESISTANCE_INDEX ], + tempSensorsConstants [ sensorIndex ] [ ADC_READ_2_TEMP_CONVERSION_COEFFICIENT ] ); + + temperatureValues [ sensorIndex ].data = temperature; +} + + +// TODO remove this function +static void processADCRead_old (U32 sensorIndex, U32 adc, U32 fpgaError, U32 fpgaCount ) +{ U32 error; BOOL isADCValid = TRUE; F32 temperature; @@ -538,12 +726,12 @@ temperature = getADC2TempConversion ( avgADCReads, tempSensorsConstants [ sensorIndex ] [ ADC_READ_GAIN_INDEX ], tempSensorsConstants [ sensorIndex ] [ ADC_READ_REF_RESISTANCE_INDEX ], - tempSensorsConstants [ sensorIndex ] [ ADC_READ_0_DEG_RESISTANCE_INDEX ] ); + tempSensorsConstants [ sensorIndex ] [ ADC_READ_0_DEG_RESISTANCE_INDEX ], 0 ); } 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; + temperature = avgADCReads * HEATERS_INTERNAL_TC_ADC_TO_TEMP_CONVERSION_COEFF; } if ( sensorIndex == TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION_TEMP_SENSOR || sensorIndex == TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION_TEMP_SENSOR ) @@ -553,6 +741,7 @@ temperatureValues [ sensorIndex ].data = temperature; } } +//TODO remove this function /************************************************************************* * @brief @@ -635,6 +824,9 @@ } } + // TDOD Fix this + tempSensorsSelfTestResult = SELF_TEST_STATUS_PASSED; + return state; } @@ -683,17 +875,32 @@ // 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() ); + processTempSnsrsADCRead ( TEMPSENSORS_INLET_PRIMARY_HEATER_TEMP_SENSOR, getFPGATPiTemp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); + processTempSnsrsADCRead ( TEMPSENSORS_OUTLET_PRIMARY_HEATER_TEMP_SENSOR, getFPGATPoTemp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); + processTempSnsrsADCRead ( TEMPSENSORS_CONDUCTIVITY_SENSOR_1_TEMP_SENSOR, getFPGACD1Temp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); + processTempSnsrsADCRead ( TEMPSENSORS_CONDUCTIVITY_SENSOR_2_TEMP_SENSOR, getFPGACD2Temp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); + processTempSnsrsADCRead ( TEMPSENSORS_OUTLET_REDUNDANCY_TEMP_SENSOR, getFPGATHDoTemp(), getFPGATHDoErrorCount(), getFPGATHDoReadCount() ); + processTempSnsrsADCRead ( TEMPSENSORS_INLET_DIALYSATE_TEMP_SENSOR, getFPGATDiTemp(), getFPGATDiErrorCount(), getFPGATDiReadCount() ); + processHtrsTempSnsrsADCRead ( TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR, getFPGAPrimaryHeaterTemp(), getFPGAPrimaryHeaterFlags(), getFPGAPrimaryHeaterReadCount() ); + processHtrsTempSnsrsADCRead ( TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE_TEMP_SESNOR, getFPGATrimmerHeaterTemp(), getFPGATrimmerHeaterFlags(), getFPGATrimmerHeaterReadCount() ); + processHtrsTempSnsrsADCRead ( TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION_TEMP_SENSOR, getFPGAPRimaryColdJunctionTemp(), getFPGATrimmerHeaterFlags(), getFPGAPrimaryHeaterReadCount() ); + processHtrsTempSnsrsADCRead ( TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION_TEMP_SENSOR, getFPGATrimmerColdJunctionTemp(), getFPGATrimmerHeaterFlags(), getFPGATrimmerHeaterReadCount() ); + + + // TODO Remove after testing + /*processADCRead_old( TEMPSENSORS_INLET_PRIMARY_HEATER_TEMP_SENSOR, getFPGATPiTemp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); + processADCRead_old( TEMPSENSORS_OUTLET_PRIMARY_HEATER_TEMP_SENSOR, getFPGATPoTemp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); + processADCRead_old( TEMPSENSORS_CONDUCTIVITY_SENSOR_1_TEMP_SENSOR, getFPGACD1Temp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); + processADCRead_old( TEMPSENSORS_CONDUCTIVITY_SENSOR_2_TEMP_SENSOR, getFPGACD2Temp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); + processADCRead_old( TEMPSENSORS_OUTLET_REDUNDANCY_TEMP_SENSOR, getFPGATHDoTemp(), getFPGATHDoErrorCount(), getFPGATHDoReadCount() ); + processADCRead_old( TEMPSENSORS_INLET_DIALYSATE_TEMP_SENSOR, getFPGATDiTemp(), getFPGATDiErrorCount(), getFPGATDiReadCount() ); + processADCRead_old( TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE_TEMP_SENSOR, getFPGAPrimaryHeaterTemp(), getFPGAPrimaryHeaterFlags(), getFPGAPrimaryHeaterReadCount() ); + processADCRead_old( TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE_TEMP_SESNOR, getFPGATrimmerHeaterTemp(), getFPGATrimmerHeaterFlags(), getFPGATrimmerHeaterReadCount() ); + processADCRead_old( TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION_TEMP_SENSOR, getFPGAPRimaryColdJunctionTemp(), getFPGATrimmerHeaterFlags(), getFPGAPrimaryHeaterReadCount() ); + processADCRead_old( TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION_TEMP_SENSOR, getFPGATrimmerColdJunctionTemp(), getFPGATrimmerHeaterFlags(), getFPGATrimmerHeaterReadCount() );*/ + //TODO remove after testing + // Check the inlet water is within temperature range at the desired time interval if ( elapsedTime == 0 ) { @@ -742,7 +949,7 @@ { U32 result = tempSensorsPublishInterval.data; - if ( OVERRIDE_KEY == tempSensorsPublishInterval.override ) + if ( tempSensorsPublishInterval.override == OVERRIDE_KEY ) { result = tempSensorsPublishInterval.ovData; } @@ -773,6 +980,7 @@ broadcastTemperatureSensorsData ( (U08*)&tempValuesForPublication, sizeof(tempValuesForPublication) ); + dataPublicationTimerCounter = 0; } } @@ -790,7 +998,6 @@ * @param sensor, temperature * @return result *************************************************************************/ - BOOL testSetMeasuredTemperatureOverride ( U32 sensor, F32 temperature ) { BOOL result = FALSE; @@ -877,12 +1084,12 @@ if ( isTestingActivated() ) { + result = TRUE; tempSensorsPublishInterval.override = OVERRIDE_RESET; tempSensorsPublishInterval.ovData = tempSensorsPublishInterval.ovInitData; } return result; - } /**@}*/