feat(gui,firmware): configurable CRSF gear speed limits and responsive GPUI layout

This commit is contained in:
bleack committed 2026-11-22 13:12:04 +01:00
1 parent febe4d5d5e
commit 426ab7c7d6
6 files changed
+578 -34

No files matched your search

+112 -14
View File
@@ -58,6 +58,46 @@ const GAZ_AVANT_MAX_PAR_VITESSE: [u16; 3] = [
const GAZ_ARRIERE_MIN: u16 = 905;
const GAZ_ARRIERE_MAX: u16 = 800;
/// Bornes CRSF configurables de la boîte séquentielle (marche avant 1/2/3 et marche arrière R).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GearSpeedLimits {
pub forward_min: u16,
pub forward_max: [u16; 3],
pub reverse_min: u16,
pub reverse_max: u16,
}
impl Default for GearSpeedLimits {
fn default() -> Self {
Self {
forward_min: GAZ_AVANT_MIN,
forward_max: GAZ_AVANT_MAX_PAR_VITESSE,
reverse_min: GAZ_ARRIERE_MIN,
reverse_max: GAZ_ARRIERE_MAX,
}
}
}
impl GearSpeedLimits {
/// Contraint chaque valeur dans les bornes CRSF valides et cohérentes.
pub fn sanitized(self) -> Self {
let forward_min = self.forward_min.clamp(CRSF_CHANNEL_MID, CRSF_CHANNEL_MAX);
let forward_max = [
self.forward_max[0].clamp(forward_min, CRSF_CHANNEL_MAX),
self.forward_max[1].clamp(forward_min, CRSF_CHANNEL_MAX),
self.forward_max[2].clamp(forward_min, CRSF_CHANNEL_MAX),
];
let reverse_min = self.reverse_min.clamp(CRSF_CHANNEL_MIN, CRSF_CHANNEL_MID);
let reverse_max = self.reverse_max.clamp(CRSF_CHANNEL_MIN, reverse_min);
Self {
forward_min,
forward_max,
reverse_min,
reverse_max,
}
}
}
const TOAST_DURATION_MS: u64 = 1500;
const RC_SEND_INTERVAL_MS: u64 = 10; // 100 Hz vers RP2350
const HAPTICS_INTERVAL_MS: u64 = 40; // 25 Hz (identique au G29)
@@ -353,7 +393,13 @@ pub fn steering_to_crsf(raw_norm: f32, deadzone: f32, expo: f32, invert: bool) -
}
/// Normalise une gâchette [0.0..1.0] avec zone morte et calcule le canal gaz CRSF selon la boîte séquentielle.
pub fn throttle_to_crsf(throttle_norm: f32, deadzone: f32, armed: bool, gear: i8) -> (u8, u16) {
pub fn throttle_to_crsf(
throttle_norm: f32,
deadzone: f32,
armed: bool,
gear: i8,
limits: GearSpeedLimits,
) -> (u8, u16) {
let dz = deadzone.clamp(0.0, 0.45);
let clamped = throttle_norm.clamp(0.0, 1.0);
let active = if clamped <= dz {
@@ -367,13 +413,14 @@ pub fn throttle_to_crsf(throttle_norm: f32, deadzone: f32, armed: bool, gear: i8
return (pct, CRSF_CHANNEL_MID);
}
let s = limits.sanitized();
let crsf = if gear == -1 {
let span = GAZ_ARRIERE_MIN as f32 - GAZ_ARRIERE_MAX as f32;
(GAZ_ARRIERE_MIN as f32 - active * span).round() as u16
let span = s.reverse_min as f32 - s.reverse_max as f32;
(s.reverse_min as f32 - active * span).round() as u16
} else if (1..=3).contains(&gear) {
let max_v = GAZ_AVANT_MAX_PAR_VITESSE[(gear - 1) as usize];
let span = max_v as f32 - GAZ_AVANT_MIN as f32;
(GAZ_AVANT_MIN as f32 + active * span).round() as u16
let max_v = s.forward_max[(gear - 1) as usize];
let span = max_v as f32 - s.forward_min as f32;
(s.forward_min as f32 + active * span).round() as u16
} else {
CRSF_CHANNEL_MID
};
@@ -404,6 +451,19 @@ pub fn encode_src_command(source: ControlSource) -> String {
format!("$SRC,{}\n", source.wire_tag())
}
pub fn encode_gears_command(limits: GearSpeedLimits) -> String {
let s = limits.sanitized();
format!(
"$GEARS,{},{},{},{},{},{}\n",
s.forward_min,
s.forward_max[0],
s.forward_max[1],
s.forward_max[2],
s.reverse_min,
s.reverse_max,
)
}
pub fn encode_rc_frame(
steer: u16,
accel: u16,
@@ -1346,6 +1406,7 @@ impl SteamControllerWorker {
active_source: Arc<AtomicU8>,
vibration_pct: Arc<AtomicU8>,
rumble_test_flag: Arc<AtomicBool>,
gear_speed_limits: Arc<Mutex<GearSpeedLimits>>,
control: EngineControlHandle,
g29_state_opt: Option<Arc<Mutex<G29State>>>,
telemetry_state_opt: Option<Arc<Mutex<TelemetryState>>>,
@@ -1384,6 +1445,7 @@ impl SteamControllerWorker {
active_source,
vibration_pct,
rumble_test_flag,
gear_speed_limits,
control,
g29_state_opt,
telemetry_state_opt,
@@ -1411,6 +1473,7 @@ impl SteamControllerWorker {
active_source: Arc<AtomicU8>,
vibration_pct: Arc<AtomicU8>,
rumble_test_flag: Arc<AtomicBool>,
gear_speed_limits: Arc<Mutex<GearSpeedLimits>>,
control: EngineControlHandle,
g29_state_opt: Option<Arc<Mutex<G29State>>>,
telemetry_state_opt: Option<Arc<Mutex<TelemetryState>>>,
@@ -1426,13 +1489,27 @@ impl SteamControllerWorker {
let mut last_haptics_tick = Instant::now() - Duration::from_millis(HAPTICS_INTERVAL_MS);
let mut rumble_is_zero = true;
// Synchronisation initiale de la source souhaitée auprès de la RP2350
// Synchronisation initiale de la source souhaitée et des vitesses auprès de la RP2350
let init_cmd = encode_src_command(last_source);
let _ = socket.send_to(init_cmd.as_bytes(), target);
let mut last_sent_gears = gear_speed_limits.lock().sanitized();
let init_gears_cmd = encode_gears_command(last_sent_gears);
let _ = socket.send_to(init_gears_cmd.as_bytes(), target);
while !stop_flag.load(Ordering::Acquire) {
let loop_start = Instant::now();
let desired_source = ControlSource::from_u8(active_source.load(Ordering::Acquire));
let current_gears = gear_speed_limits.lock().sanitized();
if current_gears != last_sent_gears {
last_sent_gears = current_gears;
let gears_cmd = encode_gears_command(current_gears);
let _ = socket.send_to(gears_cmd.as_bytes(), target);
log::info!(
"[Contrôleur] Vitesses CRSF synchronisées vers {} : {}",
target,
gears_cmd.trim()
);
}
let mut haptics_force_tick = false;
// 1. Changement de source actif (G29 <-> Steam Controller)
@@ -1563,6 +1640,7 @@ impl SteamControllerWorker {
cfg.trigger_deadzone,
st.armed,
st.gear,
current_gears,
);
let gas_released = pre_throt_pct == 0;
@@ -1589,6 +1667,7 @@ impl SteamControllerWorker {
cfg.trigger_deadzone,
st.armed,
st.gear,
current_gears,
);
st.steering_norm = steer_norm;
st.throttle_pct = throt_pct;
@@ -1623,8 +1702,13 @@ impl SteamControllerWorker {
} else {
// Mode veille (G29 actif) : mise à jour lecture seule pour le moniteur des Réglages
// sans armer, sans déclencher les actions cockpit et sans envoyer de trame $RC.
let (throt_pct, _) =
throttle_to_crsf(throttle_norm, cfg.trigger_deadzone, false, 0);
let (throt_pct, _) = throttle_to_crsf(
throttle_norm,
cfg.trigger_deadzone,
false,
0,
current_gears,
);
let mut st = state.lock();
if let Some(&last_key) = pressed_keys.last() {
st.last_button = key_code_label(last_key);
@@ -1874,16 +1958,26 @@ mod tests {
#[test]
fn test_sequential_gearbox_throttle_and_brake() {
let def = GearSpeedLimits::default();
// Désarmé : toujours au neutre
assert_eq!(throttle_to_crsf(1.0, 0.05, false, 2), (100, CRSF_CHANNEL_MID));
assert_eq!(throttle_to_crsf(1.0, 0.05, false, 2, def), (100, CRSF_CHANNEL_MID));
// Point mort N : toujours au neutre
assert_eq!(throttle_to_crsf(1.0, 0.05, true, 0), (100, CRSF_CHANNEL_MID));
assert_eq!(throttle_to_crsf(1.0, 0.05, true, 0, def), (100, CRSF_CHANNEL_MID));
// 1ère vitesse plein gaz -> 1100
assert_eq!(throttle_to_crsf(1.0, 0.05, true, 1), (100, 1100));
assert_eq!(throttle_to_crsf(1.0, 0.05, true, 1, def), (100, 1100));
// 3ème vitesse plein gaz -> 1220
assert_eq!(throttle_to_crsf(1.0, 0.05, true, 3), (100, 1220));
assert_eq!(throttle_to_crsf(1.0, 0.05, true, 3, def), (100, 1220));
// Marche arrière plein gaz -> 800
assert_eq!(throttle_to_crsf(1.0, 0.05, true, -1), (100, 800));
assert_eq!(throttle_to_crsf(1.0, 0.05, true, -1, def), (100, 800));
// Valeurs personnalisées
let custom = GearSpeedLimits {
forward_min: 1080,
forward_max: [1140, 1250, 1400],
reverse_min: 890,
reverse_max: 720,
};
assert_eq!(throttle_to_crsf(1.0, 0.05, true, 2, custom), (100, 1250));
assert_eq!(throttle_to_crsf(1.0, 0.05, true, -1, custom), (100, 720));
// Frein plein -> 1811
assert_eq!(brake_to_crsf(1.0, 0.05), (100, CRSF_CHANNEL_MAX));
assert_eq!(brake_to_crsf(0.02, 0.05), (0, CRSF_CHANNEL_MID));
@@ -1900,6 +1994,10 @@ mod tests {
encode_rc_frame(992, 1150, 992, 992, true, 2),
"$RC,992,1150,992,992,1,2\n"
);
assert_eq!(
encode_gears_command(GearSpeedLimits::default()),
"$GEARS,1065,1100,1150,1220,905,800\n"
);
}
#[test]