Skip to content

Fix accelerometer staleness and IMU list aliasing, free all servos on reset, GNU GPL v3 License#105

Open
SaintSampo wants to merge 4 commits into
mainfrom
issueFixes
Open

Fix accelerometer staleness and IMU list aliasing, free all servos on reset, GNU GPL v3 License#105
SaintSampo wants to merge 4 commits into
mainfrom
issueFixes

Conversation

@SaintSampo

@SaintSampo SaintSampo commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Accelerometer returns stale values

Fixes the "accelerometer requests seem to return accumulated value" report. Reads looked correct in a loop but returned old data when called one-off.

Block Data Update is enabled, so the LSM6DSO's output registers don't refresh until both bytes of the previous sample have been read. The timer callback only drained the gyro, so the accelerometer registers stayed latched on a sample captured right after the previous read. Read once, turn the robot, read again, and you got the orientation from before you moved it.

The timer now calls _read_acc_gyro(), which burst-reads both sensors every tick, capping staleness at one timer period (~4.8 ms at 208 Hz). BDU stays enabled, so MSB and LSB still come from the same sample and burst reads can't tear.

IMU accessors returned their internal lists

get_acc_rates(), get_gyro_rates() and get_acc_gyro_rates() returned self.irq_v directly, so a caller who stored the result saw it mutate on the next call or asynchronously from the timer callback. They now return copies.

The copy would otherwise have landed in the 208 Hz timer path, so the register read moved into a private _read_acc_gyro() that updates irq_v in place and returns nothing. The timer allocates no more than it did before.

License

Adds GNU GPL v3.

Extend reset_servos in XRPLib/resetbot.py to free any third and fourth servos if the board exposes SERVO_3 or SERVO_4.
Avoid exposing internal irq_v buffers and reduce per-tick allocations. get_acc_x_rate and get_gyro_x_rate now return list copies. Introduced _read_acc_gyro to perform an in-place burst read that updates irq_v without allocating on each timer tick. get_acc_gyro_rates now calls _read_acc_gyro and returns 2D list copies. _update_imu_readings was updated to use _read_acc_gyro. These changes prevent accidental external mutation of internal state and reduce GC pressure in callbacks.

@KalticCodes KalticCodes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For publically facing functions, please use Sphinx-format doc commwnts for auto-generated API docs

Clarified and expanded IMU method docstrings in XRPLib/imu.py to explicitly state return formats and units for get_gyro_rates and get_acc_gyro_rates. Simplified reset_servos in XRPLib/resetbot.py to only free the two default servos (1 and 2) and removed conditional checks for SERVO_3 and SERVO_4. These changes improve documentation clarity and streamline servo reset logic for boards that only expose two servos.
@SaintSampo

Copy link
Copy Markdown
Collaborator Author

Done. I clarified and expanded IMU method docstrings in XRPLib/imu.py

Also removed the servo fix since frank did that in a different PR

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.

2 participants