Initial commit

This commit is contained in:
MattMa
2026-07-10 16:58:17 +08:00
commit 7222ba1107
56 changed files with 8433 additions and 0 deletions
+271
View File
@@ -0,0 +1,271 @@
# 部署指南:Go2 NX 上的 `sru_nav_go2_ros1`
环境前提(已确认):
- Go2 onboard NX 已安装 **ROS 1 Noetic**
- 已安装 **odin_ros_driver**(发布 `/odin1/*` 话题)
- 已安装 **unitree_legged_sdk** + 一个把 `/cmd_vel` 桥接到 Go2 sport_mode 的节点
- Odin1 depth 频率 = 10 Hz,odom_highfreq 频率 = 400 Hz
---
## 1. 把本包放进 catkin 工作空间
```bash
mkdir -p ~/sru_ws/src
ln -s /path/to/SRU_Navigation/sru_nav_go2_ros1 ~/sru_ws/src/sru_nav_go2_ros1
cd ~/sru_ws
catkin_make
source devel/setup.bash
```
> 模型 ONNX 文件位于 `sru_nav_go2_ros1/models/` 下(软链到 `sru-robot-deployment/.../deployment_policies/`)。
> 部署到 Go2 NX 时如果没有原仓库,把 `vae_encoder.onnx` 与 `nav_policy.onnx` 直接拷到 `sru_nav_go2_ros1/models/` 即可。
---
## 2. 创建 Python conda 环境
为什么用 conda:onnxruntime / scipy 等 pip 包不污染系统 ROS python;NX (aarch64) 上 miniforge 是最稳的选择。
```bash
cd ~/sru_ws/src/sru_nav_go2_ros1
bash scripts/setup_conda_env.sh
```
脚本会:
1. 检测 `conda`(没有的话提示安装 miniforge for aarch64)
2. 创建名为 `sru_nav` 的环境(**python=3.8**,与 Noetic 系统 python 匹配,才能复用 `rospy` / `cv_bridge`)
3. 安装 `numpy / scipy / opencv-python / onnxruntime / rospkg / pyyaml / empy`
4. 跑一系列 import sanity check,并尝试加载两个 ONNX 模型,打印输入输出维度
**GPU 加速(可选)**:标准 pip 的 `onnxruntime-gpu` 在 Jetson 上**不能用**。需要去 [NVIDIA Jetson Zoo](https://elinux.org/Jetson_Zoo#ONNX_Runtime) 下载 JetPack 匹配的 `onnxruntime_gpu-*-cp38-cp38-linux_aarch64.whl`,然后:
```bash
conda activate sru_nav
pip install ./onnxruntime_gpu-<ver>-cp38-cp38-linux_aarch64.whl
```
不装 GPU 版本也能跑(CPU EP 上 5 Hz 推理在 NX 上一般够用,因为模型只有 ~28 MB)。
### 验证环境
```bash
bash scripts/setup_conda_env.sh --check
```
期望输出关键行:
```
[OK] numpy ...
[OK] scipy ...
[OK] cv2 ...
[OK] onnxruntime ...
ORT providers : ['CPUExecutionProvider'] # 或包含 CUDAExecutionProvider
[OK] rospy ...
[OK] cv_bridge ...
```
如果 `rospy` / `cv_bridge` 失败:确认 `/opt/ros/noetic/lib/python3/dist-packages` 存在且 conda env 使用 python 3.8。
---
## 3. 启动流程
打开 4 个终端(或用 tmux)。
### Terminal 1 — Odin1 驱动
确保 `odin_ros_driver/config/control_command.yaml` 中:
```yaml
senddepth: 1 # 启用 dense depth
sendodom: 1 # 启用 odom + odometry_highfreq
```
```bash
cd ../OAYN
mamba activate neupan
source ros_ws/devel/setup.zsh
export ROS_MASTER_URI=http://localhost:11311
export ROS_HOSTNAME=localhost
export ROS_IP=127.0.0.1
sudo ntpdate -u ntp.aliyun.com
roslaunch odin_ros_driver odin1_ros1.launch # 实际 launch 文件名以驱动包为准
```
### Terminal 2 — Go2 cmd_vel 桥
你现有的 unitree_legged_sdk 桥接节点,它订阅 `/cmd_vel`,下发到 Go2 sport_mode。
```bash
mamba activate sru_nav
export ROS_MASTER_URI=http://localhost:11311
export ROS_HOSTNAME=localhost
export ROS_IP=127.0.0.1
source devel/setup.zsh
rosrun unitree_control unitree_vel_controller __name:=vel_to_sdk
```
### Terminal 3 — SRU 导航节点(本包)
```bash
mamba activate sru_nav
export ROS_MASTER_URI=http://localhost:11311
export ROS_HOSTNAME=localhost
export ROS_IP=127.0.0.1
cd src/sru_nav_go2_ros1
bash scripts/launch_sru_nav.sh
# require_joystick=true,必须接手柄,axes[4] 推油门,15 s 看护
bash scripts/launch_sru_nav.sh --no-deadman
# require_joystick=false,cmd_vel_ratio 直接 1.0
# 发一个 /goal_pose,policy 就会驱动 /cmd_vel
```
`launch_sru_nav.sh` 自动做:source ROS → source catkin_ws → conda activate sru_nav → 把 `/opt/ros/noetic/lib/python3/dist-packages` 注入 `PYTHONPATH` → `roslaunch sru_nav_go2_ros1 sru_nav_go2.launch`。
如果手柄 `/joy` 已经在别处启动:
```bash
bash scripts/launch_sru_nav.sh --no-joy
```
如果不需要静态 TF(你自己发了 `base_link → odin1_base_link`):
```bash
bash scripts/launch_sru_nav.sh --no-tf
```
### Terminal 4 — 发目标 / 监控
```bash
# 推一个 3 m 前方的目标(odom 帧)
mamba activate sru_nav
export ROS_MASTER_URI=http://localhost:11311
export ROS_HOSTNAME=localhost
export ROS_IP=127.0.0.1
source devel/setup.zsh
rostopic pub /goal_pose geometry_msgs/PoseStamped \
"{header: {frame_id: 'odom'},
pose: {position: {x: 3.0, y: 0.0, z: 0.0}, orientation: {w: 1.0}}}" --once
# 检查输出
rostopic hz /cmd_vel # 应稳定 ~5 Hz
rostopic echo /cmd_vel -n 5
```
# 或者发布连续waypoints
```bash
mamba activate sru_nav
export ROS_MASTER_URI=http://localhost:11311
export ROS_HOSTNAME=localhost
export ROS_IP=127.0.0.1
source devel/setup.zsh
cd src/sru_nav_go2_ros1
rosrun sru_nav_go2_ros1 waypoint_runner.py \
--file config/waypoints_example.yaml
```
---
## 4. 首跑前的 sanity check 命令
```bash
# 4.1 频率检查
rostopic hz /odin1/depth_img_competetion # 应 ~10 Hz
rostopic hz /odin1/odometry_highfreq # 应 ~400 Hz
# 4.2 深度数据合理性
rostopic echo /odin1/depth_img_competetion -n 1 | head -20
# 关注 encoding:"32FC1", height/width, 不全为 0
# 4.3 odom frame
rostopic echo /odin1/odometry_highfreq -n 1 | head -20
# 关注 header.frame_id="odom", child_frame_id="odin1_base_link"
# 4.4 节点状态
rosnode info /sru_nav_node
rostopic list | grep sru_nav
# 4.5 关闭 Go2 cmd_vel 桥,单跑导航节点空载,看推理输出
rostopic echo /cmd_vel
# 拖动 Odin1 / 发个 /goal_pose,看 /cmd_vel 是否产生非零有界数值
```
---
## 5. 如何判断 odom twist 的坐标系?
**这是上机前必须确认的**。理论上 `child_frame_id="odin1_base_link"` 暗示 twist 是 body frame,但很多 SLAM 实现仍在 world frame 输出 twist——必须实测。
### 方法 A:静止判断(最快)
让 Odin1 完全静止:
```bash
rostopic echo -n 20 /odin1/odometry_highfreq | grep -A2 twist
```
所有 `linear.*` 和 `angular.*` 都应该接近 0。这一步只能排除"非零偏置 bug",分不出 world/body。
### 方法 B:纯前进运动
让 Go2 静止站立,**手动**将 Odin1 沿其镜头方向(body +X)匀速推进 1 米,过程录制:
```bash
rosbag record -O /tmp/odom_test.bag /odin1/odometry_highfreq
```
然后:
```bash
rosbag play /tmp/odom_test.bag
rqt_plot /odin1/odometry_highfreq/twist/twist/linear/x:y
```
- 如果 `linear.x` 是显著正值、`linear.y` 几乎 0 → **body frame**(已经是 base 系)
- 如果 `linear.x` 与 `linear.y` 数值与机体当前 yaw 有关(旋转 Odin1 朝向再做同样测试,结果改变)→ **world frame**
### 方法 C:旋转判断(推荐,更确定)
把 Odin1 朝任意非零方向放置(比如绕 Z 转 90°),再沿 body +X 推进:
- **body frame**:无论朝向如何,`linear.x` 总是正、`linear.y` ≈ 0
- **world frame**:朝向 +Y 推进时 `linear.y` 是正、`linear.x` ≈ 0
### 方法 D:对比 dt 数值导数
```python
# 对位置做数值导数,与 twist 对比
import rosbag, numpy as np
bag = rosbag.Bag('/tmp/odom_test.bag')
ts, pos, twist_lin = [], [], []
for _, msg, _ in bag.read_messages(topics=['/odin1/odometry_highfreq']):
ts.append(msg.header.stamp.to_sec())
pos.append([msg.pose.pose.position.x, msg.pose.pose.position.y, msg.pose.pose.position.z])
twist_lin.append([msg.twist.twist.linear.x, msg.twist.twist.linear.y, msg.twist.twist.linear.z])
ts, pos, twist_lin = map(np.array, (ts, pos, twist_lin))
vel_world = np.diff(pos, axis=0) / np.diff(ts)[:, None]
print('vel_world (sample):', vel_world[100])
print('twist_lin (sample):', twist_lin[100])
# 若两者数值在世界系几乎一致 → twist 是 world frame
# 若 twist_lin 与 vel_world 大小相同但方向旋转 → twist 是 body frame
```
### 根据结果配置本节点
- twist **是 world frame**:保持 `use_sim: false`(节点会按机体 quaternion 反旋转到 base 系,与原 DLIO 部署一致)。
- twist **是 body frame**:在 `config/sru_nav.yaml` 把 `use_sim` 设为 `true`,节点会直接透传 twist。
---
## 6. 常见问题
| 现象 | 排查 |
|---|---|
| `pip` 报 `SSL: CERTIFICATE_VERIFY_FAILED ... certificate is not yet valid` | **系统时钟回退了**(Jetson 无 RTC 电池时常见)。`date` 看一下,然后 `sudo timedatectl set-ntp true && sudo systemctl restart systemd-timesyncd`,或离线 `sudo date -s "2026-05-28 14:55:00" && sudo hwclock --systohc`。`setup_conda_env.sh` 已加前置检测。 |
| conda 报 `Unexpected error writing token file ... aau_token_host` | anaconda.org 遥测 bug,无害。脚本里 `export ANACONDA_ANON_USAGE=false` 已静音。 |
| pip 太慢 / 拉不动 | `setup_conda_env.sh` 默认走清华源;若要换 `PIP_INDEX_URL=... bash scripts/setup_conda_env.sh`。 |
| 节点启动后 `Odometry not ready, skipping depth callback.` | `rostopic hz /odin1/odometry_highfreq` 是否在跑?frame_id 是否正确? |
| `/cmd_vel` 全是 0 | 手柄 deadman——`axes[4]` 必须推过 0;或 15 s 超时。看 `cmd_vel_ratio` 日志。 |
| Twist 数值剧烈震荡 | 1) `policy_scale` 太大;2) odom twist 坐标系判错(world↔body 弄反);3) depth NaN 没被过滤 |
| `cv_bridge` import 失败 | conda env 必须是 python 3.8;并且 `PYTHONPATH` 已包含 `/opt/ros/noetic/lib/python3/dist-packages` |
| 推理速度跟不上 5 Hz | `rostopic hz /cmd_vel`;用 `nvtop`/`top` 看 NX 满载情况;考虑装 JetPack onnxruntime-gpu wheel |
| Goal 发出后立即被忽略 | `/goal_pose` 的 `frame_id` 必须等于 `/odin1/odometry_highfreq` 的 `header.frame_id`(默认 `odom`) |
---
## 7. 紧急停止
- 手柄按下 `BUTTON_ABORT`(默认 button index 9) → 立刻发零速、放弃当前目标
- 直接 `Ctrl+C` 杀掉 `sru_nav_node` → `/cmd_vel` 不再有发布者,Go2 桥应自动归零(依你的桥行为)
- 关闭电源开关(终极手段)
+441
View File
@@ -0,0 +1,441 @@
# 把 SRU 导航策略移植到 Odin1 + ROS1 的完整复现指南
> 从论文 *Spatially-Enhanced Recurrent Memory for Long-Range Mapless Navigation via End-to-End Reinforcement Learning* 与配套开源仓库
> [`sru-project-website`](https://michaelfyang.github.io/sru-project-website/) 出发,把 SRU 端到端导航策略部署到 **Odin1 深度相机 + ROS1 Noetic + 任意能消费 `geometry_msgs/Twist`(`/cmd_vel`)的机器人**(默认硬件参考为 Unitree Go2)。本仓库 `sru_nav_go2_ros1` 即为此目标的产物。
>
> **English version**: see `PORTING_GUIDE_EN.md`.
---
## 0. 这份文档是什么 / 不是什么
**是**:一份「移植决策记录 + AI 复刻 prompt + 自动验收脚本」的三合一指南。读完即可:
1. 知道我们为什么这样改、改了哪些点;
2. 用一个 slash command 让 AI agent(Cascade / Claude Code 等)从开源仓库与论文出发,**自动重新生成本仓库**;
3. 用一个脚本一键校验生成结果是否合格。
**不是**:算法原理讲解(请直接读论文与原始代码仓)。
**适用读者**:
- 已具备 Ubuntu + ROS1 + Python/conda 基本使用能力;
- 持有 Unitree Go2(或任何能订阅 `/cmd_vel` 的腿足/轮式机器人)+ Odin1 深度相机;
- 想在自己机器人上跑 SRU,或想验证「AI agent 是否能从论文+开源代码复刻一个真机部署包」。
> 0 门槛承诺:除上述前置技能外,所有路径、IP、机器人型号、摄像头型号都设计为**通过环境变量或参数覆盖**,不用改源码。
---
## 1. 上游来源
| 资产 | 链接 / 路径 | 在本移植里的角色 |
|---|---|---|
| 论文 | *Spatially-Enhanced Recurrent Memory for Long-Range Mapless Navigation via End-to-End Reinforcement Learning* | 解释观测/动作空间、奖励、网络结构(VAE 深度编码器 + LSTM-SRU + actor head) |
| 项目主页 | <https://michaelfyang.github.io/sru-project-website/> | 索引仓库、视频、论文 |
| `sru-navigation-learning` | RL 训练代码(rsl_rl 风格)。给出 PPO/MDPO 算法、reward terms、observation manager | 用于核对 **reward 设计、动作 squash、观测层次**,并取得 ONNX 导出脚本 |
| `sru-navigation-sim` | IsaacLab 环境,含 B2W / AOW 训练配置 | 取得 `policy_scaling`、随机化范围、观测形状(depth `64×5×8`)等关键数字 |
| `sru-robot-deployment` | 原作者在 Unitree B2W + ZED-X 上的真机部署 | **本仓库直接对照重写的对象**:原版用 PyTorch + DLIO odom + 自研 SDK 桥;本仓库换成 ONNX + Odin1 odom + Go2 桥 |
| `sru-pytorch-spatial-learning` | SRU/LSTM-SRU 单元的 PyTorch 实现 | 解释 RNN 隐状态形状(`rnn_hidden_size=512, num_layers=1`),导出 ONNX 时的状态展开方式 |
| `sru-depth-pretraining` | VAE depth encoder 的预训练 | 取得 `vae_encoder.onnx` 的输入形状(`H×W` 深度图归一化后单通道)与潜变量维度 |
如果你不打算复刻、只想跑:把训练完得到的两个 onnx(`vae_encoder.onnx` + `nav_policy.onnx`)丢到 `sru_nav_go2_ros1/models/`,跳到 §6。
---
## 2. 移植目标矩阵
| 维度 | 原作者部署 (`sru-robot-deployment`) | 本仓库 (`sru_nav_go2_ros1`) | 抽象后接口 |
|---|---|---|---|
| 机器人 | Unitree **B2W**(轮足) | Unitree **Go2**(纯腿足) | 任何订阅 `/cmd_vel` (`geometry_msgs/Twist`, body frame) 的机器人 |
| 深度传感 | ZED-X | **Odin1**(`32FC1` depth, ~10 Hz) | `~depth_topic` (`sensor_msgs/Image`, 32FC1, 米) |
| 里程计 | DLIO(自研 LIO) | Odin1 `odometry_highfreq` | `~odom_topic` (`nav_msgs/Odometry`, world frame) |
| 推理后端 | PyTorch JIT | **ONNX Runtime** | `models/{vae_encoder,nav_policy}.onnx` |
| 计算平台 | x86 工控机 | **Jetson Orin NX (aarch64) + conda py3.8** | 任何 Linux + ROS1 Noetic + conda |
| 控制频率 | ~10 Hz | **5 Hz**(与训练一致;Odin depth ~10 Hz 自然降采样) | `~control_frequency` |
| 操作员手柄 | PS5 PerceptiveNavigationSE2 | 同 PS5 mapping,**新增 `require_joystick: false`** 头模式 | `--no-deadman` / yaml `require_joystick` |
| 安装与启动 | rosinstall 全家桶 | **`setup_conda_env.sh` + `launch_sru_nav.sh`** 两脚本 | env vars `ENV_NAME / ROS_DISTRO / CATKIN_WS` 全部可覆盖 |
---
## 3. 核心移植决策(逐项说明动机与对照位置)
### 3.1 推理:PyTorch → ONNX Runtime
- **动机**:Jetson Orin NX 上装 PyTorch + cv_bridge + onnxruntime 的依赖网很容易爆;onnxruntime 单文件加载、CPU/GPU EP 切换简单;ARM wheel 现成。
- **接口**:`@/sru_nav_go2_ros1/src/sru_nav_go2/model.py` 实现 `LearningModel`,分别加载 `vae_encoder.onnx`(深度→latent)与 `nav_policy.onnx`(latent + 状态向量 + 上一动作 + LSTM hidden → action mean + 新 hidden)。
- **隐状态约定**:训练侧 `rnn_hidden_size=512, num_layers=1`;导出 ONNX 时把 `(h, c)` 作为输入输出张量,本端在每帧之间显式持有;`reset_hidden_state` 对应训练里 `is_first` 标志。
- **action squash**:网络已包含 `tanh`,输出在 `(-1, 1)`。本端再乘 `policy_scale=[vx_max, vy_max, ω_max]`(默认 `[0.6, 0.3, 0.6]`)得到 SI 单位速度。
### 3.2 观测层:Odin1 替代 ZED-X + DLIO
- **深度图**:Odin1 发布 `sensor_msgs/Image` (`32FC1`,单位米)。`@/sru_nav_go2_ros1/src/sru_nav_go2/navigation_policy_node.py:209-225` 里做:
1. `cv_bridge.imgmsg_to_cv2(passthrough)`;
2. `nan_to_num(nan=0, posinf=2*max_depth, neginf=0)`;
3. 截断到 `[min_depth, max_depth] = [0.25, 10.0] m`,超界置 0。
- **里程计帧约定**:Odin1 odom 的 twist 是 **world frame**。我们在 `odom_callback` 里用机体四元数把它反旋到 body frame(与训练对齐)。如果你的 odom 已经是 body frame,把 `use_sim: true` 透传即可(`@/sru_nav_go2_ros1/src/sru_nav_go2/navigation_policy_node.py:189-197`)。
- **goal 帧约定**:`/goal_pose.header.frame_id` 必须等于 odom 的 `frame_id`(默认 `odom`),否则节点直接拒绝 — 这是为了防止操作员误把机体系坐标当成 odom 系发出去。
### 3.3 动作下发:直接 `/cmd_vel`
- 原作者在 B2W 上自带 SDK 桥;Go2 直接用 unitree_legged_sdk 写一个订阅 `/cmd_vel` 的小节点(与本仓库分离)即可。本包**不**绑定具体桥。
- `policy_scale` 默认 `[0.6, 0.3, 0.6]` 比训练侧 `[1.5, 1.0, 1.0]` 保守一半多,给真机留余量;训练时已经做过 `Uniform(0.6, 1.2)` 的 scale 随机化,所以网络对运行时缩放鲁棒。
### 3.4 安全:手柄 deadman 双模
| 模式 | 触发 | 行为 |
|---|---|---|
| 默认 (`require_joystick=true`) | 实机部署 | `cmd_vel_ratio=0`,必须 `/joy` 持续发布且 `axes[4]` 推到正才有速度;`>15 s` 无 joy → 强制归零 |
| 测试 (`require_joystick=false`) | `--no-deadman` 或 yaml 改 false | 起步即 `cmd_vel_ratio=1.0`,无 joy 也跑。**只在抬轮 / 仿真 / 空旷场地**用 |
代码位置:`@/sru_nav_go2_ros1/src/sru_nav_go2/navigation_policy_node.py:118-121, 272-287`。
### 3.5 安装层:conda + 系统 ROS 共生
Jetson Orin NX 出厂有 ROS Noetic(系统 python3.8 + cv_bridge.so);同时我们要 onnxruntime / opencv / numpy 等只在 conda env 里好装。两个世界要打通:
1. **PYTHONPATH 注入**:`/opt/ros/noetic/lib/python3/dist-packages` 加到 conda python 的搜索路径,使 `rospy / cv_bridge / sensor_msgs / tf2_ros` 可 import。
2. **shebang 锁定**:catkin_make 时必须 `-DPYTHON_EXECUTABLE=$(which python3)` **在 conda env 已激活** 状态下跑,否则 `devel/lib/.../sru_nav_node` 的 `#!` 会指向 `/usr/bin/python3`,import onnxruntime 失败。
3. **ABI 冲突修复**:conda env 自带的 `libffi.so.7` 和系统 `libp11-kit.so.0` 版本符号不兼容,导致 `cv_bridge` 调 OpenCV 时崩溃(`undefined symbol: ffi_type_pointer, version LIBFFI_BASE_7.0`)。`launch_sru_nav.sh` 自动 `LD_PRELOAD=/lib/aarch64-linux-gnu/libffi.so.7` 提前加载系统 libffi。
4. **rospy 隐性依赖**:`netifaces` / `defusedxml` 在系统 python 里靠 apt 提供,conda env 里要 pip 装。`setup_conda_env.sh` 已加进默认列表。
5. **时钟检查**:Jetson 无 RTC 电池常导致系统时间回退,pip TLS 拒接握手。脚本里前置 `date` 检测并提示 `ntpdate / timedatectl`。
完整自动化脚本:`@/sru_nav_go2_ros1/scripts/setup_conda_env.sh` + `@/sru_nav_go2_ros1/scripts/launch_sru_nav.sh`。
### 3.6 故意删减项
| 训练里有的东西 | 部署里去掉的原因 |
|---|---|
| Heightmap / heightscan critic | Go2 不带 LiDAR/heightmap,需要的话外部话题再加 |
| 距离/动量等 critic 通道 | critic 仅训练用,推理只跑 actor head |
| MDPO / distillation 双 actor-critic 互蒸馏 | 训练阶段优化器结构,与导出后的 actor 无关 |
| 训练时 reward terms 计算 | 部署不需要计算 reward |
| Action scale randomization (`Uniform(0.6, 1.2)`) | 部署是固定缩放;网络已经被训练成对该范围鲁棒 |
---
## 4. 文件清单与职能
```
sru_nav_go2_ros1/
├── CMakeLists.txt # catkin 包定义;catkin_python_setup() + install scripts
├── package.xml # 依赖:rospy, sensor_msgs, geometry_msgs, nav_msgs, cv_bridge, tf2_ros
├── setup.py # 把 src/sru_nav_go2 装成 python 包
├── README.md
├── config/
│ ├── sru_nav.yaml # 运行时全部 ROS 参数;唯一应该被用户改的文件
│ └── waypoints_example.yaml # 多目标巡航示例(odom 系列点)
├── launch/
│ └── sru_nav_go2.launch # joy_node + static_tf + sru_nav_node
├── models/
│ ├── vae_encoder.onnx # 训练侧 export_onnx 产物
│ └── nav_policy.onnx
├── scripts/
│ ├── setup_conda_env.sh # 创建 sru_nav env,装 onnxruntime/cv2/netifaces…
│ ├── launch_sru_nav.sh # 一键启动 wrapper(含 LD_PRELOAD / PYTHONPATH 修复)
│ ├── sru_nav_node # ROS 节点入口(catkin 安装时拷到 devel/lib/...)
│ └── waypoint_runner.py # 顺序发 /goal_pose
├── src/sru_nav_go2/
│ ├── constants.py # 所有训练对齐常量(控制频率、轴键映射、scale 等)
│ ├── model.py # ONNX Runtime 封装:VAE encoder + LSTM-SRU policy
│ ├── navigation_policy_node.py # 主节点:odom/depth/joy/goal 回调 + cmd_vel 输出
│ ├── utils.py # 四元数<->旋转、reproject 等数学小工具
│ ├── visualization.py # rviz marker(目标向量、moving_goal、waypoint)
│ └── waypoint_manager.py # 录制/回放 waypoint 列表
└── docs/
├── DEPLOY_GO2_NX.md # 部署到 NX 的实战记录
└── PORTING_GUIDE.md # ← 你正在读的文件
```
---
## 5. 用 AI Agent 自动复刻本仓库
我们提供 **同一份移植 prompt 的三种调用形式**(内容等价,挑顺手的用):
| 入口 | 适用工具 | 启动方式 |
|---|---|---|
| `.windsurf/workflows/port-sru-to-ros.md` | Cascade(Windsurf IDE) | 在 Cascade 聊天框里输 `/port-sru-to-ros` |
| `.claude/commands/port-sru-to-ros.md` | Claude Code (CLI / IDE 插件) | `claude` 启动后 `/port-sru-to-ros` |
| `docs/PORTING_PROMPT.md` | 任意支持自定义 prompt 的 agent(Cursor、Continue、Aider…) | 把整篇内容粘进系统提示 / 任务描述 |
三个文件由同一份「**真理 prompt**」生成,差异只在启动头与文件路径约定。下面是完整的 prompt 内容(放在 `.windsurf/workflows/port-sru-to-ros.md` 里以 frontmatter 起头)。
> ⚠️ **可重复性边界**:现役 LLM 在生成 ~1500 LoC 的 ROS 移植代码时,单次成功率不是 100%。我们用 **分阶段 workflow** 把任务切成 6 步,每步都有可 grep 的验收点;任意一步偏题立刻回滚重试,比一次性长 prompt 的成功率高一个数量级。
### 5.1 分阶段 workflow 概览
| 阶段 | 输入 | 输出 | 验收 |
|---|---|---|---|
| 1. **勘查** | 上游 5 个仓库 + 论文 | 摘要 `notes/upstream_recon.md`:算法、观测/动作、I/O 形状、训练超参 | 文件存在且包含网络层数、obs dims、policy_scale |
| 2. **抽取推理路径** | `sru-navigation-learning` 的 export_onnx | `models/vae_encoder.onnx`、`models/nav_policy.onnx`、`docs/IO_SPEC.md` | onnxruntime 可加载,输入/输出名/形状记录 |
| 3. **生成 ROS 包骨架** | 上一步 IO_SPEC | `package.xml / CMakeLists.txt / setup.py / launch/ / config/sru_nav.yaml` | `catkin_make` 通过 |
| 4. **移植节点逻辑** | 原 `sru-robot-deployment` 节点 + IO_SPEC | `src/sru_nav_go2/{constants,model,utils,visualization,waypoint_manager,navigation_policy_node}.py` + `scripts/sru_nav_node` | 节点能 `roslaunch` 起来不 crash |
| 5. **写部署脚本** | 目标 platform 信息 | `scripts/setup_conda_env.sh` + `scripts/launch_sru_nav.sh` | 在干净 env 上跑通 §6 验收命令 |
| 6. **文档与验收** | 全部产物 | `README.md / docs/DEPLOY_*.md / scripts/verify_port.sh` | `bash scripts/verify_port.sh` 全绿 |
### 5.2 通用 prompt(与三个入口文件一致的核心内容)
```text
你是一名熟悉 ROS1 Noetic、conda、ONNX Runtime 与 PyTorch 的高级工程师。
【任务】
从以下上游资料出发,生成一个名为 `sru_nav_go2_ros1` 的 catkin 包,
把论文《Spatially-Enhanced Recurrent Memory for Long-Range Mapless
Navigation via End-to-End Reinforcement Learning》提出的端到端导航策略
部署到 Odin1 深度相机 + ROS1 Noetic + 任意能消费
`geometry_msgs/Twist (/cmd_vel)` 的机器人上。
【上游资料】
- 论文 PDF(按用户提供的路径读取,例如 ./2506.05997v2.pdf)
- 项目主页 https://michaelfyang.github.io/sru-project-website/
- 仓库(按用户工作区下子文件夹):
sru-navigation-learning — RL 训练 + ONNX 导出
sru-navigation-sim — IsaacLab 环境与训练配置
sru-robot-deployment — 原作者 B2W+ZED-X 真机部署(直接对标重写)
sru-pytorch-spatial-learning — SRU/LSTM-SRU 单元
sru-depth-pretraining — VAE depth encoder 预训练
- 已存在的 onnx 模型(如有):models/{vae_encoder,nav_policy}.onnx
【硬约束】
1. 目标传感器固定为 Odin1。深度话题默认 `/odin1/depth_img_competetion`
(sensor_msgs/Image, 32FC1, 单位米);里程计默认
`/odin1/odometry_highfreq` (nav_msgs/Odometry, world frame)。
2. 控制输出固定为 `/cmd_vel` (geometry_msgs/Twist, body frame),由调用方
桥接到具体机器人;本包不实现机器人 SDK 桥。
3. 推理后端只能用 onnxruntime(不引入 PyTorch 运行时依赖)。
4. 必须支持 conda env (默认 `sru_nav`, py3.8) 与系统 ROS 共生:
- PYTHONPATH 注入 /opt/ros/<DISTRO>/lib/python3/dist-packages
- 启动脚本 LD_PRELOAD 系统 libffi 修复 cv_bridge 的 ABI 冲突
- catkin_make 时锁定 PYTHON_EXECUTABLE 为 conda python
5. 所有路径、env、distro、机器人型号必须可被环境变量或 ROS 参数覆盖;
不得在源码里硬写 `/home/<user>` 一类 path。
6. 保留训练对齐的全部数值(control_frequency=5Hz, rnn_hidden=512,
policy_scale 默认 [0.6,0.3,0.6],joystick axis 映射等)。
7. 关键安全机制:手柄 deadman + 15 s 超时;提供 `require_joystick:
false` 旁路开关,但默认必须为 true。
【输出文件清单】(缺一不可)
package.xml, CMakeLists.txt, setup.py
launch/sru_nav_go2.launch
config/sru_nav.yaml, config/waypoints_example.yaml
scripts/setup_conda_env.sh, scripts/launch_sru_nav.sh,
scripts/sru_nav_node, scripts/waypoint_runner.py, scripts/verify_port.sh
src/sru_nav_go2/{__init__.py, constants.py, model.py, utils.py,
visualization.py, waypoint_manager.py,
navigation_policy_node.py}
docs/DEPLOY.md, docs/PORTING_GUIDE.md (中文), docs/PORTING_GUIDE_EN.md
models/README.md (说明从训练侧导出 onnx 的步骤)
【工作流程】(必须按顺序,每步完成后 self-check 再进下一步)
Step 1 — 勘查上游:读取仓库与论文,列出网络架构、观测维度、动作空间、
rsl_rl 算法配置、reward 列表,输出到 notes/upstream_recon.md。
Step 2 — IO 规范化:在训练仓里找到 export_onnx 入口,记录 vae_encoder
与 nav_policy 的 input/output 名与 shape,写到 docs/IO_SPEC.md。
Step 3 — 生成 catkin 骨架并 `catkin_make` 验证。
Step 4 — 实现节点:以 sru-robot-deployment 的 node 为模板,
替换传感器话题、剥离非 Odin 依赖、把 PyTorch 调用换成 onnxruntime。
Step 5 — 编写两份脚本(setup_conda_env.sh / launch_sru_nav.sh),
覆盖 §硬约束 4 的全部点。
Step 6 — 生成 verify_port.sh:检查包结构、shebang 指向、ONNX 推理、
rostopic 列表、yaml 必填字段。
【验收标准】(用户会跑 verify_port.sh 与下述命令)
1. `catkin_make` 在干净 catkin_ws 下 0 warning 0 error。
2. `bash scripts/setup_conda_env.sh --check` 全部通过。
3. `head -1 devel/lib/sru_nav_go2_ros1/sru_nav_node` 指向
conda env 的 python,不是 /usr/bin/python3。
4. `roslaunch sru_nav_go2_ros1 sru_nav_go2.launch require_joystick:=false`
起来后看到 `Navigation policy node is ready.` 且不刷错误。
5. 用 `rostopic pub` 发任意 odom + 一帧 32FC1 depth + 一个 goal_pose,
`/cmd_vel` 应输出非零 Twist。
6. `bash scripts/verify_port.sh` 全部 PASS。
【回答方式】
- 严禁直接生成 1000+ 行的单条回复;按 Step 拆分,每步先汇报计划再写文件。
- 每步结束后跑相应 self-check,把命令与输出贴回;自检失败立即修复。
- 如果上游仓库找不到某文件,明确报告并暂停,不得编造接口。
```
完整可直接执行的版本见 `@/.windsurf/workflows/port-sru-to-ros.md` 与 `@/.claude/commands/port-sru-to-ros.md`。
### 5.3 怎样验证「这份 prompt 真的能复刻本仓库」?
把上述 workflow 在一台干净机器上跑完,最后做:
```bash
diff -r --exclude=__pycache__ --exclude=.git \
./generated_sru_nav_go2_ros1/ \
./sru_nav_go2_ros1/
```
预期:**结构完全一致;逐行 diff 应集中在注释 / 字面量顺序上,不应在数值常量、话题名、函数签名上有差异**。如果出现关键差异(例如 `policy_scale` 改成 `[1, 1, 1]`、忘记 LD_PRELOAD),说明 agent 没遵 prompt,回到对应 Step 重启。
---
## 6. 个性化与 0 门槛使用
所有「可能因人而异」的点都开成参数。最常见的三类:
### 6.1 路径
| 参数 | 默认 | 覆盖方式 |
|---|---|---|
| catkin workspace | `$HOME/code/odin_sru_nav` | `CATKIN_WS=/path bash scripts/launch_sru_nav.sh` |
| conda env 名 | `sru_nav` | `ENV_NAME=my_env bash scripts/setup_conda_env.sh` |
| ROS distro | `noetic` | `ROS_DISTRO=melodic bash scripts/launch_sru_nav.sh`(仅 py3 兼容版本) |
| pip 镜像 | 清华源 | `PIP_INDEX_URL=https://pypi.org/simple bash scripts/setup_conda_env.sh` |
### 6.2 话题与帧
改 `config/sru_nav.yaml` 里这些字段,**不用改源码**:
```yaml
depth_topic: "/your/depth" # sensor_msgs/Image (32FC1, meters)
odom_topic: "/your/odometry" # nav_msgs/Odometry, world frame
joy_topic: "/joy"
goal_topic: "/goal_pose"
cmd_vel_topic: "/cmd_vel"
```
### 6.3 不同机器人
只需提供「订阅 `/cmd_vel` 的桥」。仓库**完全不感知机器人型号**。常见替换:
- Unitree Go2:用 `unitree_legged_sdk` 写 50 行的 cmd_vel→sport_mode 桥;
- Unitree B2 / B2W:用官方 ROS1 桥;
- Spot:`spot_ros` + `cmd_vel`;
- 任何 ROS 仿真:直接订阅 `/cmd_vel`。
唯一需要改的可能是 TF:默认 launch 里发 `base_link → odin1_base_link` 的静态 TF,参数 `odin1_x / y / z / roll / pitch / yaw`,按你的相机安装位置改。
### 6.4 检查环境(你提到要写进文档与 prompt 的部分)
**第一次部署时建议跑一遍**:
```bash
# 1) OS / ROS
lsb_release -a # Ubuntu 20.04 推荐 (Noetic 配套)
echo $ROS_DISTRO # 应为 noetic
which roscore # /opt/ros/noetic/bin/roscore
# 2) Conda
conda --version # ≥ 4.10
conda env list | grep -E "sru_nav|base"
# 3) 时钟(Jetson 无 RTC 电池常坏)
date # 确认是真实当前时间,否则 pip TLS 会拒接
# 4) 网络
ping -c 2 8.8.8.8 || echo "WARN: no internet, pip will fail"
# 5) 硬件
ls /dev/input/js* 2>/dev/null || echo "INFO: no joystick — use --no-deadman for headless tests"
rostopic list 2>/dev/null | grep odin1 || echo "WARN: Odin1 driver not running"
```
`scripts/verify_port.sh` 里把这一段固化为前置检查。
---
## 7. 验收脚本:`scripts/verify_port.sh`
设计成「**生成完代码后,开发者一键自检**」。完整列表见脚本本身(自带带 `[PASS]/[FAIL]/[WARN]` 标记),核心检查项:
| 检查项 | 命令 | 预期 |
|---|---|---|
| 包结构完整 | `find package.xml CMakeLists.txt setup.py launch config models scripts src/sru_nav_go2/{constants,model,navigation_policy_node}.py` | 全部存在 |
| 模型可加载 | `python -c "import onnxruntime as ort; ort.InferenceSession('models/vae_encoder.onnx')"` | 不抛异常,打印 input shape |
| catkin 通过 | `catkin_make -DPYTHON_EXECUTABLE=$(which python3)` | exit 0 |
| shebang 正确 | `head -1 devel/lib/sru_nav_go2_ros1/sru_nav_node \| grep -q miniconda3.envs.${ENV_NAME}` | match |
| 节点能起 | `timeout 8 roslaunch sru_nav_go2_ros1 sru_nav_go2.launch launch_joy:=false require_joystick:=false` | log 含 `Navigation policy node is ready` |
| 关键参数存在 | `grep -E "policy_scale\|require_joystick\|control_frequency" config/sru_nav.yaml` | 三项都命中 |
| LD_PRELOAD 修复 | `grep -q 'LD_PRELOAD.*libffi' scripts/launch_sru_nav.sh` | 命中 |
跑完终端打印 `ALL CHECKS PASSED` 才能放行真机。
---
## 8. 常见陷阱
按踩坑频率排序:
1. **catkin_make 时没在 conda env 里**:节点 shebang 烤进 `/usr/bin/python3`,运行时 `import onnxruntime` 失败。修:`conda activate sru_nav && catkin_make clean && catkin_make -DPYTHON_EXECUTABLE=$(which python3)`。
2. **`No module named netifaces / defusedxml`**:rospy 隐性依赖。`pip install netifaces defusedxml` 进 conda env,`setup_conda_env.sh` 已内置。
3. **`libp11-kit.so.0: undefined symbol: ffi_type_pointer`**:conda libffi 抢加载。`launch_sru_nav.sh` 里的 `LD_PRELOAD` 已修,新平台只需把版本号 7→8 同步。
4. **pip `certificate is not yet valid`**:Jetson 时钟回退。`sudo ntpdate -u ntp.aliyun.com && sudo hwclock --systohc`。
5. **`/cmd_vel` 全 0**:deadman 模式默认开。要么接手柄推 `axes[4]`,要么 `--no-deadman`(仅测试)。
6. **goal 被忽略**:`/goal_pose.frame_id` 必须等于 odom 的 `frame_id`。
---
## 9. 许可与致谢
- 算法与训练代码版权:原 SRU 作者团队,遵循其原 LICENSE。
- 本部署移植包 `sru_nav_go2_ros1` 在 MIT-style 许可下开源;包含上游派生代码的部分文件保留各自原始 header。
- Odin1 是第三方深度相机,本仓库不再分发其驱动。
如发现移植 bug 或想要一键支持更多机器人/相机,欢迎 issue + PR。
---
## 附录 A:与论文/训练代码核对的关键数字
| 名称 | 训练值 | 部署值 | 来源 |
|---|---|---|---|
| `control_frequency` | 10 Hz (env step) | 5 Hz | `constants.DEFAULT_CONTROL_FREQUENCY` |
| `rnn_hidden_size` | 512 | 512 | `b2w/agents/rsl_rl_cfg.py:36` |
| `rnn_num_layers` | 1 | 1 | 同上 |
| `policy_scaling` | `[1.5, 1.0, 1.0]` × `Uniform(0.8,1.2)/(0.6,1.0)/(0.8,1.2)` | `policy_scale=[0.6,0.3,0.6]`(保守) | `navigation_env_cfg.py:163` |
| `entropy_coef` | `0.00375` | n/a | `b2w/agents/rsl_rl_cfg.py:50` |
| `value_loss_coef` | `0.02` | n/a | 同上 |
| 深度图分辨率(VAE 输入) | 见 `image_input_dims=(64, 5, 8)` 编码后形状 | 任意输入分辨率,节点 resize 到训练规格 | `b2w/agents/rsl_rl_cfg.py:41` |
| `min_depth / max_depth` | 0.25 / 10.0 m | 0.25 / 10.0 m | `constants.py` |
| `JOYSTICK_TIMEOUT` | n/a | 15 s | `constants.py:16` |
附录里这张表是「训练-部署一致性」的最小验收清单。任何二次移植(换机器人 / 换相机)都应保留这些数字不变。
---
## 附录 B:从空机器开始的 0 门槛 quickstart
```bash
# === 系统级(一次性,需 sudo) =============================================
sudo apt-get update
sudo apt-get install -y curl git build-essential ros-noetic-desktop \
ros-noetic-joy ros-noetic-tf2-tools \
python3-catkin-tools
# === 装 miniconda(如已装可跳过) ==========================================
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-$(uname -m).sh
bash Miniconda3-latest-Linux-$(uname -m).sh -b -p $HOME/miniconda3
echo 'source $HOME/miniconda3/etc/profile.d/conda.sh' >> ~/.bashrc
source ~/.bashrc
# === 拉本仓库(替换成你的 fork / 仓库地址) ================================
mkdir -p ~/code/odin_sru_nav/src
cd ~/code/odin_sru_nav/src
git clone https://github.com/<YOUR_FORK>/sru_nav_go2_ros1.git
# === 装 conda 依赖(含 onnxruntime / cv2 / netifaces ...) =================
cd sru_nav_go2_ros1
bash scripts/setup_conda_env.sh # 全自动;首次约 5–10 分钟
bash scripts/setup_conda_env.sh --check # 自检全 OK 才继续
# === 编译(必须在 conda env 里) ===========================================
conda activate sru_nav
cd ~/code/odin_sru_nav
catkin_make -DPYTHON_EXECUTABLE=$(which python3)
source devel/setup.bash
# === 一键启动(默认安全模式:必须接手柄) ==================================
cd src/sru_nav_go2_ros1
bash scripts/launch_sru_nav.sh
# 或:抬轮 / 仿真 / 闭环回归测试(无手柄) ==================================
bash scripts/launch_sru_nav.sh --no-deadman
# === 一键自检 ==============================================================
bash scripts/verify_port.sh
```
到这里只要 `verify_port.sh` 全绿,就能开始真机测试。
+553
View File
@@ -0,0 +1,553 @@
# Porting the SRU navigation policy to Odin1 + ROS1: full reproduction guide
> Starting from the paper *Spatially-Enhanced Recurrent Memory for Long-Range
> Mapless Navigation via End-to-End Reinforcement Learning* and the
> open-source repositories indexed at
> [`sru-project-website`](https://michaelfyang.github.io/sru-project-website/),
> this guide explains how to deploy the SRU end-to-end navigation policy on
> **the Odin1 depth camera + ROS1 Noetic + any robot that consumes
> `geometry_msgs/Twist` on `/cmd_vel`** (the reference platform is the
> Unitree Go2). The package `sru_nav_go2_ros1` in this repository is the
> output of that effort.
>
> **中文版**: see `PORTING_GUIDE.md`.
---
## 0. What this document is — and is not
**Is**: a three-in-one guide combining (a) the porting decision log, (b) a
ready-to-use AI prompt that lets a coding agent regenerate this package
from upstream, and (c) an automated acceptance script. After reading you
should be able to:
1. understand which upstream pieces were changed and why,
2. run a slash command in Cascade / Claude Code to recreate the package
from the open-source repos and the paper,
3. run a single script to verify the result.
**Is not**: an algorithm tutorial. For the model and reward design, read
the paper and the upstream code directly.
**Audience**:
- comfortable with Ubuntu + ROS1 + Python/conda,
- has a Unitree Go2 (or any robot subscribing to `/cmd_vel`) plus an
Odin1 depth camera,
- wants to run SRU on their own robot, or to verify whether an AI agent
can reproduce a real-robot deployment package from the paper + open
code.
> Zero-barrier promise: every path, IP, robot model, and camera model is
> overridable via environment variable or ROS parameter. No source code
> edits required to retarget.
---
## 1. Upstream sources
| Asset | Link / path | Role in this port |
|---|---|---|
| Paper | *Spatially-Enhanced Recurrent Memory for Long-Range Mapless Navigation via End-to-End Reinforcement Learning* | Defines the observation/action space, rewards, and network (depth VAE + LSTM-SRU + actor head) |
| Project page | <https://michaelfyang.github.io/sru-project-website/> | Index of repos, videos, paper |
| `sru-navigation-learning` | RL training code (rsl_rl-style); contains PPO/MDPO algorithms, reward terms, observation manager | Source of truth for **reward design, action squashing, observation order**, plus the ONNX export entry point |
| `sru-navigation-sim` | IsaacLab task with B2W / AOW training configs | Provides `policy_scaling`, randomization ranges, observation shapes (depth `64×5×8`) |
| `sru-robot-deployment` | Original B2W + ZED-X real-robot deployment by the authors | The **direct rewrite target**: original uses PyTorch + DLIO odom + a custom SDK bridge; this port uses ONNX + Odin1 odom + a Go2 bridge |
| `sru-pytorch-spatial-learning` | PyTorch implementation of the SRU / LSTM-SRU cell | Explains RNN hidden-state shape (`rnn_hidden_size=512, num_layers=1`) and how `(h, c)` are exposed when exporting to ONNX |
| `sru-depth-pretraining` | Pretraining of the depth VAE encoder | Provides the input shape (`H×W` normalized depth, single channel) and latent dim of `vae_encoder.onnx` |
If you only want to run, not reproduce: drop the two trained ONNX files
(`vae_encoder.onnx` + `nav_policy.onnx`) into `sru_nav_go2_ros1/models/`
and skip to §6.
---
## 2. Porting target matrix
| Aspect | Author deployment (`sru-robot-deployment`) | This package (`sru_nav_go2_ros1`) | Abstracted interface |
|---|---|---|---|
| Robot | Unitree **B2W** (wheeled-leg) | Unitree **Go2** (pure-leg) | Any robot consuming `/cmd_vel` (`geometry_msgs/Twist`, body frame) |
| Depth sensor | ZED-X | **Odin1** (`32FC1`, ~10 Hz) | `~depth_topic` (`sensor_msgs/Image`, 32FC1, meters) |
| Odometry | DLIO (custom LIO) | Odin1 `odometry_highfreq` | `~odom_topic` (`nav_msgs/Odometry`, world frame) |
| Inference backend | PyTorch JIT | **ONNX Runtime** | `models/{vae_encoder,nav_policy}.onnx` |
| Compute platform | x86 industrial PC | **Jetson Orin NX (aarch64) + conda py3.8** | Any Linux + ROS1 Noetic + conda |
| Control rate | ~10 Hz | **5 Hz** (matches training; Odin depth ~10 Hz, naturally decimated) | `~control_frequency` |
| Operator joystick | PS5 PerceptiveNavigationSE2 | Same PS5 mapping, **plus `require_joystick: false`** for headless | `--no-deadman` / yaml `require_joystick` |
| Install / launch | rosinstall full stack | **`setup_conda_env.sh` + `launch_sru_nav.sh`** | env vars `ENV_NAME / ROS_DISTRO / CATKIN_WS`, all overridable |
---
## 3. Core porting decisions (motivation + reference)
### 3.1 Inference: PyTorch → ONNX Runtime
- **Why**: stacking PyTorch + cv_bridge + onnxruntime on a Jetson Orin NX
rapidly snowballs into a dependency mess; onnxruntime ships a single
shared-object loader, swaps CPU/GPU EP cleanly, and has prebuilt ARM
wheels.
- **Interface**: `@/sru_nav_go2_ros1/src/sru_nav_go2/model.py` implements
`LearningModel`, which loads `vae_encoder.onnx` (depth → latent) and
`nav_policy.onnx` (latent + state vector + last action + LSTM hidden →
action mean + new hidden) as two separate sessions.
- **Hidden-state contract**: training uses `rnn_hidden_size=512,
num_layers=1`; the ONNX export carries `(h, c)` as input/output tensors
and the deployment side holds them between frames. `reset_hidden_state`
matches the training `is_first` flag.
- **Action squash**: the network already includes `tanh`, so the output is
in `(-1, 1)`. The deployment then multiplies by
`policy_scale = [vx_max, vy_max, ω_max]` (default `[0.6, 0.3, 0.6]`)
to obtain SI velocities.
### 3.2 Observations: Odin1 replaces ZED-X + DLIO
- **Depth**: Odin1 publishes `sensor_msgs/Image` (`32FC1`, meters). The
callback `@/sru_nav_go2_ros1/src/sru_nav_go2/navigation_policy_node.py:209-225`:
1. `cv_bridge.imgmsg_to_cv2(passthrough)`,
2. `nan_to_num(nan=0, posinf=2*max_depth, neginf=0)`,
3. clamps to `[min_depth, max_depth] = [0.25, 10.0] m`, out-of-range
pixels set to 0.
- **Odometry frame convention**: Odin1's `twist` is **world frame**. The
`odom_callback` rotates it back to the body frame using the body
quaternion (matching training). If your odom is already in body frame,
set `use_sim: true` and the node passes `twist` straight through
(`@/sru_nav_go2_ros1/src/sru_nav_go2/navigation_policy_node.py:189-197`).
- **Goal frame contract**: `/goal_pose.header.frame_id` must equal the odom
`frame_id` (default `odom`). Otherwise the node rejects the message —
this guards against operators sending a body-frame coordinate as if it
were odom-frame.
### 3.3 Action egress: straight to `/cmd_vel`
- The original B2W deployment ships its own SDK bridge; on Go2 a tiny
separate node (subscribes to `/cmd_vel`, talks to `unitree_legged_sdk`
sport mode) is enough. **This package is robot-agnostic** and does not
bind to a specific bridge.
- `policy_scale = [0.6, 0.3, 0.6]` is a deliberate halving versus the
training value `[1.5, 1.0, 1.0]`, leaving margin for real-robot bring-up.
Training already randomized scale by `Uniform(0.6, 1.2)`, so the network
is robust to runtime scaling.
### 3.4 Safety: dual-mode joystick deadman
| Mode | Trigger | Behavior |
|---|---|---|
| Default (`require_joystick=true`) | Real-robot deployment | `cmd_vel_ratio=0` until `/joy` is published with `axes[4]` > 0; >15 s without `/joy` forces stop |
| Test (`require_joystick=false`) | `--no-deadman` or yaml=false | `cmd_vel_ratio=1.0` from start, no `/joy` required. **Lifted-wheel / sim / open-field only** |
Source: `@/sru_nav_go2_ros1/src/sru_nav_go2/navigation_policy_node.py:118-121, 272-287`.
### 3.5 Install layer: conda + system ROS coexistence
The Jetson Orin NX ships ROS Noetic (system python 3.8 + cv_bridge.so).
At the same time we want `onnxruntime / opencv / numpy / …` only inside a
conda env. The two worlds must interoperate:
1. **`PYTHONPATH` injection**: prepend
`/opt/ros/noetic/lib/python3/dist-packages` so the conda python can
import `rospy / cv_bridge / sensor_msgs / tf2_ros`.
2. **Shebang lock**: `catkin_make` must run with
`-DPYTHON_EXECUTABLE=$(which python3)` **while the conda env is
active**, otherwise `devel/lib/.../sru_nav_node`'s `#!` line will point
at `/usr/bin/python3` and `import onnxruntime` fails at runtime.
3. **ABI conflict fix**: the conda env ships `libffi.so.7`, but the system
`libp11-kit.so.0` was linked against the system `libffi`'s versioned
symbols. Without intervention `cv_bridge` crashes with
`undefined symbol: ffi_type_pointer, version LIBFFI_BASE_7.0`.
`launch_sru_nav.sh` automatically `LD_PRELOAD`s the system
`libffi.so.7` so its symbols take priority.
4. **Hidden rospy deps**: `netifaces` and `defusedxml` are provided to the
system python via apt packages; the conda env needs them via pip.
`setup_conda_env.sh` already lists both.
5. **Clock check**: Jetson without an RTC battery often boots with a stale
clock, breaking pip TLS. The script runs a `date` plausibility check
and points to `ntpdate / timedatectl`.
Full automation: `@/sru_nav_go2_ros1/scripts/setup_conda_env.sh` +
`@/sru_nav_go2_ros1/scripts/launch_sru_nav.sh`.
### 3.6 Deliberate omissions
| Present in training | Reason for omitting at deployment |
|---|---|
| Heightmap / heightscan critic | Go2 has no LiDAR/heightmap; can be added as an external topic if needed |
| Critic-only observation channels | Critic exists only at training time; only the actor head runs at inference |
| MDPO / mutual-distillation dual actor-critic | Optimizer-side mechanism; irrelevant to the exported actor |
| Reward computation | Inference does not need rewards |
| Action-scale randomization (`Uniform(0.6, 1.2)`) | Deployment uses a fixed scale; the network has been trained to be robust to this range |
---
## 4. File map and responsibilities
```
sru_nav_go2_ros1/
├── CMakeLists.txt # catkin def: catkin_python_setup() + install scripts
├── package.xml # deps: rospy, sensor_msgs, geometry_msgs, nav_msgs, cv_bridge, tf2_ros
├── setup.py # installs src/sru_nav_go2 as a Python package
├── README.md
├── config/
│ ├── sru_nav.yaml # all runtime ROS params; the only file users normally touch
│ └── waypoints_example.yaml # multi-goal tour example (odom-frame point list)
├── launch/
│ └── sru_nav_go2.launch # joy_node + static_tf + sru_nav_node
├── models/
│ ├── vae_encoder.onnx # exported from training side via export_onnx
│ └── nav_policy.onnx
├── scripts/
│ ├── setup_conda_env.sh # creates the sru_nav env, installs onnxruntime / cv2 / netifaces …
│ ├── launch_sru_nav.sh # one-liner launcher (with LD_PRELOAD / PYTHONPATH fixes)
│ ├── sru_nav_node # ROS node entrypoint (catkin install copies it under devel/lib/…)
│ ├── waypoint_runner.py # publishes /goal_pose in sequence
│ └── verify_port.sh # acceptance script (see §7)
├── src/sru_nav_go2/
│ ├── constants.py # all training-aligned constants
│ ├── model.py # ONNX Runtime wrapper: VAE encoder + LSTM-SRU policy
│ ├── navigation_policy_node.py # main node: odom/depth/joy/goal callbacks + cmd_vel out
│ ├── utils.py # quaternion ↔ rotation, projected gravity, etc.
│ ├── visualization.py # rviz markers
│ └── waypoint_manager.py # record / replay waypoint list
└── docs/
├── DEPLOY_GO2_NX.md # NX deployment field notes
├── PORTING_GUIDE.md # Chinese version
└── PORTING_GUIDE_EN.md # ← this file
```
---
## 5. Reproducing this package with an AI agent
We ship **the same porting prompt in three call forms** (use whichever
fits your tool):
| Entry | Tool | How to invoke |
|---|---|---|
| `.windsurf/workflows/port-sru-to-ros.md` | Cascade (Windsurf IDE) | type `/port-sru-to-ros` in the Cascade panel |
| `.claude/commands/port-sru-to-ros.md` | Claude Code (CLI / IDE) | start `claude`, then `/port-sru-to-ros` |
| `docs/PORTING_GUIDE.md` §5.2 | Any agent that accepts a custom prompt (Cursor, Continue, Aider, …) | paste §5.2 into the system / task prompt |
All three derive from one canonical prompt. Differences are limited to
the launch header and per-tool path conventions.
> ⚠️ **Reproducibility caveat**: a single LLM round generating ~1500 LoC
> of ROS porting code is not 100% reliable. We split the task into a
> 6-step workflow so each step has a grep-able acceptance check; this
> dramatically beats one-shot mega-prompts in success rate.
### 5.1 Workflow at a glance
| Step | Inputs | Outputs | Acceptance |
|---|---|---|---|
| 1. **Recon** | 5 upstream repos + paper | `notes/upstream_recon.md` summarizing arch, obs/action, IO shapes, training hyperparams | file exists and lists net depth, obs dims, policy_scale |
| 2. **Extract inference** | `sru-navigation-learning` export entry | `models/vae_encoder.onnx`, `models/nav_policy.onnx`, `docs/IO_SPEC.md` | onnxruntime can load both; IO names recorded |
| 3. **catkin skeleton** | Step 2 IO spec | `package.xml / CMakeLists.txt / setup.py / launch/ / config/sru_nav.yaml` | `catkin_make` succeeds |
| 4. **Port the node** | Original `sru-robot-deployment` node + IO spec | `src/sru_nav_go2/{constants,model,utils,visualization,waypoint_manager,navigation_policy_node}.py` + `scripts/sru_nav_node` | `roslaunch` brings the node up cleanly |
| 5. **Deployment scripts** | Target platform info | `scripts/setup_conda_env.sh` + `scripts/launch_sru_nav.sh` | clean-env smoke-test in §6 passes |
| 6. **Docs + verify** | All above | `README.md / docs/DEPLOY_*.md / scripts/verify_port.sh` | `bash scripts/verify_port.sh` all green |
### 5.2 Canonical prompt (the body of all three entry files)
```text
You are a senior engineer fluent in ROS1 Noetic, conda, ONNX Runtime, and
PyTorch.
[Task]
From the upstream sources below, generate a catkin package named
`sru_nav_go2_ros1` that deploys the navigation policy from the paper
"Spatially-Enhanced Recurrent Memory for Long-Range Mapless Navigation
via End-to-End Reinforcement Learning" onto an Odin1 depth camera +
ROS1 Noetic + any robot consuming `geometry_msgs/Twist` on `/cmd_vel`.
[Upstream sources]
- Paper PDF (path provided by user, e.g. ./2506.05997v2.pdf)
- Project page: https://michaelfyang.github.io/sru-project-website/
- Repos under user-provided `${UPSTREAM_DIR}`:
sru-navigation-learning — RL training + ONNX export
sru-navigation-sim — IsaacLab env + training configs
sru-robot-deployment — original B2W+ZED-X deployment (the
direct rewrite target)
sru-pytorch-spatial-learning — SRU / LSTM-SRU cell
sru-depth-pretraining — depth VAE pretraining
- Pre-existing onnx files (if any):
models/{vae_encoder,nav_policy}.onnx
[Hard constraints]
1. Sensors are fixed to Odin1: depth `/odin1/depth_img_competetion`
(sensor_msgs/Image, 32FC1, meters), odom
`/odin1/odometry_highfreq` (nav_msgs/Odometry, world frame).
2. Output is fixed to `/cmd_vel` (geometry_msgs/Twist, body frame). The
bridge to a specific robot's SDK is out of scope.
3. Only `onnxruntime` for inference. No PyTorch at runtime.
4. Must coexist with system ROS python in a conda env (default
`sru_nav`, py3.8):
- inject /opt/ros/<DISTRO>/lib/python3/dist-packages into PYTHONPATH
- LD_PRELOAD system libffi to fix cv_bridge ABI conflict
- lock catkin_make's PYTHON_EXECUTABLE to conda python
5. Every path / env / distro / robot model must be overridable via env
vars or ROS params; never write `/home/<user>` into source files.
6. Preserve all training-aligned numbers: control_frequency=5 Hz,
rnn_hidden=512, default policy_scale=[0.6,0.3,0.6], joystick axis
mapping, etc.
7. Safety: joystick deadman + 15 s timeout, with a
`require_joystick: false` bypass; default must be true.
[Output file list] (all required)
package.xml, CMakeLists.txt, setup.py
launch/sru_nav_go2.launch
config/sru_nav.yaml, config/waypoints_example.yaml
scripts/setup_conda_env.sh, scripts/launch_sru_nav.sh,
scripts/sru_nav_node, scripts/waypoint_runner.py, scripts/verify_port.sh
src/sru_nav_go2/{__init__.py, constants.py, model.py, utils.py,
visualization.py, waypoint_manager.py,
navigation_policy_node.py}
docs/DEPLOY.md, docs/PORTING_GUIDE.md (Chinese), docs/PORTING_GUIDE_EN.md
models/README.md (how to obtain / export the two ONNX files)
[Workflow] (must be sequential; self-check after each step)
Step 1 — Recon: read repos and paper; emit notes/upstream_recon.md with
net architecture, obs dims, action space, rsl_rl algorithm config,
reward list.
Step 2 — IO normalization: locate export_onnx in the training repo, log
input/output names + shapes, emit docs/IO_SPEC.md.
Step 3 — Generate catkin skeleton; verify with `catkin_make`.
Step 4 — Port the node: use sru-robot-deployment as template, swap
sensors, drop non-Odin deps, replace PyTorch calls with
onnxruntime.
Step 5 — Write the two scripts (setup + launcher), covering all of
constraint #4.
Step 6 — Generate verify_port.sh: package layout, shebang, ONNX load
test, rostopic list, yaml fields.
[Acceptance] (the user will run verify_port.sh and the commands below)
1. `catkin_make` in a clean workspace: 0 warnings, 0 errors.
2. `bash scripts/setup_conda_env.sh --check` passes.
3. `head -1 devel/lib/sru_nav_go2_ros1/sru_nav_node` points at conda
python, not `/usr/bin/python3`.
4. `roslaunch sru_nav_go2_ros1 sru_nav_go2.launch require_joystick:=false`
prints `Navigation policy node is ready.` and emits no error spam.
5. Synthetic odom + a 32FC1 depth frame + a goal_pose results in a
non-zero `/cmd_vel`.
6. `bash scripts/verify_port.sh` is all green.
[Response discipline]
- Do not emit a single 1000+ line reply; chunk per step, summarize the
plan first, then write files.
- After each step run the listed self-check, paste the command and
output, fix on failure before advancing.
- If an upstream file is missing, halt and ask. Never fabricate APIs.
```
The fully executable variants are in
`@/.windsurf/workflows/port-sru-to-ros.md` and
`@/.claude/commands/port-sru-to-ros.md`.
### 5.3 How do I prove that this prompt actually reproduces the package?
Run the workflow on a clean machine, then:
```bash
diff -r --exclude=__pycache__ --exclude=.git \
./generated_sru_nav_go2_ros1/ \
./sru_nav_go2_ros1/
```
Expected: **identical structure; line-level diffs concentrated in
comments and literal ordering, not in numeric constants, topic names,
or function signatures**. If you see drift on critical items
(`policy_scale` becoming `[1, 1, 1]`, the LD_PRELOAD line missing, etc.),
the agent did not follow the prompt — return to that step and retry.
---
## 6. Personalization and zero-barrier usage
Every "user-specific" knob is exposed as a parameter. The three most
common categories:
### 6.1 Paths
| Parameter | Default | Override |
|---|---|---|
| catkin workspace | `$HOME/code/odin_sru_nav` | `CATKIN_WS=/path bash scripts/launch_sru_nav.sh` |
| conda env name | `sru_nav` | `ENV_NAME=my_env bash scripts/setup_conda_env.sh` |
| ROS distro | `noetic` | `ROS_DISTRO=melodic bash scripts/launch_sru_nav.sh` (py3-compatible only) |
| pip mirror | Tsinghua | `PIP_INDEX_URL=https://pypi.org/simple bash scripts/setup_conda_env.sh` |
### 6.2 Topics and frames
Edit `config/sru_nav.yaml`; **do not edit source code**:
```yaml
depth_topic: "/your/depth" # sensor_msgs/Image (32FC1, meters)
odom_topic: "/your/odometry" # nav_msgs/Odometry, world frame
joy_topic: "/joy"
goal_topic: "/goal_pose"
cmd_vel_topic: "/cmd_vel"
```
### 6.3 Different robots
You only need a `/cmd_vel` bridge. The package is **robot-agnostic**.
Common substitutions:
- Unitree Go2: ~50 LoC bridge wrapping `unitree_legged_sdk` sport mode
- Unitree B2 / B2W: official ROS1 bridge
- Boston Dynamics Spot: `spot_ros` + `cmd_vel`
- Any ROS sim: subscribes to `/cmd_vel` directly
The only TF you may need to retune is the static
`base_link → odin1_base_link` published by the launch file; the args
`odin1_x / y / z / roll / pitch / yaw` mirror your camera mounting pose.
### 6.4 System-environment check (also embedded in the prompt)
Run once on a fresh host:
```bash
# 1) OS / ROS
lsb_release -a # Ubuntu 20.04 recommended (Noetic)
echo $ROS_DISTRO # expect: noetic
which roscore # /opt/ros/noetic/bin/roscore
# 2) Conda
conda --version # ≥ 4.10
conda env list | grep -E "sru_nav|base"
# 3) Clock (Jetsons without RTC battery often drift)
date # must reflect real wall time
# 4) Network
ping -c 2 8.8.8.8 || echo "WARN: no internet, pip will fail"
# 5) Hardware
ls /dev/input/js* 2>/dev/null \
|| echo "INFO: no joystick — use --no-deadman for headless tests"
rostopic list 2>/dev/null | grep odin1 \
|| echo "WARN: Odin1 driver not running"
```
`scripts/verify_port.sh` codifies the same checks.
---
## 7. Acceptance script — `scripts/verify_port.sh`
Designed for the developer to **self-check immediately after generation**.
The full set lives in the script itself (with `[PASS]/[FAIL]/[WARN]`
markers); the most important checks:
| Check | Command | Expected |
|---|---|---|
| Package layout complete | look for `package.xml CMakeLists.txt setup.py launch config models scripts src/sru_nav_go2/{constants,model,navigation_policy_node}.py` | all present |
| Models load | `python -c "import onnxruntime as ort; ort.InferenceSession('models/vae_encoder.onnx')"` | no exception, prints input shape |
| catkin builds | `catkin_make -DPYTHON_EXECUTABLE=$(which python3)` | exit 0 |
| Shebang correct | `head -1 devel/lib/sru_nav_go2_ros1/sru_nav_node \| grep -q miniconda3.envs.${ENV_NAME}` | match |
| Node comes up | `timeout 8 roslaunch sru_nav_go2_ros1 sru_nav_go2.launch launch_joy:=false require_joystick:=false` | log contains `Navigation policy node is ready` |
| Required yaml params | `grep -E "policy_scale\|require_joystick\|control_frequency" config/sru_nav.yaml` | all three matched |
| LD_PRELOAD fix | `grep -q 'LD_PRELOAD.*libffi' scripts/launch_sru_nav.sh` | match |
The script exits with `ALL CHECKS PASSED` when (and only when) every
check is green. Do not move to real-robot tests before that.
---
## 8. Common pitfalls
In rough order of how often they bite:
1. **Built `catkin_make` outside the conda env** → node shebang baked to
`/usr/bin/python3`; `import onnxruntime` fails. Fix:
`conda activate sru_nav && catkin_make clean && catkin_make -DPYTHON_EXECUTABLE=$(which python3)`.
2. **`No module named netifaces / defusedxml`** → rospy hidden deps.
`pip install netifaces defusedxml` into the conda env. Already in
`setup_conda_env.sh`.
3. **`libp11-kit.so.0: undefined symbol: ffi_type_pointer`** → conda
libffi is loaded first. The `LD_PRELOAD` fix in `launch_sru_nav.sh`
covers it; on platforms with only `libffi.so.8` change the version
number.
4. **pip `certificate is not yet valid`** → Jetson clock drift. Fix:
`sudo ntpdate -u ntp.aliyun.com && sudo hwclock --systohc`.
5. **`/cmd_vel` is all zeros** → deadman is on by default. Either plug
in the joystick and push `axes[4]`, or run with `--no-deadman` (test
mode only).
6. **Goal silently dropped** → `/goal_pose.frame_id` must equal the
odom `frame_id`.
---
## 9. License & acknowledgments
- The algorithm and training code remain copyright of the original SRU
authors under their original LICENSE.
- This deployment port `sru_nav_go2_ros1` is released under an MIT-style
license; files derived from upstream retain their original headers.
- Odin1 is a third-party depth camera; this repo does not redistribute
its driver.
If you find a porting bug or want first-class support for more robots /
cameras, please open an issue or PR.
---
## Appendix A: training–deployment numeric cross-check
| Quantity | Training value | Deployment value | Source |
|---|---|---|---|
| `control_frequency` | 10 Hz (env step) | 5 Hz | `constants.DEFAULT_CONTROL_FREQUENCY` |
| `rnn_hidden_size` | 512 | 512 | `b2w/agents/rsl_rl_cfg.py:36` |
| `rnn_num_layers` | 1 | 1 | same file |
| `policy_scaling` | `[1.5, 1.0, 1.0]` × `Uniform(0.8,1.2)/(0.6,1.0)/(0.8,1.2)` | `policy_scale=[0.6,0.3,0.6]` (conservative) | `navigation_env_cfg.py:163` |
| `entropy_coef` | `0.00375` | n/a | `b2w/agents/rsl_rl_cfg.py:50` |
| `value_loss_coef` | `0.02` | n/a | same file |
| Depth resolution into the VAE | `image_input_dims=(64, 5, 8)` (post-encoder) | input depth resized at runtime | `b2w/agents/rsl_rl_cfg.py:41` |
| `min_depth / max_depth` | 0.25 / 10.0 m | 0.25 / 10.0 m | `constants.py` |
| `JOYSTICK_TIMEOUT` | n/a | 15 s | `constants.py:16` |
This appendix is the minimum viable consistency check. Any second-order
port (different robot / different camera) should preserve these numbers
verbatim.
---
## Appendix B: zero-barrier quickstart from a bare machine
```bash
# === System packages (one-time, sudo) ====================================
sudo apt-get update
sudo apt-get install -y curl git build-essential ros-noetic-desktop \
ros-noetic-joy ros-noetic-tf2-tools \
python3-catkin-tools
# === Install miniconda (skip if you already have one) ====================
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-$(uname -m).sh
bash Miniconda3-latest-Linux-$(uname -m).sh -b -p $HOME/miniconda3
echo 'source $HOME/miniconda3/etc/profile.d/conda.sh' >> ~/.bashrc
source ~/.bashrc
# === Clone this package ==================================================
mkdir -p ~/code/odin_sru_nav/src
cd ~/code/odin_sru_nav/src
git clone https://github.com/<YOUR_FORK>/sru_nav_go2_ros1.git
# === Conda deps (onnxruntime / cv2 / netifaces / ...) ====================
cd sru_nav_go2_ros1
bash scripts/setup_conda_env.sh # 5–10 minutes the first time
bash scripts/setup_conda_env.sh --check # all PASS before continuing
# === Build (must be inside the conda env) ================================
conda activate sru_nav
cd ~/code/odin_sru_nav
catkin_make -DPYTHON_EXECUTABLE=$(which python3)
source devel/setup.bash
# === Launch (default: safe mode, requires joystick) ======================
cd src/sru_nav_go2_ros1
bash scripts/launch_sru_nav.sh
# Or: lifted-wheel / sim / closed-loop regression (no joystick) ===========
bash scripts/launch_sru_nav.sh --no-deadman
# === Self-check ==========================================================
bash scripts/verify_port.sh
```
When `verify_port.sh` is fully green, you are ready for real-robot
testing.