This commit is contained in:
2026-08-04 18:14:49 +08:00
parent 74b3c89e1e
commit 61796dfa9f
8 changed files with 201 additions and 95 deletions
+50 -18
View File
@@ -78,10 +78,10 @@ cd D:\xs\ros2
# Linux / macOS
cd /path/to/ros2
```
### 2.2 构建镜像(首次 5-10 分钟)
```bash
docker compose -f docker/docker-compose.yml build
docker compose -p ros2 -f docker/docker-compose.yml build
```
**这一步做了什么**:
@@ -116,13 +116,14 @@ docker images
## Step 3: 启动容器
```bash
docker compose -f docker/docker-compose.yml up -d
docker compose -p ros2 -f docker/docker-compose.yml up -d
```
**参数说明**:
- `-p ros2`:compose project 名,所有容器归在 `ros2` 项目下(脱离默认 `docker`)
- `up -d`:后台启动(`-d` = detached)
- `container_name: ros2_dev`:容器名叫这个
- `network_mode: host`:DDS multicast 必须
- `networks: ros2_net`:自定义 bridge(脱离 docker_default,IP 段 172.20.0.0/24)
**预期**:
```
@@ -140,15 +141,19 @@ docker ps
**进入开发终端**:
```bash
docker exec -it ros2_dev bash -lc "source /root/ros2_ws/install/setup.bash && exec bash"
docker exec -it ros2_dev bash -lc "source /opt/ros/humble/setup.bash && source /root/ros2_ws/install/setup.bash && exec bash"
```
> ❗ **两个 `source` 都要有**:
> - `source /opt/ros/humble/setup.bash` — 加载 ROS2 环境变量(rclpy / colcon / ros2 CLI)
> - `source /root/ros2_ws/install/setup.bash` — 加载本项目 12 个编译产物(只有 build 后才有 `install/`,第 4 步前先注释掉这一行)
你应该看到类似:
```
root@docker-desktop:/root/ros2_ws#
```
> 💡 **小技巧**: `docker exec -it ros2_dev bash` 进入容器;**`exec` 前要保证容器在运行**(`docker ps` 看到 `Up` 状态)。
> 💡 **小技巧**: 用 `bash scripts/shell.sh` 一键进入(自动检查容器是否在跑 + 启动 + source)。
---
@@ -160,32 +165,43 @@ root@docker-desktop:/root/ros2_ws#
source /opt/ros/humble/setup.bash # 加载 ROS2 环境
cd /root/ros2_ws # 进入工作空间
```
### 4.2 编译所有包
```bash
bash scripts/build.sh
```
*(脚本封装了 `source /opt/ros/humble/setup.bash` + `colcon build --symlink-install` 12 个包;手动等价命令见下)*
*(脚本封装了 `source /opt/ros/humble/setup.bash` + `colcon build --symlink-install --executor sequential` 12 个包;手动等价命令见下)*
**手动等价命令**:
```bash
source /opt/ros/humble/setup.bash
cd /root/ros2_ws
colcon build --symlink-install
colcon build --symlink-install --executor sequential \
--packages-select \
py_pubsub cpp_pubsub py_srv py_action_demo \
cpp_robot_tf2 py_vision_demo py_params \
cpp_custom_interface py_lifecycle_composable \
cpp_qos_demo py_overlay_dds bringup
```
> ❗ **`--executor sequential` 必需**。colcon 默认并行构建 12 个包,`cpp_custom_interface` 的 rosidl export cmake 步骤会偶发失败(已知 CMake bug)。串行构建稳定通过。
**预期输出(末尾)**:
```
[INFO] [launch]: Default logging verbosity is set to INFO
Finished <<< py_pubsub [9.5s]
Finished <<< cpp_pubsub [42s]
Finished <<< py_srv [11s]
Finished <<< py_action_demo [17s]
Finished <<< cpp_robot_tf2 [49s]
Finished <<< py_vision_demo [11s]
Finished <<< bringup [18s]
Finished <<< py_params [12s]
Finished <<< cpp_custom_interface [38s]
Finished <<< py_lifecycle_composable [8s]
Finished <<< cpp_qos_demo [22s]
Finished <<< py_overlay_dds [9s]
Finished <<< bringup [12s]
Summary: 7 packages finished [2min 30s]
Summary: 12 packages finished [3min 30s]
```
**编译产物位置**:
@@ -197,18 +213,29 @@ Summary: 7 packages finished [2min 30s]
├── py_action_demo/
├── cpp_robot_tf2/
├── py_vision_demo/
├── py_params/
├── cpp_custom_interface/
├── py_lifecycle_composable/
├── cpp_qos_demo/
├── py_overlay_dds/
└── bringup/
```
### 4.3 跑测试(可选)
```bash
bash scripts/test.sh
```
**预期**:
```
Summary: 7 packages finished [25s]
Summary: 12 packages finished [1min 30s]
0 packages failed
build/py_pubsub/pytest.xml: 11 tests, 0 errors, 0 failures, 0 skipped
build/cpp_pubsub/test_results/cpp_pubsub/test_pub_sub.gtest.xml: 3 tests, ...
build/py_srv/pytest.xml: 7 tests, ...
...
Summary: 82 tests, 0 errors, 0 failures, 0 skipped
```
---
@@ -426,14 +453,19 @@ sudo apt install ros-humble-<pkg>
### Q8: 容器里中文显示乱码
**原因**: 容器没装中文字体
**解决**: 在容器内 `export LANG=C.UTF-8 LC_ALL=C.UTF-8`
### Q9: `colcon build` 报 `CMakeCache` 或 `export_cpp_custom_interface__rosidl_generator_cExport` 错误
### Q9: `colcon build` 报 `CMakeCache` 错误
**原因**: 之前构建的 CMakeCache 有问题
**原因**: 上次 build 残留 CMakeCache,或 `cpp_custom_interface` 的 rosidl export cmake 并行构建偶发失败
**解决**:
```bash
cd /root/ros2_ws
bash scripts/clean.sh # 等价于 rm -rf build install log
bash scripts/build.sh
bash scripts/clean.sh # 等价于 rm -rf build install log
bash scripts/build.sh # 脚本已带 --executor sequential
```
如果还挂,加上 `--cmake-clean-cache`:
```bash
colcon build --symlink-install --executor sequential --cmake-clean-cache \
--packages-select <12 个包>
```
### Q10: 容器跑一段时间后磁盘满了