Index: firmware/App/Monitors/Temperature.c =================================================================== diff -u -rb04db69f541f245e543df343257bcbdb73fbbc3d -r94789b2f2324d5901685b6ff7b6224d4af3a0276 --- firmware/App/Monitors/Temperature.c (.../Temperature.c) (revision b04db69f541f245e543df343257bcbdb73fbbc3d) +++ firmware/App/Monitors/Temperature.c (.../Temperature.c) (revision 94789b2f2324d5901685b6ff7b6224d4af3a0276) @@ -7,8 +7,8 @@ * * @file Temperature.c * -* @author (last) Michael Garthwaite -* @date (last) 07-Apr-2026 +* @author (last) Vinayakam Mani +* @date (last) 14-Apr-2026 * * @author (original) Vinayakam Mani * @date (original) 25-Sep-2024 @@ -40,9 +40,7 @@ #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 50 ///< D4 temperature sensor moving average number of samples. -#define D50_TEMP_MOVING_AVG_NUM_OF_SAMPLES 50 ///< D50 temperature sensor moving average number of samples. -#define D99_TEMP_MOVING_AVG_NUM_OF_SAMPLES 50 ///< D99 temperature sensor moving average number of samples +#define TEMP_MOVING_AVG_NUM_OF_SAMPLES 50 ///< temperature sensor moving average number of samples. #define DATA_PUBLISH_COUNTER_START_COUNT 30 ///< Data publish counter start count. #define DIAL_TEMP_MOVING_AVG_NUM_OF_SAMPLES 30 ///< Dialysate temperature sensors moving average number of samples. #define D28_D30_DATA_COLLECTION_TIME_MS ( 1 * MS_PER_SECOND ) ///< Dialysate temperature sensors data collection time in milliseconds. @@ -67,6 +65,15 @@ NUM_OF_TEMPSENSORS_EXEC_STATES, ///< Total number of exec states } TEMPSENSORS_EXEC_STATES_T; +/// Filter temperature readings record. +typedef struct +{ + F32 temperatureReadings[ TEMP_MOVING_AVG_NUM_OF_SAMPLES ]; ///< Holds temperature sample rolling average. + U32 temperatureReadingsIdx; ///< Index for next sample in rolling average array. + F32 temperatureReadingsTotal; ///< Rolling total - used to calc average. + U32 temperatureReadingsCount; ///< Number of samples in rolling average buffer +} FILTER_TEMPERATURE_READINGS_T; + /// Dialysate temperature moving average structure typedef struct { @@ -85,28 +92,11 @@ static BOOL tempDriftEventCheck; ///< Temperature sensor drift event boolean. static DIAL_TEMP_MOVING_AVG_DATA_T dialTempMovingAvgData[ NUM_OF_DIAL_TEMPS ]; ///< Dialysate temperature moving average data. -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 FILTER_TEMPERATURE_READINGS_T filteredTemperatureReadings[NUM_OF_TEMPERATURE_SENSORS]; ///< Filtered temperature reading for temperature sensors. +static OVERRIDE_F32_T filteredcurrentTemperatureReadings[ NUM_OF_TEMPERATURE_SENSORS ]; ///< filtered current temperature sensor temperature readings (overrideable). static U32 tempDataCollectionTimeInterval; ///< Temperature data collection time interval in milliseconds. -static U32 d4TempSampleIntervalCounter; ///< D4 temperature sensor sample collection timer counter. -static U32 d50TempSampleIntervalCounter; ///< D50 temperature sensor sample collection timer counter. +static U32 TempSampleIntervalCounter; ///< temperature sensor sample collection timer counter. -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 F32 d99TempAvgC; ///< D99 temperature average in C. -static F32 d99TempRunningSumC; ///< D99 temperature running sum in C. -static F32 d99TempSamplesC[ D99_TEMP_MOVING_AVG_NUM_OF_SAMPLES ]; ///< D99 temperature samples array in C. -static U32 d99TempSamplesNextIndex; ///< D99 temperature sample next index number. -static U32 d99TempCount; ///< D99 Number of samples in average buffer. -static U32 d99TempSampleIntervalCounter; ///< D99 temperature sensor sample collection timer counter. - static U32 ddTempDataPublicationTimerCounter; ///< DD Temperature sensors data publish timer counter. static U32 fpTempDataPublicationTimerCounter; ///< FP Temperature sensors data publish timer counter. static OVERRIDE_U32_T ddTempSensorsPublishInterval; ///< DD Temperature sensors publish time interval override. @@ -130,28 +120,30 @@ *************************************************************************/ void initTemperature( void ) { + TEMPERATURE_SENSORS_T sensor; + startTime = 0; tempSensorsExecState = TEMPSENSORS_EXEC_STATE_START; ddTempDataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; fpTempDataPublicationTimerCounter = 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; - d99TempRunningSumC = 0.0F; - d99TempAvgC = 0.0F; - d99TempSamplesNextIndex = 0; - d99TempCount = 0; - d4TempSampleIntervalCounter = 0; - d50TempSampleIntervalCounter = 0; - d99TempSampleIntervalCounter = 0; + + TempSampleIntervalCounter = 0; tempDataCollectionTimeInterval = 0; tempDriftEventCheck = FALSE; + // Initialize override structures for each temperature sensor + for ( sensor = TEMPSENSORS_FIRST; sensor < NUM_OF_TEMPERATURE_SENSORS; sensor++ ) + { + filteredcurrentTemperatureReadings[ sensor ].data = 0.0F; + filteredcurrentTemperatureReadings[ sensor ].ovData = 0.0F; + filteredcurrentTemperatureReadings[ sensor ].ovInitData = 0.0F; + filteredcurrentTemperatureReadings[ sensor ].override = OVERRIDE_RESET; + + filteredTemperatureReadings[ sensor ].temperatureReadingsIdx = 0; + filteredTemperatureReadings[ sensor ].temperatureReadingsTotal = 0.0F; + filteredTemperatureReadings[ sensor ].temperatureReadingsCount = 0; + } + dialTempMovingAvgData[ DIAL_TEMP_D28 ].dialTempColHasTimerBeenSet = FALSE; dialTempMovingAvgData[ DIAL_TEMP_D30 ].dialTempColHasTimerBeenSet = FALSE; @@ -296,67 +288,35 @@ * @brief * The filterTemperatureReadings function adds a new temperature sensor * sample to the filters. - * @details \b Inputs: D4, D50 and D99 Temperature + * @details \b Inputs: D4, D50,D78 and D99 Temperature * @details \b Outputs: d4TempSamplesC[], d4TempSamplesNextIndex, d4TempRunningSumC, * d4TempCount, d4TempAvgC, d50TempSamplesC, d50TempRunningSumC, d50TempSamplesNextIndex, * d50TempCount, d50TempAvgC, d99TempSamplesC, d99TempRunningSumC, d99TempSamplesNextIndex, - * d99TempCount, d99TempAvgC + * d99TempCount, d99TempAvgC, * @return none *************************************************************************/ static void filterTemperatureReadings( void ) { - // Moving average sample collection interval varies based on the dialysate flow rate - if ( ++d4TempSampleIntervalCounter >= tempDataCollectionTimeInterval ) - { - // Filter D4 Temperature for AC heater - if ( d4TempCount >= D4_TEMP_MOVING_AVG_NUM_OF_SAMPLES ) - { - d4TempRunningSumC -= d4TempSamplesC[ d4TempSamplesNextIndex ]; - } + TEMPERATURE_SENSORS_T sensor; + F32 rawTemp = 0.0F; - 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; - d4TempSampleIntervalCounter = 0; - } - - // Moving average sample collection interval varies based on the dialysate flow rate - if ( ++d50TempSampleIntervalCounter >= tempDataCollectionTimeInterval ) + if ( ++TempSampleIntervalCounter >= tempDataCollectionTimeInterval ) { - if ( d50TempCount >= D50_TEMP_MOVING_AVG_NUM_OF_SAMPLES ) + for ( sensor = TEMPSENSORS_FIRST; sensor < NUM_OF_TEMPERATURE_SENSORS; sensor++ ) { - d50TempRunningSumC -= d50TempSamplesC[ d50TempSamplesNextIndex ]; - } + rawTemp = getTemperatureValue ( sensor ); - 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; - d50TempSampleIntervalCounter = 0; - } - - - // Moving average sample collection interval varies based on the dialysate flow rate - if ( ++d99TempSampleIntervalCounter >= tempDataCollectionTimeInterval ) - { - - // Filter D99 Temperature for fresh dialysate temperature - if ( d99TempCount >= D99_TEMP_MOVING_AVG_NUM_OF_SAMPLES ) - { - d99TempRunningSumC -= d99TempSamplesC[ d99TempSamplesNextIndex ]; + if ( filteredTemperatureReadings[ sensor ].temperatureReadingsCount >= TEMP_MOVING_AVG_NUM_OF_SAMPLES ) + { + filteredTemperatureReadings[ sensor ].temperatureReadingsTotal -= filteredTemperatureReadings[ sensor ].temperatureReadings[ filteredTemperatureReadings[ sensor ].temperatureReadingsIdx ]; + } + filteredTemperatureReadings[ sensor ].temperatureReadings[ filteredTemperatureReadings[ sensor ].temperatureReadingsIdx ] = rawTemp; + filteredTemperatureReadings[ sensor ].temperatureReadingsTotal += rawTemp; + filteredTemperatureReadings[ sensor ].temperatureReadingsIdx = INC_WRAP( filteredTemperatureReadings[ sensor ].temperatureReadingsIdx, 0, TEMP_MOVING_AVG_NUM_OF_SAMPLES - 1 ); + filteredTemperatureReadings[ sensor ].temperatureReadingsCount = INC_CAP( filteredTemperatureReadings[ sensor ].temperatureReadingsCount, TEMP_MOVING_AVG_NUM_OF_SAMPLES ); + filteredcurrentTemperatureReadings[ sensor ].data = filteredTemperatureReadings[ sensor ].temperatureReadingsTotal / (F32)filteredTemperatureReadings[ sensor ].temperatureReadingsCount; } - - F32 d99Temp = getTemperatureValue( D99_TEMP ); - d99TempSamplesC[ d99TempSamplesNextIndex ] = d99Temp; - d99TempRunningSumC += d99Temp; - d99TempSamplesNextIndex = INC_WRAP( d99TempSamplesNextIndex, 0, D99_TEMP_MOVING_AVG_NUM_OF_SAMPLES - 1 ); - d99TempCount = INC_CAP( d99TempCount, D99_TEMP_MOVING_AVG_NUM_OF_SAMPLES ); - d99TempAvgC = d99TempRunningSumC / (F32)d99TempCount; + TempSampleIntervalCounter = 0; } // dailysate temperature moving average @@ -449,41 +409,29 @@ /*********************************************************************//** * @brief - * The getD4AverageTemperature function returns the average temperature - * for D4 temp sensor. - * @details \b Inputs: none + * The getFilteredTemperatureValue function gets the filtered current + * Temperature sensor temperature (in deg C) for a given sensor. + * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT if given sensor is invalid. + * @details \b Inputs: filteredcurrentTemperatureReadings * @details \b Outputs: none - * @return the D4 average temperature + * @param sensor ID of temperature sensor to get temperature reading for. + * @return The filtered current temperature sensor temperature (in deg C) of + * the given sensor. *************************************************************************/ -F32 getD4AverageTemperature( void ) +F32 getFilteredTemperatureValue( TEMPERATURE_SENSORS_T sensor ) { - return d4TempAvgC; -} + F32 temperature = 0.0F; -/*********************************************************************//** - * @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; -} + if ( sensor < NUM_OF_TEMPERATURE_SENSORS ) + { + temperature = getF32OverrideValue( &filteredcurrentTemperatureReadings[ sensor ] ); + } + else + { + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DD_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_TEMPERATURE_SENSOR_SELECTED3, sensor ) + } -/*********************************************************************//** - * @brief - * The getD99AverageTemperature function returns the average temperature - * for D99 temperature sensor. - * @details \b Inputs: none - * @details \b Outputs: none - * @return the D99 average temperature - *************************************************************************/ -F32 getD99AverageTemperature( void ) -{ - return d99TempAvgC; + return temperature; } /*********************************************************************//** @@ -499,7 +447,7 @@ { F32 period = (F32)SEC_PER_MIN / ( getTDDialysateFlowrate() / BAL_CHAMBER_FILL_VOLUME_ML ) ; - U32 sampleInterval = (U32)( ( period / (F32)D4_TEMP_MOVING_AVG_NUM_OF_SAMPLES ) * MS_PER_SECOND ); + U32 sampleInterval = (U32)( ( period / (F32)TEMP_MOVING_AVG_NUM_OF_SAMPLES ) * MS_PER_SECOND ); tempDataCollectionTimeInterval = (U32) ( sampleInterval / TASK_PRIORITY_INTERVAL ); } @@ -546,11 +494,12 @@ data.d75CondTemp = getConductivityTemperature( D74_COND ); } - data.d4AvgTemp = getD4AverageTemperature(); - data.d50AvgTemp = getD50AverageTemperature(); - data.d99AvgTemp = getD99AverageTemperature(); + data.d4AvgTemp = getFilteredTemperatureValue( D4_TEMP ); + data.d50AvgTemp = getFilteredTemperatureValue( D50_TEMP ); + data.d99AvgTemp = getFilteredTemperatureValue( D99_TEMP ); data.d28AvgTemp = dialTempMovingAvgData[ DIAL_TEMP_D28 ].dialTempAvgC; data.d30AvgTemp = dialTempMovingAvgData[ DIAL_TEMP_D30 ].dialTempAvgC; + data.d78AvgTemp = getFilteredTemperatureValue( D78_TEMP ); data.d9PresTemp = getFilteredPressureSensorTemperature( D9_PRES ); data.d66PresTemp = getFilteredPressureSensorTemperature( D66_PRES ); data.d51PresTemp = getFilteredPressureSensorTemperature( D51_PRES ); @@ -608,6 +557,30 @@ /*********************************************************************//** * @brief + * The testDDTemperatureSensorFilteredReadingsOverride function overrides the + * filtered value of the specified DD temperature sensor with a given value. + * @details \b Inputs: none + * @details \b Outputs: filteredcurrentTemperatureReadings[] + * @param message Override message from Dialin which includes a sensor + * ID and override value of the temperature value for that sensor. + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testDDTemperatureSensorFilteredReadingsOverride( MESSAGE_T *message ) +{ + BOOL result = FALSE; + TEST_OVERRIDE_ARRAY_PAYLOAD_T payload; + OVERRIDE_TYPE_T ovType = getOverrideArrayPayloadFromMessage( message, &payload ); + + if ( payload.index <= NUM_OF_TEMPERATURE_SENSORS ) + { + result = f32ArrayOverride( message, &filteredcurrentTemperatureReadings[0], NUM_OF_TEMPERATURE_SENSORS - 1 ); + } + + return result; +} + +/*********************************************************************//** + * @brief * The testIOFPTemperatureSensorsDataPublishIntervalOverride function * overrides the IOFP temperature sensors data publish interval. * @details \b Inputs: none