Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -r0c1f66a170a3a0a4324fa1a3c3bfb4c7f77139b5 -r9ce06772b2f651c57144327e6cbf886e2bc22dee --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 0c1f66a170a3a0a4324fa1a3c3bfb4c7f77139b5) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 9ce06772b2f651c57144327e6cbf886e2bc22dee) @@ -53,25 +53,24 @@ #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_P_COEFFICIENT 0.0020 ///< P term for RO pump pressure control. -#define ROP_I_COEFFICIENT 0.0015 ///< I term for RO pump pressure control. +#define ROP_P_COEFFICIENT 0.25 ///< P term for RO pump pressure control. +#define ROP_I_COEFFICIENT 0.25 ///< 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.1 ///< I term for RO pump flow 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.01 ///< 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. /// The time in counts to check the flow and make sure it is in range. -#define FLOW_VERIFICATION_COUNTER_TARGET ( 2 * MS_PER_SECOND / TASK_GENERAL_INTERVAL ) +#define FLOW_VERIFICATION_COUNTER_TARGET ( 2 * MS_PER_SECOND / TASK_GENERAL_INTERVAL ) //TODO remove #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 ROP_FLOW_TO_PWM_SLOPE 0.5 ///< Slope of flow to PWM line equation. -#define ROP_FLOW_TO_PWM_INTERCEPT 0.28 ///< Intercept of flow to PWM line equation. +#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. /// 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 ) @@ -88,16 +87,17 @@ #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 SAFETY_SHUTDOWN_TIMEOUT ( 3 * MS_PER_SECOND ) ///< RO pump safety shutdown activation timeout in ms. -#define ROP_FLOW_STABILIZE_TIME ( 3 * MS_PER_SECOND ) ///< Time required for RO flow to stabilize. +#define ROP_FLOW_STABILIZE_TIME ( 3 * MS_PER_SECOND ) ///< Time required for RO flow to stabilize. TODO remove #define ROP_MAX_RAMP_UP_RETRY 5 ///< Maximum ramp up retires before alarm. +// ************ New defines for pump control ****************// + + /// Enumeration of RO pump states. typedef enum ROPump_States { RO_PUMP_OFF_STATE = 0, ///< RO pump off state - RO_PUMP_CONTROL_SETUP_STATE, ///< RO pump control setup state RO_PUMP_RAMP_UP_STATE, ///< RO pump ramp up to target flow rate state - RO_PUMP_VERIFY_FLOW_STATE, ///< RO pump maintain the flow rate for a set period of time RO_PUMP_CONTROL_TO_TARGET_STATE, ///< RO pump control to target pressure state RO_PUMP_OPEN_LOOP_STATE, ///< RO pump open loop state NUM_OF_RO_PUMP_STATES ///< Number of RO pump states @@ -130,11 +130,8 @@ 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 flowVerificationCounter = 0; ///< Counter to verify the flow is in range. static U32 roControlTimerCounter = 0; ///< determines when to perform control on RO pump. static F32 roPumpOpenLoopTargetDutyCycle = 0; ///< Target RO pump open loop PWM. -static F32 roPumpFlowRateRunningSum = 0; ///< RO pump flow rate running sum. -static F32 roPumpPressureRunningSum = 0; ///< RO pump pressure running sum. /* TODO These variables are used for POST. POST has not been implemented yet static RO_PUMP_SELF_TEST_STATE_T roPumpSelfTestState = RO_PUMP_SELF_TEST_STATE_START; ///< Current RO pump self test state. @@ -143,24 +140,20 @@ static S32 measuredFlowReadingsSum = 0; ///< Raw flow reading sums for averaging. static U32 flowFilterCounter = 0; ///< Flow filtering counter. -static U32 roPumpOnStartTime = 0; ///< Start time when RO pump turned on. -static BOOL setupROPumpControl = 0; ///< Flag to indicate if we need to setup RO pump control. static U32 rampUpRetryCount = 0; ///< Number of ramp up retries. // ********** private function prototypes ********** static RO_PUMP_STATE_T handleROPumpOffState( void ); -static RO_PUMP_STATE_T handleROPumpControlSetupState( void ); static RO_PUMP_STATE_T handleROPumpRampUpState( void ); -static RO_PUMP_STATE_T handleROPumpVerifyFlowState( void ); static RO_PUMP_STATE_T handleROPumpControlToTargetState( void ); static RO_PUMP_STATE_T handleROPumpOpenLoopState( void ); static void setROPumpTargetDutyCycle( F32 duty ); static void setROPumpControlSignalDutyCycle( F32 dutyCycle ); static void stopROPump( void ); static void publishROPumpData( void ); -static U32 getPublishROPumpDataInterval( void ); +static U32 getPublishROPumpDataInterval( void ); /*********************************************************************//** * @brief @@ -180,31 +173,29 @@ initializePIController( PI_CONTROLLER_ID_RO_PUMP, MIN_RO_PUMP_DUTY_CYCLE, ROP_P_COEFFICIENT, ROP_I_COEFFICIENT, MIN_RO_PUMP_DUTY_CYCLE, MAX_RO_PUMP_DUTY_CYCLE ); - // Initialize the I controller during ramp up - initializePIController( I_CONTROLLER_ID_RO_PUMP_RAMP_UP, MIN_RO_PUMP_DUTY_CYCLE, ROP_RAMP_UP_P_COEFFICIENT, ROP_RAMP_UP_I_COEFFICIENT, - MIN_RO_PUMP_DUTY_CYCLE, MAX_RO_PUMP_DUTY_CYCLE ); - // Initialize the persistent alarm for flow out of upper and lower range - initPersistentAlarm( ALARM_ID_FLOW_RATE_OUT_OF_UPPER_RANGE, FLOW_OUT_OF_RANGE_PERSISTENT_INTERVAL, FLOW_OUT_OF_RANGE_PERSISTENT_INTERVAL ); - initPersistentAlarm( ALARM_ID_FLOW_RATE_OUT_OF_LOWER_RANGE, FLOW_OUT_OF_RANGE_PERSISTENT_INTERVAL, FLOW_OUT_OF_RANGE_PERSISTENT_INTERVAL ); + /*initPersistentAlarm( PERSISTENT_ALARM_RO_FLOW_RATE_OUT_OF_UPPER_RANGE, ALARM_ID_FLOW_RATE_OUT_OF_UPPER_RANGE, TRUE, + FLOW_OUT_OF_RANGE_PERSISTENT_INTERVAL, FLOW_OUT_OF_RANGE_PERSISTENT_INTERVAL ); + initPersistentAlarm( PERSISTENT_ALARM_RO_FLOW_RATE_OUT_OF_LOWER_RANGE, ALARM_ID_FLOW_RATE_OUT_OF_LOWER_RANGE, TRUE, + FLOW_OUT_OF_RANGE_PERSISTENT_INTERVAL, FLOW_OUT_OF_RANGE_PERSISTENT_INTERVAL ); + // Initialize the persistent alarm for max allowed pressure out of range - initPersistentAlarm( ALARM_ID_RO_PUMP_PRESSURE_OUT_OF_RANGE, MAX_PRESSURE_OUT_OF_RANGE_PERSISTENT_INTERVAL, MAX_PRESSURE_OUT_OF_RANGE_PERSISTENT_INTERVAL ); + initPersistentAlarm( PERSISTENT_ALARM_RO_PUMP_PRESSURE_OUT_OF_RANGE, ALARM_ID_RO_PUMP_PRESSURE_OUT_OF_RANGE, TRUE, + MAX_PRESSURE_OUT_OF_RANGE_PERSISTENT_INTERVAL, MAX_PRESSURE_OUT_OF_RANGE_PERSISTENT_INTERVAL ); // Initialize the persistent alarm for ramp up to target flow timeout - initPersistentAlarm( ALARM_ID_RO_PUMP_RAMP_UP_TO_FLOW_TIMEOUT, MAX_ALLOWED_RAMP_UP_TIME, MAX_ALLOWED_RAMP_UP_TIME ); + initPersistentAlarm( PERSISTENT_ALARM_RO_PUMP_RAMP_UP_TO_TARGET_FLOW_TIMEOUT, ALARM_ID_RO_PUMP_RAMP_UP_TO_FLOW_TIMEOUT, TRUE, + MAX_ALLOWED_RAMP_UP_TIME, MAX_ALLOWED_RAMP_UP_TIME ); // Initialize the persistent alarm for not turning off the pump - initPersistentAlarm( ALARM_ID_RO_PUMP_OFF_FAULT, SAFETY_SHUTDOWN_TIMEOUT, SAFETY_SHUTDOWN_TIMEOUT ); + initPersistentAlarm( PERSISTEMT_ALARM_RO_PUMP_OFF_ERROR, ALARM_ID_RO_PUMP_OFF_FAULT, TRUE, SAFETY_SHUTDOWN_TIMEOUT, SAFETY_SHUTDOWN_TIMEOUT );*/ // Initialize the variables roControlTimerCounter = 0; roPumpOpenLoopTargetDutyCycle = 0; - roPumpFlowRateRunningSum = 0; - roPumpPressureRunningSum = 0; measuredFlowReadingsSum = 0; flowFilterCounter = 0; - flowVerificationCounter = 0; roPumpDataPublicationTimerCounter = 0; rampUpRetryCount = 0; roPumpState = RO_PUMP_OFF_STATE; @@ -242,7 +233,6 @@ roPumpControlMode = PUMP_CONTROL_MODE_CLOSED_LOOP; // Get the initial guess of the duty cycle roPumpPWMDutyCyclePct = ROP_FLOW_TO_PWM_DC( roFlowRate ); - setupROPumpControl = TRUE; result = TRUE; } // Requested max pressure is out of range @@ -280,7 +270,6 @@ roPumpPWMDutyCyclePct = 0.0; roPumpOpenLoopTargetDutyCycle = 0.0; roControlTimerCounter = 0; - flowVerificationCounter = 0; isROPumpOn = FALSE; targetROPumpPressure = 0; resetPIController( PI_CONTROLLER_ID_RO_PUMP, MIN_RO_PUMP_DUTY_CYCLE ); @@ -378,18 +367,10 @@ roPumpState = handleROPumpOffState(); break; - case RO_PUMP_CONTROL_SETUP_STATE: - roPumpState = handleROPumpControlSetupState(); - break; - case RO_PUMP_RAMP_UP_STATE: roPumpState = handleROPumpRampUpState(); break; - case RO_PUMP_VERIFY_FLOW_STATE: - roPumpState = handleROPumpVerifyFlowState(); - break; - case RO_PUMP_CONTROL_TO_TARGET_STATE: roPumpState = handleROPumpControlToTargetState(); break; @@ -439,7 +420,8 @@ * @brief * The handleROPumpOffState function handles the RO pump off state of the * controller state machine. - * @details Inputs: roPumpControlMode, roPumpPWMDutyCyclePctSet, roPumpOpenLoopTargetDutyCycle + * @details Inputs: roPumpControlMode, roPumpPWMDutyCyclePctSet, + * roPumpOpenLoopTargetDutyCycle * @details Outputs: roPumpPWMDutyCyclePct, setupROPumpControl, isROPumpOn * @return next state of the controller state machine *************************************************************************/ @@ -450,10 +432,12 @@ // If there is a flow, transition to the PI controller to get the corresponding pressure of that flow if ( getTargetROPumpFlowRate() > 0 && roPumpControlMode == PUMP_CONTROL_MODE_CLOSED_LOOP ) { + roPumpControlModeSet = PUMP_CONTROL_MODE_CLOSED_LOOP; // Set pump to on isROPumpOn = TRUE; - result = RO_PUMP_CONTROL_SETUP_STATE; - setupROPumpControl = TRUE; + roPumpDutyCyclePctSet = ROP_FLOW_TO_PWM_DC( getTargetROPumpFlowRate() ); + setROPumpControlSignalDutyCycle( roPumpDutyCyclePctSet ); + result = RO_PUMP_RAMP_UP_STATE; } // If the target duty cycle is greater than zero (minimum is 10%) and the mode has been set to open // loop, set the duty cycle @@ -469,39 +453,6 @@ /*********************************************************************//** * @brief - * The handleROPumpControlSetupState function handles the RO pump control - * setup state of the controller state machine. - * @details Inputs: setupROPumpControl, roPumpPWMDutyCyclePct - * @details Outputs: setup RO pump PI controller and wait for flow stabilize - * @return next state of the controller state machine - *************************************************************************/ -static RO_PUMP_STATE_T handleROPumpControlSetupState( void ) -{ - RO_PUMP_STATE_T result = RO_PUMP_CONTROL_SETUP_STATE; - - if ( TRUE == setupROPumpControl ) - { - roPumpControlModeSet = roPumpControlMode; - // Set initial PWM duty cycle - roPumpDutyCyclePctSet = roPumpPWMDutyCyclePct; - setROPumpControlSignalDutyCycle( roPumpDutyCyclePctSet ); - // Reset controller - resetPIController( I_CONTROLLER_ID_RO_PUMP_RAMP_UP, roPumpDutyCyclePctSet ); - setupROPumpControl = FALSE; - roPumpOnStartTime = getMSTimerCount(); - } - - if ( TRUE == didTimeout( roPumpOnStartTime, ROP_FLOW_STABILIZE_TIME ) ) - { - rampUpRetryCount = 0; - result = RO_PUMP_RAMP_UP_STATE; - } - - return result; -} - -/*********************************************************************//** - * @brief * The handleROPumpRampUpState function handles the RO pump ramp up state * of the controller state machine. * @details Inputs: roControlTimerCounter, roPumpPWMDutyCyclePctSet, @@ -523,14 +474,15 @@ BOOL isFlowOutOfRange = flowRateDeviation > ROP_FLOW_TARGET_TOLERANCE; // If the ramp up persistent alarm is active, turn off the pump and go to off state - if ( TRUE == isPersistentAlarmTriggered( ALARM_ID_RO_PUMP_RAMP_UP_TO_FLOW_TIMEOUT, isFlowOutOfRange ) ) + /*if ( TRUE == isPersistentAlarmTriggered( ALARM_ID_RO_PUMP_RAMP_UP_TO_FLOW_TIMEOUT, isFlowOutOfRange ) ) { stopROPump(); result = RO_PUMP_OFF_STATE; SET_ALARM_WITH_2_F32_DATA( ALARM_ID_RO_PUMP_RAMP_UP_TO_FLOW_TIMEOUT, actualFlowRate, MAX_ALLOWED_RAMP_UP_TIME ); - } + }*/ // Control at set interval - else if ( ++roControlTimerCounter >= ROP_RAMP_UP_CONTROL_INTERVAL ) + //else if ( ++roControlTimerCounter >= ROP_RAMP_UP_CONTROL_INTERVAL ) + if ( ++roControlTimerCounter >= ROP_RAMP_UP_CONTROL_INTERVAL ) { F32 targetPressure = getTargetROPumpPressure(); @@ -540,98 +492,37 @@ // be reset to the corresponding pressure of the target flow rate. if ( ( actualPressure > targetPressure ) || ( ( targetPressure - actualPressure ) < MAX_PRESSURE_TARGET_TOLERANCE ) ) { + resetPIController( PI_CONTROLLER_ID_RO_PUMP, roPumpDutyCyclePctSet ); result = RO_PUMP_CONTROL_TO_TARGET_STATE; } - // If the actual flow is still far from target flow, update the duty cycle using the I controller and stay in this state + // else if ( TRUE == isFlowOutOfRange ) { - roPumpDutyCyclePctSet = runPIController( I_CONTROLLER_ID_RO_PUMP_RAMP_UP, targetFlowRate, actualFlowRate ); + roPumpDutyCyclePctSet += ( targetFlowRate - actualFlowRate ) * 0.5; + //roPumpDutyCyclePctSet = ROP_FLOW_TO_PWM_DC( getTargetROPumpFlowRate() ); setROPumpControlSignalDutyCycle( roPumpDutyCyclePctSet ); } // Reached to the target flow go to the next state else { - // Reset all the variables to prepare for transition - flowVerificationCounter = 0; - roPumpFlowRateRunningSum = 0; - roPumpPressureRunningSum = 0; - result = RO_PUMP_VERIFY_FLOW_STATE; + resetPIController( PI_CONTROLLER_ID_RO_PUMP, roPumpDutyCyclePctSet ); + result = RO_PUMP_CONTROL_TO_TARGET_STATE; } roControlTimerCounter = 0; } - if ( TRUE == setupROPumpControl ) + /*if ( TRUE == setupROPumpControl ) { - resetPersistentAlarmTimer( ALARM_ID_RO_PUMP_RAMP_UP_TO_FLOW_TIMEOUT ); + //resetPersistentAlarmTimer( ALARM_ID_RO_PUMP_RAMP_UP_TO_FLOW_TIMEOUT ); result = RO_PUMP_CONTROL_SETUP_STATE; - } + }*/ return result; } /*********************************************************************//** * @brief - * The handleROPumpVerifyFlowState function handles the RO pump verify - * flow state of the RO pump controller state machine. - * @details Inputs: flowVerificationCounter, targetROPumpPressure, - * roPumpFlowRateRunningSum, roPumpFlowRateRunningSum - * @details Outputs: flowVerificationCounter, targetROPumpPressure, - * roPumpFlowRateRunningSum, roPumpFlowRateRunningSum - * @return next state of the controller state machine - *************************************************************************/ -static RO_PUMP_STATE_T handleROPumpVerifyFlowState( void ) -{ - RO_PUMP_STATE_T result = RO_PUMP_VERIFY_FLOW_STATE; - - // 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 ); - - // Check if the time for flow verification has elapsed - if ( ++flowVerificationCounter >= FLOW_VERIFICATION_COUNTER_TARGET ) - { - - // Calculate the average pressure and flow rate - F32 const targetFlowRate = getTargetROPumpFlowRate(); - F32 const avgFlowRate = roPumpFlowRateRunningSum / flowVerificationCounter; - F32 const flowRateDeviation = fabs( targetFlowRate - avgFlowRate ) / targetFlowRate; - - if ( flowRateDeviation < ROP_FLOW_TARGET_TOLERANCE ) - { - F32 const avgPressure = roPumpPressureRunningSum / flowVerificationCounter; - // If the flow has been achieved without reaching the maximum pressure, set the new pressure - // otherwise, stay with the maximum allowed pressure as target pressure - if ( avgPressure < getTargetROPumpPressure() ) - { - targetROPumpPressure = avgPressure; - } - - // Set initial PWM duty cycle and reset controller - setROPumpControlSignalDutyCycle( roPumpDutyCyclePctSet ); - resetPIController( PI_CONTROLLER_ID_RO_PUMP, roPumpDutyCyclePctSet ); - result = RO_PUMP_CONTROL_TO_TARGET_STATE; - } - else - { - if ( ++rampUpRetryCount > ROP_MAX_RAMP_UP_RETRY ) - { - SET_ALARM_WITH_2_F32_DATA( ALARM_ID_DG_RO_PUMP_TOO_MANY_RAMP_UP_RETRY, rampUpRetryCount, ROP_MAX_RAMP_UP_RETRY ); - } - result = RO_PUMP_RAMP_UP_STATE; - } - } - - if ( TRUE == setupROPumpControl ) - { - result = RO_PUMP_CONTROL_SETUP_STATE; - } - - return result; -} - -/*********************************************************************//** - * @brief * The handleROPumpControlToTargetState function handles the control to * target state of the RO pump controller state machine. * @details Inputs: roPumpPWMDutyCyclePctSet, roControlTimerCounter, @@ -648,17 +539,25 @@ { // Get the pressure to use it for setting the control F32 actualPressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_OUTLET ); + if ( actualPressure >= targetROPumpPressure ) + { + roPumpDutyCyclePctSet -= ROP_RAMP_DOWN_DUTY_CYCLE_RATIO; + resetPIController( PI_CONTROLLER_ID_RO_PUMP, roPumpDutyCyclePctSet ); + } + else + { + roPumpDutyCyclePctSet = runPIController( PI_CONTROLLER_ID_RO_PUMP, getTargetROPumpFlowRate(), getMeasuredROFlowRate() ); + } - roPumpDutyCyclePctSet = runPIController( PI_CONTROLLER_ID_RO_PUMP, getTargetROPumpPressure(), actualPressure ); setROPumpControlSignalDutyCycle( roPumpDutyCyclePctSet ); roControlTimerCounter = 0; } - if ( TRUE == setupROPumpControl ) + /*if ( TRUE == setupROPumpControl ) { result = RO_PUMP_CONTROL_SETUP_STATE; - } + }*/ return result; } @@ -834,8 +733,8 @@ if ( TRUE == isTestingActivated() ) { U32 intvl = value / TASK_PRIORITY_INTERVAL; - roPumpDataPublishInterval.ovData = intvl; - roPumpDataPublishInterval.override = OVERRIDE_KEY; + roPumpDataPublishInterval.ovData = intvl; + roPumpDataPublishInterval.override = OVERRIDE_KEY; result = TRUE; }