Branch data Line data Source code
1 : : #include "FFBEngine.h"
2 : : #include "Logger.h"
3 : : #include <cmath>
4 : : #include <algorithm>
5 : :
6 : : // ---------------------------------------------------------------------------
7 : : // Steering & Wheel Mechanics methods have been moved from FFBEngine.cpp.
8 : : // This includes the Soft Lock logic which prevents the wheel from rotating
9 : : // beyond the car's physical steering rack limits.
10 : : // ---------------------------------------------------------------------------
11 : :
12 : : // Helper: Calculate Soft Lock (v0.7.61 - Issue #117)
13 : : // Provides a progressive spring-damping force when the wheel exceeds 100% lock.
14 : 15838 : void FFBEngine::calculate_soft_lock(const TelemInfoV01* data, FFBCalculationContext& ctx) {
15 : 15838 : ctx.soft_lock_force = 0.0;
16 [ + + ]: 15838 : if (!m_soft_lock_enabled) return;
17 : :
18 : 15830 : double steer = data->mUnfilteredSteering;
19 [ + + ]: 15830 : if (!std::isfinite(steer)) return;
20 : :
21 : 15828 : double abs_steer = std::abs(steer);
22 [ + + ]: 15828 : if (abs_steer > 1.0) {
23 [ + + ]: 132 : if (!m_safety.was_soft_locked) {
24 [ + - + - ]: 13 : Logger::Get().LogFile("[Safety] Soft Lock Engaged: Steering %.1f%%", steer * 100.0);
25 : 13 : m_safety.was_soft_locked = true;
26 : : }
27 : :
28 : 132 : double excess = abs_steer - 1.0;
29 [ + + ]: 132 : double sign = (steer > 0.0) ? 1.0 : -1.0;
30 : :
31 : : // NEW: Steep Spring Ramp reaching hardware max torque quickly.
32 : : // This ensures the soft lock feels like a solid wall that doesn't "give"
33 : : // under pressure, even at zero velocity.
34 : : // At stiffness 20 (default), reaches 100% force at 0.25% excess.
35 : : // At stiffness 100 (max), reaches 100% force at 0.05% excess.
36 : 132 : double stiffness = (double)(std::max)(1.0f, m_soft_lock_stiffness);
37 : 132 : double excess_for_max = 5.0 / (stiffness * 100.0);
38 : 132 : double spring_nm = (std::min)(1.0, excess / excess_for_max) * (double)m_wheelbase_max_nm * 2.0;
39 : :
40 : : // Damping Force: opposes movement to prevent bouncing.
41 : : // Scaled by hardware torque to remain relevant across all wheelbases.
42 : 132 : double damping_nm = m_steering_velocity_smoothed * m_soft_lock_damping * (double)m_wheelbase_max_nm * 0.1;
43 : 132 : damping_nm = std::clamp(damping_nm, -(double)m_wheelbase_max_nm * 0.5, (double)m_wheelbase_max_nm * 0.5);
44 : :
45 : : // Total Soft Lock force (opposing the steering direction)
46 : 132 : ctx.soft_lock_force = -(spring_nm * sign + damping_nm);
47 : :
48 [ + - + + : 132 : if (std::abs(ctx.soft_lock_force) > 5.0 && !m_safety.soft_lock_significant) {
+ + ]
49 [ + - + - ]: 13 : Logger::Get().LogFile("[Safety] Soft Lock Significant Influence: %.1f Nm", ctx.soft_lock_force);
50 : 13 : m_safety.soft_lock_significant = true;
51 [ - + ]: 119 : } else if (std::abs(ctx.soft_lock_force) < 4.0) {
52 : 0 : m_safety.soft_lock_significant = false;
53 : : }
54 : : } else {
55 [ + + ]: 15696 : if (m_safety.was_soft_locked) {
56 : 1 : Logger::Get().LogFile("[Safety] Soft Lock Disengaged");
57 : 1 : m_safety.was_soft_locked = false;
58 : : }
59 : 15696 : m_safety.soft_lock_significant = false;
60 : : }
61 : : }
|