Index: firmware/App/Controllers/ROPump.c =================================================================== diff -u -r7df1fae66b80c946ff1bcdee4b43afb5ab7a1d4c -r58d631f8ce26bf420a3fa087d4d1d86833aba002 --- firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 7df1fae66b80c946ff1bcdee4b43afb5ab7a1d4c) +++ firmware/App/Controllers/ROPump.c (.../ROPump.c) (revision 58d631f8ce26bf420a3fa087d4d1d86833aba002) @@ -21,9 +21,10 @@ #include "mibspi.h" #include "FPGA.h" +#include "NVDataMgmt.h" #include "OperationModes.h" -#include "PIControllers.h" #include "PersistentAlarm.h" +#include "PIControllers.h" #include "Pressures.h" #include "ROPump.h" #include "SafetyShutdown.h" @@ -112,7 +113,6 @@ 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. @@ -130,13 +130,9 @@ 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. +static DG_FLOW_SENSORS_CAL_RECORD_T flowSensorsCalRecord; ///< Flow sensors calibration record. // ********** private function prototypes ********** @@ -151,14 +147,15 @@ static void stopROPump( void ); static void publishROPumpData( void ); static U32 getPublishROPumpDataInterval( void ); +static BOOL processCalibrationData( void ); /*********************************************************************//** * @brief * The initROPump function initializes the RO Pump module. * @details Inputs: roControlTimerCounter,roPumpOpenLoopTargetDutyCycle, * roPumpFlowRateRunningSum, roPumpPressureRunningSum, measuredFlowReadingsSum, * flowFilterCounter, flowVerificationCounter, roPumpState, roPumpControlMode - * roPumpDataPublicationTimerCounter, roPumpControlModeSet + * roPumpDataPublicationTimerCounter * @details Outputs: none * @return none *************************************************************************/ @@ -176,7 +173,6 @@ // 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 @@ -194,8 +190,16 @@ roPumpDataPublicationTimerCounter = 0; roPumpState = RO_PUMP_OFF_STATE; roPumpControlMode = NUM_OF_PUMP_CONTROL_MODES; - roPumpControlModeSet = roPumpControlMode; isROPumpOn = FALSE; + + // TODO temporary initialization + // Reset the calibration variables + flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].fourthOrderCoeff = 0.0; + flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].thirdOrderCoeff = 0.0; + flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].secondOrderCoeff = 0.0; + flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].gain = 1.0; + flowSensorsCalRecord.flowSensors[ CAL_DATA_RO_PUMP_FLOW_SENSOR ].offset = 0.0; + // TODO temporary initialization, remove } /*********************************************************************//** @@ -339,20 +343,38 @@ BOOL isPressureMax = actualPressure >= MAX_ALLOWED_MEASURED_PRESSURE_PSI; checkPersistentAlarm( ALARM_ID_RO_PUMP_PRESSURE_OUT_OF_RANGE, isPressureMax, actualPressure, MAX_ALLOWED_MEASURED_PRESSURE_PSI ); + // Check if a new calibration is available + if ( TRUE == isNewCalibrationRecordAvailable() ) + { + // Get the new calibration data and check its validity + processCalibrationData(); + } + // Read flow at the control set if ( ++flowFilterCounter >= FLOW_SAMPLES_TO_AVERAGE ) { - F32 avgROFlow = (F32)measuredFlowReadingsSum * FLOW_AVERAGE_MULTIPLIER; + U32 sensor; + F32 flow = RO_FLOW_ADC_TO_LPM_FACTOR / ( (F32)measuredFlowReadingsSum * FLOW_AVERAGE_MULTIPLIER ); + // If the flow is less than a certain value, FPGA will return 0xFFFF meaning that // the flow is 0. Otherwise, convert the count to flow rate in mL/min - if ( ( roFlowReading == FLOW_SENSOR_ZERO_READING ) || ( roFlowReading == 0 ) ) + if ( ( FLOW_SENSOR_ZERO_READING == roFlowReading ) || ( 0 == roFlowReading ) ) { measuredROFlowRateLPM.data = 0.0; } else { - measuredROFlowRateLPM.data = RO_FLOW_ADC_TO_LPM_FACTOR / avgROFlow; + // Right now there is only one flow sensor but a for loop is used here to be able to automatically accommodate + // the future flow sensors + for( sensor = 0; sensor < NUM_OF_CAL_DATA_FLOW_SENSORS; sensor++ ) + { + measuredROFlowRateLPM.data = pow(flow, 4) * flowSensorsCalRecord.flowSensors[ sensor ].fourthOrderCoeff + + pow(flow, 3) * flowSensorsCalRecord.flowSensors[ sensor ].thirdOrderCoeff + + pow(flow, 2) * flowSensorsCalRecord.flowSensors[ sensor ].secondOrderCoeff + + flow * flowSensorsCalRecord.flowSensors[ sensor ].gain + + flowSensorsCalRecord.flowSensors[ sensor ].offset; + } } measuredFlowReadingsSum = 0; @@ -387,7 +409,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(); } @@ -470,23 +492,51 @@ /*********************************************************************//** * @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. + * The getTargetROPumpFlowRate function gets the current target RO pump + * flow rate. + * @details Inputs: targetROPumpFlowRate + * @details Outputs: targetROPumpFlowRate + * @return the current target RO flow rate (in L/min). *************************************************************************/ -SELF_TEST_STATUS_T execROPumpTest( void ) +F32 getTargetROPumpFlowRate( void ) { - SELF_TEST_STATUS_T result = SELF_TEST_STATUS_FAILED; + return targetROPumpFlowRate; +} - // TODO - implement self-test(s) +/*********************************************************************//** + * @brief + * The getMeasuredROFlowRate function gets the measured RO pump flow rate. + * @details Inputs: measuredROFlowRateLPM + * @details Outputs: measuredROFlowRateLPM + * @return the current RO pump flow rate (in L/min). + *************************************************************************/ +F32 getMeasuredROFlowRate( void ) +{ + F32 result = measuredROFlowRateLPM.data; + if ( OVERRIDE_KEY == measuredROFlowRateLPM.override ) + { + result = measuredROFlowRateLPM.ovData; + } + return result; } /*********************************************************************//** * @brief + * The getTargetROPumpPressure function gets the current target RO pump + * pressure. + * @details Inputs: targetROPumpPressure + * @details Outputs: targetROPumpPressure + * @return the current target RO targetROPumpPressure in psi. + *************************************************************************/ +F32 getTargetROPumpPressure( void ) +{ + return targetROPumpMaxPressure; +} + +/*********************************************************************//** + * @brief * The handleROPumpOffState function handles the RO pump off state of the * controller state machine. * @details Inputs: roPumpControlMode, roPumpPWMDutyCyclePctSet, @@ -501,7 +551,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() ); @@ -583,7 +632,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 *************************************************************************/ @@ -621,7 +669,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 *************************************************************************/ @@ -745,51 +792,49 @@ /*********************************************************************//** * @brief - * The getTargetROPumpFlowRate function gets the current target RO pump - * flow rate. - * @details Inputs: targetROPumpFlowRate - * @details Outputs: targetROPumpFlowRate - * @return the current target RO flow rate (in L/min). + * The processCalibrationData function gets the calibration data and makes + * sure it is valid by checking the calibration date. The calibration date + * should not be 0. + * @details Inputs: none + * @details Outputs: flowSensorsCalRecord + * @return TRUE if the calibration record is valid, otherwise FALSE *************************************************************************/ -F32 getTargetROPumpFlowRate( void ) +static BOOL processCalibrationData( void ) { - return targetROPumpFlowRate; -} + BOOL status = TRUE; + U32 sensor; -/*********************************************************************//** - * @brief - * The getMeasuredROFlowRate function gets the measured RO pump flow rate. - * @details Inputs: measuredROFlowRateLPM - * @details Outputs: measuredROFlowRateLPM - * @return the current RO pump flow rate (in L/min). - *************************************************************************/ -F32 getMeasuredROFlowRate( void ) -{ - F32 result = measuredROFlowRateLPM.data; + // Get the calibration record from NVDataMgmt + DG_FLOW_SENSORS_CAL_RECORD_T calData = getDGFlowSensorsCalibrationRecord(); - if ( OVERRIDE_KEY == measuredROFlowRateLPM.override ) + for ( sensor = 0; sensor < NUM_OF_CAL_DATA_FLOW_SENSORS; sensor++ ) { - result = measuredROFlowRateLPM.ovData; + // Check if the calibration data that was received from NVDataMgmt is legitimate + // The calibration date item should not be zero. If the calibration date is 0, + // then the flow sensors data is not stored in the NV memory or it was corrupted. + if ( 0 == calData.flowSensors[ sensor ].calibrationTime ) + { +#ifndef DISABLE_CAL_CHECK + SET_ALARM_WITH_1_U32_DATA( ALARM_ID_DG_FLOW_SENSORS_INVALID_CALIBRATION, (U32)sensor ); +#endif + status = FALSE; + } + else + { + // The calibration data was valid, update the local copy + flowSensorsCalRecord.flowSensors[ sensor ].fourthOrderCoeff = calData.flowSensors[ sensor ].fourthOrderCoeff; + flowSensorsCalRecord.flowSensors[ sensor ].thirdOrderCoeff = calData.flowSensors[ sensor ].thirdOrderCoeff; + flowSensorsCalRecord.flowSensors[ sensor ].secondOrderCoeff = calData.flowSensors[ sensor ].secondOrderCoeff; + flowSensorsCalRecord.flowSensors[ sensor ].gain = calData.flowSensors[ sensor ].gain; + flowSensorsCalRecord.flowSensors[ sensor ].offset = calData.flowSensors[ sensor ].offset; + } } - return result; + return status; } /*********************************************************************//** * @brief - * The getTargetROPumpPressure function gets the current target RO pump - * pressure. - * @details Inputs: targetROPumpPressure - * @details Outputs: targetROPumpPressure - * @return the current target RO targetROPumpPressure in psi. - *************************************************************************/ -F32 getTargetROPumpPressure( void ) -{ - return targetROPumpMaxPressure; -} - -/*********************************************************************//** - * @brief * The publishROPumpData function publishes RO pump data at the set interval. * @details Inputs: roPumpDataPublicationTimerCounter * @details Outputs: roPumpDataPublicationTimerCounter @@ -813,6 +858,7 @@ } } + /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/