Ask Your Question
0

Listener doesn't receive msgs if launchs Talker with system command

asked 2014-07-23 03:38:28 -0500

genzostyle gravatar image

updated 2014-07-23 03:39:49 -0500

In the example in the tutorial of subscriber and listener everything works well, subscriber sends and listener receives messages. Using Linux, why if I launch the subscriber program from the listener with the command system in a new terminal, the listener doesn't receive any messages?

    #include "ros/ros.h"
    #include <std_msgs/Int32.h>   
    #include <signal.h> 
    #include <sstream>
    int main(int argc, char **argv)
    {    
      ros::init(argc, argv, "talker");
      ros::NodeHandle n;
      ros::Publisher chatter_pub = n.advertise<std_msgs::Int32>("chat", 1);
      ros::Rate loop_rate(1);
      int count = 0;
      while (ros::ok())
      {
        std_msgs::Int32 msg;
        msg.data = getpid();
        ROS_INFO("%i", msg.data);
        chatter_pub.publish(msg);
        ros::spinOnce();
        loop_rate.sleep();
        ++count;   
   }    
    return 0;
}

LISTENER:

#include "ros/ros.h"
#include <std_msgs/Int32.h>

void chatterCallback(const std_msgs::Int32::ConstPtr& msg)
{
  ROS_INFO("I heard: [%i]", msg->data);
}

int main(int argc, char **argv)
{
 int  status = system("gnome-terminal -x ./talker");
 printf("STATUS: %d", status);

  ros::init(argc, argv, "listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("chat", 1000, chatterCallback);
  ros::Rate loop_rate(1);
  int count = 0;
  while (ros::ok())
  {
    ros::spinOnce();
    loop_rate.sleep();


     ++count;
      }
  return 0;
}

STATUS:2

edit retag flag offensive close merge delete

Comments

what happens if you do int status = system("gnome-terminal -x rosrun your_package talker");

Mehdi. gravatar image Mehdi.  ( 2014-07-23 03:53:15 -0500 )edit

With int status = system("gnome-terminal -x rosrun talker"); listener opens a new terminal, but appear the text "There was an error creating the child process for this terminal. Failed to execute child process "rosrun" (No such file or directory)" and two buttons on the right: "Profile Preferences" and Relaunch.

genzostyle gravatar image genzostyle  ( 2014-07-23 04:15:36 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-07-23 04:44:57 -0500

updated 2014-07-23 04:54:50 -0500

You need to make sure that the executable for talker is on the same folder where you execute your listener or provide the absolute path to the talker command when you call system().

Also note that your process will be frozen until the system() call returns (when the process talker finishes).

You might encounter problems because of environment variables needed by ROS not being set when you call system().

One more thing, system() is evil and here is why. If you are trying to start automatically your talker when your listener starts, you should use a roslaunch file.

edit flag offensive delete link more
0

answered 2014-08-01 03:14:57 -0500

genzostyle gravatar image

Right! I must call " int status = system("gnome-terminal -x ./talker"); " in a thread so they can work in parallel. Thanks

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-07-23 03:38:28 -0500

Seen: 238 times

Last updated: Aug 01 '14