怎樣在美國做網(wǎng)站百度網(wǎng)站大全
ROS2系列文章目錄
ROS2中nav_msgs/msg/Path 數(shù)據(jù)含義及使用
ROS2中std_msgs/msg/Header 數(shù)據(jù)含義及使用
ROS中TF變換詳解
文章目錄
- ROS2系列文章目錄
- ROS2中l(wèi)aunch編寫及參數(shù)含義(xml、python)
- 一、ROS官方介紹
- 二、實現(xiàn)案例
- 1.編寫主函數(shù)、CMakeLists.txt及package.xml
- 2.編寫啟動節(jié)點的launch文件
- 3.編譯并運行
ROS2中l(wèi)aunch編寫及參數(shù)含義(xml、python)
一、ROS官方介紹
ROS 2中的啟動系統(tǒng)負責(zé)幫助用戶描述其系統(tǒng)的配置,然后按描述執(zhí)行。系統(tǒng)的配置包括運行什么程序,在哪里運行,傳遞什么參數(shù),以及ROS特定的約定,這些約定通過為每個組件提供不同的配置,使其易于在整個系統(tǒng)中重用組件。它還負責(zé)監(jiān)測啟動過程的狀態(tài),并報告和/或?qū)@些過程的狀態(tài)變化作出反應(yīng)。
ROS2官方說明:http://docs.ros.org/en/humble/Tutorials/Intermediate/Launch/Creating-Launch-Files.html#write-the-launch-file
二、實現(xiàn)案例
1.編寫主函數(shù)、CMakeLists.txt及package.xml
此處創(chuàng)建learn_ros2功能包,在learn_ros2/src目錄下建立main.cpp,該函數(shù)具體實現(xiàn)功能參照博文:ROS2中nav_msgs/msg/Path 數(shù)據(jù)含義及使用
#include <nav_msgs/msg/path.hpp>
#include <rclcpp/rclcpp.hpp>
#include <string>
#include <unistd.h>
using namespace std;
class My_node:public rclcpp::Node{
public:My_node(std::string node_name):Node(node_name){}
};
int main(int argc, char**argv){rclcpp::init(argc,argv);//節(jié)點初始化std::shared_ptr<My_node> node_ptr = std::make_shared<My_node>("test_node");rclcpp::Publisher<nav_msgs::msg::Path>::SharedPtr nav_pub = node_ptr->create_publisher<nav_msgs::msg::Path>("/global_path",1);nav_msgs::msg::Path path;geometry_msgs::msg::PoseStamped pose;path.header.frame_id = "world";while (rclcpp::ok()){path.header.stamp = node_ptr->now();path.poses.clear();for (int i = 0; i < 10; i++){pose.header.frame_id = "world";pose.header.stamp = node_ptr->now();pose.pose.position.set__x(i);pose.pose.position.set__y(0.2*i*i+2);pose.pose.position.set__z(0);pose.pose.orientation.set__x(0);pose.pose.orientation.set__y(0);pose.pose.orientation.z = 0;pose.pose.orientation.w = 1;path.poses.push_back(pose);}nav_pub->publish(path);sleep(1);std::cout<<"已發(fā)送path"<<std::endl;} std::cout<<"退出程序"<<std::endl;
}
指定功能包learn_ros2的CMakeLists.txt如下:
cmake_minimum_required(VERSION 3.8)
project(learn_ros2)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(rclpy REQUIRED)
find_package(std_msgs REQUIRED)
find_package(nav_msgs REQUIRED)
if(BUILD_TESTING)find_package(ament_lint_auto REQUIRED)# the following line skips the linter which checks for copyrights# comment the line when a copyright and license is added to all source filesset(ament_cmake_copyright_FOUND TRUE)# the following line skips cpplint (only works in a git repo)# comment the line when this package is in a git repo and when# a copyright and license is added to all source filesset(ament_cmake_cpplint_FOUND TRUE)ament_lint_auto_find_test_dependencies()
endif()
#添加可執(zhí)行文件名
add_executable(test_node src/main.cpp)
#給可執(zhí)行文件鏈接ros2的標(biāo)準庫及其他依賴
ament_target_dependencies(test_node rclcpp std_msgs nav_msgs)
#將可執(zhí)行文件安裝在指定目錄下
install(TARGETS test_node
DESTINATION lib/${PROJECT_NAME})
#將launch目錄下的文件安裝在指定目錄下
install(DIRECTORY launchDESTINATION share/${PROJECT_NAME})
ament_package()
2.編寫啟動節(jié)點的launch文件
在learn_ros2目錄下建立launch文件夾,并在文件目錄中新建python及xml文件如下:
test_launch.launch.py,test_xml_launch.xml
首先使用python實現(xiàn)test_launch.launch.py文件,具體含義參考注釋
from launch import LaunchDescription
from launch_ros.actions import Nodedef generate_launch_description():parameters_basic1 = Node(package="learn_ros2",executable="test_node",)# 創(chuàng)建LaunchDescription對象launch_description,用于描述launch文件launch_description = LaunchDescription([parameters_basic1])# 返回讓ROS2根據(jù)launch描述執(zhí)行節(jié)點return launch_description
該處使用的url網(wǎng)絡(luò)請求的數(shù)據(jù)。
使用XML實現(xiàn)test_xml_launch.xml文件,具體含義參考注釋
<launch><!-- 啟動節(jié)點,命名為/name1/test_node --><node pkg="learn_ros2" exec="test_node" name="test_node" namespace="name_1"/><!-- 啟動節(jié)點,命名為/name2/test_node --><node pkg="learn_ros2" exec="test_node" name="test_node" namespace="name_2"/><!-- 啟動節(jié)點,命名為/name3/test_node,同時將話題/global_path,映射為/test_node3/global_path--><node pkg="learn_ros2" exec="test_node" name="test_node" namespace="name_3"><remap from="/global_path" to="/test_node3/global_path"/></node> </launch>
3.編譯并運行
注意:編寫完成launch文件后,要使用ros2編譯命令對功能包的可執(zhí)行文件進行生成
使用test_xml_launch.xml啟動各個程序
source install/setup.bash
ros2 launch learn_ros2 test_xml_launch.xml
使用test_launch.launch.py啟動各個程序如下: