Index: firmware/App/Drivers/ConductivitySensors.c =================================================================== diff -u -re2681bed3a45f89c804ddf2ae607fc416bd61d5d -rb91d956a773bcf1319f816f36b270b31c68948de --- firmware/App/Drivers/ConductivitySensors.c (.../ConductivitySensors.c) (revision e2681bed3a45f89c804ddf2ae607fc416bd61d5d) +++ firmware/App/Drivers/ConductivitySensors.c (.../ConductivitySensors.c) (revision b91d956a773bcf1319f816f36b270b31c68948de) @@ -33,28 +33,32 @@ // ********** private definitions ********** -#define CELL_COEFF_LOW_BOUNDS 3 ///< Low bounds for cell coefficient values. -#define CELL_COEFF_HIGH_BOUNDS 6 ///< High bounds for cell coefficient values. -#define RESISTANCE_OFFSET_LOW_BOUNDS -1000 ///< Low bounds for resistance offset values. -#define RESISTANCE_OFFSET_HIGH_BOUNDS 1000 ///< High bounds for resistance offset values. +#define CELL_COEFF_LOW_BOUNDS 3.0F ///< Low bounds for cell coefficient values. +#define CELL_COEFF_HIGH_BOUNDS 6.0F ///< High bounds for cell coefficient values. +#define RESISTANCE_OFFSET_LOW_BOUNDS -1000.0F ///< Low bounds for resistance offset values. +#define RESISTANCE_OFFSET_HIGH_BOUNDS 1000.0F ///< High bounds for resistance offset values. #define RESISTANCE_SCALE_LOW_BOUNDS 0.5F ///< Low bounds for resistance scale values. #define RESISTANCE_SCALE_HIGH_BOUNDS 3.0F ///< High bounds for resistance scale values. #define TEMPERATURE_CORRECTION_LOW_BOUNDS 0.005F ///< Low bounds for temperature correction values. #define TEMPERATURE_CORRECTION_HIGH_BOUNDS 0.04F ///< High bounds for temperature correction values. -#define TEMPERATURE_OFFSET_LOW_BOUNDS -10 ///< Low bounds for temperature scale values. -#define TEMPERATURE_OFFSET_HIGH_BOUNDS 10 ///< High bounds for temperature offset values. +#define TEMPERATURE_OFFSET_LOW_BOUNDS -10.0F ///< Low bounds for temperature scale values. +#define TEMPERATURE_OFFSET_HIGH_BOUNDS 10.0F ///< High bounds for temperature offset values. #define TEMPERATURE_SCALE_LOW_BOUNDS 0.5F ///< Low bounds for temperature scale values. #define TEMPERATURE_SCALE_HIGH_BOUNDS 2.0F ///< High bounds for temperature scale values. + #define LOWER_WORD_SIZE 4 ///< Size in bytes of the lower word size for retrieving revision data. #define MID_WORD_SIZE 4 ///< Size in bytes of the middle word size for retrieving revision data. -#define UPPER_WORD_SIZE 2 ///< Size in bytes of the upper word size for retrieving revision data. +#define UPPER_WORD_SIZE 4 ///< Size in bytes of the upper word size for retrieving revision data. + #define MAX_CONDUCTIVITY_SENSOR_FAILURES 2 ///< Number of failures before alarming in timed window count. #define MAX_CONDUCTIVITY_SENSOR_FAILURE_WINDOW_MS ( 60 * MS_PER_SECOND ) ///< Set time for timed window count. #define MAX_ALLOWED_UNCHANGED_CONDUCTIVITY_READS ( 500 / TASK_PRIORITY_INTERVAL ) ///< New reading every 333ms, expect to get valid new reading in 500ms. -#define SIEMENS_TO_MICROSIEMENS_CONVERSION 1000000 ///< Siemens to microSiemens conversion factor. #define COND_SENSORS_FPGA_ERROR_TIMEOUT_MS ( 2 * MS_PER_SECOND ) ///< Conductivity sensors FPGA error timeout in milliseconds. #define COND_SENSORS_READ_ERR_MAX_CNT 255 ///< Conductivity sensors read and error count max value. +#define COND_TEMP_OFFSET 25 ///< Temperature offset constant used in RTD calculations. +#define SIEMENS_TO_MICROSIEMENS_CONVERSION 1000000 ///< Siemens to microSiemens conversion factor. + /// Defined states for the conductivity write transaction. typedef enum Conductvity_States { @@ -66,6 +70,10 @@ /// Conductivity Sensor Status group typedef struct { + F32 rawTemperature; ///< raw Temperature in C from the conductivity sensor. + F32 rawResistance; ///< raw Resistance in Ohms from the conductivity sensor. + F32 calculatedTemperature; + F32 calculatedResistance; U08 condReadCount; ///< Last conductivity read count. U08 condErrorCount; ///< Last conductivity error count. U08 tempReadCount; ///< Last temperature read count. @@ -102,6 +110,9 @@ static U32 getTemperatureSensorErrorCount( CONDUCTIVITY_SENSORS_T sensor ); static BOOL monitorCalDataReads( U32 sensorId ); static BOOL checkConductivityCoefficientRanges( U32 sensorId ); +static void calculateConductivityUpdatedStandard( CONDUCTIVITY_SENSORS_T sensorNum, BOOL isFPSensor ); +static void calculateTemperature( CONDUCTIVITY_SENSORS_T sensorNum ); +static void calculateResistance( CONDUCTIVITY_SENSORS_T sensorNum, BOOL isFPSensor ); /*********************************************************************//** * @brief @@ -334,7 +345,7 @@ { CONDUCTIVITY_STATE_T state = CONDUCTIVITY_INIT_STATE; CONDUCTIVITY_SENSORS_T sensor; - BOOL calResult = TRUE; + BOOL calResult = FALSE; readConductivitySensorCalData(); @@ -347,7 +358,7 @@ switch( conductivitySensorStatus[ sensor ].calMemCount ) { case CAL_DATA_1: - conductivitySensorCoefficients[ sensor ].K_high = (U32)conductivitySensorStatus[ sensor ].calData; + conductivitySensorCoefficients[ sensor ].K_high = (F32)conductivitySensorStatus[ sensor ].calData; conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ CAL_DATA_1 ] = TRUE; break; case CAL_DATA_2: @@ -359,11 +370,11 @@ conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ CAL_DATA_3 ] = TRUE; break; case CAL_DATA_4: - conductivitySensorCoefficients[ sensor ].zeta_high = (S32)conductivitySensorStatus[ sensor ].calData; + conductivitySensorCoefficients[ sensor ].zeta_high = (F32)conductivitySensorStatus[ sensor ].calData; conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ CAL_DATA_4 ] = TRUE; break; case CAL_DATA_5: - conductivitySensorCoefficients[ sensor ].K_low = (U32)conductivitySensorStatus[ sensor ].calData; + conductivitySensorCoefficients[ sensor ].K_low = (F32)conductivitySensorStatus[ sensor ].calData; conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ CAL_DATA_5 ] = TRUE; break; case CAL_DATA_6: @@ -375,23 +386,23 @@ conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ CAL_DATA_7 ] = TRUE; break; case CAL_DATA_8: - conductivitySensorCoefficients[ sensor ].zeta_low = (S32)conductivitySensorStatus[ sensor ].calData; + conductivitySensorCoefficients[ sensor ].zeta_low = (F32)conductivitySensorStatus[ sensor ].calData; conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ CAL_DATA_8 ] = TRUE; break; case CAL_DATA_9: conductivitySensorCoefficients[ sensor ].beta = (F32)conductivitySensorStatus[ sensor ].calData; conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ CAL_DATA_9 ] = TRUE; break; case CAL_DATA_10: - conductivitySensorCoefficients[ sensor ].delta = (S32)conductivitySensorStatus[ sensor ].calData; + conductivitySensorCoefficients[ sensor ].delta = (F32)conductivitySensorStatus[ sensor ].calData; conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ CAL_DATA_10 ] = TRUE; break; case CAL_DATA_11: - conductivitySensorCoefficients[ sensor ].reserved1 = (U32)conductivitySensorStatus[ sensor ].calData; + conductivitySensorCoefficients[ sensor ].reserved1 = (F32)conductivitySensorStatus[ sensor ].calData; conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ CAL_DATA_11 ] = TRUE; break; case CAL_DATA_12: - conductivitySensorCoefficients[ sensor ].reserved2 = (U32)conductivitySensorStatus[ sensor ].calData; + conductivitySensorCoefficients[ sensor ].reserved2 = (F32)conductivitySensorStatus[ sensor ].calData; conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ CAL_DATA_12 ] = TRUE; break; case SW_REV_LOWER_WORD: @@ -403,7 +414,7 @@ conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ SW_REV_MID_WORD ] = TRUE; break; case SW_REV_UPPER_WORD: - memcpy( ( conductivitySensorRevisions[ sensor ].swRev + ( LOWER_WORD_SIZE + MID_WORD_SIZE ) ), &conductivitySensorStatus[ sensor ].calData, sizeof( U16 ) ); + memcpy( ( conductivitySensorRevisions[ sensor ].swRev + ( LOWER_WORD_SIZE + MID_WORD_SIZE ) ), &conductivitySensorStatus[ sensor ].calData, sizeof( U32 ) ); conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ SW_REV_UPPER_WORD ] = TRUE; break; case HW_REV_LOWER_WORD: @@ -415,7 +426,7 @@ conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ SW_REV_MID_WORD ] = TRUE; break; case HW_REV_UPPER_WORD: - memcpy( ( conductivitySensorRevisions[ sensor ].hwRev + ( LOWER_WORD_SIZE + MID_WORD_SIZE ) ), &conductivitySensorStatus[ sensor ].calData, sizeof( U16 ) ); + memcpy( ( conductivitySensorRevisions[ sensor ].hwRev + ( LOWER_WORD_SIZE + MID_WORD_SIZE ) ), &conductivitySensorStatus[ sensor ].calData, sizeof( U32 ) ); conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ SW_REV_UPPER_WORD ] = TRUE; break; case HW_SERIAL_LOWER_WORD: @@ -427,7 +438,7 @@ conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ SW_REV_MID_WORD ] = TRUE; break; case HW_SERIAL_UPPER_WORD: - memcpy( ( conductivitySensorRevisions[ sensor ].hwRev + ( LOWER_WORD_SIZE + MID_WORD_SIZE ) ), &conductivitySensorStatus[ sensor ].calData, sizeof( U16 ) ); + memcpy( ( conductivitySensorRevisions[ sensor ].hwRev + ( LOWER_WORD_SIZE + MID_WORD_SIZE ) ), &conductivitySensorStatus[ sensor ].calData, sizeof( U32 ) ); conductivitySensorStatus[ sensor ].hasCalSlotBeenRead[ SW_REV_UPPER_WORD ] = TRUE; break; } @@ -439,9 +450,9 @@ { conductivitySensorStatus[ sensor ].calReadComplete = checkConductivityCoefficientRanges( sensor ); - if ( FALSE == conductivitySensorStatus[ sensor ].calReadComplete ) + if ( TRUE == conductivitySensorStatus[ sensor ].calReadComplete ) { - calResult = FALSE; + calResult = TRUE; } } } @@ -465,6 +476,7 @@ static CONDUCTIVITY_STATE_T handleConductivityReadDataState( void ) { CONDUCTIVITY_STATE_T state = CONDUCTIVITY_READ_DATA_STATE; + BOOL isFPSensor = FALSE; CONDUCTIVITY_SENSORS_T sensor; readConductivitySensorData(); @@ -475,6 +487,14 @@ { checkCondCounters( sensor, getConductivitySensorReadCount( sensor ), getConductivitySensorErrorCount( sensor ), getTemperatureSensorReadCount( sensor ), getTemperatureSensorErrorCount( sensor ) ); + + if ( ( sensor >= FIRST_FP_COND_SENSOR ) && + ( sensor <= LAST_FP_COND_SENSOR ) ) + { + isFPSensor = TRUE; + } + + calculateConductivityUpdatedStandard( sensor, isFPSensor ); } return state; } @@ -821,7 +841,95 @@ return result; } +/*********************************************************************//** + * @brief + * The calculateConductivityUpdatedStandard function calculates the conductivity value. + * @details \b Inputs: conductivitySensorCoefficients - Conductivity Coefficients + * @details \b Inputs: conductivitySensorStatus - Raw measurement values + * @details \b Outputs: conductivitySensorStatus - calculated conductivity value + * @param sensorNum - Conductivity sensor index value. + * @param isFPSensor - T/F if sensor is on FP hardware. + * @return + *************************************************************************/ +static void calculateConductivityUpdatedStandard( CONDUCTIVITY_SENSORS_T sensorNum, BOOL isFPSensor ) +{ + F64 calculatedConductivity = 0.0; + F64 alpha = 0.0; + F64 k = 0.0; + if ( TRUE == isFPSensor ) + { + alpha = conductivitySensorCoefficients[ sensorNum ].alpha_low; + k = conductivitySensorCoefficients[ sensorNum ].K_low; + } + else + { + alpha = conductivitySensorCoefficients[ sensorNum ].alpha_high; + k = conductivitySensorCoefficients[ sensorNum ].K_high; + } + calculateResistance( sensorNum, isFPSensor ); + calculateTemperature( sensorNum ); + + calculatedConductivity = ( ( k / conductivitySensorStatus[ sensorNum ].calculatedResistance ) * + ( 1 + alpha * ( COND_TEMP_OFFSET - conductivitySensorStatus[ sensorNum ].calculatedTemperature ) ) ); + currentConductivityReadings[ sensorNum ].data = calculatedConductivity; +} + +/*********************************************************************//** + * @brief + * The calculateResistance function calculates the temperature values. + * @details \b Inputs: conductivitySensorCoefficients - Conductivity Coefficients + * @details \b Inputs: conductivitySensorStatus - Raw measurement values + * @details \b Outputs: conductivitySensorStatus - calculated resistance value + * @param sensorNum - Conductivity sensor index value. + * @param isFPSensor - T/F if sensor is on FP hardware. + * @return + *************************************************************************/ +static void calculateResistance( CONDUCTIVITY_SENSORS_T sensorNum, BOOL isFPSensor ) +{ + + F32 calculatedResistance = 0.0; + F32 eta = 0.0; + F32 zeta = 0.0; + F32 R = conductivitySensorStatus[ sensorNum ].rawResistance; + + if ( TRUE == isFPSensor ) + { + eta = conductivitySensorCoefficients[ sensorNum ].eta_low; + zeta = conductivitySensorCoefficients[ sensorNum ].zeta_low; + } + else + { + eta = conductivitySensorCoefficients[ sensorNum ].eta_high; + zeta = conductivitySensorCoefficients[ sensorNum ].zeta_high; + } + calculatedResistance = ( ( eta * R ) + zeta ); + conductivitySensorStatus[ sensorNum ].calculatedResistance = calculatedResistance; +} + +/*********************************************************************//** + * @brief + * The calculateTemperature function calculates the temperature values. + * @details \b Inputs: conductivitySensorCoefficients - Conductivity Coefficients + * @details \b Inputs: conductivitySensorStatus - Raw measurement values + * @details \b Outputs: conductivitySensorStatus - calculated temperature value + * @param sensorNum - Conductivity sensor index value. + * @return + *************************************************************************/ +static void calculateTemperature( CONDUCTIVITY_SENSORS_T sensorNum ) +{ + + F32 calculatedTemperature = 0.0; + F32 beta = conductivitySensorCoefficients[ sensorNum ].beta; + F32 gamma = conductivitySensorCoefficients[ sensorNum ].delta; + F32 Z = conductivitySensorStatus[ sensorNum ].rawTemperature; + + calculatedTemperature = ( ( beta * Z ) + gamma ); + conductivitySensorStatus[ sensorNum ].calculatedTemperature = calculatedTemperature; + currentTemperatureReadings[ sensorNum ].data = calculatedTemperature; +} + + /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ Index: firmware/App/Drivers/ConductivitySensors.h =================================================================== diff -u -r8a7e671214ef3f742677f6de58b2b0ea86e8fe5c -rb91d956a773bcf1319f816f36b270b31c68948de --- firmware/App/Drivers/ConductivitySensors.h (.../ConductivitySensors.h) (revision 8a7e671214ef3f742677f6de58b2b0ea86e8fe5c) +++ firmware/App/Drivers/ConductivitySensors.h (.../ConductivitySensors.h) (revision b91d956a773bcf1319f816f36b270b31c68948de) @@ -36,7 +36,7 @@ */ // ********** public definitions ********** -#define CONDUCTIVITY_CAL_CHAR_LENGTH 10 +#define CONDUCTIVITY_CAL_CHAR_LENGTH 12 /// Enumeration of conductivity sensors. typedef enum ConductivitySensors @@ -90,18 +90,18 @@ } CONDUCTIVITY_CAL_IDX_T; typedef struct { - U32 K_high; ///< (cell coefficient, high range), default = 4, range = [3, 6] + F32 K_high; ///< (cell coefficient, high range), default = 4, range = [3, 6] F32 alpha_high; ///< (temperature correction, high range), default = 0.02, range = [0.005, 0.04] F32 eta_high; ///< (resistance scale factor, high range), default = 1, range = [0.5, 3] - S32 zeta_high; ///< (resistance offset, high range), default = 0, range = [-1000, 1000] - U32 K_low; ///< (cell coefficient, low range), default = 4, range = [3, 6] + F32 zeta_high; ///< (resistance offset, high range), default = 0, range = [-1000, 1000] + F32 K_low; ///< (cell coefficient, low range), default = 4, range = [3, 6] F32 alpha_low; ///< (temperature correction, low range), default = 0.02, range = [0.005, 0.04] F32 eta_low; ///< (resistance scale factor, low range), default = 1, range = [0.5, 3] - S32 zeta_low; ///< (resistance offset, low range), default = 0, range = [-1000, 1000] + F32 zeta_low; ///< (resistance offset, low range), default = 0, range = [-1000, 1000] F32 beta; ///< (Temperature scale factor), default = 1, range = [0.5, 2] - S32 delta; ///< (Temperature offset), default = 0, range = [-10, 10] - U32 reserved1; ///< Reserved cal slot. - U32 reserved2; ///< Reserved cal slot. + F32 delta; ///< (Temperature offset), default = 0, range = [-10, 10] + F32 reserved1; ///< Reserved cal slot. + F32 reserved2; ///< Reserved cal slot. } CONDUCTIVITY_COEFFICIENTS_T; typedef struct Index: firmware/App/Monitors/Conductivity.c =================================================================== diff -u -rff93c14403f911ff4f28815c8c89f87395079123 -rb91d956a773bcf1319f816f36b270b31c68948de --- firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision ff93c14403f911ff4f28815c8c89f87395079123) +++ firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision b91d956a773bcf1319f816f36b270b31c68948de) @@ -178,17 +178,8 @@ #ifdef __TEENSY_CONDUCTIVITY_DRIVER__ execConductivityTeensy(); #else - readConductivitySensorData(); + execConductivitySensors(); #endif - //control conductivity sensor - // TODO : need more clarity on why and when to execute following control. -#if 0 - handleConductivitySensorsReset(); - handleConductivitySensorsInitProcedure(); - execConductivitySensorWrite(); - execConductivitySensorRead(); -#endif - filterConductivitySensors(); calcRORejectionRatio(); // TODO: should this be called here or inside filter function filterRORejectionRatioReadings(); @@ -259,7 +250,7 @@ #ifdef __TEENSY_CONDUCTIVITY_DRIVER__ F32 rawCond = getTeensyConductivityValue( sensor ); #else - F32 rawCond = getConductivityValue( sensor ); + F32 calculatedConductivity = getConductivityValue( sensor ); #endif // TODO - calibrate @@ -268,8 +259,8 @@ { filteredConductivityReadings[ sensor ].conductivityReadingsTotal -= filteredConductivityReadings[ sensor ].conductivityReadings[ filteredConductivityReadings[ sensor ].conductivityReadingsIdx ]; } - filteredConductivityReadings[ sensor ].conductivityReadings[ filteredConductivityReadings[ sensor ].conductivityReadingsIdx ] = rawCond; - filteredConductivityReadings[ sensor ].conductivityReadingsTotal += rawCond; + 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; @@ -321,9 +312,9 @@ for ( sensor = FIRST_DD_COND_SENSOR; sensor < NUM_OF_CONDUCTIVITY_SENSORS; sensor++ ) { #ifdef __TEENSY_CONDUCTIVITY_DRIVER__ - F32 rawTemp = getTeensyConductivityTemperatureValue( sensor ); + F32 calculatedTemperature = getTeensyConductivityTemperatureValue( sensor ); #else - F32 rawTemp = getConductivityTemperatureValue( sensor ); + F32 calculatedTemperature = getConductivityTemperatureValue( sensor ); #endif // TODO - calibrate @@ -332,8 +323,8 @@ { filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsTotal -= filteredConductivityTemperatureReadings[sensor].conductivityTempReadings[ filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsIdx ]; } - filteredConductivityTemperatureReadings[sensor].conductivityTempReadings[ filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsIdx ] = rawTemp; - filteredConductivityTemperatureReadings[sensor].conductivityTempReadingsTotal += rawTemp; + 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;