Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -r82d72d52bc2b2836ba8195f3d43b111e877bcd61 -rab304e2ca6e3e40ed8cb12650e9855ae0b9649d8 --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 82d72d52bc2b2836ba8195f3d43b111e877bcd61) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision ab304e2ca6e3e40ed8cb12650e9855ae0b9649d8) @@ -7,17 +7,15 @@ * * @file ROPump.c * -* @author (last) Sean Nash -* @date (last) 08-Jul-2020 +* @author (last) Quang Nguyen +* @date (last) 14-Sep-2020 * * @author (original) Sean * @date (original) 04-Apr-2020 * ***************************************************************************/ -#ifndef _VECTORCAST_ - #include -#endif +#include #include "etpwm.h" #include "mibspi.h" @@ -26,11 +24,11 @@ #include "OperationModes.h" #include "PIControllers.h" #include "Pressures.h" +#include "ROPump.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" #include "TaskPriority.h" #include "Timers.h" -#include "ROPump.h" #ifdef EMC_TEST_BUILD #include "Heaters.h" #endif @@ -45,7 +43,7 @@ // ********** private definitions ********** -#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 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 @@ -69,7 +67,24 @@ // 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 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). + +#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. + +// TODO - this is a place holder for real conversion +#define ROP_PSI_TO_PWM_DC(p) ( 0.2 + ( (F32)((p) - 100) * 0.01 ) ) ///< conversion factor from target PSI to PWM duty cycle estimate. +#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). + /// Enumeration of RO pump states. typedef enum ROPump_States { @@ -81,13 +96,13 @@ NUM_OF_RO_PUMP_STATES ///< Number of RO pump states } RO_PUMP_STATE_T; -/// Enumeration of RO pump self test states. +/// 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_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; // TODO - test code - remove later @@ -114,8 +129,6 @@ static F32 measuredROPumpPressure = 0.0; ///< measured RO pressure (in PSI) // TODO remove static F32 tgtROPumpPressure = 0.0; ///< Target RO control pressure (in PSI) -static S32 measuredFlowReadingsSum = 0; ///< Raw flow reading sums for averaging -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 @@ -128,6 +141,16 @@ 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 OVERRIDE_U32_T targetROPumpPressure = { 0, 0, 0, 0 }; ///< Target RO pressure (in PSI). + +static S32 measuredFlowReadingsSum = 0; ///< Raw flow reading sums for averaging. +static U32 flowFilterCounter = 0; ///< Flow filtering counter. + + +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 ); @@ -138,7 +161,7 @@ static void setROPumpControlSignalPWM( F32 newPWM ); static void stopROPump( void ); static void publishROPumpData( void ); -static DATA_GET_PROTOTYPE( U32, getPublishROPumpDataInterval ); +static U32 getPublishROPumpDataInterval( void ); /*********************************************************************//** * @brief @@ -163,8 +186,46 @@ 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 @@ -322,7 +383,7 @@ /*********************************************************************//** * @brief - * The isROPumpOn function returns the status of RO pump + * The isROPumpOn function returns the status of RO pump. * @details * Inputs: none * Outputs: none @@ -487,8 +548,8 @@ /*********************************************************************//** * @brief - * The handleROPumpControlToTargetState function handles the "control to \n - * target" state of the ro pump controller state machine. + * The handleROPumpControlToTargetState function handles the "control to + * target" state of the RO pump controller state machine. * @details * Inputs: roPumpPWMDutyCyclePctSet, roControlTimerCounter * Outputs: roPumpPWMDutyCyclePctSet, roControlTimerCounter @@ -551,8 +612,8 @@ /*********************************************************************//** * @brief - * The setROPumpControlSignalPWM function sets the PWM duty cycle for \n - * the RO pump to a given %. + * The setROPumpControlSignalPWM function sets the PWM duty cycle for the + * RO pump to a given %. * @details * Inputs: none * Outputs: PWM duty cycle zeroed @@ -581,14 +642,13 @@ /*********************************************************************//** * @brief - * The getPublishROPumpDataInterval function gets the RO pump data \n - * publication interval. + * The getPublishROPumpDataInterval function gets the RO pump data publish interval. * @details * Inputs: roPumpDataPublishInterval * Outputs: none * @return the current RO pump data publication interval (in ms). *************************************************************************/ -U32 getPublishROPumpDataInterval( void ) +static U32 getPublishROPumpDataInterval( void ) { U32 result = roPumpDataPublishInterval.data; @@ -623,8 +683,7 @@ /*********************************************************************//** * @brief - * The getMeasuredROFlowRate function gets the measured RO pump \n - * flow rate. + * The getMeasuredROFlowRate function gets the measured RO pump flow rate. * @details * Inputs: measuredROFlowRateLPM * Outputs: none @@ -644,8 +703,7 @@ /*********************************************************************//** * @brief - * The publishROPumpData function publishes RO pump data at the set \n - * interval. + * The publishROPumpData function publishes RO pump data at the set interval. * @details * Inputs: target pressure, measured pressure, measured RO pump speed. * Outputs: RO pump data is published to CAN bus. @@ -665,7 +723,7 @@ /*********************************************************************//** * @brief - * The execROPumpTest function executes the state machine for the ROPump self test. + * The execROPumpTest function executes the state machine for the ROPump self-test. * @details * Inputs: none * Outputs: none @@ -675,22 +733,20 @@ { SELF_TEST_STATUS_T result = SELF_TEST_STATUS_FAILED; - // TODO - implement self test(s) + // TODO - implement self-test(s) return result; } -/**@}*/ - /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ + - /*********************************************************************//** * @brief - * The testSetROPumpDataPublishIntervalOverride function overrides the \n + * The testSetROPumpDataPublishIntervalOverride function overrides the * RO pump data publish interval. * @details * Inputs: none @@ -716,7 +772,7 @@ /*********************************************************************//** * @brief - * The testResetROPumpDataPublishIntervalOverride function resets the override \n + * The testResetROPumpDataPublishIntervalOverride function resets the override * of the RO pump data publish interval. * @details * Inputs: none @@ -762,15 +818,14 @@ return result; } -/*********************************************************************//** - * @brief - * The testResetTargetROPumpFlowRateOverride function resets the override of the \n - * target RO flow rate. - * @details - * Inputs: targetROPumpFlowRate - * Outputs: targetROPumpFlowRate - * @param: none - * @return TRUE if override reset successful, FALSE if not +/*********************************************************************//** + * @brief + * The testResetTargetROPumpPressureOverride function resets the override of the + * target RO pressure. + * @details + * Inputs : none + * Outputs : targetROPumpPressure + * @return TRUE if override reset successful, FALSE if not *************************************************************************/ BOOL testResetTargetROPumpFlowRateOverride( void ) { @@ -786,11 +841,61 @@ } return result; -} +} + +/*********************************************************************//** + * @brief + * The testSetTargetROPumpPressureOverride function overrides the target + * RO pressure. + * @details + * Inputs : none + * Outputs : targetROPumpPressure + * @param value override target RO pressure (in PSI) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetTargetROPumpPressureOverride( U32 value ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + targetROPumpPressure.ovInitData = targetROPumpPressure.data; // backup current target pressure + targetROPumpPressure.ovData = value; + targetROPumpPressure.override = OVERRIDE_KEY; + result = setROPumpTargetPressure( value, roPumpControlMode ); + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetTargetROPumpPressureOverride function resets the override of the + * target RO pressure. + * @details + * Inputs : none + * Outputs : targetROPumpPressure + * @return TRUE if override reset successful, FALSE if not + *************************************************************************/ +BOOL testResetTargetROPumpPressureOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + targetROPumpPressure.data = targetROPumpPressure.ovInitData; // restore pre-override target pressure + targetROPumpPressure.override = OVERRIDE_RESET; + targetROPumpPressure.ovInitData = 0; + targetROPumpPressure.ovData = 0; + result = setROPumpTargetPressure( targetROPumpPressure.data, roPumpControlMode ); + } + + return result; +} /*********************************************************************//** * @brief - * The testSetMeasuredROFlowRateOverride function overrides the measured \n + * The testSetMeasuredROFlowRateOverride function overrides the measured * RO flow rate. * @details * Inputs: none @@ -814,12 +919,13 @@ /*********************************************************************//** * @brief - * The testResetMeasuredROFlowRateOverride function resets the override of the \n + * The testResetMeasuredROFlowRateOverride function resets the override of the * measured RO flow rate. * @details * Inputs: none * Outputs: measuredROFlowRateLPM * @param: none + * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testResetMeasuredROFlowRateOverride( void ) @@ -835,3 +941,5 @@ return result; } + +/**@}*/