Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -ra9315539f527b92523b1598ff91e47db4d71dae2 -rbb7a4629cc96d96b6d1749c924e77886e0d64bcb --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision a9315539f527b92523b1598ff91e47db4d71dae2) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision bb7a4629cc96d96b6d1749c924e77886e0d64bcb) @@ -8,7 +8,7 @@ * @file ROPump.c * * @author (last) Dara Navaei -* @date (last) 24-Feb-2022 +* @date (last) 31-Mar-2022 * * @author (original) Sean * @date (original) 04-Apr-2020 @@ -35,6 +35,7 @@ #include "TaskGeneral.h" #include "TaskPriority.h" #include "Timers.h" +#include "Valves.h" #ifdef EMC_TEST_BUILD #include "Heaters.h" #endif @@ -48,46 +49,46 @@ #define RO_PUMP_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Interval (ms/task time) at which the RO Pump data is published on the CAN bus. -#define MAX_RO_PUMP_DUTY_CYCLE 0.99 ///< Max duty cycle. -#define MIN_RO_PUMP_DUTY_CYCLE 0.0 ///< Min duty cycle. +#define MAX_RO_PUMP_DUTY_CYCLE 0.99F ///< Max duty cycle. +#define MIN_RO_PUMP_DUTY_CYCLE 0.0F ///< Min duty cycle. #define ROP_CONTROL_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the RO pump is controlled. #define ROP_RAMP_UP_CONTROL_INTERVAL ( 500 / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the RO pump is controlled. -#define ROP_RAMP_UP_P_COEFFICIENT 0.22 ///< P term for RO pump ramp up to flow control. -#define ROP_FLOW_CONTROL_P_COEFFICIENT 0.25 ///< P term for RO pump flow control. -#define ROP_FLOW_CONTROL_I_COEFFICIENT 0.25 ///< I term for RO pump flow control. -#define ROP_MAX_PRESSURE_P_COEFFICIENT 0.01 ///< P term for RO pump max pressure control. -#define ROP_MAX_PRESSURE_I_COEFFICIENT 0.01 ///< I term for RO pump max pressure control. +#define ROP_RAMP_UP_P_COEFFICIENT 0.22F ///< P term for RO pump ramp up to flow control. +#define ROP_FLOW_CONTROL_P_COEFFICIENT 0.25F ///< P term for RO pump flow control. +#define ROP_FLOW_CONTROL_I_COEFFICIENT 0.25F ///< I term for RO pump flow control. +#define ROP_MAX_PRESSURE_P_COEFFICIENT 0.01F ///< P term for RO pump max pressure control. +#define ROP_MAX_PRESSURE_I_COEFFICIENT 0.01F ///< I term for RO pump max pressure control. -#define ROP_FLOW_TARGET_TOLERANCE 0.03 ///< Tolerance in between the target flow rate and the actual flow rate in percentage. -#define ROP_RAMP_DOWN_DUTY_CYCLE_RATIO 0.03 ///< Pump ramp down duty cycle ratio when the pressure higher than max defined. +#define ROP_FLOW_TARGET_TOLERANCE 0.03F ///< Tolerance in between the target flow rate and the actual flow rate in percentage. +#define ROP_RAMP_DOWN_DUTY_CYCLE_RATIO 0.03F ///< Pump ramp down duty cycle ratio when the pressure higher than max defined. #define FLOW_SENSOR_ZERO_READING 0xFFFF ///< Flow sensor reading indicates zero flow (or flow lower than can be detected by sensor). #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 FLOW_AVERAGE_MULTIPLIER ( 1.0F / (F32)FLOW_SAMPLES_TO_AVERAGE ) ///< Optimization - multiplying is faster than dividing. -#define RO_FLOW_ADC_TO_LPM_FACTOR 300.0 ///< 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 300.0F ///< Conversion factor from ADC counts to LPM (liters/min) for RO flow rate (multiply this by inverse of FPGA reading). -#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. +#define ROP_FLOW_TO_PWM_SLOPE 0.1F ///< Slope of flow to PWM line equation. +#define ROP_FLOW_TO_PWM_INTERCEPT 0.0F ///< Intercept of flow to PWM line equation. /// Initial conversion factor from target flow rate to PWM duty cycle estimate. #define ROP_FLOW_TO_PWM_DC(flow) ( ROP_FLOW_TO_PWM_SLOPE * flow + ROP_FLOW_TO_PWM_INTERCEPT ) -#define MAX_ALLOWED_FLOW_DEVIATION 0.1 ///< Max allowed deviation from target flow. +#define MAX_ALLOWED_FLOW_DEVIATION 0.1F ///< Max allowed deviation from target flow. #define FLOW_OUT_OF_RANGE_PERSISTENT_INTERVAL ( 12 * MS_PER_SECOND ) ///< Flow out of range time out in counts. #define MAX_PRESSURE_TARGET_TOLERANCE 5 ///< Pressure tolerance from maximum set pressure by user in psi. #define MAX_ALLOWED_PRESSURE_PSI 130 ///< Maximum allowed pressure that the RO pump can go to. #define MIN_ALLOWED_PRESSURE_PSI 10 ///< Minimum allowed pressure that the RO pump can go to. #define MAX_ALLOWED_MEASURED_PRESSURE_PSI 135 ///< Maximum allowed pressure that the sensor measures. RO pump shut off pressure is 140psi. #define MAX_PRESSURE_OUT_OF_RANGE_PERSISTENT_INTERVAL MS_PER_SECOND ///< Maximum allowed time that the pressure can be very high. #define MAX_ALLOWED_RAMP_UP_TIME ( 10 * MS_PER_SECOND ) ///< Maximum allowed ramp up time to a flow rate in ms. -#define ROP_PSI_TO_PWM_DC(p) ( 0.2 + ( (F32)((p) - 100) * 0.01 ) ) ///< Conversion factor from target PSI to PWM duty cycle estimate. +#define ROP_PSI_TO_PWM_DC(p) ( 0.2F + ( (F32)((p) - 100) * 0.01F ) ) ///< Conversion factor from target PSI to PWM duty cycle estimate. #define SAFETY_SHUTDOWN_TIMEOUT ( 3 * MS_PER_SECOND ) ///< RO pump safety shutdown activation timeout in ms. -#define ROP_FEEDBACK_0_PCT_DUTY_CYCLE_VOLTAGE 2.51 ///< RO pump 0% duty cycle feedback voltage. -#define ROP_DUTY_CYCLE_OUT_OF_RANGE_TOLERANCE 0.05 ///< RO pump duty cycle out of range tolerance. +#define ROP_FEEDBACK_0_PCT_DUTY_CYCLE_VOLTAGE 2.51F ///< RO pump 0% duty cycle feedback voltage. +#define ROP_DUTY_CYCLE_OUT_OF_RANGE_TOLERANCE 0.05F ///< RO pump duty cycle out of range tolerance. #define DATA_PUBLISH_COUNTER_START_COUNT 50 ///< Data publish counter start count. @@ -139,8 +140,9 @@ static S32 measuredFlowReadingsSum = 0; ///< Raw flow reading sums for averaging. static U32 flowFilterCounter = 0; ///< Flow filtering counter. static DG_FLOW_SENSORS_CAL_RECORD_T flowSensorsCalRecord; ///< Flow sensors calibration record. +static OVERRIDE_F32_T measuredROFlowRateWithConcPumpsLPM = { 0.0, 0.0, 0.0, 0 }; ///< Measure RO flow rate with concentrate pumps (L/min). static DG_RO_PUMP_CAL_RECORD_T roPumpCalRecord; ///< RO pump calibration record. -static F32 rawFlowLP; ///< Raw flow rate without subtracting the CP1 and CP2 in L/min. +static F32 roVolumeL; ///< RO water generated in liters. // ********** private function prototypes ********** @@ -198,7 +200,7 @@ roPumpControlMode = NUM_OF_PUMP_CONTROL_MODES; isROPumpOn = FALSE; roPumpFeedbackDutyCyclePct = 0.0; - rawFlowLP = 0.0; + roVolumeL = 0.0; } /*********************************************************************//** @@ -335,16 +337,16 @@ *************************************************************************/ void execROPumpMonitor( void ) { - U16 roFlowReading = getFPGAROPumpFlowRate(); + U32 roFlowReading = (U32)getFPGAROPumpFlowRate(); F32 roFeedbackVoltage = getIntADCVoltageConverted( INT_ADC_RO_PUMP_FEEDBACK_DUTY_CYCLE ); - - // 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 F32 actualPressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_OUTLET ); BOOL isPressureMax = ( actualPressure >= MAX_ALLOWED_MEASURED_PRESSURE_PSI ? TRUE : FALSE ); + + // Update sum for flow average calculation + measuredFlowReadingsSum += (S32)roFlowReading; + checkPersistentAlarm( ALARM_ID_RO_PUMP_PRESSURE_OUT_OF_RANGE, isPressureMax, actualPressure, MAX_ALLOWED_MEASURED_PRESSURE_PSI ); // Check if a new calibration is available @@ -365,9 +367,9 @@ flow * flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].gain + flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].offset; - rawFlowLP = flow; - measuredROFlowRateLPM.data = flow - ( getMeasuredPumpSpeed( CONCENTRATEPUMPS_CP1_ACID ) / ML_PER_LITER ) - - ( getMeasuredPumpSpeed( CONCENTRATEPUMPS_CP2_BICARB ) / ML_PER_LITER ); + measuredROFlowRateWithConcPumpsLPM.data = flow; + measuredROFlowRateLPM.data = flow - ( getMeasuredPumpSpeed( CONCENTRATEPUMPS_CP1_ACID ) / ML_PER_LITER ) - + ( getMeasuredPumpSpeed( CONCENTRATEPUMPS_CP2_BICARB ) / ML_PER_LITER ); // If the flow is less than a certain value, FPGA will return 0xFFFF meaning that // the flow is 0. @@ -380,39 +382,49 @@ flowFilterCounter = 0; } -#ifndef IGNORE_RO_PUMP_MONITOR - // The feedback voltage is on the 0V line so when the duty cycle is 0, the feedback is 2.5V - // The duty cycle is calculated by getting the 1 - (ratio of feedback / to the voltage at 0 percent duty cycle). - roPumpFeedbackDutyCyclePct = 1.0 - ( roFeedbackVoltage / ROP_FEEDBACK_0_PCT_DUTY_CYCLE_VOLTAGE ); + if ( ( measuredROFlowRateLPM.data > NEARLY_ZERO ) && ( (U32)VALVE_STATE_CLOSED == getValveState( (U32)VBF ) ) ) + { + roVolumeL += ( measuredROFlowRateLPM.data * SEC_PER_MIN ); + } - // To monitor the flow, the control mode must be in closed loop mode and the pump should be control to flow state - // If the pump is controlled to the maximum pressure, the flow might be different from the target flow for more than 10% - // but the pump is not able to achieve the flow. - if ( ( PUMP_CONTROL_MODE_CLOSED_LOOP == roPumpControlMode ) && ( RO_PUMP_CONTROL_TO_TARGET_FLOW_STATE == roPumpState ) ) +#ifndef _RELEASE_ + if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_RO_PUMP_MONITOR ) != SW_CONFIG_ENABLE_VALUE ) +#endif { - F32 currentFlow = getMeasuredROFlowRateLPM(); - F32 targetFlow = getTargetROPumpFlowRate(); - // The flow cannot be out of range for than 10% of the target flow - BOOL isFlowOutOfRange = ( fabs( 1.0 - ( currentFlow / targetFlow ) ) > MAX_ALLOWED_FLOW_DEVIATION ? TRUE : FALSE ); - // Figure out whether flow is out of range from which side - BOOL isFlowOutOfUpperRange = ( isFlowOutOfRange && ( currentFlow > targetFlow ) ? TRUE : FALSE ); - BOOL isFlowOutOfLowerRange = ( isFlowOutOfRange && ( currentFlow < targetFlow ) ? TRUE : FALSE ); + // The feedback voltage is on the 0V line so when the duty cycle is 0, the feedback is 2.5V + // The duty cycle is calculated by getting the 1 - (ratio of feedback / to the voltage at 0 percent duty cycle). + roPumpFeedbackDutyCyclePct = 1.0 - ( roFeedbackVoltage / ROP_FEEDBACK_0_PCT_DUTY_CYCLE_VOLTAGE ); - checkPersistentAlarm( ALARM_ID_FLOW_RATE_OUT_OF_UPPER_RANGE, isFlowOutOfUpperRange, currentFlow, targetFlow ); - checkPersistentAlarm( ALARM_ID_FLOW_RATE_OUT_OF_LOWER_RANGE, isFlowOutOfLowerRange, currentFlow, targetFlow ); - } + // To monitor the flow, the control mode must be in closed loop mode and the pump should be control to flow state + // If the pump is controlled to the maximum pressure, the flow might be different from the target flow for more than 10% + // but the pump is not able to achieve the flow. + if ( ( PUMP_CONTROL_MODE_CLOSED_LOOP == roPumpControlMode ) && ( RO_PUMP_CONTROL_TO_TARGET_FLOW_STATE == roPumpState ) ) + { + F32 currentFlow = getMeasuredROFlowRateLPM(); + F32 targetFlow = getTargetROPumpFlowRate(); + // The flow cannot be out of range for than 10% of the target flow + BOOL isFlowOutOfRange = ( fabs( 1.0 - ( currentFlow / targetFlow ) ) > MAX_ALLOWED_FLOW_DEVIATION ? TRUE : FALSE ); + // Figure out whether flow is out of range from which side + BOOL isFlowOutOfUpperRange = ( isFlowOutOfRange && ( currentFlow > targetFlow ) ? TRUE : FALSE ); + BOOL isFlowOutOfLowerRange = ( isFlowOutOfRange && ( currentFlow < targetFlow ) ? TRUE : FALSE ); - // Check whether the Duty cycle is out of range - BOOL isDCOutOfRange = ( fabs( roPumpFeedbackDutyCyclePct - roPumpDutyCyclePctSet ) > ROP_DUTY_CYCLE_OUT_OF_RANGE_TOLERANCE ? TRUE : FALSE ); + checkPersistentAlarm( ALARM_ID_FLOW_RATE_OUT_OF_UPPER_RANGE, isFlowOutOfUpperRange, currentFlow, targetFlow ); + checkPersistentAlarm( ALARM_ID_FLOW_RATE_OUT_OF_LOWER_RANGE, isFlowOutOfLowerRange, currentFlow, targetFlow ); + } - checkPersistentAlarm( ALARM_ID_RO_PUMP_DUTY_CYCLE_OUT_OF_RANGE, isDCOutOfRange, roPumpFeedbackDutyCyclePct, roPumpDutyCyclePctSet ); + // Check whether the Duty cycle is out of range + BOOL isDCOutOfRange = ( fabs( roPumpFeedbackDutyCyclePct - roPumpDutyCyclePctSet ) > ROP_DUTY_CYCLE_OUT_OF_RANGE_TOLERANCE ? TRUE : FALSE ); - // Check if it the alarm has timed out and if the pump is supposed to be off but it is still on, activate the safety shutdown - if ( ( TRUE == isAlarmActive( ALARM_ID_RO_PUMP_DUTY_CYCLE_OUT_OF_RANGE ) ) && ( FALSE == isROPumpOn ) ) - { - activateSafetyShutdown(); + /* TODO this is commented out until the DVT boards are available + checkPersistentAlarm( ALARM_ID_RO_PUMP_DUTY_CYCLE_OUT_OF_RANGE, isDCOutOfRange, roPumpFeedbackDutyCyclePct, roPumpDutyCyclePctSet ); + + // Check if it the alarm has timed out and if the pump is supposed to be off but it is still on, activate the safety shutdown + + if ( ( TRUE == isAlarmActive( ALARM_ID_RO_PUMP_DUTY_CYCLE_OUT_OF_RANGE ) ) && ( FALSE == isROPumpOn ) ) + { + activateSafetyShutdown(); + }*/ } -#endif // Publish RO pump data on interval publishROPumpData(); @@ -546,6 +558,19 @@ /*********************************************************************//** * @brief + * The getMeasuredROFlowRateWithConcPumpsLPM function gets the measured RO + * pump flow rate with the concentrate pumps. + * @details Inputs: measuredROFlowRateWithConcPumpsLPM + * @details Outputs: measuredROFlowRateWithConcPumpsLPM + * @return the current RO pump flow rate with the concentrate pumps (in L/min). + *************************************************************************/ +F32 getMeasuredROFlowRateWithConcPumpsLPM( void ) +{ + return getF32OverrideValue( &measuredROFlowRateWithConcPumpsLPM ); +} + +/*********************************************************************//** + * @brief * The getTargetROPumpPressure function gets the current target RO pump * pressure. * @details Inputs: targetROPumpPressure @@ -559,6 +584,18 @@ /*********************************************************************//** * @brief + * The getROGeneratedVolumeL function returns the RO generated volume in liters. + * @details Inputs: none + * @details Outputs: none + * @return the RO generated volume in liters + *************************************************************************/ +F32 getROGeneratedVolumeL( void ) +{ + return roVolumeL; +} + +/*********************************************************************//** + * @brief * The handleROPumpOffState function handles the RO pump off state of the * controller state machine. * @details Inputs: roPumpControlMode, roPumpPWMDutyCyclePctSet, @@ -606,17 +643,15 @@ RO_PUMP_STATE_T state = RO_PUMP_RAMP_UP_TO_TARGET_FLOW_STATE; // Get the current pressure from the sensor - F32 actualPressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_OUTLET ); - F32 targetFlowRate = getTargetROPumpFlowRate(); - F32 actualFlowRate = (F32)getMeasuredROFlowRateLPM(); - + F32 actualPressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_OUTLET ); + F32 targetFlowRate = getTargetROPumpFlowRate(); + F32 actualFlowRate = (F32)getMeasuredROFlowRateLPM(); F32 flowRateDeviation = fabs( targetFlowRate - actualFlowRate ) / targetFlowRate; BOOL isFlowOutOfRange = flowRateDeviation > ROP_FLOW_TARGET_TOLERANCE; + F32 targetPressure = getTargetROPumpPressure(); if ( ++roControlTimerCounter >= ROP_RAMP_UP_CONTROL_INTERVAL ) { - F32 targetPressure = getTargetROPumpPressure(); - // If the actual pressure is greater than the target pressure or it is within the tolerance of the maximum pressure, move to set // to target pressure straight. At the beginning the maximum pressure is set in the targetROPumpPressure override variable. // If the flow rate was reached without reaching to maximum pressure, the pressure that was set to targetROPumpPressure override will @@ -808,15 +843,14 @@ { RO_PUMP_DATA_T pumpData; - pumpData.roPumpTgtFlowRateLM = getTargetROPumpFlowRate(); - pumpData.roPumpTgtPressure = getTargetROPumpPressure(); - pumpData.measROFlowRate = getMeasuredROFlowRateLPM(); - pumpData.roPumpDutyCycle = roPumpDutyCyclePctSet * FRACTION_TO_PERCENT_FACTOR; - pumpData.roPumpState = (U32)roPumpState; - pumpData.roPumpFBDutyCycle = roPumpFeedbackDutyCyclePct * FRACTION_TO_PERCENT_FACTOR; - pumpData.roPumpRawFlowRateMLP = rawFlowLP * ML_PER_LITER; + pumpData.roPumpTgtFlowRateLM = getTargetROPumpFlowRate(); + pumpData.roPumpTgtPressure = getTargetROPumpPressure(); + pumpData.measROFlowRate = getMeasuredROFlowRateLPM(); + pumpData.roPumpDutyCycle = roPumpDutyCyclePctSet * FRACTION_TO_PERCENT_FACTOR; + pumpData.roPumpState = (U32)roPumpState; + pumpData.roPumpFBDutyCycle = roPumpFeedbackDutyCyclePct * FRACTION_TO_PERCENT_FACTOR; + pumpData.roPumpMeasFlowWithConcPumps = getMeasuredROFlowRateWithConcPumpsLPM(); - broadcastData( MSG_ID_RO_PUMP_DATA, COMM_BUFFER_OUT_CAN_DG_BROADCAST, (U08*)&pumpData, sizeof( RO_PUMP_DATA_T ) ); roPumpDataPublicationTimerCounter = 0; }