input: correct gyro orientation drift with the accelerometer #899
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!899
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/899/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@ew-sudo on GitHub · 2026-09-28 21:53 UTC · original
ew-sudo:gyro-drift-fix→main· +60 −13 · 1 files · 2 commits · merged 2026-09-30 18:40 UTCProblem
The pad orientation (
orientation_x/y/z/w) is built by integrating the gyro alone. Every real gyro has a small bias, so the integrated orientation slowly drifts. In practice, gyro-controlled sections in Astro's Playroom (PPSA01325) get worse over time: when the controller is held still the camera or object slowly slides to one side, and you have to hold the pad tilted to keep it straight. A large rotation of the pad sometimes "fixes" it temporarily, then the drift comes back.Change
A Mahony-style complementary filter is added in
GameController::Motion. When the pad is roughly at rest (|accel| between 0.8 and 1.2 G), the angular velocity used for integration is nudged so that the predicted "up" converges on the measured gravity direction. This corrects the tilt (pitch/roll) drift. The heading (yaw) cannot be observed from gravity and is left unchanged.ResetOrientation/ motion sensor state changes. A reset done while the pad is tilted keeps its meaning.KYTY_GYRO_CORRECTIONsets the gain (default 1.0).KYTY_GYRO_CORRECTION=0restores the previous behavior (pure gyro integration).Testing
controller.cpp. With a deliberately large gyro bias, the tilt error over 60 s went from 74–90° without correction to about 1–2° with it, for both a still and a moving pad, and for a reset done while the pad is tilted.@coderabbitai[bot] on GitHub · 2026-09-28 21:58 UTC · original · edited 2026-09-28 22:01 UTC
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
📝 Walkthrough
Walkthrough
GameController now captures a normalized gravity reference when acceleration magnitude is between 0.9 and 1.1 G. When the correction gain is positive, the reference is valid, and acceleration magnitude is between 0.8 and 1.2 G, gyro integration uses a corrected rate while reported gyro samples remain raw. KYTY_GYRO_CORRECTION sets the gain and defaults to 1.0. Motion-state changes and orientation resets invalidate the reference. The 100,000-microsecond integration interval limit remains.
Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: 🟡 Moderate · up to
5ccb4Gyro tilt correction can be wrong after switching controllers or after a bad first reading, which would make motion controls drift or tilt. The scope is limited to motion input, and setting KYTY_GYRO_CORRECTION=0 restores the previous behavior. Fix the controller-switch reset before merging.
Security Architecture Review
Security architecture risk: 🔵 Low · up to
5ccb4Switching controllers can leave tilt correction using a reference captured from the previous controller. This can affect reported orientation, but no security-sensitive use of that orientation was established.
Retained concerns
Security review details
Security Blast Radius
Trust Boundaries and Controls
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@ -135,1 +135,4 @@uint64_t m_gyro_time = 0;// Accelerometer gravity direction in the reset frame.std::array<float, 3> m_up_reference {0.0f, 1.0f, 0.0f};bool m_up_reference_valid = false;@coderabbitai[bot] on GitHub · 2026-09-28 21:58 UTC · original · edited 2026-09-30 18:40 UTC
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 1677
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 7039
Clear the gravity reference when the active controller changes.
GameController::CheckActivechangesm_active_idbut does not clearm_up_reference_valid. It also does not resetm_state.orientationon this path. After a controller switch, the next gyro event can therefore correct the new controller using the previous controller's gravity reference.Clear the flag when
CheckActivechanges the active connection state:Suggested fix
✅ Addressed in commit
9315c75@ -450,0 +480,4 @@const Vec3 up {m_state.accel[0] / magnitude, m_state.accel[1] / magnitude,m_state.accel[2] / magnitude};m_up_reference = QuatRotate(m_state.orientation, up);m_up_reference_valid = true;@coderabbitai[bot] on GitHub · 2026-09-28 21:58 UTC · original
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 41638
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 8566
Require a stable interval before latching
m_up_reference.The sample
{0.4, 1.0, 0.0}has magnitude about1.077 G, so it passes the current check while indicating about 22° of lateral tilt. AfterResetOrientation()clearsm_up_reference_valid, this sample can become the reference. With the default positive correction gain, later gyro updates can drive the orientation toward that incorrect reference until another reset.Require a stable sample interval with low gyro activity before capturing the reference.
@coderabbitai[bot] on GitHub · 2026-09-28 21:58 UTC · original
On
src/libs/controller.cppline 508 (outdated):🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 14954
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 31530
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 5191
🌐 Web query:
official SDL3 documentation SDL_EVENT_GAMEPAD_SENSOR_UPDATE gamepad sensor update event accelerometer gyro💡 Result:
Ignore stale accelerometer samples during gyro correction.
SDL delivers accelerometer and gyro updates as separate sensor events.
Motionstores acceleration without a timestamp, then uses the stored value on every gyro event. If accelerometer delivery stops while gyro delivery continues, correction can bias orientation toward an obsolete direction.Record the latest accelerometer timestamp and skip correction after the sample becomes stale.
Suggested fix
@coderabbitai[bot] on GitHub · 2026-09-28 21:58 UTC · original · reviewed
Actionable comments posted: 3
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
4e8611d6-8821-42df-9388-0ad8a0f14d5d📥 Commits
Reviewing files that changed from the base of the PR and between
62a0222e24and5ccb400d13.📒 Files selected for processing (1)
src/libs/controller.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.