简体   繁体   中英

How to calculate speed and distance from acceleration in iOS

Okay, this is an odd one - and the question has been asked before in various guises and always closed out with the opinion that a) it won't be accurate and b) that it will get less accurate with time.

I understand this - but I'm doing an experiment to see whether that accuracy can be boosted at all by taking into account other evidential sources (for example, if a map has been plotted in advance then the direction of travel from the compass combined with the route could provide another evidence source).

The problem is that my code is clearly rubbish - so I'd welcome your opinion. I suspect that this might be a brown paper bag error!

My ViewController.h looks like this:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIAccelerometerDelegate> {

    UIAccelerometer *accelerometer;

    long last_speed;
    long distance_travelled;

    long lastAccel;
    long long lastTime;

    IBOutlet UITextField* speedView;
    IBOutlet UITextField* distanceView;
}

@end

And my ViewController.m looks like this:

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    lastAccel = 0;
    last_speed = 0;
    distance_travelled = 0;
    lastTime = (long)(NSTimeInterval)([NSDate.date timeIntervalSince1970] * 1000.0);

    accelerometer = UIAccelerometer.sharedAccelerometer;
    accelerometer.updateInterval = 0.1;
    accelerometer.delegate = self;
}

- (void)accelerometer:(UIAccelerometer *)meter
        didAccelerate:(UIAcceleration *)acceleration
{
    long long currentTime = (long)(NSTimeInterval)([NSDate.date timeIntervalSince1970] * 1000.0);
    long long deltaTime = currentTime - lastTime;
    lastTime = currentTime;

    long accel_x = acceleration.x;
    long accel_y = acceleration.y;
    long accel_z = acceleration.z;

    long integrated_acceleration = sqrtl(accel_x*accel_x + accel_y*accel_y + accel_z*accel_z);

    long average_acceleration = (lastAccel + integrated_acceleration) / 2;

    long speed = last_speed + (average_acceleration * deltaTime);
    long average_speed = (speed + last_speed) / 2;
    distance_travelled = distance_travelled + (average_speed * deltaTime);

    last_speed = speed;
    lastAccel = integrated_acceleration;

    [speedView setText:[NSString stringWithFormat:@"%ld", speed]];
    [distanceView setText:[NSString stringWithFormat:@"%ld", distance_travelled]];
}

@end

When the code is run, the speed and the distance keep going up continually, and deceleration (me slowing down) is never taken into account - so even when I stop, speed and distance keep going up.

Even by the standards of 'this will be a bit inaccurate' that's taking it too far!

All thoughts and opinions gratefully received (well almost - I know that it won't be accurate!)

You can't calculate Speed using UIAccelerometer.

Please find the link for more clarification : How to calculate speed using accelerometer in ios

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