# Track the velocity(or average velocity) of a basketball being dribbled

I'm trying to get the velocity of a dribbled basketball while someone is wearing the sensor on his or her wrist.

I'm a noob at best and i'm lost. I purchased a  metawear c. For a while now I've been trying to figure out a way to extract the velocity from the stream of data. To some extent I understand that the data received will show error. I have tried the V = V0 + (forms of acceleration * time), the data seems to be accumulating.

I have 3 questions.
1. Am I lacking an additionally sensor which would make my task astronomically easier?
2. Is there a way I can  get a rough estimate on an algorithm (example: Have a set time step like 0.1 sec and taking the average of the data between the time step) to get closer to an actual reading? or Kalman filter?
3. Am I asking the wrong questions because of a lack of knowledge?

## Comments

• All inertial measurement units (to my knowledge) measure quantities in the reference frame of the sensor body. This means if the orientation of the sensor changes over time, so will the reference frame in which it records acceleration readings.

I'm guessing that you want velocity measurements in some fixed ('world') reference frame. One way to do this is to track the orientation of the device over time using angular velocity measurements from the gyroscope, then apply a change of basis transformation on the accelerometer samples to convert them from body frame to world frame.

After that you also need to subtract the acceleration due to gravity in the world frame -- if you choose the vertical axis of the world frame to be aligned with the direction of Earth's gravitational field (i.e. 'down'), this amounts to adding or subtracting 1g from the z axis depending on the reference frame orientation.
• @jdjones "I'm guessing that you want velocity measurements in some fixed ('world') reference frame. One way to do this is to track the orientation of the device over time using angular velocity measurements from the gyroscope, then apply a change of basis transformation on the accelerometer samples to convert them from body frame to world frame."

I need a little direction because I'm a little lost. To be more specific, should I make a complementary filter to smooth the data of the accel and the gyro? After doing so should I use Euler angles or quaternions to find the change in orientation? Is that more than I need for my app?
• Yes, you will need a complimentary filter to remove the noise from both the gyro and accelerometer data.  You can check out this post from our blog and the accompanying links to get an introduction to sensor fusion.

Change in orientation can be done with only accelerometer data if you're simply looking to determine if the sensor is in portrait/landscape or face up/down orientation.  If you are looking for something orientation in 3D space, you will need euler angles or quaternions.
• edited January 2017
I now have the meta motion R with sensor fusion. I'm taking the linear acceleration, then computationally integrating it. I'm not getting the numbers that I should.
• What numbers are the numbers you are getting vs. what is expected?  What code are you using to post process the data?
• This double array holds the XYZ values of my accelerometer. This is linear-acceleration numbers, an algorithm has accounted and taken out the gravity factor.

accelCompoDoub = parseDouble(accelCompoString);
accelCompoDoub = parseDouble(accelCompoString);
accelCompoDoub = parseDouble(accelCompoString);
My app will track elapsed time and will also need deltaT. DeltaT will be used to computationally integrate acceleration.

currentTime = System.nanoTime();
if (startTime == 0.0 && previousTime == 0.0){
startTime = currentTime;
previousTime = currentTime;
}else{
deltaT = currentTime - previousTime;
}

runningTime = currentTime - startTime;

runningTime = TimeUnit.NANOSECONDS.toMillis(runningTime);
runningTimeDouble = runningTime/1000.0;
Converted from nano to micro and not to milli. The reason being was that my data encountered a lot of 0.0's.

deltaT = TimeUnit.NANOSECONDS.toMicros(deltaT);
deltaTDoub = deltaT/1000000.0;
previousTime = currentTime;
This is where velocity = velocity + (accel) * (change in time)

velocityComp += accelCompoDoub*deltaTDoub;
velocityComp += accelCompoDoub*deltaTDoub;
velocityComp += accelCompoDoub*deltaTDoub;
Log.i("Velocity", Arrays.toString(velocityComp));
My results as I'm moving the sensor up at a rapid speed on the z axis. This isn't right and I've tried to correct it. Thanks for helping me out. Let me know if additional info is needed.

Results from calculations.

I/Velocity: [-0.063722465, -0.132597938, 0.06367645599999999]
I/Velocity: [-7.25024E-4, -0.0018097799999999998, 5.87136E-4]
I/Velocity: [-8.90561E-4, -0.0026836670000000003, 3.80492E-4]
I/Velocity: [-7.223840000000001E-4, -0.002324192, -8.8788E-5]
I/Velocity: [-0.0014603620000000002, -0.0046326660000000006, -0.0013441560000000002]
I/Velocity: [-0.056317518, -0.161929746, -0.10376649000000002]
I/Velocity: [-5.45706E-4, -0.001396428, -0.001515708]
I/Velocity: [-0.00108585, -0.00286045, -0.0047196]
I/Velocity: [-0.001350592, -0.00551372, -0.010952008000000001]
I/Velocity: [-0.027746648999999998, -0.12593060399999997, -0.285394104]

P.S: I also tried to limit the sample so that the deltaT is approximately .01 seconds.

• @eric the comment above was the one that i posted on StackOverflow
• For discrete sampling, you cannot simply sum the acceleration as you would if you had continuous sampling.  Try using the formula

a(n) = v(n) - v(n - 1) / deltaT
• @eric Wouldn't  a(n) = (v(n) -v(n-1))/deltaT and a(n)*deltaT + v(n-1) be the same? The output was incorrect. I know its a summation, the data only goes pretty much in one direction. While shaking the sensor the change didn't occur like I thought it would. i'm trying to see if a trapezoid summation would work.

• The code you provided does not compute velocity using the aforementioned equation.
• I'm new to all this so thanks for helping and having patience with me. @eric I'm trying to interpret the formula.
So i'm looking at the formula
"a(n) = v(n) - v(n - 1) / deltaT"
Your saying that i have to take V(n) current velocity and subtract the previous velocity  all divided by the change in time?

If I multiply the current acceleration component by the change in time wouldn't i get the change in velocity for that component?
a(n)*deltaT = v(n) - v(n-1)

My code with the suggestion you made. Please let me know if I got what you were saying correct
`vel =  ac  * deltaTDoub;velTotal = velTotal + vel;`

• My apologies.  you are correct in that your code did compute velocity with the average velocity equation.  Some more poking around yielded these threads on SO:
http://stackoverflow.com/questions/12926459/calculating-distance-using-linear-acceleration-android
This discussion has been closed.