Index: firmware/App/Controllers/Valves.c =================================================================== diff -u -r5bfaf341319b9fd763d07ad0a68a986028c37587 -r5a5eda0e64a8990f5adecae65c035bfaeb77d8dd --- firmware/App/Controllers/Valves.c (.../Valves.c) (revision 5bfaf341319b9fd763d07ad0a68a986028c37587) +++ firmware/App/Controllers/Valves.c (.../Valves.c) (revision 5a5eda0e64a8990f5adecae65c035bfaeb77d8dd) @@ -1,14 +1,14 @@ /************************************************************************** * -* Copyright (c) 2019-2021 Diality Inc. - All Rights Reserved. +* Copyright (c) 2020-2022 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 * * @author (last) Dara Navaei -* @date (last) 10-Nov-2021 +* @date (last) 26-May-2022 * * @author (original) Dara Navaei * @date (original) 07-Aug-2020 @@ -21,8 +21,10 @@ #include "FPGA.h" #include "MessageSupport.h" +#include "NVDataMgmt.h" #include "SafetyShutdown.h" #include "SystemCommMessages.h" +#include "Switches.h" #include "TaskPriority.h" #include "Timers.h" #include "Valves.h" @@ -73,7 +75,7 @@ #define ENABLE_VALVE_CURRENT_RELAXATION 0x8000 ///< Enable valves current relaxation #define DISABLE_VALVE_CURRENT_RELAXATION 0x7FFF ///< Disable valves current relaxation -#define ADC_TO_CURRENT_CONVERSION_CONSTANT 2048.0 ///< Valves ADC count to current conversion +#define ADC_TO_CURRENT_CONVERSION_CONSTANT 2048.0F ///< Valves ADC count to current conversion #define INITIAL_EDGE_OFFSET_READ_COUNT 100U ///< Offset in counts from energized and de-energized edges @@ -90,7 +92,7 @@ #define MAX_ALLOWED_FAILED_HOMINGS 3U ///< Maximum allowed failed homings /// The time that the valve must be at the edge to be considered for edge detection static const U32 HOMING_EDGE_DETECTION_TIME_INTERVAL = ( MS_PER_SECOND / ( 2 * TASK_PRIORITY_INTERVAL ) ); -#define VALVES_CURRENT_THRESHOLD_AMPS 1.0 ///< Valves current threshold +#define VALVES_CURRENT_THRESHOLD_AMPS 1.0F ///< Valves current threshold /// Valves over current time interval counter static const U32 MAX_OVER_CURRENT_TIME_INTERVAL_COUNTER = ( MS_PER_SECOND / ( TASK_PRIORITY_INTERVAL ) ); /// Valves out of range time interval counter @@ -102,10 +104,12 @@ #define VALVE_MAX_ALLOWED_PWM_PERCENT 100U ///< Valve maximum allowed PWM in percent #define VALVE_CW_PWM_TO_CNT_CONVERSION( pwm ) ( ( 20 * pwm ) + 2500 ) ///< Valve clockwise PWM to count conversion -#define VALVE_CW_CNT_TO_PWM_CONVERSION( cnt ) ( ( 0.05 * cnt ) - 125 ) ///< Valve clockwise count to PWM conversion +#define VALVE_CW_CNT_TO_PWM_CONVERSION( cnt ) ( ( 0.05F * cnt ) - 125 ) ///< Valve clockwise count to PWM conversion #define VALVE_CCW_PWM_TO_CNT_CONVERSION( pwm ) ( ( -20 * pwm ) + 2500 ) ///< Valve counter clockwise PWM to count conversion -#define VALVE_CCW_CNT_TO_PWM_CONVERSION( cnt ) ( ( -0.05 * cnt ) + 125 ) ///< Valve counter clockwise count to PWM conversion +#define VALVE_CCW_CNT_TO_PWM_CONVERSION( cnt ) ( ( -0.05F * cnt ) + 125 ) ///< Valve counter clockwise count to PWM conversion +#define DATA_PUBLISH_COUNTER_START_COUNT 80 ///< Data publish counter start count. + /// Exec valve self test states typedef enum valves_Self_Test_States { @@ -213,18 +217,15 @@ static const U16 VALVE_CONTROL_MODES_RESET_BITS[ NUM_OF_VALVES ] = { VDI_RESET_CONTROL_BIT_MASK, VDO_RESET_CONTROL_BIT_MASK, VBA_RESET_CONTROL_BIT_MASK, VBV_RESET_CONTROL_BIT_MASK }; ///< Valves control modes rest bits -#ifndef DISABLE_3WAY_VALVES -#ifndef DISABLE_VALVE_ALARMS static const U16 VALVE_CONTROL_STATUS_BITS[ NUM_OF_VALVES ][ NUM_OF_VALVE_CONTROL_STATUS ] = { { VDI_INIT_STATUS_BIT_MASK, VDI_ENABLE_PID_STATUS_BIT_MASK, VDI_ENABLE_BYPASS_STATUS_BIT_MASK, VDI_RESET_CONTROL_BIT_MASK }, { VDO_INIT_STATUS_BIT_MASK, VDO_ENABLE_PID_STATUS_BIT_MASK, VDO_ENABLE_BYPASS_STATUS_BIT_MASK, VDO_RESET_CONTROL_BIT_MASK }, { VBA_INIT_STATUS_BIT_MASK, VBA_ENABLE_PID_STATUS_BIT_MASK, VBA_ENABLE_BYPASS_STATUS_BIT_MASK, VBA_RESET_CONTROL_BIT_MASK }, { VBV_INIT_STATUS_BIT_MASK, VBV_ENABLE_PID_STATUS_BIT_MASK, VBV_ENABLE_BYPASS_STATUS_BIT_MASK, VBV_RESET_CONTROL_BIT_MASK } }; ///< Valves control status bits -#endif -#endif static U16 valvesControlSetBits = 0x0000; ///< Valves control set bit static OPN_CLS_STATE_T valveAirTrapStatus; ///< Air trap valve status (open/close) +static HD_VALVES_CAL_RECORD_T valvesCalibrationRecord; ///< Valves calibration record. // Self test function prototypes static VALVE_SELF_TEST_STATE_T handleValveSelfTestEnableValves( void ); @@ -280,11 +281,12 @@ // Initialize some of the variables for ( valve = VDI; valve < NUM_OF_VALVES; valve++ ) { - valvesStatus[ valve ].execState = VALVE_STATE_WAIT_FOR_POST; - valvesStatus[ valve ].current.data = 0.0; - valvesStatus[ valve ].current.ovData = 0.0; + valvesStatus[ valve ].execState = VALVE_STATE_WAIT_FOR_POST; + valvesStatus[ valve ].current.data = 0.0; + valvesStatus[ valve ].current.ovData = 0.0; valvesStatus[ valve ].current.ovInitData = 0.0; - valvesStatus[ valve ].current.override = 0; + valvesStatus[ valve ].current.override = 0; + valvesStatus[ valve ].dataPublishCounter = DATA_PUBLISH_COUNTER_START_COUNT; } // Close air trap valve @@ -304,8 +306,16 @@ BOOL result = FALSE; if ( valve < NUM_OF_VALVES ) - { - valvesStatus[ valve ].hasHomingBeenRequested = TRUE; + { // If haven't already homed the valves, home the valves + if ( valvesStatus[ valve ].hasValveBeenHomed != TRUE ) + { + valvesStatus[ valve ].hasHomingBeenRequested = TRUE; + } + // Otherwise, go to position A (home position) + else + { + setValvePosition( valve, VALVE_POSITION_A_INSERT_EJECT ); + } result = TRUE; } else @@ -518,11 +528,14 @@ break; #endif +#ifndef _VECTORCAST_ + // The default cannot be reached in VectorCAST since the cases are run in a for loop default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_HD_VALVES_INVALID_EXEC_STATE, valvesStatus[ valve ].execState ); valvesStatus[ valve ].execState = VALVE_STATE_IDLE; break; +#endif } publishValvesData( valve ); @@ -567,7 +580,10 @@ { VALVE_SELF_TEST_STATE_T state = VALVE_SELF_TEST_COMPLETE; - if ( TRUE == areValvesFunctional() ) + BOOL calStatus = getNVRecord2Driver( GET_CAL_VALVES, (U08*)&valvesCalibrationRecord, sizeof( HD_VALVES_CAL_RECORD_T ), + NUM_OF_CAL_DATA_HD_VALVES, ALARM_ID_NO_ALARM ); + + if ( ( TRUE == areValvesFunctional() ) && ( TRUE == calStatus ) ) { valvesSelfTestResult = SELF_TEST_STATUS_PASSED; } @@ -610,16 +626,13 @@ *************************************************************************/ static VALVE_STATE_T handleValveStateHomingNotStarted( VALVE_T valve ) { - VALVE_STATE_T state = VALVE_STATE_HOMING_NOT_STARTED; + VALVE_STATE_T state = VALVE_STATE_HOMING_NOT_STARTED; + OPN_CLS_STATE_T frontDoor = getSwitchStatus( FRONT_DOOR ); - // TODO connect this flag to the doors driver later - // For now it is assumed the door is closed - BOOL isDoorClosed = TRUE; - // If homing has been requested or POST is completed and the door has been close for the specified - // Period of time, start the homing - if ( valveSelfTestState == VALVE_SELF_TEST_COMPLETE && ( valvesStatus[ valve ].hasHomingBeenRequested || isDoorClosed ) - && ( ! valvesStatus[ valve ].hasHomingFailed ) ) + // Period of time, start the homing process + if ( ( VALVE_SELF_TEST_COMPLETE == valveSelfTestState ) && ( ( TRUE == valvesStatus[ valve ].hasHomingBeenRequested ) || ( STATE_CLOSED == frontDoor ) ) + && ( FALSE == valvesStatus[ valve ].hasHomingFailed ) ) { // Get ready for the energized state valvesStatus[ valve ].homingEdgeDetectionCounter = 0; @@ -712,11 +725,19 @@ // Positions B and C will have an offset from the edge to make sure each time the valve will not hit the edge valvesStatus[ valve ].positions[ VALVE_POSITION_B_OPEN ] = positionB - INITIAL_EDGE_OFFSET_READ_COUNT; + valvesStatus[ valve ].positions[ VALVE_POSITION_C_CLOSE ] = currentPosition + INITIAL_EDGE_OFFSET_READ_COUNT; // Position A is the average of the Position B that was read last time and position C that was the target of this state valvesStatus[ valve ].positions[ VALVE_POSITION_A_INSERT_EJECT ] = ( valvesStatus[ valve ].positions[ VALVE_POSITION_B_OPEN ] - valvesStatus[ valve ].positions[ VALVE_POSITION_C_CLOSE ] ) / 2; +#ifndef _RELEASE_ + if ( ( getSoftwareConfigStatus( SW_CONFIG_ENABLE_VBA_SPECIAL_POSITION_C ) == SW_CONFIG_ENABLE_VALUE ) && ( VBA == valve ) ) + { + valvesStatus[ valve ].positions[ VALVE_POSITION_C_CLOSE ] = currentPosition + 3000; + } +#endif + // 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; @@ -734,9 +755,7 @@ valvesStatus[ valve ].hasValveBeenHomed = FALSE; valvesStatus[ valve ].hasHomingFailed = TRUE; state = VALVE_STATE_HOMING_NOT_STARTED; -#ifndef DISABLE_VALVE_ALARMS SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_VALVE_HOMING_FAILED, (U32)valve ); -#endif } else { @@ -773,16 +792,6 @@ { VALVE_STATE_T state = VALVE_STATE_IDLE; -#ifdef TST_3WAY_VALVES_ALWAYS_OPEN - static BOOL valvesOpenedForSarina[NUM_OF_VALVES] = { FALSE, FALSE, FALSE, FALSE }; - - if ( FALSE == valvesOpenedForSarina[ valve ] ) - { - valvesOpenedForSarina[ valve ] = TRUE; - setValvePosition( valve, VALVE_POSITION_B_OPEN ); - } -#endif - if ( TRUE == valvesStatus[ valve ].hasHomingBeenRequested ) { state = VALVE_STATE_HOMING_NOT_STARTED; @@ -856,14 +865,12 @@ state = VALVE_STATE_IDLE; } // Check if the valve's transition time has timed out - else if ( didTimeout( valvesStatus[ valve ].transitionStartTime, VALVE_TRANSITION_TIMEOUT_MS ) ) + else if ( TRUE == didTimeout( valvesStatus[ valve ].transitionStartTime, VALVE_TRANSITION_TIMEOUT_MS ) ) { - //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 ( ( TRUE == isAlarmActive( ALARM_ID_HD_VALVE_TRANSITION_TIMEOUT ) ) && ( VALVE_POSITION_C_CLOSE == valvesStatus[ valve ].commandedPosition ) ) { @@ -949,15 +956,12 @@ *************************************************************************/ static void execMonitorValves( void ) { -#ifndef DISABLE_3WAY_VALVES // Check if the valves are still enabled areValvesFunctional(); -#endif // Get the current position of the valves in counts and store them getAndMonitorValvesCurrentFPGAPosition(); -#ifndef DISABLE_3WAY_VALVES // Get the current in ADC and convert them to amps // Check whether any of the valves are over current convertAndMonitorValvesCurrent(); @@ -967,7 +971,6 @@ #ifdef DEBUG_ENABLED getValvesCurrentPWM(); #endif -#endif } /*********************************************************************//** @@ -982,8 +985,6 @@ static BOOL areValvesFunctional( void ) { BOOL result = TRUE; -#ifndef DISABLE_3WAY_VALVES -#ifndef DISABLE_VALVE_ALARMS VALVE_T valve; VALVE_MODE_T mode; @@ -997,42 +998,43 @@ // Depending on the control mode of the valve, different bit masks will be checked if ( mode == VALVE_CONTROL_MODE_ENABLE_PID ) { - if ( ! ( status & VALVE_CONTROL_STATUS_BITS[ valve ][ VALVE_CONTROL_STATUS_INITIALIZED ] ) ) + if ( FALSE == ( status & VALVE_CONTROL_STATUS_BITS[ valve ][ VALVE_CONTROL_STATUS_INITIALIZED ] ) ) { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_VALVE_NOT_FUNCTIONAL, (U32)valve, (U32)mode ); result = FALSE; } - if ( ! ( status & VALVE_CONTROL_STATUS_BITS[ valve ][ VALVE_CONTROL_STATUS_PID_ENABLED ] ) ) + if ( FALSE == ( status & VALVE_CONTROL_STATUS_BITS[ valve ][ VALVE_CONTROL_STATUS_PID_ENABLED ] ) ) { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_VALVE_NOT_FUNCTIONAL, (U32)valve, (U32)mode ); result = FALSE; } } else if ( mode == VALVE_CONTROL_MODE_ENABLE_BYPASS ) { - if ( ! ( status & VALVE_CONTROL_STATUS_BITS[ valve ][ VALVE_CONTROL_STATUS_BYPASS_ENABLED ] ) ) + if ( FALSE == ( status & VALVE_CONTROL_STATUS_BITS[ valve ][ VALVE_CONTROL_STATUS_BYPASS_ENABLED ] ) ) { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_VALVE_NOT_FUNCTIONAL, (U32)valve, (U32)mode ); result = FALSE; } } else if ( mode == VALVE_CONTROL_MODE_DISABLE_ALL ) { - if ( ! ( status & ( ~VALVE_CONTROL_STATUS_BITS[ valve ][ VALVE_CONTROL_STATUS_DISABLED ] ) ) ) + if ( FALSE == ( status & ( ~VALVE_CONTROL_STATUS_BITS[ valve ][ VALVE_CONTROL_STATUS_DISABLED ] ) ) ) { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_VALVE_NOT_FUNCTIONAL, (U32)valve, (U32)mode ); result = FALSE; } } +#ifndef _VECTORCAST_ + // The default cannot be reached in VectorCAST since the cases are run in a for loop // Invalid mode was selected else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_HD_VALVES_INVALID_CONTROL_MODE_SELECTED, (U32)valve ); result = FALSE; } - } #endif -#endif + } return result; } @@ -1122,9 +1124,7 @@ // Check if the current is over the threshold for the defined amount of time if ( valvesStatus[ valve ].overCurrentCounter > MAX_OVER_CURRENT_TIME_INTERVAL_COUNTER ) { -#ifndef DISABLE_VALVE_ALARMS SET_ALARM_WITH_2_F32_DATA( ALARM_ID_HD_VALVE_CURRENT_OUT_OF_RANGE, (F32)valve, current ); -#endif } // If the current is below the threshold again and the counter for the time is greater than else if ( ( current < VALVES_CURRENT_THRESHOLD_AMPS ) && ( valvesStatus[ valve ].overCurrentCounter > 0 ) ) @@ -1158,7 +1158,6 @@ valvesStatus[ VBA ].currentPositionInCounts.data = (S32)getFPGAValveBloodArterialPosition(); valvesStatus[ VBV ].currentPositionInCounts.data = (S32)getFPGAValveBloodVenousPosition(); -#ifndef DISABLE_3WAY_VALVES // Check the position of each valve for ( valve = VDI; valve < NUM_OF_VALVES; valve++ ) { @@ -1185,11 +1184,9 @@ valvesStatus[ valve ].positionOutOfRangeCounter++; } - if ( valvesStatus[ valve ].positionOutOfRangeCounter > MAX_POS_DEVIATION_TIME_INTERVAL_COUNTER ) + 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, currentPosition ); -#endif } else if ( abs( currentPosition - commandedPosition ) < MAX_DEVIATION_FROM_TARGET_IN_COUNTS && valvesStatus[ valve ].positionOutOfRangeCounter > 0 ) @@ -1198,7 +1195,6 @@ } } } -#endif } /*********************************************************************//** @@ -1516,7 +1512,6 @@ return result; } -#ifdef DEBUG_ENABLED /*********************************************************************//** * @brief * The testSetValvePWMOverride function overrides the valves PWM in bypass @@ -1677,6 +1672,4 @@ return result; } -#endif - /**@}*/