Measuring velocity via iPhone SDK - iphone

I need to implement a native iPhone app to measure the velocity of the phone (basically a speedometer). I know that you can do so via the CoreLocation API fairly easily, but I am concerned about battery consumption since this is to be a real-time measurement that could be used for up to a couple of hours at a time. My understanding is that while you are actively monitoring for events from the LocationManager (even though I don't actually care about GPS location) it is battery-intensive.
The other obvious option to explore would be using the accelerometers to try and calculate speed, but there is nothing in the API to help you do so. Based on my research, it should be possible to do this, but seems extremely complicated and error-prone. Translating from acceleration to velocity can be tricky to begin with, plus the iPhone accelerometer data can be "noisy". I'm familiar with the SDK example that demonstrates using low/high pass filtering, etc. -- but I have not seen a good example anywhere that shows calculating velocity.
Does anyone have any real-world experience with this they can share? Code would be fantastic, but really I just want to know if anyone has successfully done this (for a long-lived app) and what approach they took.
EDIT: I've got a working prototype that uses the LocationManager API. It works OK, but the update cycle is far from ideal for a real-time measurement of velocity. Depending on circumstances, it can take up to 4-5 seconds sometimes to update. Cruising at a given speed tends to work OK, but accel/decel tend to lag very badly from a user interaction standpoint. Also, I need to feed velocity into some other calculations that I'm doing and the precision is not really what I need.
It seems possible based on (very few) other apps I've seen, notably gMeter which claims to make no use of GPS but calculates velocity accurately. I'm really surprised there are no references or any sample code that demonstrates this anywhere that I can find. I realize it's complex, but surely there's something out there.

Since the GPS and accelerometer have different weaknesses, your best option is probably a combined approach - Get a measurement from the GPS every minute or so, then add realtime changes from the accelerometer.

From a practical standpoint, you are not going get accurate velocity from forces acting on the accelerometer.
Use the GPS with readings taken at 1 minute intervals and put the GPS to sleep inbetween.
Here is an example:
SpeedViewController.h
CLLocationManager *locManager;
CLLocationSpeed speed;
NSTimer *timer;
#property (nonantomic,retain) NSTimer *timer;
SpeedViewController.m
#define kRequiredAccuracy 500.0 //meters
#define kMaxAge 10.0 //seconds
- (void)startReadingLocation {
[locManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSTimeInterval ageInSeconds = [newLocation.timestamp timeIntervalSinceNow];
//ensure you have an accurate and non-cached reading
if( newLocation.horizontalAccuracy > kRequiredAccuracy || fabs(ageInSeconds) > kMaxAge )
return;
//get current speed
currentSpeed = newLocation.speed;
//this puts the GPS to sleep, saving power
[locManager stopUpdatingLocation];
//timer fires after 60 seconds, then stops
self.timer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:#selector(timeIntervalEnded:) userInfo:nil repeats:NO];
}
//this is a wrapper method to fit the required selector signature
- (void)timeIntervalEnded:(NSTimer*)timer {
[self startReadingLocation];
}

The error in the acceleration will accumulate over time. Your best bet is to get an accurate velocity from the GPS, maybe once a minute or less:
distanceTravelled = sqrt( (position2.x-position1.x)^2 + (position2.y-position1.y)^2 )
velocity = distanceTravelled/timeBetweenGPSReadings
(where ^2 means squared)
Then take frequent measurements of the accelerometer:
newVelocity = oldVelocity + accelerometer*timeBetweenAccelerometerReadings

I'm not sure you'll get very far trying to track velocity using the accelerometer. To do this, you'd have to make sure you captured EVERY acceleration, since any missed data points would indicate the wrong velocity (this is assuming, of course, you're able to convert the reported accelerometer values into standard units). Thus, you'd have to constantly run the accelerometer stuff, which sucks quite a bit of juice in itself (and, again, you won't be guaranteed all accelerations). I'd recommend using CoreLocation.

While I don't know the iPhone API, I do know something about GPS and inertial navigation. It might be useful.
The GPS receivers I have worked with can all provide a direct measurement of velocity from the GPS signals they receive. These measurements are more accurate than position data even. I don't know if the Apple API provides access, or even if apple has configured their receiver to provide this data. This would be the more efficient route to getting a velocity measurement.
The next path, given that you have accelerometer data and GPS data is to combine them as mentioned earlier by other posters and comments. Using the GPS to periodically correct the accumulated intertial measurement from the accelerometer data works very well in practice. It provides the benefit of more frequent accelerometer measurements, and the accuracy of the GPS measurements. A Kalman filter is commonly used. But given the accuracy and timing limits of your chosen platform a kalman filter may be overkill and something simpler to implement and run should work fine.
Anyway, just some things to think about.

Related

How to balance start time for a multiplayer game?

I'm making a multiplayer game with GameKit. My issue is that when two devices are connected the game starts running with a slight time difference. On of the devices starts running the game a bit later. But this is not what i want. i want it to start simultaneously on both devices. So the first thing that i do is i check time of the beginning on both devices like this:
startTime = [NSDate timeIntervalSinceReferenceDate];
and this is how it looks:
361194394.193559
Then I send startTime value to the other device and then i compare received value with startTime of the other device.
- (void)balanceTime:(double)partnerTime
{
double time_diff = startTime - partnerTime;
if (time_diff < 0)
startTimeOut = -time_diff;
}
So if difference between two start times is negative it means that this device is starting earlier and therefore it has to wait for exactly the difference assigned to startTimeOut variable, which is a double and usually is something like 2.602417. So then i pause my game in my update method
- (void)update:(ccTime)dt
{
if (startTimeOut > 0)
{
NSLog(#"START TIME OUT %f", startTimeOut);
startTimeOut -= dt;
return;
}
}
But it unfortunately it doesn't help. Moreover it even extends the difference between start times of the devices. I just can't get why. Seems like everything i'm doing is reasonable. What am i doing wrong? How do i correct it? What would you do? Thanks a lot
As Almo commented, it is not possible to synchronize two devices to the same time. At the lowest level you will gnaw your teeth out on the Heisenberg Uncertainty Principle. Even getting two devices to synchronize to within a tenth of a second is not a trivial task. In addition, time synchronization would have to happen more or less frequently since the clocks in each device run ever so slightly asynchronous (ie a teeny bit faster or a weeny bit slower).
You also have to consider the lag introduced by sending data over Wifi, Blutooth or over the air. This lag is not a constant, and can be 10ms in one frame and 1000ms in another. You can't cancel out lag, nor can you predict it. But you can predict player movements.
The solution for games, or at least one of them, is client-side prediction and dead reckoning. This SO question has a few links of interest.

How accurate is CoreLocation's accuracy measurement

I have been testing the accuracy of various aspects of CoreLocation and have been surprised to find the accuracy value supplied on new and old locations passed to the CLLocationMangerDelegate method: (void)locationManager:didUpdateToLocation:fromLocation is wildly inaccurate itself.
Walking certain stretches of pavement(along a stretch of road with buildings along one side) I have found that whilst CLLocationManager claims the CLLocations it is supplying have a horizontal accuracy of 5 meters, in actual fact the position shown by the MKUserAnnotation is anything up to 30 meters away from the position I know I'm in (often showing as being the opposite side of a row of buildings). This suggests that the accuracy reading itself is not accurate. CLLocationManager is telling me its coordinates are accurate to within 5 meters but are actually as many as 30 meters out.
To clarify, this is not a caching/timestamp issue It is repeatable in certain areas, and not something that improves over time.
Has anyone else encountered this issue?
CoreLocation is not a human and does not know where you "really" are, is the essence of the answer to your question. It is using the sensors on your device and giving you the best answer it's got, and the problems with its ability to know the accuracy of its claimed answer, reflect that the problem of location is inherently very difficult.
To digress into the location data itself: CoreLocation draws from cell tower triangulation, nearby SSIDs, and finally GPS, to determine the device's location. These do have varying degrees of accuracy, but what they have in common is that if you let the device work for longer, you'll get a more accurate answer. However, because all of the tools that CoreLocation uses are expensive in terms of battery life, it will stop as soon as it has generated an answer that meets your criteria for "good enough."
This is why another answer told you to set your desiredAccuracy to kCLLocationAccuracyBestForNavigation - when you do that, you're basically telling CoreLocation "show me the best you've got," and you've also volunteered for the CPU load, time, and battery drain that come with that. You've also gone off the normal scale that CoreLocation uses to figure out what's "good enough" - instead of telling it ahead of time when it can stop, you've basically said "I'll tell you when it's good enough."
CoreLocation will thus keep on trying to give you better answers until you tell it you're done, and you need to carry out your responsibility for deciding what that point is. This is also where you need to bear in mind that there are limits to how accurate the tools in CoreLocation's repertoire can be - you can get lots of significant digits of latitude and longitude, but by venturing off the scale, you've volunteered for the task of figuring out how trustworthy those numbers are. The pre-defined scales of accuracy are 10 meters, 100 meters, 1 km, and 3km. The fact that the creators of CoreLocation chose those cut-off points should tell you something about the difficulty of the problem - if better accuracy were easy, it would probably already be in the framework.
So circling back around: CoreLocation is reporting the accuracy of its answers faithfully, it's just that the data underlying those answer is problematic, and if it knew more about the accuracy of its accuracy claims, it would reflect that in its accuracy claims. Consider the hypothetical situation where CoreLocation knows exactly how accurate its accuracy claims are, then the situation where CoreLocation knows nothing about how accurate its accuracy claims are, and consider how you'd design an API to account for the problem. CoreLocation is as sure as it can reasonably be about how sure it is, and if you can figure out a tractable-within-the-limits-of-the-hardware way to be more sure about how sure you are, App Store wealth awaits you.
the hardware takes some time to get the accuracy. the best accuracy is 65m in 2-3 seconds. else u have 1414m accuracy

Do I get more accurate or faster accelerometer readings with Core Motion?

I can use this method of the Core Motion framework:
- (void)startAccelerometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMAccelerometerHandler)handler
as an alternative to the
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
UIAccelerationDelegate method of UIKit. There, I have to specify the frequency of updates. I want to get acceleration data as fast and accurate as possible.
Is Core Motion my friend here? What's the frequency of readings I can expect from that method of Core Motion? The documentation doesn't talk about it.
Edit! Now that this answer is OVER A YEAR OLD, certainly it is only of historic value. At this stage, I guess, everyone should only be using CoreMotion and that's that. Hope this historic answer it helps an history students!
My impression is that: CMAttitude and CMDeviceMotion give you the benefit of both the accel'r and the gyro (if the gyro is present). Whereas CMAccelerometerData is only the accel'r. (Exactly like in the old days before Core Motion.)
My impression is that the two specific advantages of CoreMotion are:
(One) the mindboggling convenience of having Quaternions and attitude given to you on a silver platter
(Two) apparently, both gyro and accel'r are used on machines with a gyro, with no extra work from you ... a huge advantage.
I believe there is no actual technical difference in the quality of usage of the accelerometer per se.
Thus: if (for some reason) you want only the accel'r, and you can do the math (if relevant to your task at hand), you could use the old-school accel'r delegate.
It seems that in short, none of us will ever again use the old-school accelerometer delegate!

Getting displacement from accelerometer data with Core Motion

I am developing an augmented reality application that (at the moment) wants to display a simple cube on top of a surface, and be able to move in space (both rotating and displacing) to look at the cube in all the different angles. The problem of calibrating the camera doesn't apply here since I ask the user to place the iPhone on the surface he wants to place the cube on and then press a button to reset the attitude.
To find out the camera rotation is very simple with the Gyroscope and Core Motion. I do it this way:
if (referenceAttitude != nil) {
[attitude multiplyByInverseOfAttitude:referenceAttitude];
}
CMRotationMatrix mat = attitude.rotationMatrix;
GLfloat rotMat[] = {
mat.m11, mat.m21, mat.m31, 0,
mat.m12, mat.m22, mat.m32, 0,
mat.m13, mat.m23, mat.m33, 0,
0, 0, 0, 1
};
glMultMatrixf(rotMat);
This works really well.
More problems arise anyway when I try to find the displacement in space during an acceleration.
The Apple Teapot example with Core Motion just adds the x, y and z values of the acceleration vector to the position vector. This (apart from having not much sense) has the result of returning the object to the original position after an acceleration. (Since the acceleration goes from positive to negative or vice versa).
They did it like this:
translation.x += userAcceleration.x;
translation.y += userAcceleration.y;
translation.z += userAcceleration.z;
What should I do to find out displacement from the acceleration in some istant? (with known time difference). Looking some other answers, it seems like I have to integrate twice to get velocity from acceleration and then position from velocity. But there is no example in code whatsoever, and I don't think that is really necessary. Also, there is the problem that when the iPhone is still on a plane, accelerometer values are not null (there is some noise I think). How much should I filter those values? Am I supposed to filter them at all?
Cool, there are people out there struggling with the same problem so it is worth to spent some time :-)
I agree with westsider's statement as I spent a few weeks of experimenting with different approaches and ended up with poor results. I am sure that there won't be an acceptable solution for either larger distances or slow motions lasting for more than 1 or 2 seconds. If you can live with some restrictions like small distances (< 10 cm) and a given minimum velocity for your motions, then I believe there might be the chance to find a solution - no guarantee at all. If so, it will take you a pretty hard time of research and a lot of frustration, but if you get it, it will be very very cool :-) Maybe you find these hints useful:
First of all to make things easy just look at one axis e.g x but consider both left (-x) and right (+x) to have a representable situation.
Yes you are right, you have to integrate twice to get the position as function of time. And for further processing you should store the first integration's result (== velocity), because you will need it in a later stage for optimisation. Do it very careful because every tiny bug will lead to huge errors after short period of time.
Always bear in mind that even a very small error (e.g. <0.1%) will grow rapidly after doing integration twice. Situation will become even worse after one second if you configure accelerometer with let's say 50 Hz, i.e. 50 ticks are processed and the tiny neglectable error will outrun the "true" value. I would strongly recommend to not rely on trapezoidal rule but to use at least Simpson or a higher degree Newton-Cotes formula.
If you managed this, you will have to keep an eye on setting up the right low pass filtering. I cannot give a general value but as a rule of thumb experimenting with filtering factors between 0.2 and 0.8 will be a good starting point. The right value depends on the business case you need, for instance what kind of game, how fast to react on events, ...
Now you will have a solution which is working pretty good under certain circumstances and within a short period of time. But than after a few seconds you will run into trouble because your object is drifting away. Now you will enter the difficult part of the solution which I failed to handle eventually within the given time scope :-(
One promising approach is to introduce something I call "synthectic forces" or "virtual forces". This is some strategy to react on several bad situations triggering the object to drift away although the device remains fixed (? no native speaker, I mean without moving) in your hands. The most troubling one is a velocity greater than 0 without any acceleration. This is an unavoidable result of error propagation and can be handled by slowing down artificially that means introducing a virtual deceleration even if there is no real counterpart. A very simplified example:
if (vX > 0 && lastAccelerationXTimeStamp > 0.3sec) {
vX *= 0.9;
}
`
You will need a combination of such conditions to tame the beast. A lot of try and error is required to get a feeling for the right way to go and this will be the hard part of the problem.
If you ever managed to crack the code, pleeeease let me know, I am very curious to see if it is possible in general or not :-)
Cheers Kay
When the iPhone 4 was very new, I spent many, many hours trying to get an accurate displacement using accelerometers and gyroscope. There shouldn't have been much concern about incremental drift as device needed only move a couple of meters at most and the data collection typically ran for a few minutes at most. We tried all sorts of approaches and even had help from several Apple engineers. Ultimately, it seemed that the gyroscope wasn't up to the task. It was good for 3D orientation but that was it ... again, according to very knowledgable engineers.
I would love to hear someone contradict this - because the app never really turned out as we had hoped, etc.
I am also trying to get displacement on the iPhone. Instead of using integration I used the basic physics formula of d = .5a * t^2 assuming an initial velocity of 0 (doesn't sound like you can assume initial velocity of 0). So far it seems to work quite well.
My problem is that I'm using the deviceMotion.and the values are not correct. deviceMotion.gravity read near 0. Any ideas? - OK Fixed, apparently deviceMotion.gravity has a x, y, and z values. If you don't specify which you want you get back x (which should be near 0).
Find this question two years later, I just find a AR project on iOS 6 docset named pARk, It provide a proximate displacement capture and calculation using Gyroscope, aka CoreMotion.Framework.
I'm just starting leaning the code.
to be continued...

iPhone CoreLocation: How to get the most accurate speed

I'm experimenting with adding the GPS functionality to my iPhone app. It's a workout app that will be used while walking or running. So what I want to use GPS for is to show the speed that the person is moving in Mph and minute/mile.
How should I configure the CLLocationManager so I get the best possible results? What should I set desiredAccuracy and distanceFilter?
I've tried with:
distanceFilter = 10 and desiredAccuracy = kCLLocationAccuracyNearestTenMeters
and reading
CLLocation.speed property
Testing while driving around in my car the accuracy seems good compared to the car speedometer although it takes a while to update. I realize that the update delay may very well be the time it takes to query the GPS location, but I'm not sure if changing the above two parameters would give better results.
Should I use kCLLocationAccuracyBest and some other value for distanceFilter?
I'm interested to hear from others using CoreLocation to get speed. What are you doing to get more accurate results?
For best results, you should use kCLLocationAccuracyBest. What you put into your distance filter depends on up with which faults you're willing to put. Basically, you're going to have to make decisions based on accuracy vs availability. That is, during periods when a best-accuracy answer is not available, what will you display?
One approach is to let the phone deliver less-accurate answers and, using a projection of what was happening the last time you had best-accuracy information, see if what you have makes sense.
That is, suppose I'm jogging at 6mph to the North. You plot me along point-A, point-B, point-C... then you get a low-accuracy answer (maybe kCLLocationAccuracyNearest100Meters.) Look at the spot where it says I am and figure out "could I have gotten to that spot from point-C if I'd continued along my current path, making reasonable adjustments for possible changes in speed?" If so, then the new point is within the realm of possibility. (If not, then toss it out.) Then project from point-C at my last-known speed and figure out where you think I probably am, ballistically. Save that as ballistic-point-D.
Of course, you're using the accelerometer to get some sort of inertial sense of which way I went, right? So, you can't know direction (you don't know what way the phone is pointing), but you can make a reasonable stab at distance.
Using all this information, plot the most likely spot where you think I probably am.
NOTE: When testing, don't just drive in good-cell coverage areas. See how your app performs out in the hills, away from cell phones. A lot of people like to bike & jog those areas!
Disclaimer: I've only played with CoreLocation a bit, I've not tested the accuracy very closely.
I'd expect that you'd get the most accurate results by using the defaults for distanceFilter and desiredAccuracy. Less-frequent updates are only going to give you less data to work with.
One issue you're likely to run into is when the location fix is lost for a while, then comes back. The naive, connect-the-dots approach to figuring out distance traveled is going to tend to under-estimate the actual speed of the runner. Rather than using CLLocation.speed, you might get better results calculating speed based on some heuristic approximation to the line the runner is actually following.