ROS2教程(3) - colcon构建包、创作工作空间、创建包 - Linux

使用colcon构建包

install

# install colcon
sudo apt install python3-colcon-common-extensions

start

mkdir -p ~/ros2_ws/src && cd ~/ros2_ws
git clone https://github.com/ros2/examples src/examples -b humble
# 构建
colcon build --symlink-install
# tree
.
├── build
├── install
├── log
└── src
# 运行测试
colcon test
# 环境
source install/setup.bash
# run
ros2 run examples_rclcpp_minimal_subscriber subscriber_member_function # 订阅节点
ros2 run examples_rclcpp_minimal_publisher publisher_member_function # 发布节点

创建自己的包

colcon 支持多种构建类型,建议的构建类型是 ament_cmakeament_python ,此外。还支持纯cmake包。

为了方便起见,您可以使用工具ros2 pkg create 来创建包。(对于catkin用户来说这相当于catkin_create_package)。

设置colcon_cd

colcon_cd 可以快速将路径切换到ros包的工作路径。

colcon_cd <package_name>
echo "source /usr/share/colcon_cd/function/colcon_cd.sh" >> ~/.bashrc
echo "export _colcon_cd_root=/opt/ros/humble/" >> ~/.bashrc

提示

如果你不想构建特定的包,请在目录中设置一个名为COLCON_IGNORE的空文件,该文件不会被检索。

如果你想避免在CMake包中配置和构建测试,你可以通过:--cmake-args -DBUILD_TESTING=0

如果你想从一个包中运行一个特定的测试:colcon test --packages-select YOUR_PKG_NAME --ctest-args -R YOUR_TEST_IN_PKG

创建工作空间

mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/src
git clone https://github.com/ros/ros_tutorials.git -b humble

# 解决依赖关系
cd .. && rosdep install -i --from-path src --rosdistro humble -y
# 如果前面的配置没有问题的话,终端应该输出
# #All required rosdeps installed successfully

# 用 colcon build
colcon build
# 控制台将返回类似以下消息
Starting >>> turtlesim
Finished <<< turtlesim [5.49s]
Summary: 1 package finished [5.58s]

ls
# build  install  log  src

source install/local_setup.bash

# 此时就可以运行了
ros2 run turtlesim turtlesim_node
  • 拓展知识关于 colcon

--packages-up-to builds the package you want, plus all its dependencies, but not the whole workspace (saves time)
构建您想要的包,以及它的所有依赖项,但不是整个工作区(节省时间)。

--symlink-install saves you from having to rebuild every time you tweak python scripts
使您不必每次调整python脚本时都重新构建。

--event-handlers console_direct+ shows console output while building (can otherwise be found in the log directory)
在构建时显示控制台输出(否则可以在目录中找到)。

--executor sequential processes the packages one by one instead of using parallelism
逐个处理包,而不是使用并行。

创建包

# ros2 中创建包的语法
ros2 pkg create --build-type ament_cmake --license Apache-2.0 <package_name>
# eg  在本教程中,您将使用可选的参数 --node-name 在包中创建一个简单的Hello World类型的可执行文件。
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_cmake --license Apache-2.0 --node-name my_node my_package
# 此时可以在~/ros2_ws/src/my_package/下看到有自动生成的一堆文件
# 这时就可以构建我们的包了
colcon build
# 由于之前的 ros_tutorials/ 已经编译过了,所以可以指定构建刚刚新创建的包
colcon build --packages-select my_package
cd ros2_ws && source install/local_setup.bash
ros2 run my_package my_node # 输出 hello world my_package package
  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值