Index: firmware/App/Monitors/Temperature.c =================================================================== diff -u -r0df32171c99e44512f8ea90ab2068e25ef4bcce2 -r1781335a8c1833fad17b275bf44c7f6675e68423 --- firmware/App/Monitors/Temperature.c (.../Temperature.c) (revision 0df32171c99e44512f8ea90ab2068e25ef4bcce2) +++ firmware/App/Monitors/Temperature.c (.../Temperature.c) (revision 1781335a8c1833fad17b275bf44c7f6675e68423) @@ -15,13 +15,16 @@ * ***************************************************************************/ +#include "BalancingChamber.h" #include "ConductivitySensors.h" #include "Messaging.h" #include "MessageSupport.h" #include "OperationModes.h" +#include "PersistentAlarm.h" #include "Temperature.h" #include "Timers.h" #include "TaskPriority.h" +#include "TDInterface.h" #include "Utilities.h" /** @@ -34,10 +37,24 @@ #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 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 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. +#define D28_D30_TEMP_SENSORS_MAX_DEVIATION_C 3.0F ///< Dialysate temperature sensors maximum allowed deviation in C. +#define DIALYSATE_TEMP_SNSRS_OUT_OF_RANGE_TIMEOUT_MS ( 10 * MS_PER_SECOND ) ///< Dialysate temperature sensors drift timeout in milliseconds. +#define D28_D30_TEMP_SENSORS_MAX_DEVIATION_IN_HEAT_DIS_C 5.0F ///< Dialysate temperature sensors maximum allowed deviation in heat disinfect in C. +/// Dialysate temperature sensors enums +typedef enum Dial_Temps_Sensors +{ + DIAL_TEMP_D28 = 0, ///< Dialysate temperature D28. + DIAL_TEMP_FIRST = DIAL_TEMP_D28, ///< Dialysate temperature first. + DIAL_TEMP_D30, ///< Dialysate temperature D30. + NUM_OF_DIAL_TEMPS ///< Number of Dialysate temperature sensors. +} DIAL_TEMPERATURE_SENSORS_T; + /// Temperature sensor exec states. typedef enum tempSensors_Exec_States { @@ -46,16 +63,31 @@ NUM_OF_TEMPSENSORS_EXEC_STATES, ///< Total number of exec states } TEMPSENSORS_EXEC_STATES_T; +/// Dialysate temperature moving average structure +typedef struct +{ + BOOL dialTempColHasTimerBeenSet; ///< Dialysate temperature has data collection timer started boolean flag. + U32 dialTempDataColStartTimeMS; ///< Dialysate temperature data collection start time in milliseconds. + F32 dialTempRunningSumC; ///< Dialysate temperature running sum in C. + F32 dialTempAvgC; ///< Dialysate temperature average in C. + F32 dialTempSamplesC[ DIAL_TEMP_MOVING_AVG_NUM_OF_SAMPLES ]; ///< Dialysate temperature samples array in C. + U32 dialTempSamplesNextIndex; ///< Dialysate temperature sample next index number. +} DIAL_TEMP_MOVING_AVG_DATA_T; + // ********** private data ********** static TEMPSENSORS_EXEC_STATES_T tempSensorsExecState; ///< TemperatureSensor exec state. static U32 startTime; ///< start time to read FPGA values. +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 U32 tempDataColStartTimeMS; ///< Temperature data collection start time in milliseconds. +static U32 tempDataColTimeInterval; ///< Temperature data collection time interval in milliseconds. static F32 d50TempAvgC; ///< D50 temperature average in C. static F32 d50TempRunningSumC; ///< D50 temperature running sum in C. @@ -72,6 +104,8 @@ static TEMPSENSORS_EXEC_STATES_T handleExecStart( void ); static TEMPSENSORS_EXEC_STATES_T handleExecGetADCValues( void ); static void filterTemperatureReadings( void ); +static void filterDialTemperatureReadings( void ); +static void getTempMovingAverageTimeInterval( void ); static void publishTemperatureSensorsData( void ); /*********************************************************************//** @@ -83,6 +117,8 @@ *************************************************************************/ void initTemperature( void ) { + DIAL_TEMPERATURE_SENSORS_T j; + startTime = 0; tempSensorsExecState = TEMPSENSORS_EXEC_STATE_START; dataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; @@ -94,9 +130,26 @@ d50TempAvgC = 0.0F; d50TempSamplesNextIndex = 0; d50TempCount = 0; + tempDataColStartTimeMS = 0; + tempDataColTimeInterval = 0; + tempDriftEventCheck = FALSE; + for ( j = DIAL_TEMP_FIRST; j < NUM_OF_DIAL_TEMPS; j++ ) + { + dialTempMovingAvgData[ j ].dialTempAvgC = 0.0F; + dialTempMovingAvgData[ j ].dialTempColHasTimerBeenSet = FALSE; + dialTempMovingAvgData[ j ].dialTempRunningSumC = 0.0F; + dialTempMovingAvgData[ j ].dialTempSamplesNextIndex = 0; + dialTempMovingAvgData[ j ].dialTempDataColStartTimeMS = getMSTimerCount(); + + memset( dialTempMovingAvgData[ j ].dialTempSamplesC, 0.0F, sizeof( F32 ) * DIAL_TEMP_MOVING_AVG_NUM_OF_SAMPLES ); + } + // Initialize the temperature sensors initTemperatureSensors(); + + // Persistent alarm for the temperature sensors range check + initPersistentAlarm( ALARM_ID_DD_DIALYSATE_TEMPERATURE_SENSORS_OUT_OF_RANGE, 0, DIALYSATE_TEMP_SNSRS_OUT_OF_RANGE_TIMEOUT_MS ); } /*********************************************************************//** @@ -166,6 +219,9 @@ // Monitor the temperature values monitorTemperatureSenors(); + //Update the timeinterval + getTempMovingAverageTimeInterval(); + // Filter D4/D50 temperature readings filterTemperatureReadings(); @@ -229,19 +285,28 @@ *************************************************************************/ static void filterTemperatureReadings( void ) { - // Filter D4 Temperature ( 1sec filter) - if ( d4TempCount >= D4_TEMP_MOVING_AVG_NUM_OF_SAMPLES ) + // Moving average sample collection interval varies based on the dialysate flow rate + if ( 0 == tempDataColStartTimeMS ) { - d4TempRunningSumC -= d4TempSamplesC[ d4TempSamplesNextIndex ]; + tempDataColStartTimeMS = getMSTimerCount(); } - 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; + else if ( TRUE == didTimeout( tempDataColStartTimeMS , tempDataColTimeInterval ) ) + { + // Filter D4 Temperature for AC heater + 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; + tempDataColStartTimeMS = getMSTimerCount(); + } - // Filter D50 Temperature ( 250 ms filter ) + // Filter D50 Temperature ( 250 ms filter ) for trimmer heater if ( d50TempCount >= D50_TEMP_MOVING_AVG_NUM_OF_SAMPLES ) { d50TempRunningSumC -= d50TempSamplesC[ d50TempSamplesNextIndex ]; @@ -252,10 +317,85 @@ 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; + + // dailysate temperature moving average + filterDialTemperatureReadings(); } /*********************************************************************//** * @brief + * The filterDialTemperatureReadings function adds a new dialysate temperature + * sensor sample to the filters. + * @details \b Inputs: dialysate temperature + * @details \b Outputs: dialTempMovingAvgData[] + * @return none + *************************************************************************/ +static void filterDialTemperatureReadings( void ) +{ + DIAL_TEMPERATURE_SENSORS_T i; + + for ( i = DIAL_TEMP_FIRST; i < NUM_OF_DIAL_TEMPS; i++ ) + { + if ( FALSE == dialTempMovingAvgData[ i ].dialTempColHasTimerBeenSet ) + { + dialTempMovingAvgData[ i ].dialTempDataColStartTimeMS = getMSTimerCount(); + dialTempMovingAvgData[ i ].dialTempColHasTimerBeenSet = TRUE; + } + 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 ]; + + dialTempMovingAvgData[ i ].dialTempDataColStartTimeMS = getMSTimerCount(); + dialTempMovingAvgData[ i ].dialTempColHasTimerBeenSet = TRUE; + dialTempMovingAvgData[ i ].dialTempSamplesC[ currentIndex ] = temperatureC; + dialTempMovingAvgData[ i ].dialTempRunningSumC = dialTempMovingAvgData[ i ].dialTempRunningSumC + temperatureC - prevSampleToRemoveC; + dialTempMovingAvgData[ i ].dialTempSamplesNextIndex = INC_WRAP( dialTempMovingAvgData[ i ].dialTempSamplesNextIndex, 0, DIAL_TEMP_MOVING_AVG_NUM_OF_SAMPLES - 1 ); + dialTempMovingAvgData[ i ].dialTempAvgC = dialTempMovingAvgData[ i ].dialTempRunningSumC / (F32)DIAL_TEMP_MOVING_AVG_NUM_OF_SAMPLES; + } + } +} + +/*********************************************************************//** + * @brief + * The checkDialysateTemperatureSensors function checks whether the + * dialysate temperature sensors have drifted. If they are drifted, it raises + * an alarm. + * @details \b Inputs: dialysate temperature + * @details \b Outputs: None + * @return none + *************************************************************************/ +void checkDialysateTemperatureSensors( void ) +{ + DD_OP_MODE_T op = getCurrentOperationMode(); + + if ( ( DD_MODE_GEND == op ) || ( DD_MODE_HEAT == op ) ) + { + F32 d28Temp = dialTempMovingAvgData[ DIAL_TEMP_D28 ].dialTempAvgC; + F32 d30Temp = dialTempMovingAvgData[ DIAL_TEMP_D30 ].dialTempAvgC; + F32 driftC = ( DD_MODE_HEAT == op ? D28_D30_TEMP_SENSORS_MAX_DEVIATION_IN_HEAT_DIS_C : D28_D30_TEMP_SENSORS_MAX_DEVIATION_C ); + BOOL isDriftOut = ( fabs( d28Temp - d30Temp ) > driftC ? TRUE : FALSE ); + + checkPersistentAlarm( ALARM_ID_DD_DIALYSATE_TEMPERATURE_SENSORS_OUT_OF_RANGE, isDriftOut, fabs( d28Temp - d30Temp ), driftC ); + + if ( ( FALSE == tempDriftEventCheck ) && ( TRUE == isDriftOut ) ) + { + // Send event only condition trigger, not continuously. + tempDriftEventCheck = TRUE; + SEND_EVENT_WITH_2_U32_DATA(DD_EVENT_TEMPERATURE_DRIFT, d28Temp, d30Temp) + } + + if ( ( TRUE == tempDriftEventCheck ) && ( FALSE == isDriftOut ) ) + { + tempDriftEventCheck = FALSE; + } + } +} + +/*********************************************************************//** + * @brief * The getD4AverageTemperature function returns the average temperature * for D4 temp sensor. * @details \b Inputs: none @@ -282,6 +422,22 @@ /*********************************************************************//** * @brief + * The getTempMovingAverageTimeInterval function calculates the temperature + * interval used for sample collection based on the dialysate flow rate, + * to find the average value. + * @details \b Inputs: dialysate flow rate + * @details \b Outputs: none + * @return the temperature interval for sample collection + *************************************************************************/ +static void getTempMovingAverageTimeInterval( void ) +{ + F32 period = (F32)SEC_PER_MIN / ( getTDDialysateFlowrate() / BAL_CHAMBER_FILL_VOLUME_ML ) ; + + tempDataColTimeInterval = (U32)( ( period / (F32)D4_TEMP_MOVING_AVG_NUM_OF_SAMPLES ) * MS_PER_SECOND ); +} + +/*********************************************************************//** + * @brief * The publishTemperatureSensorsData function broadcasts the temperature * sensors data at the publication interval. * @details \b Inputs: dataPublicationTimerCounter and publish interval time. @@ -309,6 +465,8 @@ data.d75CondTemp = getConductivityTemperatureValue( D74_COND ); data.d4AvgTemp = getD4AverageTemperature(); data.d50AvgTemp = getD50AverageTemperature(); + data.d28AvgTemp = dialTempMovingAvgData[ DIAL_TEMP_D28 ].dialTempAvgC; + data.d30AvgTemp = dialTempMovingAvgData[ DIAL_TEMP_D30 ].dialTempAvgC; broadcastData( MSG_ID_DD_TEMPERATURE_DATA, COMM_BUFFER_OUT_CAN_DD_BROADCAST, (U08*)&data, sizeof( TEMPERATURE_SENSORS_DATA_T ) ); dataPublicationTimerCounter = 0;