??★,°:.☆( ̄▽?zhuān)?/$:.°★ ??
這篇文章主要介紹ros2環(huán)境安裝與基礎(chǔ)入門(mén)。
學(xué)其所用,用其所學(xué)?!?jiǎn)⒊?/font>
歡迎來(lái)到我的博客,一起學(xué)習(xí),共同進(jìn)步。
喜歡的朋友可以關(guān)注一下,下次更新不迷路??
??1. ros2介紹
ROS 2 (Robot Operating System 2)是一個(gè)開(kāi)源的機(jī)器人操作系統(tǒng),它是ROS(Robot Operating System)的下一代版本。它提供了一系列工具、庫(kù)和約定,用于構(gòu)建機(jī)器人應(yīng)用程序。與ROS 1相比,ROS 2具有更強(qiáng)大的功能,更好的性能和更好的可靠性。
ROS 2采用分布式消息傳遞機(jī)制,可以在不同的計(jì)算機(jī)上進(jìn)行通信,并支持多種編程語(yǔ)言,包括C++、Python、Java等。ROS 2還提供了更好的安全性和實(shí)時(shí)性,使其適用于更廣泛的機(jī)器人應(yīng)用場(chǎng)景。
ROS 2的核心組件包括:
rclcpp:ROS客戶(hù)端庫(kù),在C++中使用。 rclpy:ROS客戶(hù)端庫(kù),在Python中使用。
rosidl:服務(wù)接口定義語(yǔ)言,用于描述ROS消息和服務(wù)。 rmw:ROS中間件,用于管理節(jié)點(diǎn)之間的通信。
ros2cli:命令行界面工具,用于管理ROS 2系統(tǒng)。
ROS2的一大特點(diǎn)是集成了DDS,支持的DDS有:
Fast RTPS:該實(shí)現(xiàn)基于eProsima的Fast RTPS庫(kù),是ROS 2默認(rèn)的DDS實(shí)現(xiàn)。Fast RTPS是一個(gè)高性能、可靠的DDS實(shí)現(xiàn),采用了快速序列化機(jī)制(Fast Buffers)和動(dòng)態(tài)類(lèi)型支持(DynamicTypes),支持多種平臺(tái)和編程語(yǔ)言。
Cyclone DDS:該實(shí)現(xiàn)由ADLINK開(kāi)發(fā),是另一個(gè)高性能、開(kāi)源的DDS實(shí)現(xiàn)。Cyclone DDS支持多種平臺(tái)和編程語(yǔ)言,并提供了一些高級(jí)功能,如分布式安全和QoS配置。
RTI Connext DDS:該實(shí)現(xiàn)由Real-Time Innovations公司開(kāi)發(fā),是一個(gè)商業(yè)級(jí)別的DDS實(shí)現(xiàn)。RTI Connext DDS提供了廣泛的功能和工具,如實(shí)時(shí)監(jiān)測(cè)、故障診斷和網(wǎng)絡(luò)優(yōu)化等。
??2. ros2安裝
Ubuntu 18.04可以安裝ROS 2 Dashing Diademata
和ROS 2 Eloquent Elusor
版本。建議使用Eloquent
版本,因?yàn)樗亲钚碌拈L(zhǎng)期支持版本,并提供了更多的功能和改進(jìn)。
小魚(yú)的安裝命令:wget http://fishros.com/install -O fishros && . fishros
根據(jù)需求選擇對(duì)應(yīng)的ros2版本即可。
??3. ros2基礎(chǔ)使用
示例測(cè)試:
# 發(fā)布訂閱
ros2 run demo_nodes_cpp listener
ros2 run demo_nodes_cpp talker
# 小烏龜
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
# CLI
ros2 pkg list
ros2 node list
ros2 node info <node_name>
ros2 pkg create <package-name> --build-type {cmake,ament_cmake,ament_python} --dependencies <依賴(lài)名字> # 創(chuàng)建功能包
ros2 bag record /topic_name
rviz2
gazebo
代碼模板:https://github.com/mikeferguson/ros2_cookbook
國(guó)內(nèi)參考:https://fishros.com/d2lros2foxy/#/codebook/README
cmake工程引用rclcpp示例:
創(chuàng)建main.cpp,寫(xiě)一個(gè)hello_world_cpp節(jié)點(diǎn)示例:
#include "rclcpp/rclcpp.hpp"
#include <iostream>
int main(int argc, char** argv)
{
rclcpp::init(argc, argv);
std::cout << "Hello ROS2!" << std::endl;
rclcpp::spin(std::make_shared<rclcpp::Node>("hello_world_cpp"));
return 0;
}
創(chuàng)建CMakeLists.txt,引用rclcpp頭文件和鏈接庫(kù):
cmake_minimum_required(VERSION 3.11)
project(main)
find_package(rclcpp REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main rclcpp::rclcpp)
然后編譯即可:
mkdir build && cd build
cmake ..
make
./main
安裝colcon編譯工具并測(cè)試案例:
# 安裝編譯工具
sudo apt-get install python3-colcon-common-extensions
# 下載源碼
mkdir colcon_test && cd colcon_test
git clone https://ghproxy.com/https://github.com/ros2/examples src/examples -b eloquent
colcon build
# 運(yùn)行自己編譯的節(jié)點(diǎn)
source install/setup.bash
ros2 run examples_rclcpp_minimal_subscriber subscriber_member_function
source install/setup.bash
ros2 run examples_rclcpp_minimal_publisher publisher_member_function
運(yùn)行如下:
??4. ros2節(jié)點(diǎn)編寫(xiě)
簡(jiǎn)單的節(jié)點(diǎn)
下面演示一個(gè)ROS2節(jié)點(diǎn)的創(chuàng)建:
# 新建節(jié)點(diǎn)目錄
mkdir -p ros2_ws/src
# 創(chuàng)建功能包(ament_cmake編譯,rclcpp依賴(lài))
cd ros2_ws/src
ros2 pkg create name_of_pack --build-type ament_cmake --dependencies rclcpp
# 創(chuàng)建編寫(xiě)main.cpp
touch name_of_pack/src/main.cpp
# 修改(末尾追加)CMakeLists.txt
add_executable(RosNode src/main.cpp)
ament_target_dependencies(RosNode rclcpp)
install(TARGETS
RosNode
DESTINATION lib/${PROJECT_NAME}
)
# 編譯運(yùn)行
colcon build
source install/setup.bash
ros2 run name_of_pack RosNode
# 測(cè)試
ros2 node list
ros2 node info /RosNode_2
#include "rclcpp/rclcpp.hpp"
int main(int argc, char **argv)
{
/* 初始化rclcpp */
rclcpp::init(argc, argv);
/* 創(chuàng)建節(jié)點(diǎn) */
auto node = std::make_shared<rclcpp::Node>("RosNode_2");
/* 打印日志 */
RCLCPP_INFO(node->get_logger(), "ros2節(jié)點(diǎn)已經(jīng)啟動(dòng).");
/* 運(yùn)行節(jié)點(diǎn),并檢測(cè)退出信號(hào) Ctrl+C */
rclcpp::spin(node);
/* 停止運(yùn)行 */
rclcpp::shutdown();
return 0;
}
發(fā)布者和訂閱者
下面創(chuàng)建一個(gè)發(fā)布者和訂閱者:
# 新建目錄
mkdir -p mytest_ws/src
cd mytest_ws/src
ros2 pkg create subscribe_and_publish --build-type ament_cmake --dependencies rclcpp
touch subscribe_and_publish/src/publisher.cpp
touch subscribe_and_publish/src/subscriber.cpp
# CMakeLists.txt
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
add_executable(publisher src/publisher.cpp)
ament_target_dependencies(publisher rclcpp std_msgs)
add_executable(subscribe1 src/subscriber.cpp)
ament_target_dependencies(subscribe1 rclcpp std_msgs)
install(TARGETS
publisher
subscriber
DESTINATION lib/${PROJECT_NAME}
)
# package.xml
<depend>rclcpp</depend>
<depend>std_msgs</depend>
# 編譯運(yùn)行
colcon build
ros2 run subscribe_and_publish publisher
ros2 run subscribe_and_publish subscriber
// publisher.cpp
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
class Publisher : public rclcpp::Node
{
public:
// 構(gòu)造函數(shù),參數(shù)為節(jié)點(diǎn)名稱(chēng)
Publisher(std::string name) : Node(name)
{
RCLCPP_INFO(this->get_logger(), "大家好,我是%s.", name.c_str());
// 創(chuàng)建發(fā)布者
subscribe_and_publish_publisher_ = this->create_publisher<std_msgs::msg::String>("subscribe_and_publish", 10);
// 創(chuàng)建定時(shí)器,500ms為周期,定時(shí)發(fā)布
timer_ = this->create_wall_timer(std::chrono::milliseconds(500), std::bind(&Publisher::timer_callback, this));
}
private:
void timer_callback()
{
// 創(chuàng)建消息
std_msgs::msg::String message;
message.data = "1234";
// 日志打印
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
// 發(fā)布消息
subscribe_and_publish_publisher_->publish(message);
}
// 聲名定時(shí)器指針
rclcpp::TimerBase::SharedPtr timer_;
// 聲明話(huà)題發(fā)布者指針
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr subscribe_and_publish_publisher_;
};
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
/* 產(chǎn)生一個(gè)的節(jié)點(diǎn) */
auto node = std::make_shared<Publisher>("publisher");
/* 運(yùn)行節(jié)點(diǎn),并檢測(cè)退出信號(hào) */
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
// subscriber.cpp
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
class Subscribe : public rclcpp::Node
{
public:
Subscribe(std::string name) : Node(name)
{
RCLCPP_INFO(this->get_logger(), "大家好,我是%s.", name.c_str());
// 創(chuàng)建一個(gè)訂閱者訂閱話(huà)題
subscribe_and_publish_subscribe_ = this->create_subscription<std_msgs::msg::String>("subscribe_and_publish", 10, std::bind(&Subscribe::command_callback, this, std::placeholders::_1));
}
private:
// 聲明一個(gè)訂閱者
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscribe_and_publish_subscribe_;
// 收到話(huà)題數(shù)據(jù)的回調(diào)函數(shù)
void command_callback(const std_msgs::msg::String::SharedPtr msg)
{
RCLCPP_INFO(this->get_logger(), "收到[%s]指令", msg->data.c_str());
};
};
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
/*產(chǎn)生一個(gè)的節(jié)點(diǎn)*/
auto node = std::make_shared<Subscribe>("subscriber");
/* 運(yùn)行節(jié)點(diǎn),并檢測(cè)退出信號(hào)*/
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-485989.html
以上。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-485989.html
到了這里,關(guān)于【ros2】ros2環(huán)境安裝與基礎(chǔ)入門(mén)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!