# ros2-learn **Repository Path**: passerjia02/ros2-learn ## Basic Information - **Project Name**: ros2-learn - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-03-01 - **Last Updated**: 2025-12-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ROS2学习 ## 学习资料 - [古月居](https://book.guyuehome.com/) - [鱼香ros](https://fishros.com/d2lros2/#/) - tutorials代码下载: `git clone https://gitee.com/guyuehome/ros2_21_tutorials.git` `https://github.com/ros2/examples` ## 编译工具安装 `sudo apt-get install python3-colcon-common-extensions` ## 编译命令 ```sh # 初始化目录(功能包) # ros2 pkg create --build-type <包类型 cpp/python> <包名> ros2 pkg create --build-type ament_cmake learning_pkg_c ros2 pkg create --build-type ament_python learning_pkg_python # 创建带ros2通信的功能包 (rclcpp为ros2通信依赖库,xf_msg_info为自定义通信接口) ros2 pkg create xf_msg_use_cpp --build-type ament_cmake --dependencies rclcpp xf_msg_info --node-name msg_user # 编译 会在工作空间生成build、log和install文件夹 # 可以通过 --build-base参数和--install-base,指定构建目录和安装目录 --merge-install 合并安装,不在build目录下创建字目录 colcon build # 编译工作空间(根目录,也就是功能包的上级目录)所有功能包 colcon build --packages-select learning_pkg_python # 编译指定功能包 colcon build --path learning_pkg_python # 编译指定目录下所有功能包 ``` ## 运行命令 ```sh # 在工作空间,先source 一下资源 source install/setup.bash # 启动节点 ros2 run 节点定义在setup.py的entry_points中 ros2 run learning_pkg_python node_helloworld # 通过配置文件启动节点 # Python文件 ros2 launch learning_pkg_python pyLearnLaunch.py # XML文件 ros2 launch learning_pkg_python pyLearnLaunch.xml ``` ## 数据包相关 ```sh # 录制bag包 生成bag包的根目录在 运行命令的终端目录 ros2 bag record /topic-name # 记录多个 ros2 bag record topic-name1 topic-name2 # 记录所有 ros2 bag record -a # 指定输出文件名 ros2 bag record -o file-name topic-name # 录制为mcap文件便于可视化 ros2 bag record -o output.mcap -s mcap # 查看bag包内容 ros2 bag info bag-file-name ## ros2播包必须包含数据包内的自定义数据类型(如果有),否则会报错Library could not be found # 播放bag包 ros2 bag play # 使用ros2的topic的指令来查看数据 ros2 topic echo /chatter # 循环播放 ros2 bag play rosbag2_2021_10_03-15_31_41_0.db3 -l # 播放包内的单个topic ros2 bag play rosbag2_2021_10_03-15_31_41_0.db3 --topics /chatter # 使用接口实现自定义数据包 # 创建功能包 ros2 pkg create example_ros2_interfaces --build-type ament_cmake --dependencies rosidl_default_generators # 话题接口放到msg文件夹下, 服务接口放到srv文件夹下 # 编译后会生成对应的cpp和py文件 # 查看接口列表 ros2 interface list # 查看接口信息 ros2 interface show example_ros2_interfaces/msg/ObjectPosition ``` ## 其他命令 ```sh # 查看功能包列表 ros2 pkg list # 查看可执行文件 ros2 pkg executables # 列出turtlesim功能包的所有可执行文件 ros2 pkg executables turtlesim # 查看节点列表 ros2 node list # 查看节点信息 ros2 node info # 查看话题列表 ros2 topic list # 查看话题详细信息(--verbose) ros2 topic info /iflytek/vehicle_service --verbose # 使用conda管理环境 unset PYTHONPATH conda activate source /opt/ros//setup.zsh # 使用rosbridge 虚安装requirements.txt中的python依赖 sudo apt remove ros-jazzy-rosbridge-suite sudo apt install ros-jazzy-rosbridge-suite ros2 launch rosbridge_server rosbridge_websocket_launch.xml # 自定义端口或路径 # ros2 launch rosbridge_server rosbridge_websocket_launch.xml port:=9090 url_path:=/rosbridge # rosbridge使用 sudo apt install ros-humble-rosbridge-suite pip install tornado netifaces pymongo cbor2 # 启动rosbridge WebSocket服务器(默认端口9090) ros2 launch rosbridge_server rosbridge_websocket_launch.xml # 使用自定义的rosbridge xml配置文件 ros2 launch rosbridge_suite/rosbridge_server/launch/rosbridge_websocket_launch.xml ``` ## protobuf转换 ```sh # 编译生成.proto的描述文件(Schema 文件) protoc --descriptor_set_out=my_schema.bin --include_imports file1.proto file2.proto file3.proto # find modules/ -type d -name "node_modules" -prune -o -type f -name "*.proto" -print |xargs ~/project/txmap/thirdParty/protoc/bin/protoc --descriptor_set_out=my_schema.bin --include_imports # 如果 proto 文件分布在不同的目录中 protoc --proto_path=./proto_dir1 --proto_path=./proto_dir2 \ --descriptor_set_out=my_schema.bin --include_imports \ ./proto_dir1/file1.proto ./proto_dir2/file2.proto # 将bag包转换成mcap包 mcap convert input.bag output.mcap \ --proto-schemas my_schema.bin \ --compression zstd \ --chunk-size 4MB # compression 压缩算法 zstd/lz4/zlib # chunk-size 压缩块大小,优化大文件读写性能 ``` ## rosbags使用 [rosbags说明文档](https://ternaris.gitlab.io/rosbags/) ```sh # bag 转mcap rosbags-convert --src input.bag --dst output_dir/ --dst-storage mcap ``` ## 局域网节点 ```sh # 所有主机设置统一的域id export ROS_DOMAIN_ID=42 export ROS_LOCALHOST_ONLY=0 export CYCLONEDDS_URI=/home/jmc/project/ros_bridge/cyclonedds_config.xml export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp # 或配置为使用fastdds export RMW_IMPLEMENTATION=rmw_fastrtps_cpp export FASTRTPS_DEFAULT_PROFILES_FILE=/vad/ros2test/fastdds_config.xml ``` ## QA 1. wsl 中使用`ros2 daemon status`显示 Connection timed out - 解决方法:重启ros2 daemon守护进程 `ros2 daemon start` 2. 其他依赖 ```sh pip install empy==3.3.4 catkin_pkg lark ``` 3. conda 造成的依赖冲突问题 ```sh export PYTHONPATH=$PYTHONPATH:/home/jmc/miniconda3/envs/ros2/lib/python3.10/site-packages colcon build --symlink-install ```