Index: firmware/App/Drivers/ConductivitySensors.c =================================================================== diff -u -rb04db69f541f245e543df343257bcbdb73fbbc3d -r20d45b98f5e2de8c6d3b9da7d74c5a9db87a6618 --- firmware/App/Drivers/ConductivitySensors.c (.../ConductivitySensors.c) (revision b04db69f541f245e543df343257bcbdb73fbbc3d) +++ firmware/App/Drivers/ConductivitySensors.c (.../ConductivitySensors.c) (revision 20d45b98f5e2de8c6d3b9da7d74c5a9db87a6618) @@ -74,7 +74,7 @@ F32 rawTemperature; ///< Raw Temperature in C from the conductivity sensor. F32 rawResistance; ///< Raw Resistance in kOhms from the conductivity sensor. F32 calculatedTemperature; ///< Calculated temperature in C. - F32 calculatedResistance; ///< Calculated resistance in Ohms + F32 calculatedResistance; ///< Calculated resistance in Ohms. U08 condReadCount; ///< Last conductivity read count. U08 condErrorCount; ///< Last conductivity error count. U08 tempReadCount; ///< Last temperature read count. @@ -972,7 +972,97 @@ currentTemperatureReadings[ sensorNum ].data = calculatedTemperature; } +/*********************************************************************//** + * @brief + * The getConductivityReadCount function gets the conductivity read count + * for a given conductivity sensor id. + * @details \b Inputs: FPGA + * @details \b Outputs: none + * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT if given sensor is invalid. + * @param sensorId conductivity sensor id + * @return last known read count. + *************************************************************************/ +U08 getConductivityReadCount( CONDUCTIVITY_SENSORS_T sensor ) +{ + U08 readCount = 0; + switch( sensor ) + { + case D17_COND: + readCount = getFPGAD17CondReadCount(); + break; + case D27_COND: + readCount = getFPGAD27CondReadCount(); + break; + case D29_COND: + readCount = getFPGAD29CondReadCount(); + break; + case D43_COND: + readCount = getFPGAD43CondReadCount(); + break; + case D74_COND: + readCount = getFPGAD74CondReadCount(); + break; + case P9_COND: + readCount = getFPGAP9CondReadCount(); + break; + case P18_COND: + readCount = getFPGAP18CondReadCount(); + break; + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DD_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_CONDUCTIVITY_SENSOR_ID1, sensor ) + break; + } + + return readCount; +} + +/*********************************************************************//** + * @brief + * The getConductivityTemperatureReadCount function gets the temperature + * read count for a given conductivity sensor id. + * @details \b Inputs: FPGA + * @details \b Outputs: none + * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT if given sensor is invalid. + * @param sensorId conductivity sensor id + * @return last known read count. + *************************************************************************/ +U08 getConductivityTemperatureReadCount( CONDUCTIVITY_SENSORS_T sensor ) +{ + U08 readCount = 0; + + switch( sensor ) + { + case D17_COND: + readCount = getFPGAD17TempReadCount(); + break; + case D27_COND: + readCount = getFPGAD27TempReadCount(); + break; + case D29_COND: + readCount = getFPGAD29TempReadCount(); + break; + case D43_COND: + readCount = getFPGAD43TempReadCount(); + break; + case D74_COND: + readCount = getFPGAD74TempReadCount(); + break; + case P9_COND: + readCount = getFPGAP9TempReadCount(); + break; + case P18_COND: + readCount = getFPGAP18TempReadCount(); + break; + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DD_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_CONDUCTIVITY_SENSOR_ID1, sensor ) + break; + } + + return readCount; +} + + /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ Index: firmware/App/Drivers/ConductivitySensors.h =================================================================== diff -u -rb04db69f541f245e543df343257bcbdb73fbbc3d -r20d45b98f5e2de8c6d3b9da7d74c5a9db87a6618 --- firmware/App/Drivers/ConductivitySensors.h (.../ConductivitySensors.h) (revision b04db69f541f245e543df343257bcbdb73fbbc3d) +++ firmware/App/Drivers/ConductivitySensors.h (.../ConductivitySensors.h) (revision 20d45b98f5e2de8c6d3b9da7d74c5a9db87a6618) @@ -105,6 +105,8 @@ F32 getConductivity( CONDUCTIVITY_SENSORS_T sensor ); F32 getConductivityTemperature( CONDUCTIVITY_SENSORS_T sensor ); +U08 getConductivityReadCount( CONDUCTIVITY_SENSORS_T sensor ); +U08 getConductivityTemperatureReadCount( CONDUCTIVITY_SENSORS_T sensor ); BOOL testConductivitySensorConductivityReadingsOverride( MESSAGE_T *message ); BOOL testConductivitySensorTemperatureReadingsOverride( MESSAGE_T *message ); Index: firmware/App/Monitors/Conductivity.c =================================================================== diff -u -rb04db69f541f245e543df343257bcbdb73fbbc3d -r20d45b98f5e2de8c6d3b9da7d74c5a9db87a6618 --- firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision b04db69f541f245e543df343257bcbdb73fbbc3d) +++ firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision 20d45b98f5e2de8c6d3b9da7d74c5a9db87a6618) @@ -34,18 +34,18 @@ #define COND_SENSOR_REPORT_PERIOD ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Broadcast conductivity values message every second. #define DATA_PUBLISH_COUNTER_START_COUNT 40 ///< Data publish counter start count. -#define CONDUCTIVITY_SAMPLE_FILTER_MS ( 500 ) ///< Filter conductivity data for given time -#define CONDUCTIVITY_TEMP_SAMPLE_FILTER_MS ( 500 ) ///< Filter conductivity temperature data for given time -#define SIZE_OF_FLOW_ROLLING_AVG ( CONDUCTIVITY_SAMPLE_FILTER_MS / TASK_PRIORITY_INTERVAL ) ///< Filtered conductivity moving average sample count. -#define SIZE_OF_FLOW_TEMP_ROLLING_AVG ( CONDUCTIVITY_TEMP_SAMPLE_FILTER_MS / TASK_PRIORITY_INTERVAL ) ///< Filtered conductivity temprature moving average sample count. +#define CONDUCTIVITY_SAMPLE_FILTER_MS ( 50 ) ///< Filter conductivity data for given time. Currently set to have 5 samples over 3.5s ( 700ms sample rate ) +#define CONDUCTIVITY_TEMP_SAMPLE_FILTER_MS ( 50 ) ///< Filter conductivity temperature data for given time. Currently set to have 5 samples over 3.5s ( 700ms sample rate ) +#define SIZE_OF_COND_ROLLING_AVG ( CONDUCTIVITY_SAMPLE_FILTER_MS / TASK_PRIORITY_INTERVAL ) ///< Filtered conductivity moving average sample count. +#define SIZE_OF_COND_TEMP_ROLLING_AVG ( CONDUCTIVITY_TEMP_SAMPLE_FILTER_MS / TASK_PRIORITY_INTERVAL ) ///< Filtered conductivity temprature moving average sample count. #define RO_RR_MOVING_AVG_NUM_OF_SAMPLES 30 ///< RO rejection ratio moving average number of samples. #define FRACTION_TO_PERCENT_CONVERSION_FACTOR 100.0F ///< RO rejection ratio factor to percentage conversion factor value #define RO_RR_SAMPLE_COLLECTION_INTERVAL 10 ///< Collect RO rejection ratio sample for every 10th time in priority task /// Filter conductivity readings record. typedef struct { - F32 conductivityReadings[ SIZE_OF_FLOW_ROLLING_AVG ]; ///< Holds conductivity sample rolling average. + F32 conductivityReadings[ SIZE_OF_COND_ROLLING_AVG ]; ///< Holds conductivity sample rolling average. U32 conductivityReadingsIdx; ///< Index for next sample in rolling average array. F32 conductivityReadingsTotal; ///< Rolling total - used to calc average. U32 conductivityReadingsCount; ///< Number of samples in rolling average buffer @@ -54,17 +54,19 @@ /// Filter conductivity sensor temperature readings record. typedef struct { - F32 conductivityTempReadings[ SIZE_OF_FLOW_ROLLING_AVG ]; ///< Holds conductivity sample rolling average. + F32 conductivityTempReadings[ SIZE_OF_COND_TEMP_ROLLING_AVG ]; ///< Holds conductivity sample rolling average. U32 conductivityTempReadingsIdx; ///< Index for next sample in rolling average array. F32 conductivityTempReadingsTotal; ///< Rolling total - used to calc average. U32 conductivityTempReadingsCount; ///< Number of samples in rolling average buffer } FILTER_CONDUCTIVITY_TEMPERATURE_READINGS_T; // ********** private data ********** -static FILTER_CONDUCTIVITY_READINGS_T filteredConductivityReadings[NUM_OF_CONDUCTIVITY_SENSORS]; ///< Filtered conductivity reading for conductivity sensors. +static U08 condPrevReadCount[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Previous read count for conductivity per sensor +static U08 condTempPrevReadCount[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Previous read count for temperature per sensor. +static FILTER_CONDUCTIVITY_READINGS_T filteredConductivityReadings[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Filtered conductivity reading for conductivity sensors. static OVERRIDE_F32_T filteredcurrentConductivityReadings[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< filtered current conductivity sensor conductivity readings (overrideable). -static FILTER_CONDUCTIVITY_TEMPERATURE_READINGS_T filteredConductivityTemperatureReadings[NUM_OF_CONDUCTIVITY_SENSORS]; ///< Filtered temperature reading for conductivity sensors. +static FILTER_CONDUCTIVITY_TEMPERATURE_READINGS_T filteredConductivityTemperatureReadings[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Filtered temperature reading for conductivity sensors. static OVERRIDE_F32_T filteredcurrentTemperatureReadings[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< filtered current conductivity sensor temperature readings (overrideable). static U32 ddConductivityPublishTimerCounter; ///< DD Conductivity data publication counter. static OVERRIDE_U32_T ddConductivityDataPublishInterval; ///< DD Conductivity sensors publish time interval override. @@ -142,6 +144,9 @@ filteredConductivityTemperatureReadings[ sensor ].conductivityTempReadingsIdx = 0; filteredConductivityTemperatureReadings[ sensor ].conductivityTempReadingsTotal = 0.0F; filteredConductivityTemperatureReadings[ sensor ].conductivityTempReadingsCount = 0; + + condPrevReadCount[ sensor ] = 0; + condTempPrevReadCount[ sensor ] = 0; } ddConductivityDataPublishInterval.data = COND_SENSOR_REPORT_PERIOD; @@ -240,37 +245,48 @@ { CONDUCTIVITY_SENSORS_T sensor; F32 calculatedConductivity = 0.0F; + BOOL freshData = FALSE; for ( sensor = FIRST_DD_COND_SENSOR; sensor < NUM_OF_CONDUCTIVITY_SENSORS; sensor++ ) { - if ( getTestConfigStatus( TEST_CONFIG_DD_FP_ENABLE_BETA_2_0_HW ) != TRUE ) { if ( sensor != D74_COND ) { calculatedConductivity = getTeensyConductivityValue( sensor ); + freshData = TRUE; } else { calculatedConductivity = getConductivity( sensor ); + freshData = TRUE; } } else { - calculatedConductivity = getConductivity( sensor ); + // Check FPGA read counts for freshness rather than wait on fix time. + if ( condPrevReadCount[ sensor ] != getConductivityReadCount( sensor ) ) + { + calculatedConductivity = getConductivity( sensor ); + condPrevReadCount[ sensor ] = getConductivityReadCount( sensor ); + freshData = TRUE; + } } - // TODO - calibrate - - if ( filteredConductivityReadings[ sensor ].conductivityReadingsCount >= SIZE_OF_FLOW_ROLLING_AVG ) + // Apply sample to filter if we know it's fresh, v2/v3 only. V1's will always update filter regardless of freshness. + if ( TRUE == freshData ) { - filteredConductivityReadings[ sensor ].conductivityReadingsTotal -= filteredConductivityReadings[ sensor ].conductivityReadings[ filteredConductivityReadings[ sensor ].conductivityReadingsIdx ]; + // TODO - calibrate + if ( filteredConductivityReadings[ sensor ].conductivityReadingsCount >= SIZE_OF_COND_ROLLING_AVG ) + { + filteredConductivityReadings[ sensor ].conductivityReadingsTotal -= filteredConductivityReadings[ sensor ].conductivityReadings[ filteredConductivityReadings[ sensor ].conductivityReadingsIdx ]; + } + filteredConductivityReadings[ sensor ].conductivityReadings[ filteredConductivityReadings[ sensor ].conductivityReadingsIdx ] = calculatedConductivity; + filteredConductivityReadings[ sensor ].conductivityReadingsTotal += calculatedConductivity; + filteredConductivityReadings[ sensor ].conductivityReadingsIdx = INC_WRAP( filteredConductivityReadings[ sensor ].conductivityReadingsIdx, 0, SIZE_OF_COND_ROLLING_AVG - 1 ); + filteredConductivityReadings[ sensor ].conductivityReadingsCount = INC_CAP( filteredConductivityReadings[ sensor ].conductivityReadingsCount, SIZE_OF_COND_ROLLING_AVG ); + filteredcurrentConductivityReadings[ sensor ].data = filteredConductivityReadings[ sensor ].conductivityReadingsTotal / (F32)filteredConductivityReadings[ sensor ].conductivityReadingsCount; } - filteredConductivityReadings[ sensor ].conductivityReadings[ filteredConductivityReadings[ sensor ].conductivityReadingsIdx ] = calculatedConductivity; - filteredConductivityReadings[ sensor ].conductivityReadingsTotal += calculatedConductivity; - filteredConductivityReadings[ sensor ].conductivityReadingsIdx = INC_WRAP( filteredConductivityReadings[ sensor ].conductivityReadingsIdx, 0, SIZE_OF_FLOW_ROLLING_AVG - 1 ); - filteredConductivityReadings[ sensor ].conductivityReadingsCount = INC_CAP( filteredConductivityReadings[ sensor ].conductivityReadingsCount, SIZE_OF_FLOW_ROLLING_AVG ); - filteredcurrentConductivityReadings[ sensor ].data = filteredConductivityReadings[ sensor ].conductivityReadingsTotal / (F32)filteredConductivityReadings[ sensor ].conductivityReadingsCount; } } @@ -316,29 +332,48 @@ { CONDUCTIVITY_SENSORS_T sensor; F32 calculatedTemperature = 0.0F; + BOOL freshData = FALSE; for ( sensor = FIRST_DD_COND_SENSOR; sensor < NUM_OF_CONDUCTIVITY_SENSORS; sensor++ ) { if ( getTestConfigStatus( TEST_CONFIG_DD_FP_ENABLE_BETA_2_0_HW ) != TRUE ) { - calculatedTemperature = getTeensyConductivityTemperatureValue( sensor ); + if ( sensor != D74_COND ) + { + calculatedTemperature = getTeensyConductivityTemperatureValue( sensor ); + freshData = TRUE; + } + else + { + calculatedTemperature = getConductivityTemperature( sensor ); + freshData = TRUE; + } } else { - calculatedTemperature = getConductivityTemperature( sensor ); + // Check FPGA read counts for freshness rather than wait on fix time. + if ( condTempPrevReadCount[ sensor ] != getConductivityTemperatureReadCount( sensor ) ) + { + calculatedTemperature = getConductivityTemperature( sensor ); + condTempPrevReadCount[ sensor ] = getConductivityTemperatureReadCount( sensor ); + freshData = TRUE; + } } - // TODO - calibrate - - if ( filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsCount >= SIZE_OF_FLOW_ROLLING_AVG ) + // Apply sample to filter if we know it's fresh, v2/v3 only. V1's will always update filter regardless of freshness. + if ( TRUE == freshData ) { - filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsTotal -= filteredConductivityTemperatureReadings[sensor].conductivityTempReadings[ filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsIdx ]; + // TODO - calibrate + if ( filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsCount >= SIZE_OF_COND_TEMP_ROLLING_AVG ) + { + filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsTotal -= filteredConductivityTemperatureReadings[sensor].conductivityTempReadings[ filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsIdx ]; + } + filteredConductivityTemperatureReadings[sensor].conductivityTempReadings[ filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsIdx ] = calculatedTemperature; + filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsTotal += calculatedTemperature; + filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsIdx = INC_WRAP( filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsIdx, 0, SIZE_OF_COND_TEMP_ROLLING_AVG - 1 ); + filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsCount = INC_CAP( filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsCount, SIZE_OF_COND_TEMP_ROLLING_AVG ); + filteredcurrentTemperatureReadings[sensor].data = filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsTotal / (F32)filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsCount; } - filteredConductivityTemperatureReadings[sensor].conductivityTempReadings[ filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsIdx ] = calculatedTemperature; - filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsTotal += calculatedTemperature; - filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsIdx = INC_WRAP( filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsIdx, 0, SIZE_OF_FLOW_ROLLING_AVG - 1 ); - filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsCount = INC_CAP( filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsCount, SIZE_OF_FLOW_ROLLING_AVG ); - filteredcurrentTemperatureReadings[sensor].data = filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsTotal / (F32)filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsCount; } }