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
task = device.sensorFusion!.linearAcceleration.startNotificationsAsync { (obj, error) in
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
This discussion has been closed.
Comments