ROS2学习(2)------第一个程序
- 操作系统:ubuntu22.04
- IDE:Visual Studio Code
- 编程语言:C++11
- ROS版本:2
第一个程序主要是为了熟悉程序编写的环境,不在乎程序的内容,越简单越好,主要是为了验证环境的正确性,一般都是写一个Hello World,这次我们改个Hello ROS2吧,表示我要学习ROS2了,这次采用的是python语言,下一步再写一个C++程序。
1.创建工作空间
如果你还没有ROS 2的工作空间,可以通过以下命令创建一个:
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/
2.环境配置
每次打开新的终端时,都需要source ROS 2的setup文件
source install/local_setup.bash
3.创建包
首先,在src目录下创建一个新的ROS 2包
cd src
ros2 pkg create my_package --build-type ament_python --dependencies rclpy
终端输出如下:
going to create a new package
package name: my_package
destination directory: /media/dingxin/data/projects/ros2/ros2_ws/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['dingxin <76457551@qq.com>']
licenses: ['TODO: License declaration']
build type: ament_python
dependencies: ['rclpy']
creating folder ./my_package
creating ./my_package/package.xml
creating source folder
creating folder ./my_package/my_package
creating ./my_package/setup.py
creating ./my_package/setup.cfg
creating folder ./my_package/resource
creating ./my_package/resource/my_package
creating ./my_package/my_package/__init__.py
creating folder ./my_package/test
creating ./my_package/test/test_copyright.py
creating ./my_package/test/test_flake8.py
creating ./my_package/test/test_pep257.py[WARNING]: Unknown license 'TODO: License declaration'. This has been set in the package.xml, but no LICENSE file has been created.
It is recommended to use one of the ament license identitifers:
Apache-2.0
BSL-1.0
BSD-2.0
BSD-2-Clause
BSD-3-Clause
GPL-3.0-only
LGPL-3.0-only
MIT
MIT-0
会创建一个名为my_package的新Python包,并为其设置必要的依赖项。
4.编写我的第一个程序的脚本
在my_package文件夹中创建一个名为example_nodepy的脚本,文件位置如下图:
并向其中添加以下内容:
import rclpy
from rclpy.node import Nodeclass MinimalPublisher(Node):def __init__(self):super().__init__('minimal_publisher')self.get_logger().info('Hello ROS2!')def main(args=None):rclpy.init(args=args)minimal_publisher = MinimalPublisher()rclpy.spin(minimal_publisher)minimal_publisher.destroy_node()rclpy.shutdown()if __name__ == '__main__':main()
5.修改setup.py文件
确保你的setup.py文件包含对这些新脚本的入口点,修改 entry_points内容,把example_node加上
rom setuptools import find_packages, setuppackage_name = 'my_package'setup(name=package_name,version='0.0.0',packages=find_packages(exclude=['test']),data_files=[('share/ament_index/resource_index/packages',['resource/' + package_name]),('share/' + package_name, ['package.xml']),],install_requires=['setuptools'],zip_safe=True,maintainer='shuaige',maintainer_email='123456@qq.com',description='TODO: Package description',license='MIT-0',tests_require=['pytest'],entry_points={'console_scripts': ['example_node = my_package.example_node:main',],},
)
6.构建与运行
回到ros2_ws目录,输入以下命令:
colcon build --packages-select my_package --symlink-install
如果终端出现以下内容:
colcon:未找到命令
说明系统里没有安装colcon,需要安装colcon包,安装指令如下:
sudo apt install python3-colcon-common-extensions
再重新执行colcon build --packages-select my_package --symlink-install,终端输出如下:
Starting >>> my_package
Finished <<< my_package [0.51s] Summary: 1 package finished [0.66s]
Source最近构建的软件包:
source install/local_setup.bash
7.运行程序
ros2 run my_package example_node
终端输出如下:
[INFO] [1747215197.134689929] [minimal_publisher]: Hello ROS2!
第一个程序编写并且运行成功!,值得庆祝,鼓掌