Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -rc4434389fe6c3314c2bba98dc2f4cf737551f637 -r0831cb6bf766fe461340b711f2ff14c3d797e10d --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision c4434389fe6c3314c2bba98dc2f4cf737551f637) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 0831cb6bf766fe461340b711f2ff14c3d797e10d) @@ -48,6 +48,9 @@ #define ROP_P_COEFFICIENT 0.005 ///< P term for RO pump control #define ROP_I_COEFFICIENT 0.0025 ///< I term for RO pump control +#define FLOW_SAMPLES_TO_AVERAGE (250 / TASK_PRIORITY_INTERVAL) ///< Averaging flow data over 250 ms intervals. +#define FLOW_AVERAGE_MULTIPLIER (1.0 / (F32)FLOW_SAMPLES_TO_AVERAGE) ///< Optimization - multiplying is faster than dividing. + #define ROP_PSI_TO_PWM_DC(p) ( 0.2 + ( (F32)((p) - 100) * 0.01 ) ) ///< conversion factor from target PSI to PWM duty cycle estimate TODO - this is a place holder for real conversion #define RO_FLOW_ADC_TO_LPM_FACTOR 10909.0909 ///< conversion factor from ADC counts to LPM (liters/min) for RO flow rate (multiply this by inverse of FPGA reading). @@ -88,6 +91,9 @@ static OVERRIDE_F32_T measuredROFlowRateLPM = { 0.0, 0.0, 0.0, 0 }; ///< measured RO flow rate (in LPM). static F32 measuredROPumpPressure = 0.0; ///< measured RO pressure (in PSI). +static S32 measuredFlowReadingsSum = 0; ///< Raw flow reading sums for averaging. +static U32 flowFilterCounter = 0; ///< used to schedule flow filtering. + static U32 roControlTimerCounter = 0; ///< determines when to perform control on ro pump static RO_PUMP_SELF_TEST_STATE_T roPumpSelfTestState = RO_PUMP_SELF_TEST_STATE_START; ///< current ro pump self test state @@ -183,7 +189,18 @@ { S32 roFlow = (S32)getFPGAROPumpFlowRate(); - measuredROFlowRateLPM.data = ( roFlow == 0 ? 0 : RO_FLOW_ADC_TO_LPM_FACTOR / (F32)(roFlow) ); + // update sum for flow average calculation + measuredFlowReadingsSum += roFlow; + // filter every 250ms + if ( ++flowFilterCounter >= FLOW_SAMPLES_TO_AVERAGE ) + { + F32 avgROFlow = (F32)measuredFlowReadingsSum * FLOW_AVERAGE_MULTIPLIER; + + measuredROFlowRateLPM.data = ( measuredFlowReadingsSum == 0 ? 0.0 : RO_FLOW_ADC_TO_LPM_FACTOR / avgROFlow ); + measuredFlowReadingsSum = 0; + flowFilterCounter = 0; + } + measuredROPumpPressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_OUTLET ); // TODO - check pressure?