之前都是学习一些命令行或者demo操作,现在开始创建自己的开发包
创建包的命令:
ros2 pkg create <pkg-name> --dependencies [deps]
举例:
C++版本:
ros2 pkg create <pkg-name> --dependencies [deps] --build-type ament_cmake
python 版本:
ros2 pkg create <pkg-name> --dependencies [deps] --build-type ament_python
然后,您可以修改package.xml更新软件包信息(例如依赖项,描述和作者身份)。
cmake_minimum_required(VERSION 3.5)
project(cpp_srvcli)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(tutorial_interfaces REQUIRED)
add_executable(server src/add_two_ints_server.cpp)
ament_target_dependencies(server
rclcpp tutorial_interfaces)
add_executable(client src/add_two_ints_client.cpp)
ament_target_dependencies(client
rclcpp tutorial_interfaces)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
install(TARGETS
server
client
DESTINATION lib/${PROJECT_NAME})
ament_package()
上面是之前的CMakeLists.txt
,主要用到add_executable()
来增加可执行文件,ament_target_dependencies
来添加依赖,要安装启动文件和节点,可以使用install()
宏,该宏位于文件末尾,但位于ament_package()
宏之前。
[develop]
script-dir=$base/lib/py_srvcli
[install]
install-scripts=$base/lib/py_srvcli
标志这脚本和安装路径
from setuptools import setup
package_name = 'py_srvcli'
setup(
name=package_name,
version='0.0.0',
packages=[package_name],
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='utry',
maintainer_email='jinmenglei',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'service = py_srvcli.service_member_function:main',
'client = py_srvcli.client_member_function:main',
],
},
)
包含版本信息和入口等
通过命令安装colcon
sudo apt install python3-colcon-common-extensions
创建ros2_example_ws
作为下面的工作空间
mkdir -p ~/ros2_example_ws/src
cd ~/ros2_example_ws
我们这里使用官方的例子
git clone https://github.com/ros2/examples src/examples
切换分支
cd ~/ros2_example_ws/src/examples/
git checkout $ROS_DISTRO
cd ~/ros2_example_ws
代码的架构:
.
└── src
└── examples
├── CONTRIBUTING.md
├── LICENSE
├── rclcpp
├── rclpy
└── README.md
51 directories, 149 files
执行下面命令编译
colcon build --symlink-install
这允许通过更改源空间中的文件(例如Python文件或其他未编译的资源)来更改已安装的文件,以加快迭代。
执行test
命令就行
colcon test
. install/setup.bash
测试一个订阅和发布
ros2 run examples_rclcpp_minimal_subscriber subscriber_member_function
ros2 run examples_rclcpp_minimal_publisher publisher_member_function
ros2
编译时基于ament_cmake
,这个和之前的catkin
有不少差别
这边直接上链接地址吧,直接看这个文档就型
主要时这几个部分,
传送门:https://index.ros.org/doc/ros2/Tutorials/Ament-CMake-Documentation/
主要介绍怎么创建自己的工作空间,创建之后,记得把source yourworkspace
添加到.bashrc
里去