I want to store in an array messages from a ROS topic for further elaboration

asked 2016-11-18 10:07:38 -0500

Marcofon gravatar image

updated 2016-11-22 08:47:14 -0500

Hello, sorry if this question is too simple for you, but i don't have good programming skills and ROS knowledge. I have a ROS topic in which are published some numbers that are heart beat intervals in seconds. I need to subscribe to that topic and do this kind of elaboration: The idea is to have a little array of ten numbers in which i can store continuously ten heart beat. Then i have a bigger array of 60 numbers that must go up by ten position in order to have at the bottom the newest ten values of the small array and it has to "throw away" the ten oldest values ( i did a bit of research and maybe i have to use a vector instead of an array because in C++ array are fixed as far as i read ). Then i have to print every time these 60 values in a text file (i mean in a loop, so the the text file will be continuously overwritten). Moreover, i see that ROS outputs the data from a topic in this manner: data: 0.678 with every data divided from the others by --- in a column. What i really want, because i need it for a script that reads text file in this manner, is a text file in which the values are in one column without spaces and other signs or words, like this:


I provide below the code for my node, in which, for now, i did only the subscribing part, since i have no idea on how to do the things that i have to do later. Thank you in advance for your help!!!


#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);


return 0;

NOTE: I didn't include the Float32/64 header because i publish the heart beats as a string. I don't know if this is of interest.

EDIT: Ok, with some good help of two guys over the web, now i have added a callback function that could be the one that i need and i called it into my subscriber. But, for now, i'm only able to create an empty file, data seem somewhat to be missing. Any advice? Thanks!

Code of the callback function that is called by the subscriber:

#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>

using namespace std;

static std::deque<std::string> queue_buffer;
static int entries_added_since_last_write = 0;
std::ofstream data_file("/home/marco/catkin_ws/src/heart_rate_monitor/my_data_file.txt");
void write_data_to_file()
// open file

if (data_file.is_open())
for (int i = 0; i < queue_buffer ...
edit retag flag offensive close merge delete



You need a callback function for the subscriber to enter each time there is a message received. In this callback function you can use the data.


JoshMarino gravatar image JoshMarino  ( 2016-11-18 21:43:51 -0500 )edit

@JoshMarino thank you for your reply! Can you explain me in details how to do it? Thank you again!

Marcofon gravatar image Marcofon  ( 2016-11-20 08:58:55 -0500 )edit

Have you tried following the tutorial in the link I provides for subscribers in C++?

JoshMarino gravatar image JoshMarino  ( 2016-11-20 11:17:27 -0500 )edit

@JoshMarino hello, I'm sorry. Which links? EDIT: check the edit in the question. Do you have an idea of what is wrong or missing? Thank you!

Marcofon gravatar image Marcofon  ( 2016-11-20 12:31:53 -0500 )edit

I have added also the publisher code.

Marcofon gravatar image Marcofon  ( 2016-11-22 05:14:41 -0500 )edit

I solved the problem!

Marcofon gravatar image Marcofon  ( 2016-11-22 08:47:29 -0500 )edit