基于windows的ROS2开发。
官方参考文档:
Creating your first ROS 2 package — ROS 2 Documentation: Galactic documentation
(1) 创建开发包ROS2中创建开发包的命令格式是
ros2 pkg create --build-type ament_cmake
其中 `--build-type ament_cmake` 是ROS2特有的配置参数,当然还有,
--ament-cmake-args [* [* …]]
例如,
ament build . --force-cmake-configure --cmake-args -DCMAKE_BUILD_TYPE=Debug -- --ament-cmake-args -DCMAKE_BUILD_TYPE=Release
下面我们首先创建目录,然后创建开发包,
cd dev_ws\src
ros2 pkg create --build-type ament_cmake --node-name my_node my_package
输入后会输出下面这一堆信息,
going to create a new package
package name: my_package
destination directory: /home/user/dev_ws/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: [' ']
licenses: ['TODO: License declaration']
build type: ament_cmake
dependencies: []
node_name: my_node
creating folder ./my_package
creating ./my_package/package.xml
creating source and include folder
creating folder ./my_package/src
creating folder ./my_package/include/my_package
creating ./my_package/CMakeLists.txt
creating ./my_package/src/my_node.cpp
这样,我们创建了一个node名字叫my_node的开发包,包的名字叫my_package。听起来有点拗口,如果我们在编译成功后打开vs2019项目,那么相当于解决方案my_package下有一个子项目,名字就叫my_node,如下,
现在我们来看一下这个开发包。
一个完整的ROS开发包通常包括两个主要配置文件:package.xml
和 CMakeLists.txt,这里我们一行代码都没写,生成的主要文件内容如下,
package.xml
my_package
0.0.0
TODO: Package description
tansh
TODO: License declaration
ament_cmake
ament_lint_auto
ament_lint_common
ament_cmake
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(my_package)
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)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package( REQUIRED)
add_executable(my_node src/my_node.cpp)
target_include_directories(my_node PUBLIC
$
$)
target_compile_features(my_node PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17
install(TARGETS my_node
DESTINATION lib/${PROJECT_NAME})
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()
ament_package()
src/my_node.cpp
#include
int main(int argc, char ** argv)
{
(void) argc;
(void) argv;
printf("hello world my_package package\n");
return 0;
}
(2)编译开发包
现在,我们切换到dev_ws目录下,输入下面的指令开始编译
colcon build --merge-install
其中--merge-install取决于开发人员的需求,是告诉编译器,如果有多个项目,就直接把结果放在一个包里面;这样可以避免目录过深。当然,这样的缺点就是项目间的隔离效果不行,可能会有库的冲突。
当然,你可能会把很多开发包放在/dev_ws/src下面,那么,当你只想编译my_package这个开发包的时候,可以使用下面的指令,
colcon build --packages-select my_package --merge-install
同样,如果以前编译时用了--merge-install指令,这里也必须带上,否则会报错,
[0.542s] colcon ERROR colcon build: The install directory 'install' was created with the layout 'merged'. Please remove the install directory, pick a different one or add the '--merge-install' option.
接下来就可以运行这个项目了,首先启动环境,
call install/local_setup.bat
运行,
ros2 run my_package my_node
然后就能看出输出,
hello world my_package package
本质上是运行了一次my_node.exe这个可执行文件,该文件的位置一般在
dev_ws\build\my_package\Release\my_node.exe
dev_ws\build\my_package\Debug\my_node.exe
因为目前我们这个文件没有用到任何ROS2的依赖,所以你可以直接切换到这个目录执行,效果完全一样,
cd dev_ws\build\my_package\Release
my_node
在实际开发过程中,当你需要用到ROS2的依赖库的时候,如果不想用"ros2 run"这样的指令,只需要配置好路径,也可以像普通程序那样正常执行了。
参考资料
build - Build Packages — colcon documentation
本文结束