Index: firmware/App/Controllers/Valves.c =================================================================== diff -u -r51fa142977df9fbfdc2db8ea00903e1568ef9a08 -raf0ce65ce725e352e2b0e1ca2f047bd94afc6e9f --- firmware/App/Controllers/Valves.c (.../Valves.c) (revision 51fa142977df9fbfdc2db8ea00903e1568ef9a08) +++ firmware/App/Controllers/Valves.c (.../Valves.c) (revision af0ce65ce725e352e2b0e1ca2f047bd94afc6e9f) @@ -43,20 +43,19 @@ 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 U16 currentValvesStates = INIT_VALVES_STATES; ///< initialize current valves states -static int execValvesCounter = 0; ///< initialize execValves counter +static int valveStateMismatchCounter = 0; ///< initialize valve state mismatch 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 // ********** private function prototypes ********** -static BOOL checkValves( VALVES_T valve, VALVE_STATE_NAMES_T valveState ); ///< check validity of requested valve state +static BOOL checkValveStateName( VALVES_T valve, VALVE_STATE_NAMES_T valveState ); ///< check validity of requested valve state name for given valve +U32 convertValveStateNameToValveState(VALVE_STATE_NAMES_T valveState); ///< Convert valve state name to de-/energized valve state U16 fromU32ArrayToU16( OVERRIDE_U32_T array[ NUM_OF_VALVES ] ); ///< convert array of U32 to U16 for statesValves preparation static void publishValvesStates( void ); static DATA_GET_PROTOTYPE( U32, getPublishValvesStatesInterval ); -/**@}*/ - /*********************************************************************//** * @brief * The initValves function initializes the Valves module. @@ -67,6 +66,17 @@ *************************************************************************/ void initValves( void ) { + // initialize current valves states (for both override and actual) + int i; + + for (i = 0; i < NUM_OF_VALVES; i++) + { + valves[ i ].data = 0; + valves[ i ].ovInitData = 0; + valves[ i ].ovData = 0; + valves[ i ].override = OVERRIDE_RESET; + } + setFPGAValveStates( INIT_VALVES_STATES ); // initially set all valves to de-energized state via FPGA valvesStatesPublicationTimerCounter = 0; // reset valves states publication timer } @@ -81,46 +91,43 @@ *************************************************************************/ void execValves( void ) { - execValvesCounter++; // increment execValves counter by 1 - - if ( execValvesCounter <= 2 ) + if ( valveStateMismatchCounter < 2 ) { valvesStates = getFPGAValveStates(); // get valves states from FPGA - currentValvesStates = fromU32ArrayToU16( valves ); // Check if valves states from FPGA (valvesStates) equals current valves states (valves) if ( valvesStates != currentValvesStates ) { valvesStates = currentValvesStates; - - // set valves states via FPGA to current valves states - setFPGAValveStates( valvesStates ); + valveStateMismatchCounter++; // increment valve state mismatch counter by 1 } else // valvesStates from FPGA match valves' current states { - execValvesCounter = 0; // reset execValves counter + valveStateMismatchCounter = 0; // reset valve state mismatch counter because valves and valvesStates match } - - publishValvesStates(); // publish valves states on interval } else // if requested valve state does not match FPGA-provided valve state three (3) times in row { activateAlarmNoData( ALARM_ID_VALVE_EXEC_COUNTER_ERROR ); } + + currentValvesStates = fromU32ArrayToU16( valves ); + setFPGAValveStates( valvesStates ); // set valves states via FPGA to current valves states + publishValvesStates(); // publish valves states on interval } /*********************************************************************//** * @brief - * The checkValveState function checks the validity of the requested valve \n - * state for given valve. + * The checkValveStateName function checks the validity of requested valve \n + * state name for given valve. * @details * Inputs : valve, valveState * Outputs : none * @return none *************************************************************************/ -static BOOL checkValves( VALVES_T valve, VALVE_STATE_NAMES_T valveState ) +static BOOL checkValveStateName( VALVES_T valve, VALVE_STATE_NAMES_T valveState ) { - BOOL result = FALSE; // initialize checkValves result flag to FALSE + BOOL result = FALSE; // initialize result flag to FALSE switch ( valveState ) { @@ -253,83 +260,77 @@ for ( i = 0; i < NUM_OF_VALVES; i++) { - if ( OVERRIDE_KEY == array[ i ].override ) - { - result += array[ i ].ovData * pow( 2, i ); - } - else - { - result += array[ i ].data * pow( 2, i ); - } + result |= ( getValveState( i ) == 1 ? 0x0001 << i : 0 ); } return result; } /*********************************************************************//** * @brief - * The convertToValveState function converts a text-based valve state \n - * to its corresponding de-energized (0) or energized (1) valve state. + * The convertValveStateNameToValveState function converts valve state \n + * name to its corresponding de-energized (0) or energized (1) valve state. * @details * Inputs : none * Outputs : none * @return converted U32 vState *************************************************************************/ -U32 convertToValveState(VALVE_STATE_NAMES_T valveState) +U32 convertValveStateNameToValveState(VALVE_STATE_NAMES_T valveState) { - U32 vState = 0; // initialize state to 0 + U32 vState = DEENERGIZED; // initialize valve state to de-energized switch (valveState) { case VALVE_STATE_OPEN: - vState = 1; // energized + vState = ENERGIZED; break; case VALVE_STATE_CLOSED: - vState = 0; // de-energized + vState = DEENERGIZED; break; case VALVE_STATE_OPEN_C_TO_NO: - vState = 0; // de-energized + vState = DEENERGIZED; break; case VALVE_STATE_DRAIN_C_TO_NC: - vState = 1; // energized + vState = ENERGIZED; break; case VALVE_STATE_NOFILL_C_TO_NO: - vState = 0; // de-energized + vState = DEENERGIZED; break; case VALVE_STATE_FILL_C_TO_NC: - vState = 1; // energized + vState = ENERGIZED; break; case VALVE_STATE_DRAIN_C_TO_NO: - vState = 0; // de-energized + vState = DEENERGIZED; break; case VALVE_STATE_RECIRC_C_TO_NC: - vState = 1; // energized + vState = ENERGIZED; break; case VALVE_STATE_R1_C_TO_NO: - vState = 0; // de-energized + vState = DEENERGIZED; break; case VALVE_STATE_R1_C_TO_NC: - vState = 1; // energized + vState = ENERGIZED; break; case VALVE_STATE_R2_C_TO_NO: - vState = 0; // de-energized + vState = DEENERGIZED; break; case VALVE_STATE_R2_C_TO_NC: - vState = 1; // energized + vState = ENERGIZED; break; default: + activateAlarmNoData( ALARM_ID_VALVE_ERROR ); break; } @@ -348,15 +349,11 @@ { BOOL result = FALSE; // initialize result flag to FALSE - if ( checkValves ( valve, valveState ) ) + if ( checkValveStateName ( valve, valveState ) ) { - valves[ valve ].data = convertToValveState( valveState ); // not done on override to allow DG state machine to set valves states back to real values + valves[ valve ].data = convertValveStateNameToValveState( valveState ); // not done on override to allow DG state machine to set valves states back to real values result = TRUE; } - else - { - activateAlarmNoData( ALARM_ID_VALVE_STATE_ERROR ); - } return result; } @@ -412,8 +409,6 @@ } } -/**@}*/ - /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ @@ -521,3 +516,5 @@ return result; } + +/**@}*/