| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | |
| 8 | #include "Common/Common.h" |
| 9 | #include "Common/MathUtil.h" |
| 10 | |
| 11 | #include "InputCommon/ControlReference/ControlReference.h" |
| 12 | #include "InputCommon/ControllerEmu/Control/Control.h" |
| 13 | |
| 14 | namespace ControllerEmu |
| 15 | { |
| 16 | // Maximum period for calculating an average stable value. |
| 17 | // Just to prevent failures due to timer overflow. |
| 18 | static constexpr auto MAXIMUM_CALIBRATION_DURATION = std::chrono::hours(1); |
| 19 | |
| 20 | // If calibration updates do not happen at this rate, restart calibration period. |
| 21 | // This prevents calibration across periods of no regular updates. (e.g. between game sessions) |
| 22 | // This is made slightly lower than the UI update frequency of 30. |
| 23 | static constexpr auto WORST_ACCEPTABLE_CALIBRATION_UPDATE_FREQUENCY = 25; |
| 24 | |
| 25 | IMUGyroscope::IMUGyroscope(std::string name_, std::string ui_name_) |
| 26 | : ControlGroup(std::move(name_), std::move(ui_name_), GroupType::IMUGyroscope) |
| 27 | { |
| 28 | AddInput(Translatability::Translate, _trans("Pitch Up")); |
| 29 | AddInput(Translatability::Translate, _trans("Pitch Down")); |
| 30 | AddInput(Translatability::Translate, _trans("Roll Left")); |
| 31 | AddInput(Translatability::Translate, _trans("Roll Right")); |
| 32 | AddInput(Translatability::Translate, _trans("Yaw Left")); |
| 33 | AddInput(Translatability::Translate, _trans("Yaw Right")); |
| 34 | |
| 35 | AddSetting(&m_deadzone_setting, |
| 36 | {_trans("Dead Zone"), |
| 37 | // i18n: "°/s" is the symbol for degrees (angular measurement) divided by seconds. |
| 38 | _trans("°/s"), |
| 39 | // i18n: Refers to the dead-zone setting of gyroscope input. |
| 40 | _trans("Angular velocity to ignore and remap.")}, |
| 41 | 2, 0, 180); |
| 42 | |
| 43 | AddSetting(&m_calibration_period_setting, |
| 44 | {_trans("Calibration Period"), |
| 45 | // i18n: "s" is the symbol for seconds. |
| 46 | _trans("s"), |
| 47 | // i18n: Refers to the "Calibration" setting of gyroscope input. |
| 48 | _trans("Time period of stable input to trigger calibration. (zero to disable)")}, |
| 49 | 3, 0, 30); |
| 50 | } |
| 51 | |
| 52 | void IMUGyroscope::RestartCalibration() |
| 53 | { |
| 54 | m_calibration_period_start = Clock::now(); |
| 55 | m_running_calibration.Clear(); |
| 56 | } |
| 57 | |
| 58 | void IMUGyroscope::UpdateCalibration(const StateData& state) |
| 59 | { |
| 60 | const auto now = Clock::now(); |
| 61 | const auto calibration_period = m_calibration_period_setting.GetValue(); |
| 62 | |
| 63 | // If calibration time is zero. User is choosing to not calibrate. |
| 64 | if (!calibration_period) |
| 65 | { |
| 66 | // Set calibration to zero. |
| 67 | m_calibration = {}; |
| 68 | RestartCalibration(); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | // If there is no running calibration a new gyro was just mapped or calibration was just enabled, |
| 73 | // apply the current state as calibration, it's often better than zeros. |
| 74 | if (!m_running_calibration.Count()) |
| 75 | { |
| 76 | m_calibration = state; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | const auto calibration_freq = |
| 81 | m_running_calibration.Count() / |
| 82 | std::chrono::duration_cast<std::chrono::duration<double>>(now - m_calibration_period_start) |
| 83 | .count(); |
| 84 | |
| 85 | const auto potential_calibration = m_running_calibration.Mean(); |
| 86 | const auto current_difference = state - potential_calibration; |
| 87 | const auto deadzone = GetDeadzone(); |
| 88 | |
| 89 | // Check for required calibration update frequency |
| 90 | // and if current data is within deadzone distance of mean stable value. |
| 91 | if (calibration_freq < WORST_ACCEPTABLE_CALIBRATION_UPDATE_FREQUENCY || |
| 92 | std::ranges::any_of(current_difference.data, |
| 93 | [&](auto c) { return std::abs(c) > deadzone; })) |
| 94 | { |
| 95 | RestartCalibration(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Update running mean stable value. |
| 100 | m_running_calibration.Push(state); |
| 101 | |
| 102 | // Apply calibration after configured time. |
| 103 | const auto calibration_duration = now - m_calibration_period_start; |
| 104 | if (calibration_duration >= std::chrono::duration<double>(calibration_period)) |
| 105 | { |
| 106 | m_calibration = m_running_calibration.Mean(); |
| 107 | |
| 108 | if (calibration_duration >= MAXIMUM_CALIBRATION_DURATION) |
| 109 | { |
| 110 | RestartCalibration(); |
| 111 | m_running_calibration.Push(m_calibration); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | auto IMUGyroscope::GetRawState() const -> StateData |
| 117 | { |
| 118 | return StateData(controls[1]->GetState() - controls[0]->GetState(), |
| 119 | controls[2]->GetState() - controls[3]->GetState(), |
| 120 | controls[4]->GetState() - controls[5]->GetState()); |
| 121 | } |
| 122 | |
| 123 | bool IMUGyroscope::AreInputsBound() const |
| 124 | { |
| 125 | return std::ranges::any_of( |
| 126 | controls, [](const auto& control) { return control->control_ref->BoundCount() > 0; }); |
| 127 | } |
| 128 | |
| 129 | bool IMUGyroscope::CanCalibrate() const |
| 130 | { |
| 131 | // If the input gate is disabled, miscalibration to zero values would occur. |
| 132 | return ControlReference::GetInputGate(); |
| 133 | } |
| 134 | |
| 135 | std::optional<IMUGyroscope::StateData> IMUGyroscope::GetState(bool update) |
| 136 | { |
| 137 | if (!AreInputsBound()) |
| 138 | { |
| 139 | if (update) |
| 140 | { |
| 141 | // Set calibration to zero. |
| 142 | m_calibration = {}; |
| 143 | RestartCalibration(); |
| 144 | } |
| 145 | return std::nullopt; |
| 146 | } |
| 147 | |
| 148 | auto state = GetRawState(); |
| 149 | |
| 150 | // Alternatively we could open the control gate around GetRawState() while calibrating, |
| 151 | // but that would imply background input would temporarily be treated differently for our controls |
| 152 | if (update && CanCalibrate()) |
| 153 | UpdateCalibration(state); |
| 154 | |
| 155 | state -= m_calibration; |
| 156 | |
| 157 | // Apply "deadzone". |
| 158 | for (auto& c : state.data) |
| 159 | c *= std::abs(c) > GetDeadzone(); |
| 160 | |
| 161 | return state; |
| 162 | } |
| 163 | |
| 164 | ControlState IMUGyroscope::GetDeadzone() const |
| 165 | { |
| 166 | return m_deadzone_setting.GetValue() / 360 * MathUtil::TAU; |
| 167 | } |
| 168 | |
| 169 | bool IMUGyroscope::IsCalibrating() const |
| 170 | { |
| 171 | const auto calibration_period = m_calibration_period_setting.GetValue(); |
| 172 | return calibration_period && (Clock::now() - m_calibration_period_start) >= |
| 173 | std::chrono::duration<double>(calibration_period); |
| 174 | } |
| 175 | |
| 176 | } // namespace ControllerEmu |