How to combine data from subscriber and data from node itself?

asked 2022-10-17 05:20:51 -0500

Edvard gravatar image

updated 2022-10-18 06:05:10 -0500

ravijoshi gravatar image

Hi, Sorry for my mistakes. English is not my native language. I have a node that calculates the position of my robot with an interval of one second. I have a separate topic where data from sensors is advertised. I need to write this data combined in one CSV file. How can I achieve this? Appreciate any help.

Update: Below is my code:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <tf/transform_listener.h>
#include <iostream>
#include <fstream>

tf::TransformListener listener;
tf::StampedTransform transform;
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
  std::stringstream sensor_data(msg->data);
  std::string segment;
  std::vector<std::string> parsed_data;

  while (std::getline(sensor_data, segment, '_'))
  {
    parsed_data.push_back(segment);
  }

  std::ofstream myfile;
  myfile.open("/home/mikita/catkin_ws/src/ros-minicar/minicar_current_pose/src/pose_file.csv");

  myfile << transform.getOrigin().x() << "," << transform.getOrigin().y() << "," << transform.getOrigin().z()
         << parsed_data[1] << "," << parsed_data[3] << "," << parsed_data[5] << "," << parsed_data[7] << parsed_data[9]
         << "," << parsed_data[11] << "," << parsed_data[13] << "," << parsed_data[15] << "\n";
}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "listener_sensor_data");

  ros::NodeHandle n;
  ros::Subscriber chatter_pub = n.subscribe<std_msgs::String>("Sensor_Data", 1000, chatterCallback);
  ros::spin();

  return 0;
}

That's how I think it should look. Sorry, I can't add here code that publishes sensor_data because it's still not complete and written by another person. So all that I know is that strings will be published.

I suppose adding a listener and transforming the header is best, but it's just a prototype. I'll fix those once the code starts working.

edit retag flag offensive close merge delete

Comments

I suggest adding the code to your question. If the code is very large, please share minimal reproducible example

ravijoshi gravatar image ravijoshi  ( 2022-10-17 05:25:18 -0500 )edit

just create global variable.

CroCo gravatar image CroCo  ( 2022-10-17 14:51:14 -0500 )edit

Thank you for sharing the code. It seems that you are using special strings in which values are separated by an underscore.

I can't add here code that publishes sensor_data...

I understand. However, may I request you share a sample sensor_data? On the other hand, I feel that instead of using string data, you could use a better ROS message. This is why I am interested to know what kind of data it is. How does it look?

I suppose adding a listener and transforming the header is best ...

This statement confuses me because your objective is to dump received data into a file. Why do we need a listener? What kind of transformation are you talking about?

I believe you need to add more details to the question and make the objective crystal clear.

ravijoshi gravatar image ravijoshi  ( 2022-10-18 06:13:17 -0500 )edit

I'm very grateful for the time you spend helping me. Thank you! I actually successfully solved this issue about hour ago. But just in case you curious, I use string because the man, who writes code, that will publish sensor data said me, that it will be more comfortable for him to publish this data in string format.

Those data are just 8 values of int and double type. I receive them, parse and then write them in csv file.

I thought to add them in header just to make code looks a little bit more decent.

And about my objective. I have sensor data in topic, I need to receive this data and write them in csv file along side with robot position in x,y,z coordinates.

Again, thank you for your help.

Edvard gravatar image Edvard  ( 2022-10-18 08:22:42 -0500 )edit

I'm sorry for this, but I need a little hint and I don't know if I should open new question or I can ask it here.I launched this node with launch file and it's printing messages with coordinates in terminal as It should but not writing any data in file, but in I run it with rosrun it works as it supposed to work. Maybe it's something obvious, but I'm have no idea where to look.

Edvard gravatar image Edvard  ( 2022-10-18 09:29:46 -0500 )edit

I actually successfully solved this issue about hour ago.

I am glad you made it work.

...it will be more comfortable for him to publish this data in string format.

Thanks for the clarification. I understand

I thought to add them in header ...

Sorry, which header? I couldn't see any in your code above.

... 8 values of int and double type. I receive them, parse ....

Parsing a string doesn't sound great to me, as I am concerned about memory consumption. Though, it works fine in many cases.

... then write them in csv file.

I suggest not doing an IO operation inside a callback. Better to append the data in a global variable (say a std::vector) as indicated by @CroCo and write the file upon exiting the node. Basically, IO operations are CPU intensive and take a significant amount of time. Please note that this suggestion is completely ...(more)

ravijoshi gravatar image ravijoshi  ( 2022-10-19 00:11:18 -0500 )edit