Index: firmware/App/Monitors/Conductivity.c =================================================================== diff -u -rccebe8df730dba8ad2d11e244a60788002a73b3b -r3d0c86bab8540914d9f3cfdbdd321f35db623999 --- firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision ccebe8df730dba8ad2d11e244a60788002a73b3b) +++ firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision 3d0c86bab8540914d9f3cfdbdd321f35db623999) @@ -8,7 +8,7 @@ * @file Conductivity.c * * @author (last) Michael Garthwaite -* @date (last) 06-Mar-2026 +* @date (last) 15-Apr-2026 * * @author (original) Vinayakam Mani * @date (original) 13-Sep-2024 @@ -35,8 +35,8 @@ #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 ( 30 ) ///< Filter conductivity data for given time -#define CONDUCTIVITY_TEMP_SAMPLE_FILTER_MS ( 30 ) ///< 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 +63,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. @@ -107,11 +109,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; @@ -152,6 +151,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; @@ -182,19 +184,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 @@ -260,34 +251,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_COND_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_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; } } @@ -332,34 +338,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_COND_TEMP_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_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; } } @@ -703,4 +724,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; +} /**@}*/