ROS框架——发布者功能包和订阅者功能包进行bool类型数据结构的topic通讯
code review!
零.同时运行两个功能包
一.发布者功能包
1.1.文件结构
1.2.bool_publisher_node.cpp
代码
#include <ros/ros.h>
#include <std_msgs/Bool.h>
int main(int argc, char **argv) {
// 初始化ROS节点
ros::init(argc, argv, "bool_publisher_node");
ros::NodeHandle nh;
// 创建一个ROS发布器,发布名为 "bool_topic" 的topic,消息类型为std_msgs::Bool
ros::Publisher bool_pub = nh.advertise<std_msgs::Bool>("/bool_topic", 1);
// 在这里执行你希望的操作,比如循环发布bool状态
ros::Rate loop_rate(1); // 发布频率为1Hz
bool status = false;
while (ros::ok()) {
// 执行你的业务逻辑来更新bool状态,这里仅为示例
status = !status; // 反转bool状态
// 创建消息并发布
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.文件结构
2.2.bool_subscriber_node.cpp
代码
#include <ros/ros.h>
#include <std_msgs/Bool.h>
// 回调函数,处理接收到的消息
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节点
ros::init(argc, argv, "bool_subscriber_node");
ros::NodeHandle nh;
// 创建一个ROS订阅器,用于接收名为 "bool_topic" 的topic
ros::Subscriber bool_sub = nh.subscribe("/bool_topic", 1, boolCallback);
// 循环等待接收消息
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
代码文章来源:https://www.toymoban.com/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可同时启动两个功能包的rosnode)
bool_topic.launch
代码文章来源地址https://www.toymoban.com/news/detail-605681.html
<launch>
<!-- 启动publisher_bool_topic节点 -->
<node name="publisher_bool_node" pkg="publisher_bool_topic" type="bool_publisher_node" output="screen" />
<!-- 启动subscriber_bool_topic节点 -->
<node name="subscriber_bool_node" pkg="subscriber_bool_topic" type="bool_subscriber_node" output="screen" />
</launch>
到了这里,关于ROS框架——发布者功能包和订阅者功能包进行bool类型数据结构的topic通讯的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!