有没有办法在两个ROS节点之间具有优先级

Is there a way to have a precedence between two ROS nodes?

本文关键字:节点 ROS 之间 优先级 两个 有没有      更新时间:2023-10-16

我问您是否有一种方法可以在两个ROS节点之间具有优先级。特别是,我有一个ROS节点,它使输出是一个文本文件,其中包含60个数据,并且每次都会重新创建它,因为数据正在更改。然后,我有一个必须分析该文本文件的节点。基本上,我需要进行一些更改,以便具有在运行作者节点运行时停止分析仪节点的机制,然后必须向分析仪节点发送信号以使其能够运行并分析文本文件。然后,作者节点必须返回,让我们说"负责"才能再次重写文本文件。因此,简单地说,是一个循环。有人告诉我,一个可能的解决方案可能是一个"信号量"主题,例如,作者节点在其中写开幕时的布尔值为1的布尔值1,文本文件的写作和关闭,因此分析仪节点知道该文件尚未准备就绪,因此无法进行详细说明。而且,当作者完成并关闭文本文件时,必须发布一个值0,允许分析仪节点分析。我搜索了布尔值的出版,发现了一个可以像这样的代码:

ros::Publisher pub = n.advertise<std_msgs::Bool>("semaphore", 1000);
std_msgs::Bool state;
state.data = 1;

我不知道我是否只能在作者节点和分析仪节点中的订阅者中使用发布者。也许我必须在两个节点中使用两个节点,类似于:作者在主题信号量中放置1,因此分析仪知道无法访问文本文件,制作文本文件,然后在主题中放置0并订阅话题再次等待1;分析仪做类似的事情,但相反。我将两个代码放在下面,因为我不知道将发布者和订阅者放在哪里,以及如何使它们正常工作。如果可能的话,我必须将工作流程保持在我的代码中。注意:几乎每10秒创建一个新的文本文件,因为文本文件中的书面数据来自另一个ROS主题,并且作者中的代码具有执行此类阐述的机制。先感谢您!!!编辑:现在,正如我在上一篇评论中解释的那样,这些代码已通过基于主题的解决方案进行纠正。

作者代码:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "std_msgs/Bool.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <deque>
#include "heart_rate_monitor/analyse_heart_rate.h"
using namespace std;

static std::deque<std::string> queue_buffer;
static int entries_added_since_last_write = 0;
ros::Publisher pub;
void write_data_to_file()
{
// open file;
std::ofstream data_file("/home/marco/catkin_ws/src/heart_rate_monitor/my_data_file.txt");
if (data_file.is_open())
{
for (int i = 0; i < queue_buffer.size(); ++i)
{
  data_file << queue_buffer[i] << std::endl;
}
}
else
{
std::cout << "Error - Cannot open file." << std::endl;
exit(1);
}
data_file.close();
std_msgs::Bool state;
state.data = 0;
pub.publish(state);
}
void process_message(const std_msgs::String::ConstPtr& string_msg)
{
std_msgs::Bool state;
state.data = 1;
pub.publish(state);
// if buffer has already 60 entries, throw away the oldest one
if (queue_buffer.size() == 60)
{
queue_buffer.pop_front();
}
// add the new data at the end
queue_buffer.push_back(string_msg->data);
// check if 10 elements have been added and write to file if so
entries_added_since_last_write++;
if (entries_added_since_last_write >= 10
  && queue_buffer.size() == 60)
{
// write data to file and reset counter
write_data_to_file();
entries_added_since_last_write = 0;
}
}

int main(int argc, char **argv)
{
ros::init(argc, argv, "writer");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/HeartRateInterval", 1000, process_message);
pub = n.advertise<std_msgs::Bool>("/semaphore", 1000);
ros::spin();
return 0;
}

分析仪代码:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "std_msgs/Bool.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <deque>
#include "heart_rate_monitor/analyse_heart_rate.h"
void orderCallback(const std_msgs::Bool::ConstPtr& msg)
{
if (msg->data == 0)
{
chdir("/home/marco/catkin_ws/src/heart_rate_monitor");
system("get_hrv -R my_data_file.txt >doc.txt");
}
}

int main(int argc, char **argv)
{
ros::init(argc, argv, "analyzer");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/semaphore", 1000, orderCallback);
ros::spin();
return 0;
}

这可以简单地使用ROS服务完成。基本上,当您的节点A获取消息时,它可以执行所需的内容(写文件),然后从节点B中索取serice(分析文件)。

我唯一看到的是节点A将必须等待节点B服务完成。如果b不需要太多时间,就不会引起问题。

代码段:

srv

创建一个名为" Analyse_heart_rate.srv"的服务在您的包装的SRV文件夹中(我认为是" heart_rate_monitor"。

在文件中指定服务结构的请求/响应:

string filename
---
bool result

cmakelists

添加以下行:

add_service_files(
  FILES
  analyse_heart_rate.srv
)

服务服务器

 #include "ros/ros.h"
 #include "heart_rate_monitor/analyse_heart_rate.h"

bool analyse(heart_rate_monitor::analyse_heart_rate::Request  &req,
     heart_rate_monitor::analyse_heart_rate::Response &res)
{
  res.result = analyse_text_file(req.filename);
  return true;
}
int main(int argc, char **argv)
{
  ros::init(argc, argv, "heart_rate_analyser_server");
  ros::NodeHandle n;
  ros::ServiceServer service = n.advertiseService("heart_rate_analyser", analyse);
  ROS_INFO("Ready to analyse requests.");
  ros::spin();
  return 0;
}

服务客户端

#include "ros/ros.h"
#include "heart_rate_monitor/analyse_heart_rate.h"
void process_message(const std_msgs::String::ConstPtr& string_msg)
{
    std::string output_filename;
    do_staff_with_message();
    write_data_to_file_(output_filename);
     heart_rate_monitor::analyse_heart_rate srv;
     srv.filename = output_filename ;
     if (client.call(srv))
     {
        ROS_INFO("Result: %d", (bool)srv.response.result);
     }
     else
     {
        ROS_ERROR("Failed to call service heart_rate_analyser");
     }
}
int main(int argc, char **argv)
{
  ros::init(argc, argv, "add_two_ints_client");
  ros::NodeHandle n;
  ros::ServiceClient client = n.serviceClient<heart_rate_monitor::analyse_heart_rate>("heart_rate_analyser");
  ros::Subscriber sub = n.subscribe("/HeartRateInterval", 1000, process_message);
  return 0;
}

以这种方式,每当消息传递到"服务客户端"中时,它将对其进行处理并最终将其写入文件。然后,它询问"服务服务器"处理之前创建的文件...

当然,这只是一个片段,可以满足您的需求。

欢呼。