Index: firmware/App/Controllers/Valves.c =================================================================== diff -u -r8466e63f95f65a3ffb18c3af85ac99328e41167b -r8bd1ae47aa13a843aa8abd6321ddc050deacb4a6 --- firmware/App/Controllers/Valves.c (.../Valves.c) (revision 8466e63f95f65a3ffb18c3af85ac99328e41167b) +++ firmware/App/Controllers/Valves.c (.../Valves.c) (revision 8bd1ae47aa13a843aa8abd6321ddc050deacb4a6) @@ -1,17 +1,17 @@ /************************************************************************** * -* Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. +* Copyright (c) 2019-2021 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * -* @file Valves.c +* @file Valves.c * -* @author (last) Sean Nash -* @date (last) 14-Oct-2020 +* @author (last) Dara Navaei +* @date (last) 10-Nov-2021 * -* @author (original) Dara Navaei -* @date (original) 07-Aug-2020 +* @author (original) Dara Navaei +* @date (original) 07-Aug-2020 * ***************************************************************************/ @@ -20,6 +20,7 @@ #include "gio.h" #include "FPGA.h" +#include "MessageSupport.h" #include "SafetyShutdown.h" #include "SystemCommMessages.h" #include "TaskPriority.h" @@ -131,7 +132,7 @@ typedef enum valve_modes { VALVE_CONTROL_MODE_ENABLE_PID = 0, ///< Valve control mode enable PID - VALVE_CONTORL_MODE_ENABLE_BYPASS, ///< Valve control mode enable bypass + VALVE_CONTROL_MODE_ENABLE_BYPASS, ///< Valve control mode enable bypass VALVE_CONTROL_MODE_DISABLE_ALL, ///< Valve control mode disable all modes NUM_OF_VALVE_CONTROL_MODES, ///< Number of valve control modes } VALVE_MODE_T; @@ -169,7 +170,8 @@ { VALVE_POSITION_T commandedPosition; ///< Valve commanded position enum VALVE_POSITION_T currentPosition; ///< Valve current position enum - OVERRIDE_S32_T currentPositionInCounts; ///< Valve current position in counts + VALVE_POSITION_T pendingCommandedPosition; ///< Valve pending position enum + OVERRIDE_S32_T currentPositionInCounts; ///< Valve current position in counts S16 targetPositionInCounts; ///< Valve target position in counts BOOL hasTransitionBeenRequested; ///< Valve transition request flag VALVE_STATE_T execState; ///< Valve execution state @@ -257,8 +259,7 @@ /*********************************************************************//** * @brief * The initValves function initializes the valves driver. - * @details Inputs: valveSelfTestState, valvesSelfTestResult, valvesStatus, - * valvesControlSetBits + * @details Inputs: none * @details Outputs: valveSelfTestState, valvesSelfTestResult, valvesStatus * @return none *************************************************************************/ @@ -293,7 +294,7 @@ /*********************************************************************//** * @brief * The homeValve function sets the homing request flag of a valve to TRUE. - * @details Inputs: valvesStatus + * @details Inputs: none * @details Outputs: valvesStatus (The flag that sets the homing request) * @param valve that is set to be homed * @return none @@ -318,7 +319,7 @@ /*********************************************************************//** * @brief * The setValvePosition function sets the requested position of a valve. - * @details Inputs: valvesStatus + * @details Inputs: none * @details Outputs: valvesStatus * @param valve that its position is set * @param position that the valve is set @@ -330,11 +331,10 @@ // Valve not in position cannot be requested // All the other positions are valid - if ( position != VALVE_POSITION_NOT_IN_POSITION && valve < NUM_OF_VALVES ) + if ( ( position != VALVE_POSITION_NOT_IN_POSITION ) && ( valve < NUM_OF_VALVES ) ) { - valvesStatus[ valve ].commandedPosition = position; + valvesStatus[ valve ].pendingCommandedPosition = position; valvesStatus[ valve ].hasTransitionBeenRequested = TRUE; - result = TRUE; } else @@ -372,7 +372,7 @@ /*********************************************************************//** * @brief * The getValveCurrent function returns the current of a valve. - * @details Inputs: none + * @details Inputs: valvesStatus * @details Outputs: valvesStatus * @param valve that the current is requested * @return returns the current of the valve in float @@ -393,7 +393,7 @@ * @brief * The setValveBloodTrap function set the blood trap valve to open or close * position. - * @details Inputs: valveAirTrapStatus + * @details Inputs: none * @details Outputs: valveAirTrapStatus * @param state enumeration of open or close for valve * @return: none @@ -532,8 +532,8 @@ /*********************************************************************//** * @brief * The handleValveSelfTestEnableValves function starts the valves self test. - * @details Inputs: valvesSelfTestResult, valvesSelfTestResult - * @details Outputs: valvesSelfTestResult, valvesSelfTestResult + * @details Inputs: none + * @details Outputs: valvesStatus, valvesSelfTestResult * @return next state of the self test state machine *************************************************************************/ static VALVE_SELF_TEST_STATE_T handleValveSelfTestEnableValves( void ) @@ -559,7 +559,7 @@ * @brief * The handleValveSelfTestConfirmEnable function checks to make sure the * valves are enabled properly. - * @details Inputs: valvesSelfTestResult + * @details Inputs: none * @details Outputs: valvesSelfTestResult * @return next state of the self test state machine *************************************************************************/ @@ -720,7 +720,7 @@ // Set the current position to Position C and the commanded position to Position A valvesStatus[ valve ].hasValveBeenHomed = TRUE; valvesStatus[ valve ].currentPosition = VALVE_POSITION_C_CLOSE; - valvesStatus[ valve ].commandedPosition = VALVE_POSITION_A_INSERT_EJECT; + valvesStatus[ valve ].pendingCommandedPosition = VALVE_POSITION_A_INSERT_EJECT; valvesStatus[ valve ].hasTransitionBeenRequested = TRUE; valvesStatus[ valve ].hasHomingBeenRequested = FALSE; valvesStatus[ valve ].numberOfFailedHomings = 0; @@ -783,34 +783,31 @@ } #endif - if ( valvesStatus[ valve ].hasHomingBeenRequested ) + if ( TRUE == valvesStatus[ valve ].hasHomingBeenRequested ) { state = VALVE_STATE_HOMING_NOT_STARTED; } - else if ( valvesStatus[ valve ].hasValveBeenHomed && valvesStatus[ valve ].hasTransitionBeenRequested ) + else if ( ( TRUE == valvesStatus[ valve ].hasValveBeenHomed ) && ( TRUE == valvesStatus[ valve ].hasTransitionBeenRequested ) ) { - if ( valvesStatus[ valve ].currentPosition != valvesStatus[ valve ].commandedPosition ) + if ( valvesStatus[ valve ].currentPosition != valvesStatus[ valve ].pendingCommandedPosition ) { // Just set the valves to transition so it will not be in a known position for a while - valvesStatus[ valve ].transitionStartTime = getMSTimerCount(); + valvesStatus[ valve ].commandedPosition = valvesStatus[ valve ].pendingCommandedPosition; + valvesStatus[ valve ].transitionStartTime = getMSTimerCount(); + valvesStatus[ valve ].hasTransitionBeenRequested = FALSE; setValveNextStep( valve, TRANSITION_STEP_CHANGE_IN_COUNTS ); state = VALVE_STATE_IN_TRANSITION; } - else - { - valvesStatus[ valve ].hasTransitionBeenRequested = FALSE; - } } // This option is only available in a debug build #ifdef DEBUG_ENABLED // Check if the valves have been homed and a bypass mode has been requested - else if ( valvesStatus[ valve ].hasValveBeenHomed && - valvesStatus[ valve ].bypassModeStatus.hasBypassModeBeenRequeseted ) + else if ( ( TRUE == valvesStatus[ valve ].hasValveBeenHomed ) && ( TRUE == valvesStatus[ valve ].bypassModeStatus.hasBypassModeBeenRequeseted ) ) { // Set the valve control mode to bypass and set the PWM - setValveControlMode( valve, VALVE_CONTORL_MODE_ENABLE_BYPASS ); - valvesStatus[ valve ].controlMode = VALVE_CONTORL_MODE_ENABLE_BYPASS; + setValveControlMode( valve, VALVE_CONTROL_MODE_ENABLE_BYPASS ); + valvesStatus[ valve ].controlMode = VALVE_CONTROL_MODE_ENABLE_BYPASS; valvesStatus[ valve ].bypassModeStatus.hasChangeBeenRequested = FALSE; setFPGAValvePWM( valve ); @@ -847,15 +844,12 @@ { // The valve is in range. Set the current position to the commanded position valvesStatus[ valve ].currentPosition = commandedPositionEnum; - // Set the transition request to false as it has been serviced - valvesStatus[ valve ].hasTransitionBeenRequested = FALSE; // If we are in Pos B, enable current relaxation if ( commandedPositionEnum == VALVE_POSITION_B_OPEN ) { // Enable current relaxation setFPGAValveSetPoint( valve, currentPosition, TRUE ); - valvesStatus[ valve ].hasTransitionBeenRequested = TRUE; } // Go back to Idle state @@ -864,18 +858,16 @@ // Check if the valve's transition time has timed out else if ( didTimeout( valvesStatus[ valve ].transitionStartTime, VALVE_TRANSITION_TIMEOUT_MS ) ) { - valvesStatus[ valve ].hasTransitionBeenRequested = FALSE; + //valvesStatus[ valve ].hasTransitionBeenRequested = FALSE; TODO remove // Go back to Idle state state = VALVE_STATE_IDLE; #ifndef DISABLE_VALVE_ALARMS SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_VALVE_TRANSITION_TIMEOUT, (U32)valve ); #endif // If the valve transition timeout is active and the valve's commanded positions is position C, activate safety shutdown - if ( isAlarmActive( ALARM_ID_HD_VALVE_TRANSITION_TIMEOUT ) && valvesStatus[ valve ].commandedPosition == VALVE_POSITION_C_CLOSE ) + if ( ( TRUE == isAlarmActive( ALARM_ID_HD_VALVE_TRANSITION_TIMEOUT ) ) && ( VALVE_POSITION_C_CLOSE == valvesStatus[ valve ].commandedPosition ) ) { activateSafetyShutdown(); - // Set the alarm - SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_VALVE_TRANSITION_TIMEOUT, (U32)valve ); } } // Check if the valve is close to the temporary target position and if it is, assign the next target position @@ -926,8 +918,7 @@ /*********************************************************************//** * @brief * The setValveControlMode function sets the valves control mode. - * @details Inputs: valvesControlSetBits, valvesControlModesSetBits, - * valvesControlModesResetBits + * @details Inputs: none * @details Outputs: valvesControlSetBits * @param valve that its control mode is set * @param mode the control mode that the valve will be set to @@ -984,7 +975,7 @@ * The areValvesEnabled function checks the current valves status from FPGA * to the enable bit mask of each valve. If any of the valves is not enabled * the function raises an alarm. - * @details Inputs: valvesStatus, valvesControlStatusBits + * @details Inputs: valvesStatus * @details Outputs: none * @return Returns TRUE if all the valves are enabled properly *************************************************************************/ @@ -1017,7 +1008,7 @@ result = FALSE; } } - else if ( mode == VALVE_CONTORL_MODE_ENABLE_BYPASS ) + else if ( mode == VALVE_CONTROL_MODE_ENABLE_BYPASS ) { if ( ! ( status & VALVE_CONTROL_STATUS_BITS[ valve ][ VALVE_CONTROL_STATUS_BYPASS_ENABLED ] ) ) { @@ -1048,7 +1039,7 @@ /*********************************************************************//** * @brief - * The setFPGAValue function sets the position of a valve to FPGA. + * The setFPGAValveSetPoint function sets the position of a valve to FPGA. * @details Inputs: none * @details Outputs: none * @param valve that its FPGA set point is updated @@ -1157,8 +1148,8 @@ static void getAndMonitorValvesCurrentFPGAPosition( void ) { VALVE_T valve; - S16 currentPostion = 0; - S16 commandedPoistion = 0; + S16 currentPosition = 0; + S16 commandedPosition = 0; VALVE_POSITION_T commandedPositionEnum; // Get the position of the valves and update the structure of each valve @@ -1176,9 +1167,9 @@ { U32 maxDeviation = MAX_DEVIATION_FROM_TARGET_IN_COUNTS; - currentPostion = getValvePositionCounts( valve ); + currentPosition = getValvePositionCounts( valve ); commandedPositionEnum = valvesStatus[ valve ].commandedPosition; - commandedPoistion = valvesStatus[ valve ].positions[ commandedPositionEnum ]; + commandedPosition = valvesStatus[ valve ].positions[ commandedPositionEnum ]; // If the current position is B, current relaxation requires looser range check if ( VALVE_POSITION_B_OPEN == commandedPositionEnum ) @@ -1189,18 +1180,18 @@ // Check if the current position has deviated from the position it is supposed to be in // For more than a certain amount of time. If it has, raise an alarm // Absolute value is used for comparison to cover +/- from the commanded position - if ( abs( currentPostion - commandedPoistion ) > maxDeviation ) + if ( abs( currentPosition - commandedPosition ) > maxDeviation ) { valvesStatus[ valve ].positionOutOfRangeCounter++; } if ( valvesStatus[ valve ].positionOutOfRangeCounter > MAX_POS_DEVIATION_TIME_INTERVAL_COUNTER ) { #ifndef DISABLE_VALVE_ALARMS - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_VALVE_POSITION_OUT_OF_RANGE, (U32)valve, currentPostion ); + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_VALVE_POSITION_OUT_OF_RANGE, (U32)valve, currentPosition ); #endif } - else if ( abs( currentPostion - commandedPoistion ) < MAX_DEVIATION_FROM_TARGET_IN_COUNTS && + else if ( abs( currentPosition - commandedPosition ) < MAX_DEVIATION_FROM_TARGET_IN_COUNTS && valvesStatus[ valve ].positionOutOfRangeCounter > 0 ) { valvesStatus[ valve ].positionOutOfRangeCounter = 0; @@ -1225,6 +1216,7 @@ if ( ++valvesStatus[ valve ].dataPublishCounter >= getU32OverrideValue( &valvesDataPublishInterval ) ) { HD_VALVE_DATA_T valveData; + valveData.valveID = (U32)valve; valveData.state = (U32)valvesStatus[ valve ].execState; valveData.currentPosID = (U32)getValvePosition( valve ); @@ -1237,7 +1229,7 @@ valveData.pwm = valvesStatus[ valve ].bypassModeStatus.currentPWMInPercent; valveData.airTrapValve = (U32)getValveAirTrapStatus(); - broadcastHDValves( &valveData ); + broadcastData( MSG_ID_HD_VALVES_DATA, COMM_BUFFER_OUT_CAN_HD_BROADCAST, (U08*)&valveData, sizeof( HD_VALVE_DATA_T ) ); valvesStatus[ valve ].dataPublishCounter = 0; } @@ -1386,7 +1378,7 @@ * @brief * The getValvesCurrentPWM function gets the PWM counts from the FPGA module * and converts them in to duty cycle in percent. - * @details Inputs: valvesStatus + * @details Inputs: none * @details Outputs: valvesStatus * @return none *************************************************************************/ @@ -1456,7 +1448,7 @@ * @brief * The testResetValvesDataPublishInterval function resets the override * of the valves publish interval. - * @details Inputs: valvesDataPublishInterval + * @details Inputs: none * @details Outputs: valvesDataPublishInterval * @return TRUE if override reset successful, FALSE if not *************************************************************************/ @@ -1477,7 +1469,7 @@ /*********************************************************************//** * @brief * The testSetValvesPositionOverride function overrides the valves position. - * @details Inputs: valvesPositionOverride, valvesStatus + * @details Inputs: none * @details Outputs: valvesPositionOverride * @param valve to override its position * @param position of the valve that will be overridden @@ -1567,7 +1559,7 @@ * @brief * The testResetValvePWMOverride function resets the override PWM in bypass * control mode. - * @details Inputs: valvesStatus + * @details Inputs: none * @details Outputs: valvesStatus * @param valve that its PWM override will be reset * @return TRUE if override reset successful, FALSE if not @@ -1640,8 +1632,8 @@ * @brief * The testSetValvesPositionCountOverride function overrides the valves * position in counts. - * @details Inputs: valvesPositionOverride, valvesStatus - * @details Outputs: valvesPositionOverride + * @details Inputs: valvesStatus + * @details Outputs: none * @param valve to override its position * @param position of the valve that will be overridden * @return TRUE if override successful, FALSE if not @@ -1666,7 +1658,7 @@ * @brief * The testResetValvesPositionCountOverride function resets the override * of the valves' position in counts. - * @details Inputs: valvesStatus + * @details Inputs: none * @details Outputs: valvesStatus * @param valve that its position count override will be reset * @return TRUE if override reset successful, FALSE if not