pibot_bringup提供了ROS1的交互的node以及相關(guān)工具,我們通過移植pibot_bringup至ROS2,學(xué)習(xí)相關(guān)的ROS2開發(fā)
1. 環(huán)境搭建
前文RO2的安裝與簡單測試跑了一個(gè)簡單的demo,具體環(huán)境搭建不再贅述
2. 創(chuàng)建包
我們創(chuàng)建ROS2的包
mkdir -p ~/ros2_ws/src
ros2 pkg create --build-type ament_cmake pibot_bringup --dependencies rclcpp
-
ros2 pkg create
這里創(chuàng)建一個(gè)包,命令對應(yīng)ROS1中的catkin_create_pkg pibot_bringup std_msgs rospy roscpp
3. 編譯包
按照cpp_pubsub sample,在CMakeLists.txt添加執(zhí)行文件, 即添加一個(gè)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,簡單寫個(gè)main函數(shù)
#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參數(shù)說明
? 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使用符號鏈接而不是復(fù)制文件,如
? 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中生成的是一個(gè)軟連接執(zhí)行build中的對應(yīng)文件
--packages-select
只編譯指定包,如
colcon build --packages-select pibot_bringup--packages-ignore
忽略指定包--event-handlers
添加編譯時(shí)的事件處理
colcon --log-base /dev/null build --symlink-install --event-handlers console_direct+ log-
直接在終端輸出編譯日志,可以看到 需要打開某項(xiàng)功能只需要后面添加
+,刪除則添加-
默認(rèn)選項(xiàng)如下
(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+)
我們可以把喜歡的配置設(shè)置為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