Index: firmware/App/Monitors/Conductivity.c =================================================================== diff -u -rc07917caced76b53a0ed8f35167fac6f9d8310a4 -rf4a8565c7edec12094b8a737ed7b8cd3424a9208 --- firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision c07917caced76b53a0ed8f35167fac6f9d8310a4) +++ firmware/App/Monitors/Conductivity.c (.../Conductivity.c) (revision f4a8565c7edec12094b8a737ed7b8cd3424a9208) @@ -7,8 +7,8 @@ * * @file Conductivity.c * -* @author (last) Vinayakam Mani -* @date (last) 10-Sep-2025 +* @author (last) “rkallala” +* @date (last) 09-Dec-2025 * * @author (original) Vinayakam Mani * @date (original) 13-Sep-2024 @@ -20,6 +20,7 @@ #include "Conductivity.h" #include "MessageSupport.h" #include "Messaging.h" +#include "ModeGenPermeate.h" #include "OperationModes.h" #include "TaskPriority.h" #include "Utilities.h" @@ -37,6 +38,9 @@ #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 30 ///< 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 +#define RO_RR_SAMPLE_COLLECTION_INTERVAL 10 ///< Collect RO rejection ratio sample for every 10th time in priority task /// Filter conductivity readings record. typedef struct @@ -66,13 +70,26 @@ static OVERRIDE_U32_T ddConductivityDataPublishInterval; ///< DD Conductivity sensors publish time interval override. static U32 fpConductivityPublishTimerCounter; ///< FP Conductivity data publication counter. static OVERRIDE_U32_T fpConductivityDataPublishInterval; ///< FP 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. +static U32 roRRSampleIntervalCounter; ///< RO rejection ratio sample collection timer counter. // ********** 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 @@ -89,7 +106,21 @@ ddConductivityPublishTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; fpConductivityPublishTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; + roRRPublishTimerCounter = 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; + roRRSampleIntervalCounter = 0; + memset( &roRRSamples, 0, sizeof( roRRSamples ) ); + // Initialize override structures for each conductivity sensor for ( sensor = FIRST_DD_COND_SENSOR; sensor < NUM_OF_CONDUCTIVITY_SENSORS; sensor++ ) { @@ -121,6 +152,11 @@ fpConductivityDataPublishInterval.ovData = COND_SENSOR_REPORT_PERIOD; fpConductivityDataPublishInterval.ovInitData = 0; fpConductivityDataPublishInterval.override = OVERRIDE_RESET; + + roRRDataPublishInterval.data = COND_SENSOR_REPORT_PERIOD; + roRRDataPublishInterval.ovData = COND_SENSOR_REPORT_PERIOD; + roRRDataPublishInterval.ovInitData = 0; + roRRDataPublishInterval.override = OVERRIDE_RESET; } /*********************************************************************//** @@ -147,6 +183,8 @@ #endif filterConductivitySensors(); + calcRORejectionRatio(); // TODO: should this be called here or inside filter function + filterRORejectionRatioReadings(); // publish conductivity sensors publishConductivitySensorsData(); } @@ -289,10 +327,123 @@ /*********************************************************************//** * @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, Tank fill 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 ) +{ + F32 roRR = 0.0F; + + // Moving average samples are collected for every 100 msec + if ( ++roRRSampleIntervalCounter >= RO_RR_SAMPLE_COLLECTION_INTERVAL ) + { + // Filter RO rejection ratio + roRRRunningSum -= roRRSamples[ roRRSamplesNextIndex ]; + 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; + roRRSampleIntervalCounter = 0; + + // Update the Filter RO rejection ratio during tank fill if the tank is filling + if ( getCurrentGenPermeateState() == FP_GENP_TANK_FILL_STATE ) + { + roRRTankFillAvg = roRRAvg.data; + } + } +} + +/*********************************************************************//** + * @brief + * The getRORejectonRatio function returns the RO rejection ratio + * @details \b Inputs: roRejectionRatio + * @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: roRejectionRatioTankFill + * @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: roRRAvg + * @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: roRRTankFillAvg + * @details \b Outputs: none + * @return the average tank fill RO rejection ratio in percentage + *************************************************************************/ +F32 getTankFillRORRAverage( void ) +{ + return roRRTankFillAvg; +} + +/*********************************************************************//** + * @brief * The publishConductivitySensorsData function publishes DD conductivity data * at a set interval. - * @details \b Inputs: conductivityPublishTimerCounter - * @details \b Outputs: conductivityPublishTimerCounter + * @details \b Inputs: ddconductivityPublishTimerCounter, fpConductivityPublishTimerCounter, + * roRRPublishTimerCounter + * @details \b Outputs: ddconductivityPublishTimerCounter, fpConductivityPublishTimerCounter, + * roRRPublishTimerCounter, DD, FP, RO data broadcast message sent * @details \b Message \b Sent: MSG_ID_DD_CONDUCTIVITY_DATA to publish DD conductivity data. * @details \b Message \b Sent: MSG_ID_FP_CONDUCTIVITY_DATA to publish FP conductivity data. * @return none @@ -325,6 +476,21 @@ broadcastData( MSG_ID_FP_CONDUCTIVITY_DATA, COMM_BUFFER_OUT_CAN_FP_BROADCAST, (U08*)&data, sizeof( FP_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 ) ); + } } @@ -465,4 +631,38 @@ return result; } +/*********************************************************************//** + * @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 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; +} + /**@}*/