# Calculating Velocity using Linear acceleration

I'm attempting to calculate velocity on the z axis with sensor fusion using with Linear velocity in IMUPlus mode.

Basically I'm moving the device with my hand left to right in the z index direction.

i'm using the following to calculate velocity:

var lastTime : Double = 0; // the last timestap
var firstRun = true; // flag to wait until last timestamp has been populated
var velocity : Decimal = 0.0; //current velocity

if let obj = obj {
let current : Double = obj.timestamp.timeIntervalSince1970;
let currentNumber = Decimal(current);
let lastNumber = Decimal(lastTime);
let z = obj.z;

if(!firstRun){
let currentNumber = Decimal(current);
let lastNumber = Decimal(lastTime);
let timeDiff = currentNumber - lastNumber;

let accelMeterSquaredDecimal = Decimal(Double(z) * 9.80665);  // gravity to meters per second second
let velocityChange = accelMeterSquaredDecimal*timeDiff;
velocity = velocity + velocityChange;
}

if(firstRun == true ){
lastTime = obj.timestamp.timeIntervalSince1970;
firstRun = false;
}
}
}

A couple issues with this approach.

1. Linear acceleration at a stand still is never zero, so velocity will constantly increase or decrease ( depending on the output) even at a standstill. Would using the correction data help in the aspect at all?
2. When coming to a stop I would expect the inverse acceleration to happen and velocity to become zero again but it does not zero out, perhaps due to number 1.

The end game is to determine the speed at any given time.

Thanks,
Mark

• Hi Mark,

In general, there are couple of things to keep in mind when using accelerometers to calculate a "linear velocity":

1. Consider using a high pass filter to filter out the constant force of gravity and other low frequency noise, this will help you "zero out" the sensor as much as possible when it's not moving.

2. Use offsets to calibrate the sensors in each orientation, this will help you get closer to the expected magnitude value and works even when the accelerometer is in constant motion.

3. Consider using a low pass filter to filter out high frequency noise, which can affect your velocity outputs significantly if your motions are fast.

Thanks,
Yu
This discussion has been closed.