[英]I want to store in an array messages from a ROS topic for further elaboration
很抱歉,这个问题对您来说太简单了,但是我没有很好的编程技能和ROS知识。 我有一个ROS主题,其中发布了一些数字(以秒为单位的心跳间隔)。 我需要订阅该主题并进行详细说明:想法是要有十个数字组成的小数组,我可以在其中连续存储十个心跳。 然后,我有一个更大的数组,其中包含60个数字,必须将这些数字上移十个位置,以使小数组的最新十个值位于底部,并且它必须“舍弃”十个最旧的值(我做了一些研究也许我必须使用向量而不是数组,因为据我所知,在C ++中数组是固定的。 然后,我每次必须在文本文件中打印这60个值(我的意思是循环,所以文本文件将被连续覆盖)。 此外,我看到,ROS由一个主题输出数据以这种方式: data: 0.987
与每一个数据通过与其他划分---
在一列。 我真正想要的是因为我需要一个以这种方式读取文本文件的脚本,因此它是一个文本文件,其中值位于一列中,没有空格和其他符号或单词,如下所示:
0.404
0.952
0.956
0.940
0.960
我在下面为我的节点提供了代码,由于我不知道如何处理以后要做的事情,因此到目前为止,我只执行了订阅部分。 预先感谢您的帮助!!!
码:
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
int main(int argc, char **argv)
{
ros::init(argc, argv, "writer");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/HeartRateInterval", 1000);
ros::spin();
return 0;
}
注意:我没有包括Float32 / 64标头,因为我将心跳发布为字符串。 我不知道这是否有意义。
编辑:我将在ROS主题上发布数据的发布者节点的代码下面包括代码。
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main(int argc, char **argv)
{
ros::init(argc, argv, "heart_rate_monitor");
ros::NodeHandle n;
ros::Publisher pub = n.advertise<std_msgs::String>("/HeartRateInterval", 1000);
ros::Rate loop_rate(1);
while (ros::ok())
{
ifstream inputFile("/home/marco/Scrivania/marks.txt");
string line;
while (getline(inputFile, line)) {
istringstream ss(line);
string heart;
ss >> heart;
std_msgs::String msg;
msg.data = ss.str();
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
}
return 0;
}
由于所公布的是“变量” msg
,我试图给出答案的变量的代码来替换string_msg
用msg
,但一切都没有改变。 谢谢!
我不确定我是否确切了解您想要什么,但是这里有一个简短的示例,可以满足您的需求。
我在这里使用的std::deque
具有60个值的循环缓冲区。 您的代码中缺少的是一个回调函数process_message
,该函数在每次process_message
新消息时都会为订户调用。
我没有编译此代码,因此它可能不会立即编译,但基础知识已经存在。
#include <ros/ros.h>
#include <std_msgs/String.h>
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <deque>
static std::deque<std::string> queue_buffer;
static int entries_added_since_last_write = 0;
void write_data_to_file()
{
// open file
std::ofstream data_file("my_data_file.txt");
if (data_file.is_open())
{
for (int i = 0; i < queue_buffer.size(); ++i)
{
data_file << queue_buffer[i] << std::end;
}
}
else
{
std::cout << "Error - Cannot open file." << std::endl;
exit(1);
}
data_file.close();
}
void process_message(const std_msgs::String::ConstPtr& string_msg)
{
// 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);
ros::spin();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.