3.8 创建话题的发布和订阅节点

本文档详细介绍了如何在ROS中创建发布和订阅节点。首先,讲解了如何编辑发布节点,包括创建源码文件、源码解析及流程总结。接着,介绍了订阅节点的创建,同样涵盖创建源码、解析和流程。最后,讨论了节点的编译和运行步骤,以实现话题消息的发布与订阅功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     前面我们有介绍话题以及节点的概念,也尝试使用了话题的命令行工具对话题发布了消息,本节我们将通过创建节点的方式来持续发布和订阅话题消息。

3.8.1 编辑发布节点

  我们来通过源码实现一个节点,创建一个名为 chatter 的话题,并向其发布话题消息“ hello world”。

3.8.1.1 创建并编辑源码文件

  首先还是进入我们之前创建的功能包 beginner_tutorials 的目录下,创建一个 src 文件夹,今后我们在功能包 beginner_tutorials 中创建的源码文件一般默认就存放在这个 src 文件夹中。

roscd beginner_tutorials
mkdir src

  创建源码文件,并输入以下代码内容:

cd src
gedit talker.cpp
  27 #include "ros/ros.h"
  28 #include "std_msgs/String.h"
  29 
  30 #include <sstream>
  31 
  32 /**
  33  * This tutorial demonstrates simple sending of messages over the ROS system.
  34  */
  35 int main(int argc, char **argv)
  36 {
  37   /**
  38    * The ros::init() function needs to see argc and argv so that it can perform
  39    * any ROS arguments and name remapping that were provided at the command line.
  40    * For programmatic remappings you can use a different version of init() which takes
  41    * remappings directly, but for most command-line programs, passing argc and argv is
  42    * the easiest way to do it.  The third argument to init() is the name of the node.
  43    *
  44    * You must call one of the versions of ros::init() before using any other
  45    * part of the ROS system.
  46    */
  47   ros::init(argc, argv, "talker");
  48 
  49   /**
  50    * NodeHandle is the main access point to communications with the ROS system.
  51    * The first NodeHandle constructed will fully initialize this node, and the last
  52    * NodeHandle destructed will close down the node.
  53    */
  54   ros::NodeHandle n;
  55 
  56   /**
  57    * The advertise() function is how you tell ROS that you want to
  58    * publish on a given topic name. This invokes a call to the ROS
  59    * master node, which keeps a registry of who is publishing and who
  60    * is subscribing. After this advertise() call is made, the master
  61    * node will notify anyone who is trying to subscribe to this topic name,
  62    * and they will in turn negotiate a peer-to-peer connection with this
  63    * node.  advertise() returns a Publisher object which allows you to
  64    * publish messages on that topic through a call to publish().  Once
  65    * all copies of the returned Publisher object are destroyed, the topic
  66    * will be automatically unadvertised.
  67    *
  68    * The second parameter to advertise() is the size of the message queue
  69    * used for publishing messages.  If messages are published more quickly
  70    * than we can send them, the number here specifies how many messages to
  71    * buffer up bef
解释这段代码#include "image_process.hpp" using namespace std::chrono_literals; using namespace cv; using namespace std; void imgProcess::imageCallback(sensor_msgs::msg::Image rosImage) { this->get_parameter("B_low_threshold", B_low_threshold_); this->get_parameter("G_low_threshold", G_low_threshold_); this->get_parameter("R_low_threshold", R_low_threshold_); this->get_parameter("B_high_threshold", B_high_threshold_); this->get_parameter("G_high_threshold", G_high_threshold_); this->get_parameter("R_high_threshold", R_high_threshold_); auto cvImage = cv_bridge::toCvCopy(rosImage, rosImage.encoding); cv::Mat img = cvImage->image; cvtColor(img, img, CV_RGB2BGR); //HPupdate int cout = 0; for (int i = 0; i <= 380; ++i) { int x = i; int y = 996; cv::Vec3b pixel = img.at<cv::Vec3b>(cv::Point(x, y)); //RCLCPP_INFO(this->get_logger(),"R:%d,G:%d,B:%d",pixel[0],pixel[1],pixel[2]); if (pixel[0] == 131 && pixel[1] == 131 && pixel[2] == 131) { game_mode_=EASY; //RCLCPP_INFO(this->get_logger(),"easy"); cout++; }else if(pixel[0] == 106 && pixel[1] == 106 && pixel[2] == 106){ game_mode_=HARD; //RCLCPP_INFO(this->get_logger(),"hard"); cout++; } } sentry_HP_= cout/3.8; //RCLCPP_ERROR(this->get_logger(),"HP:%f",sentry_HP_); if (sentry_HP_==0){//游戏重开 // RCLCPP_INFO(this->get_logger(),"game over"); for (auto &row : pixel_status_map){ std::fill(row.begin(), row.end(), OBSTACLE); } for (int i = 0; i < 8; ++i) { mapInfo[i].is_exist_and_out_range=false; mapInfo[i].pos.x=1000; mapInfo[i].pos.y=1000; } is_completed_explored_=false; old_explore_pose.x=0; old_explore_pose.y=0; one_outdoor_pose.x=1000; one_outdoor_pose.y=1000; } //bullet update cout = 0; for (int i = 2
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值