Index: firmware/App/Controllers/ConductivitySensors.c =================================================================== diff -u -r30fd485858736c08d8bc4fe8bcb94cf1b545492e -r7965487decd9984a22f4d9ec56d1fb2590d1583b --- firmware/App/Controllers/ConductivitySensors.c (.../ConductivitySensors.c) (revision 30fd485858736c08d8bc4fe8bcb94cf1b545492e) +++ firmware/App/Controllers/ConductivitySensors.c (.../ConductivitySensors.c) (revision 7965487decd9984a22f4d9ec56d1fb2590d1583b) @@ -28,22 +28,40 @@ // ********** private definitions ********** -#define COND_SENSOR_PROBE_TYPE 1000 -#define COND_SENSOR_TEMPERATURE_COEF 0.02 -#define COND_SENSOR_REPORT_PERIOD (100 / TASK_PRIORITY_INTERVAL) ///< Broadcast conductivity values message every 100 ms. +#define COND_SENSOR_PROBE_TYPE 100 ///< 1K cell constant conductivity probe +#define COND_SENSOR_DECIMAL_CONVERSION 100 ///< Conductivity value from FPGA has two decimal place +#define COND_SENSOR_TEMPERATURE_COEF 0.02 ///< Linear temperature coefficient of variation at 25 Celcius for fresh water +#define COND_SENSOR_REFERENCE_TEMPERATURE 25 ///< Reference temperature for conductivity sensor +#define COND_SENSOR_REPORT_PERIOD (1000 / TASK_PRIORITY_INTERVAL) ///< Broadcast conductivity values message every 1000 ms. +#define COND_SENSOR_CPi_CPo_MAX_VALUE 2000 ///< TBD +#define COND_SENSOR_CPi_CPo_MIN_VALUE 100 ///< TBD + +#define MAX_ALLOWED_UNCHANGED_CONDUCTIVITY_READS 100 ///< New reading every 640 ms, expect to get new reading in 1s + // ********** private data ********** -static U32 readCount[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Read count for conductivity readings. +static U32 associateTempSensor[ NUM_OF_CONDUCTIVITY_SENSORS ] = { + TEMPSENSORS_INLET_PRIMARY_HEATER_TEMP_SENSOR, + TEMPSENSORS_OUTLET_PRIMARY_HEATER_TEMP_SENSOR, + TEMPSENSORS_CONDUCTIVITY_SENSOR_1_TEMP_SENSOR, + TEMPSENSORS_CONDUCTIVITY_SENSOR_2_TEMP_SENSOR, +}; + +static U32 readCount[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Read count for conductivity readings. +static U32 internalErrorCount[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Internal error count for conductivity readings. static OVERRIDE_F32_T compensatedConductivityValues[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Latest conductivity values. static OVERRIDE_U32_T conductivityDataPublishInterval = { COND_SENSOR_REPORT_PERIOD, COND_SENSOR_REPORT_PERIOD, 0, 0 }; static U32 conductivityDataPublicationTimerCounter = 0; // ********** private function prototypes ********** -static U32 getConductivityDataPublishInterval( void ); +static void processCPiCPoSensorRead( U32 sensorId, U32 fgpaRead, U08 fpgaReadCount, U08 fpgaErrorCount ); +static F32 getCompensatedConductivity( U32 conductivity, F32 temperature); +static DATA_GET_PROTOTYPE( U32, getConductivityDataPublishInterval ); + /************************************************************************* * @brief * The initConductivitySensors function initializes the ConductivitySensors module. @@ -59,12 +77,14 @@ for ( i = 0; i < NUM_OF_CONDUCTIVITY_SENSORS; i++ ) { readCount[ i ] = 0; + internalErrorCount[ i ] = 0; compensatedConductivityValues[ i ].data = 0.0; compensatedConductivityValues[ i ].ovData = 0.0; compensatedConductivityValues[ i ].ovInitData = 0.0; compensatedConductivityValues[ i ].override = OVERRIDE_RESET; + // TODO: Ability to change probe type // setFPGAConductivityProbeType( COND_SENSOR_PROBE_TYPE ); } } @@ -77,23 +97,41 @@ * Outputs : Advertising conductivity sensors' data. * @return none *************************************************************************/ -void execConductivitySensors(void) +void execConductivitySensors( void ) { - U08 const fpgaCPiReadCount = getFPGACPiReadCount(); - if ( readCount[ CONDUCITIVYSENSORS_CPI_SENSOR ] != fpgaCPiReadCount ) { - readCount[ CONDUCITIVYSENSORS_CPI_SENSOR ] = fpgaCPiReadCount; + processCPiCPoSensorRead( CONDUCITIVYSENSORS_CPI_SENSOR, getFPGACPi(), getFPGACPiReadCount(), getFPGACPiErrorCount() ); + processCPiCPoSensorRead( CONDUCITIVYSENSORS_CPO_SENSOR, getFPGACPi(), getFPGACPoReadCount(), getFPGACPoErrorCount() ); - F32 const temperatureValue = getTemperatureValue( TEMPSENSORS_INLET_PRIMARY_HEATER_TEMP_SENSOR ); - F32 const compensatedCoef = ( 1 + ( COND_SENSOR_TEMPERATURE_COEF * (temperatureValue - 25) ) ); - F32 const rawConductivity = (F32)( getFPGACPi() ) / 100; + if ( ++conductivityDataPublicationTimerCounter >= getConductivityDataPublishInterval() ) + { + conductivityDataPublicationTimerCounter = 0; + broadcastConductivityData( getConductivityValue(CONDUCITIVYSENSORS_CPI_SENSOR), + getConductivityValue(CONDUCITIVYSENSORS_CPO_SENSOR), + 0, + 0); + } +} - compensatedConductivityValues[ CONDUCITIVYSENSORS_CPI_SENSOR ].data = rawConductivity * compensatedCoef; +/************************************************************************* + * @brief + * The checkWaterConductivity function checks conductivity value + * @details + * Inputs : none + * Outputs : Trigger alarms when conductivity is out of allowed range + * @param state : Operational state of DG + * @return none + *************************************************************************/ +void checkWaterConductivity( F32 conductivity, U32 state ) +{ + if ( conductivity > COND_SENSOR_CPi_CPo_MAX_VALUE ) + { + SET_ALARM_WITH_2_F32_DATA( ALARM_ID_INLET_WATER_HIGH_CONDUCITIVY, conductivity, state ); } - if ( ++conductivityDataPublicationTimerCounter >= getConductivityDataPublishInterval() ) + if ( conductivity < COND_SENSOR_CPi_CPo_MIN_VALUE ) { - conductivityDataPublicationTimerCounter = 0; + SET_ALARM_WITH_2_F32_DATA( ALARM_ID_INLET_WATER_LOW_CONDUCITIVY, conductivity, state ); } } @@ -105,7 +143,7 @@ * Inputs : compensatedConductivityValues[] * Outputs : none * @param sensorId : Id of conductivity sensor to get conductivity value - * @return the compensated conductivity value for the given sensor id. + * @return compensated conductivity *************************************************************************/ F32 getConductivityValue( U32 sensorId ) { @@ -130,12 +168,69 @@ return result; } + /************************************************************************* * @brief + * The getCompensatedConductivity function calculates the compensated + * conductivity based on given temperature and conductivity taken at + * reference temperature of 25 Celcius. + * @details + * Inputs : temperature + * Outputs : none + * @param conductivity, temperature + * @return compensated conductivity based on temperature + *************************************************************************/ +static F32 getCompensatedConductivity( U32 conductivity, F32 temperature) +{ + // EC = EC_25 * (1 + temp_coef * (temperature - 25)) + F32 const compensatedCoef = ( 1 + ( COND_SENSOR_TEMPERATURE_COEF * (temperature - COND_SENSOR_REFERENCE_TEMPERATURE) ) ); + + return conductivity * compensatedCoef; +} + +/************************************************************************* + * @brief + * The processCPiCPoSensorRead function checks if there is an error in FPGA + * and FPGA read count. If there is any error in the FPGA error, it raises an + * alarm. If the read count has changed, the new reading will be processed. + * @details + * Inputs : none + * Outputs : none + * @param sensorId, fgpaRead, fpgaReadCount, fpgaErrorCount + * @return none + *************************************************************************/ +static void processCPiCPoSensorRead( U32 sensorId, U32 fgpaRead, U08 fpgaReadCount, U08 fpgaErrorCount ) +{ + if ( fpgaErrorCount == 0 ) + { + if ( (readCount[ sensorId ] < fpgaReadCount) || ( readCount[ sensorId ] > fpgaReadCount )) { + F32 const temperature = getTemperatureValue( associateTempSensor[ sensorId ] ); + F32 const conductivity = ( (F32)( fgpaRead ) / COND_SENSOR_DECIMAL_CONVERSION ); + readCount[ sensorId ] = fpgaReadCount; + internalErrorCount[ sensorId ] = 0; + compensatedConductivityValues[ sensorId ].data = getCompensatedConductivity( conductivity, temperature ); + } + else + { + ++internalErrorCount[ sensorId ]; + if ( internalErrorCount[ sensorId ] > MAX_ALLOWED_UNCHANGED_CONDUCTIVITY_READS ) + { + activateAlarmNoData( ALARM_ID_SOFTWARE_FAULT ); + } + } + } + else + { + SET_ALARM_WITH_1_F32_DATA( ALARM_ID_SOFTWARE_FAULT, fpgaErrorCount ); + } +} + +/************************************************************************* + * @brief * The getConductivityDataPublishInterval function gets the conductivity * data publication interval. * @details - * Inputs : getConductivityDataPublishInterval + * Inputs : conductivityDataPublishInterval * Outputs : none * @return the current conductivity data publication interval (in ms/task interval). *************************************************************************/ @@ -162,7 +257,7 @@ * The testSetConductivityOverride function overrides the compensated * conductivity value of given sensor id. * @details - * Inputs : none + * Inputs : compensatedConductivityValues[] * Outputs : compensatedConductivityValues[] * @param sensorId : Id of conductivity sensor to get conductivity value * @param value : override compensated conductivity value @@ -190,7 +285,7 @@ * The testResetConductivityOverride function resets the override of the \n * conductivity sensor value. * @details - * Inputs : none + * Inputs : compensatedConductivityValues[] * Outputs : compensatedConductivityValues[] * @param sensorId : Id of the conductivity sensor to override. * @return TRUE if reset successful, FALSE if not @@ -217,7 +312,7 @@ * The testSetConductivityDataPublishIntervalOverride function overrides * the conductivity data publish interval. * @details - * Inputs : none + * Inputs : conductivityDataPublishInterval * Outputs : conductivityDataPublishInterval * @param value : override conductivity data publish interval with (in ms) * @return TRUE if override successful, FALSE if not @@ -241,7 +336,7 @@ * The testResetConductivityDataPublishIntervalOverride function resets * the override of the conductivity data publish interval. * @details - * Inputs : none + * Inputs : conductivityDataPublishInterval * Outputs : conductivityDataPublishInterval * @return TRUE if override reset successful, FALSE if not *************************************************************************/