Branch data Line data Source code
1 : : #ifndef FFBSAFETYMONITOR_H
2 : : #define FFBSAFETYMONITOR_H
3 : :
4 : : #include <cmath>
5 : : #include <algorithm>
6 : : #include <cstring>
7 : : #include "io/lmu_sm_interface/InternalsPluginWrapper.h"
8 : : #include "logging/Logger.h"
9 : : #include "utils/StringUtils.h"
10 : :
11 : : class FFBSafetyMonitor {
12 : : public:
13 : : static constexpr float SAFETY_SLEW_NORMAL = 1000.0f;
14 : : static constexpr float SAFETY_SLEW_RESTRICTED = 100.0f;
15 : :
16 : 473 : FFBSafetyMonitor() = default;
17 : :
18 : : // FFB Safety Settings
19 : : float m_safety_window_duration = 0.0f;
20 : : float m_safety_gain_reduction = 0.3f;
21 : : float m_safety_smoothing_tau = 0.2f;
22 : : float m_spike_detection_threshold = 500.0f;
23 : : float m_immediate_spike_threshold = 1500.0f;
24 : : float m_safety_slew_full_scale_time_s = 1.0f;
25 : : bool m_stutter_safety_enabled = false;
26 : : float m_stutter_threshold = 1.5f;
27 : :
28 : : // API methods for FFBEngine
29 : : double GetSafetyTimer() const { return safety_timer; }
30 : : void ClearSafetySmoothedForce() { safety_smoothed_force = 0.0; }
31 : 16 : void SetSafetySmoothedForce(double f) { safety_smoothed_force = f; }
32 : 473 : void SetTimePtr(const double* ptr) { m_time_ptr = ptr; }
33 : :
34 : 31654 : bool GetLastAllowed() const { return last_allowed; }
35 : 15838 : void SetLastAllowed(bool val) { last_allowed = val; }
36 : 16116 : signed char GetLastControl() const { return last_mControl; }
37 : 285 : void SetLastControl(signed char val) { last_mControl = val; }
38 : :
39 : : bool IsFFBAllowed(const VehicleScoringInfoV01& scoring, unsigned char gamePhase) const;
40 : :
41 : : void TriggerSafetyWindow(const char* reason, double now = -1.0);
42 : : double ApplySafetySlew(double target_force, double current_output_force, double dt, bool restricted, double now = -1.0);
43 : :
44 : : double ProcessSafetyMitigation(double norm_force, double dt);
45 : : void UpdateTockDetection(double steering, double norm_force, double dt);
46 : :
47 : : // Legacy 3-arg version for tests (stateful)
48 : : // Primarily for standalone testing where a real time source is not available.
49 : : [[nodiscard]] double ApplySafetySlew(double target_force, double dt, bool restricted);
50 : :
51 : : // Internal state (Kept public for legacy test compatibility as per Approach A)
52 : : signed char last_mControl = -2;
53 : : double safety_timer = 0.0;
54 : : double safety_smoothed_force = 0.0;
55 : : bool safety_is_seeded = false;
56 : : int spike_counter = 0;
57 : : double tock_timer = 0.0;
58 : : double last_tock_log_time = -999.0;
59 : : double last_reset_log_time = -999.0;
60 : : char last_reset_reason[64] = "";
61 : : double last_massive_spike_log_time = -999.0;
62 : : double last_high_spike_log_time = -999.0;
63 : : bool last_allowed = true;
64 : : double m_last_now = 0.0;
65 : : const double* m_time_ptr = nullptr;
66 : :
67 : : // Soft Lock State
68 : : bool was_soft_locked = false;
69 : : bool soft_lock_significant = false;
70 : : };
71 : :
72 : : #endif // FFBSAFETYMONITOR_H
|