介绍如何使用在ros2下通过python来编程

Python

ros2与Python入门教程-目录 - 创客智造

如何使用在ros2下通过python来编程

创建ros2软件包

ros2与Python入门教程-创建ros2包 - 创客智造

新建ros2包

cd ~/dev_ws/src
ros2 pkg create --build-type ament_python --node-name python_node python_package

############## output ###########
~/dev_ws/src$ ros2 pkg create --build-type ament_python --node-name python_node python_package
going to create a new package
package name: python_package
destination directory: /home/ubuntu/dev_ws/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['ubuntu <[email protected]>']
licenses: ['TODO: License declaration']
build type: ament_python
dependencies: []
node_name: python_node
creating folder ./python_package
creating ./python_package/package.xml
creating source folder
creating folder ./python_package/python_package
creating ./python_package/setup.py
creating ./python_package/setup.cfg
creating folder ./python_package/resource
creating ./python_package/resource/python_package
creating ./python_package/python_package/__init__.py
creating folder ./python_package/test
creating ./python_package/test/test_copyright.py
creating ./python_package/test/test_flake8.py
creating ./python_package/test/test_pep257.py
creating ./python_package/python_package/python_node.py

文件结构

# 文件结构
~/dev_ws/src/python_package$ tree
.
├── package.xml
├── python_package
│   ├── __init__.py
│   └── python_node.py
├── resource
│   └── python_package
├── setup.cfg
├── setup.py
└── test
    ├── test_copyright.py
    ├── test_flake8.py
    └── test_pep257.py

3 directories, 9 files

编译包

# 编译包
cd ~/dev_ws/
colcon build --packages-select python_package

# output
~/dev_ws$ colcon build --packages-select python_package
Starting >>> python_package
Finished <<< python_package [0.55s]          

Summary: 1 package finished [0.75s]

# 运行 
. install/setup.bash
ros2 run python_package python_node

# output
~/dev_ws$ ros2 run python_package python_node
Hi from python_package.