当前位置: 首页 > 工具软件 > Pi-Bot > 使用案例 >

PIBOT移植ROS2记录(1)-环境搭建

夏飞跃
2023-12-01

pibot_bringup提供了ROS1的交互的node以及相关工具,我们通过移植pibot_bringupROS2,学习相关的ROS2开发

1. 环境搭建

前文RO2的安装与简单测试跑了一个简单的demo,具体环境搭建不再赘述

2. 创建包

我们创建ROS2的包

mkdir -p ~/ros2_ws/src
ros2 pkg create --build-type ament_cmake pibot_bringup --dependencies rclcpp
  • ros2 pkg create
    这里创建一个包,命令对应ROS1中的catkin_create_pkg pibot_bringup std_msgs rospy roscpp

3. 编译包

按照cpp_pubsub sample,在CMakeLists.txt添加执行文件, 即添加一个executable和其依赖

diff --git a/pibot_bringup/CMakeLists.txt b/pibot_bringup/CMakeLists.txt
index b561fa6..c1a7568 100644
--- a/pibot_bringup/CMakeLists.txt
+++ b/pibot_bringup/CMakeLists.txt
@@ -9,6 +9,16 @@ endif()
 find_package(ament_cmake REQUIRED)
 find_package(rclcpp REQUIRED)
 
+add_executable(pibot_driver src/main.cpp
+                      )
+
+ament_target_dependencies(pibot_driver 
+                      rclcpp)
+
+install(TARGETS
+  pibot_driver
+  DESTINATION lib/${PROJECT_NAME})
+
 if(BUILD_TESTING)
   find_package(ament_lint_auto REQUIRED)
   # the following line skips the linter which checks for copyrights

添加main.cpp,简单写个main函数

#include "rclcpp/rclcpp.hpp"

int main(int argc, char* argv[]) {
  rclcpp::init(argc, argv);

  rclcpp::shutdown();

  return 0;
}

添加完成使用下面命令编译即可

cd ~/ros2_ws
colcon build --symlink-install

输出success

➜  ros2_ws colcon build --symlink-install              
Starting >>> pibot_bringup
...
Finished <<< pibot_bringup [9.56s]                     

Summary: 1 package finished [9.91s]

4. colcon build参数说明

➜  ros2_ws git:(master) colcon build --help
usage: colcon build [-h] [--build-base BUILD_BASE] [--install-base INSTALL_BASE] [--merge-install]
                    [--symlink-install] [--test-result-base TEST_RESULT_BASE] [--continue-on-error]
                    [--executor {parallel,sequential}] [--parallel-workers NUMBER]
                    [--event-handlers [name1+ [name2- ...]]] [--ignore-user-meta]
                    [--metas [PATH [PATH ...]]] [--base-paths [PATH [PATH ...]]]
                    [--packages-ignore [PKG_NAME [PKG_NAME ...]]]
                    [--packages-ignore-regex [PATTERN [PATTERN ...]]] [--paths [PATH [PATH ...]]]

Event handler arguments:
  --event-handlers [name1+ [name2- ...]]
                        Enable (+) or disable (-) event handlers (default: compile_commands+ console_cohesion-
                        console_direct- console_package_list- console_start_end+ console_stderr+
                        desktop_notification+ event_log+ log+ log_command+ status+ store_result+ summary+
                        terminal_title+)
  ...
  • --symlink-install
    install使用符号链接而不是复制文件,如
➜  ros2_ws ls -al install/pibot_bringup/lib/pibot_bringup/pibot_driver 
lrwxrwxrwx 1 david david 52 5月   3 21:16 install/pibot_bringup/lib/pibot_bringup/pibot_driver -> /home/pibot/ros2_ws/build/pibot_bringup/pibot_driver

可以看到install中生成的是一个软连接执行build中的对应文件

测试的时候建议使用这个选项,后面涉及到launch的修改,跟ROS1不同的是,如果不用这个--symlink-install选项,是需要重新编译才能生效的

  • --packages-select
    只编译指定包,如
    colcon build --packages-select pibot_bringup

  • --packages-ignore
    忽略指定包

  • --event-handlers
    添加编译时的事件处理
    colcon --log-base /dev/null build --symlink-install --event-handlers console_direct+ log-

直接在终端输出编译日志,可以看到 需要打开某项功能只需要后面添加+,删除则添加-
默认选项如下
(default: compile_commands+ console_cohesion- console_direct- console_package_list- console_start_end+ console_stderr+ desktop_notification+ event_log+ log+ log_command+ status+ store_result+ summary+ terminal_title+)

我们可以把喜欢的配置设置为alias写入到~/.bashrc中,如

alias pbmake="colcon --log-base /dev/null build --symlink-install --event-handlers console_direct+ log-"

这样编译就可以直接使用pbmake编译了

本文代码https://gitee.com/pibot/pibot_bringup/tree/70573f847e238957630352d5baa3444cd8539395

 类似资料: