Forum Archive

Get Data from objc_util.__Structure

gyronaut

I would like to read the raw gyroscope data via objc_util. Generally it seems to work. The problem is that the return value is a Object-C structure resulting in a obc_util.__Structure class. The question is how I can the fetch the values of this structure. Appending a simple ".x" does not work.

omz

Structure classes that are auto-generated by objc_util use the lowercase alphabet for their field names, i.e. the first field is named a, the second b, etc.

omz

If you want to access the fields with their original names, you have to define a subclass of ctypes.Structure and pass that as the optional restype keyword argument of an ObjC method call. Here's an example of that:

from objc_util import *
from ctypes import Structure, c_double
import time

class CMRotationRate (Structure):
    _fields_ = [('x', c_double), ('y', c_double), ('z', c_double)]

CMMotionManager = ObjCClass('CMMotionManager')
mgr = CMMotionManager.alloc().init()
try:
    mgr.startGyroUpdates()
    for i in range(10):
        time.sleep(1)
        gyro_data = mgr.gyroData()
        if not gyro_data:
            print('data not available (yet?)')
            continue
        # Using the custom struct here:
        rate = gyro_data.rotationRate(argtypes=[], restype=CMRotationRate)
        # You can now access the struct's fields as x, y, z:
        print(rate.x, rate.y, rate.z)
finally:
    mgr.stopGyroUpdates()
    mgr.release()

gyronaut

Wow! Thanks a lot!!!
class CMRotationRate (Structure):
fields = [('x', c_double), ('y', c_double), ('z', c_double)]

and
gyro_data.rotationRate(argtypes=[], restype=CMRotationRate)
was exactly the answer I searched for.
It was my last open issue impementing an AHRS algorithm on Pythonista.