【发布时间】:2016-11-28 10:03:16
【问题描述】:
我在问你是否有办法在两个 ROS 节点之间设置优先级。特别是,我有一个 ROS 节点,它生成一个包含 60 个数据的文本文件的输出,并且每次都重新创建它,因为数据在变化。然后我有一个必须分析该文本文件的节点。基本上,我需要做一些更改,以便在编写器节点运行时停止分析器节点的机制,然后它必须向分析器节点发送信号以使其能够运行和分析文本文件。然后编写器节点必须返回让我们说“负责”才能再次重写文本文件。所以,简单来说,就是一个循环。有人告诉我,一个可能的解决方案可以是类似“信号量”主题的东西,作者节点在其中写入,例如,在打开、写入和关闭文本文件时,布尔值 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;
}
【问题讨论】:
标签: c++ arrays text messages ros