Index: firmware/App/Monitors/Conductivity.c =================================================================== diff -u -r019e570cacd748a39dfbd500fe5bd65f596f488c -r20d45b98f5e2de8c6d3b9da7d74c5a9db87a6618 --- firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision 019e570cacd748a39dfbd500fe5bd65f596f488c) +++ firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision 20d45b98f5e2de8c6d3b9da7d74c5a9db87a6618) @@ -8,7 +8,7 @@ * @file Conductivity.c * * @author (last) Michael Garthwaite -* @date (last) 06-Mar-2026 +* @date (last) 09-Apr-2026 * * @author (original) Vinayakam Mani * @date (original) 13-Sep-2024 @@ -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. @@ -102,11 +104,8 @@ { CONDUCTIVITY_SENSORS_T sensor; -#ifdef __TEENSY_CONDUCTIVITY_DRIVER__ initConductivityTeensy(); -#else initConductivitySensors(); -#endif ddConductivityPublishTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; fpConductivityPublishTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; @@ -145,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; @@ -175,19 +177,8 @@ void execConductivity( void ) { //read conductivity sensors raw value -#ifdef __TEENSY_CONDUCTIVITY_DRIVER__ execConductivityTeensy(); -#else - readConductivitySensors(); -#endif - //control conductivity sensor - // TODO : need more clarity on why and when to execute following control. -#if 0 - handleConductivitySensorsReset(); - handleConductivitySensorsInitProcedure(); - execConductivitySensorWrite(); - execConductivitySensorRead(); -#endif + execConductivitySensors(); filterConductivitySensors(); calcRORejectionRatio(); // TODO: should this be called here or inside filter function @@ -253,34 +244,49 @@ static void filterConductivitySensorReadings( void ) { CONDUCTIVITY_SENSORS_T sensor; - F32 rawCond = 0.0F; + F32 calculatedConductivity = 0.0F; + BOOL freshData = FALSE; for ( sensor = FIRST_DD_COND_SENSOR; sensor < NUM_OF_CONDUCTIVITY_SENSORS; sensor++ ) { -#ifdef __TEENSY_CONDUCTIVITY_DRIVER__ - if (sensor != D74_COND ) + if ( getTestConfigStatus( TEST_CONFIG_DD_FP_ENABLE_BETA_2_0_HW ) != TRUE ) { - rawCond = getTeensyConductivityValue( sensor ); + if ( sensor != D74_COND ) + { + calculatedConductivity = getTeensyConductivityValue( sensor ); + freshData = TRUE; + } + else + { + calculatedConductivity = getConductivity( sensor ); + freshData = TRUE; + } } else { - rawCond = getConductivityValue( 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; + } } -#else - rawCond = getConductivityValue( sensor ); -#endif - // 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 ] = rawCond; - filteredConductivityReadings[ sensor ].conductivityReadingsTotal += rawCond; - 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; } } @@ -325,34 +331,49 @@ static void filterConductivitySensorTemperatureReadings( void ) { CONDUCTIVITY_SENSORS_T sensor; - F32 rawTemp = 0.0F; + F32 calculatedTemperature = 0.0F; + BOOL freshData = FALSE; for ( sensor = FIRST_DD_COND_SENSOR; sensor < NUM_OF_CONDUCTIVITY_SENSORS; sensor++ ) { -#ifdef __TEENSY_CONDUCTIVITY_DRIVER__ - if (sensor != D74_COND ) + if ( getTestConfigStatus( TEST_CONFIG_DD_FP_ENABLE_BETA_2_0_HW ) != TRUE ) { - rawTemp = getTeensyConductivityTemperatureValue( sensor ); + if ( sensor != D74_COND ) + { + calculatedTemperature = getTeensyConductivityTemperatureValue( sensor ); + freshData = TRUE; + } + else + { + calculatedTemperature = getConductivityTemperature( sensor ); + freshData = TRUE; + } } else { - rawTemp = getConductivityTemperatureValue( 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; + } } -#else - rawTemp = getConductivityTemperatureValue( sensor ); -#endif - // 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 ] = rawTemp; - filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsTotal += rawTemp; - 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; } } @@ -696,4 +717,77 @@ return result; } +/*********************************************************************//** + * @brief + * The testConductivitySensorReadingsOverride function overrides the value of the + * specified conductivity sensor with a given value and determines which driver + * override. + * @details \b Inputs: testConfig + * @details \b Outputs: none + * @param message Override message from Dialin which includes an sensor + * ID and override value of the conductivity sensor. + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testConductivitySensorReadingsOverride( MESSAGE_T *message ) +{ + BOOL result = FALSE; + TEST_OVERRIDE_ARRAY_PAYLOAD_T payload; + OVERRIDE_TYPE_T ovType = getOverrideArrayPayloadFromMessage( message, &payload ); + + if ( getTestConfigStatus( TEST_CONFIG_DD_FP_ENABLE_BETA_2_0_HW ) != TRUE ) + { + + if ( payload.index != D74_COND ) + { + result = testTeensyConductivitySensorReadingsOverride( message ); + } + else + { + result = testConductivitySensorReadingsOverride( message ); + } + } + else + { + result = testConductivitySensorConductivityReadingsOverride( message ); + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testConductivitySensorTemperatureOverride function overrides the value of the + * specified conductivity sensor with a given value and determines which driver + * override. + * @details \b Inputs: testConfig + * @details \b Outputs: none + * @param message Override message from Dialin which includes an sensor + * ID and override value of the conductivity sensor. + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testConductivitySensorTemperatureOverride( MESSAGE_T *message ) +{ + BOOL result = FALSE; + TEST_OVERRIDE_ARRAY_PAYLOAD_T payload; + OVERRIDE_TYPE_T ovType = getOverrideArrayPayloadFromMessage( message, &payload ); + + if ( getTestConfigStatus( TEST_CONFIG_DD_FP_ENABLE_BETA_2_0_HW ) != TRUE ) + { + + if ( payload.index != D74_COND ) + { + result = testTeensyConductivitySensorTemperatureReadingsOverride( message ); + } + else + { + result = testConductivitySensorTemperatureReadingsOverride( message ); + } + } + else + { + result = testConductivitySensorTemperatureReadingsOverride( message ); + } + + return result; +} /**@}*/