Ask Your Question

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

asked 2016-11-25 03:52:13 -0500

Marcofon gravatar image

updated 2016-12-01 07:55:29 -0500

Hello, I'm asking you if there is a way to have a precedence between two ROS nodes. In particular, i have a ROS node that makes an output that is a text file with 60 data in it, and it recreates it every time because the data are changing. Then i have a node that has to analyze that text file. Basically, what i need is to make some changes to have a mechanism that stops the analyzer node when the writer node is running, then it has to send a signal to the analyzer node to make it able to run and analyze the text file. And then the writer node has to return let's say "in charge" to be able to re-write the text file again. So, in simple words, is a loop. Someone told me that a possible solution can be something like a "semaphore" topic in which the writer node writes, for example, a boolean value of 1 when is doing the opening, the writing and the closing of the text file, so the analyzer node knows that cannot do its elaboration, since the file is not ready yet. And, when the writer has finished and closed the text file, it has to be published a value 0 that permits the analysis by the analyzer node. I searched for the publishing of boolean values and i found a code that can be something like this:

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

I don't know if i have only to use the publisher in the writer node and the subscriber in the analyzer node. Maybe i have to use both of them in the two nodes, something like: writer put a 1 in the topic semaphore so the analyzer knows that cannot access the text file, makes the text file and then put a 0 in the topic and subscribe to the topic waiting again a 1; the analyzer does something similar but in reverse. I put the two codes below, because i don't have any idea where to put the publisher and the the subscriber and how to make them working well. NOTE: a new text file is created almost every 10 seconds, since in the text file are written data coming from another ROS topic and the code in the writer has a mechanism to do this kind of elaboration. Thank you in advance!!! EDIT: I edited the codes with a topic based solution mentioned in my last comment.

Writer code:

#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 ...
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-11-25 04:28:07 -0500

NEngelhard gravatar image

Your approach is a bit strange. Are you aware that you can send a vectors in a ROS Message? Just send all of your values in a single message and do not take the detour over the filesystem.

edit flag offensive delete link more


@NEngelhard thank you for your reply! I'm sorry but I don't understand what you are saying. If you are talking about the data written in the text file, I must send them in batch of 60 data at once.

Marcofon gravatar image Marcofon  ( 2016-11-25 04:53:00 -0500 )edit

Why don't you pack them in a single ROS-Message?

NEngelhard gravatar image NEngelhard  ( 2016-11-25 06:07:45 -0500 )edit

@NEngelhard because i need to do step by step elaborations to 60 data at a time.

Marcofon gravatar image Marcofon  ( 2016-11-25 08:07:19 -0500 )edit

What do you mean by 'elaborations'? You want to process all 60 data points at a time, so just send all of them in a single message.

NEngelhard gravatar image NEngelhard  ( 2016-11-25 10:10:01 -0500 )edit

@NEngelhard they are coming from a live stream of data, so I have to store 60 of them in an array and then they are written on the text file. The elaborations are some statistics methods used by the script launched by the analyzer node.

Marcofon gravatar image Marcofon  ( 2016-11-25 10:29:58 -0500 )edit

But why do you write them into a file?

NEngelhard gravatar image NEngelhard  ( 2016-11-25 10:36:11 -0500 )edit

@Marcofon: what @NEngelhard is trying to say, is that you could make your first node store the 60 data points in an internal array (just a variable in the node), then construct a ROS msg with an array as a member, populate the array from the variable, then send the msg in one go to the other node.

gvdhoorn gravatar image gvdhoorn  ( 2016-11-25 10:47:23 -0500 )edit

That would seem to achieve what you want to do, without using any intermediate temporary files.

See the Array handling section on the wiki/msg page.

gvdhoorn gravatar image gvdhoorn  ( 2016-11-25 10:48:36 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2016-11-25 03:52:13 -0500

Seen: 250 times

Last updated: Dec 01 '16