feat: initial commit of SteamFrame FPV Control Center

This commit is contained in:
bleackstorm committed 2026-11-05 19:18:46 +01:00
commit 4459f5ffa4
18 files changed
+8571

No files matched your search

+253
View File
@@ -0,0 +1,253 @@
use anyhow::Result;
use glam::{EulerRot, Quat};
use std::net::{SocketAddr, UdpSocket};
use std::time::{Duration, Instant};
/// Opcode MSPv2 officiel ExpressLRS Backpack pour le Head Tracking (Pan, Tilt, Roll)
pub const MSP_ELRS_BACKPACK_SET_PTR: u16 = 0x0383;
/// Bornes CRSF standard (11 bits : 172 = 988µs, 992 = 1500µs, 1811 = 2012µs)
pub const CRSF_CHANNEL_MIN: u16 = 172;
pub const CRSF_CHANNEL_MID: u16 = 992;
pub const CRSF_CHANNEL_MAX: u16 = 1811;
#[derive(Debug, Clone, Copy)]
pub struct HeadPoseSample {
pub pan_deg: f32,
pub tilt_deg: f32,
pub roll_deg: f32,
pub pan_crsf: u16,
pub tilt_crsf: u16,
pub roll_crsf: u16,
pub packets_sent: u64,
pub send_errors: u64,
}
impl Default for HeadPoseSample {
fn default() -> Self {
Self {
pan_deg: 0.0,
tilt_deg: 0.0,
roll_deg: 0.0,
pan_crsf: CRSF_CHANNEL_MID,
tilt_crsf: CRSF_CHANNEL_MID,
roll_crsf: CRSF_CHANNEL_MID,
packets_sent: 0,
send_errors: 0,
}
}
}
pub struct NomadHeadTracker {
socket: UdpSocket,
target: SocketAddr,
zero_quat_inv: Quat,
max_pan_deg: f32,
max_tilt_deg: f32,
max_roll_deg: f32,
use_crsf: bool,
packets_sent: u64,
send_errors: u64,
last_error_log: Option<Instant>,
}
impl NomadHeadTracker {
pub fn new(
nomad_ip: &str,
nomad_port: u16,
max_pan_deg: f32,
max_tilt_deg: f32,
max_roll_deg: f32,
use_crsf: bool,
) -> Result<Self> {
let socket = UdpSocket::bind("0.0.0.0:0").map_err(|e| {
log::error!(
"[Wi-Fi Nomad] Impossible de créer le socket UDP sortant : {}",
e
);
e
})?;
socket.set_nonblocking(true)?;
let target: SocketAddr = format!("{}:{}", nomad_ip, nomad_port).parse().map_err(|e| {
log::error!(
"[Wi-Fi Nomad] Adresse IP ou port invalide ({}:{}) : {}",
nomad_ip,
nomad_port,
e
);
e
})?;
log::info!(
"[Wi-Fi Nomad] Émetteur Head-Tracking prêt vers {} (Protocole: {})",
target,
if use_crsf {
"CRSF RC_CHANNELS_PACKED (0x16)"
} else {
"MSPv2 ELRS_BACKPACK_SET_PTR (0x0383)"
}
);
Ok(Self {
socket,
target,
zero_quat_inv: Quat::IDENTITY,
max_pan_deg,
max_tilt_deg,
max_roll_deg,
use_crsf,
packets_sent: 0,
send_errors: 0,
last_error_log: None,
})
}
/// Réinitialise le centre du suivi de tête (Tare) sur la posture actuelle du casque
pub fn recenter(&mut self, current_orientation: Quat) {
self.zero_quat_inv = current_orientation.inverse();
log::info!("[Head-Tracker] Centre (Tare) réinitialisé sur la position actuelle du casque.");
}
/// Calcule Pan/Tilt/Roll depuis le quaternion OpenXR et envoie la trame UDP au Nomad
pub fn send_pose(&mut self, current_orientation: Quat) -> Result<HeadPoseSample> {
let relative = (self.zero_quat_inv * current_orientation).normalize();
let (yaw_rad, pitch_rad, roll_rad) = relative.to_euler(EulerRot::YXZ);
let pan_deg = -yaw_rad.to_degrees();
let tilt_deg = pitch_rad.to_degrees();
let roll_deg = -roll_rad.to_degrees();
let pan_crsf = Self::deg_to_crsf(pan_deg, self.max_pan_deg);
let tilt_crsf = Self::deg_to_crsf(tilt_deg, self.max_tilt_deg);
let roll_crsf = Self::deg_to_crsf(roll_deg, self.max_roll_deg);
let send_res = if self.use_crsf {
let packet = Self::encode_crsf_rc_channels(pan_crsf, tilt_crsf, roll_crsf);
self.socket.send_to(&packet, self.target)
} else {
let packet = Self::encode_mspv2_ptr(pan_crsf, tilt_crsf, roll_crsf);
self.socket.send_to(&packet, self.target)
};
match send_res {
Ok(_) => {
self.packets_sent = self.packets_sent.wrapping_add(1);
}
Err(e) => {
self.send_errors = self.send_errors.wrapping_add(1);
let should_log = self
.last_error_log
.map(|t| t.elapsed() >= Duration::from_secs(3))
.unwrap_or(true);
if should_log {
log::error!(
"[Wi-Fi Nomad] Échec d'envoi UDP vers {} : {} (Vérifiez la connexion Wi-Fi au Backpack du RadioMaster Nomad)",
self.target,
e
);
self.last_error_log = Some(Instant::now());
}
}
}
Ok(HeadPoseSample {
pan_deg,
tilt_deg,
roll_deg,
pan_crsf,
tilt_crsf,
roll_crsf,
packets_sent: self.packets_sent,
send_errors: self.send_errors,
})
}
#[inline]
pub fn deg_to_crsf(angle_deg: f32, max_deg: f32) -> u16 {
let norm = (angle_deg / max_deg).clamp(-1.0, 1.0);
let span = (CRSF_CHANNEL_MAX - CRSF_CHANNEL_MIN) as f32 / 2.0;
(CRSF_CHANNEL_MID as f32 + norm * span).round() as u16
}
pub fn encode_mspv2_ptr(pan: u16, tilt: u16, roll: u16) -> [u8; 15] {
let mut buf = [0u8; 15];
buf[0] = b'$';
buf[1] = b'X';
buf[2] = b'<';
buf[3] = 0x00;
buf[4..6].copy_from_slice(&MSP_ELRS_BACKPACK_SET_PTR.to_le_bytes());
buf[6..8].copy_from_slice(&6u16.to_le_bytes());
buf[8..10].copy_from_slice(&pan.to_le_bytes());
buf[10..12].copy_from_slice(&tilt.to_le_bytes());
buf[12..14].copy_from_slice(&roll.to_le_bytes());
buf[14] = Self::crc8_dvb_s2(&buf[3..14]);
buf
}
pub fn encode_crsf_rc_channels(pan: u16, tilt: u16, roll: u16) -> [u8; 26] {
let mut ch = [CRSF_CHANNEL_MID; 16];
ch[0] = pan;
ch[1] = tilt;
ch[2] = roll;
let mut frame = [0u8; 26];
frame[0] = 0xEE;
frame[1] = 24;
frame[2] = 0x16;
let mut bit_buf: u32 = 0;
let mut bits_in_buf: u8 = 0;
let mut byte_idx = 3;
for &val in &ch {
bit_buf |= ((val & 0x07FF) as u32) << bits_in_buf;
bits_in_buf += 11;
while bits_in_buf >= 8 {
frame[byte_idx] = (bit_buf & 0xFF) as u8;
byte_idx += 1;
bit_buf >>= 8;
bits_in_buf -= 8;
}
}
frame[25] = Self::crc8_dvb_s2(&frame[2..25]);
frame
}
pub fn crc8_dvb_s2(data: &[u8]) -> u8 {
let mut crc: u8 = 0;
for &byte in data {
crc ^= byte;
for _ in 0..8 {
if (crc & 0x80) != 0 {
crc = (crc << 1) ^ 0xD5;
} else {
crc <<= 1;
}
}
}
crc
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_deg_to_crsf_bounds() {
assert_eq!(NomadHeadTracker::deg_to_crsf(0.0, 90.0), CRSF_CHANNEL_MID);
assert_eq!(NomadHeadTracker::deg_to_crsf(90.0, 90.0), CRSF_CHANNEL_MAX);
assert_eq!(NomadHeadTracker::deg_to_crsf(-90.0, 90.0), CRSF_CHANNEL_MIN);
}
#[test]
fn test_mspv2_ptr_packet_header() {
let pkt = NomadHeadTracker::encode_mspv2_ptr(992, 992, 992);
assert_eq!(&pkt[0..3], b"$X<");
assert_eq!(u16::from_le_bytes([pkt[4], pkt[5]]), MSP_ELRS_BACKPACK_SET_PTR);
assert_eq!(u16::from_le_bytes([pkt[6], pkt[7]]), 6);
}
}