ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

I want to publish in a ROS topic floating numbers from a text file

asked 2016-11-14 09:03:52 -0500

Marcofon gravatar image

updated 2016-11-16 08:41:05 -0500

Hello, what i'm trying to do is to read a text file which contains many floating numbers in one column, as in this example of my file:


in particular i want to read line by line and, for every time that a line is read i want that this numerical value is stored in a variable that has to be published on a ROS topic with a wait time of about 1 second and then it has to do it again until the end of the file. I'm trying to write my question in a step-by-step way to be (maybe) more clear:

1- Read the first line (for ex. 0.872)

2- Save the number 0.872 in a variable

3- Publish that variable that has only 0.872 as a value in a ROS topic

4- Wait for a second

5- Repeat the loop until the end of file.

With some searching around the web, on Stack Overflow, and here on ROS Answers i wrote a code that, for now, publishes the numbers on the terminal (even if it's not what i want), since i don't know how to implement the ROS commands for publishing although i've read the tutorials. But some numbers (i've noticed that happens only when the last digit is 0 like 0.900 is cut in 0.9) appear to be cut like this:










instead of being equal in precision to the other numbers. Here is the code that i wrote it for now:

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main()
ifstream inputFile("/home/marco/Scrivania/marks.txt");
string line;

while (getline(inputFile, line))
istringstream ss(line);

float heart;

ss >> heart;
cout << heart << endl << endl;

NOTE: It is written in plain C++ language without any ROS commands like ROS init, etc. So, if you can would be great if you can tell me also how to do it, since I've only made one node of ROS so far.

Thank you in advance and excuse me if i'm bothering you with this kind of questions, but, as you can see from my previous questions, I'm a true noob that has to do a project a little far away from my typical field of work.


LITTLE EDIT: including also: #include <iomanip> and modifying the last line in: std::cout << std::fixed << std::setprecision(3) << heart << endl << endl; appears to have have fixed the cutting problems.

EDIT: I was able to put this code on a ROS node and it compiles and runs with no problem, now i'm trying to do the message part. For now, all the numbers are printed on the terminal at once.


#include "ros/ros.h"
#include "std_msgs/String.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream ...
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2016-11-14 10:44:37 -0500

BrettHemes gravatar image

updated 2016-11-14 10:46:46 -0500

Marcofon, you are close with the code you already have. I would suggest you read through the publisher (and subscriber) tutorials here. The code in the tutorials should do almost exactly what you want (with some minor tweaks). Try replacing the msg population with your code and change the loop_rate to 1.0

edit flag offensive delete link more


Thank you for your reply! I will try to re-read them for trying to solve the problem, i hope you can help me if i can't solve it! Thank you again.

Marcofon gravatar image Marcofon  ( 2016-11-15 03:13:09 -0500 )edit

I tried but i really don't know how to do it, i updated my answers with my last try. I hope that you can help me.

Marcofon gravatar image Marcofon  ( 2016-11-15 04:42:36 -0500 )edit

Looks like you are making progress! The catkin_make errors should give you an idea of what is going wrong. Start with the first error reported and see if you can't get them fixed one at a time. If you are still stuck, post the errors so we can better help.

BrettHemes gravatar image BrettHemes  ( 2016-11-15 08:30:31 -0500 )edit

I tried to solve the first error and then the others, but with no luck! I'll post some of them below: ‘heart’ was not declared in this scope ss << std::cout << std::fixed << std::setprecision(3) << heart << endl << endl << count; error: cannot convert ‘std::basic_stringstream<char>::__string_type {aka std::basic_string<char>}’ to ‘std_msgs::Float64_<std::allocator<void> >::_data_type {aka double}’ in assignment = ss.str();

Marcofon gravatar image Marcofon  ( 2016-11-15 08:50:21 -0500 )edit

These are basic c++ errors and not ROS-related. The variable heart is declared inside of the while loop and therefore is indeed out of scope after leaving the loop. msg expects a float but you are giving it a stringstream. Without a decent c/c++ background learning ROS is going to be difficult...

BrettHemes gravatar image BrettHemes  ( 2016-11-15 14:41:03 -0500 )edit

Yes of course, i know and i wrote it in my question! But i must do it, it's a project. As you can see, i'm trying, I'm not asking without trying first. But, if i'm stuck, i ask advices and help. Can you please help me? I know that maybe I'm bothering you, but i really don't have almost any help!

Marcofon gravatar image Marcofon  ( 2016-11-16 03:17:04 -0500 )edit

As you can see from my last edit, i was able, with a bit of struggle and a good amount of luck, to do what i wanted. Do you have an hint for the little problem of the node that doesn't close when i use Ctrl+c? Thank you very much!

Marcofon gravatar image Marcofon  ( 2016-11-16 08:43:26 -0500 )edit

Question Tools

1 follower


Asked: 2016-11-14 09:03:52 -0500

Seen: 1,747 times

Last updated: Nov 16 '16