Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -r359cf587f2f935ef9f40a16d17c4105efa653c1d -r2510a84808ee5179d3f84f0b04f24c5a4122dbe5 --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 359cf587f2f935ef9f40a16d17c4105efa653c1d) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 2510a84808ee5179d3f84f0b04f24c5a4122dbe5) @@ -52,29 +52,33 @@ #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 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.009 ///< 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_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_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 ROP_PSI_TO_PWM_DC(p) ( 0.2 + ( (F32)((p) - 100) * 0.01 ) ) //TODO remove ///< conversion factor from target PSI to PWM duty cycle estimate TODO - this is a place holder for real conversion +//#define ROP_PSI_TO_PWM_DC(p) ( 0.2 + ( (F32)((p) - 100) * 0.01 ) ) //TODO remove ///< conversion factor from target PSI to PWM duty cycle estimate TODO - this is a place holder for real conversion #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 / 1000)) //TODO dara ///< Initial conversion factor from target flow rate to PWM duty cycle estimate -#define ROP_RAMP_UP_P_COEFFICIENT 0.0 //TODO dara -#define ROP_RAMP_UP_I_COEFFICIENT 0.09 //TODO dara -#define ROP_FLOW_TARGET_TOLERANCE 0.1 //TODO dara ///< Tolerance in between the target flow rate and the actual flow rate in mL - /// 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 - RO_PUMP_RAMP_UP_STATE, ///< RO pump ramp up to target flow rate 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 few seconds state RO_PUMP_CONTROL_TO_TARGET_STATE, ///< RO pump control to target pressure state NUM_OF_RO_PUMP_STATES ///< Number of RO pump states } RO_PUMP_STATE_T; @@ -93,11 +97,6 @@ #define RO_PUMP_TEST_SPI5_PORT_MASK 0x00000100 // (ENA - re-purposed as input GPIO) // dialIn pump stop and direction macros #define GET_DIP_SW0_TEST() ( ( mibspiREG5->PC2 & RO_PUMP_TEST_SPI5_PORT_MASK ) != 0 ) - -// TODO: Remove the below code. FOR TESTING ONLY -#define PRIMARY_HEATER_MIBSPI1_PORT_MASK 0x00000002 // (CS1 - re-purposed as input GPIO) -#define TOGGLEPUMP() (( mibspiREG1->PC2 & PRIMARY_HEATER_MIBSPI1_PORT_MASK ) != 0) -//TODO: Remove the above code. FOR TESTING ONLY // ********** private data ********** @@ -107,29 +106,33 @@ 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. -static PUMP_CONTROL_MODE_T roPumpControlModeSet = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< currently set RO pump control mode. +static PUMP_CONTROL_MODE_T roPumpControlModeSet = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< currently set RO pump control mode. //TODO remove, the control mode set is picked +// implicitly from the function that is called -//static OVERRIDE_U32_T roPumpDataPublishInterval = { 0, 0, 0, 0 }; //From branch Remove ///< interval (in ms) at which to publish RO flow data to CAN bus. -//static OVERRIDE_U32_T targetROPumpPressure = { 0, 0, 0, 0 }; //From branch ///< Target RO pressure (in PSI). //TODO remove static OVERRIDE_F32_T targetROPumpFlowRate = { 0, 0, 0, 0 }; //From Branch ///< Target RO flow rate (in LPM_) // TODO dara -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. -static OVERRIDE_U32_T targetROPumpPressure = { 0, 0, 0, 0 }; ///< Target RO pressure (in PSI). +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. +static OVERRIDE_U32_T targetROPumpPressure = { 0, 0, 0, 0 }; ///< Target RO pressure (in PSI). //TODO remove 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). -static F32 tgtROPumpPressure = 0.0; // TODO dara +static F32 measuredROPumpPressure = 0.0; ///< measured RO pressure (in PSI). // TODO remove +static F32 tgtROPumpPressure = 0.0; static S32 measuredFlowReadingsSum = 0; ///< Raw flow reading sums for averaging. -static U32 flowFilterCounter = 0; ///< used to schedule flow filtering. +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 - + +/* 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 static U32 roPumpSelfTestTimerCount = 0; ///< timer counter for ro pump self test - +*/ // ********** private function prototypes ********** static RO_PUMP_STATE_T handleROPumpOffState( void ); -static RO_PUMP_STATE_T handleROPumpRampUpState( void ); +static RO_PUMP_STATE_T handleROPumpRampUpState( void ); +static RO_PUMP_STATE_T handleROPumpVerifyFlowState( void ); static RO_PUMP_STATE_T handleROPumpControlToTargetState( void ); static void setROPumpControlSignalPWM( F32 newPWM ); static void stopROPump( void ); @@ -148,7 +151,7 @@ { stopROPump(); - // initialize RO pump PI controller + // Initialize RO pump PI controller initializePIController( PI_CONTROLLER_ID_RO_PUMP, MIN_RO_PUMP_PWM_DUTY_CYCLE, ROP_P_COEFFICIENT, ROP_I_COEFFICIENT, MIN_RO_PUMP_PWM_DUTY_CYCLE, MAX_RO_PUMP_PWM_DUTY_CYCLE ); @@ -183,7 +186,7 @@ #ifdef EMC_TEST_BUILD roPumpPWMDutyCyclePct = 1.0; #else - roPumpPWMDutyCyclePct = ROP_PSI_TO_PWM_DC( roPressure ); + //roPumpPWMDutyCyclePct = ROP_PSI_TO_PWM_DC( roPressure ); #endif result = TRUE; @@ -228,7 +231,8 @@ stopROPump(); roPumpState = RO_PUMP_OFF_STATE; roPumpPWMDutyCyclePct = 0.0; - roControlTimerCounter = 0; + roControlTimerCounter = 0; + flowVerificationCounter = 0; isROPumpOn = FALSE; resetPIController( PI_CONTROLLER_ID_RO_PUMP, MIN_RO_PUMP_PWM_DUTY_CYCLE ); } @@ -296,6 +300,10 @@ 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: @@ -306,7 +314,9 @@ SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, 0, roPumpState ) // TODO - replace 1st param with s/w fault enum roPumpState = RO_PUMP_OFF_STATE; break; - } + } + + //publishROPumpData(); } /*********************************************************************//** @@ -364,12 +374,12 @@ // the corresponding pressure of that flow if ( getTargetROPumpFlowRate() > 0 ) { - roPumpControlModeSet = roPumpControlMode; + //roPumpControlModeSet = roPumpControlMode; TODO remove this since the mode type is implicitly is picked from the function // set initial PWM duty cycle - roPumpPWMDutyCyclePctSet = ROP_FLOW_TO_PWM_DC( getTargetROPumpFlowRate() ); - setROPumpControlSignalPWM( roPumpPWMDutyCyclePctSet ); + roPumpPWMDutyCyclePctSet = ROP_FLOW_TO_PWM_DC( getTargetROPumpFlowRate() ); // Assume a liter/permin for top end + setROPumpControlSignalPWM ( roPumpPWMDutyCyclePctSet ); // reset controller - resetPIController( I_CONTROLLER_ID_RO_PUMP_RAMP_UP, roPumpPWMDutyCyclePctSet ); + resetPIController ( I_CONTROLLER_ID_RO_PUMP_RAMP_UP, roPumpPWMDutyCyclePctSet ); // set pump to on isROPumpOn = TRUE; result = RO_PUMP_RAMP_UP_STATE; @@ -389,7 +399,6 @@ isROPumpOn = TRUE; result = RO_PUMP_CONTROL_TO_TARGET_STATE; }*/ - return result; } @@ -402,40 +411,66 @@ if ( ++roControlTimerCounter >= ROP_CONTROL_INTERVAL ) { F32 targetFlowRate = targetROPumpFlowRate.data; - if ( OVERRIDE_KEY == targetROPumpFlowRate.override ) { targetFlowRate = targetROPumpFlowRate.ovData; } - F32 actualFlowRate = (F32)getMeasuredROFlowRate(); if ( fabs( actualFlowRate - targetFlowRate ) > ROP_FLOW_TARGET_TOLERANCE ) { F32 newPWM = runPIController( I_CONTROLLER_ID_RO_PUMP_RAMP_UP, targetFlowRate, actualFlowRate ); roPumpPWMDutyCyclePctSet = newPWM; setROPumpControlSignalPWM( newPWM ); + } + else if ( fabs( actualFlowRate - targetFlowRate ) <= ROP_FLOW_TARGET_TOLERANCE ) + { + result = RO_PUMP_VERIFY_FLOW_STATE; } - else - { - // 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 ); - - roPumpControlModeSet = roPumpControlMode; - // set initial PWM duty cycle - roPumpPWMDutyCyclePctSet = roPumpPWMDutyCyclePct; - setROPumpControlSignalPWM( roPumpPWMDutyCyclePctSet ); - // reset controller - resetPIController( PI_CONTROLLER_ID_RO_PUMP, roPumpPWMDutyCyclePctSet ); - - result = RO_PUMP_CONTROL_TO_TARGET_STATE; - } + roControlTimerCounter = 0; } return result; } + +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; + } + + if ( fabs( actualFlowRate - targetFlowRate ) <= ROP_FLOW_TARGET_TOLERANCE ) + { + 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 ); + + roPumpControlModeSet = roPumpControlMode; + // 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; + } + } + else + { + flowVerificationCounter = 0; + result = RO_PUMP_RAMP_UP_STATE; + } + + return result; +} /*********************************************************************//** * @brief @@ -454,11 +489,7 @@ if ( ++roControlTimerCounter >= ROP_CONTROL_INTERVAL ) { F32 actualPressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_OUTLET ); - - //if ( OVERRIDE_KEY == targetROPumpFlowRate.override ) - //{ - // targetFlowRate = targetROPumpFlowRate.ovData; - //} + F32 newPWM = runPIController( PI_CONTROLLER_ID_RO_PUMP, tgtROPumpPressure, actualPressure ); roPumpPWMDutyCyclePctSet = newPWM; setROPumpControlSignalPWM( newPWM ); @@ -599,11 +630,10 @@ // publish RO pump data on interval if ( ++roPumpDataPublicationTimerCounter >= getPublishROPumpDataInterval() ) { - //U32 presStPt = getTargetROPumpPressure(); - F32 targetPressure = tgtROPumpPressure; + F32 targetPressure = measuredROPumpPressure; F32 measFlow = getMeasuredROFlowRate(); F32 pumpPWMPctDutyCycle = roPumpPWMDutyCyclePctSet * FRACTION_TO_PERCENT_FACTOR; - broadcastROPumpData( targetPressure, measFlow, pumpPWMPctDutyCycle ); + broadcastROPumpData( targetPressure, measFlow, pumpPWMPctDutyCycle, (U32)roPumpState ); roPumpDataPublicationTimerCounter = 0; } }