Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -r1d235091e158221f1fa8b1579140905a13249a54 -r16a65b53941177fb79af9a262740da5f992f4776 --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 1d235091e158221f1fa8b1579140905a13249a54) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 16a65b53941177fb79af9a262740da5f992f4776) @@ -56,7 +56,7 @@ #define ROP_I_COEFFICIENT 0.0015 ///< I term for RO pump pressure control #define ROP_RAMP_UP_P_COEFFICIENT 0.0 ///< P term for RO pump flow control -#define ROP_RAMP_UP_I_COEFFICIENT 0.009 ///< I term for RO pump flow control +#define ROP_RAMP_UP_I_COEFFICIENT 0.01 ///< I term for RO pump flow control #define ROP_FLOW_TARGET_TOLERANCE 0.05 ///< Tolerance in between the target flow rate and the actual flow rate in liter #define FLOW_SENSOR_ZERO_READING 0xFFFF ///< Flow sensor reading indicates zero flow (or flow lower than can be detected by sensor) @@ -110,18 +110,20 @@ static OVERRIDE_F32_T targetROPumpFlowRate = { 0, 0, 0, 0 }; ///< Target RO flow rate (in LPM_) 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. + 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 LPM). static F32 measuredROPumpPressure = 0.0; ///< measured RO pressure (in PSI). // TODO remove -static F32 tgtROPumpPressure = 0.0; +static F32 tgtROPumpPressure = 0.0; ///< Target RO control pressure (in PSI) static S32 measuredFlowReadingsSum = 0; ///< Raw flow reading sums for averaging. static U32 flowFilterCounter = 0; ///< used to schedule flow filtering. static U32 flowVerificationCounter = 0; ///< Counter to verify the flow is in range static U32 roControlTimerCounter = 0; ///< determines when to perform control on ro pump -static U32 roPumpOpenLooptargetPWM = 0; +static U32 roPumpOpenLooptargetPWM = 0; ///< Target RO pump open loop PWM +static F32 roPumpFlowRateRunningSum = 0; +static F32 roPumpPressureRunningSum = 0; /* The variables used for POST. They are not in use yet, so they are commented out static RO_PUMP_SELF_TEST_STATE_T roPumpSelfTestState = RO_PUMP_SELF_TEST_STATE_START; ///< current ro pump self test state @@ -412,10 +414,11 @@ if ( ++roControlTimerCounter >= ROP_CONTROL_INTERVAL ) { F32 targetFlowRate = targetROPumpFlowRate.data; - if ( OVERRIDE_KEY == targetROPumpFlowRate.override ) + if ( targetROPumpFlowRate.override == OVERRIDE_KEY ) { targetFlowRate = targetROPumpFlowRate.ovData; - } + } + F32 actualFlowRate = (F32)getMeasuredROFlowRate(); if ( fabs( actualFlowRate - targetFlowRate ) > ROP_FLOW_TARGET_TOLERANCE ) @@ -437,47 +440,54 @@ /*********************************************************************//** * @brief - * The handleROPumpVerifyFlowState function handles the ro pump verify \n - * flow state of the ro pump controller state machine. + * The handleROPumpVerifyFlowState function handles the RO pump verify \n + * flow state of the RO pump controller state machine. * @details * Inputs : flowVerificationCounter, roPumpPWMDutyCyclePctSet, \n - * tgtROPumpPressure + * tgtROPumpPressure, roPumpFlowRateRunningSum, roPumpPressureRunningSum * Outputs : flowVerificationCounter, roPumpPWMDutyCyclePctSet, \n - * tgtROPumpPressure + * tgtROPumpPressure, roPumpFlowRateRunningSum, roPumpPressureRunningSum * @return next state *************************************************************************/ static RO_PUMP_STATE_T handleROPumpVerifyFlowState( void ) { RO_PUMP_STATE_T result = RO_PUMP_VERIFY_FLOW_STATE; - F32 actualFlowRate = (F32)getMeasuredROFlowRate(); - F32 targetFlowRate = targetROPumpFlowRate.data; - if ( OVERRIDE_KEY == targetROPumpFlowRate.override ) - { - targetFlowRate = targetROPumpFlowRate.ovData; - } + // Calculate the running sum of the flow rate and RO pump outlet pressure + roPumpFlowRateRunningSum = roPumpFlowRateRunningSum + (F32)getMeasuredROFlowRate(); + roPumpPressureRunningSum = roPumpPressureRunningSum + getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_OUTLET ); - if ( fabs( actualFlowRate - targetFlowRate ) <= ROP_FLOW_TARGET_TOLERANCE ) + // Check if the time for flow verification has elapsed + if ( ++flowVerificationCounter >= FLOW_VERIFICATION_COUNTER_TARGET ) { - if ( ++flowVerificationCounter >= FLOW_VERIFICATION_COUNTER_TARGET ) - { - // Reset the P controller for the flow rate - resetPIController( I_CONTROLLER_ID_RO_PUMP_RAMP_UP, MIN_RO_PUMP_PWM_DUTY_CYCLE ); - tgtROPumpPressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_OUTLET ); + // Calculate the average pressure and flow rate + F32 avgPressure = roPumpPressureRunningSum / flowVerificationCounter; + F32 avgFlowRate = roPumpFlowRateRunningSum / flowVerificationCounter; - // set initial PWM duty cycle - roPumpPWMDutyCyclePctSet = roPumpPWMDutyCyclePct; - setROPumpControlSignalPWM( roPumpPWMDutyCyclePctSet ); - // reset controller - resetPIController( PI_CONTROLLER_ID_RO_PUMP, roPumpPWMDutyCyclePctSet ); - flowVerificationCounter = 0; - result = RO_PUMP_CONTROL_TO_TARGET_STATE; + F32 targetFlowRate = targetROPumpFlowRate.data; + if ( targetROPumpFlowRate.override == OVERRIDE_KEY ) + { + targetFlowRate = targetROPumpFlowRate.ovData; } - } - else - { + // Calculate the flow rate deviation from the target flow rate + F32 flowRateDeviation = ( targetFlowRate - avgFlowRate ) / targetFlowRate; + // Use the flow rate deviation to adjust the average calculated pressure. This + // pressure is used as the target pressure + avgPressure = avgPressure + ( avgPressure * flowRateDeviation ); + + // Reset the P controller for the flow rate + resetPIController( I_CONTROLLER_ID_RO_PUMP_RAMP_UP, MIN_RO_PUMP_PWM_DUTY_CYCLE ); + tgtROPumpPressure = avgPressure; + + // set initial PWM duty cycle + roPumpPWMDutyCyclePctSet = roPumpPWMDutyCyclePct; + setROPumpControlSignalPWM( roPumpPWMDutyCyclePctSet ); + // reset controller + resetPIController( PI_CONTROLLER_ID_RO_PUMP, roPumpPWMDutyCyclePctSet ); flowVerificationCounter = 0; - result = RO_PUMP_RAMP_UP_STATE; + roPumpFlowRateRunningSum = 0; + roPumpPressureRunningSum = 0; + result = RO_PUMP_CONTROL_TO_TARGET_STATE; } return result; @@ -654,10 +664,9 @@ // publish RO pump data on interval if ( ++roPumpDataPublicationTimerCounter >= getPublishROPumpDataInterval() ) { - F32 targetPressure = measuredROPumpPressure; F32 measFlow = getMeasuredROFlowRate(); F32 pumpPWMPctDutyCycle = roPumpPWMDutyCyclePctSet * FRACTION_TO_PERCENT_FACTOR; - broadcastROPumpData( targetPressure, measFlow, pumpPWMPctDutyCycle, (U32)roPumpState ); + broadcastROPumpData( tgtROPumpPressure, measFlow, pumpPWMPctDutyCycle, (U32)roPumpState ); roPumpDataPublicationTimerCounter = 0; } }