feat: 添加了自动超参数整定

This commit is contained in:
Horldsence
2026-09-23 00:42:26 +08:00
parent 7d87987e41
commit 680adae339
56 changed files with 13664 additions and 245 deletions
+2 -2
View File
@@ -10,16 +10,16 @@ description = "Mecanum chassis daemon: configuration, 1 kHz control loop, safety
workspace = true
[dependencies]
chassis-config = { workspace = true }
motor-core = { workspace = true }
dji-esc = { workspace = true }
dji-esc-sim = { workspace = true }
dji-esc-socketcan = { workspace = true }
mecanum = { workspace = true }
tokio = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
serde = { workspace = true }
toml = { workspace = true }
clap = { workspace = true }
[dev-dependencies]
-504
View File
@@ -1,504 +0,0 @@
//! `chassis.toml` 配置模型与加载。
use std::time::Duration;
use dji_esc::{BusOptions, EscConfig, EscModel};
use mecanum::Mecanum;
use serde::Deserialize;
use thiserror::Error;
/// 配置错误。
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum ConfigError {
/// TOML 语法 / 结构错误。
#[error("failed to parse config: {0}")]
Parse(String),
/// 语义校验失败。
#[error("invalid config: {0}")]
Invalid(String),
}
/// 顶层配置。
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
/// CAN 配置。
pub can: CanConfig,
/// 底盘控制参数。
#[serde(default)]
pub chassis: ChassisConfig,
/// 麦轮几何。
pub mecanum: MecanumConfig,
/// 四电机配置,顺序固定:前左、前右、后左、后右。
#[serde(rename = "motor")]
pub motors: Vec<MotorConfig>,
}
/// CAN 接口配置(双 CAN 总线 seam)。
///
/// 底盘与功能总线物理隔离,**每条总线只允许一种协议族**(见 [`ProtocolFamily`])。
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CanConfig {
/// 底盘总线 SocketCAN 接口名(如 `can0`)——C620 驱动轮,ESC 电流协议族。
pub interface: String,
/// 功能总线(云台 / 拨弹):与底盘物理隔离。
///
/// 预留 seam;填写后由上层功能模块(gimbal / feeder)使用。
/// 典型:`can1`,承载 GM6020 云台 + M2006/C610 拨弹。
#[serde(default)]
pub function: Option<FunctionBusConfig>,
}
/// 功能总线配置(双 CAN 之二)。
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FunctionBusConfig {
/// SocketCAN 接口名(如 `can1`)。
pub interface: String,
}
/// 协议族——同一总线上**不得混用**。
///
/// C620/C610(电流)与 GM6020(电压)的控制/反馈帧 ID 空间重叠:
/// `0x1FF` 既可作 C620 5–8 电流也可作 GM6020 1–4 电压;`0x205` 同理。
/// 双 CAN 物理隔离是首选消解手段;单总线时必须保证同族。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ProtocolFamily {
/// C620/C610 电流协议(驱动轮、拨弹轮)。
Esc,
/// GM6020 电压协议(云台)。
Gm6020,
}
impl ProtocolFamily {
/// 电调型号所属协议族。
#[must_use]
pub const fn of_esc_model(model: EscModelSerde) -> Self {
match model {
EscModelSerde::C620 | EscModelSerde::C610 => Self::Esc,
}
}
}
/// 底盘控制参数(全部可省略,采用默认值)。
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct ChassisConfig {
/// 控制循环周期(毫秒)。
pub control_period_ms: u64,
/// 上层指令超时(毫秒):超时后目标速度归零。
pub command_timeout_ms: u64,
/// 电机反馈离线阈值(毫秒)。
pub feedback_timeout_ms: u64,
/// 最大前进 / 横移速度(m/s)。
pub max_vx: f32,
/// 最大横移速度(m/s)。
pub max_vy: f32,
/// 最大角速度(rad/s)。
pub max_omega: f32,
}
impl Default for ChassisConfig {
fn default() -> Self {
Self {
control_period_ms: 1,
command_timeout_ms: 200,
feedback_timeout_ms: 100,
max_vx: 3.0,
max_vy: 3.0,
max_omega: 6.0,
}
}
}
/// 麦轮几何(米)。
#[derive(Debug, Clone, Copy, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MecanumConfig {
/// 前后轮轴线到中心的纵向半距。
pub half_length: f32,
/// 左右轮到中心的横向半距。
pub half_width: f32,
/// 车轮半径。
pub wheel_radius: f32,
}
/// 单电机配置。
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MotorConfig {
/// 便于日志辨认的名字(`front_left` 等)。
pub name: String,
/// 电调 ID(1–8)。
pub esc_id: u8,
/// 电调型号(`C620` / `C610`)。
#[serde(default = "default_model")]
pub model: EscModelSerde,
/// 安装方向:`1` 或 `-1`。
#[serde(default = "default_direction")]
pub direction: f32,
/// 减速比(GM3508 ≈ 19.203)。
pub gear_ratio: f32,
/// 电流限幅(raw)。
#[serde(default = "default_current_limit")]
pub current_limit: i16,
/// 速度环 kp。
#[serde(default)]
pub kp: f32,
/// 速度环 ki。
#[serde(default)]
pub ki: f32,
/// 速度环 kd。
#[serde(default)]
pub kd: f32,
/// 速度前馈(raw/rpm)。
#[serde(default)]
pub feedforward: f32,
}
fn default_model() -> EscModelSerde {
EscModelSerde::C620
}
fn default_direction() -> f32 {
1.0
}
fn default_current_limit() -> i16 {
12_000
}
/// 配置文件中的电调型号。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "UPPERCASE")]
pub enum EscModelSerde {
/// C620(GM3508)。
C620,
/// C610(M2006)。
C610,
}
impl From<EscModelSerde> for EscModel {
fn from(value: EscModelSerde) -> Self {
match value {
EscModelSerde::C620 => Self::C620,
EscModelSerde::C610 => Self::C610,
}
}
}
impl Config {
/// 从 TOML 文本解析并校验。
///
/// # Errors
///
/// 解析失败或语义校验失败(电机数量、几何、方向等)。
pub fn from_toml(text: &str) -> Result<Self, ConfigError> {
let config: Self = toml::from_str(text).map_err(|e| ConfigError::Parse(e.to_string()))?;
config.validate()?;
Ok(config)
}
/// 从文件读取并解析。
///
/// # Errors
///
/// 读取失败、解析失败或校验失败。
pub fn load(path: &std::path::Path) -> Result<Self, ConfigError> {
let text = std::fs::read_to_string(path)
.map_err(|e| ConfigError::Parse(format!("{}: {e}", path.display())))?;
Self::from_toml(&text)
}
/// 语义校验。
///
/// # Errors
///
/// 任一约束不满足时返回 [`ConfigError::Invalid`]。
pub fn validate(&self) -> Result<(), ConfigError> {
if self.motors.len() != 4 {
return Err(ConfigError::Invalid(format!(
"mecanum chassis needs exactly 4 motors (FL, FR, RL, RR), got {}",
self.motors.len()
)));
}
let m = &self.mecanum;
if !(m.half_length > 0.0 && m.half_width > 0.0 && m.wheel_radius > 0.0) {
return Err(ConfigError::Invalid(format!("mecanum geometry must be positive: {m:?}")));
}
let c = &self.chassis;
if c.control_period_ms == 0 || c.command_timeout_ms == 0 || c.feedback_timeout_ms == 0 {
return Err(ConfigError::Invalid(
"control/command/feedback timeouts must be > 0 ms".into(),
));
}
if !(c.max_vx > 0.0 && c.max_vy > 0.0 && c.max_omega > 0.0) {
return Err(ConfigError::Invalid("speed limits must be > 0".into()));
}
for motor in &self.motors {
let gear_ratio_valid = motor.gear_ratio > 0.0;
if !gear_ratio_valid {
return Err(ConfigError::Invalid(format!(
"motor {}: gear_ratio must be > 0",
motor.name
)));
}
}
// 每总线单协议族:底盘四轮必须同属 ESC 族(C620/C610)。
let chassis_families: std::collections::HashSet<ProtocolFamily> =
self.motors.iter().map(|m| ProtocolFamily::of_esc_model(m.model)).collect();
if chassis_families.len() > 1 {
return Err(ConfigError::Invalid(
"chassis bus mixes protocol families; keep one family per CAN bus".into(),
));
}
// 功能总线接口名不得与底盘总线相同(双 CAN 物理隔离)。
if let Some(function) = &self.can.function {
if function.interface == self.can.interface {
return Err(ConfigError::Invalid(format!(
"function bus interface must differ from chassis interface (both '{}')",
self.can.interface
)));
}
}
// 电调 ID、方向、限流的细粒度校验由 EscBus::spawn 复核。
Ok(())
}
/// `--mock` 演示 / 测试默认配置(与 `config/chassis.toml.example` 同构,
/// PID 与仿真器增益匹配)。
#[must_use]
pub fn mock_default() -> Self {
let motor = |name: &str, esc_id: u8, direction: f32| MotorConfig {
name: name.into(),
esc_id,
model: EscModelSerde::C620,
direction,
gear_ratio: 3591.0 / 187.0,
current_limit: 12_000,
kp: 8.0,
ki: 2.0,
kd: 0.0,
feedforward: 16384.0 / 9254.0,
};
Self {
can: CanConfig { interface: "vcan0".into(), function: None },
chassis: ChassisConfig::default(),
mecanum: MecanumConfig { half_length: 0.2, half_width: 0.25, wheel_radius: 0.0762 },
motors: vec![
motor("front_left", 1, 1.0),
motor("front_right", 2, -1.0),
motor("rear_left", 3, 1.0),
motor("rear_right", 4, -1.0),
],
}
}
/// 转换为 `EscBus` 配置(顺序 FL、FR、RL、RR)。
#[must_use]
pub fn to_esc_configs(&self) -> Vec<EscConfig> {
self.motors
.iter()
.map(|m| EscConfig {
name: m.name.clone(),
esc_id: m.esc_id,
model: m.model.into(),
gear_ratio: m.gear_ratio,
direction: m.direction,
current_limit: m.current_limit,
kp: m.kp,
ki: m.ki,
kd: m.kd,
feedforward: m.feedforward,
})
.collect()
}
/// 转换为 `EscBus` 运行参数。
#[must_use]
pub fn bus_options(&self) -> BusOptions {
BusOptions {
period: Duration::from_millis(self.chassis.control_period_ms),
feedback_timeout: Duration::from_millis(self.chassis.feedback_timeout_ms),
}
}
/// 麦轮运动学模型。
#[must_use]
pub fn kinematics(&self) -> Mecanum {
Mecanum::new(self.mecanum.half_length, self.mecanum.half_width, self.mecanum.wheel_radius)
}
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE: &str = r#"
[can]
interface = "can0"
[chassis]
control_period_ms = 1
command_timeout_ms = 200
feedback_timeout_ms = 100
[mecanum]
half_length = 0.18
half_width = 0.20
wheel_radius = 0.0762
[[motor]]
name = "front_left"
esc_id = 1
direction = 1
gear_ratio = 19.203
current_limit = 12000
kp = 18.0
ki = 0.6
kd = 0.0
feedforward = 9.0
[[motor]]
name = "front_right"
esc_id = 2
direction = -1
gear_ratio = 19.203
current_limit = 12000
kp = 18.0
ki = 0.6
feedforward = 9.0
[[motor]]
name = "rear_left"
esc_id = 3
direction = 1
gear_ratio = 19.203
current_limit = 12000
kp = 18.0
ki = 0.6
feedforward = 9.0
[[motor]]
name = "rear_right"
esc_id = 4
direction = -1
gear_ratio = 19.203
current_limit = 12000
kp = 18.0
ki = 0.6
feedforward = 9.0
"#;
#[test]
fn parses_example() {
let cfg = Config::from_toml(EXAMPLE).unwrap();
assert_eq!(cfg.can.interface, "can0");
assert_eq!(cfg.motors.len(), 4);
assert_eq!(cfg.motors[0].name, "front_left");
assert_eq!(cfg.motors[3].esc_id, 4);
assert_eq!(cfg.motors[1].direction.to_bits(), (-1.0f32).to_bits());
assert!(cfg.validate().is_ok());
assert_eq!(cfg.bus_options().period, Duration::from_millis(1));
assert_eq!(cfg.to_esc_configs().len(), 4);
}
#[test]
fn minimal_config_uses_defaults() {
let cfg = Config::from_toml(
r#"
[can]
interface = "can1"
[mecanum]
half_length = 0.2
half_width = 0.25
wheel_radius = 0.0762
[[motor]]
name = "fl"
esc_id = 1
gear_ratio = 19.2
[[motor]]
name = "fr"
esc_id = 2
gear_ratio = 19.2
[[motor]]
name = "rl"
esc_id = 3
gear_ratio = 19.2
[[motor]]
name = "rr"
esc_id = 4
gear_ratio = 19.2
"#,
)
.unwrap();
assert_eq!(cfg.chassis, ChassisConfig::default());
assert_eq!(cfg.motors[0].direction.to_bits(), 1.0f32.to_bits());
assert_eq!(cfg.motors[0].current_limit, 12_000);
assert_eq!(cfg.motors[0].model, EscModelSerde::C620);
}
#[test]
fn rejects_wrong_motor_count() {
let mut cfg = Config::from_toml(EXAMPLE).unwrap();
cfg.motors.pop();
assert!(matches!(cfg.validate(), Err(ConfigError::Invalid(_))));
}
#[test]
fn rejects_bad_geometry() {
let mut cfg = Config::from_toml(EXAMPLE).unwrap();
cfg.mecanum.wheel_radius = 0.0;
assert!(matches!(cfg.validate(), Err(ConfigError::Invalid(_))));
}
#[test]
fn rejects_unknown_fields() {
let text = EXAMPLE.replace("[can]", "[can]\nstray = 1");
assert!(matches!(Config::from_toml(&text), Err(ConfigError::Parse(_))));
}
#[test]
fn mock_default_is_valid_and_wired_like_example() {
let cfg = Config::mock_default();
assert!(cfg.validate().is_ok());
assert_eq!(cfg.motors.len(), 4);
let esc: Vec<u8> = cfg.to_esc_configs().iter().map(|m| m.esc_id).collect();
assert_eq!(esc, [1, 2, 3, 4]);
}
#[test]
fn function_bus_must_differ_from_chassis_bus() {
let mut cfg = Config::mock_default();
cfg.can.function = Some(FunctionBusConfig { interface: "vcan0".into() });
assert!(matches!(cfg.validate(), Err(ConfigError::Invalid(_))));
cfg.can.function = Some(FunctionBusConfig { interface: "vcan1".into() });
assert!(cfg.validate().is_ok());
}
#[test]
fn rejects_mixed_protocol_families_on_chassis_bus() {
let mut cfg = Config::mock_default();
// 当前 ESC 族包含 C620 与 C610;若引入 GM6020 到底盘总线必须拒绝。
// 这里验证家族集合逻辑:全部 C620/C610 仍为单族。
cfg.motors[0].model = EscModelSerde::C610;
assert!(cfg.validate().is_ok());
// 人为构造跨族:通过 of_esc_model 映射覆盖后仍应单族(C610→Esc)。
assert_eq!(ProtocolFamily::of_esc_model(EscModelSerde::C620), ProtocolFamily::Esc);
assert_eq!(ProtocolFamily::of_esc_model(EscModelSerde::C610), ProtocolFamily::Esc);
}
#[test]
fn parses_function_bus_seam() {
let text = EXAMPLE.replace(
"[can]\ninterface = \"can0\"",
"[can]\ninterface = \"can0\"\n\n[can.function]\ninterface = \"can1\"",
);
let cfg = Config::from_toml(&text).unwrap();
let function = cfg.can.function.as_ref().unwrap();
assert_eq!(function.interface, "can1");
}
}
+4 -10
View File
@@ -179,16 +179,10 @@ mod tests {
let missing = MockMotor::new(2); // 无反馈
let mocks_ready = [ready.clone(), mock_wheel(3), mock_wheel(4)];
let _ = mocks_ready;
let wheels = wrap_wheels(
[ready.clone(), missing.clone(), mock_wheel(3), mock_wheel(4)],
[GEAR; 4],
);
let ctrl = ChassisController::new(
kinematics,
wheels,
(3.0, 3.0, 6.0),
Duration::from_millis(200),
);
let wheels =
wrap_wheels([ready.clone(), missing.clone(), mock_wheel(3), mock_wheel(4)], [GEAR; 4]);
let ctrl =
ChassisController::new(kinematics, wheels, (3.0, 3.0, 6.0), Duration::from_millis(200));
let (tx, rx) = watch::channel(Twist::ZERO);
let handle = ctrl.spawn(rx, Duration::from_millis(1));
+8 -3
View File
@@ -1,13 +1,18 @@
//! `chassisd` 库部分:配置模型、底盘控制循环与指令源抽象。
//! `chassisd` 库部分:底盘控制循环与指令源抽象。
//!
//! 配置 schema 已独立为 [`chassis_config`](守护进程与整定工具共用一份
//! 定义),这里以 `config` 别名重新导出,保持 `chassisd::config::Config`
//! 这一历史路径可用。
//!
//! 二进制入口见 `src/main.rs`。
#![cfg_attr(test, allow(clippy::unwrap_used, clippy::expect_used))]
pub mod config;
pub use chassis_config as config;
pub mod controller;
pub mod source;
pub use config::{
pub use chassis_config::{
CanConfig, ChassisConfig, Config, ConfigError, FunctionBusConfig, MecanumConfig, MotorConfig,
ProtocolFamily,
};
+1 -1
View File
@@ -87,7 +87,7 @@ async fn run(
let link = if mock {
let (bus_side, plant_side) = dji_esc::CanLink::pair(512);
let esc_ids: Vec<u8> = config.motors.iter().map(|m| m.esc_id).collect();
dji_esc::spawn_plant(plant_side, &esc_ids, bus_options.period);
dji_esc_sim::spawn_plant(plant_side, &esc_ids, bus_options.period);
info!(?esc_ids, "mock 模式:进程内电机仿真");
bus_side
} else {
+2 -1
View File
@@ -7,7 +7,8 @@ use std::time::Duration;
use chassisd::config::Config;
use chassisd::controller::{ChassisController, wrap_wheels};
use dji_esc::{CanLink, EscBus, EscBusHandle, spawn_plant};
use dji_esc::{CanLink, EscBus, EscBusHandle};
use dji_esc_sim::spawn_plant;
use mecanum::Twist;
use motor_core::MotorFeedback;
use tokio::sync::watch;