I am working on a GPS Apple Watch app for use when sailing which requires high accuracy for a start line scenario. I suspect the additional gyro/accelerometer inputs are actually hurting, not helping GPS accuracy. For example, CLLocation.speed variability seems very high compared to other instruments. (i.e 4.4 knots, 2.2 knots, 6.8 knots, 4.6 knots ... , when other sailing GPS instruments read 4.5 ,4.3 ,4.6 ,4.5, ...)
I understand that having the iPhone nearby will make the watch offload GPS processing to the iPhone. This definitely helps accuracy, but does not seem to help variability. When I am testing off the water (walking or riding a bike), the variability is much lower than sailing. I get similar results running my code on iPhone app and on watch (with iPhone nearby).
func startReceivingLocationChanges(locationManager: CLLocationManager) -> Bool {
// Do not start services that aren't available.
if !CLLocationManager.locationServicesEnabled() {
// Location services is not available.
return false
}
// Configure and start the service.
locationManager.desiredAccuracy = kCLLocationBestForNavigation // kCLLocationBestForNavigation seems to have accuracy issues when sailing, try kCLLocationBest???
locationManager.distanceFilter = kCLDistanceFilterNone // In meters.
locationManager.delegate = self
locationManager.startUpdatingLocation()
return true
}
I am considering trying kCLLocationAccuracyBest to turn OFF the Gyro/accel in hopes that this may help. I am assuming speed and course are simple calculations based on lat/long changes, but perhaps not? I can do these calculations myself if needed. Also interested in possibly using some 3rd party filtering code to "smooth" the curves. Curious what others have tried. (I suspect this might also be an issue for swimmers, and/or mountain biking tracking scenarios where movement is not "smooth" or easily filtered out.)
Related
I need to track when the user is traveling. For this I setup CLLocationManager like this:
self.locationManager.requestAlwaysAuthorization()
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
self.locationManager.distanceFilter = 200
self.locationManager.pausesLocationUpdatesAutomatically = false
self.locationManager.allowsBackgroundLocationUpdates = true
self.locationManager.activityType = .other
So my app gets the location even if it is not running, saves the location in the DB and gets to background again. Later when the user is starting the app the travel is getting extracted from the new locations in the db.
This is working more or less with an energy consumption of ~15%. The position is determined with the help of cell towers and not by gps.
I realized that on some days the tracking is not working for some regions (train tracks). Do you have an idea why this happens? (Only iOS10 devices).
Do you see improvements to reach my goal? Is there anything else that can be used to determine a travel? (A travel is when you commute with more than 15 km/h and more than 15 min).
Thanks
There can be multiple activity type which can help you locate tracks, boats etc. Following is the apple doc on activity type:
public enum CLActivityType : Int {
case other
case automotiveNavigation // for automotive navigation
case fitness // includes any pedestrian activities
case otherNavigation // for other navigation cases (excluding pedestrian navigation), e.g. navigation for boats, trains, or planes
}
You can use self.locationManager.activityType = .otherNavigation
With iOS 12 you can also monitor position using the new activity type:
"Use this activity type if the expected user activity is well above
ground level."
CLActivityType.airborne
As Apple suggest in the reference: https://developer.apple.com/documentation/corelocation/clactivitytype/airborne
I would need very accurate speed for my app, even when the user is only walking. I tried using the GPS, it works well but not for speeds that low. So my question is, could I use the accelerometer to get some more accurate values? If yes, how would I do that?
I do not think you will get something from accelerometer. Accelerometer only returns you motion (x,y,z). I do not know how you could able to get speed out of that, unless you will do some linear modelling algorithm that requires heavy research, that could be a Ph.D topic I guess.
I propose the following implementation.
- (id) init
{
self = [super init];
if (self != nil) {
self.manager = [[CLLocationManager alloc] init];
self.manager.delegate = self;
[self.manager startUpdatingLocation];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"Speed = %f", newLocation.speed);
}
or you could use directly CMPedometer class which I believe that class developed exactly for physical activity applications. I have recently used that class and I am quite satisfied.
import CoreMotion
let lengthFormatter = NSLengthFormatter()
let pedometer = CMPedometer()
pedometer.startPedometerUpdatesFromDate(NSDate(), withHandler: { data, error in
if !error {
println("Steps Taken: \(data.numberOfSteps)")
var distance = data.distance.doubleValue
println("Distance: \(lengthFormatter.stringFromMeters(distance))")
var time = data.endDate.timeIntervalSinceDate(data.startDate)
var speed = distance / time
println("Speed: \(lengthFormatter.stringFromMeters(speed)) / s")
}
})
could I use the accelerometer to get some more accurate values
No, the accelerometer cannot tell you anything about speed. Acceleration and speed are very different things. Indeed, during motion with a constant speed, there is no acceleration (except gravity and similar side effects). You could travel at the speed of light and experience no acceleration (if you could travel at the speed of light).
It really isn't possible to get a decent speed estimate from the Accelerometer. That even if you start with the device at rest and end with it at rest and alsoo using the gyroscope.
Averaging the GPS location is probably the best, possibly a moving average of some sort.
I already have answerded similar question many times.
The apporach with the accelerometer will not work well for more than some seconds. The cause is the dramatic accuracy loss due double integration.
This is well explained in a Goole Talk "Sensor Fusioning" video.
What you can do, for low GPS speed:
A bit simplified:
1. Take locations evry 5 seconds,
2. calculate distance between fix(i-5) and fix(i),
3. calcuate time difference,
and cacluate then the speed in m/s by distanceMeters / timeDeltaSeconds
An advanced solution would use a time window of let's say 5 seconds,
and this evaluation window will move 1 step further every second,
using the same 3 calculation steps as above
I want to calculate user steps(like pedometer).I know that with iPhone 5s, 6 and 6+ we can use CMStepCounter or CMPedometer class(which use M7 chip of devices) but iPhone 5 and lower versions does not support M7 chip, so we can't use CoreMotion. By searching all over internet i came to know that we can use accelerometer sensor for this purpose. But after spending a lot of time still i'm not able to make an accurate algorithm that works.
Edit2: After spending several days on searching google i tried a lot but still unable to find an working algorithm for counting user step using accelerometer.
Can anybody out there who can help me?
CMMotionManager is what you are looking for if you are using later versions of iOS.
However, if you want to continue with iOS 5 or lower you need to use the following although this is deprecated.
UIAccelerometer * accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.delegate = self;
The method where you can get x, y, z values is:
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
//Check your x,y,z values to find step ...
}
If you need to know the logic behind step counter you can search and read through in Google :)
I am making an alarm that shuts off only after the phone has been moved a certain distance(to force the user out of bed). I need to be able to find the distance travelled after the alarm has gone off, then take this distance and use it in a method to shut the alarm sound off if the minimum distance has been travelled. Any ideas?
I'm using the following to update the location. Any thoughts on how to incorporate this into disabling the alarm?
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if(!newLocation) {
NSLog(#"No movement");
}
if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
(oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
{
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
CLLocationDistance distance = [loc2 distanceFromLocation:loc1];
if (distance>=4) {
NSLog(#"Very good!");
}
}
NSLog(#"Location:%#", newLocation);
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"Could not find location: %#", error);
}
A couple of thoughts:
You're not considering horizontalAccuracy. First if this is negative for either coordinate, then you just can't do any comparison. And even if they're both non-negative, you probably want to back out the horizontal accuracies if you really want to determine if the user moved a certain distances.
Look at not only the latitude and longitude, but the horizontalAccuracy, too.
You're not considering that when you turn on location services, there are frequently a whole series of coordinates that come in, and the coordinates may bounce around as the horizontal accuracy gets better and better. You want to make sure you don't consider this slow triangulation as the movement necessary to qualify as having gotten out of bed.
Make sure you do your experimentation on a real device, in real world scenarios. Using the simulator is definitely not a good test. Nor is using a device plugged into your computer (because you you want to see your GPS in real world scenario, where someone has moved around before going to bed).
I'd suggest starting with a simple app that logs location events in a database (or other format) and perhaps show the log in a tableview, so you don't always have to go back to your computer to review the results. Fire up that app, walk around, and then look at the location events you get. That will help you get your arms around the patterns you see as people use their actual devices.
Note that if this app is destined for the app store, you'll want to engage lots of different people in different environments and different devices and different scenarios (poor GPS locations, with wifi, no wifi, etc.). Even when you do your own real-world GPS experience with a physical device, you must appreciate that everyone else's may experience different GPS results.
I must confess to some reservations whether the iPhone GPS is accurate enough for this sort of app as a generalized solution, and you'll probably want to warn the user to make sure to plug in their iPhone before they go to sleep so the GPS doesn't drain the battery. You might also want to play tricks like turning on location services after the user has demonstrated that they're up and about. You might also not even want to turn on location services until a few minutes before the alarm, thereby assuring that the GPS is as accurate as it can be when the alarm goes off, but not draining their battery as they sleep if they don't happen to have it plugged in and charging.
Update:
Probably easier than calculating distances yourself, use the location services distance filter (i.e. only generate events when the location changes by x meters). For example:
- (void)startLocationManager
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 5.0; // detect when I move 5 meters
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
When I do that, standing completely still, as soon as I turn on location services, I get a whole bunch of events before I even start moving. You can see the GPS warming up, and getting more and more accurate:
latitude longitude horizontalAccuracy
---------------- ------------------ ------------------
39.9482837548282 (82.4963494095571) 65
39.9482817585030 (82.4964543834170) 10
39.9482392622539 (82.4964914314290) 5
39.9481753502422 (82.4964918505242) 5
39.9481269028419 (82.4964797805836) 5
I wait for that to quite down (it took 15-20 seconds in my case), and then I started walking in a straight line, looking at the distance from the last location I got above.
latitude longitude horizontalAccuracy distance
---------------- ------------------ ------------------ --------
39.9481722908476 (82.4964962091138) 5 5.3
39.9482206544289 (82.4965029146363) 5 10.4
39.9482627315828 (82.4965282279839) 5 15.4
39.9483157471204 (82.4965248752227) 5 21.2
I must confess that while I didn't measure it, these distances didn't feel quite right, but possibly within the 5 meter tolerance I set up with my distanceFilter.
Anyway, as all of this evidences, the right process is probably going to be
turn on location services, setting the distanceFilter appropriate for your app;
wait for the location to settle down;
make sure the horizontalAccuracy is even plausible for this process to work at all; and
wait for the new location notification based upon the GPS determining that the distanceFilter has been exceeded.
This never will be perfect (e.g. I had to walk a good 6-10 meters before my "5 meter" distanceFilter kicked in, probably a combination of the GPS lagging a few seconds and the horizontalAccuracy), but it might be "good enough for government work."
(By the way, I've changed my coordinates in the above log, so don't look for me in this Ohio cornfield, but it gives you and idea of the sort of pattern you may see.)
the following are listed in CLLocation.h but from my experience they are deceiving names- possibly originally thought up to serve two purposes, 1. to test the accuracy of the location returned, but also 2. to set how hard the location manager works, specifically what is enabled (gps (how many sat channels), how hard the wifi works, triangulation etc.
extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation; // (raw value: -2)
extern const CLLocationAccuracy kCLLocationAccuracyBest; // (raw value: -1)
extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters; // (raw value: 10)
extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters; // (raw value: 100)
extern const CLLocationAccuracy kCLLocationAccuracyKilometer; // (raw value: 1000)
extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers; // (raw value: 3000)
I would love to take a look at CLLocation.m, but as that is not likely to happen any time soon- does anyone have any field testing showing what they think is going on with these different modes.
ie, kCLLocationAccuracyBest = 10 satellite (channels/trunks?), 100% power to wifi etc..
I'm kind of guessing at straws here- I think this is the type of information apple should have provided-
what I really want to know is, what is actually happening with kCLLocationAccuracyThreeKilometers in relation to battery draw- is the gps on? 1 sat trunk? wifi enabled? wifi on a timer? who knows? I know I'd like to
I agree with Olie that hiding the details of the algorithm is intended to protect the app developer from worrying about how location is determined. That said, I believe it's still reasonable to ask the question: "what are the power implications of my accuracy selection?".
I have a little bit of information that might guide your decision on which to use, but I don't know the true details of Apple's implementation.
First, assume that as the reading becomes more accurate, the system will need to use more power-hungry radios. For example, the GPS will be required for the most detailed readings, inside 100 Meters, and it uses the most power.
Here is an educated guess at the mechanism used to determine the accuracy. List is ordered with (1) being the highest battery drain.
GPS - kCLLocationAccuracyBestForNavigation;
GPS - kCLLocationAccuracyBest;
GPS - kCLLocationAccuracyNearestTenMeters;
WiFi (or GPS in rural area) - kCLLocationAccuracyHundredMeters;
Cell Tower - kCLLocationAccuracyKilometer;
Cell Tower - kCLLocationAccuracyThreeKilometers;
When choosing, it is recommended by Apple that you select the most coarse-grained accuracy that your application can afford.
Hope that helps, a.little.
In the business district of a major city, wifi and cell tower triangulation are both very good. Residential suburbs they're not so good. In rural areas they barely work if they work at all.
GPS doesn't work very well indoors, and can take a very long time to get any fix at all without cell tower assistance (possibly 20 minutes!!). It takes that long for the satelites to broadcast enough information to determine your location, and there can be packet loss (clouds, buildings, trees, mountains, etc). It's worth noting that a proper high end GPS will have an antenna the size of a basket ball, no handheld GPS can get a perfect signal.
Even outdoors with perfect signal, GPS is inaccurate when you change direction rapidly (such as on the highway or a windy road). The BestForNavigation setting uses the accelerometer and gyroscope to offset this.
Currently, the iOS platform uses:
GPS: very accurate, but high power draw, slow and not always available. some hardware doesn't have a GPS.
WiFi: lots of power draw, and only works in the city. Can also be flat out wrong (eg place you in the wrong city)
Cell Tower: almost no power draw at all, and works well in the city. Not so great in rural areas. Doesn't exist on some hardware.
Accelerometer: slight improvements to other location fixes, but huge power draw.
Gyroscope: slight improvements to other location fixes, but huge power draw. iPhone 4 only.
You give it an accuracy in meters that you need (the constants are just nice names for meters), and it will use a combination of the above, to get you that level of accuracy with the fastest possible fix and lowest possible power draw. The technique it uses will change, from one user to another, and will change depending on where in the world the user is standing at the time.
The whole point of using extern rather than exposing what is actually happening is so that the under-gerwerkkins can change and your code doesn't have to worry about it to pick up the improvements.
That said, CLLocationAccuracy is typedef-ed to double, so I think it's fair to guess that kCLLocationAccuracyNearestTenMeters = 10.0, kCLLocationAccuracyHundredMeters = 100.0, etc. Best is likely either 0, 1 or kCLLocationAccuracyNearestTenMeters, and BestForNavigation is probably one they tossed it to help folks like TomTom, etc.
If you REALLY want to know, you can print out the values -- they're just doubles.
I do not believe that the number of satellites or power to wifi is altered based on your desired accuracy. The way I understand the algorithms, there is an approximation calculation that, the more times through the loop, the more accurate it gets. Hence, less-accurate just bails earlier.
But, again, the more important point is: it doesn't matter. Apple specifically doesn't describe what goes on behind the scenes because that's not part of the design. The design is: if you use kCLLocationAccuracyKilometer, you'll get an answer that's within a kilometer, etc. And Apple is now free to change how they arrive at that without you caring. This sort of isolation is a basic tenet of object oriented programming.
EDIT:
CORRECTION -- I'm just now watching the WWDC session on location (Session 115) and, at about 22:00 or so, he talks about how, when using BestForNavigation, this adds in some gyroscope correction (when available.) However, he warns that, while this is power & CPU intensive, and should be only used when necessary, as with turn-by-turn navigation.
I'm not sure how much more I can talk about this publically but, if you're a registered developer, you can get the sessions from iTunes-U.
(This is WWDC-2010, btw.)