#include #include #include
当前位置:   article > 正文

在ros中利用C++订阅话题输出为TXT文本及从文本读取发布话题_ros话题发布信息从文件读取

ros话题发布信息从文件读取

订阅话题输出TXT文本

/home/zero/catkin_ws/src/tang/src/save_point.cpp

rosrun tang save_point
  • 1
#include "ros/ros.h"
#include <geometry_msgs/PointStamped.h>
#include <fstream>
#include <iostream>
#include <Eigen/Core>
#include <iomanip>
using namespace std;

void subCallback(const geometry_msgs::PointStamped &msg)
{
   Eigen::Vector3d way_points;
   way_points(0)  = msg.point.x * 100 / 100;
   way_points(1)  = msg.point.y * 100 / 100;
   way_points(2)  = msg.point.z * 100 / 100;
//    double x = msg.point.x * 100 / 100;
//    double y = msg.point.y * 100 / 100;
//    double z = msg.point.z * 100 / 100;

   std::ofstream out("/home/zero/catkin_ws/src/tang/point.txt",std::ios::app);
   //<<fixed<<setprecision(2)的作用就是将输出结果转化为小数点后两位 
   // out<<fixed<<setprecision(2)<<way_points(0)<<"\t"<<way_points(1)<<"\t"<<way_points(2)<<std::endl;
   out<<fixed<<setprecision(2)<<way_points(0)<<"\t\t"<<way_points(1)<<"\t\t"<<way_points(2)<<std::endl; 
   // out<<fixed<<setprecision(2)<<x<<" "<<y<<" "<<z<<std::endl;
   out.close();
}
 
int main(int argc, char **argv){

    ros::init(argc,argv,"subscrible_01"); //初始化节点
    ros::NodeHandle nh;
    ros::Subscriber sub = nh.subscribe("clicked_point", 1000, subCallback);
    
    ros::spin ();
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

读取txt文本发布话题

/home/zero/catkin_ws/src/tang/src/read_point02.cpp

rosrun tang read_point02
  • 1
#include "ros/ros.h"
#include "std_msgs/String.h"

#include <geometry_msgs/PointStamped.h>
#include <sstream>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
 
void LoadImages(const string &trajectory, vector<string> &position_x)
{
    ifstream fTimes;
    // 数据流读取文件
    fTimes.open(trajectory.c_str());
    position_x.reserve(5000);

    while(!fTimes.eof())
    {
        string s;
        getline(fTimes,s);
        if(!s.empty())
        {
            // stringstream ss;
            // ss << s;
            //ROS_INFO("%s",s.c_str());
	        position_x.push_back(s); 
        }
    }
}
 
 
int main(int argc, char **argv){

    vector<string> vposition;
 
    // LoadImages(string(argv[1]),vposition);
    LoadImages("/home/zero/catkin_ws/src/tang/point.txt",vposition);
 
    ros::init(argc,argv,"publish_02"); //初始化节点
    ros::NodeHandle n; 
    ros::Publisher chatter_pub = n.advertise<geometry_msgs::PointStamped>("clicked_point",1); //定义发送端
    ros::Rate loop_rate(10); //频率
    //vector<string>::iterator i = vposition.begin();
    //auto i = vposition.begin();

    geometry_msgs::PointStamped msg;
    msg.header.frame_id = "map";
    // int num = vposition.size();

    // printf("num = %d \n",num);
    int i = 0;
    while (ros::ok()){
        // if (i == (vposition.end() - 5)){
        if (i > (vposition.size() - 1)){
            break;
        }


        string x, y, z;
    
        // for (int j = 0; j < vposition.size(); j++)
        // {
        x = vposition[i].substr(0, 5); //substr就是截取字符,20指的是在第几个字符上,13从第20个数据向后截取的长度。
        
        msg.point.x = atof(x.c_str());
        //X.push_back(atof(x.c_str())); //string类型转化为double型分为两步,先将string转为字符数组,然后数组转为double。
                                    //c_str将string转为数组,atof将数组转为double。
        y = vposition[i].substr(6, 11);
        msg.point.y = atof(y.c_str());
        
        msg.point.z = 0;

        chatter_pub.publish(msg);

        ROS_INFO("i = %d \n",i);
        i = i + 1;    
        ros::spinOnce();
        loop_rate.sleep();
	}
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

读取效果

在这里插入图片描述

参考文献:

C / C++ 保留两位小数(setprecision(n)的一些用法总结):

https://blog.csdn.net/qq_36667170/article/details/79265224
  • 1

C++读取txt文件的行数据和列数据:

https://blog.csdn.net/weixin_40224801/article/details/105207642
  • 1

C++读取txt文件,并利用ROS将其作为数据流输出:

https://blog.csdn.net/yg838457845/article/details/81232169
  • 1

C++如何在文件中保存数据并对齐?

https://blog.csdn.net/qq_34107003/article/details/107531724
  • 1

ROS学习 Python读写文本文件:

https://www.cnblogs.com/TIANHUAHUA/p/8443973.html
  • 1

ros中c++输出gps坐标为txt文件:

https://blog.csdn.net/weixin_41621060/article/details/108319923
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/462930
推荐阅读
相关标签