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

+31
View File
@@ -251,6 +251,14 @@ static const char *const text_en[UI_TEXT_COUNT] = {
[UI_MENU_CHANNELS_AUTO] = "Automatic", [UI_MENU_CHANNELS_STEREO] = "Stereo", [UI_MENU_CHANNELS_51] = "5.1 surround",
[UI_MENU_MOTION] = "Motion controls", [UI_MENU_MOTION_DESC] = "Aiming with the controller's gyroscope.",
[UI_MENU_GYRO_CALIBRATION] = "Gyro bias calibration",
[UI_MENU_GYRO_CALIBRATION_DESC] = "Subtracts the controller's resting drift, measured with the calibrate row "
"below. The measured values stay saved when this is off.",
[UI_MENU_GYRO_CALIBRATE] = "Calibrate gyro",
[UI_MENU_GYRO_CALIBRATE_DESC] = "Estimate and remove the gyroscope's resting drift. Hold the controller still.",
[UI_MENU_GYRO_CALIBRATE_WAIT] = "Calibrating...",
[UI_MENU_GYRO_CALIBRATED] = "%s, calibrated",
[UI_MENU_GYRO_CALIBRATE_FAILED] = "Calibration failed",
[UI_MENU_RUMBLE_STRENGTH] = "Vibration strength",
[UI_MENU_RUMBLE_STRENGTH_DESC] = "How strong the controller vibrates.",
[UI_MENU_RUMBLE_CURVE] = "Vibration curve",
@@ -782,6 +790,14 @@ static const char *const text_ru[UI_TEXT_COUNT] = {
[UI_MENU_CHANNELS_AUTO] = "Автоматически", [UI_MENU_CHANNELS_STEREO] = "Стерео", [UI_MENU_CHANNELS_51] = "Объёмный 5.1",
[UI_MENU_MOTION] = "Управление движением", [UI_MENU_MOTION_DESC] = "Прицеливание гироскопом контроллера.",
[UI_MENU_GYRO_CALIBRATION] = "Калибровка смещения гироскопа",
[UI_MENU_GYRO_CALIBRATION_DESC] = "Убирает покоящееся смещение контроллера, измеренное строкой калибровки ниже. "
"Измеренные значения сохраняются и при выключенном состоянии.",
[UI_MENU_GYRO_CALIBRATE] = "Калибровка гироскопа",
[UI_MENU_GYRO_CALIBRATE_DESC] = "Оценить и убрать покоящееся смещение гироскопа. Держите контроллер неподвижно.",
[UI_MENU_GYRO_CALIBRATE_WAIT] = "Калибровка...",
[UI_MENU_GYRO_CALIBRATED] = "%s, откалиброван",
[UI_MENU_GYRO_CALIBRATE_FAILED] = "Калибровка не удалась",
[UI_MENU_RUMBLE_STRENGTH] = "Сила вибрации",
[UI_MENU_RUMBLE_STRENGTH_DESC] = "Насколько сильно вибрирует контроллер.",
[UI_MENU_RUMBLE_CURVE] = "Кривая вибрации",
@@ -1232,6 +1248,14 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = {
[UI_MENU_CHANNELS_AUTO] = "Automático", [UI_MENU_CHANNELS_STEREO] = "Estéreo", [UI_MENU_CHANNELS_51] = "Surround 5.1",
[UI_MENU_MOTION] = "Controles de movimento", [UI_MENU_MOTION_DESC] = "Mirar com o giroscópio do controle.",
[UI_MENU_GYRO_CALIBRATION] = "Calibração do desvio do giroscópio",
[UI_MENU_GYRO_CALIBRATION_DESC] = "Remove o desvio de repouso do controle, medido pela linha de calibração "
"abaixo. Os valores medidos ficam salvos mesmo com isto desligado.",
[UI_MENU_GYRO_CALIBRATE] = "Calibrar giroscópio",
[UI_MENU_GYRO_CALIBRATE_DESC] = "Estimar e remover o desvio de repouso do giroscópio. Segure o controle parado.",
[UI_MENU_GYRO_CALIBRATE_WAIT] = "Calibrando...",
[UI_MENU_GYRO_CALIBRATED] = "%s, calibrado",
[UI_MENU_GYRO_CALIBRATE_FAILED] = "Falha na calibração",
[UI_MENU_RUMBLE_STRENGTH] = "Intensidade da vibração",
[UI_MENU_RUMBLE_STRENGTH_DESC] = "Quão forte o controle vibra.",
[UI_MENU_RUMBLE_CURVE] = "Curva da vibração",
@@ -1845,6 +1869,13 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = {
[UI_MENU_CHANNELS_AUTO] = "自动", [UI_MENU_CHANNELS_STEREO] = "立体声", [UI_MENU_CHANNELS_51] = "5.1 环绕声",
[UI_MENU_MOTION] = "体感操作",
[UI_MENU_MOTION_DESC] = "用手柄的陀螺仪瞄准。",
[UI_MENU_GYRO_CALIBRATION] = "陀螺仪偏置校准",
[UI_MENU_GYRO_CALIBRATION_DESC] = "减去下面校准行测得的手柄静止偏移。关闭时测量值仍然保留。",
[UI_MENU_GYRO_CALIBRATE] = "校准陀螺仪",
[UI_MENU_GYRO_CALIBRATE_DESC] = "估计并消除陀螺仪的静止偏移。请保持手柄静止。",
[UI_MENU_GYRO_CALIBRATE_WAIT] = "校准中...",
[UI_MENU_GYRO_CALIBRATED] = "%s,已校准",
[UI_MENU_GYRO_CALIBRATE_FAILED] = "校准失败",
[UI_MENU_RUMBLE_STRENGTH] = "振动强度",
[UI_MENU_RUMBLE_STRENGTH_DESC] = "手柄振动的强弱。",
[UI_MENU_RUMBLE_CURVE] = "振动曲线",