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

Write data from topic to text file in C++ in ros node

asked 2022-08-28 03:18:06 -0600

Basma gravatar image

I am trying to write poses to text file, the code is :

#include <ros/ros.h>
#include <nav_msgs/Odometry.h>
#include <iostream>
#include <string>
#include <fstream>

#include <sstream>
#include <stdio.h>

std::ofstream ofs ("home/basma/catkin_ws/poses.txt",std::ofstream::app);
void callhandler(const nav_msgs::Odometry::ConstPtr& msg)

{ if (ofs.is_open())
           //store array contents to text file

           ofs <<msg.pose.pose.x<<"\n";
    else printf("Unable to open file \n");

}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "main");
    ros::NodeHandle nh;
    ros::Subscriber Listener = nh.subscribe<nav_msgs::Odometry>("/odom", 100, callhandler);
    ros::spin();

    return 0;
}

But when use catkin_make the following error occure:

/home/basma/catkin_ws/src/poses_to_fileC/src/poses_to_fileC.cpp: In function ‘void callhandler(const ConstPtr&)’:
/home/basma/catkin_ws/src/poses_to_fileC/src/poses_to_fileC.cpp:15:22: error: ‘const ConstPtr’ {aka ‘const class boost::shared_ptr<const nav_msgs::Odometry_<std::allocator<void> > >’} has no member named ‘pose’

Could anyone help me? 15 | ofs <<msg.pose.pose.x&lt;&lt;"\n";< p="">

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2022-08-28 16:38:56 -0600

BlakeAnderson gravatar image

The build error is caused by the fact that msg is a pointer. You must dereference it using the -> operator.

edit flag offensive delete link more

Comments

Do you mean update the declaration as:

void callhandler(const nav_msgs::Odometry::ConstPtr-> msg)
Basma gravatar image Basma  ( 2022-08-29 03:57:02 -0600 )edit

You need to do: msg->pose.pose.x. This is called "dereferencing" the pointer.

I strongly recommend familiarizing yourself with C++ pointers: https://cplusplus.com/doc/tutorial/po...

BlakeAnderson gravatar image BlakeAnderson  ( 2022-08-29 09:49:03 -0600 )edit
0

answered 2022-09-07 05:26:06 -0600

Basma gravatar image

The problem has been sloved: I was wrongly running the node as : rosrun pkg_name nodefile.cpp But the correct way is running as :rosrun pkg_name nodefile

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-08-28 03:18:06 -0600

Seen: 389 times

Last updated: Sep 07 '22