我想在 ROS 主题中发布文本文件中的浮点数
I want to publish in a ROS topic floating numbers from a text file
我正在尝试读取一个文本文件,该文件的一列中包含许多浮点数,如本例所示:
0.872
0.876
0.880
0.888
0.900
特别是我想逐行阅读它,每次读取一行时,我希望将该数值存储在一个变量中,该变量必须在 ROS 主题上发布,等待时间约为1 秒,然后它必须再次执行直到文件末尾。
我正在尝试逐步写下我的问题,以便(也许)更清楚:
- 读取第一行(例如 0.872)
- 将数字 0.872 保存在变量中
- 在 ROS 主题中发布只有 0.872 的值的变量
- 等一下
- 重复循环直到文件结束。
通过在网络和 Stack Overflow 上进行一些搜索,我编写了一个代码,目前可以在终端上打印数字(即使这不是我想要的),因为我不知道如何尽管我已经阅读了教程,但仍要执行用于发布的 ROS 命令。
但是有些数字似乎是这样切割的:
0.876
0.88
0.888
0.9
0.9
0.876
0.904
0.908
0.88
而不是在精度上与其他数字相等。
这是我现在编写的代码:
#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;
}
注意:它是用普通的 C++ 语言编写的,没有任何 ROS 命令,如 ROS init 等。所以,如果你能告诉我怎么做,那就太好了,因为我只做了一个到目前为止的 ROS 节点。
编辑:将建议的修改放入 ROS 节点、编译和执行后,它运行没有问题。
然后我尝试添加主题部分,但没有成功。我更新了下面的代码:
#include "ros/ros.h"
#include "std_msgs/Float32.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;
ifstream inputFile("/home/marco/Scrivania/marks.txt");
string line;
while (getline(inputFile, line))
{
istringstream ss(line);
float heart;
ss >> heart;
//std::cout << std::fixed << std::setprecision(3) << heart << endl << endl;
}
ros::Publisher HeartRateInterval_pub = n.advertise<std_msgs::Float32>("HeartRateInterval", 1000);
ros::Rate loop_rate(1);
int count = 0;
while (ros::ok())
{
std_msgs::Float32 msg;
std::stringstream ss;
ss << std::cout << std::fixed << std::setprecision(3) << heart << endl << endl << count;
msg.data = ss.str();
HeartRateInterval_pub.publish(msg)
ROS_INFO("%s", msg.data.c_str());
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
另外
#include <iomanip>
然后,在输出值之前
cout << setprecision(3) << heart //... etc
这是我经过一番努力和运气得到的代码!它做我想做的事,唯一的小问题是我可以使用 Ctrl+c 关闭 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;
}
我正在尝试读取一个文本文件,该文件的一列中包含许多浮点数,如本例所示:
0.872
0.876
0.880
0.888
0.900
特别是我想逐行阅读它,每次读取一行时,我希望将该数值存储在一个变量中,该变量必须在 ROS 主题上发布,等待时间约为1 秒,然后它必须再次执行直到文件末尾。 我正在尝试逐步写下我的问题,以便(也许)更清楚:
- 读取第一行(例如 0.872)
- 将数字 0.872 保存在变量中
- 在 ROS 主题中发布只有 0.872 的值的变量
- 等一下
- 重复循环直到文件结束。
通过在网络和 Stack Overflow 上进行一些搜索,我编写了一个代码,目前可以在终端上打印数字(即使这不是我想要的),因为我不知道如何尽管我已经阅读了教程,但仍要执行用于发布的 ROS 命令。 但是有些数字似乎是这样切割的:
0.876
0.88
0.888
0.9
0.9
0.876
0.904
0.908
0.88
而不是在精度上与其他数字相等。 这是我现在编写的代码:
#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;
}
注意:它是用普通的 C++ 语言编写的,没有任何 ROS 命令,如 ROS init 等。所以,如果你能告诉我怎么做,那就太好了,因为我只做了一个到目前为止的 ROS 节点。
编辑:将建议的修改放入 ROS 节点、编译和执行后,它运行没有问题。 然后我尝试添加主题部分,但没有成功。我更新了下面的代码:
#include "ros/ros.h"
#include "std_msgs/Float32.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;
ifstream inputFile("/home/marco/Scrivania/marks.txt");
string line;
while (getline(inputFile, line))
{
istringstream ss(line);
float heart;
ss >> heart;
//std::cout << std::fixed << std::setprecision(3) << heart << endl << endl;
}
ros::Publisher HeartRateInterval_pub = n.advertise<std_msgs::Float32>("HeartRateInterval", 1000);
ros::Rate loop_rate(1);
int count = 0;
while (ros::ok())
{
std_msgs::Float32 msg;
std::stringstream ss;
ss << std::cout << std::fixed << std::setprecision(3) << heart << endl << endl << count;
msg.data = ss.str();
HeartRateInterval_pub.publish(msg)
ROS_INFO("%s", msg.data.c_str());
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
另外
#include <iomanip>
然后,在输出值之前
cout << setprecision(3) << heart //... etc
这是我经过一番努力和运气得到的代码!它做我想做的事,唯一的小问题是我可以使用 Ctrl+c 关闭 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;
}