Index: firmware/App/Controllers/Pressures.c =================================================================== diff -u -ra7bf3ca23ea37a61000379facae628a31b3ecc59 -rf308cc4c35eab630ebbbde405cfe47d049afeafb --- firmware/App/Controllers/Pressures.c (.../Pressures.c) (revision a7bf3ca23ea37a61000379facae628a31b3ecc59) +++ firmware/App/Controllers/Pressures.c (.../Pressures.c) (revision f308cc4c35eab630ebbbde405cfe47d049afeafb) @@ -7,21 +7,22 @@ * * @file Pressures.c * -* @author (last) Sean Nash -* @date (last) 25-Jun-2020 +* @author (last) Quang Nguyen +* @date (last) 03-Aug-2020 * * @author (original) Sean * @date (original) 04-Apr-2020 * ***************************************************************************/ - +#include + #include "Pressures.h" #include "AlarmMgmt.h" #include "FPGA.h" #include "InternalADC.h" -#include "LoadCell.h" // TODO - test - remove later #include "OperationModes.h" -#include "SystemCommMessages.h" +#include "SystemCommMessages.h" +#include "TaskGeneral.h" #include "TaskPriority.h" #include "Timers.h" @@ -32,15 +33,19 @@ // ********** private definitions ********** -/// Default publication interval for pressure and occlusion data. -#define PRESSURES_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< interval (ms/task time) at which the pressures data is published on the CAN bus. +#define PUMP_PRESSURE_ZERO 777 ///< ADC counts equivalent to 0 PSI for pump in/out pressure sensors. +#define PUMP_PRESSURE_PSIA_PER_COUNT 0.06434 ///< PSIA per ADC count conversion factor for pump in/out pressure sensors. +#define PUMP_PRESSURE_PSIA_TO_PSI_OFFSET 14.7 ///< Subtract this offset to convert PSIA to PSI. -#define PUMP_PRESSURE_ZERO 777 ///< ADC counts equivalent to 0 PSI for pump in/out pressure sensors. -#define PUMP_PRESSURE_PSIA_PER_COUNT 0.06434 ///< PSIA per ADC count conversion factor for pump in/out pressure sensors. -#define PUMP_PRESSURE_PSIA_TO_PSI_OFFSET 14.7 ///< Subtract this offset to convert PSIA to PSI. +#define PRESSURE_SAMPLES_TO_AVERAGE (200 / TASK_PRIORITY_INTERVAL) ///< Averaging pressure data over the reporting interval. +#define PRESSURE_AVERAGE_MULTIPLIER (1.0 / (F32)PRESSURE_SAMPLES_TO_AVERAGE) ///< Optimization - multiplying is faster than dividing. -#define PRESSURE_SAMPLES_TO_AVERAGE (200 / TASK_PRIORITY_INTERVAL) ///< Averaging pressure data over the reporting interval. -#define PRESSURE_AVERAGE_MULTIPLIER (1.0 / (F32)PRESSURE_SAMPLES_TO_AVERAGE) ///< Optimization - multiplying is faster than dividing. +#define PRESSURE_SENSORS_ADC_BITS 12U ///< Pressure sensors ADC bits +#define PRESSURE_SENSORS_ADC_MAX_COUNT ( pow(2, PRESSURE_SENSORS_ADC_BITS) - 1 ) ///< Pressure sensors max ADC count + +#define MIN_INLET_WATER_PRESSURE 25 ///< Minimum water input pressure +#define INLET_WATER_PRESSURE_PERSISTENCE_COUNT ( 5 * MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Persistence count for pressure out of range error +#define PRESSURES_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< interval (ms/task time) at which the pressures data is published on the CAN bus. /// Defined states for the pressures monitor state machine. typedef enum PresOccl_States @@ -61,25 +66,29 @@ // ********** private data ********** -static PRESSURE_STATE_T pressuresState = PRESSURE_INIT_STATE; ///< current state of pressure monitor state machine. +static PRESSURE_STATE_T pressuresState; ///< current state of pressure monitor state machine. static U32 pressuresDataPublicationTimerCounter = 0; ///< used to schedule pressure data publication to CAN bus. - -static OVERRIDE_U32_T pressuresDataPublishInterval = { PRESSURES_DATA_PUB_INTERVAL, PRESSURES_DATA_PUB_INTERVAL, 0, 0 }; ///< interval (in ms/task interval) at which to publish pressures data to CAN bus. + +/// interval (in ms/task interval) at which to publish pressures data to CAN bus. +static OVERRIDE_U32_T pressuresDataPublishInterval = { PRESSURES_DATA_PUB_INTERVAL, PRESSURES_DATA_PUB_INTERVAL, 0, 0 }; static OVERRIDE_F32_T pressures[ NUM_OF_PRESSURE_SENSORS ]; ///< Measured pressure from sensors. static S32 measuredPressureReadingsSum[ NUM_OF_PRESSURE_SENSORS ]; ///< Raw pressure sensor sums for averaging. -static U32 pressureFilterCounter = 0; ///< used to schedule pressure sensor filtering. +static U32 pressureFilterCounter = 0; ///< Used to schedule pressure sensor filtering. +static U32 inletWaterLowPressureCounter = 0; ///< Persistence counter for inlet water low pressure +static U32 inletWaterPressureInRangeCounter = 0; ///< Persistence counter for inlet water in range pressure -static PRESSURE_SELF_TEST_STATE_T pressuresSelfTestState = PRESSURE_SELF_TEST_STATE_START; ///< current pressure self test state. -static U32 pressuresSelfTestTimerCount = 0; ///< timer counter for pressure self test. +static PRESSURE_SELF_TEST_STATE_T pressuresSelfTestState; ///< Current pressure self test state. +static SELF_TEST_STATUS_T pressuresSelfTestResult; ///< Self test result of the Pressures module // ********** private function prototypes ********** static PRESSURE_STATE_T handlePressuresInitState( void ); static PRESSURE_STATE_T handlePressuresContReadState( void ); -static void checkPressures( void ); static void publishPressuresData( void ); static DATA_GET_PROTOTYPE( U32, getPublishPressuresDataInterval ); + +static SELF_TEST_STATUS_T handleSelfTestADCCheck( void ); /*********************************************************************//** * @brief @@ -104,10 +113,11 @@ } pressuresState = PRESSURE_INIT_STATE; - pressuresSelfTestState = PRESSURE_SELF_TEST_STATE_START; + pressuresSelfTestState = PRESSURE_SELF_TEST_STATE_START; + inletWaterLowPressureCounter = 0; + inletWaterPressureInRangeCounter = 0; pressuresDataPublicationTimerCounter = 0; - pressuresSelfTestTimerCount = 0; -} +} /*********************************************************************//** * @brief @@ -131,7 +141,7 @@ break; default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_SOFTWARE_FAULT, 0, pressuresState ) // TODO - replace 1st param with s/w fault enum + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, 0, pressuresState ) // TODO - replace 1st param with s/w fault enum pressuresState = PRESSURE_INIT_STATE; break; } @@ -205,37 +215,21 @@ measuredPressureReadingsSum[ PRESSURE_SENSOR_DRAIN_PUMP_OUTLET ] = 0; } - // check for pressure ranges - checkPressures(); - // TODO - any other checks return result; } /*********************************************************************//** * @brief - * The checkPressures function checks the pressure sensors are in range. - * @details - * Inputs : pressures[] - * Outputs : none - * @return none - *************************************************************************/ -static void checkPressures( void ) -{ - // TODO - RO inlet s/b 30..90 PSI when inlet valve is open (w/ persistence) -} - -/*********************************************************************//** - * @brief * The getPublishPresOcclDataInterval function gets the pressure/occlusion data \n * publication interval. * @details * Inputs : pressuresDataPublishInterval * Outputs : none * @return the current pressures data publication interval (in task intervals). *************************************************************************/ -U32 getPublishPressuresDataInterval( void ) +static U32 getPublishPressuresDataInterval( void ) { U32 result = pressuresDataPublishInterval.data; @@ -246,6 +240,38 @@ return result; } + +/*********************************************************************//** + * @brief + * The checkInletPressures function checks inlet water pressure value + * and triggers an alarm when pressure value is out of allowed range. + * @details + * Inputs : pressures[] + * Outputs : none + * @return none + *************************************************************************/ +void checkInletPressure( void ) +{ + F32 const pressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_INLET ); + if ( pressure < MIN_INLET_WATER_PRESSURE ) + { + ++inletWaterLowPressureCounter; + inletWaterPressureInRangeCounter = 0; + if ( inletWaterLowPressureCounter > INLET_WATER_PRESSURE_PERSISTENCE_COUNT ) + { + SET_ALARM_WITH_1_F32_DATA( ALARM_ID_INLET_WATER_LOW_PRESSURE, pressure ); + } + } + else + { + ++inletWaterPressureInRangeCounter; + inletWaterLowPressureCounter = 0; + if ( inletWaterPressureInRangeCounter > INLET_WATER_PRESSURE_PERSISTENCE_COUNT ) + { + clearAlarm( ALARM_ID_INLET_WATER_LOW_PRESSURE ); + } + } +} /*********************************************************************//** * @brief @@ -272,7 +298,7 @@ } else { - activateAlarmNoData( ALARM_ID_SOFTWARE_FAULT ); + activateAlarmNoData( ALARM_ID_DG_SOFTWARE_FAULT ); } return result; @@ -317,38 +343,79 @@ /*********************************************************************//** * @brief - * The execPressuresTest function executes the state machine for the \n + * The execPressureSelfTest function executes the state machine for the \n * Pressures self test. * @details - * Inputs : none - * Outputs : none - * @return the current state of the Pressures self test. + * Inputs : pressuresSelfTestState + * Outputs : pressuresSelfTestState + * @return PressuresSelfTestResult (SELF_TEST_STATUS_T) *************************************************************************/ -SELF_TEST_STATUS_T execPressuresTest( void ) -{ - SELF_TEST_STATUS_T result = SELF_TEST_STATUS_FAILED; +SELF_TEST_STATUS_T execPressureSelfTest( void ) +{ + switch ( pressuresSelfTestState ) + { + case PRESSURE_SELF_TEST_STATE_START: + pressuresSelfTestState = PRESSURE_TEST_STATE_IN_PROGRESS; + pressuresSelfTestResult = SELF_TEST_STATUS_IN_PROGRESS; + break; + + case PRESSURE_TEST_STATE_IN_PROGRESS: + pressuresSelfTestResult = handleSelfTestADCCheck(); + pressuresSelfTestState = PRESSURE_TEST_STATE_COMPLETE; + break; + + case PRESSURE_TEST_STATE_COMPLETE: + // Done with self test + break; + + default: + SET_ALARM_WITH_2_U32_DATA ( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_TEMPERATURE_SENSORS_INVALID_SELF_TEST_STATE, + pressuresSelfTestState ); + break; + } + + return pressuresSelfTestResult; +} + +/*********************************************************************//** + * @brief + * The handleSelfTestADCCheck function checks whether the ADC reads and + * report status back. If the reads are above the maximum 12bit ADC count + * or below 0, it will throw an alarm. + * @details + * Inputs : none + * Outputs : none + * @param none + * @return result (SELF_TEST_STATUS_T) + *************************************************************************/ +static SELF_TEST_STATUS_T handleSelfTestADCCheck( void ) +{ + SELF_TEST_STATUS_T result = SELF_TEST_STATUS_PASSED; + U16 const inletPressureADCReading = getIntADCReading( INT_ADC_RO_PUMP_INLET_PRESSURE ); + + if ( ( inletPressureADCReading == 0 ) || ( inletPressureADCReading >= PRESSURE_SENSORS_ADC_MAX_COUNT ) ) + { + result = SELF_TEST_STATUS_FAILED; + SET_ALARM_WITH_1_U32_DATA ( ALARM_ID_PRESSURE_SENSOR_FAULT, inletPressureADCReading ); + } + + return result; +} - // TODO - implement self test(s) - return result; -} - -/**@}*/ - - /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ + - /*********************************************************************//** * @brief * The testSetPressuresDataPublishIntervalOverride function overrides the \n * pressure and occlusion data publish interval. * @details * Inputs : none * Outputs : pressuresDataPublishInterval - * @param value : override pressure and occlusion data publish interval with (in ms) + * @param value override pressure and occlusion data publish interval with (in ms) * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetPressuresDataPublishIntervalOverride( U32 value ) @@ -396,8 +463,8 @@ * specified pressure sensor with a given value. * Inputs : none * Outputs : pressures[] - * @param sensor : ID of pressure sensor to override for - * @param value : override value for the given pressure sensor ID + * @param sensor ID of pressure sensor to override for + * @param value override value for the given pressure sensor ID * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetDGPressureSensorOverride( U32 sensor, F32 value ) @@ -424,7 +491,7 @@ * @details * Inputs : none * Outputs : pressures[] - * @param value : ID of sensor to reset override pressure for + * @param value ID of sensor to reset override pressure for * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testResetDGPressureSensorOverride( U32 sensor ) @@ -445,4 +512,3 @@ } /**@}*/ -