目錄
前言
1 話題簡(jiǎn)介
2 常用指令
3?RCLCPP實(shí)現(xiàn)實(shí)現(xiàn)話題
3.1 創(chuàng)建工作空間
3.2 代碼編寫
3.2.1 發(fā)布端編寫
3.2.2 發(fā)布端編寫
前言
ROS2中的一個(gè)重要概念是話題(Topic)。話題是一種通過(guò)發(fā)布者和訂閱者之間進(jìn)行異步通信的機(jī)制。發(fā)布者(Publisher)將消息發(fā)布到特定的話題上,而訂閱者(Subscriber)則可以選擇訂閱感興趣的話題,并接收發(fā)布者發(fā)送的消息。
話題可以用來(lái)傳遞各種類型的消息,例如傳感器數(shù)據(jù)、機(jī)器人狀態(tài)、控制指令等。每個(gè)話題有一個(gè)唯一的名稱,并且可以有多個(gè)發(fā)布者和訂閱者。
ROS2提供了一套API來(lái)操作話題。開發(fā)者可以使用語(yǔ)言(如C++和Python)來(lái)編寫發(fā)布者和訂閱者節(jié)點(diǎn),并在運(yùn)行時(shí)將它們連接到ROS2系統(tǒng)中。
使用話題,開發(fā)者可以實(shí)現(xiàn)多個(gè)節(jié)點(diǎn)之間的解耦和靈活性。不同的節(jié)點(diǎn)可以以不同的速度發(fā)布和接收消息,可以動(dòng)態(tài)地增加或刪除節(jié)點(diǎn),而不會(huì)影響其他節(jié)點(diǎn)的運(yùn)行。
總結(jié)來(lái)說(shuō),ROS2中的話題機(jī)制提供了一種靈活和可擴(kuò)展的通信方式,可以方便地實(shí)現(xiàn)不同節(jié)點(diǎn)之間的消息傳遞和協(xié)作。它是ROS2中重要的基礎(chǔ)組件之一,為構(gòu)建機(jī)器人應(yīng)用提供了強(qiáng)大的功能和便利的開發(fā)體驗(yàn)。
1 話題簡(jiǎn)介
支持1對(duì)1,1對(duì)多,多對(duì)1,多對(duì)多。
為了方便發(fā)送者和接收者進(jìn)行數(shù)據(jù)的交換,ROS2幫我們?cè)跀?shù)據(jù)傳遞時(shí)做好了消息的序列化和反序列化,而且ROS2的消息序列化與反序列化通信是可以做到跨編程語(yǔ)言、跨平臺(tái)和跨設(shè)備之間的。
一個(gè)節(jié)點(diǎn)發(fā)布數(shù)據(jù)到某個(gè)話題上,另外一個(gè)節(jié)點(diǎn)就可以通過(guò)訂閱話題拿到數(shù)據(jù)。
2 常用指令
首先運(yùn)行一個(gè)發(fā)布節(jié)點(diǎn):
ros2 run demo_nodes_cpp talker
查看上面發(fā)布節(jié)點(diǎn)的話題信息:?
查看話題列表
ros2 topic list -t?
打印話題
ros2 topic echo /chatter?
?查看topic信息
ros2 topic info ?/chatter
查看消息類型
ros2 interface show std_msgs/msg/String
手動(dòng)發(fā)布命令
ros2 topic pub /chatter std_msgs/msg/String 'data: "123"'
圖形化查看節(jié)點(diǎn)信息
rqt_graph
查看話題頻率
ros2 topic hz /chatter
3?RCLCPP實(shí)現(xiàn)實(shí)現(xiàn)話題
3.1 創(chuàng)建工作空間
cd project/
mkdir -p project/
cd project/
ros2 pkg create cpp_topic --build-type ament_cmake --dependencies rclcpp
touch cpp_topic/src/publisher.cpp
3.2 代碼編寫
調(diào)用Node的成員函數(shù)create_publisher并傳入對(duì)應(yīng)的參數(shù)即可。通過(guò)文檔可以看出,至少需要傳入消息類型(msgT)、話題名稱(topic_name)和 服務(wù)質(zhì)量(qos)。
官方rclcpp文檔地址:rclcpp: rclcpp: ROS Client Library for C++
消息接口是ROS2通信時(shí)必須的一部分,通過(guò)消息接口ROS2才能完成消息的序列化和反序列化。ROS2為定義好了常用的消息接口,并生成了相應(yīng)的C++和Python的依賴文件,通過(guò)腳本直接導(dǎo)入。
通過(guò)ros2 pkg指令創(chuàng)建的功能包目錄結(jié)構(gòu)如下:
$ tree -a
.
└── cpp_topic
? ? ├── CMakeLists.txt
? ? ├── include
? ? │???└── cpp_topic
? ? ├── package.xml
? ? └── src
? ? ? ? └── publisher.cpp
3.2.1 發(fā)布端編寫
publisher.cpp
/*****ros2官方demo*******/
#include <chrono>
#include <functional>
#include <memory>
#include <string>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using namespace std::chrono_literals;
/* This example creates a subclass of Node and uses std::bind() to register a
* member function as a callback from the timer. */
class MinimalPublisher : public rclcpp::Node
{
public:
MinimalPublisher()
: Node("minimal_publisher"), count_(0)
{
publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
timer_ = this->create_wall_timer(
500ms, std::bind(&MinimalPublisher::timer_callback, this));
}
private:
void timer_callback()
{
auto message = std_msgs::msg::String();
message.data = "Hello, world! " + std::to_string(count_++);
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
publisher_->publish(message);
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
size_t count_;
};
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalPublisher>());
rclcpp::shutdown();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(cpp_topic)
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(std_msgs REQUIRED)
add_executable(pulisher src/publisher.cpp)
ament_target_dependencies(pulisher rclcpp std_msgs)
install(TARGETS
pulisher
DESTINATION lib/${PROJECT_NAME})
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 files
set(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 files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
package.xml
<?xml version="1.0"?>
<?xml-model schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>cpp_topic</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="xxxxx@xxx.com">linux</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>std_msgs</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
在project目錄運(yùn)行如下編譯指令
colcon build --packages-select cpp_topic
編譯完成后目錄如下:
運(yùn)行
source install/setup.bash
ros2 run cpp_topic pulisher
此時(shí)可以通過(guò)命令訂閱該topic
3.2.2 發(fā)布端編寫
上面過(guò)程中是通過(guò)ros2自動(dòng)的命令進(jìn)行話題的訂閱,下面使用c++代碼實(shí)現(xiàn)話題訂閱。
創(chuàng)建訂閱節(jié)點(diǎn)文件
touch cpp_topic/src/subscriber.cpp
subscriber.cpp:
/*****ros2官方demo*******/
#include <functional>
#include <memory>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using std::placeholders::_1;
class MinimalSubscriber : public rclcpp::Node
{
public:
MinimalSubscriber()
: Node("minimal_subscriber")
{
subscription_ = this->create_subscription<std_msgs::msg::String>(
"topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
}
private:
void topic_callback(const std_msgs::msg::String & msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalSubscriber>());
rclcpp::shutdown();
return 0;
}
CMakeLists.txt增加如下代碼
add_executable(subscriber src/subscriber.cpp)
ament_target_dependencies(subscriber rclcpp std_msgs)
install(TARGETS
subscriber
DESTINATION lib/${PROJECT_NAME})
在project目錄運(yùn)行如下編譯指令
colcon build --packages-select cpp_topic
在project目錄先運(yùn)行上面的發(fā)布節(jié)點(diǎn)
source install/setup.bash
ros2 run cpp_topic pulisher
?新開一個(gè)終端進(jìn)入到project目錄,運(yùn)行訂閱節(jié)點(diǎn)
source install/setup.bash
ros2 run cpp_topic subscriber
?
?
無(wú)論你是初學(xué)者還是有經(jīng)驗(yàn)的開發(fā)者,希望我的博客能對(duì)你的學(xué)習(xí)之路有所幫助。如果你覺(jué)得這篇文章有用,不妨點(diǎn)擊點(diǎn)贊、收藏,或者留下你的評(píng)論分享你的見(jiàn)解和經(jīng)驗(yàn),也歡迎你對(duì)我博客的內(nèi)容提出建議和問(wèn)題。每一次的點(diǎn)贊、評(píng)論、分享和關(guān)注都是對(duì)我的最大支持,也是對(duì)我持續(xù)分享和創(chuàng)作的動(dòng)力。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-806972.html
閱讀我的CSDN主頁(yè),解鎖更多精彩內(nèi)容:Tech Embedded-CSDN博客文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-806972.html
到了這里,關(guān)于ROS2學(xué)習(xí)筆記三:話題Topic的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!