Index: firmware/App/Drivers/ConductivitySensors.c =================================================================== diff -u -r4abd56b1da4a58b11c520f287e155e233d0bbae9 -r6375bf7a63e644d6175a9b89b6fb3df05a763869 --- firmware/App/Drivers/ConductivitySensors.c (.../ConductivitySensors.c) (revision 4abd56b1da4a58b11c520f287e155e233d0bbae9) +++ firmware/App/Drivers/ConductivitySensors.c (.../ConductivitySensors.c) (revision 6375bf7a63e644d6175a9b89b6fb3df05a763869) @@ -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,94 @@ 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 -r6a5f0a5c76ae840ef58e695b5602f9d3bb89f65d -r6375bf7a63e644d6175a9b89b6fb3df05a763869 --- firmware/App/Drivers/ConductivitySensors.h (.../ConductivitySensors.h) (revision 6a5f0a5c76ae840ef58e695b5602f9d3bb89f65d) +++ firmware/App/Drivers/ConductivitySensors.h (.../ConductivitySensors.h) (revision 6375bf7a63e644d6175a9b89b6fb3df05a763869) @@ -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 -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;