Index: firmware/App/Controllers/BloodFlow.c =================================================================== diff -u -rf8feb10a7e19e17148e4ce8b247316c9772d1753 -r5e77f78c5dee9dfb441bd5d2053f7f4ac50dc619 --- firmware/App/Controllers/BloodFlow.c (.../BloodFlow.c) (revision f8feb10a7e19e17148e4ce8b247316c9772d1753) +++ firmware/App/Controllers/BloodFlow.c (.../BloodFlow.c) (revision 5e77f78c5dee9dfb441bd5d2053f7f4ac50dc619) @@ -55,7 +55,8 @@ #define BP_HOME_RATE 100 ///< target pump speed (in estimate mL/min) for homing. #define BP_HOME_TIMEOUT_MS 10000 ///< maximum time allowed for homing to complete (in ms). -#define BP_SPEED_CALC_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< interval (ms/task time) at which the blood pump speed is calculated. +#define BP_SPEED_CALC_INTERVAL ( 40 / TASK_PRIORITY_INTERVAL ) ///< interval (ms/task time) at which the blood pump speed is calculated. +#define BP_SPEED_CALC_BUFFER__LEN 25 ///< number of hall sensor counts kept in buffer to hold last 1 second of count data. #define BP_HALL_EDGE_COUNTS_PER_REV 48 ///< number of hall sensor edge counts per motor revolution. #define BP_MAX_ROTOR_SPEED_RPM 100.0 ///< maximum rotor speed allowed for blood pump. @@ -150,9 +151,10 @@ static BOOL bpStopAtHomePosition = FALSE; ///< stop blood pump at next home position static U32 bpHomeStartTime = 0; ///< when did blood pump home command begin? (in ms) -static U16 bpLastMotorHallSensorCount = 0; ///< last hall sensor count for the blood pump motor +static U16 bpLastMotorHallSensorCounts[ BP_SPEED_CALC_BUFFER__LEN ]; ///< last hall sensor count for the blood pump motor +static U32 bpMotorSpeedCalcIdx = 0; ///< index into 1 second buffer of motor speed hall sensor counts static MOTOR_DIR_T bpMotorDirectionFromHallSensors = MOTOR_DIR_FORWARD; ///< pump direction according to hall sensor count -static U32 bpMotorSpeedCalcTimerCtr = 0; ///< counter determines interval for calculating blood pump motor speed from hall sensor count. +static U32 bpMotorSpeedCalcTimerCtr = 0; ///< counter determines interval for calculating blood pump motor speed from hall sensor count. static U32 errorBloodFlowVsMotorSpeedPersistTimerCtr = 0; ///< persistence timer counter for flow vs. motor speed error condition. static U32 errorBloodMotorOffPersistTimerCtr = 0; ///< persistence timer counter for motor off check error condition. @@ -199,15 +201,22 @@ * @return none *************************************************************************/ void initBloodFlow( void ) -{ - bpLastMotorHallSensorCount = getFPGABloodPumpHallSensorCount(); +{ + U32 i; stopBloodPump(); setBloodPumpDirection( MOTOR_DIR_FORWARD ); // zero rolling flow average buffer resetBloodFlowMovingAverage(); - + + // zero motor hall sensors counts buffer + bpMotorSpeedCalcIdx = 0; + for ( i = 0; i < BP_SPEED_CALC_BUFFER__LEN; i++ ) + { + bpLastMotorHallSensorCounts[ i ] = 0; + } + // initialize blood flow PI controller initializePIController( PI_CONTROLLER_ID_BLOOD_FLOW, MIN_BLOOD_PUMP_PWM_DUTY_CYCLE, BP_P_COEFFICIENT, BP_I_COEFFICIENT, @@ -894,8 +903,9 @@ { if ( ++bpMotorSpeedCalcTimerCtr >= BP_SPEED_CALC_INTERVAL ) { - U16 bpMotorHallSensorCount = getFPGABloodPumpHallSensorCount(); - U16 incDelta = ( bpMotorHallSensorCount >= bpLastMotorHallSensorCount ? bpMotorHallSensorCount - bpLastMotorHallSensorCount : ( HEX_64_K - bpLastMotorHallSensorCount ) + bpMotorHallSensorCount ); + U16 bpMotorHallSensorCount = getFPGABloodPumpHallSensorCount(); + U32 nextIdx = INC_WRAP( bpMotorSpeedCalcIdx, 0, BP_SPEED_CALC_BUFFER__LEN - 1 ); + U16 incDelta = ( bpMotorHallSensorCount >= bpLastMotorHallSensorCounts[ nextIdx ] ? bpMotorHallSensorCount - bpLastMotorHallSensorCounts[ nextIdx ] : ( HEX_64_K - bpLastMotorHallSensorCounts[ nextIdx ] ) + bpMotorHallSensorCount ); U16 decDelta = HEX_64_K - incDelta; U16 delta; @@ -914,7 +924,8 @@ } // update last count for next time - bpLastMotorHallSensorCount = bpMotorHallSensorCount; + bpLastMotorHallSensorCounts[ nextIdx ] = bpMotorHallSensorCount; + bpMotorSpeedCalcIdx = nextIdx; bpMotorSpeedCalcTimerCtr = 0; } }