Index: firmware/App/Monitors/Temperature.c =================================================================== diff -u -r5422509f9d8db102b62952ba9f5923e832d6b2fa -r0df32171c99e44512f8ea90ab2068e25ef4bcce2 --- firmware/App/Monitors/Temperature.c (.../Temperature.c) (revision 5422509f9d8db102b62952ba9f5923e832d6b2fa) +++ firmware/App/Monitors/Temperature.c (.../Temperature.c) (revision 0df32171c99e44512f8ea90ab2068e25ef4bcce2) @@ -34,7 +34,8 @@ #define TEMP_SENSORS_DATA_PUBLISH_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Temperature sensors publish data time interval. #define TEMP_SENSORS_FPGA_ERROR_TIMEOUT_MS ( 2 * MS_PER_SECOND ) ///< Temperature sensors FPGA error timeout in milliseconds. - +#define D4_TEMP_MOVING_AVG_NUM_OF_SAMPLES ( 2 * MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< D4 temperature sensor moving average number of samples ( 2sec filter ). +#define D50_TEMP_MOVING_AVG_NUM_OF_SAMPLES 25 ///< D50 temperature sensor moving average number of samples ( 250ms filter ). #define DATA_PUBLISH_COUNTER_START_COUNT 30 ///< Data publish counter start count. /// Temperature sensor exec states. @@ -48,8 +49,20 @@ // ********** private data ********** static TEMPSENSORS_EXEC_STATES_T tempSensorsExecState; ///< TemperatureSensor exec state. -static U32 startTime; ///< star time to read FPGA values. +static U32 startTime; ///< start time to read FPGA values. +static F32 d4TempAvgC; ///< D4 temperature average in C. +static F32 d4TempRunningSumC; ///< D4 temperature running sum in C. +static F32 d4TempSamplesC[ D4_TEMP_MOVING_AVG_NUM_OF_SAMPLES ]; ///< D4 temperature samples array in C. +static U32 d4TempSamplesNextIndex; ///< D4 temperature sample next index number. +static U32 d4TempCount; ///< D4 Number of samples in average buffer. + +static F32 d50TempAvgC; ///< D50 temperature average in C. +static F32 d50TempRunningSumC; ///< D50 temperature running sum in C. +static F32 d50TempSamplesC[ D50_TEMP_MOVING_AVG_NUM_OF_SAMPLES ]; ///< D50 temperature samples array in C. +static U32 d50TempSamplesNextIndex; ///< D50 temperature sample next index number. +static U32 d50TempCount; ///< D50 Number of samples in average buffer. + static U32 dataPublicationTimerCounter; ///< Temperature sensors data publish timer counter. static OVERRIDE_U32_T tempSensorsPublishInterval = { TEMP_SENSORS_DATA_PUBLISH_INTERVAL, TEMP_SENSORS_DATA_PUBLISH_INTERVAL, 0, 0 }; ///< Temperature sensors publish time interval override. @@ -58,6 +71,7 @@ static TEMPSENSORS_EXEC_STATES_T handleExecStart( void ); static TEMPSENSORS_EXEC_STATES_T handleExecGetADCValues( void ); +static void filterTemperatureReadings( void ); static void publishTemperatureSensorsData( void ); /*********************************************************************//** @@ -69,9 +83,17 @@ *************************************************************************/ void initTemperature( void ) { - startTime = 0; - tempSensorsExecState = TEMPSENSORS_EXEC_STATE_START; - dataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; + startTime = 0; + tempSensorsExecState = TEMPSENSORS_EXEC_STATE_START; + dataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; + d4TempRunningSumC = 0.0F; + d4TempAvgC = 0.0F; + d4TempSamplesNextIndex = 0; + d4TempCount = 0; + d50TempRunningSumC = 0.0F; + d50TempAvgC = 0.0F; + d50TempSamplesNextIndex = 0; + d50TempCount = 0; // Initialize the temperature sensors initTemperatureSensors(); @@ -144,6 +166,9 @@ // Monitor the temperature values monitorTemperatureSenors(); + // Filter D4/D50 temperature readings + filterTemperatureReadings(); + // Publish the data publishTemperatureSensorsData(); } @@ -194,6 +219,69 @@ /*********************************************************************//** * @brief + * The filterTemperatureReadings function adds a new temperature sensor + * sample to the filters. + * @details \b Inputs: D4 and D50 Temperature + * @details \b Outputs: d4TempSamplesC[], d4TempSamplesNextIndex, d4TempRunningSumC, + * d4TempCount, d4TempAvgC, d50TempSamplesC, d50TempRunningSumC, d50TempSamplesNextIndex, + * d50TempCount, d50TempAvgC + * @return none + *************************************************************************/ +static void filterTemperatureReadings( void ) +{ + // Filter D4 Temperature ( 1sec filter) + if ( d4TempCount >= D4_TEMP_MOVING_AVG_NUM_OF_SAMPLES ) + { + d4TempRunningSumC -= d4TempSamplesC[ d4TempSamplesNextIndex ]; + } + F32 d4Temp = getTemperatureValue( D4_TEMP ); + d4TempSamplesC[ d4TempSamplesNextIndex ] = d4Temp; + d4TempRunningSumC += d4Temp; + d4TempSamplesNextIndex = INC_WRAP( d4TempSamplesNextIndex, 0, D4_TEMP_MOVING_AVG_NUM_OF_SAMPLES - 1 ); + d4TempCount = INC_CAP( d4TempCount, D4_TEMP_MOVING_AVG_NUM_OF_SAMPLES ); + d4TempAvgC = d4TempRunningSumC / (F32)d4TempCount; + + // Filter D50 Temperature ( 250 ms filter ) + if ( d50TempCount >= D50_TEMP_MOVING_AVG_NUM_OF_SAMPLES ) + { + d50TempRunningSumC -= d50TempSamplesC[ d50TempSamplesNextIndex ]; + } + F32 d50Temp = getTemperatureValue( D50_TEMP ); + d50TempSamplesC[ d50TempSamplesNextIndex ] = d50Temp; + d50TempRunningSumC += d50Temp; + d50TempSamplesNextIndex = INC_WRAP( d50TempSamplesNextIndex, 0, D50_TEMP_MOVING_AVG_NUM_OF_SAMPLES - 1 ); + d50TempCount = INC_CAP( d50TempCount, D50_TEMP_MOVING_AVG_NUM_OF_SAMPLES ); + d50TempAvgC = d50TempRunningSumC / (F32)d50TempCount; +} + +/*********************************************************************//** + * @brief + * The getD4AverageTemperature function returns the average temperature + * for D4 temp sensor. + * @details \b Inputs: none + * @details \b Outputs: none + * @return the D4 average temperature + *************************************************************************/ +F32 getD4AverageTemperature( void ) +{ + return d4TempAvgC; +} + +/*********************************************************************//** + * @brief + * The getD50AverageTemperature function returns the average temperature + * for D50 temp sensor. + * @details \b Inputs: none + * @details \b Outputs: none + * @return the D50 average temperature + *************************************************************************/ +F32 getD50AverageTemperature( void ) +{ + return d50TempAvgC; +} + +/*********************************************************************//** + * @brief * The publishTemperatureSensorsData function broadcasts the temperature * sensors data at the publication interval. * @details \b Inputs: dataPublicationTimerCounter and publish interval time. @@ -219,6 +307,8 @@ data.d30CondTemp = getConductivityTemperatureValue( D29_COND ); data.d44CondTemp = getConductivityTemperatureValue( D43_COND ); data.d75CondTemp = getConductivityTemperatureValue( D74_COND ); + data.d4AvgTemp = getD4AverageTemperature(); + data.d50AvgTemp = getD50AverageTemperature(); broadcastData( MSG_ID_DD_TEMPERATURE_DATA, COMM_BUFFER_OUT_CAN_DD_BROADCAST, (U08*)&data, sizeof( TEMPERATURE_SENSORS_DATA_T ) ); dataPublicationTimerCounter = 0;