Index: firmware/App/Monitors/Temperature.c =================================================================== diff -u -r387ea33c2e45705550905bc1a97e13db1cc95bf8 -rdfe2f0df5623aaea1aebdbf22a512fb919abdf01 --- firmware/App/Monitors/Temperature.c (.../Temperature.c) (revision 387ea33c2e45705550905bc1a97e13db1cc95bf8) +++ firmware/App/Monitors/Temperature.c (.../Temperature.c) (revision dfe2f0df5623aaea1aebdbf22a512fb919abdf01) @@ -1,26 +1,28 @@ /************************************************************************** * -* Copyright (c) 2024-2024 Diality Inc. - All Rights Reserved. +* Copyright (c) 2024-2026 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 Temperature.c * -* @author (last) Vinayakam Mani -* @date (last) 23-Sep-2024 +* @author (last) Michael Garthwaite +* @date (last) 06-Mar-2026 * * @author (original) Vinayakam Mani -* @date (original) 23-Sep-2024 +* @date (original) 25-Sep-2024 * ***************************************************************************/ #include "BalancingChamber.h" -#include "ConductivitySensors.h" +#include "Conductivity.h" +#include "Flow.h" #include "Messaging.h" #include "MessageSupport.h" #include "OperationModes.h" #include "PersistentAlarm.h" +#include "Pressure.h" #include "Temperature.h" #include "Timers.h" #include "TaskPriority.h" @@ -33,12 +35,14 @@ */ // ********** private definitions ********** + #define ADC_FPGA_READ_DELAY 30U ///< Delay in ms before reading the ADC values from FPGA. #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 25 ///< D50 temperature sensor moving average number of samples ( 250ms filter ). +#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 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. @@ -86,19 +90,28 @@ 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 U32 tempDataColTimeInterval; ///< Temperature data collection time interval in milliseconds. +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 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. +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. +static OVERRIDE_U32_T fpTempSensorsPublishInterval; ///< FP Temperature sensors publish time interval override. + // ********** private function prototypes ********** static TEMPSENSORS_EXEC_STATES_T handleExecStart( void ); @@ -117,24 +130,40 @@ *************************************************************************/ void initTemperature( void ) { - 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; - d4TempSampleIntervalCounter = 0; - tempDataColTimeInterval = 0; - tempDriftEventCheck = FALSE; + 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; + tempDataCollectionTimeInterval = 0; + tempDriftEventCheck = FALSE; dialTempMovingAvgData[ DIAL_TEMP_D28 ].dialTempColHasTimerBeenSet = FALSE; dialTempMovingAvgData[ DIAL_TEMP_D30 ].dialTempColHasTimerBeenSet = FALSE; + ddTempSensorsPublishInterval.data = TEMP_SENSORS_DATA_PUBLISH_INTERVAL; + ddTempSensorsPublishInterval.ovData = TEMP_SENSORS_DATA_PUBLISH_INTERVAL; + ddTempSensorsPublishInterval.ovInitData = 0; + ddTempSensorsPublishInterval.override = OVERRIDE_RESET; + + fpTempSensorsPublishInterval.data = TEMP_SENSORS_DATA_PUBLISH_INTERVAL; + fpTempSensorsPublishInterval.ovData = TEMP_SENSORS_DATA_PUBLISH_INTERVAL; + fpTempSensorsPublishInterval.ovInitData = 0; + fpTempSensorsPublishInterval.override = OVERRIDE_RESET; // Initialize the temperature sensors initTemperatureSensors(); @@ -154,6 +183,7 @@ { SELF_TEST_STATUS_T result = SELF_TEST_STATUS_IN_PROGRESS; + //TODO: Uncomment below once Non volatile record is implemented. //BOOL calStatus = getNVRecord2Driver( GET_CAL_TEMP_SENSORS, (U08*)&tempSensorCalRecord, sizeof( DD_TEMP_SENSORS_CAL_RECORD_T ), // NUM_OF_CAL_DATA_TEMP_SENSORS, ALARM_ID_DD_TEMPERATURE_SENSORS_INVALID_CAL_RECORD ); BOOL calStatus = TRUE; @@ -224,7 +254,7 @@ * The handleExecStart function waits for a period of time and switches to * the state that reads the ADC values from FPGA. * @details \b Inputs: startTime - * @details \b Outputs: startTime, baroCoeffsWaitToRcvStartTime + * @details \b Outputs: startTime * @return the next state of the state machine *************************************************************************/ static TEMPSENSORS_EXEC_STATES_T handleExecStart( void ) @@ -239,7 +269,6 @@ else if ( TRUE == didTimeout( startTime, ADC_FPGA_READ_DELAY ) ) { startTime = 0; - setBaroSensorCoefficientReadStartTime(); state = TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES; } @@ -267,16 +296,17 @@ * @brief * The filterTemperatureReadings function adds a new temperature sensor * sample to the filters. - * @details \b Inputs: D4 and D50 Temperature + * @details \b Inputs: D4, D50 and D99 Temperature * @details \b Outputs: d4TempSamplesC[], d4TempSamplesNextIndex, d4TempRunningSumC, * d4TempCount, d4TempAvgC, d50TempSamplesC, d50TempRunningSumC, d50TempSamplesNextIndex, - * d50TempCount, d50TempAvgC + * d50TempCount, d50TempAvgC, d99TempSamplesC, d99TempRunningSumC, d99TempSamplesNextIndex, + * d99TempCount, d99TempAvgC * @return none *************************************************************************/ static void filterTemperatureReadings( void ) { // Moving average sample collection interval varies based on the dialysate flow rate - if ( ++d4TempSampleIntervalCounter >= tempDataColTimeInterval ) + if ( ++d4TempSampleIntervalCounter >= tempDataCollectionTimeInterval ) { // Filter D4 Temperature for AC heater if ( d4TempCount >= D4_TEMP_MOVING_AVG_NUM_OF_SAMPLES ) @@ -293,19 +323,41 @@ d4TempSampleIntervalCounter = 0; } - // Filter D50 Temperature ( 250 ms filter ) for trimmer heater - if ( d50TempCount >= D50_TEMP_MOVING_AVG_NUM_OF_SAMPLES ) + // Moving average sample collection interval varies based on the dialysate flow rate + if ( ++d50TempSampleIntervalCounter >= tempDataCollectionTimeInterval ) { - d50TempRunningSumC -= d50TempSamplesC[ d50TempSamplesNextIndex ]; + 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; + d50TempSampleIntervalCounter = 0; } - 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; + // 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 ]; + } + + 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; + } + // dailysate temperature moving average filterDialTemperatureReadings(); } @@ -321,6 +373,9 @@ static void filterDialTemperatureReadings( void ) { DIAL_TEMPERATURE_SENSORS_T i; + F32 temperatureC = 0.0F; + U32 currentIndex = 0; + F32 prevSampleToRemoveC = 0.0f; for ( i = DIAL_TEMP_FIRST; i < NUM_OF_DIAL_TEMPS; i++ ) { @@ -332,10 +387,19 @@ else if ( TRUE == didTimeout( dialTempMovingAvgData[ i ].dialTempDataColStartTimeMS, D28_D30_DATA_COLLECTION_TIME_MS ) ) { CONDUCTIVITY_SENSORS_T sensor = ( DIAL_TEMP_D28 == i ? D27_COND : D29_COND ); - F32 temperatureC = getConductivityTemperatureValue( sensor ); - U32 currentIndex = dialTempMovingAvgData[ i ].dialTempSamplesNextIndex; - F32 prevSampleToRemoveC = dialTempMovingAvgData[ i ].dialTempSamplesC[ currentIndex ]; + if ( getTestConfigStatus( TEST_CONFIG_DD_FP_ENABLE_BETA_2_0_HW ) != TRUE ) + { + temperatureC = getTeensyConductivityTemperatureValue( sensor ); + } + else + { + temperatureC = getConductivityTemperature( sensor ); + } + + currentIndex = dialTempMovingAvgData[ i ].dialTempSamplesNextIndex; + prevSampleToRemoveC = dialTempMovingAvgData[ i ].dialTempSamplesC[ currentIndex ]; + dialTempMovingAvgData[ i ].dialTempDataColStartTimeMS = getMSTimerCount(); dialTempMovingAvgData[ i ].dialTempColHasTimerBeenSet = TRUE; dialTempMovingAvgData[ i ].dialTempSamplesC[ currentIndex ] = temperatureC; @@ -410,6 +474,19 @@ /*********************************************************************//** * @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; +} + +/*********************************************************************//** + * @brief * The getTempMovingAverageTimeInterval function calculates the temperature * interval used for sample collection based on the dialysate flow rate, * to find the average value. @@ -423,44 +500,86 @@ U32 sampleInterval = (U32)( ( period / (F32)D4_TEMP_MOVING_AVG_NUM_OF_SAMPLES ) * MS_PER_SECOND ); - tempDataColTimeInterval = (U32) ( sampleInterval / TASK_PRIORITY_INTERVAL ); + tempDataCollectionTimeInterval = (U32) ( sampleInterval / TASK_PRIORITY_INTERVAL ); } /*********************************************************************//** * @brief - * The publishTemperatureSensorsData function broadcasts the temperature - * sensors data at the publication interval. - * @details \b Inputs: dataPublicationTimerCounter and publish interval time. - * @details \b Outputs: dataPublicationTimerCounter - * @details \b Message \b Sent: MSG_ID_DD_TEMPERATURE_DATA publishes the temperature - * data in a periodic interval. + * The publishTemperatureSensorsData function broadcasts the + * temperature sensors data at the publication interval. + * @details \b Inputs: ddTempDataPublicationTimerCounter + * fpTempDataPublicationTimerCounter and publish interval time. + * @details \b Outputs: ddTempDataPublicationTimerCounter, fpTempDataPublicationTimerCounter + * @details \b Message \b Sent: MSG_ID_DD_TEMPERATURE_DATA, MSG_ID_FP_TEMPERATURE_DATA + * publishes the temperature data in a periodic interval. * @return none *************************************************************************/ static void publishTemperatureSensorsData( void ) { - if ( ++dataPublicationTimerCounter >= getU32OverrideValue( &tempSensorsPublishInterval ) ) + // publish DD temperature data on interval + if ( ++ddTempDataPublicationTimerCounter >= getU32OverrideValue( &ddTempSensorsPublishInterval ) ) { - TEMPERATURE_SENSORS_DATA_T data; + TEMPERATURE_SENSORS_DD_DATA_T data; data.d1Temp = getTemperatureValue( D1_TEMP ); - data.x6Temp = getTemperatureValue( X6_TEMP ); + data.d78Temp = getTemperatureValue( D78_TEMP ); data.d4Temp = getTemperatureValue( D4_TEMP ); data.d50Temp = getTemperatureValue( D50_TEMP ); + data.d99Temp = getTemperatureValue( D99_TEMP ); data.boardTemp = getTemperatureValue( BRD_TEMP ); - data.baroTemp = getTemperatureValue( BARO_TEMP ); - data.d16CondTemp = getConductivityTemperatureValue( D17_COND ); - data.d28CondTemp = getConductivityTemperatureValue( D27_COND ); - data.d30CondTemp = getConductivityTemperatureValue( D29_COND ); - data.d44CondTemp = getConductivityTemperatureValue( D43_COND ); - data.d75CondTemp = getConductivityTemperatureValue( D74_COND ); + + if ( getTestConfigStatus( TEST_CONFIG_DD_FP_ENABLE_BETA_2_0_HW ) != TRUE ) + { + data.d16CondTemp = getTeensyConductivityTemperatureValue( D17_COND ); + data.d28CondTemp = getTeensyConductivityTemperatureValue( D27_COND ); + data.d30CondTemp = getTeensyConductivityTemperatureValue( D29_COND ); + data.d44CondTemp = getTeensyConductivityTemperatureValue( D43_COND ); + data.d75CondTemp = getTeensyConductivityTemperatureValue( D74_COND ); + } + else + { + data.d16CondTemp = getConductivityTemperature( D17_COND ); + data.d28CondTemp = getConductivityTemperature( D27_COND ); + data.d30CondTemp = getConductivityTemperature( D29_COND ); + data.d44CondTemp = getConductivityTemperature( D43_COND ); + data.d75CondTemp = getConductivityTemperature( D74_COND ); + } + data.d4AvgTemp = getD4AverageTemperature(); data.d50AvgTemp = getD50AverageTemperature(); + data.d99AvgTemp = getD99AverageTemperature(); data.d28AvgTemp = dialTempMovingAvgData[ DIAL_TEMP_D28 ].dialTempAvgC; data.d30AvgTemp = dialTempMovingAvgData[ DIAL_TEMP_D30 ].dialTempAvgC; + data.d9PresTemp = getFilteredPressureSensorTemperature( D9_PRES ); + data.d66PresTemp = getFilteredPressureSensorTemperature( D66_PRES ); + data.d51PresTemp = getFilteredPressureSensorTemperature( D51_PRES ); + data.d18PresTemp = getFilteredPressureSensorTemperature( D18_PRES ); + data.d41PresTemp = getFilteredPressureSensorTemperature( D41_PRES ); + data.d87PresTemp = getFilteredPressureSensorTemperature( D87_PRES ); - broadcastData( MSG_ID_DD_TEMPERATURE_DATA, COMM_BUFFER_OUT_CAN_DD_BROADCAST, (U08*)&data, sizeof( TEMPERATURE_SENSORS_DATA_T ) ); - dataPublicationTimerCounter = 0; + broadcastData( MSG_ID_DD_TEMPERATURE_DATA, COMM_BUFFER_OUT_CAN_DD_BROADCAST, (U08*)&data, sizeof( TEMPERATURE_SENSORS_DD_DATA_T ) ); + ddTempDataPublicationTimerCounter = 0; } + // publish FP temperature data on interval + if ( ++fpTempDataPublicationTimerCounter >= getU32OverrideValue( &fpTempSensorsPublishInterval ) ) + { + TEMPERATURE_SENSORS_FP_DATA_T data; + + data.m3Temp = getFilteredPressureSensorTemperature( M3_PRES ); + data.p8Temp = getFilteredPressureSensorTemperature( P8_PRES ); + data.p13Temp = getFilteredPressureSensorTemperature( P13_PRES ); + data.p17Temp = getFilteredPressureSensorTemperature( P17_PRES ); + data.p46Temp = getFilteredPressureSensorTemperature( P46_PRES ); + data.p10Temp = getFilteredConductivitySensorTemperature( P9_COND ); + data.p19Temp = getFilteredConductivitySensorTemperature( P18_COND ); + data.p7Temp = getFilteredFlowSensorTemperature( P7_FLOW ); + data.p16Temp = getFilteredFlowSensorTemperature( P16_FLOW ); + data.p7InternalTemp = getFlowInternalTemperature( P7_FLOW ); + data.p16InternalTemp = getFlowInternalTemperature( P16_FLOW ); + + broadcastData( MSG_ID_FP_TEMPERATURE_DATA, COMM_BUFFER_OUT_CAN_FP_BROADCAST, (U08*)&data, sizeof( TEMPERATURE_SENSORS_FP_DATA_T ) ); + fpTempDataPublicationTimerCounter = 0; + } } @@ -471,20 +590,36 @@ /*********************************************************************//** * @brief - * The testTemperatureSensorsDataPublishIntervalOverride function overrides the - * temperature sensor data publish interval. + * The testDDTemperatureSensorsDataPublishIntervalOverride function + * overrides the DD temperature sensor data publish interval. * @details \b Inputs: none - * @details \b Outputs: tempSensorsPublishInterval + * @details \b Outputs: ddTempSensorsPublishInterval * @param Override message from Dialin which includes the interval - * (in ms) to override the temperature sensor data broadcast interval to. + * (in ms) to override the DD temperature sensor data broadcast interval to. * @return TRUE if override successful, FALSE if not *************************************************************************/ -BOOL testTemperatureSensorsDataPublishIntervalOverride( MESSAGE_T *message ) +BOOL testDDTemperatureSensorsDataPublishIntervalOverride( MESSAGE_T *message ) { - BOOL result = u32BroadcastIntervalOverride( message, &tempSensorsPublishInterval, TASK_PRIORITY_INTERVAL ); + BOOL result = u32BroadcastIntervalOverride( message, &ddTempSensorsPublishInterval, TASK_PRIORITY_INTERVAL ); return result; } +/*********************************************************************//** + * @brief + * The testIOFPTemperatureSensorsDataPublishIntervalOverride function + * overrides the IOFP temperature sensors data publish interval. + * @details \b Inputs: none + * @details \b Outputs: fpTempSensorsPublishInterval + * @param Override message from Dialin which includes the interval + * (in ms) to override the IOFP temperature sensor data broadcast interval to. + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testIOFPTemperatureSensorsDataPublishIntervalOverride( MESSAGE_T *message ) +{ + BOOL result = u32BroadcastIntervalOverride( message, &fpTempSensorsPublishInterval, TASK_PRIORITY_INTERVAL ); + return result; +} + /**@}*/