Index: firmware/App/Monitors/Conductivity.c =================================================================== diff -u -r2b0862b34518c0ce835fe5cb5bb3460c8316d269 -r6375bf7a63e644d6175a9b89b6fb3df05a763869 --- firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision 2b0862b34518c0ce835fe5cb5bb3460c8316d269) +++ firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision 6375bf7a63e644d6175a9b89b6fb3df05a763869) @@ -33,10 +33,9 @@ // ********** private definitions ********** #define COND_SENSOR_REPORT_PERIOD ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Broadcast conductivity values message every second. -#define COND_SENSOR_UPDATE_INTERVAL ( 700 / TASK_PRIORITY_INTERVAL ) ///< Time in task intervals for new sensor data #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 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. @@ -63,9 +62,11 @@ // ********** 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. @@ -82,10 +83,6 @@ static U32 roRRCount; ///< RO rejection ratio Number of samples in average buffer. static F32 roRRTankFillAvg; ///< Average RO rejection ratio during permeate tank fill state. static U32 roRRSampleIntervalCounter; ///< RO rejection ratio sample collection timer counter. -static U32 condtempDataCollectionTimeInterval; ///< Conductivity Temperature data collection time interval in task counts. -static U32 condtempSampleIntervalCounter; ///< Conductivity Temperature sensor sample collection timer counter. -static U32 condDataCollectionTimeInterval; ///< Conductivity data collection time interval in task counts. -static U32 condSampleIntervalCounter; ///< Conductivity sensor sample collection timer counter. // ********** private function prototypes ********** @@ -124,10 +121,6 @@ roRRAvg.ovInitData = 0.0F; roRRAvg.override = OVERRIDE_RESET; roRRSampleIntervalCounter = 0; - condtempDataCollectionTimeInterval= COND_SENSOR_UPDATE_INTERVAL; - condtempSampleIntervalCounter = 0; - condDataCollectionTimeInterval = COND_SENSOR_UPDATE_INTERVAL; - condSampleIntervalCounter = 0; memset( &roRRSamples, 0, sizeof( roRRSamples ) ); @@ -151,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; @@ -249,23 +245,38 @@ { CONDUCTIVITY_SENSORS_T sensor; F32 calculatedConductivity = 0.0F; + BOOL freshData = FALSE; - if ( ++condSampleIntervalCounter >= condDataCollectionTimeInterval ) + for ( sensor = FIRST_DD_COND_SENSOR; sensor < NUM_OF_CONDUCTIVITY_SENSORS; sensor++ ) { - 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 ( 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 + { + // 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; + } + } + // 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 ) + { // TODO - calibrate - if ( filteredConductivityReadings[ sensor ].conductivityReadingsCount >= SIZE_OF_COND_ROLLING_AVG ) { filteredConductivityReadings[ sensor ].conductivityReadingsTotal -= filteredConductivityReadings[ sensor ].conductivityReadings[ filteredConductivityReadings[ sensor ].conductivityReadingsIdx ]; @@ -276,9 +287,7 @@ filteredConductivityReadings[ sensor ].conductivityReadingsCount = INC_CAP( filteredConductivityReadings[ sensor ].conductivityReadingsCount, SIZE_OF_COND_ROLLING_AVG ); filteredcurrentConductivityReadings[ sensor ].data = filteredConductivityReadings[ sensor ].conductivityReadingsTotal / (F32)filteredConductivityReadings[ sensor ].conductivityReadingsCount; } - condSampleIntervalCounter = 0; } - } /*********************************************************************//** @@ -323,37 +332,49 @@ { CONDUCTIVITY_SENSORS_T sensor; F32 calculatedTemperature = 0.0F; + BOOL freshData = FALSE; - if ( ++condtempSampleIntervalCounter >= condtempDataCollectionTimeInterval ) + for ( sensor = FIRST_DD_COND_SENSOR; sensor < NUM_OF_CONDUCTIVITY_SENSORS; sensor++ ) { - for ( sensor = FIRST_DD_COND_SENSOR; sensor < NUM_OF_CONDUCTIVITY_SENSORS; sensor++ ) + if ( getTestConfigStatus( TEST_CONFIG_DD_FP_ENABLE_BETA_2_0_HW ) != TRUE ) { - // Filter conductivity temp - if ( filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsCount >= SIZE_OF_COND_TEMP_ROLLING_AVG ) + if ( sensor != D74_COND ) { - filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsTotal -= filteredConductivityTemperatureReadings[sensor].conductivityTempReadings[ filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsIdx ]; - } - - if ( getTestConfigStatus( TEST_CONFIG_DD_FP_ENABLE_BETA_2_0_HW ) != TRUE ) - { calculatedTemperature = getTeensyConductivityTemperatureValue( sensor ); + freshData = TRUE; } else { calculatedTemperature = getConductivityTemperature( sensor ); + freshData = TRUE; } + } + else + { + // 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; + } + } + // 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 ) + { + // 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; - } - - condtempSampleIntervalCounter = 0; } - } /*********************************************************************//** @@ -488,9 +509,9 @@ data.d17Cond = getFilteredConductivity( D17_COND ); data.d27Cond = getFilteredConductivity( D27_COND ); - data.d29Cond = getConductivity( D17_COND ); + data.d29Cond = getFilteredConductivity( D29_COND ); data.d43Cond = getFilteredConductivity( D43_COND ); - data.d74Cond = getConductivity( D27_COND ); + data.d74Cond = getFilteredConductivity( D74_COND ); ddConductivityPublishTimerCounter = 0; broadcastData( MSG_ID_DD_CONDUCTIVITY_DATA, COMM_BUFFER_OUT_CAN_DD_BROADCAST, (U08*)&data, sizeof( DD_CONDUCTIVITY_DATA_T ) ); @@ -501,7 +522,7 @@ { FP_CONDUCTIVITY_DATA_T data; - data.p9Conductivity = getConductivity( D43_COND ); + data.p9Conductivity = getFilteredConductivity( P9_COND ); data.p18Conductivity = getFilteredConductivity( P18_COND ); fpConductivityPublishTimerCounter = 0;