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

while doing rosrun the storage file "storage.csv" is getting genrated and data is being stored but while doing roslaunch even the file "storage.csv" is not being generated

asked 2021-10-29 23:34:46 -0500

singhkartikey95 gravatar image

while doing rosrun the storage file "storage.csv" is getting genrated and data is being stored but while doing roslaunch even the file "storage.csv" is not being generated

talker..

#include "ros/ros.h"
#include "std_msgs/String.h"

#include <sstream>

int main(int argc, char **argv)
{
  ros::init(argc, argv, "talker_node");
  ros::NodeHandle n;
  ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1);
  ros::Rate loop_rate(10);
  int count = 0;
  while (ros::ok())
  {
    std_msgs::String msg;

    std::stringstream ss;
    ss << "hello world " << count;
    msg.data = ss.str();
    ROS_INFO("%s", msg.data.c_str());
    chatter_pub.publish(msg);


    ros::spinOnce();



    loop_rate.sleep();

    ++count;
  }


  return 0;
}
// %EndTag(FULLTEXT)%

listener

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
#include <fstream>

std::ofstream MyFile; // to open the CSV file to store the data


void storage_callback(const std_msgs::String::ConstPtr& msg)
{
    MyFile.open("storage_csv.csv",std::ios::app);
    MyFile <<msg->data<<"\n";

         ROS_INFO("I recieve: [%s]", msg->data.c_str());
                    MyFile.close();
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "listener_node"); // creating node
    ros::NodeHandle n;
    ROS_INFO("Storage code");


    MyFile.open("storage_csv.csv");//,std::ios::app);
    MyFile << "listner data."<<"\n";    
    MyFile.close();

ros::Subscriber sub = n.subscribe ("/chatter", 1, storage_callback);

ros::spin();
    return 0;
}

cmake update

add_executable(talker1_executable src/talker.cpp)
target_link_libraries(talker1_executable ${catkin_LIBRARIES})

add_executable(listener1_executable src/listener.cpp)
target_link_libraries(listener1_executable ${catkin_LIBRARIES})

launch file

<launch>

<node name="takler_node" pkg="storage" type="talker1_executable" output="screen"/>
<node name="listener_node" pkg="storage" type="listener1_executable" output="screen"/>

</launch>
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-11-02 16:48:35 -0500

Mike Scheutzow gravatar image

The current working directory is changed when you use roslaunch. I believe it defaults to $HOME/.ros, so you should find your file in that location.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2021-10-29 23:34:46 -0500

Seen: 88 times

Last updated: Nov 02 '21