Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -r00b32a5dad2e136d31cfaf0de16f7767b9920fec -rd0ddf8fe0bc062cbf2f3520134dbe1d5a5b85115 --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 00b32a5dad2e136d31cfaf0de16f7767b9920fec) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision d0ddf8fe0bc062cbf2f3520134dbe1d5a5b85115) @@ -20,6 +20,7 @@ #include "etpwm.h" #include "mibspi.h" +#include "AlarmDefs.h" #include "FPGA.h" #include "OperationModes.h" #include "PIControllers.h" @@ -76,10 +77,8 @@ #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 ( 30 * MS_PER_SECOND ) ///< Maximum allowed ramp up time to a flow rate in ms. -#define RAMP_UP_TIME_OUT_COUNT ( MAX_ALLOWED_RAMP_UP_TIME / \ - TASK_GENERAL_INTERVAL ) ///< Ramp up time out in counts. -#define ROP_PSI_TO_PWM_DC(p) ( 0.2 + ( (F32)((p) - 100) * 0.01 ) ) ///< conversion factor from target PSI to PWM duty cycle estimate. TODO remove? +#define MAX_ALLOWED_RAMP_UP_TIME ( 5 * 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. /// Enumeration of RO pump states. typedef enum ROPump_States @@ -132,7 +131,6 @@ static S32 measuredFlowReadingsSum = 0; ///< Raw flow reading sums for averaging static U32 flowFilterCounter = 0; ///< Flow filtering counter -static U32 rampUp2FlowTimeoutCounter = 0; ///< Counter for ramping up to flow time // ********** private function prototypes ********** @@ -151,8 +149,11 @@ /*********************************************************************//** * @brief * The initROPump function initializes the RO Pump module. - * @details Inputs: rampUp2FlowTimeoutCounter - * @details Outputs: rampUp2FlowTimeoutCounter + * @details Inputs: roControlTimerCounter,roPumpOpenLoopTargetDutyCycle, + * roPumpFlowRateRunningSum, roPumpPressureRunningSum, measuredFlowReadingsSum, + * flowFilterCounter, flowVerificationCounter, roPumpState, roPumpControlMode + * roPumpDataPublicationTimerCounter, roPumpControlModeSet + * @details Outputs: none * @return none *************************************************************************/ void initROPump( void ) @@ -178,15 +179,17 @@ 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( 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 variables - rampUp2FlowTimeoutCounter = 0; roControlTimerCounter = 0; roPumpOpenLoopTargetDutyCycle = 0; roPumpFlowRateRunningSum = 0; roPumpPressureRunningSum = 0; measuredFlowReadingsSum = 0; flowFilterCounter = 0; - rampUp2FlowTimeoutCounter = 0; flowVerificationCounter = 0; roPumpDataPublicationTimerCounter = 0; roPumpState = RO_PUMP_OFF_STATE; @@ -224,7 +227,6 @@ roPumpControlMode = PUMP_CONTROL_MODE_CLOSED_LOOP; // Get the initial guess of the duty cycle roPumpPWMDutyCyclePct = ROP_FLOW_TO_PWM_DC( roFlowRate ); - rampUp2FlowTimeoutCounter = 0; result = TRUE; } // Requested max pressure is out of range @@ -264,7 +266,6 @@ roControlTimerCounter = 0; flowVerificationCounter = 0; isROPumpOn = FALSE; - rampUp2FlowTimeoutCounter = 0; targetROPumpPressure = 0; resetPIController( PI_CONTROLLER_ID_RO_PUMP, MIN_RO_PUMP_DUTY_CYCLE ); } @@ -389,9 +390,8 @@ * @brief * The execROPumpTest function executes the state machine for the RO pump * self-test. - * @details - * Inputs: TODO FILL UP - * Outputs: TODO FILL UP + * @details Inputs: TODO FILL UP + * @details Outputs: TODO FILL UP * @return the current state of the ROPump self test. *************************************************************************/ SELF_TEST_STATUS_T execROPumpTest( void ) @@ -459,26 +459,28 @@ // Get the current pressure from the sensor F32 actualPressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_OUTLET ); + F32 targetFlowRate = getTargetROPumpFlowRate(); + F32 actualFlowRate = (F32)getMeasuredROFlowRate(); - // If we are still in the ramp up mode after the specified time, get the current pressure - // and control from this - if ( ++rampUp2FlowTimeoutCounter > RAMP_UP_TIME_OUT_COUNT ) + BOOL isFlowInRange = fabs( targetFlowRate - actualFlowRate ) > ROP_FLOW_TARGET_TOLERANCE; + + // Check if the ramp up has timed out + checkPersistentAlarm( PERSISTENT_ALARM_RO_PUMP_RAMP_UP_TO_TARGET_FLOW_TIMEOUT, isFlowInRange, actualFlowRate, MAX_ALLOWED_RAMP_UP_TIME ); + + // If the ramp up persistent alarm is active, turn off the pump and go to off state + if ( isAlarmActive( ALARM_ID_RO_PUMP_RAMP_UP_TO_FLOW_TIMEOUT ) ) { - targetROPumpPressure = actualPressure; - rampUp2FlowTimeoutCounter = 0; - result = RO_PUMP_CONTROL_TO_TARGET_STATE; + stopROPump(); + result = RO_PUMP_OFF_STATE; } // Control at set interval else if ( ++roControlTimerCounter >= ROP_CONTROL_INTERVAL ) { - F32 targetFlowRate = getTargetROPumpFlowRate(); - F32 actualFlowRate = (F32)getMeasuredROFlowRate(); - // 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 // be reset to the corresponding pressure of the target flow rate. - if ( actualPressure > getTargetROPumpPressure() || ( getTargetROPumpPressure() - actualPressure ) > MAX_PRESSURE_TARGET_TOLERANCE ) + if ( actualPressure > getTargetROPumpPressure() || ( getTargetROPumpPressure() - actualPressure ) < MAX_PRESSURE_TARGET_TOLERANCE ) { result = RO_PUMP_CONTROL_TO_TARGET_STATE; }