【发布时间】:2016-11-18 16:18:29
【问题描述】:
如果这个问题对你来说太简单了,我很抱歉,但我没有良好的编程技能和 ROS 知识。我有一个 ROS 主题,其中发布了一些以秒为单位的心跳间隔数字。我需要订阅该主题并进行此类详细说明:想法是有一个十个数字的小数组,我可以在其中连续存储十个心跳。然后我有一个更大的 60 个数字数组,必须上升 10 个位置,以便在底部有小数组的最新十个值,它必须“丢弃”十个最旧的值(我做了一些研究也许我必须使用向量而不是数组,因为在 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,但没有任何改变。谢谢!
【问题讨论】:
-
解决了在 process_message 函数的第二个 if 中将
==10更改为>=10。答案评论部分有详细解释。谢谢!
标签: c++ arrays text messages ros