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
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>
Asked by singhkartikey95 on 2021-10-29 23:34:46 UTC
Answers
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.
Asked by Mike Scheutzow on 2021-11-02 16:48:35 UTC
Comments