switch: Fix gyro ZRO handling in IMU sensor updates#15860
Open
alexeev wants to merge 2 commits into
Open
Conversation
Collaborator
|
This looks good! Can you please retarget the main branch? We'll cherry-pick this to release-3.4.x after merging. |
added 2 commits
June 22, 2026 10:13
…libration. Prevent reading potentially stale memory. No functional changes.
…IMU data for Switch Controllers.
7b16045 to
84b7408
Compare
Author
|
@slouken, sure, re-targeted to the main and tested. Works as expected:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
This PR fixes gyroscope drift handling for Nintendo Switch controllers by applying the calibrated gyroscope zero-rate offsets when reporting live IMU sensor data.
The change is split into two commits:
LoadIMUCalibration()SendSensorUpdate()1. Preparatory refactor
LoadIMUCalibration()reads factory IMU calibration data first, and then attempts to read user calibration data from SPI flash.The existing user calibration check relies on
pIMUScalestill pointing at memory that is updated by the nextWriteSubcommand()call:This works today because the HID read path uses the same backing buffer, but the sequencing is fragile: the magic value is checked before the pointer is explicitly assigned to the newly returned SPI payload.
The first commit makes this dependency explicit by assigning the user calibration payload pointer before checking the magic value. This is intended as a no-behavior-change cleanup that makes the following bug fix safer and easier to review.
2. Gyroscope offset fix
LoadIMUCalibration()already reads the calibrated gyroscope raw offsets from SPI flash. These values represent the controller's zero-rate offset.Previously, these raw offset values were only used while computing the gyroscope scale factors, and were not retained for live sensor updates. As a result,
SendSensorUpdate()multiplied raw gyroscope samples by the scale factor without first subtracting the calibrated zero-rate offset.This PR stores the calibrated gyroscope offsets and applies the standard IMU conversion:
The fix preserves the existing axis remapping and sign conventions in
SendSensorUpdate().Testing
Tested using
testcontrollerwith a Nintendo Switch Pro Controller over USB and Bluetooth on MacOS Tahoe 26.5.1Before this change, a stationary controller reported a persistent gyroscope bias/drift of roughly
8.5deg/s.After this change, with the controller stationary on a flat surface, the reported gyroscope values hover close to zero.
Existing Issue(s)
No existing GitHub issue found.
Other
I'd appreciate guidance on forward-porting to main.