ROS框架——發(fā)布者功能包和訂閱者功能包進(jìn)行bool類型數(shù)據(jù)結(jié)構(gòu)的topic通訊
code review!
零.同時(shí)運(yùn)行兩個(gè)功能包
一.發(fā)布者功能包
1.1.文件結(jié)構(gòu)
1.2.bool_publisher_node.cpp
代碼
#include <ros/ros.h>
#include <std_msgs/Bool.h>
int main(int argc, char **argv) {
// 初始化ROS節(jié)點(diǎn)
ros::init(argc, argv, "bool_publisher_node");
ros::NodeHandle nh;
// 創(chuàng)建一個(gè)ROS發(fā)布器,發(fā)布名為 "bool_topic" 的topic,消息類型為std_msgs::Bool
ros::Publisher bool_pub = nh.advertise<std_msgs::Bool>("/bool_topic", 1);
// 在這里執(zhí)行你希望的操作,比如循環(huán)發(fā)布bool狀態(tài)
ros::Rate loop_rate(1); // 發(fā)布頻率為1Hz
bool status = false;
while (ros::ok()) {
// 執(zhí)行你的業(yè)務(wù)邏輯來(lái)更新bool狀態(tài),這里僅為示例
status = !status; // 反轉(zhuǎn)bool狀態(tài)
// 創(chuàng)建消息并發(fā)布
std_msgs::Bool msg;
msg.data = status;
bool_pub.publish(msg);
ros::spinOnce(); // 處理訂閱的消息(如果有)
loop_rate.sleep();
}
return 0;
}
1.3.CMakeLists.txt
代碼
cmake_minimum_required(VERSION 3.0.2)
project(publisher_bool_topic)
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
)
catkin_package()
include_directories(
${catkin_INCLUDE_DIRS}
)
add_executable(bool_publisher_node src/bool_publisher_node.cpp)
target_link_libraries(bool_publisher_node ${catkin_LIBRARIES})
install(TARGETS bool_publisher_node
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
1.4.package.xml
代碼
<?xml version="1.0"?>
<package format="2">
<name>publisher_bool_topic</name>
<version>0.0.0</version>
<description>The publisher_bool_topic package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
<maintainer email="user@todo.todo">user</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license>
<!-- Url tags are optional, but multiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/publisher_bool_topic</url> -->
<!-- Author tags are optional, multiple are allowed, one per tag -->
<!-- Authors do not have to be maintainers, but could be -->
<!-- Example: -->
<!-- <author email="jane.doe@example.com">Jane Doe</author> -->
<!-- The *depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: -->
<!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
<!-- <depend>roscpp</depend> -->
<!-- Note that this is equivalent to the following: -->
<!-- <build_depend>roscpp</build_depend> -->
<!-- <exec_depend>roscpp</exec_depend> -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<!-- Use build_export_depend for packages you need in order to build against this package: -->
<!-- <build_export_depend>message_generation</build_export_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use exec_depend for packages you need at runtime: -->
<!-- <exec_depend>message_runtime</exec_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<!-- Use doc_depend for packages you need only for building documentation: -->
<!-- <doc_depend>doxygen</doc_depend> -->
<buildtool_depend>catkin</buildtool_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
二.訂閱者功能包
2.1.文件結(jié)構(gòu)
2.2.bool_subscriber_node.cpp
代碼
#include <ros/ros.h>
#include <std_msgs/Bool.h>
// 回調(diào)函數(shù),處理接收到的消息
void boolCallback(const std_msgs::Bool::ConstPtr& msg) {
bool status = msg->data;
ROS_INFO("Received bool status: %s", status ? "true" : "false");
}
int main(int argc, char** argv) {
// 初始化ROS節(jié)點(diǎn)
ros::init(argc, argv, "bool_subscriber_node");
ros::NodeHandle nh;
// 創(chuàng)建一個(gè)ROS訂閱器,用于接收名為 "bool_topic" 的topic
ros::Subscriber bool_sub = nh.subscribe("/bool_topic", 1, boolCallback);
// 循環(huán)等待接收消息
ros::spin();
return 0;
}
2.3.CMakeLists.txt
代碼
cmake_minimum_required(VERSION 3.0.2)
project(subscriber_bool_topic)
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
)
catkin_package()
include_directories(
${catkin_INCLUDE_DIRS}
)
add_executable(bool_subscriber_node src/bool_subscriber_node.cpp)
target_link_libraries(bool_subscriber_node ${catkin_LIBRARIES})
install(TARGETS bool_subscriber_node
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
2.4.package.xml
代碼文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-605681.html
<?xml version="1.0"?>
<package format="2">
<name>subscriber_bool_topic</name>
<version>0.0.0</version>
<description>The subscriber_bool_topic package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
<maintainer email="user@todo.todo">user</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license>
<!-- Url tags are optional, but multiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/subscriber_bool_topic</url> -->
<!-- Author tags are optional, multiple are allowed, one per tag -->
<!-- Authors do not have to be maintainers, but could be -->
<!-- Example: -->
<!-- <author email="jane.doe@example.com">Jane Doe</author> -->
<!-- The *depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: -->
<!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
<!-- <depend>roscpp</depend> -->
<!-- Note that this is equivalent to the following: -->
<!-- <build_depend>roscpp</build_depend> -->
<!-- <exec_depend>roscpp</exec_depend> -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<!-- Use build_export_depend for packages you need in order to build against this package: -->
<!-- <build_export_depend>message_generation</build_export_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use exec_depend for packages you need at runtime: -->
<!-- <exec_depend>message_runtime</exec_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<!-- Use doc_depend for packages you need only for building documentation: -->
<!-- <doc_depend>doxygen</doc_depend> -->
<buildtool_depend>catkin</buildtool_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
三.launch文件(該launch可同時(shí)啟動(dòng)兩個(gè)功能包的rosnode)
bool_topic.launch
代碼文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-605681.html
<launch>
<!-- 啟動(dòng)publisher_bool_topic節(jié)點(diǎn) -->
<node name="publisher_bool_node" pkg="publisher_bool_topic" type="bool_publisher_node" output="screen" />
<!-- 啟動(dòng)subscriber_bool_topic節(jié)點(diǎn) -->
<node name="subscriber_bool_node" pkg="subscriber_bool_topic" type="bool_subscriber_node" output="screen" />
</launch>
到了這里,關(guān)于ROS框架——發(fā)布者功能包和訂閱者功能包進(jìn)行bool類型數(shù)據(jù)結(jié)構(gòu)的topic通訊的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!