Hi all,
This is my first post here, but before I go into details, a big thank you to @omz for this amazing tool and all the Pythonista community for all the help I've been silently getting from your posts. Thank you all!
I apologize in advance for this first long post, I promise future ones will be shorter.
I'm relatively confident with Python and I've been programming in different languages (namely VB, Perl and Python) for many years, both for business and mostly for pleasure. I'm quite new to Pythonista and IOS programming, though, and all in all by no means a professional coder, more of a 'quick and dirty' inefficient DIY builder, which I'm sure you'll easily spot in my poor code. I'm also stubborn, and don't like to bother people if I can learn from others independently or find a way to do the job myself, so I've been delaying the moment to come for help; but the moment has now arrived.
I'm a private pilot in my spare time, and I'm writing a prototype app for a custom flight log and associated utilities that I would like to have in my flights. At this point in time, I wanted to add heading (from magnetometer) and then course (from GPS) information in order to compute wind drift conditions, so I started writing a little separate module using the objc_util bridge.
For the time being, this simply creates a CLLocation manager object, starts the heading updates, uses the didUpdateHeading delegate to receive the updates and presents the information in a simple view with a couple of label objects. I'm also printing out to the console for debugging purposes.
It took me quite some time to figure out how to get this heading information working, but now that I finally I got it I'm stuck with the fact that the heading updates simply stop after a random number of updates are received (say in 5 to 10 seconds); as I start moving the device pointing at different positions, sometimes I may receive forty or fifty updates, sometimes only five or six. I get no error message (or at least I can't see any), it just looks like the script is frozen. I can close the view, but when I do this the [x] button in the console is grayed out; for some reason I have the feeling that the process is still running and listening for updates but these are simply either not received or not reflected in the view label or printed out to the console. It would seem to me that the systems might be overloaded with updates? This seems odd to me, since any other 'compass' app out there is obviously reporting all updates constantly. I need best accuracy and frequency for my purposes, but still I tried to use the filter variable to reduce the frequency and report only updates greater than 3 degrees; the filter works, but the behaviour is still the same, updates stop rather quickly.
If I try to use location updates (startUpdatingLocation() for GPS lat/lon position), it is even worse; I get a couple of location updates at most, maybe a couple of heading updates, sometimes none, and then it freezes. So I commented out location updates for the time being.
So all this still leads me to believe there is some kind of problem regarding competing resources, several script processes running or something similar going on, but I have no idea how to even debug the situation.
I have been testing on two different platforms with similar results:
- iPad 2nd gen, GPS and GSM capabilities on IOS 9.3.5
- iPhone 8 on IOS 12.1.2
And Here is my code:
Any help truly appreciated!
Thx!
# coding: utf-8
from objc_util import *
import ui
def printMethods(whichObject):
#print (dir(whichObject))
for method in dir(whichObject):
print (method)
#- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading;
def locationManager_didUpdateHeading_(self, _cmd, manager, newHeading):
headingObj = ObjCInstance(newHeading)
print ("Magnetic:", headingObj.magneticHeading())
print ("True:",headingObj.trueHeading())
print ("ACCURACY:",headingObj.headingAccuracy())
v['m_Heading_value'].text = str(int(headingObj.magneticHeading()))
def locationManager_didUpdateLocations_(_self,_cmd,manager,locations):
locations=ObjCInstance(locations)
print (locations[0].coordinate().a)
print (locations[0].coordinate().b)
def locationManager_didFailWithError_(_self,_cmd,manager,error):
error=ObjCInstance(error)
print (error)
methods = [locationManager_didUpdateHeading_,locationManager_didUpdateLocations_,locationManager_didFailWithError_]
protocols = ['CLLocationManagerDelegate']
MyLocationManagerDelegate = create_objc_class('MyLocationManagerDelegate', methods=methods, protocols=protocols)
v = ui.load_view()
@on_main_thread
def mymain():
CLLocationManager = ObjCClass ('CLLocationManager')
myloc = CLLocationManager.alloc().init().autorelease()
#print (printMethods(myloc))
delegate = MyLocationManagerDelegate.alloc().init()
myloc.setDelegate_(delegate)
locationAvailable = CLLocationManager.headingAvailable()
print ("HEADING AVAILABLE")
print (locationAvailable)
print ("CURRENT HEADING ORIENTATION")
print (myloc.headingOrientation())
print ("NEW HEADING ORIENTATION")
myloc.headingOrientation = 3
print (myloc.headingOrientation())
myloc.headingFilter = 3
print ("NEW THRESHOLD")
print (myloc.headingFilter())
print ("START MONITORING ")
#myloc.startUpdatingLocation()
myloc.startUpdatingHeading()
v.present('sheet')
if __name__ == '__main__':
mymain()