Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -r1a7b3fdc8c9b47ae713a7ec37670a96df7d73818 -r5a36a768d11cc597a36b894c1fb3a5e5590130f1 --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 1a7b3fdc8c9b47ae713a7ec37670a96df7d73818) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 5a36a768d11cc597a36b894c1fb3a5e5590130f1) @@ -112,8 +112,11 @@ static F32 roPumpPWMDutyCyclePct = 0.0; ///< Initial RO pump PWM duty cycle. static F32 roPumpDutyCyclePctSet = 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. TODO do we need this? +static F32 pendingROPumpCmdMaxPressure = 0.0; ///< Delayed (pending) RO pump max pressure (in PSI) setting. +static F32 pendingROPumpCmdTargetFlow = 0.0; ///< Delayed (pending) RO pump target flow rate (in mL/min) setting. +static U32 pendingROPumpCmdCountDown = 0; ///< Delayed (pending) RO pump command count down timer (in task intervals). + static F32 targetROPumpFlowRate = 0.0; ///< Target RO flow rate (in L/min). static F32 targetROPumpMaxPressure = 0.0; ///< Target RO max allowed pressure (in PSI). @@ -126,11 +129,6 @@ static F32 roPumpOpenLoopTargetDutyCycle = 0; ///< Target RO pump open loop PWM. -/* 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. -static U32 roPumpSelfTestTimerCount = 0; ///< Timer counter for RO pump self test. -*/ - static S32 measuredFlowReadingsSum = 0; ///< Raw flow reading sums for averaging. static U32 flowFilterCounter = 0; ///< Flow filtering counter. @@ -154,7 +152,7 @@ * @details Inputs: roControlTimerCounter,roPumpOpenLoopTargetDutyCycle, * roPumpFlowRateRunningSum, roPumpPressureRunningSum, measuredFlowReadingsSum, * flowFilterCounter, flowVerificationCounter, roPumpState, roPumpControlMode - * roPumpDataPublicationTimerCounter, roPumpControlModeSet + * roPumpDataPublicationTimerCounter * @details Outputs: none * @return none *************************************************************************/ @@ -172,16 +170,12 @@ // 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 ); // 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 ); - // 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 ); - // Initialize the persistent alarm for not turning off the pump initPersistentAlarm( ALARM_ID_RO_PUMP_OFF_FAULT, SAFETY_SHUTDOWN_TIMEOUT, SAFETY_SHUTDOWN_TIMEOUT ); @@ -193,7 +187,6 @@ roPumpDataPublicationTimerCounter = 0; roPumpState = RO_PUMP_OFF_STATE; roPumpControlMode = NUM_OF_PUMP_CONTROL_MODES; - roPumpControlModeSet = roPumpControlMode; isROPumpOn = FALSE; } @@ -249,6 +242,49 @@ /*********************************************************************//** * @brief + * The setROPumpTargetFlowRateDelayed function sets a new target flow rate for the + * RO pump to be set after given delay. + * @details Inputs: none + * @details Outputs: pendingROPumpCmdMaxPressure, pendingROPumpCmdTargetFlow, + * pendingROPumpCmdCountDown + * @param roFlowRate which is target RO flow rate + * @param maxPressure which is the maximum allowed pressure that the RO pump + * can reach + * @param delayMs delay duration (in ms) before RO pump started + * @return TRUE if new target flow rate is set successfully, FALSE if not + *************************************************************************/ +BOOL setROPumpTargetFlowRateDelayed( F32 roFlowRate, U32 maxPressure, U32 delayMs ) +{ + BOOL result = FALSE; + + // First of all, the flow rate must be in range + if ( ( roFlowRate <= MAX_RO_FLOWRATE_LPM ) && ( roFlowRate >= MIN_RO_FLOWRATE_LPM ) ) + { + // Then the max pressure that we are allowed to reach must be in range + if ( ( maxPressure >= MIN_ALLOWED_PRESSURE_PSI ) && ( maxPressure <= MAX_ALLOWED_PRESSURE_PSI ) ) + { + pendingROPumpCmdMaxPressure = (F32)maxPressure; + pendingROPumpCmdTargetFlow = roFlowRate; + pendingROPumpCmdCountDown = delayMs / TASK_GENERAL_INTERVAL; + result = TRUE; + } + // Requested max pressure is out of range + else + { + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_RO_PUMP_INVALID_FLOW_RATE_SET, maxPressure ) + } + } + // Requested flow rate is out of range + else + { + SET_ALARM_WITH_2_F32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_RO_PUMP_INVALID_PRESSURE_SELECTED, roFlowRate ) + } + + return result; +} + +/*********************************************************************//** + * @brief * The signalROPumpHardStop function stops the RO pump immediately and * resets all the variables associated with the RO pump run. * @details Inputs: targetROPumpFlowRate, roPumpState, roPumpPWMDutyCyclePct, @@ -343,7 +379,7 @@ checkPersistentAlarm( ALARM_ID_RO_PUMP_OFF_FAULT, isPumpRunning, pressureInlet, ( pressureInlet + MAX_PRESSURE_TARGET_TOLERANCE ) ); // Check if it has timed out - if ( isAlarmActive( ALARM_ID_RO_PUMP_OFF_FAULT ) ) + if ( TRUE == isAlarmActive( ALARM_ID_RO_PUMP_OFF_FAULT ) ) { activateSafetyShutdown(); } @@ -362,6 +398,26 @@ *************************************************************************/ void execROPumpController( void ) { + // Handle pending delayed RO pump command + if ( pendingROPumpCmdCountDown > 0 ) + { + pendingROPumpCmdCountDown--; + if ( 0 == pendingROPumpCmdCountDown ) + { + targetROPumpMaxPressure = pendingROPumpCmdMaxPressure; + targetROPumpFlowRate = pendingROPumpCmdTargetFlow; + pendingROPumpCmdMaxPressure = 0.0; + pendingROPumpCmdTargetFlow = 0.0; + roPumpControlMode = PUMP_CONTROL_MODE_CLOSED_LOOP; + roPumpState = RO_PUMP_RAMP_UP_TO_TARGET_FLOW_STATE; + // Get the initial guess of the duty cycle + roPumpPWMDutyCyclePct = ROP_FLOW_TO_PWM_DC( targetROPumpFlowRate ); + roControlTimerCounter = 0; + isROPumpOn = TRUE; + } + } + + // Execute RO pump control state machine switch ( roPumpState ) { case RO_PUMP_OFF_STATE: @@ -393,36 +449,19 @@ /*********************************************************************//** * @brief - * The isReverseOsmosisPumpOn function returns the on/off status of RO pump. + * The isROPumpRunning function returns the on/off status of RO 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 isReverseOsmosisPumpOn( void ) +BOOL isROPumpRunning( void ) { return isROPumpOn; } /*********************************************************************//** * @brief - * The execROPumpTest function executes the state machine for the RO pump - * self-test. - * @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 ) -{ - SELF_TEST_STATUS_T result = SELF_TEST_STATUS_FAILED; - - // TODO - implement self-test(s) - - return result; -} - -/*********************************************************************//** - * @brief * The handleROPumpOffState function handles the RO pump off state of the * controller state machine. * @details Inputs: roPumpControlMode, roPumpPWMDutyCyclePctSet, @@ -437,7 +476,6 @@ // 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; roPumpDutyCyclePctSet = ROP_FLOW_TO_PWM_DC( getTargetROPumpFlowRate() ); @@ -519,7 +557,6 @@ * The handleROPumpControlToTargetFlowState function handles the control to * target flow state of the RO pump controller state machine. * @details Inputs: roPumpPWMDutyCyclePctSet, roControlTimerCounter, - * roPumpControlModeSet * @details Outputs: roPumpPWMDutyCyclePctSet, roControlTimerCounter * @return next state of the controller state machine *************************************************************************/ @@ -557,7 +594,6 @@ * The handleROPumpControlToMaxPressureState function handles the control * to max pressure state of the RO pump controller state machine. * @details Inputs: roPumpPWMDutyCyclePctSet, roControlTimerCounter, - * roPumpControlModeSet * @details Outputs: roPumpPWMDutyCyclePctSet, roControlTimerCounter * @return next state of the controller state machine *************************************************************************/