Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -r2ac0ed667ccf0d77a65905157af9badbd19007cd -r17a448770daa138ebeb6ce79974966e650828f25 --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 2ac0ed667ccf0d77a65905157af9badbd19007cd) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 17a448770daa138ebeb6ce79974966e650828f25) @@ -17,14 +17,14 @@ //#include -//#include "FlowSensors.h" +#include "FlowSensor.h" //#include "NVDataMgmt.h" #include "Messaging.h" #include "MessageSupport.h" #include "OperationModes.h" #include "PersistentAlarm.h" -//#include "PIControllers.h" -//#include "Pressures.h" +#include "PIControllers.h" +#include "Pressure.h" #include "ROPump.h" #include "SafetyShutdown.h" #include "TaskGeneral.h" @@ -38,115 +38,146 @@ // ********** private definitions ********** -#define RO_PUMP_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the RO Pump data is published on the CAN bus. -#define ROP_CONTROL_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the RO pump is controlled. +#define RO_PUMP_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the RO Pump data is published on the CAN bus. +#define ROP_CONTROL_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the RO pump is controlled. -//#define ROP_FLOW_CONTROL_P_COEFFICIENT 0.15F ///< P term for RO pump flow control. -//#define ROP_FLOW_CONTROL_I_COEFFICIENT 0.65F ///< I term for RO pump flow control. -//#define ROP_MAX_PRESSURE_P_COEFFICIENT 0.01F ///< P term for RO pump max pressure control. -//#define ROP_MAX_PRESSURE_I_COEFFICIENT 0.01F ///< I term for RO pump max pressure control. -//#define ROP_PWM_STEP_LIMIT 0.50F ///< Current maximum PWM step limit used in RO Profiles. +#define ROP_FLOW_CONTROL_P_COEFFICIENT 0.15F ///< P term for RO pump flow control. +#define ROP_FLOW_CONTROL_I_COEFFICIENT 0.65F ///< I term for RO pump flow control. +#define ROP_PRESSURE_CONTROL_P_COEFFICIENT 0.01F ///< P term for RO pump pressure control. +#define ROP_PRESSURE_CONTROL_I_COEFFICIENT 0.01F ///< I term for RO pump pressure control. +#define ROP_PWM_STEP_LIMIT 0.50F ///< Current maximum PWM step limit used in RO Profiles. +#define ROP_RAMP_DOWN_DUTY_CYCLE_RATIO 0.03F ///< Pump ramp down duty cycle ratio when the pressure higher than max defined. -#define DATA_PUBLISH_COUNTER_START_COUNT 10 ///< Data publish counter start count. +#define ROP_FLOW_TO_PWM_SLOPE 0.5F ///< Slope of flow to PWM line equation. +#define ROP_FLOW_TO_PWM_INTERCEPT 0.0F ///< Intercept of flow to PWM line equation. +#define ROP_PRESSURE_TO_PWM_SLOPE 0.5F ///< Slope of pressure to PWM line equation. +#define ROP_PRESSURE_TO_PWM_INTERCEPT 0.0F ///< Intercept of pressure to PWM line equation. +#define DATA_PUBLISH_COUNTER_START_COUNT 10 ///< Data publish counter start count. + +#define ROP_FLOW_TO_PWM(flow) ( ROP_FLOW_TO_PWM_SLOPE * flow + ROP_FLOW_TO_PWM_INTERCEPT ) ///< PWM line equation for flow. +#define ROP_PRESSURE_TO_PWM(pres) ( ROP_PRESSURE_TO_PWM_SLOPE * pres + ROP_PRESSURE_TO_PWM_INTERCEPT ) ///< PWM line equation for pressure. + + + /// Enumeration of RO pump states. typedef enum ROPump_States { - RO_PUMP_OFF_STATE = 0, ///< RO pump off state. - RO_PUMP_OPEN_LOOP_STATE, ///< RO pump open loop state. - NUM_OF_RO_PUMP_STATES ///< Number of RO pump states. + RO_PUMP_OFF_STATE = 0, ///< RO pump off state. + RO_PUMP_CONTROL_TO_TARGET_FLOW_STATE, ///< RO pump control to target flow state. + RO_PUMP_CONTROL_TO_TARGET_PRESSURE_STATE, ///< RO pump control to max pressure state. + RO_PUMP_OPEN_LOOP_STATE, ///< RO pump open loop state. + NUM_OF_RO_PUMP_STATES ///< Number of RO pump states. } RO_PUMP_STATE_T; -/// Enumeration of RO pump self-test states. -//typedef enum ROPump_Self_Test_States -//{ -// RO_PUMP_SELF_TEST_STATE_START = 0, ///< RO pump self-test start state. -// RO_PUMP_TEST_STATE_IN_PROGRESS, ///< RO pump self-tests in progress state. -// RO_PUMP_TEST_STATE_COMPLETE, ///< RO pump self-tests completed state. -// NUM_OF_RO_PUMP_SELF_TEST_STATES ///< Number of RO pump self-test states. -//} RO_PUMP_SELF_TEST_STATE_T; - // ********** private data ********** -static RO_PUMP_STATE_T roPumpState[ NUM_OF_BOOST_PUMPS ]; ///< Current state of pump controller state machine. -static PUMP_CONTROL_MODE_T roPumpControlMode[ NUM_OF_BOOST_PUMPS ]; ///< Requested pump control mode. -static BOOL isROPumpOn[ NUM_OF_BOOST_PUMPS ]; ///< Flag indicating whether pump is currently running. -static BOOL stopPumpRequest[ NUM_OF_BOOST_PUMPS ]; ///< Flag indicating pump stop is requested. -static U32 roPumpDataPublicationTimerCounter; ///< Used to schedule RO pump data publication to CAN bus. -static OVERRIDE_U32_T roPumpDataPublishInterval; ///< Interval (in ms) at which to publish boost pump data to CAN bus. -static U32 roControlTimerCounter; ///< Determines when to perform control on RO pump. -static U32 roPumpControlInterval; ///< RO pump Control interval. - +static RO_PUMP_STATE_T roPumpState; ///< Current state of pump controller state machine. +static PUMP_CONTROL_MODE_T roPumpControlMode; ///< Requested pump control mode. +static BOOL isROPumpOn; ///< Flag indicating whether pump is currently running. +static BOOL stopPumpRequest; ///< Flag indicating pump stop is requested. +static U32 roPumpDataPublicationTimerCounter; ///< Used to schedule RO pump data publication to CAN bus. +static OVERRIDE_U32_T roPumpDataPublishInterval; ///< Interval (in ms) at which to publish boost pump data to CAN bus. +static U32 roControlTimerCounter; ///< Determines when to perform control on RO pump. +static OVERRIDE_F32_T targetROPumpFlowRate; ///< Target RO flow rate (in L/min). +static OVERRIDE_F32_T targetROPumpPressure; ///< Target RO max allowed pressure (in PSI). +static F32 roPumpDutyCyclePctSet; ///< Currently set RO pump PWM duty cycle. +static F32 roPumpOpenLoopTargetDutyCycle; ///< Target RO pump open loop PWM. // ********** private function prototypes ********** -static RO_PUMP_STATE_T handleROPumpOffState( RO_BOOST_PUMP_T pumpID ); -static RO_PUMP_STATE_T handleROPumpOpenLoopState( RO_BOOST_PUMP_T pumpID ); +static RO_PUMP_STATE_T handleROPumpOffState( void ); +static RO_PUMP_STATE_T handleROPumpControlToTargetFlowState( void ); +static RO_PUMP_STATE_T handleROPumpControlToTargetPressureState( void ); +static RO_PUMP_STATE_T handleROPumpOpenLoopState( void ); +static F32 roPumpPresToPWM( F32 targetPressure ); +static F32 roPumpFlowToPWM( F32 targetFlow ); +static void stopROPump( void ); static void publishROPumpData( void ); + /*********************************************************************//** * @brief * The initROPump function initializes the RO Pump module. - * @details \b Inputs: none + * @details \b Inputs: roPumpState, roPumpControlMode, isROPumpOn, stopPumpRequest, + * roControlTimerCounter, roPumpDutyCyclePctSet, roPumpOpenLoopTargetDutyCycle, roPumpDataPublicationTimerCounter, + * roPumpDataPublishInterval, targetROPumpFlowRate, targetROPumpPressure * @details \b Outputs: RO Pump controller unit initialized * @return none *************************************************************************/ void initROPump( void ) { - U32 pump; + initFluidPump(); - initBoostPump(); - // Initialize the variables - for ( pump = 0; pump < NUM_OF_BOOST_PUMPS; pump++ ) - { - roPumpState[ pump ] = RO_PUMP_OFF_STATE; - roPumpControlMode[ pump ] = NUM_OF_PUMP_CONTROL_MODES; - isROPumpOn[ pump ] = FALSE; - stopPumpRequest[ pump ] = FALSE; - } - roPumpControlInterval = ROP_CONTROL_INTERVAL; - roControlTimerCounter = 0; - roPumpDataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; - roPumpDataPublishInterval.data = RO_PUMP_DATA_PUB_INTERVAL; - roPumpDataPublishInterval.ovData = RO_PUMP_DATA_PUB_INTERVAL; - roPumpDataPublishInterval.ovInitData = 0; - roPumpDataPublishInterval.override = OVERRIDE_RESET; + // Initialize RO pump PI controller to flow + initializePIController( PI_CONTROLLER_ID_RO_PUMP_FLOW, MIN_FLUID_PUMP_DUTY_CYCLE_PCT, ROP_FLOW_CONTROL_P_COEFFICIENT, ROP_FLOW_CONTROL_I_COEFFICIENT, + MIN_FLUID_PUMP_DUTY_CYCLE_PCT, MAX_FLUID_PUMP_DUTY_CYCLE_PCT, FALSE, 0 ); + + // Initialize RO pump PI controller to target pressure + initializePIController( PI_CONTROLLER_ID_RO_PUMP_PRES, MIN_FLUID_PUMP_DUTY_CYCLE_PCT, ROP_PRESSURE_CONTROL_P_COEFFICIENT, ROP_PRESSURE_CONTROL_I_COEFFICIENT, + MIN_FLUID_PUMP_DUTY_CYCLE_PCT, MAX_FLUID_PUMP_DUTY_CYCLE_PCT, FALSE, 0 ); + + roPumpState = RO_PUMP_OFF_STATE; + roPumpControlMode = NUM_OF_PUMP_CONTROL_MODES; + isROPumpOn = FALSE; + stopPumpRequest = FALSE; + roControlTimerCounter = 0; + roPumpDutyCyclePctSet = 0; + roPumpOpenLoopTargetDutyCycle = 0; + roPumpDataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; + roPumpDataPublishInterval.data = RO_PUMP_DATA_PUB_INTERVAL; + roPumpDataPublishInterval.ovData = RO_PUMP_DATA_PUB_INTERVAL; + roPumpDataPublishInterval.ovInitData = 0; + roPumpDataPublishInterval.override = OVERRIDE_RESET; + targetROPumpFlowRate.data = 0; + targetROPumpFlowRate.ovData = 0; + targetROPumpFlowRate.ovInitData = 0; + targetROPumpFlowRate.override = OVERRIDE_RESET; + targetROPumpPressure.data = 0; + targetROPumpPressure.ovData = 0; + targetROPumpPressure.ovInitData = 0; + targetROPumpPressure.override = OVERRIDE_RESET; + + stopROPump(); } /*********************************************************************//** * @brief * The execROPumpController function executes the RO pump controller. - * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT if pump is in an invalid state - * @details \b Inputs: roPumpState[] - * @details \b Outputs: roPumpState[] + * @details \b Alarm: ALARM_ID_FP_SOFTWARE_FAULT if pump is in an invalid state + * @details \b Inputs: roPumpState + * @details \b Outputs: roPumpState * @return none *************************************************************************/ void execROPumpController( void ) { - RO_BOOST_PUMP_T pump; // Update RO pump feedback from FPGA - readBoostPumps(); + readFluidPumps(); - // Execute pump control state machine for each boost pump - for ( pump = (RO_BOOST_PUMP_T)0; pump < NUM_OF_BOOST_PUMPS; pump++ ) + switch ( roPumpState ) { - switch ( roPumpState[ pump ] ) - { - case RO_PUMP_OFF_STATE: - roPumpState[ pump ] = handleROPumpOffState( pump ); - break; + case RO_PUMP_OFF_STATE: + roPumpState = handleROPumpOffState(); + break; - case RO_PUMP_OPEN_LOOP_STATE: - roPumpState[ pump ] = handleROPumpOpenLoopState( pump ); - break; + case RO_PUMP_CONTROL_TO_TARGET_FLOW_STATE: + roPumpState = handleROPumpControlToTargetFlowState(); + break; - default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_RO_PUMP_INVALID_EXEC_STATE, roPumpState[ pump ] ) - roPumpState[ pump ] = RO_PUMP_OFF_STATE; - break; - } + case RO_PUMP_CONTROL_TO_TARGET_PRESSURE_STATE: + roPumpState = handleROPumpControlToTargetPressureState(); + break; + + case RO_PUMP_OPEN_LOOP_STATE: + roPumpState = handleROPumpOpenLoopState(); + break; + + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_FP_SOFTWARE_FAULT, SW_FAULT_ID_RO_INVALID_PUMP_DUTY_CYCLE_SELECTED, roPumpState ) + roPumpState = RO_PUMP_OFF_STATE; + break; } // Publish RO pump data on interval @@ -157,69 +188,380 @@ * @brief * The handleROPumpOffState function handles the off state of the RO pump * state machine. - * @details \b Inputs: none - * @details \b Outputs: none - * @param pumpID ID of boost pump to handle off state for + * @details \b Inputs: isROPumpOn, roPumpControlMode, roPumpOpenLoopTargetDutyCycle + * @details \b Outputs: isROPumpOn, roPumpDutyCyclePctSet * @return next state *************************************************************************/ -static RO_PUMP_STATE_T handleROPumpOffState( RO_BOOST_PUMP_T pumpID ) +static RO_PUMP_STATE_T handleROPumpOffState( void ) { - RO_PUMP_STATE_T state = RO_PUMP_OFF_STATE; + RO_PUMP_STATE_T state = RO_PUMP_OFF_STATE; + isROPumpOn = FALSE; + // If there is a target pressure set, transition to the PI controller and control to pressure. + if ( ( getTargetROPumpPressure() > 0.0F ) && ( PUMP_CONTROL_MODE_CLOSED_LOOP == roPumpControlMode ) ) + { + // Set pump to on + isROPumpOn = TRUE; + roPumpDutyCyclePctSet = roPumpPresToPWM( getTargetROPumpPressure() ); + setFluidPumpPctToPWMDutyCycle( P12_PUMP, roPumpDutyCyclePctSet ); + state = RO_PUMP_CONTROL_TO_TARGET_PRESSURE_STATE; + } + + // If there is a target flow set, transition to the PI controller and control to flow + else if ( ( getTargetROPumpFlowRateLPM() > 0.0F ) && ( PUMP_CONTROL_MODE_CLOSED_LOOP == roPumpControlMode ) ) + { + // Set pump to on + isROPumpOn = TRUE; + roPumpDutyCyclePctSet = roPumpFlowToPWM( getTargetROPumpFlowRateLPM() ); + setFluidPumpPctToPWMDutyCycle( P12_PUMP, roPumpDutyCyclePctSet ); + state = RO_PUMP_CONTROL_TO_TARGET_FLOW_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 + else if ( ( roPumpOpenLoopTargetDutyCycle > 0.0F ) && ( PUMP_CONTROL_MODE_OPEN_LOOP == roPumpControlMode ) ) + { + setFluidPumpPctToPWMDutyCycle( P12_PUMP, roPumpOpenLoopTargetDutyCycle ); + roPumpDutyCyclePctSet = roPumpOpenLoopTargetDutyCycle; + isROPumpOn = TRUE; + state = RO_PUMP_OPEN_LOOP_STATE; + } return state; } /*********************************************************************//** * @brief * The handleROPumpOpenLoopState function handles the open loop control * state of the RO pump state machine. - * @details \b Inputs: stopPumpRequest[] - * @details \b Outputs: roPumpState[], stopPumpRequest[] - * @param pumpID ID of boost pump to handle open loop control state for + * @details \b Inputs: stopPumpRequest, roPumpControlMode, + * roPumpDutyCyclePctSet + * @details \b Outputs: roPumpState, stopPumpRequest * @return next state *************************************************************************/ -static RO_PUMP_STATE_T handleROPumpOpenLoopState( RO_BOOST_PUMP_T pumpID ) +static RO_PUMP_STATE_T handleROPumpOpenLoopState( void ) { RO_PUMP_STATE_T state = RO_PUMP_OPEN_LOOP_STATE; - if ( TRUE == stopPumpRequest[ pumpID ] ) + if ( TRUE == stopPumpRequest ) { - stopPumpRequest[ pumpID ] = FALSE; - roPumpState[ pumpID ] = RO_PUMP_OFF_STATE; + stopPumpRequest = FALSE; + state = RO_PUMP_OFF_STATE; } + // If there is a target pressure set, transition to the PI controller and control to pressure. + if ( ( getTargetROPumpPressure() > 0.0F ) && ( PUMP_CONTROL_MODE_CLOSED_LOOP == roPumpControlMode ) ) + { + //transition to closed loop + resetPIController( PI_CONTROLLER_ID_RO_PUMP_PRES, roPumpDutyCyclePctSet, 0 ); + state = RO_PUMP_CONTROL_TO_TARGET_PRESSURE_STATE; + } + // If there is a target flow set, transition to the PI controller and control to flow + else if ( ( getTargetROPumpFlowRateLPM() > 0.0F ) && ( PUMP_CONTROL_MODE_CLOSED_LOOP == roPumpControlMode ) ) + { + ///transition to closed loop + resetPIController( PI_CONTROLLER_ID_RO_PUMP_FLOW, roPumpDutyCyclePctSet, 0 ); + state = RO_PUMP_CONTROL_TO_TARGET_FLOW_STATE; + } + return state; } /*********************************************************************//** * @brief - * The signalROPumpStop function stops the RO pump immediately and - * resets all the variables associated with the RO pump run. - * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT if invalid pump ID given - * @details \b Inputs: roPumpState[] - * @details \b Outputs: stopPumpRequest[] - * @param pumpID ID of boost pump to stop + * The handleROPumpControlToTargetFlowState function handles the control to + * target flow state of the RO pump controller state machine. + * @details Inputs: roPumpPWMDutyCyclePctSet, roControlTimerCounter + * @details Outputs: roPumpPWMDutyCyclePctSet, roControlTimerCounter + * @return next state of the controller state machine + *************************************************************************/ +static RO_PUMP_STATE_T handleROPumpControlToTargetFlowState( void ) +{ + RO_PUMP_STATE_T state = RO_PUMP_CONTROL_TO_TARGET_FLOW_STATE; + + // Control at set interval + if ( ( ++roControlTimerCounter >= ROP_CONTROL_INTERVAL ) && ( PUMP_CONTROL_MODE_CLOSED_LOOP == roPumpControlMode ) ) + { + roPumpDutyCyclePctSet = runPIController( PI_CONTROLLER_ID_RO_PUMP_FLOW, getTargetROPumpFlowRateLPM(), getFlowRate( P16_FLOW ) ); + setFluidPumpPctToPWMDutyCycle( P12_PUMP, roPumpDutyCyclePctSet ); + roControlTimerCounter = 0; + + } + + return state; +} + +/*********************************************************************//** + * @brief + * The handleROPumpControlToMaxPressureState function handles the control + * to target pressure state of the RO pump controller state machine. + * @details Inputs: roPumpPWMDutyCyclePctSet, roControlTimerCounter + * @details Outputs: roPumpPWMDutyCyclePctSet, roControlTimerCounter + * @return next state of the controller state machine + *************************************************************************/ +static RO_PUMP_STATE_T handleROPumpControlToTargetPressureState( void ) +{ + RO_PUMP_STATE_T state = RO_PUMP_CONTROL_TO_TARGET_PRESSURE_STATE; + + // Control at set interval + if ( ( ++roControlTimerCounter >= ROP_CONTROL_INTERVAL ) && ( PUMP_CONTROL_MODE_CLOSED_LOOP == roPumpControlMode ) ) + { + roPumpDutyCyclePctSet = runPIController( PI_CONTROLLER_ID_RO_PUMP_PRES, getTargetROPumpPressure(), getFilteredPressure( P17_PRES ) ); + setFluidPumpPctToPWMDutyCycle( P12_PUMP, roPumpDutyCyclePctSet ); + roControlTimerCounter = 0; + + } + + return state; +} + +/*********************************************************************//** + * @brief + * The setROPumpTargetFlowRate function sets a new target flow rate for the + * RO pump. + * @details Inputs: roPumpOpenLoopTargetDutyCycle + * @details Outputs: targetROPumpFlowRate, roPumpControlMode, roPumpDutyCyclePctSet, + * roControlTimerCounter, isROPumpOn + * @param roFlowRate which is target RO flow rate in mL/min + * @return TRUE if new target flow rate is set successfully, FALSE if not + *************************************************************************/ +BOOL setROPumpTargetFlowRateLPM( F32 roFlowRate ) +{ + BOOL result = FALSE; + + // First of all, the flow rate must be in range + if ( ( roFlowRate <= MAX_RO_FLOWRATE_LPM ) && ( roFlowRate >= MIN_RO_FLOWRATE_LPM ) ) + { + targetROPumpFlowRate.data = roFlowRate; + roPumpControlMode = PUMP_CONTROL_MODE_CLOSED_LOOP; + // Get the initial guess of the duty cycle + roPumpDutyCyclePctSet = roPumpFlowToPWM( getTargetROPumpFlowRateLPM() ); + roControlTimerCounter = 0; + isROPumpOn = TRUE; + + // Clear previous target data + if ( getTargetROPumpPressure() > 0.0F ) + { + targetROPumpPressure.data = 0; + } + if ( roPumpOpenLoopTargetDutyCycle > 0.0F ) + { + roPumpOpenLoopTargetDutyCycle = 0; + } + result = TRUE; + } + // Requested flow rate is out of range + else + { + SET_ALARM_WITH_2_F32_DATA( ALARM_ID_FP_SOFTWARE_FAULT, SW_FAULT_ID_RO_PUMP_INVALID_FLOW_RATE_SET, roFlowRate ) + } + + return result; +} + +/*********************************************************************//** + * @brief + * The setROPumpTargetPressure function sets a new target pressure for the + * RO pump. + * @details Inputs: roPumpOpenLoopTargetDutyCycle, targetROPumpFlowRate + * @details Outputs: roPumpOpenLoopTargetDutyCycle, targetROPumpFlowRate, isROPumpOn, + * roControlTimerCounter, roPumpDutyCyclePctSet, roPumpControlMode, targetROPumpPressure + * @param roPressure which is target RO pressure + * @return TRUE if new target flow rate is set successfully, FALSE if not + *************************************************************************/ +BOOL setROPumpTargetPressure( F32 roPressure ) +{ + BOOL result = FALSE; + + // First of all, the pressure must be in range + if ( ( roPressure <= MAX_RO_PRESSURE_PSI ) && ( roPressure >= MIN_RO_PRESSURE_PSI ) ) + { + targetROPumpPressure.data = roPressure; + roPumpControlMode = PUMP_CONTROL_MODE_CLOSED_LOOP; + // Get the initial guess of the duty cycle + roPumpDutyCyclePctSet = roPumpPresToPWM( getTargetROPumpPressure() ); + roControlTimerCounter = 0; + isROPumpOn = TRUE; + // Clear previous target data + if ( getTargetROPumpFlowRateLPM() > 0.0F ) + { + targetROPumpFlowRate.data = 0; + } + if ( roPumpOpenLoopTargetDutyCycle > 0.0F ) + { + roPumpOpenLoopTargetDutyCycle = 0; + } + result = TRUE; + } + // Requested flow rate is out of range + else + { + SET_ALARM_WITH_2_F32_DATA( ALARM_ID_FP_SOFTWARE_FAULT, SW_FAULT_ID_RO_PUMP_INVALID_PRESSURE_SELECTED, roPressure ) + } + + return result; +} + +/*********************************************************************//** + * @brief + * The setROPumpTargetDutyCycle function sets the duty cycle that the + * pump should run. + * @details Inputs: roPumpOpenLoopTargetDutyCycle, roPumpPWMDutyCyclePct, + * targetROPumpPressure, targetROPumpFlowRate + * @details Outputs: roPumpOpenLoopTargetDutyCycle + * roPumpControlMode, targetROPumpFlowRate, targetROPumpPressure + * @param duty which is the duty cycle * @return none *************************************************************************/ -void signalROPumpStop( RO_BOOST_PUMP_T pumpID ) +BOOL setROPumpTargetDutyCycle( F32 dutyCycle ) { - if ( pumpID < NUM_OF_BOOST_PUMPS ) + BOOL status = FALSE; + + if ( ( dutyCycle >= MIN_FLUID_PUMP_DUTY_CYCLE_PCT ) && ( dutyCycle <= MAX_FLUID_PUMP_DUTY_CYCLE_PCT ) ) { - setBoostPumpPWMDutyCycle( pumpID, 0 ); // stop pump immediately - if ( roPumpState[ pumpID ] != RO_PUMP_OFF_STATE ) + roPumpOpenLoopTargetDutyCycle = dutyCycle; + roPumpControlMode = PUMP_CONTROL_MODE_OPEN_LOOP; + status = TRUE; + + // Set the new duty cycle of the pump + setFluidPumpPctToPWMDutyCycle( P12_PUMP, roPumpOpenLoopTargetDutyCycle ); + // Clear previous target data + if ( getTargetROPumpFlowRateLPM() > 0.0F ) { - stopPumpRequest[ pumpID ] = TRUE; // set stop request so state machine goes to off state + targetROPumpFlowRate.data = 0; } + if ( getTargetROPumpPressure() > 0.0F ) + { + targetROPumpPressure.data = 0; + } } else { - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_RO_PUMP_INVALID_PUMP_ID1, (U32)pumpID ) + SET_ALARM_WITH_2_F32_DATA( ALARM_ID_FP_SOFTWARE_FAULT, SW_FAULT_ID_RO_INVALID_PUMP_DUTY_CYCLE_SELECTED, dutyCycle ) } + + return status; } /*********************************************************************//** * @brief - * The publishROPumpData function publishes RO pump data at the set interval. + * The getTargetROPumpPressure function gets the current target RO pump + * pressure. + * @details Inputs: targetROPumpPressure + * @details Outputs: none + * @return the current target RO pressure in PSI + *************************************************************************/ +F32 getTargetROPumpPressure( void ) +{ + F32 pressure = getF32OverrideValue( &targetROPumpPressure ); + + return pressure; +} + +/*********************************************************************//** + * @brief + * The getTargetROPumpFlowRateMLPM function gets the current target RO pump + * flow rate. + * @details Inputs: targetROPumpFlowRate + * @details Outputs: none + * @return the current target RO flow rate (in L/min). + *************************************************************************/ +F32 getTargetROPumpFlowRateLPM( void ) +{ + F32 flowRate = getF32OverrideValue( &targetROPumpFlowRate ); + + return flowRate; +} + +/*********************************************************************//** + * @brief + * The roPumpPresToPWM function calculates the duty cycle for the given target + * pressure. + * @details Inputs: none + * @details Outputs: dutyCyclePct + * @return the current target RO pump PWM in a percentage. + *************************************************************************/ +static F32 roPumpPresToPWM( F32 targetPressure ) +{ + F32 dutyCyclePct = ROP_PRESSURE_TO_PWM( targetPressure ); + + return dutyCyclePct; +} + +/*********************************************************************//** + * @brief + * The roPumpFlowToPWM function calculates the duty cycle for the given target + * flow rate. + * @details Inputs: none + * @details Outputs: dutyCyclePct + * @return the current target RO pump PWM in a percentage. + *************************************************************************/ +static F32 roPumpFlowToPWM( F32 targetFlow ) +{ + F32 dutyCyclePct = ROP_FLOW_TO_PWM( targetFlow ); + + return dutyCyclePct; +} + +/*********************************************************************//** + * @brief + * The signalROPumpStop function stops the P12 pump immediately and + * resets all the variables associated with the P12 pump run. + * @details \b Inputs: roPumpState[] + * @details \b Outputs: stopPumpRequest[] + * @param pumpID ID of boost pump to stop + * @return none + *************************************************************************/ +void signalROPumpHardStop( void ) +{ + + if( targetROPumpFlowRate.data > 0.0F ) + { + targetROPumpFlowRate.data = 0.0; + resetPIController( PI_CONTROLLER_ID_RO_PUMP_FLOW, MIN_FLUID_PUMP_DUTY_CYCLE_PCT, 0 ); + } + + if( targetROPumpPressure.data > 0.0F ) + { + targetROPumpPressure.data = 0.0; + resetPIController( PI_CONTROLLER_ID_RO_PUMP_PRES, MIN_FLUID_PUMP_DUTY_CYCLE_PCT, 0 ); + } + + roPumpState = RO_PUMP_OFF_STATE; + roPumpDutyCyclePctSet = 0; + roControlTimerCounter = 0; + roPumpOpenLoopTargetDutyCycle = 0; + stopROPump(); +} + +/*********************************************************************//** + * @brief + * The stopROPump function sets the P12 pump duty cycle to zero. + * @details Inputs: isROPumpOn, roPumpPWMDutyCyclePctSet + * @details Outputs: isROPumpOn, roPumpPWMDutyCyclePctSet + * @return none + *************************************************************************/ +static void stopROPump( void ) +{ + isROPumpOn = FALSE; + roPumpDutyCyclePctSet = 0.0; + // Set the new duty cycle of the pump + setFluidPumpPWMDutyCycle( P12_PUMP, 0 ); +} + +/*********************************************************************//** + * @brief + * The isROPumpRunning function returns the on/off status of P12 pump. + * @details Inputs: isROPumpOn + * @details Outputs: none + * @return isROPumpOn the boolean flag that is TRUE if the pump is on and + * FALSE if it is off + *************************************************************************/ +BOOL isROPumpRunning( void ) +{ + return isROPumpOn; +} +/*********************************************************************//** + * @brief + * The publishROPumpData function publishes p12 pump data at the set interval. * @details \b Message \b Sent: * @details \b Inputs: roPumpDataPublicationTimerCounter * @details \b Outputs: roPumpDataPublicationTimerCounter @@ -232,16 +574,15 @@ { RO_PUMP_DATA_T pumpData; - pumpData.p12PumpState = (U32)roPumpState[ P12_PUMP ]; - pumpData.p12PumpDutyCycle = (U32)getBoostPumpPWMDutyCycle( P12_PUMP ); - pumpData.p12PumpFBDutyCycle = (U32)getBoostPumpReadPWMDutyCycle( P12_PUMP ); - pumpData.p12PumpSpeed = getBoostPumpRPM( P12_PUMP ); - pumpData.p40PumpState = (U32)roPumpState[ P40_PUMP ]; - pumpData.p40PumpDutyCycle = (U32)getBoostPumpPWMDutyCycle( P40_PUMP ); - pumpData.p40PumpFBDutyCycle = (U32)getBoostPumpReadPWMDutyCycle( P40_PUMP ); - pumpData.p40PumpSpeed = getBoostPumpRPM( P40_PUMP ); + pumpData.p12PumpState = (U32)roPumpState; + pumpData.p12PumpDutyCycle = (U32)getFluidPumpPWMDutyCycle( P12_PUMP ); + pumpData.p12PumpFBDutyCycle = (U32)getFluidPumpReadPWMDutyCycle( P12_PUMP ); + pumpData.p12PumpSpeed = getFluidPumpRPM( P12_PUMP ); + pumpData.p12TargetPressure = getTargetROPumpPressure(); + pumpData.p12TargetFlow = getTargetROPumpFlowRateLPM(); + pumpData.p12TargetDutyCycle = roPumpOpenLoopTargetDutyCycle; - broadcastData( MSG_ID_RO_PUMP_DATA, COMM_BUFFER_OUT_CAN_RO_BROADCAST, (U08*)&pumpData, sizeof( RO_PUMP_DATA_T ) ); + broadcastData( MSG_ID_FP_RO_PUMP_DATA, COMM_BUFFER_OUT_CAN_RO_BROADCAST, (U08*)&pumpData, sizeof( RO_PUMP_DATA_T ) ); roPumpDataPublicationTimerCounter = 0; } } @@ -269,4 +610,39 @@ return result; } +/*********************************************************************//** + * @brief + * The testROPumpDataPublishIntervalOverride function overrides the RO pump + * data publish interval. + * @details Inputs: roPumpDataPublishInterval + * @details Outputs: roPumpDataPublishInterval + * @param message Override message from Dialin which includes the value + * that override ro pump data publish interval with (in ms) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testROPumpTargetPressureOverride( MESSAGE_T *message ) +{ + BOOL result = f32Override( message, &targetROPumpPressure ); + + return result; +} + +/*********************************************************************//** + * @brief + * The testROPumpDataPublishIntervalOverride function overrides the RO pump + * data publish interval. + * @details Inputs: roPumpDataPublishInterval + * @details Outputs: roPumpDataPublishInterval + * @param message Override message from Dialin which includes the value + * that override ro pump data publish interval with (in ms) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testROPumpTargetFlowOverride( MESSAGE_T *message ) +{ + BOOL result = f32Override( message, &targetROPumpFlowRate ); + + return result; +} + + /**@}*/