简体   繁体   中英

Get device motion direction using accelerometer

I'm trying to detect the direction of motion of an android device using the accelerometer. I've removed the gravity components from the accelerometer output values and deducted the angles.

So I am able to get the direction of motion but I also get arbitrary angles momentarily. Like when I stop moving the device.

Any suggestions how I could filter those angles out?

EDIT: I've somewhat been able to solve this by taking the mean of the current and past values. Another problem that persists is that initially for a few moments, the accelerometer reports values in the opposite direction of motion.

That's a typical trouble with accelerometers... Initially there's no any solution because of the inertia, etc... But You can try using some kind of "integer controller".

Another possible solution is detecting abrupt changes on the acceleration and interpret them as changes of direction, I mean, for example if you have the acceleration on the X edge (Ax).

while(1){
Ax = readAx();
if(changeSign(Ax)){  //From + to - or - to +.
      //Do what ever you need, for example if sign is changed and keep on it then is that the mobilephone is been moved in the other direction. Else if it's acceleration is close to 0 it means that the device has stopped
}else{
     //The device keep moving on the same direction.
}
}

Feel free to be creative. There are many ways to manage a solution depends on your target.

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM