Skip to content

Add calibration offsets and radii as properties on BNO055 library #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from

Conversation

katlings
Copy link
Contributor

Issue #21

the @property decorator + Struct had some interesting __get__ interactions, resulting in some repetition in the code. Happy to revisit if someone has a better idea.

I didn't use the ReadOnlyStructs already defined in this file because letting the user overwrite the calibration is supported in Arduino, so someday it would be nice to do that here too. I don't think it'll take much

@caternuson
Copy link
Contributor

Thanks for the PR. It's not passing the Travis check. Looks like it's missing docstrings. Let us know if you need help checking and reading the Travis report.

@tannewt
Copy link
Member

tannewt commented Mar 8, 2019

@caternuson Would you mind picking this up? I think @katlings is travelling. They did this during the PyCascades sprint last week.

@katlings
Copy link
Contributor Author

katlings commented Mar 8, 2019

My bad! @tannewt is right, I'm on the road, but I should be able to find time to fix this up on Monday. If you'd like it sooner, feel free to take over!

@caternuson
Copy link
Contributor

@katlings @tannewt I'm fine with waiting until Monday. I wouldn't be able to get to this myself until then anyway.

@katlings
Copy link
Contributor Author

@caternuson Check again! :)

@deshipu
Copy link
Contributor

deshipu commented Mar 12, 2019

It looks good and is readable, but that's a lot of repeated code. I wonder if it would make sense to have something like a context manager for encapsulating that code. Something like:

class Mode:
    __init__(self, sensor, mode):
        self.sensor = sensor
        self.mode = mode
        self.last_mode = None
   
    ___enter__(self):
        self.last_mode = self.sensor.mode
        self.sensor.mode = mode
        return self
      
    __exit__(self, exc_type, exc_value, traceback):
        self.sensor.mode = last_mode

and then:

with EnsureMode(self, CONFIG_MODE):
    return self._offsets_accelerometer

Alternatively (and I think ultimately better), perhaps we could have a mode-aware version of the Struct class? Something like:

class ModeStruct(Struct):
    def __init__(self, register_address, struct_format, mode):
        super().__init__(register_address, struct_format)
        self.mode = mode

    def __get__(self, obj, objtype=None):
        last_mode = obj.mode
        obj.mode = self.mode
        result = super().__get__(obj, objtype)
        obj.mode = last_mode
        return result

    def __set__(self, obj, value):
        last_mode = obj.mode
        obj.mode = self.mode
        super().__set__(obj, value)
        obj.mode = last_mode

@tannewt tannewt requested a review from caternuson March 12, 2019 17:56
@tannewt
Copy link
Member

tannewt commented Mar 12, 2019

@deshipu That's a good idea but let's not require it for this PR. That way someone else can pick it up.

@katlings
Copy link
Contributor Author

@deshipu I like both solutions; the mode struct in particular looks like it would enable manually setting these parameters (which I believe is supported) with no further fuss.

I'm still traveling and am happy to follow up when I get home tomorrow, though I don't have the right board to test this with and might need @tannewt's help for that.

@tannewt
Copy link
Member

tannewt commented Mar 12, 2019

@katlings I'm happy to get you boards if you want to keep working on this when you get home.

@caternuson
Copy link
Contributor

@katlings Thanks, this is looking good. Nice continued use of adafruit_register. Would you be up for adding write capability as well?

Current code for reading tests and works:

Adafruit CircuitPython 3.1.2 on 2019-01-07; Adafruit ItsyBitsy M0 Express with samd21g18
>>> import board, busio
>>> import adafruit_bno055
>>> i2c = busio.I2C(board.SCL, board.SDA)
>>> bno = adafruit_bno055.BNO055(i2c)
>>> bno.offsets_magnetometer
(0, 0, 0)
>>> bno.offsets_gyroscope
(-2, -2, 0)
>>> bno.offsets_accelerometer
(0, 0, 0)
>>> bno.radius_magnetometer
0
>>> 

@tannewt
Copy link
Member

tannewt commented Mar 26, 2019

@katlings Still interested in wrapping this up?

Scott Irwin and others added 2 commits May 6, 2019 15:53
…pose calibration and radii as r/w properties on BNO055
expose calibration and radii as r/w properties on BNO055
@sjirwin
Copy link

sjirwin commented May 6, 2019

The code is working from a python point of view, but the board does not always accept the new calibration value

>>> import board, busio
>>> import adafruit_bno055
>>> i2c = busio.I2C(board.SCL, board.SDA)
>>> sensor = adafruit_bno055.BNO055(i2c)
>>> sensor.radius_accelerometer, sensor.radius_magnetometer
((1000,), (0,))
>>> sensor.radius_accelerometer = 999,
>>> sensor.radius_accelerometer, sensor.radius_magnetometer
((1000,), (0,))
>>> sensor.radius_accelerometer = 999,
>>> sensor.radius_accelerometer, sensor.radius_magnetometer
((999,), (0,))
>>> sensor.radius_accelerometer, sensor.radius_magnetometer
((1000,), (0,))
>>> sensor.radius_accelerometer, sensor.radius_magnetometer
((1000,), (43,))

@tannewt
Copy link
Member

tannewt commented May 9, 2019

This ready to go?

@caternuson
Copy link
Contributor

Are 1D tuples the only way to get this to work? I'm concerned that subtle little , that's required when dealing with 1D tuples could end up being a source of confusion.

@sjirwin
Copy link

sjirwin commented May 10, 2019

I am pretty sure we can make that more natural. Will look at it this weekend and submit an updated PR.

Any advice on how to submit the PR directly and avoid @katlings having to broker this through her fork?

@tannewt
Copy link
Member

tannewt commented May 10, 2019

@sjirwin you can make a new PR to this repo and we can close this one.

@evaherrada
Copy link
Collaborator

Seems this PR was left open by mistake.

@evaherrada evaherrada closed this May 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants