# unitree_sdk2_rs
Unitree SDK2 的 Rust 封装。
- **msg 驱动**:从 `msgs/*.msg` 自动生成 Rust 类型(CDR 布局与 C++ 一致)
- **CDR 序列化**:用 `cdr` crate,与 Cyclone DDS 逐字节兼容
- **订阅/发布**:`subscribe::<T>(topic)` / `Publisher::<T>` 泛型 API
- **RPC**:`rpc::RpcClient` / `rpc::RpcServer`
## 依赖的 SDK
构建时从 git 获取 unitree_sdk2(`[package.metadata]` 配置,可用 feature 或环境变量切换):
```toml
# 消费方 Cargo.toml
unitree_sdk2_rs = { path = "...", features = ["internal"] } # internal / custom / 默认 GitHub
```
| default | https://github.com/unitreerobotics/unitree_sdk2.git |
| internal | internal url |
| custom | 配合 env `UNITREE_SDK2_URL` |
环境变量可覆盖:`UNITREE_SDK2_SOURCE` / `UNITREE_SDK2_URL` / `UNITREE_SDK2_DIR`。
## 快速开始
```rust
use unitree_sdk2_rs::{init_dds, subscribe, Publisher};
use unitree_sdk2_rs::unitree_hg::msg::dds_::BmsState;
init_dds(0, "eth0", "");
let (_sub, rx) = subscribe::<BmsState>("rt/lf/bmsstate");
```
## 结构
```
unitree_sdk2_rs/
src/
lib.rs # 库入口:subscribe / Publisher / init_dds / RPC
rpc.rs # RpcClient / RpcServer
gen/ # build.rs 从 msg 生成(gen_types / gen_ffi / gen_topics)
cpp/
dds_bridge.{h,cpp} # Subscriber/Publisher 模板 + CDR 序列化
rpc_bridge.{h,cpp} # RPC 桥
msgs/ # msg 源文件(unitree_hg/unitree_go/unitree_api)
build.rs # msg→Rust/C++ 代码生成 + SDK 获取 + cxx 编译
```