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; Index: firmware/App/Controllers/Valves.h =================================================================== diff -u -r986147effa2865d32f37c8c4c2b8b396eda847e0 -r425de1be48f86645589caea4ac832c58655f75b2 --- firmware/App/Controllers/Valves.h (.../Valves.h) (revision 986147effa2865d32f37c8c4c2b8b396eda847e0) +++ firmware/App/Controllers/Valves.h (.../Valves.h) (revision 425de1be48f86645589caea4ac832c58655f75b2) @@ -30,20 +30,22 @@ // ********** public definitions ********** -/// Enumeration of valve states +/// Enumeration of available valve states per valve type (from SA and DG SRS documents) typedef enum ValveStates { - VALVE_STATE_OPEN = 0, ///< Open valve state - VALVE_STATE_CLOSED, ///< Closed valve state - VALVE_STATE_FILL, ///< Fill valve state - VALVE_STATE_NOFILL, ///< No Fill valve state - VALVE_STATE_DRAIN, ///< Drain valve state - VALVE_STATE_RECIRCULATE, ///< Recirculate valve state - VALVE_STATE_R1_C_TO_NO, ///< Reservoir 1 Common to Normally Open valve state - VALVE_STATE_R1_C_TO_NC, ///< Reservoir 1 Common to Normally Closed valve state - VALVE_STATE_R2_C_TO_NO, ///< Reservoir 2 Common to Normally Open valve state - VALVE_STATE_R2_C_TO_NC, ///< Reservoir 2 Common to Normally Closed valve state - NUM_OF_VALVE_STATES ///< Number of valve states + VALVE_STATE_OPEN = 0, ///< Open valve state, used only for VPi, VBf, and VSP + VALVE_STATE_CLOSED, ///< Closed valve state, used only for VPi, VBf, and VSP + VALVE_STATE_OPEN_C_TO_NO, ///< Open Common to Normally Open valve state, used only for VPd + VALVE_STATE_DRAIN_C_TO_NC, ///< Drain Common to Normally Closed valve state, used only for VPd + VALVE_STATE_NOFILL_C_TO_NO, ///< No Fill Common to Normally Open valve state, used only for VPo + VALVE_STATE_FILL_C_TO_NC, ///< Fill Common to Normally Closed valve state, used only for VPo + VALVE_STATE_DRAIN_C_TO_NO, ///< Drain Common to Normally Open valve state, used only for VDr and VRc + VALVE_STATE_RECIRC_C_TO_NC, ///< Recirculate Common to Normally Closed valve state, used only for VDr and VRc + VALVE_STATE_R1_C_TO_NO, ///< Reservoir 1 Common to Normally Open valve state, used only for VRo and VRi + VALVE_STATE_R1_C_TO_NC, ///< Reservoir 1 Common to Normally Closed valve state, used only for VRd and VRf + VALVE_STATE_R2_C_TO_NO, ///< Reservoir 2 Common to Normally Open valve state, used only for VRd and VRf + VALVE_STATE_R2_C_TO_NC, ///< Reservoir 2 Common to Normally Closed valve state, used only for VRo and VRi + NUM_OF_VALVE_STATES ///< number of valve states } VALVE_STATES_T; /// Enumeration of valves @@ -65,18 +67,15 @@ // ********** public function prototypes ********** -void initValves( void ); // Initialize the valves module -void execValves( void ); // Execute the valves driver +void initValves( void ); // initialize the valves driver +void execValves( void ); // execute the valves driver -// SELF_TEST_STATUS_T execValvesTest( void ); // TODO: Add back in for POST on DG valves +BOOL setValveState( VALVES_T valve, VALVE_STATES_T valveState ); // set valve's state to valveState +DATA_ARRAY_GET_PROTOTYPE( U32, getValveState, valveID ); // get valve state for given valve -BOOL setValveState( VALVES_T valve, VALVE_STATES_T valveState ); // Set valve's state to valveState - -DATA_ARRAY_GET_PROTOTYPE( F32, getValveState, valveID ); // Get valveID's state - BOOL testSetValvesStatesPublishIntervalOverride( U32 value ); BOOL testResetValvesStatesPublishIntervalOverride( void ); -BOOL testSetValveStateOverride( U32 valve, F32 state ); +BOOL testSetValveStateOverride( U32 valve, U32 state ); BOOL testResetValveStateOverride( U32 valveID ); /**@}*/ Index: firmware/App/Services/SystemCommMessages.c =================================================================== diff -u -r986147effa2865d32f37c8c4c2b8b396eda847e0 -r425de1be48f86645589caea4ac832c58655f75b2 --- firmware/App/Services/SystemCommMessages.c (.../SystemCommMessages.c) (revision 986147effa2865d32f37c8c4c2b8b396eda847e0) +++ firmware/App/Services/SystemCommMessages.c (.../SystemCommMessages.c) (revision 425de1be48f86645589caea4ac832c58655f75b2) @@ -363,32 +363,19 @@ * refer to setFPGAValveStates function in FPGA.c for details * @return TRUE if msg successfully queued for transmit, FALSE if not *************************************************************************/ -BOOL broadcastValvesStates( F32 valvesStates ) +BOOL broadcastValvesStates( U16 valvesStates ) { BOOL result; MESSAGE_T msg; U08 *payloadPtr = msg.payload; - VALVE_STATES_T payload; // create a message record blankMessage( &msg ); msg.hdr.msgID = MSG_ID_DG_VALVES_STATES; - msg.hdr.payloadLen = sizeof( VALVE_STATES_T ); + msg.hdr.payloadLen = sizeof( U16 ); - payload.VRF = valvesStates; - payload.VRI = valvesStates; - payload.VRD = valvesStates; - payload.VRO = valvesStates; - payload.VPO = valvesStates; - payload.VBF = valvesStates; - payload.VRC = valvesStates; - payload.VDR = valvesStates; - payload.VPI = valvesStates; - payload.VSP = valvesStates; - payload.VPD = valvesStates; + memcpy( payloadPtr, &valvesStates, sizeof( U16 ) ); - memcpy( payloadPtr, &payload, sizeof( VALVE_STATES_T ) ); - // serialize the message (w/ sync, CRC, and appropriate CAN padding) and add serialized message data to appropriate comm buffer result = serializeMessage( msg, COMM_BUFFER_OUT_CAN_DG_BROADCAST, ACK_NOT_REQUIRED ); Index: firmware/App/Services/SystemCommMessages.h =================================================================== diff -u -r986147effa2865d32f37c8c4c2b8b396eda847e0 -r425de1be48f86645589caea4ac832c58655f75b2 --- firmware/App/Services/SystemCommMessages.h (.../SystemCommMessages.h) (revision 986147effa2865d32f37c8c4c2b8b396eda847e0) +++ firmware/App/Services/SystemCommMessages.h (.../SystemCommMessages.h) (revision 425de1be48f86645589caea4ac832c58655f75b2) @@ -39,7 +39,7 @@ BOOL broadcastLoadCellData( F32 loadCellA1, F32 loadCellA2, F32 loadCellB1, F32 loadCellB2 ); // MSG_ID_DG_VALVES_STATES -BOOL broadcastValvesStates( F32 valvesStates ); +BOOL broadcastValvesStates( U16 valvesStates ); // *********** public test support message functions ********** Index: firmware/App/Tasks/TaskPriority.c =================================================================== diff -u -rc8c60f543b53cd96008914a01145f5b08426903e -r425de1be48f86645589caea4ac832c58655f75b2 --- firmware/App/Tasks/TaskPriority.c (.../TaskPriority.c) (revision c8c60f543b53cd96008914a01145f5b08426903e) +++ firmware/App/Tasks/TaskPriority.c (.../TaskPriority.c) (revision 425de1be48f86645589caea4ac832c58655f75b2) @@ -14,13 +14,13 @@ * **************************************************************************/ -#include "gio.h" - #include "FPGA.h" +#include "gio.h" #include "InternalADC.h" #include "LoadCell.h" -#include "WatchdogMgmt.h" #include "TaskPriority.h" +#include "Valves.h" +#include "WatchdogMgmt.h" /************************************************************************* * @brief taskPriority