Index: firmware/App/Controllers/Valves.c =================================================================== diff -u -r986147effa2865d32f37c8c4c2b8b396eda847e0 -r425de1be48f86645589caea4ac832c58655f75b2 --- firmware/App/Controllers/Valves.c (.../Valves.c) (revision 986147effa2865d32f37c8c4c2b8b396eda847e0) +++ firmware/App/Controllers/Valves.c (.../Valves.c) (revision 425de1be48f86645589caea4ac832c58655f75b2) @@ -16,7 +16,9 @@ #include "AlarmMgmt.h" #include "FPGA.h" +#include #include "OperationModes.h" +#include "stdbool.h" #include "SystemCommMessages.h" #include "TaskPriority.h" #include "Timers.h" @@ -29,36 +31,26 @@ // ********** private definitions ********** -#define DEENERGIZED 0 -#define ENERGIZED 1 -#define INIT_VALVES_STATES 0b0000000000000000 +#define DEENERGIZED 0 /// 0 for de-energized valve +#define ENERGIZED 1 /// 1 for energized valve +#define INIT_VALVES_STATES 0 /// 0 in U16 initial state of all valves -/// Default publication interval for valve states -#define VALVES_STATE_PUB_INTERVAL ( 500 / TASK_PRIORITY_INTERVAL ) ///< interval (ms/task time) at which the valves state is published on the CAN bus +/// Default publication interval for valves states +#define VALVES_STATE_PUB_INTERVAL ( 500 / TASK_PRIORITY_INTERVAL ) ///< interval ( ms / task time) at which valves states are published on CAN bus -/// Defined states for the valves self test state machine -typedef enum Valves_Self_Test_States -{ - VALVE_SELF_TEST_STATE_START = 0, ///< Self test start state - VALVE_TEST_STATE_IN_PROGRESS, ///< Self test in progress state - VALVE_TEST_STATE_COMPLETE, ///< Self test completed state - NUM_OF_VALVE_SELF_TEST_STATES ///< Number of valve self test states -} VALVE_SELF_TEST_STATE_T; - // ********** private data ********** -static VALVE_STATES_T valvesStates = INIT_VALVES_STATES; ///< current state of valve driver state machine -static U32 valvesStatesPublicationTimerCounter = 0; ///< used to schedule valve state publication to CAN bus +static U16 valvesStates = INIT_VALVES_STATES; ///< initialize valves states for FPGA to set +static U32 valvesStatesPublicationTimerCounter = 0; ///< used to schedule valve state publication to CAN bus +static int execValvesCounter = 0; ///< initialize execValves counter +static OVERRIDE_U32_T valves[ NUM_OF_VALVES ]; ///< Current valves states static OVERRIDE_U32_T valvesStatesPublishInterval = { VALVES_STATE_PUB_INTERVAL, VALVES_STATE_PUB_INTERVAL, 0, 0 }; ///< interval (in ms/task interval) at which to publish valves state to CAN bus -static OVERRIDE_F32_T valves[ NUM_OF_VALVES ]; ///< Obtained state from valves -static VALVE_SELF_TEST_STATE_T valvesSelfTestState = VALVE_SELF_TEST_STATE_START; ///< current valve self test state -static U32 valvesSelfTestTimerCount = 0; ///< timer counter for valve self test - // ********** private function prototypes ********** -static void checkValves( void ); +static BOOL checkValves( VALVES_T valve, VALVE_STATES_T valveState ); ///< check validity of requested valve state +U16 fromIntArrayToU16( int array[ NUM_OF_VALVES ] ); ///< convert array of Int to U16 for statesValves preparation static void publishValvesStates( void ); static DATA_GET_PROTOTYPE( U32, getPublishValvesStatesInterval ); @@ -74,44 +66,163 @@ *************************************************************************/ void initValves( void ) { - setFPGAValveStates( INIT_VALVES_STATES ); - valvesStatesPublicationTimerCounter = 0; - valvesSelfTestTimerCount = 0; + setFPGAValveStates( INIT_VALVES_STATES ); // initially set all valves to de-energized state via FPGA + valvesStatesPublicationTimerCounter = 0; // reset valves states publication timer } /*********************************************************************//** * @brief - * The execValves function executes the valve driver. + * The execValves function executes the valves driver. * @details * Inputs : valvesStates * Outputs : valvesStates * @return none *************************************************************************/ void execValves( void ) { - setFPGAValveStates( valvesStates ); + execValvesCounter++; // increment execValves counter by 1 - // publish valves states on interval - publishValvesStates(); + if ( execValvesCounter <= 2 ) + { + //int i = 0; + + //for ( valves[ i ]; i <= NUM_OF_VALVES; i++ ) + //{ + // valvesStates = getFPGAValveStates(); + + // if ( getValveState( valves [ i ] ) == fromU16ToIntArray( getFPGAValveStates(), i ) + // { + // setFPGAValveStates( valvesStates ); // set valves states via FPGA + // publishValvesStates(); // publish valves states on interval + // execValvesCounter = 0; // reset execValves counter + // } + //} + if ( OVERRIDE_KEY == valves[ valve ].override ) + { + valvesStates = valves[ valve ].ovData; + } + else + { + valvesStates = valves[ valve ].data; + } + + setFPGAValveStates( valvesStates ); + publishValvesStates(); // publish valves states on interval + execValvesCounter = 0; // reset execValves counter + } + else // if requested valve state does not match FPGA-provided valve state three (3) times in row + { + activateAlarmNoData( ALARM_ID_VALVE_EXEC_COUNTER_ERROR ); + } + + publishValvesStates(); // publish valves states on interval } /*********************************************************************//** * @brief - * The checkValveState function checks the validity of the requested valve state. + * The checkValveState function checks the validity of the requested valve \n + * state for given valve. * @details - * Inputs : valveID, valveState + * Inputs : valve, valveState * Outputs : none * @return none *************************************************************************/ -static void checkValves( void ) +static BOOL checkValves( VALVES_T valve, VALVE_STATES_T valveState ) { - /* TODO: compare the requested valve state to the allowable valves \n - * states for a given valve; - * - if checks, return True - * - if not, return false - */ + BOOL result = FALSE; // initialize checkValves result flag to FALSE - return; + switch ( valveState ) + { + case VALVE_STATE_OPEN: + if ( ( valve == VPI ) || ( valve == VBF ) || ( valve == VSP ) ) + { + result = TRUE; + } + break; + + case VALVE_STATE_CLOSED: + if ( ( valve == VPI ) || ( valve == VBF ) || ( valve == VSP ) ) + { + result = TRUE; + } + break; + + case VALVE_STATE_OPEN_C_TO_NO: + if ( valve == VPD ) + { + result = TRUE; + } + break; + + case VALVE_STATE_DRAIN_C_TO_NC: + if ( valve == VPD ) + { + result = TRUE; + } + break; + + case VALVE_STATE_NOFILL_C_TO_NO: + if ( valve == VPO ) + { + result = TRUE; + } + break; + + case VALVE_STATE_FILL_C_TO_NC: + if ( valve == VPO ) + { + result = TRUE; + } + break; + + case VALVE_STATE_DRAIN_C_TO_NO: + if ( ( valve == VDR ) || ( valve == VRC ) ) + { + result = TRUE; + } + break; + + case VALVE_STATE_RECIRC_C_TO_NC: + if ( ( valve == VDR ) || ( valve == VRC ) ) + { + result = TRUE; + } + break; + + case VALVE_STATE_R1_C_TO_NO: + if ( ( valve == VRO ) || ( valve == VRI ) ) + { + result = TRUE; + } + break; + + case VALVE_STATE_R1_C_TO_NC: + if ( ( valve == VRD ) || ( valve == VRF ) ) + { + result = TRUE; + } + break; + + case VALVE_STATE_R2_C_TO_NO: + if ( ( valve == VRD ) || ( valve == VRF ) ) + { + result = TRUE; + } + break; + + case VALVE_STATE_R2_C_TO_NC: + if ( ( valve == VRO ) || ( valve == VRI ) ) + { + result = TRUE; + } + break; + + default: + activateAlarmNoData( ALARM_ID_VALVE_STATE_CHECK_ERROR ); + break; + } + + return result; } /*********************************************************************//** @@ -137,15 +248,140 @@ /*********************************************************************//** * @brief + * The fromIntArrayToU16 function converts an array of Int valve states \n + * to U16 valvesStates that can be passed to FPGA for setting valves. + * @details + * Inputs : none + * Outputs : none + * @return converted U16 valvesStates + *************************************************************************/ +U16 fromIntArrayToU16( int array[ NUM_OF_VALVES ] ) +{ + U16 result = 0; // initialize result flag to 0 + int i; + + for ( i = 0; i < NUM_OF_VALVES; i++) + { + result += array[ i ] * pow( 2, i ); + } + + return result; +} + +/*********************************************************************//** + * @brief + * The convertToValveState function converts a text-based valve state \n + * to its corresponding de-energized (0) or energized (1) valve state. + * @details + * Inputs : none + * Outputs : none + * @return converted U32 vState + *************************************************************************/ +U32 convertToValveState(VALVE_STATES_T valveState) +{ + U32 vState = 0; // initialize state to 0 + + switch (valveState) + { + case VALVE_STATE_OPEN: + vState = 1; // Energized + break; + + case VALVE_STATE_CLOSED: + vState = 0; // De-energized + break; + + case VALVE_STATE_OPEN_C_TO_NO: + vState = 0; // De-energized + break; + + case VALVE_STATE_DRAIN_C_TO_NC: + vState = 1; // Energized + break; + + case VALVE_STATE_NOFILL_C_TO_NO: + vState = 0; // De-energized + break; + + case VALVE_STATE_FILL_C_TO_NC: + vState = 1; // Energized + break; + + case VALVE_STATE_DRAIN_C_TO_NO: + vState = 0; // De-energized + break; + + case VALVE_STATE_RECIRC_C_TO_NC: + vState = 1; // Energized + break; + + case VALVE_STATE_R1_C_TO_NO: + vState = 0; // De-energized + break; + + case VALVE_STATE_R1_C_TO_NC: + vState = 1; // Energized + break; + + case VALVE_STATE_R2_C_TO_NO: + vState = 0; // De-energized + break; + + case VALVE_STATE_R2_C_TO_NC: + vState = 1; // Energized + break; + + default: + break; + } + + return vState; +} + +/*********************************************************************//** + * @brief + * The setValveState function sets the valve state for given valve. + * @details + * Inputs : none + * Outputs : none + * @return TRUE if new valve state is set for given valve + *************************************************************************/ +BOOL setValveState( VALVES_T valve, VALVE_STATES_T valveState ) +{ + BOOL result = FALSE; // initialize result flag to FALSE + + if ( checkValves ( valve, valveState ) ) + { + if ( OVERRIDE_KEY == valves[ valve ].override ) + { + valves[ valve ].ovData = convertToValveState( valveState ); + } + else + { + valves[ valve ].data = convertToValveState( valveState ); + } + + result = TRUE; + } + else + { + activateAlarmNoData( ALARM_ID_VALVE_STATE_ERROR ); + } + + return result; +} + +/*********************************************************************//** + * @brief * The getValveState function gets the current valve state for given valve. * @details * Inputs : valveState * Outputs : none * @return the current valve state for given valve. *************************************************************************/ -F32 getValveState( U32 valve ) +U32 getValveState( U32 valve ) { - BOOL valveState = DEENERGIZED; + int valveState = DEENERGIZED; if ( valve < NUM_OF_VALVES ) { @@ -160,7 +396,7 @@ } else { - activateAlarmNoData( ALARM_ID_VALVE_STATE_ERROR ); + activateAlarmNoData( ALARM_ID_VALVE_ERROR ); } return valveState; @@ -186,24 +422,6 @@ } } -/*********************************************************************//** - * @brief - * The execValvesTest function executes the state machine for the \n - * valves self test. - * @details - * Inputs : none - * Outputs : none - * @return the current state of the valves self test. - *************************************************************************/ -SELF_TEST_STATUS_T execValvesTest( void ) -{ - SELF_TEST_STATUS_T result = SELF_TEST_STATUS_FAILED; - - // TODO - implement self test(s) - - return result; -} - /**@}*/ /************************************************************************* @@ -270,7 +488,7 @@ * @param value : override value for the given valve ID * @return TRUE if override successful, FALSE if not *************************************************************************/ -BOOL testSetValveStateOverride( U32 valve, F32 value ) +BOOL testSetValveStateOverride( U32 valve, U32 value ) { BOOL result = FALSE;