Add gyro bias calibration for the six-axis sensor

The yaw axis has no gravity reference, so it drifts with the gyro's
manufacturing bias. Two controllers of the same model can drift in
opposite directions, and the sensor fusion had no way to estimate or
remove the offset: aiming with motion turned slowly on its own and
needed constant correction.

A Calibrate gyro row on the Controls tab collects 250 gyro samples
(about a second) while the controller is held still, averages them per
axis into a bias, subtracts that bias from every sample before the
fusion integrates it, and restarts the angle so the estimate starts
clean. Collection has no motion threshold: a controller whose bias
exceeds any absolute threshold would otherwise never finish, which is
how the first version of this stalled with no way out. Five seconds
without the expected samples ends the attempt, from the sample path and
from the menu alike, so a sensor thread that stops feeding it is
reported instead of leaving the row on "calibrating". The first raw
sample and the final bias are logged, so a report carries the numbers
that were measured.

The row's value says where the controller stands: calibrating,
"<controller>, calibrated" once it is done or when the bias was loaded
from totk.ini at start, and "calibration failed" when an attempt timed
out with no bias in effect. A controller change cancels a calibration
that is in progress and clears the bias and the label, so neither is
carried over to the new controller.

The behaviour sits behind gyro_calibration (the Controls tab's "Gyro
bias calibration", default on, live): off, the fusion takes the raw
gyro, an attempt in progress is dropped, and nothing is loaded at
start, while the saved keys stay in totk.ini so turning it back on
restores the last calibration.

The bias and the controller's name go to totk.ini when calibration
completes, outside the fusion's lock, and are loaded at the next start
only when the connected controller has the same name.

Tested with a Switch Pro Controller on the 1.4.3 build: the log shows a
z-axis bias of 0.139 rad/s (8 degrees per second), calibration finishes
in about a second, the yaw drift is gone, and the row reports each state.
This commit is contained in:
palxex committed 2026-11-15 02:26:30 +08:00
1 parent 3858ad4b51
commit dc13b194ce
8 files changed
+343 -5

No files matched your search

+12
View File
@@ -343,6 +343,18 @@
[input]
# Gamepad motion controls (0 = off) and vibration (0 = off).
# motion = 1
# Gyro bias calibration (1 = on): the Controls tab's "Calibrate gyro" row measures the controller's resting drift and
# subtracts it from the gyro readings, which removes the yaw drift. Off, the fusion takes the raw gyro, an attempt in
# progress is dropped and nothing is loaded at start; the four keys below stay in the file, so turning it back on
# restores the last calibration.
# gyro_calibration = 1
# The measured bias and the controller it was measured from, written by the game when calibration finishes; they are
# not meant to be typed. The bias is loaded at the next start only when gyro_bias_pad names the controller that is
# connected, so calibrate each controller separately: another one starts with no bias until it is calibrated.
# gyro_bias_x = 0
# gyro_bias_y = 0
# gyro_bias_z = 0
# gyro_bias_pad = Nintendo Switch Pro Controller
# rumble = 1
# Rumble strength in percent (0 to 300; 0 = off) and the exponent on the amplitude (0.2 to 1.5; lower = weak pulses
# stronger, 1 = linear). rumble_triggers = 1 also drives the impulse triggers of an Xbox pad.