I want to publish in a ROS topic floating numbers from a text file
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:
0.872
0.876
0.880
0.888
0.900
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:
0.876
0.88
0.888
0.9
0.9
0.876
0.904
0.908
0.88
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.
Marco
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.
Code:
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream ...