Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -r8a916f65cd66ab85f6f220c4e9e9c8a1bc6b0616 -r4497341becc80fd28f718b50fa316bd6015dd866 --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 8a916f65cd66ab85f6f220c4e9e9c8a1bc6b0616) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 4497341becc80fd28f718b50fa316bd6015dd866) @@ -64,7 +64,7 @@ #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 RO_FLOW_ADC_TO_LPM_FACTOR 5555 ///< Conversion factor from ADC counts to LPM (liters/min) for RO flow rate (multiply this by inverse of FPGA reading). +#define RO_FLOW_ADC_TO_LPM_FACTOR 5555 ///< Conversion factor from pulse period (2us units) to flow rate (liters/min) for RO flow rate (divide this by pulse period). #define ROP_FLOW_TO_PWM_SLOPE 0.1 ///< Slope of flow to PWM line equation. #define ROP_FLOW_TO_PWM_INTERCEPT 0.0 ///< Intercept of flow to PWM line equation. @@ -315,7 +315,8 @@ /*********************************************************************//** * @brief - * The execROPumpMonitor function executes the RO pump monitor. + * The execROPumpMonitor function executes the RO pump monitor. The RO flow + * sensor is read, filtered, converted to L/min and calibrated. * @details Inputs: measuredFlowReadingsSum, flowFilterCounter, * measuredROFlowRateLPM, measuredROFlowRateLPM, roPumpState, * flowOutOfRangeCounter, roPumpControlMode @@ -327,12 +328,8 @@ { U16 roFlowReading = getFPGAROPumpFlowRate(); - // If the flow is less than a certain value, FPGA will return 0xFFFF meaning that the flow is 0. - if ( FLOW_SENSOR_ZERO_READING != roFlowReading ) - { - // Update sum for flow average calculation - measuredFlowReadingsSum += (S32)roFlowReading; - } + // Update sum for flow average calculation + measuredFlowReadingsSum += (S32)roFlowReading; // Read the pressure at the sensor. The pump cannot be more that the maximum allowed pressure // to make sure the hardware (especially the ROF) is not damaged. If it is the case, we need to stop immediately @@ -351,18 +348,19 @@ if ( ++flowFilterCounter >= FLOW_SAMPLES_TO_AVERAGE ) { U32 sensor; + F32 flow = RO_FLOW_ADC_TO_LPM_FACTOR / ( (F32)measuredFlowReadingsSum * FLOW_AVERAGE_MULTIPLIER ); // Convert flow sensor period to L/min - F32 flow = RO_FLOW_ADC_TO_LPM_FACTOR / ( (F32)measuredFlowReadingsSum * FLOW_AVERAGE_MULTIPLIER ); + // Apply calibration to flow sensor reading + measuredROFlowRateLPM.data = pow(flow, 4) * flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].fourthOrderCoeff + + pow(flow, 3) * flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].thirdOrderCoeff + + pow(flow, 2) * flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].secondOrderCoeff + + flow * flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].gain + + flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].offset; - // Right now there is only one flow sensor but a for loop is used here to be able to automatically accommodate - // any future flow sensors TODO - this loop works with the flowSensorsCalRecord, but not with the measuredROFlowRateLPM structure. - for( sensor = 0; sensor < NUM_OF_CAL_DATA_FLOW_SENSORS; sensor++ ) + // If the flow is less than a certain value, FPGA will return 0xFFFF meaning that the flow is 0. + if ( FLOW_SENSOR_ZERO_READING == roFlowReading ) { - measuredROFlowRateLPM.data = pow(flow, 4) * flowSensorsCalRecord.flowSensors[ sensor ].fourthOrderCoeff + - pow(flow, 3) * flowSensorsCalRecord.flowSensors[ sensor ].thirdOrderCoeff + - pow(flow, 2) * flowSensorsCalRecord.flowSensors[ sensor ].secondOrderCoeff + - flow * flowSensorsCalRecord.flowSensors[ sensor ].gain + - flowSensorsCalRecord.flowSensors[ sensor ].offset; + measuredROFlowRateLPM.data = 0.0; } measuredFlowReadingsSum = 0;