Skip to content

Imu improvements #35

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

Merged
merged 22 commits into from
Jul 18, 2023
Merged

Imu improvements #35

merged 22 commits into from
Jul 18, 2023

Conversation

sfe-SparkFro
Copy link
Collaborator

A handful of general improvements to the IMU class. Notable changes include:

  • Fix IMU calibration loop to actually get an average for the offset, rather than just the last measurement (num_vals was never incremented)
  • Optimize IMU calibration to only divide once after measurement loop (division takes a long time)
  • Add ODR functions
  • Change default ODR to 208Hz, and default update period to 5ms (close as possible to 208Hz)
  • Change default FS ranges to max (not clipping is more important than fine resolution, but feel free to change if max is too much)
  • Check WHO_AM_I register during initialization (still need to implement something intelligent if it fails)
  • Reset IMU during initialization (clears any previous configuration in case the processor is reset without power cycling the IMU)

Actually increment num_vals so we're using more than just the last measurement
Add short delay before calibration loop to ensure IMU has started measurements
Delay measurements by update_time rather than a magic number
Optimize calibration loop by doing division after the loop finishes
Change ODR to 208Hz for both accelerometer and gyroscope
Change default update_time to 5ms (close as possible to 208Hz)
Change FS range of both sensors to maximum (not clipping is more important than fine resolution here)
Check WHO_AM_I register (should do something intelligent if it fails)
Set RESET and BOOT bits to reset sensor during initialization
Function calls were overriding previously set values
Should really set the start time *after* waiting for the sensor to get going
@sfe-SparkFro
Copy link
Collaborator Author

Hold on, don't accept yet, just found a bug

@sfe-SparkFro
Copy link
Collaborator Author

Fixed!

@ksiegall
Copy link
Member

Another comment for a pretty big improvement to this code; now that we're providing a way to change the data rates, we probably don't just want to be assuming that the imu update time is 5ms (which is an approximation all on it's own). What you can do is change the update timer to use frequency instead of period (set after calibration) and then pass in the currently set ODR for the gyro, which would eliminate any drift that would have resulted from using the wrong frequency. That would probably be a good change to make before this gets merged in

@sfe-SparkFro
Copy link
Collaborator Author

Good point, I can change the timer to use frequency instead of period (didn't even realize that was an option!)

@fgrossman
Copy link
Collaborator

fgrossman commented Jul 16, 2023 via email

Add new imu_defs.py with the following:

Both possible I2C addresses
Register address definitions
Struct layout definitions with bit fields to match registers
Dictionaries with possible ODR and FS values
Note - to reduce file size, only registers that are used have been included. It should be easy to expand these as needed

Add the following member functions:

is_connected()
reset()
_set_bdu()
_set_if_inc()

Refactor imu.py to use these new features
Add 'Hz' and 'dps' to rate function calls
Fix register assignment in gyro_rate()
Timer now uses frequency instead of period
Frequency is now calculated when gyro_rate() is called
Remove update_time parameter from calibrate()
Add _start_timer() and stop_timer()
@sfe-SparkFro
Copy link
Collaborator Author

sfe-SparkFro commented Jul 16, 2023

I like the idea of blink codes in main.py!

I'm cleaning up some of the IMU code right now. Do we really need power()? There's some edge cases that could occur with the rate functions (eg. if a user calls acc_rate(rate_1), then power(False), then acc_rate(rate_2), then power(True), the rate will go back to rate_1). Obviously we could handle the edge cases, but I'm not really sure what the use case is for including power() with the XRP. Everything else takes up way more power, and if the user really wants to turn the IMU off, they can just send '0Hz' to the rate functions.

Edit: I've removed it for now. If we want it back, I can revert the commit

Remove unused member variables
Add comments to __init__()
Add constants for mg and mpds per least significant bit
It has some edge cases with the rate functions. We could handle them, but that's more effort than just removing power(). It's not really needed for the XRP, but users can still reduce power consumption by sending '0Hz' to the rate functions.
Bits and bytes have better naming parity between each other
Burst reading speeds up data transmission a lot, up to 3x faster than reading single bytes when reading lots of registers (eg. reading 12 bytes at 400kHz should be almost 500us faster)
Also best to minimize the amount of time spent hogging the I2C bus in case something else needs it
Needs to be defined before calling gyro_rate()
Forgot to change from gyro to accel when copying, oops!
Need to index into bytearray rather than reassigning the variable
mg/LSB and dps/LSB are defined for the minimum range of each sensor, so need to scale when using different ranges
@sfe-SparkFro
Copy link
Collaborator Author

Ok, finally got around to testing this. Had to fix a couple mistakes, but should be all working now!

Copy link
Member

@ksiegall ksiegall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few minor nitpicks, everything looks pretty good otherwise

Also add _reset_member_variables() to reduce duplicate code in reset() and __init__()
@fgrossman
Copy link
Collaborator

fgrossman commented Jul 18, 2023 via email

Copy link
Member

@ksiegall ksiegall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All functionality confirmed
Review complete

@ksiegall ksiegall merged commit e8fdc08 into Open-STEM:main Jul 18, 2023
@sfe-SparkFro sfe-SparkFro deleted the imu_improvements branch July 18, 2023 20:31
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.

3 participants