Index: firmware/App/Monitors/Conductivity.c =================================================================== diff -u -r2652d50bbc5e78ed6fe3ad9ccbca0be6f802f1ff -rb110edbfea44736075d89212a7529df6df64e521 --- firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision 2652d50bbc5e78ed6fe3ad9ccbca0be6f802f1ff) +++ firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision b110edbfea44736075d89212a7529df6df64e521) @@ -22,6 +22,7 @@ #include "ConductivitySensor.h" #include "MessageSupport.h" #include "Messaging.h" +#include "ModeGenPermeate.h" #include "TaskPriority.h" #include "Utilities.h" @@ -38,6 +39,8 @@ #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 RO_RR_MOVING_AVG_NUM_OF_SAMPLES 300 ///< 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 /// Filter conductivity readings record. typedef struct @@ -65,13 +68,25 @@ static OVERRIDE_F32_T filteredcurrentTemperatureReadings[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< filtered current conductivity sensor temperature readings (overrideable). static U32 conductivityPublishTimerCounter; ///< Conductivity data publication counter. static OVERRIDE_U32_T conductivityDataPublishInterval; ///< Conductivity sensors publish time interval override. +static F32 roRejectionRatio; ///< All time RO rejection ratio. +static F32 roRejectionRatioTankFill; ///< RO rejection ratio during permeate tank fill state. +static U32 roRRPublishTimerCounter; ///< RO rejection ratio publication counter. +static OVERRIDE_U32_T roRRDataPublishInterval; ///< RO rejection ratio publish time interval override. +static OVERRIDE_F32_T roRRAvg; ///< Average RO rejection ratio. +static F32 roRRRunningSum; ///< RO rejection ratio running sum. +static F32 roRRSamples[ RO_RR_MOVING_AVG_NUM_OF_SAMPLES ]; ///< RO rejection ratio samples array. +static U32 roRRSamplesNextIndex; ///< RO rejection ratio sample next index number. +static U32 roRRCount; ///< RO rejection ratio Number of samples in average buffer. +static F32 roRRTankFillAvg; ///< Average RO rejection ratio during permeate tank fill state. // ********** private function prototypes ********** static void publishConductivitySensorsData( void ); static void filterConductivitySensors( void ); static void filterConductivitySensorReadings( void ); static void filterConductivitySensorTemperatureReadings( void ); +static void calcRORejectionRatio( void ); +static void filterRORejectionRatioReadings( void ); /*********************************************************************//** * @brief @@ -88,6 +103,16 @@ initConductivitySensors(); conductivityPublishTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; + roRejectionRatio = 0.0F; + roRejectionRatioTankFill = 0.0F; + roRRRunningSum = 0.0F; + roRRSamplesNextIndex = 0; + roRRCount = 0; + roRRTankFillAvg = 0.0F; + roRRAvg.data = 0.0F; + roRRAvg.ovData = 0.0F; + roRRAvg.ovInitData = 0.0F; + roRRAvg.override = OVERRIDE_RESET; // Initialize override structures for each conductivity sensor for ( sensor = CONDUCTIVITYSENSORS_FIRST; sensor < NUM_OF_CONDUCTIVITY_SENSORS; sensor++ ) @@ -115,6 +140,11 @@ conductivityDataPublishInterval.ovData = COND_SENSOR_REPORT_PERIOD; conductivityDataPublishInterval.ovInitData = 0; conductivityDataPublishInterval.override = OVERRIDE_RESET; + + roRRDataPublishInterval.data = COND_SENSOR_REPORT_PERIOD; + roRRDataPublishInterval.ovData = COND_SENSOR_REPORT_PERIOD; + roRRDataPublishInterval.ovInitData = 0; + roRRDataPublishInterval.override = OVERRIDE_RESET; } /*********************************************************************//** @@ -139,6 +169,8 @@ execConductivitySensorRead(); filterConductivitySensors(); + calcRORejectionRatio(); // TODO: should this be called here or inside filter function + filterRORejectionRatioReadings(); // publish conductivity sensors publishConductivitySensorsData(); } @@ -281,6 +313,115 @@ /*********************************************************************//** * @brief + * The calcRORejectionRatio function calculates the RO rejection ratio using + * the P9 sensor conductivity value and P18 sensor conductivity value. + * @details Inputs: P9 sensor conductivity, P18 sensor conductivity + * @details Outputs: RO rejection ratio + * @return none + *************************************************************************/ +static void calcRORejectionRatio( void ) +{ + F32 feedConductivity = getFilteredConductivity( P9_COND ); + F32 permeateConductivity = getFilteredConductivity( P18_COND ); + + roRejectionRatio = RO_REJECTION_RATIO_OUT_OF_RANGE_VALUE; + + if ( fabs(feedConductivity) >= NEARLY_ZERO ) + { + roRejectionRatio = FRACTION_TO_PERCENT_CONVERSION_FACTOR * ( ( feedConductivity - permeateConductivity ) / feedConductivity ); + } + if ( getCurrentGenPermeateState() == FP_GENP_TANK_FILL_STATE ) + { + roRejectionRatioTankFill = roRejectionRatio; + } +} + +/*********************************************************************//** + * @brief + * The filterRORejectionRatioReadings function adds a new ro rejection ratio + * sample to the filters. + * @details \b Inputs: RO rejection ratio all time and during tank fill state + * @details \b Outputs: roRRRunningSumC, roRRSamples[], roRRSamplesNextIndex, + * roRRCount, roRRAvg, roRRTankFillAvg + * @return none + *************************************************************************/ +static void filterRORejectionRatioReadings( void ) +{ + // Filter RO rejection ratio + if ( roRRCount >= RO_RR_MOVING_AVG_NUM_OF_SAMPLES ) + { + roRRRunningSum -= roRRSamples[ roRRSamplesNextIndex ]; + } + + F32 roRR = getRORejectonRatio(); + roRRSamples[ roRRSamplesNextIndex ] = roRR; + roRRRunningSum += roRR; + roRRSamplesNextIndex = INC_WRAP( roRRSamplesNextIndex, 0, RO_RR_MOVING_AVG_NUM_OF_SAMPLES - 1 ); + roRRCount = INC_CAP( roRRCount, RO_RR_MOVING_AVG_NUM_OF_SAMPLES ); + roRRAvg.data = roRRRunningSum / (F32)roRRCount; + + // Update the Filter RO rejection ratio during tank fill if the tank is filling + if ( getCurrentGenPermeateState() == FP_GENP_TANK_FILL_STATE ) + { +// roRRTankFillAvg = getRORRAverage(); + roRRTankFillAvg = roRRAvg.data; + } +} + +/*********************************************************************//** + * @brief + * The getRORejectonRatio function returns the RO rejection ratio + * @details \b Inputs: none + * @details \b Outputs: none + * @return the RO rejection ratio in percentage + *************************************************************************/ +F32 getRORejectonRatio( void ) +{ + return roRejectionRatio; +} + +/*********************************************************************//** + * @brief + * The getTankFillRORejectionRatio function returns the RO rejection ratio + * during tank fill state + * @details \b Inputs: none + * @details \b Outputs: none + * @return the tank fill RO rejection ratio in percentage + *************************************************************************/ +F32 getTankFillRORejectionRatio( void ) +{ + return roRejectionRatioTankFill; +} + +/*********************************************************************//** + * @brief + * The getRORRAverage function returns the average RO rejection ratio + * @details \b Inputs: none + * @details \b Outputs: none + * @return the average RO rejection ratio in percentage + *************************************************************************/ +F32 getRORRAverage( void ) +{ + F32 avgRORR = getF32OverrideValue( &roRRAvg ); + + return avgRORR; +} + +/*********************************************************************//** + * @brief + * The getTankFillRORRAverage function returns the average RO rejection ratio + * during tank fill state + * @details \b Inputs: none + * @details \b Outputs: none + * @return the average tank fill RO rejection ratio in percentage + *************************************************************************/ +F32 getTankFillRORRAverage( void ) +{ + return roRRTankFillAvg; +} + +/*********************************************************************//** + * @brief * The publishConductivitySensorsData function publishes FP conductivity data * at a set interval. * @details \b Inputs: conductivityPublishTimerCounter @@ -295,12 +436,26 @@ { CONDUCTIVITY_DATA_T data; - data.p9Conductivity = getFilteredConductivity( P9_COND ); - data.p18Conductivity = getFilteredConductivity( P18_COND ); + data.p9Conductivity = getFilteredConductivity( P9_COND ); + data.p18Conductivity = getFilteredConductivity( P18_COND ); conductivityPublishTimerCounter = 0; broadcastData( MSG_ID_FP_CONDUCTIVITY_DATA, COMM_BUFFER_OUT_CAN_FP_BROADCAST, (U08*)&data, sizeof( CONDUCTIVITY_DATA_T ) ); } + // publish ro rejection ratio data on interval + if ( ++roRRPublishTimerCounter >= getU32OverrideValue( &roRRDataPublishInterval ) ) + { + RO_REJECTION_RATIO_DATA_T data; + + data.rawRORejectionRatio = getRORejectonRatio(); + data.rawRORejectionRatioTankFill = getTankFillRORejectionRatio(); + data.avgRORejectionRatio = getRORRAverage(); + data.avgRORejectionRatioTankFill = getTankFillRORRAverage(); + data.genPermeateState = (U32)getCurrentGenPermeateState(); + roRRPublishTimerCounter = 0; + + broadcastData( MSG_ID_FP_RO_REJECTION_RATIO_DATA, COMM_BUFFER_OUT_CAN_FP_BROADCAST, (U08*)&data, sizeof( RO_REJECTION_RATIO_DATA_T ) ); + } } @@ -328,6 +483,23 @@ /*********************************************************************//** * @brief + * The testRORejectionRatioDataPublishIntervalOverride function overrides the + * RO Rejection ratio data publish interval. + * @details \b Inputs: none + * @details \b Outputs: roRRDataPublishInterval + * @param message Override message from Dialin which includes the value + * that override valves states publish interval with (in ms) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testRORejectionRatioDataPublishIntervalOverride( MESSAGE_T *message ) +{ + BOOL result = u32BroadcastIntervalOverride( message, &roRRDataPublishInterval, TASK_PRIORITY_INTERVAL ); + + return result; +} + +/*********************************************************************//** + * @brief * The testConductivitySensorFilteredReadingsOverride function overrides the * filtered value of the specified conductivity sensor with a given value. * @details \b Inputs: none @@ -361,4 +533,21 @@ return result; } +/*********************************************************************//** + * @brief + * The testRORejectionRatioFilteredOverride function + * overrides the filtered RO rejection value with a given value. + * @details \b Inputs: none + * @details \b Outputs: roRRAvg + * @param message Override message from Dialin which includes override value + * of the RO rejection ratio. + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testRORejectionRatioFilteredOverride( MESSAGE_T *message ) +{ + BOOL result = f32Override( message, &roRRAvg ); + + return result; +} + /**@}*/