Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -r59543c15efd37e0e23269768df9a1cb9b6a3d296 -rdcd360fb4dc37db2dcbeb7fb14fb327fe68235f4 --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 59543c15efd37e0e23269768df9a1cb9b6a3d296) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision dcd360fb4dc37db2dcbeb7fb14fb327fe68235f4) @@ -1,17 +1,17 @@ /************************************************************************** * -* Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. +* Copyright (c) 2019-2021 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * -* @file ROPump.c +* @file ROPump.c * -* @author (last) Quang Nguyen -* @date (last) 14-Sep-2020 +* @author (last) Dara Navaei +* @date (last) 09-Nov-2021 * -* @author (original) Sean -* @date (original) 04-Apr-2020 +* @author (original) Sean +* @date (original) 04-Apr-2020 * ***************************************************************************/ @@ -21,7 +21,9 @@ #include "mibspi.h" #include "FPGA.h" +#include "InternalADC.h" #include "NVDataMgmt.h" +#include "MessageSupport.h" #include "OperationModes.h" #include "PersistentAlarm.h" #include "PIControllers.h" @@ -64,7 +66,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 pulse period (2us units) to flow rate (liters/min) for RO flow rate (divide this by pulse period). +#define RO_FLOW_ADC_TO_LPM_FACTOR 300 ///< 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. @@ -83,6 +85,10 @@ #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 SAFETY_SHUTDOWN_TIMEOUT ( 3 * MS_PER_SECOND ) ///< RO pump safety shutdown activation timeout in ms. +#define ROP_FEEDBACK_0_PCT_DUTY_CYCLE_VOLTAGE 0.0 ///< RO pump 0% duty cycle feedback voltage. +#define ROP_FEEDBACK_100_PCT_DUTY_CYCLE_VOLTAGE 2.5 ///< RO pump 100% duty cycle feedback voltage. +#define ROP_DUTY_CYCLE_OUT_OF_RANGE_TOLERANCE 0.05 ///< RO pump duty cycle out of range tolerance. + /// Enumeration of RO pump states. typedef enum ROPump_States { @@ -110,6 +116,7 @@ static BOOL isROPumpOn = FALSE; ///< RO pump is currently running. static F32 roPumpPWMDutyCyclePct = 0.0; ///< Initial RO pump PWM duty cycle. static F32 roPumpDutyCyclePctSet = 0.0; ///< Currently set RO pump PWM duty cycle. +static F32 roPumpFeedbackDutyCyclePct = 0.0; ///< RO pump feedback duty cycle in percent. static PUMP_CONTROL_MODE_T roPumpControlMode = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< Requested RO pump control mode. static F32 pendingROPumpCmdMaxPressure = 0.0; ///< Delayed (pending) RO pump max pressure (in PSI) setting. @@ -120,8 +127,7 @@ static F32 targetROPumpMaxPressure = 0.0; ///< Target RO max allowed pressure (in PSI). static OVERRIDE_U32_T roPumpDataPublishInterval = { RO_PUMP_DATA_PUB_INTERVAL, - RO_PUMP_DATA_PUB_INTERVAL, - 0, 0 }; ///< Interval (in ms) at which to publish RO flow data to CAN bus. + RO_PUMP_DATA_PUB_INTERVAL, 0, 0 }; ///< Interval (in ms) at which to publish RO flow data to CAN bus. static OVERRIDE_F32_T measuredROFlowRateLPM = { 0.0, 0.0, 0.0, 0 }; ///< Measured RO flow rate (in L/min). static U32 roControlTimerCounter = 0; ///< Determines when to perform control on RO pump. @@ -188,6 +194,7 @@ roPumpState = RO_PUMP_OFF_STATE; roPumpControlMode = NUM_OF_PUMP_CONTROL_MODES; isROPumpOn = FALSE; + roPumpFeedbackDutyCyclePct = 0.0; } /*********************************************************************//** @@ -324,15 +331,16 @@ *************************************************************************/ void execROPumpMonitor( void ) { - U16 roFlowReading = getFPGAROPumpFlowRate(); + U16 roFlowReading = getFPGAROPumpFlowRate(); + U16 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; + BOOL isPressureMax = ( actualPressure >= MAX_ALLOWED_MEASURED_PRESSURE_PSI ? TRUE : FALSE ); checkPersistentAlarm( ALARM_ID_RO_PUMP_PRESSURE_OUT_OF_RANGE, isPressureMax, actualPressure, MAX_ALLOWED_MEASURED_PRESSURE_PSI ); // Check if a new calibration is available @@ -345,57 +353,67 @@ // Read flow at the control set if ( ++flowFilterCounter >= FLOW_SAMPLES_TO_AVERAGE ) { - 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 + + flow * flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].gain + flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].offset; - // 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 = 0.0; - } + // 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 = 0.0; + } measuredFlowReadingsSum = 0; - flowFilterCounter = 0; + flowFilterCounter = 0; } #ifndef IGNORE_RO_PUMP_MONITOR + F32 roFeedbackDutyCycle = ROP_FEEDBACK_100_PCT_DUTY_CYCLE_VOLTAGE / roFeedbackVoltage; + // 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 const currentFlow = getMeasuredROFlowRate(); - F32 const targetFlow = getTargetROPumpFlowRate(); + F32 currentFlow = getMeasuredROFlowRate(); + F32 targetFlow = getTargetROPumpFlowRate(); // The flow cannot be out of range for than 10% of the target flow - BOOL const isFlowOutOfRange = fabs( 1.0 - ( currentFlow / targetFlow ) ) > MAX_ALLOWED_FLOW_DEVIATION; + 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 const isFlowOutOfUpperRange = isFlowOutOfRange && ( currentFlow > targetFlow ); - BOOL const isFlowOutOfLowerRange = isFlowOutOfRange && ( currentFlow < targetFlow ); + BOOL isFlowOutOfUpperRange = ( isFlowOutOfRange && ( currentFlow > targetFlow ) ? TRUE : FALSE ); + BOOL isFlowOutOfLowerRange = ( isFlowOutOfRange && ( currentFlow < targetFlow ) ? 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 ); } - // If the pump is off and PPi + 5psi < PPo for a certain period of time, activate safety shutdown - if ( FALSE == isROPumpOn ) + if ( ( FALSE == isROPumpOn ) && ( roFeedbackVoltage != ROP_FEEDBACK_0_PCT_DUTY_CYCLE_VOLTAGE ) ) { - F32 pressureInlet = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_INLET ); - BOOL isPumpRunning = ( pressureInlet + MAX_PRESSURE_TARGET_TOLERANCE ) < actualPressure; + checkPersistentAlarm( ALARM_ID_RO_PUMP_DUTY_CYCLE_OUT_OF_RANGE, TRUE, roFeedbackVoltage, ROP_FEEDBACK_0_PCT_DUTY_CYCLE_VOLTAGE ); - checkPersistentAlarm( ALARM_ID_RO_PUMP_OFF_FAULT, isPumpRunning, pressureInlet, ( pressureInlet + MAX_PRESSURE_TARGET_TOLERANCE ) ); - // Check if it has timed out - if ( TRUE == isAlarmActive( ALARM_ID_RO_PUMP_OFF_FAULT ) ) + if ( TRUE == isAlarmActive( ALARM_ID_RO_PUMP_DUTY_CYCLE_OUT_OF_RANGE ) ) { activateSafetyShutdown(); } } + + if ( TRUE == isROPumpOn ) + { + BOOL isDCOutOfRange = ( fabs( roFeedbackDutyCycle - roPumpDutyCyclePctSet ) > ROP_DUTY_CYCLE_OUT_OF_RANGE_TOLERANCE ? TRUE : FALSE ); + roPumpFeedbackDutyCyclePct = roFeedbackDutyCycle; + + checkPersistentAlarm( ALARM_ID_RO_PUMP_DUTY_CYCLE_OUT_OF_RANGE, TRUE, roFeedbackDutyCycle, roPumpDutyCyclePctSet ); + } + else + { + checkPersistentAlarm( ALARM_ID_RO_PUMP_DUTY_CYCLE_OUT_OF_RANGE, FALSE, roFeedbackDutyCycle, roPumpDutyCyclePctSet ); + } #endif // Publish RO pump data on interval @@ -646,7 +664,6 @@ // Get the pressure to use it for setting the control F32 actualPressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_OUTLET ); - if ( actualPressure >= targetROPumpMaxPressure ) { resetPIController( PI_CONTROLLER_ID_RO_PUMP_MAX_PRES, roPumpDutyCyclePctSet ); @@ -741,6 +758,9 @@ roPumpPWMDutyCyclePct = roPumpOpenLoopTargetDutyCycle; roPumpDutyCyclePctSet = roPumpPWMDutyCyclePct; roPumpControlMode = PUMP_CONTROL_MODE_OPEN_LOOP; + + // Set the new duty cycle of the pump + setROPumpControlSignalDutyCycle( roPumpOpenLoopTargetDutyCycle ); } /*********************************************************************//** @@ -832,8 +852,10 @@ pumpData.measROFlowRate = getMeasuredROFlowRate(); pumpData.roPumpDutyCycle = roPumpDutyCyclePctSet * FRACTION_TO_PERCENT_FACTOR; pumpData.roPumpState = (U32)roPumpState; + pumpData.roPumpFBDutyCycle = roPumpFeedbackDutyCyclePct * FRACTION_TO_PERCENT_FACTOR; - broadcastROPumpData( &pumpData ); + + broadcastData( MSG_ID_RO_PUMP_DATA, COMM_BUFFER_OUT_CAN_DG_BROADCAST, (U08*)&pumpData, sizeof( RO_PUMP_DATA_T ) ); roPumpDataPublicationTimerCounter = 0; } } @@ -905,7 +927,7 @@ if ( TRUE == isTestingActivated() ) { // The flow rate and pressure must be in range - if ( flow <= MAX_RO_FLOWRATE_LPM && flow >= MIN_RO_FLOWRATE_LPM ) + if ( ( flow <= MAX_RO_FLOWRATE_LPM ) && ( flow >= MIN_RO_FLOWRATE_LPM ) ) { result = setROPumpTargetFlowRate( flow, MAX_ALLOWED_PRESSURE_PSI ); }