|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cmath> |
| 4 | +#include <cstdint> |
| 5 | +#include <iostream> |
| 6 | + |
| 7 | +// ===================================================== |
| 8 | +// HLV-RAPS Stability Indicator Upgrade (v3.3.0) |
| 9 | +// ===================================================== |
| 10 | + |
| 11 | +namespace StabilityConfig { |
| 12 | + |
| 13 | +enum class ManeuverClass { |
| 14 | + CRUISE, |
| 15 | + MILD_MANEUVER, |
| 16 | + AGGRESSIVE_MANEUVER |
| 17 | +}; |
| 18 | + |
| 19 | +struct Thresholds { |
| 20 | + double S_u_min; |
| 21 | + double S_u_warn; |
| 22 | + double S_u_rate_limit; |
| 23 | +}; |
| 24 | + |
| 25 | +constexpr Thresholds CRUISE_THRESHOLDS = {0.82, 0.86, 0.015}; |
| 26 | +constexpr Thresholds MILD_THRESHOLDS = {0.78, 0.83, 0.025}; |
| 27 | +constexpr Thresholds AGGRESSIVE_THRESHOLDS = {0.72, 0.78, 0.040}; |
| 28 | + |
| 29 | +inline constexpr Thresholds get_thresholds(ManeuverClass mclass) { |
| 30 | + switch (mclass) { |
| 31 | + case ManeuverClass::MILD_MANEUVER: |
| 32 | + return MILD_THRESHOLDS; |
| 33 | + case ManeuverClass::AGGRESSIVE_MANEUVER: |
| 34 | + return AGGRESSIVE_THRESHOLDS; |
| 35 | + case ManeuverClass::CRUISE: |
| 36 | + default: |
| 37 | + return CRUISE_THRESHOLDS; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +} // namespace StabilityConfig |
| 42 | + |
| 43 | +// ===================================================== |
| 44 | +// DSM Integration |
| 45 | +// ===================================================== |
| 46 | + |
| 47 | +enum class DSMFlagType { |
| 48 | + NONE = 0, |
| 49 | + SU_LOW = 1, |
| 50 | + SU_RATE_VIOLATION = 2, |
| 51 | + SU_HYSTERESIS_TRANSITION = 3 |
| 52 | +}; |
| 53 | + |
| 54 | +struct DSMEvent { |
| 55 | + uint32_t timestamp; |
| 56 | + StabilityConfig::ManeuverClass maneuver_class; |
| 57 | + double S_u; |
| 58 | + double dS_u_dt; |
| 59 | + DSMFlagType flag_type; |
| 60 | +}; |
| 61 | + |
| 62 | +// ===================================================== |
| 63 | +// Stability Indicator Core |
| 64 | +// ===================================================== |
| 65 | + |
| 66 | +class StabilityIndicator { |
| 67 | +public: |
| 68 | + StabilityIndicator() |
| 69 | + : last_S_u_(1.0), |
| 70 | + last_timestamp_(0), |
| 71 | + is_safe_mode_(true), |
| 72 | + initialized_(false) {} |
| 73 | + |
| 74 | + // Pure mathematical computation of S_u |
| 75 | + // S_u = 1 / (1 + phi^2 + chi^3) |
| 76 | + static double compute_Su(double phi, double chi) { |
| 77 | + double phi_sq = phi * phi; |
| 78 | + double chi_cu = chi * chi * chi; |
| 79 | + return 1.0 / (1.0 + phi_sq + chi_cu); |
| 80 | + } |
| 81 | + |
| 82 | + // Logging hook (purely deterministic and side-effect free besides console out for simulation) |
| 83 | + static DSMEvent emit_dsm_event( |
| 84 | + uint32_t timestamp, |
| 85 | + StabilityConfig::ManeuverClass mclass, |
| 86 | + double S_u, |
| 87 | + double dS_u_dt, |
| 88 | + DSMFlagType flag_type) { |
| 89 | + |
| 90 | + DSMEvent event = {timestamp, mclass, S_u, dS_u_dt, flag_type}; |
| 91 | + // In a real system, this might push to a queue. For now, it returns the struct. |
| 92 | + return event; |
| 93 | + } |
| 94 | + |
| 95 | + // Stateful wrapper |
| 96 | + DSMEvent update_stability_state( |
| 97 | + uint32_t timestamp, |
| 98 | + StabilityConfig::ManeuverClass mclass, |
| 99 | + double phi, |
| 100 | + double chi) { |
| 101 | + |
| 102 | + double current_S_u = compute_Su(phi, chi); |
| 103 | + double dS_u_dt = 0.0; |
| 104 | + |
| 105 | + auto thresholds = StabilityConfig::get_thresholds(mclass); |
| 106 | + DSMFlagType flag_out = DSMFlagType::NONE; |
| 107 | + |
| 108 | + if (initialized_ && timestamp > last_timestamp_) { |
| 109 | + double dt = static_cast<double>(timestamp - last_timestamp_); |
| 110 | + dS_u_dt = (current_S_u - last_S_u_) / dt; |
| 111 | + |
| 112 | + if (std::abs(dS_u_dt) > thresholds.S_u_rate_limit) { |
| 113 | + flag_out = DSMFlagType::SU_RATE_VIOLATION; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Hysteresis State Machine |
| 118 | + // EnterSafe: S_u >= S_u_warn |
| 119 | + // ExitSafe: S_u <= S_u_min |
| 120 | + bool transitioned = false; |
| 121 | + if (!is_safe_mode_) { |
| 122 | + if (current_S_u >= thresholds.S_u_warn) { |
| 123 | + is_safe_mode_ = true; |
| 124 | + transitioned = true; |
| 125 | + } |
| 126 | + } else { |
| 127 | + if (current_S_u <= thresholds.S_u_min) { |
| 128 | + is_safe_mode_ = false; |
| 129 | + transitioned = true; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (transitioned && flag_out == DSMFlagType::NONE) { |
| 134 | + flag_out = DSMFlagType::SU_HYSTERESIS_TRANSITION; |
| 135 | + } |
| 136 | + |
| 137 | + if (!is_safe_mode_ && flag_out == DSMFlagType::NONE) { |
| 138 | + // If we are currently unsafe but haven't triggered another flag, |
| 139 | + // we emit SU_LOW to indicate pre-safing boundary violation. |
| 140 | + if (current_S_u <= thresholds.S_u_min) { |
| 141 | + flag_out = DSMFlagType::SU_LOW; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // State update |
| 146 | + last_S_u_ = current_S_u; |
| 147 | + last_timestamp_ = timestamp; |
| 148 | + initialized_ = true; |
| 149 | + |
| 150 | + return emit_dsm_event(timestamp, mclass, current_S_u, dS_u_dt, flag_out); |
| 151 | + } |
| 152 | + |
| 153 | + bool is_safe() const { return is_safe_mode_; } |
| 154 | + |
| 155 | +private: |
| 156 | + double last_S_u_; |
| 157 | + uint32_t last_timestamp_; |
| 158 | + bool is_safe_mode_; |
| 159 | + bool initialized_; |
| 160 | +}; |
0 commit comments