Index: firmware/App/Controllers/Pressures.c =================================================================== diff -u -rf308cc4c35eab630ebbbde405cfe47d049afeafb -rfeb93744f73bc0a3d58841bb02bd05c38357f35d --- firmware/App/Controllers/Pressures.c (.../Pressures.c) (revision f308cc4c35eab630ebbbde405cfe47d049afeafb) +++ firmware/App/Controllers/Pressures.c (.../Pressures.c) (revision feb93744f73bc0a3d58841bb02bd05c38357f35d) @@ -8,19 +8,19 @@ * @file Pressures.c * * @author (last) Quang Nguyen -* @date (last) 03-Aug-2020 +* @date (last) 14-Sep-2020 * * @author (original) Sean * @date (original) 04-Apr-2020 * ***************************************************************************/ -#include -#include "Pressures.h" #include "AlarmMgmt.h" #include "FPGA.h" #include "InternalADC.h" -#include "OperationModes.h" +#include "OperationModes.h" +#include "PersistentAlarm.h" +#include "Pressures.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" #include "TaskPriority.h" @@ -33,69 +33,63 @@ // ********** private definitions ********** -#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. +#define MIN_INLET_WATER_PRESSURE 25 ///< Minimum water input pressure. +#define INLET_WATER_PRESSURE_PERSISTENCE_PERIOD ( 5 * MS_PER_SECOND ) ///< Persistence period 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 { - PRESSURE_INIT_STATE = 0, ///< Initialization state. - PRESSURE_CONTINUOUS_READ_STATE, ///< Continuous read sensors state. - NUM_OF_PRESSURE_STATES ///< Number of pressure monitor states. + PRESSURE_INIT_STATE = 0, ///< Initialization state + PRESSURE_CONTINUOUS_READ_STATE, ///< Continuous read sensors state + NUM_OF_PRESSURE_STATES ///< Number of pressure monitor states } PRESSURE_STATE_T; -/// Defined states for the pressures self test state machine. +/// Defined states for the pressures self-test state machine. typedef enum Pressures_Self_Test_States { - PRESSURE_SELF_TEST_STATE_START = 0, ///< Self test start state. - PRESSURE_TEST_STATE_IN_PROGRESS, ///< Self test in progress state. - PRESSURE_TEST_STATE_COMPLETE, ///< Self test completed state. - NUM_OF_PRESSURE_SELF_TEST_STATES ///< Number of pressure self test states. + PRESSURE_SELF_TEST_STATE_START = 0, ///< Self-test start state + PRESSURE_TEST_STATE_IN_PROGRESS, ///< Self-test in progress state + PRESSURE_TEST_STATE_COMPLETE, ///< Self-test completed state + NUM_OF_PRESSURE_SELF_TEST_STATES ///< Number of pressure self-test states } PRESSURE_SELF_TEST_STATE_T; // ********** private data ********** -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 PRESSURE_STATE_T pressuresState; ///< current state of pressure monitor state machine. +static U32 pressuresDataPublicationTimerCounter = 0; ///< used to schedule pressure data publication 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 OVERRIDE_U32_T pressuresDataPublishInterval = { PRESSURES_DATA_PUB_INTERVAL, + PRESSURES_DATA_PUB_INTERVAL, 0, 0 }; /// Pressure data publish interval. +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 inletWaterLowPressureCounter = 0; ///< Persistence counter for inlet water low pressure -static U32 inletWaterPressureInRangeCounter = 0; ///< Persistence counter for inlet water in range pressure +static S32 measuredPressureReadingsSum[ NUM_OF_PRESSURE_SENSORS ]; ///< Raw pressure sensor sums for averaging. +static U32 pressureFilterCounter = 0; ///< Used to schedule pressure sensor filtering. -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 +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 publishPressuresData( void ); -static DATA_GET_PROTOTYPE( U32, getPublishPressuresDataInterval ); +static U32 getPublishPressuresDataInterval( void ); static SELF_TEST_STATUS_T handleSelfTestADCCheck( void ); /*********************************************************************//** * @brief * The initPressures function initializes the Pressures module. - * @details - * Inputs : none - * Outputs : Pressures module initialized. + * @details Inputs: none + * @details Outputs: Pressures module initialized. * @return none *************************************************************************/ void initPressures( void ) @@ -114,17 +108,20 @@ pressuresState = PRESSURE_INIT_STATE; pressuresSelfTestState = PRESSURE_SELF_TEST_STATE_START; - inletWaterLowPressureCounter = 0; - inletWaterPressureInRangeCounter = 0; - pressuresDataPublicationTimerCounter = 0; + pressuresDataPublicationTimerCounter = 0; + + initPersistentAlarm( PERSISTENT_ALARM_INLET_WATER_LOW_PRESSURE, ALARM_ID_INLET_WATER_LOW_PRESSURE, + FALSE, INLET_WATER_PRESSURE_PERSISTENCE_PERIOD, INLET_WATER_PRESSURE_PERSISTENCE_PERIOD ); + initPersistentAlarm( PERSISTENT_ALARM_INLET_WATER_PRESSURE_FAULT, ALARM_ID_INLET_WATER_PRESSURE_FAULT, + FALSE, INLET_WATER_PRESSURE_PERSISTENCE_PERIOD, INLET_WATER_PRESSURE_PERSISTENCE_PERIOD ); } /*********************************************************************//** * @brief - * The execPressures function executes the pressure monitor. - * @details - * Inputs : pressuresState - * Outputs : pressuresState + * The execPressures function executes the pressures' monitor state machine + * and publish pressures' data. + * @details Inputs: pressuresState + * @details Outputs: pressuresState * @return none *************************************************************************/ void execPressures( void ) @@ -152,11 +149,10 @@ /*********************************************************************//** * @brief - * The handlePressuresInitState function handles the pressures initialize state \n + * The handlePressuresInitState function handles the pressures initialize state * of the pressures monitor state machine. - * @details - * Inputs : TBD - * Outputs : TBD + * @details Inputs: none + * @details Outputs: none * @return next state *************************************************************************/ static PRESSURE_STATE_T handlePressuresInitState( void ) @@ -168,11 +164,10 @@ /*********************************************************************//** * @brief - * The handlePressuresContReadState function handles the continuous read state \n + * The handlePressuresContReadState function handles the continuous read state * of the pressures monitor state machine. - * @details - * Inputs : TBD - * Outputs : pressure sensor values updated + * @details Inputs: pressureFilterCounter + * @details Outputs: pressure sensor values updated * @return next state *************************************************************************/ static PRESSURE_STATE_T handlePressuresContReadState( void ) @@ -222,11 +217,9 @@ /*********************************************************************//** * @brief - * The getPublishPresOcclDataInterval function gets the pressure/occlusion data \n - * publication interval. - * @details - * Inputs : pressuresDataPublishInterval - * Outputs : none + * The getPublishPressuresDataInterval function gets the pressure data publish internval. + * @details Inputs: pressuresDataPublishInterval + * @details Outputs: none * @return the current pressures data publication interval (in task intervals). *************************************************************************/ static U32 getPublishPressuresDataInterval( void ) @@ -243,42 +236,44 @@ /*********************************************************************//** * @brief - * The checkInletPressures function checks inlet water pressure value + * The checkInletPressure function checks inlet water pressure value * and triggers an alarm when pressure value is out of allowed range. - * @details - * Inputs : pressures[] - * Outputs : none + * @details Inputs: RO pump inlet pressure sensor value + * @details Outputs: Triggers low pressure persistent alarm * @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 ); - } - } + BOOL const isPressureTooLow = ( pressure < MIN_INLET_WATER_PRESSURE ); + + // TODO add the right limit values + checkPersistentAlarm( PERSISTENT_ALARM_INLET_WATER_LOW_PRESSURE, isPressureTooLow, pressure, 0 ); } + +/*********************************************************************//** + * @brief + * The checkInletPressureFault function checks inlet water pressure value + * and triggers a machine fault when pressure value is out of allowed range. + * @details Inputs: RO pump inlet pressure sensor value + * @details Outputs: Triggers pressure fault persistent alarm + * @return none + *************************************************************************/ +void checkInletPressureFault( void ) +{ + F32 const pressure = getMeasuredDGPressure( PRESSURE_SENSOR_RO_PUMP_INLET ); + BOOL const isPressureTooLow = ( pressure < MIN_INLET_WATER_PRESSURE ); + + // TODO add the right limit values + checkPersistentAlarm( PERSISTENT_ALARM_INLET_WATER_PRESSURE_FAULT, isPressureTooLow, pressure, 0 ); +} /*********************************************************************//** * @brief * The getMeasuredArterialPressure function gets the current arterial pressure. - * @details - * Inputs : arterialPressure - * Outputs : none + * @details Inputs: arterialPressure + * @details Outputs: none + * @param pressureID pressure sensor ID * @return the current arterial pressure (in mmHg). *************************************************************************/ F32 getMeasuredDGPressure( U32 pressureID ) @@ -306,11 +301,9 @@ /*********************************************************************//** * @brief - * The publishPressuresData function publishes DG pressures data at the \n - * set interval. - * @details - * Inputs : pressuresDataPublicationTimerCounter - * Outputs : DG Pressures data are published to CAN bus. + * The publishPressuresData function publishes DG pressures data at a set interval. + * @details Inputs: pressuresDataPublicationTimerCounter + * @details Outputs: Pressures data are published to CAN bus * @return none *************************************************************************/ static void publishPressuresData( void ) @@ -343,11 +336,9 @@ /*********************************************************************//** * @brief - * The execPressureSelfTest function executes the state machine for the \n - * Pressures self test. - * @details - * Inputs : pressuresSelfTestState - * Outputs : pressuresSelfTestState + * The execPressureSelfTest function executes the pressures self-test's state machine. + * @details Inputs: pressuresSelfTestState + * @details Outputs: pressuresSelfTestState * @return PressuresSelfTestResult (SELF_TEST_STATUS_T) *************************************************************************/ SELF_TEST_STATUS_T execPressureSelfTest( void ) @@ -365,12 +356,11 @@ break; case PRESSURE_TEST_STATE_COMPLETE: - // Done with self test + // 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 ); + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_TEMPERATURE_SENSORS_INVALID_SELF_TEST_STATE, pressuresSelfTestState ); break; } @@ -381,22 +371,20 @@ * @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 + * or equals zero, it will throw an alarm. + * @details Inputs: ADC reading for RO pump inlet pressure sensor + * @details Outputs: Performed ADC check self-test. * @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 ) ) + if ( ( inletPressureADCReading == 0 ) || ( inletPressureADCReading >= INT_ADC_FULL_SCALE_BITS ) ) { result = SELF_TEST_STATUS_FAILED; - SET_ALARM_WITH_1_U32_DATA ( ALARM_ID_PRESSURE_SENSOR_FAULT, inletPressureADCReading ); + SET_ALARM_WITH_1_U32_DATA( ALARM_ID_PRESSURE_SENSOR_FAULT, inletPressureADCReading ); } return result; @@ -407,15 +395,14 @@ * 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) + * The testSetPressuresDataPublishIntervalOverride function overrides the + * pressure data publish interval. + * @details Inputs: none + * @details Outputs: pressuresDataPublishInterval + * @param value value to override pressure data publish interval with (in ms) * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetPressuresDataPublishIntervalOverride( U32 value ) @@ -436,11 +423,10 @@ /*********************************************************************//** * @brief - * The testResetPressuresDataPublishIntervalOverride function resets the override \n + * The testResetPressuresDataPublishIntervalOverride function resets the override * of the pressure and occlusion data publish interval. - * @details - * Inputs : none - * Outputs : pressuresDataPublishInterval + * @details Inputs: none + * @details Outputs: pressuresDataPublishInterval * @return TRUE if override reset successful, FALSE if not *************************************************************************/ BOOL testResetPressuresDataPublishIntervalOverride( void ) @@ -459,12 +445,12 @@ /*********************************************************************//** * @brief - * The testSetDGPressureSensorOverride function overrides the value of the \n + * The testSetDGPressureSensorOverride function overrides the value of the * 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 + * @details Inputs: none + * @details Outputs: pressures[] + * @param sensor pressure sensor ID + * @param value override value for pressure data * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetDGPressureSensorOverride( U32 sensor, F32 value ) @@ -488,10 +474,9 @@ * @brief * The testResetDGPressureSensorOverride function resets the override of the \n * specified DG pressure sensor. - * @details - * Inputs : none - * Outputs : pressures[] - * @param value ID of sensor to reset override pressure for + * @details Inputs: none + * @details Outputs: pressures[] + * @param sensor pressure sensor ID * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testResetDGPressureSensorOverride( U32 sensor )