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
+80 -38
View File
@@ -71,12 +71,12 @@ CMD ["bash"]
```powershell
# Windows
docker compose -f D:\xs\ros2\docker\docker-compose.yml build
docker compose -p ros2 -f D:\xs\ros2\docker\docker-compose.yml build
```
```bash
# Linux
docker compose -f docker/docker-compose.yml build
docker compose -p ros2 -f docker/docker-compose.yml build
```
镜像名:`ros2-humble-dev:latest`,约 3GB。
@@ -115,37 +115,55 @@ RUN pip3 install ultralytics==8.0.0 numpy==1.26
[`docker/docker-compose.yml`](../docker/docker-compose.yml):
```yaml
name: ros2 # 独立 compose project(脱离默认 docker 分组)
services:
ros2:
build: .
image: ros2-humble-dev:latest
container_name: ros2_dev
privileged: true # 调试用
stdin_open: true # docker exec -it
privileged: true # 调试用
stdin_open: true # docker exec -it
tty: true
network_mode: host # DDS multicast 必须
networks: # 自定义 bridge(脱离 docker_default)
ros2_net:
ipv4_address: 172.20.0.10 # 固定 IP,便于 ROS_STATIC_PEERS
environment:
- ROS_DOMAIN_ID=0
# 不要设 RMW_IMPLEMENTATION=rmw_cyclonedds_cpp:
# 镜像没装 cyclone dds,CMake 配置会失败。
- RCUTILS_COLORIZED_OUTPUT=1
volumes:
- ..:/root/ros2_ws # bind mount 本机 D:\xs\ros2
- ..:/root/ros2_ws # bind mount 本机 D:\xs\ros2
working_dir: /root/ros2_ws
command: ["bash", "-lc", "tail -f /dev/null"] # 容器永不退
networks:
ros2_net:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/24
gateway: 172.20.0.1
```
### 3.2 为什么 host network
### 3.2 为什么自定义 bridge 而不是 host network
ROS2 默认用 DDS multicast 在同一网段自动发现节点。多容器或跨主机时,**bridge 网络
会拦截 multicast**,导致节点看不见彼此。
ROS2 默认用 DDS multicast 在同一网段自动发现节点。
**默认 docker bridge 网络会拦截 multicast**,导致容器内节点看不见彼此。
`network_mode: host` 让容器用宿主机的网络栈,直接走 multicast。
本仓库选择**自定义 bridge**(`ros2_net`,子网 `172.20.0.0/24`),原因:
- 跟系统 `docker_default` 隔离,不让无关容器"窜"进 ROS 节点组
- 容器固定 IP(`172.20.0.10`),便于 `ROS_STATIC_PEERS` 配置
- bridge 内 multicast 在同一 docker 网络内**能正常通**——本仓库默认 fastdds + 单机场景下,所有节点都在 `ros2_dev` 容器里,bridge 完全够用
如果做**跨主机 ROS2 部署**(PC ↔ RDK X5 ↔ RK3506,见 `100-embedded-deployment.md`),需要 `network_mode: host` 或 host gateway。
### 3.3 为什么 bind mount 整个工程
@@ -158,28 +176,28 @@ ROS2 默认用 DDS multicast 在同一网段自动发现节点。多容器或跨
```powershell
# 构建镜像
docker compose -f D:\xs\ros2\docker\docker-compose.yml build
docker compose -p ros2 -f D:\xs\ros2\docker\docker-compose.yml build
# 启动(后台)
docker compose -f D:\xs\ros2\docker\docker-compose.yml up -d
docker compose -p ros2 -f D:\xs\ros2\docker\docker-compose.yml up -d
# 状态
docker compose -f D:\xs\ros2\docker\docker-compose.yml ps
docker compose -p ros2 -f D:\xs\ros2\docker\docker-compose.yml ps
# 进开发终端
docker exec -it ros2_dev bash -lc "source /root/ros2_ws/install/setup.bash && exec bash"
# 进开发终端(必须先 source ROS2 + 本项目 install)
docker exec -it ros2_dev bash -lc "source /opt/ros/humble/setup.bash && source /root/ros2_ws/install/setup.bash && exec bash"
# 一次性跑命令
docker exec ros2_dev bash -lc "cd /root/ros2_ws && colcon build --packages-select py_pubsub"
# 关
docker compose -f D:\xs\ros2\docker\docker-compose.yml down
docker compose -p ros2 -f D:\xs\ros2\docker\docker-compose.yml down
# 重启
docker compose -f D:\xs\ros2\docker\docker-compose.yml restart
docker compose -p ros2 -f D:\xs\ros2\docker\docker-compose.yml restart
# 删容器(保留镜像)
docker compose -f D:\xs\ros2\docker\docker-compose.yml down
docker compose -p ros2 -f D:\xs\ros2\docker\docker-compose.yml down
# 删镜像
docker rmi ros2-humble-dev:latest
@@ -192,30 +210,49 @@ docker logs -f ros2_dev
## 5. 一键启动脚本
### 5.1 start.ps1 (Windows)
```powershell
powershell D:\xs\ros2\start.ps1
本仓库的所有"一键"命令都在 `scripts/` 目录下,**没有**项目根的 `start.ps1` /
`start.sh` / `build.sh`(老文档里残留的引用一律作废,统一指向 `scripts/`)。
### 5.1 进入开发终端(交互式 shell)
```bash
bash scripts/shell.sh
```
等价于:
```powershell
docker compose build # 首次 5-10min
docker compose up -d
docker exec ros2_dev bash -lc "cd /root/ros2_ws && bash build.sh"
docker exec -it ros2_dev bash -lc "source install/setup.bash && exec bash"
- `docker ps` 看容器是否在跑
- 如果没跑 → 自动 `docker run -d -it ... ros2-humble-dev:latest bash` 启起来
- `docker exec -it ros2_dev bash -lc "source /opt/ros/humble/setup.bash && source /root/ros2_ws/install/setup.bash && cd /root/ros2_ws && exec bash"`
### 5.2 编译 12 个包(容器内)
```bash
bash scripts/build.sh
```
等价于:
```bash
source /opt/ros/humble/setup.bash
cd /root/ros2_ws
colcon build --symlink-install --executor sequential \
--packages-select <12 个包>
```
### 5.2 start.sh (Linux/macOS)
> ❗ **`--executor sequential` 必需**。colcon 默认 parallel,12 个包并行构建
> 时 `cpp_custom_interface` 的 rosidl export cmake 会偶发 CMake 报错。
> 串行构建 100% 稳定。详见 `doc/80-package-build.md`。
### 5.3 跑所有测试
```bash
./start.sh
bash scripts/test.sh
```
等价于 `colcon test --executor sequential --packages-select <12 个包>` + `colcon test-result --all`
### 5.4 跑 launch demo
```bash
bash scripts/launch.sh pubsub_launch 30 # 跑 30 秒自动停
bash scripts/launch.sh full_demo_launch -1 # -1 = 一直跑(后台)
```
### 5.3 build.sh (容器内)
### 5.5 清理构建产物
```bash
bash build.sh
# 1) source /opt/ros/humble/setup.bash
# 2) colcon build --symlink-install --packages-select <all>
# 3) ls install/
# 4) 打印运行命令速查
bash scripts/clean.sh # 等价于 rm -rf build install log
```
---
@@ -337,22 +374,27 @@ services:
## 9. 在本仓库里跑
### 9.1 一键启
### 9.1 一键启(推荐用 Makefile)
```powershell
powershell D:\xs\ros2\start.ps1
make build # 首次构建镜像
make up # 后台启动容器
make shell # 进入开发终端(自动 source ROS2 + install)
make colcon-build # 编译 12 包
make colcon-test # 跑 82 测试
make full-demo # 跑 11 节点 full_demo
```
### 9.2 进入后跑测试
```bash
docker exec ros2_dev bash -lc "source /opt/ros/humble/setup.bash && cd /root/ros2_ws && colcon test --packages-select py_pubsub cpp_pubsub py_srv py_action_demo cpp_robot_tf2 py_vision_demo bringup"
bash scripts/test.sh # 推荐:自带 --executor sequential
```
### 9.3 跑 demo
```bash
docker exec ros2_dev bash -lc "source /root/ros2_ws/install/setup.bash && ros2 launch bringup full_demo_launch.py"
docker exec ros2_dev bash -lc "source /opt/ros/humble/setup.bash && source /root/ros2_ws/install/setup.bash && ros2 launch bringup full_demo_launch.py"
```
---