Index: firmware/App/Controllers/Valves.c =================================================================== diff -u -re5d1d67106a93a6cd1b5692b586625d715732e2f -rf0c52f6adff7b61132953890a74f1c462b31eedf --- firmware/App/Controllers/Valves.c (.../Valves.c) (revision e5d1d67106a93a6cd1b5692b586625d715732e2f) +++ firmware/App/Controllers/Valves.c (.../Valves.c) (revision f0c52f6adff7b61132953890a74f1c462b31eedf) @@ -169,14 +169,14 @@ { VALVE_POSITION_T commandedPosition; ///< Valve commanded position enum VALVE_POSITION_T currentPosition; ///< Valve current position enum - S16 currentPositionInCounts; ///< Valve current position in counts + 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 U32 transitionStartTime; ///< Valve transition start time S16 positions[ NUM_OF_VALVE_POSITIONS ]; ///< Valve positions array (Pos A, Pos B, Pos C) VALVE_MODE_T controlMode; ///< Valve control mode (PID, bypass, disable) - F32 current; ///< Valve current + OVERRIDE_F32_T current; ///< Valve current U32 overCurrentCounter; ///< Valve over current counter U32 positionOutOfRangeCounter; ///< Valve position out of range counter U32 dataPublishCounter; ///< Valve data publish counter @@ -243,10 +243,11 @@ static BOOL areValvesFunctional( void ); static void setFPGAValveSetPoint( VALVE_T valve, S16 position, BOOL enableCurrentRelaxation ); static void convertAndMonitorValvesCurrent( void ); -static void getAndMonitorValvesCurrentPosition( void ); +static void getAndMonitorValvesCurrentFPGAPosition( void ); static U32 getPublishValvesDataInterval( void ); static void publishValvesData( VALVE_T valve ); static void setValveNextStep( VALVE_T valve, U32 stepChange ); +static S16 getValvePositionCounts( VALVE_T valve ); // These functions will be used in debug only mode #ifdef DEBUG_ENABLED @@ -280,6 +281,10 @@ 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 ].current.ovInitData = 0.0; + valvesStatus[ valve ].current.override = 0; } // Close air trap valve @@ -367,6 +372,26 @@ /*********************************************************************//** * @brief + * The getValveCurrent function returns the current of a valve. + * @details Inputs: none + * @details Outputs: valvesStatus + * @param valve that the current is requested + * @return returns the current of the valve in float + *************************************************************************/ +F32 getValveCurrent( VALVE_T valve ) +{ + F32 current = valvesStatus[ valve ].current.data; + + if ( OVERRIDE_KEY == valvesStatus[ valve ].current.override ) + { + current = valvesStatus[ valve ].current.ovData; + } + + return current; +} + +/*********************************************************************//** + * @brief * The setValveBloodTrap function set the blood trap valve to open or close * position. * @details Inputs: valveAirTrapStatus @@ -599,7 +624,7 @@ { // Get ready for the energized state valvesStatus[ valve ].homingEdgeDetectionCounter = 0; - valvesStatus[ valve ].targetPositionInCounts = valvesStatus[ valve ].currentPositionInCounts + HOMING_STEP_CHANGE_IN_COUNTS; + valvesStatus[ valve ].targetPositionInCounts = getValvePositionCounts( valve ) + HOMING_STEP_CHANGE_IN_COUNTS; valvesStatus[ valve ].hasValveBeenHomed = FALSE; valvesStatus[ valve ].currentPosition = VALVE_POSITION_NOT_IN_POSITION; setFPGAValveSetPoint( valve, valvesStatus[ valve ].targetPositionInCounts, FALSE ); @@ -622,7 +647,7 @@ { VALVE_STATE_T state = VALVE_STATE_HOMING_FIND_ENERGIZED_EDGE; - S16 currentPosition = valvesStatus[ valve ].currentPositionInCounts; + S16 currentPosition = getValvePositionCounts( valve ); S16 targetPosition = valvesStatus[ valve ].targetPositionInCounts; S16 deltaPosition = targetPosition - currentPosition; @@ -643,7 +668,6 @@ { valvesStatus[ valve ].homingEdgeDetectionCounter++; } - } // Reached to target, go for the next target else @@ -669,7 +693,7 @@ { VALVE_STATE_T state = VALVE_STATE_HOMING_FIND_DEENERGIZED_EDGE; - S16 currentPosition = valvesStatus[ valve ].currentPositionInCounts; + S16 currentPosition = getValvePositionCounts( valve ); S16 targetPosition = valvesStatus[ valve ].targetPositionInCounts; S16 deltaPosition = currentPosition - targetPosition; @@ -815,7 +839,7 @@ // Get the corresponding counts of the positions S16 commandedPosition = valvesStatus[ valve ].positions[ commandedPositionEnum ]; - S16 currentPosition = valvesStatus[ valve ].currentPositionInCounts; + S16 currentPosition = getValvePositionCounts( valve ); S16 targetPosition = valvesStatus[ valve ].targetPositionInCounts; // Check if the valve is within range of the final commanded position. @@ -941,7 +965,7 @@ #endif // Get the current position of the valves in counts and store them - getAndMonitorValvesCurrentPosition(); + getAndMonitorValvesCurrentFPGAPosition(); #ifndef DISABLE_3WAY_VALVES // Get the current in ADC and convert them to amps @@ -1084,22 +1108,22 @@ U16 currentInADC = 0; currentInADC = getFPGAValveDialyzerInletCurrentCounts(); - valvesStatus[ VDI ].current = ( (F32)currentInADC / ADC_TO_CURRENT_CONVERSION_CONSTANT ) - 1; + valvesStatus[ VDI ].current.data = ( (F32)currentInADC / ADC_TO_CURRENT_CONVERSION_CONSTANT ) - 1; currentInADC = getFPGAValveDialyzerOutletCurrentCounts(); - valvesStatus[ VDO ].current = ( (F32)currentInADC / ADC_TO_CURRENT_CONVERSION_CONSTANT ) - 1; + valvesStatus[ VDO ].current.data = ( (F32)currentInADC / ADC_TO_CURRENT_CONVERSION_CONSTANT ) - 1; currentInADC = getFPGAValveBloodArterialCurrentCounts(); - valvesStatus[ VBA ].current = ( (F32)currentInADC / ADC_TO_CURRENT_CONVERSION_CONSTANT ) - 1; + valvesStatus[ VBA ].current.data = ( (F32)currentInADC / ADC_TO_CURRENT_CONVERSION_CONSTANT ) - 1; currentInADC = getFPGAValveBloodVenousCurrentCounts(); - valvesStatus[ VBV ].current = ( (F32)currentInADC / ADC_TO_CURRENT_CONVERSION_CONSTANT ) - 1; + valvesStatus[ VBV ].current.data = ( (F32)currentInADC / ADC_TO_CURRENT_CONVERSION_CONSTANT ) - 1; // Check the current of all the valves for ( valve = VDI; valve < NUM_OF_VALVES; valve++) { // Absolute value is used to cover both positive and negative values - F32 current = fabs( valvesStatus[ valve ].current ); + F32 current = fabs( getValveCurrent( valve ) ); if ( current > VALVES_CURRENT_THRESHOLD_AMPS ) { @@ -1113,8 +1137,7 @@ #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 ) + else if ( ( current < VALVES_CURRENT_THRESHOLD_AMPS ) && ( valvesStatus[ valve ].overCurrentCounter > 0 ) ) { valvesStatus[ valve ].overCurrentCounter = 0; } @@ -1123,27 +1146,27 @@ /*********************************************************************//** * @brief - * The getAndMonitorValvesCurrentPosition function gets the current position \n - * of the valves and stores them into the structure of each valve. The function \n - * checks whether any of the valves have deviated from their current position \n - * if they are in idle state. If any of the valves have deviated, the function \n - * raises an alarm. + * The getAndMonitorValvesCurrentFPGAPosition function gets the current + * position of the valves from FPGA and stores them into the structure of + * each valve.The function checks whether any of the valves have deviated + * from their current position if they are in idle state. If any of the + * valves have deviated, the function raises an alarm. * @details Inputs: valvesStatus * @details Outputs: valvesStatus * @return none *************************************************************************/ -static void getAndMonitorValvesCurrentPosition( void ) +static void getAndMonitorValvesCurrentFPGAPosition( void ) { VALVE_T valve; S16 currentPostion = 0; S16 commandedPoistion = 0; VALVE_POSITION_T commandedPositionEnum; // Get the position of the valves and update the structure of each valve - valvesStatus[ VDI ].currentPositionInCounts = getFPGAValveDialyzerInletPosition(); - valvesStatus[ VDO ].currentPositionInCounts = getFPGAValveDialyzerOutletPosition(); - valvesStatus[ VBA ].currentPositionInCounts = getFPGAValveBloodArterialPosition(); - valvesStatus[ VBV ].currentPositionInCounts = getFPGAValveBloodVenousPosition(); + valvesStatus[ VDI ].currentPositionInCounts.data = (S32)getFPGAValveDialyzerInletPosition(); + valvesStatus[ VDO ].currentPositionInCounts.data = (S32)getFPGAValveDialyzerOutletPosition(); + valvesStatus[ VBA ].currentPositionInCounts.data = (S32)getFPGAValveBloodArterialPosition(); + valvesStatus[ VBV ].currentPositionInCounts.data = (S32)getFPGAValveBloodVenousPosition(); #ifndef DISABLE_3WAY_VALVES // Check the position of each valve @@ -1154,7 +1177,7 @@ { U32 maxDeviation = MAX_DEVIATION_FROM_TARGET_IN_COUNTS; - currentPostion = valvesStatus[ valve ].currentPositionInCounts; + currentPostion = getValvePositionCounts( valve ); commandedPositionEnum = valvesStatus[ valve ].commandedPosition; commandedPoistion = valvesStatus[ valve ].positions[ commandedPositionEnum ]; @@ -1226,9 +1249,9 @@ valveData.valveID = (U32)valve; valveData.state = (U32)valvesStatus[ valve ].execState; valveData.currentPosID = (U32)valvesStatus[ valve ].currentPosition; - valveData.currentPos = valvesStatus[ valve ].currentPositionInCounts; + valveData.currentPos = getValvePositionCounts( valve ); valveData.nextPos = valvesStatus[ valve ].targetPositionInCounts; - valveData.current = valvesStatus[ valve ].current; + valveData.current = getValveCurrent( valve ); valveData.posC = valvesStatus[ valve ].positions[ VALVE_POSITION_C_CLOSE ]; valveData.posA = valvesStatus[ valve ].positions[ VALVE_POSITION_A_INSERT_EJECT ]; valveData.posB = valvesStatus[ valve ].positions[ VALVE_POSITION_B_OPEN ]; @@ -1255,7 +1278,7 @@ S16 nextStep = 0; VALVE_POSITION_T currentPositionEnum = valvesStatus[ valve ].currentPosition; VALVE_POSITION_T commandedPositionEnum = valvesStatus[ valve ].commandedPosition; - S16 currentPosition = valvesStatus[ valve ].currentPositionInCounts; + S16 currentPosition = getValvePositionCounts( valve ); S16 commandedPosition = valvesStatus[ valve ].positions[ commandedPositionEnum ]; // If the next step is less than the specified step change, set that @@ -1281,22 +1304,22 @@ // Subtract the defined number of steps for the next transition if ( commandedPositionEnum == VALVE_POSITION_B_OPEN ) { - valvesStatus[ valve ].targetPositionInCounts = valvesStatus[ valve ].currentPositionInCounts + nextStep; + valvesStatus[ valve ].targetPositionInCounts = getValvePositionCounts( valve ) + nextStep; } if ( commandedPositionEnum == VALVE_POSITION_C_CLOSE ) { - valvesStatus[ valve ].targetPositionInCounts = valvesStatus[ valve ].currentPositionInCounts - nextStep; + valvesStatus[ valve ].targetPositionInCounts = getValvePositionCounts( valve ) - nextStep; } break; case VALVE_POSITION_B_OPEN: // If the valve is currently in position B, subtract the defined number of steps for the next transition - valvesStatus[ valve ].targetPositionInCounts = valvesStatus[ valve ].currentPositionInCounts - nextStep; + valvesStatus[ valve ].targetPositionInCounts = getValvePositionCounts( valve ) - nextStep; break; case VALVE_POSITION_C_CLOSE: // If the valve is currently in position C, add the defined number of steps for the next transition - valvesStatus[ valve ].targetPositionInCounts = valvesStatus[ valve ].currentPositionInCounts + nextStep; + valvesStatus[ valve ].targetPositionInCounts = getValvePositionCounts( valve ) + nextStep; break; default: @@ -1309,6 +1332,27 @@ setFPGAValveSetPoint( valve, valvesStatus[ valve ].targetPositionInCounts, FALSE ); } +/*********************************************************************//** + * @brief + * The getValvePositionCounts function returns a valve's position in + * counts. + * @details Inputs: valvesStatus + * @details Outputs: none + * @param valve that its position in counts is requested + * @return current position of the valve in counts (S16) + *************************************************************************/ +static S16 getValvePositionCounts( VALVE_T valve ) +{ + S16 position = (S16)valvesStatus[ valve ].currentPositionInCounts.data; + + if ( OVERRIDE_KEY == valvesStatus[ valve ].currentPositionInCounts.override ) + { + position = (S16)valvesStatus[ valve ].currentPositionInCounts.ovData; + } + + return position; +} + #ifdef DEBUG_ENABLED /*********************************************************************//** * @brief @@ -1464,7 +1508,7 @@ { BOOL result = FALSE; - if ( TRUE == isTestingActivated() && valve < NUM_OF_VALVES ) + if ( ( TRUE == isTestingActivated() ) && ( valve < NUM_OF_VALVES ) ) { // Get the current position of the valve as the initial position valvesPositionOverride[ valve ].ovInitData = (U32)valvesStatus[ valve ].currentPosition; @@ -1483,14 +1527,14 @@ * of the valves publish interval. * @details Inputs: valvesDataPublishInterval * @details Outputs: valvesDataPublishInterval - * @param valve that its data publish will be overridden + * @param valve that its position override will be reset * @return TRUE if override reset successful, FALSE if not *************************************************************************/ BOOL testResetValvesPositionOverride( U32 valve ) { BOOL result = FALSE; - if ( TRUE == isTestingActivated() && valve < NUM_OF_VALVES ) + if ( ( TRUE == isTestingActivated() ) && ( valve < NUM_OF_VALVES ) ) { valvesPositionOverride[ valve ].override = OVERRIDE_RESET; valvesPositionOverride[ valve ].ovData = valvesPositionOverride[ valve ].ovInitData; @@ -1518,13 +1562,13 @@ { BOOL result = FALSE; - if ( TRUE == isTestingActivated() && valve < NUM_OF_VALVES ) + if ( ( TRUE == isTestingActivated() ) && ( valve < NUM_OF_VALVES ) ) { BOOL homingStatus = valvesStatus[ valve ].hasValveBeenHomed; // The PWM should be in range and also the valve direction should be legal and the valve must have been homed // Prior to running this command - if ( pwm <= VALVE_MAX_ALLOWED_PWM_PERCENT && direction < NUM_OF_VALVE_DIRECTION && homingStatus ) + if ( ( pwm <= VALVE_MAX_ALLOWED_PWM_PERCENT ) && ( direction < NUM_OF_VALVE_DIRECTION ) && ( TRUE == homingStatus ) ) { valvesStatus[ (VALVE_T)valve ].bypassModeStatus.commandedPWMInPercent = (U16)pwm; valvesStatus[ (VALVE_T)valve ].bypassModeStatus.direction = (VALVE_DIRECTION_T)direction; @@ -1533,7 +1577,6 @@ valvesStatus[ (VALVE_T)valve ].bypassModeStatus.hasBypassModeBeenRequeseted = TRUE; // This flag is used to check if a change in PWM has been requested so the new PWM is processed and set valvesStatus[ (VALVE_T)valve ].bypassModeStatus.hasChangeBeenRequested = TRUE; - result = TRUE; } } @@ -1547,25 +1590,122 @@ * control mode. * @details Inputs: valvesStatus * @details Outputs: valvesStatus - * @param valve that its data publish will be overridden + * @param valve that its PWM override will be reset * @return TRUE if override reset successful, FALSE if not *************************************************************************/ BOOL testResetValvePWMOverride( U32 valve ) { BOOL result = FALSE; // Check if the valve is in the range of the valves available - if ( TRUE == isTestingActivated() && valve < NUM_OF_VALVES ) + if ( ( TRUE == isTestingActivated() ) && ( valve < NUM_OF_VALVES ) ) { // Turn off the bypass mode request and the change request valvesStatus[ (VALVE_T)valve ].bypassModeStatus.hasBypassModeBeenRequeseted = FALSE; valvesStatus[ (VALVE_T)valve ].bypassModeStatus.hasChangeBeenRequested = FALSE; + result = TRUE; + } + return result; +} + +/*********************************************************************//** + * @brief + * The testSetValvesCurrentOverride function overrides the valves' current. + * @details Inputs: valvesStatus + * @details Outputs: valvesStatus + * @param valve to override its current + * @param current of the valve that will be overridden + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetValvesCurrentOverride( U32 valve, F32 current ) +{ + BOOL result = FALSE; + + // Check if the valve is in the range of the valves available + if ( ( TRUE == isTestingActivated() ) && ( valve < NUM_OF_VALVES ) ) + { + valvesStatus[ (VALVE_T)valve ].current.override = OVERRIDE_KEY; + valvesStatus[ (VALVE_T)valve ].current.ovData = current; + valvesStatus[ (VALVE_T)valve ].current.ovInitData = valvesStatus[ (VALVE_T)valve ].current.data; result = TRUE; } return result; } + +/*********************************************************************//** + * @brief + * The testResetValvesCurrentOverride function resets the override + * of the valves' current. + * @details Inputs: valvesStatus + * @details Outputs: valvesStatus + * @param valve that its current override will be reset + * @return TRUE if override reset successful, FALSE if not + *************************************************************************/ +BOOL testResetValvesCurrentOverride( U32 valve ) +{ + BOOL result = FALSE; + + if ( ( TRUE == isTestingActivated() ) && ( valve < NUM_OF_VALVES ) ) + { + valvesStatus[ (VALVE_T)valve ].current.override = OVERRIDE_RESET; + valvesStatus[ (VALVE_T)valve ].current.ovData = valvesStatus[ (VALVE_T)valve ].current.ovInitData; + result = TRUE; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testSetValvesPositionCountOverride function overrides the valves + * position in counts. + * @details Inputs: valvesPositionOverride, valvesStatus + * @details Outputs: valvesPositionOverride + * @param valve to override its position + * @param position of the valve that will be overridden + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetValvesPositionCountOverride( U32 valve, U32 count ) +{ + BOOL result = FALSE; + + // Check if the valve is in the range of the valves available + if ( ( TRUE == isTestingActivated() ) && ( valve < NUM_OF_VALVES ) ) + { + valvesStatus[ (VALVE_T)valve ].currentPositionInCounts.override = OVERRIDE_KEY; + valvesStatus[ (VALVE_T)valve ].currentPositionInCounts.ovData = (S32)count; + valvesStatus[ (VALVE_T)valve ].currentPositionInCounts.ovInitData = valvesStatus[ (VALVE_T)valve ].currentPositionInCounts.data; + result = TRUE; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetValvesPositionCountOverride function resets the override + * of the valves' position in counts. + * @details Inputs: valvesStatus + * @details Outputs: valvesStatus + * @param valve that its position count override will be reset + * @return TRUE if override reset successful, FALSE if not + *************************************************************************/ +BOOL testResetValvesPositionCountOverride( U32 valve ) +{ + BOOL result = FALSE; + + if ( ( TRUE == isTestingActivated() ) && ( valve < NUM_OF_VALVES ) ) + { + valvesStatus[ (VALVE_T)valve ].currentPositionInCounts.override = OVERRIDE_RESET; + valvesStatus[ (VALVE_T)valve ].currentPositionInCounts.ovData = valvesStatus[ (VALVE_T)valve ].currentPositionInCounts.ovInitData; + result = TRUE; + } + + return result; +} + #endif /**@}*/