eldruin / bmi160-rs

Platform-agnostic Rust driver for the BMI160 inertial measurement unit (IMU)
Apache License 2.0
12 stars 11 forks source link

Add more configuration options #5

Open thedevleon opened 1 year ago

thedevleon commented 1 year ago

Coming from Zephyr (and it's BMI160 library), there are two features that seem to be missing that would be really nice to have:

eldruin commented 1 year ago

Sounds like a nice project. I would welcome this in a PR.

avsaase commented 6 months ago

https://github.com/eldruin/bmi160-rs/pull/9 added functions to set the gyro and acc scales and read the scaled sensor values.

I'm now looking into setting the ACC_CONF and GYR_CONF values and I'm looking for ideas for a good API.

The ACC_CONF register has one bit for the undersampling mode, three bits for the bandwidth parameter and four for the ODR. The challenge is that the setting of the undersampling mode affects the meaning of the the bandwidth parameter. When undersampling is disabled, the bandwidth parameter controls the filter mode (normal, OSR2, OSR4). When undersampling is enabled, the bandwidth parameter controls how many samples are averaged (twelve possible values).

I was thinking this relationship could be captured with enums:

struct AccelerometerConfig {
    undersampling_mode: AccelerometerUndersamplingMode,
    output_data_rate: AccelerometerOutputDataRate
}

enum AccelerometerUndersamplingMode {
    Disabled(FilterMode),
    Enabled(AveragedSamples),
}

enum FilterMode {
    Normal,
    OSR2,
    OSR4,
}

enum AveragedSamples {
    One,
    Two,
    Four,
    // etc
}

enum AccelerometerOutputDataRate {
    Odr25Hz,
    Odr50Hz,
    // etc.
}

This feels a little clunky so I'm open to alternatives.

The GYR_CONF register is a bit simpler because it does not have an undersampling mode.

eldruin commented 6 months ago

That is also what I thought of. I could not immediately think of something simpler that does not lead to returning InvalidInputData errors just for the sake of a "simpler" interface. Also, maybe the user code does not feel as clunky.