Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -r45263215b372cd579e8e16bb8073c287c726c55d -r1f500f8e6159a3fbab85ea68389e918a6df66400 --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 45263215b372cd579e8e16bb8073c287c726c55d) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 1f500f8e6159a3fbab85ea68389e918a6df66400) @@ -45,36 +45,37 @@ #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_PWM_STEP_CHANGE 0.01 ///< max duty cycle change for controller -#define MAX_RO_PUMP_PWM_DUTY_CYCLE 0.99 ///< max duty cycle -#define MIN_RO_PUMP_PWM_DUTY_CYCLE 0.00 ///< min duty cycle +#define MAX_RO_PUMP_PWM_STEP_CHANGE 0.01 ///< max duty cycle change for controller. +#define MAX_RO_PUMP_PWM_DUTY_CYCLE 0.99 ///< max duty cycle. +#define MIN_RO_PUMP_PWM_DUTY_CYCLE 0.00 ///< 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_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_CONTROL_INTERVAL ( MS_PER_SECOND / 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_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.05 ///< Tolerance in between the target flow rate and the actual flow rate in liter +#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.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) +#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_VERIFICATION_COUNTER_TARGET 40U ///< The time in counts to check the flow and make sure it is in range +#define FLOW_AVERAGE_MULTIPLIER ( 1.0 / (F32)FLOW_SAMPLES_TO_AVERAGE ) ///< Optimization - multiplying is faster than dividing. +#define FLOW_VERIFICATION_COUNTER_TARGET 40U ///< The time in counts to check the flow and make sure it is in range. #define RO_FLOW_ADC_TO_LPM_FACTOR 10909.0909 ///< conversion factor from ADC counts to LPM (liters/min) for RO flow rate (multiply this by inverse of FPGA reading). -// Initial PWM for the requested flow rate. It is assumed that 100% duty cycle will provide 1.2 LPM -#define ROP_FLOW_TO_PWM_DC(flow) ( (F32)(flow / 1.2) ) ///< Initial conversion factor from target flow rate to PWM duty cycle estimate +#define ROP_FLOW_TO_PWM_DC(flow) ( (F32)( flow / MAX_RO_FLOWRATE_LPM ) ) ///< Initial conversion factor from target flow rate to PWM duty cycle estimate. +/* TODO remove #define MAX_RO_PUMP_PWM_STEP_CHANGE 0.01 ///< Max duty cycle change for controller. #define MAX_RO_PUMP_PWM_DUTY_CYCLE 0.99 ///< Max duty cycle. #define MIN_RO_PUMP_PWM_DUTY_CYCLE 0.00 ///< 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_P_COEFFICIENT 0.0020 ///< P term for RO pump control. #define ROP_I_COEFFICIENT 0.0015 ///< I term for RO pump control. +*/ #define FLOW_SENSOR_ZERO_READING 0xFFFF ///< Flow sensor reading indicates zero flow (or flow lower than can be detected by sensor). @@ -118,7 +119,7 @@ static BOOL isROPumpOn = FALSE; ///< RO pump is currently running static F32 roPumpPWMDutyCyclePct = 0.0; ///< initial RO pump PWM duty cycle static F32 roPumpPWMDutyCyclePctSet = 0.0; ///< currently set RO pump PWM duty cycle -static PUMP_CONTROL_MODE_T roPumpControlMode = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< requested RO pump control mode //TODO remove? +static PUMP_CONTROL_MODE_T roPumpControlMode = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< requested RO pump control mode static PUMP_CONTROL_MODE_T roPumpControlModeSet = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< currently set RO pump control mode static OVERRIDE_F32_T targetROPumpFlowRate = { 0, 0, 0, 0 }; ///< Target RO flow rate (in LPM) @@ -151,7 +152,10 @@ 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 RO_PUMP_STATE_T handleROPumpOpenLoopState( void ); + +static BOOL setROPumpTargetPressure( U32 roPressure, PUMP_CONTROL_MODE_T mode ); +static BOOL setROPumpTargetPWM( F32 pwm ); static void setROPumpControlSignalPWM( F32 newPWM ); static void stopROPump( void ); static void publishROPumpData( void ); @@ -180,65 +184,28 @@ MIN_RO_PUMP_PWM_DUTY_CYCLE, MAX_RO_PUMP_PWM_DUTY_CYCLE ); } -//TODO TEST AND REMOVE /*********************************************************************//** * @brief - * The setROPumpTargetPressure function sets a new target pressure for the RO pump. - * @details - * Inputs : none - * Outputs : targetROPumpPressure, roPumpPWMDutyCyclePct - * @param roPressure new target RO pressure - * @param mode new control mode - * @return TRUE if new target pressure is set, FALSE if not - *************************************************************************/ -BOOL setROPumpTargetPressure( U32 roPressure, PUMP_CONTROL_MODE_T mode ) -{ - BOOL result = FALSE; - - // verify pressure - if ( roPressure >= 10 && roPressure <= 50 ) - { - targetROPumpPressure.data = roPressure; - roPumpControlMode = mode; - // set PWM duty cycle target to an estimated initial target based on target pressure - then we'll control to pressure going forward -#ifdef EMC_TEST_BUILD - roPumpPWMDutyCyclePct = 1.0; -#else - roPumpPWMDutyCyclePct = ROP_PSI_TO_PWM_DC( roPressure ); -#endif - - result = TRUE; - } - else // requested pressure out of range - { - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, 0, roPressure ) // TODO - replace 1st param with s/w fault enum - } - - return result; -} -//TODO TEST AND REMOVE - -/*********************************************************************//** - * @brief * The setROPumpTargetPWM function sets the PWM that the pump should run * @details * Inputs: roPumpOpenLooptargetPWM, roPumpControlMode * Outputs: roPumpOpenLooptargetPWM, roPumpControlMode * @param: pwm * @return whether the value was set (TRUE) or not (FALSE) *************************************************************************/ -BOOL setROPumpTargetPWM( F32 pwm ) +static BOOL setROPumpTargetPWM( F32 pwm ) { BOOL result = FALSE; - if ( pwm > 0 ) + if ( pwm >= MIN_RO_PUMP_PWM_DUTY_CYCLE && pwm < MAX_RO_PUMP_PWM_DUTY_CYCLE ) { roPumpOpenLoopTargetPWM = pwm; - roPumpPWMDutyCyclePct = roPumpOpenLoopTargetPWM; //ROP_FLOW_TO_PWM_DC( getTargetROPumpFlowRate() ); + roPumpPWMDutyCyclePct = roPumpOpenLoopTargetPWM; roPumpPWMDutyCyclePctSet = roPumpPWMDutyCyclePct; roPumpControlMode = PUMP_CONTROL_MODE_OPEN_LOOP; result = TRUE; } + return result; } @@ -272,7 +239,7 @@ } else // requested pressure out of range { - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, 0, roFlowRate ) // TODO - replace 1st param with s/w fault enum + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_RO_PUMP_INVALID_FLOW_RATE_SET, roFlowRate ) } return result; @@ -320,7 +287,7 @@ F32 avgROFlow = (F32)measuredFlowReadingsSum * FLOW_AVERAGE_MULTIPLIER; // If the flow is less than a certain value, FPGA will return 0xFFFF meaning that - // the flow is 0 + // the flow is 0. Otherwise, convert the count to flow rate in mL/min if ( ( roFlowReading == FLOW_SENSOR_ZERO_READING ) || ( roFlowReading == 0 ) ) { measuredROFlowRateLPM.data = 0.0; @@ -333,13 +300,14 @@ flowFilterCounter = 0; } - //measuredROPumpPressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_OUTLET ); TODO what do we do with this? + //measuredROPumpPressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_OUTLET ); TODO what do we do with this? + // TODO - check pressure? // TODO - check flow? - // publish RO pump data on interval + // Publish RO pump data on interval publishROPumpData(); } @@ -376,7 +344,7 @@ break; default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, 0, roPumpState ) // TODO - replace 1st param with s/w fault enum + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_RO_PUMP_INVALID_EXEC_STATE, roPumpState ) roPumpState = RO_PUMP_OFF_STATE; break; } @@ -614,6 +582,42 @@ static RO_PUMP_STATE_T handleROPumpOpenLoopState( void ) { return RO_PUMP_OPEN_LOOP_STATE; +} + +/*********************************************************************//** + * @brief + * The setROPumpTargetPressure function sets a new target pressure for the RO pump. + * @details + * Inputs : none + * Outputs : targetROPumpPressure, roPumpPWMDutyCyclePct + * @param roPressure new target RO pressure + * @param mode new control mode + * @return TRUE if new target pressure is set, FALSE if not + *************************************************************************/ +static BOOL setROPumpTargetPressure( U32 roPressure, PUMP_CONTROL_MODE_T mode ) +{ + BOOL result = FALSE; + + // Verify pressure + if ( roPressure >= 10 && roPressure <= 50 ) + { + targetROPumpPressure.data = roPressure; + roPumpControlMode = mode; + // set PWM duty cycle target to an estimated initial target based on target pressure - then we'll control to pressure going forward +#ifdef EMC_TEST_BUILD + roPumpPWMDutyCyclePct = 1.0; +#else + roPumpPWMDutyCyclePct = ROP_PSI_TO_PWM_DC( roPressure ); +#endif + + result = TRUE; + } + else // requested pressure out of range + { + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, 0, roPressure ) // TODO - replace 1st param with s/w fault enum + } + + return result; } /*********************************************************************//**