Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hi, Like there is 1 subscriber and 1 publisher in the tutorial. Try to write 2 chatters. i.e each chatter should be able to listen and also talk. In the subscriber of the tutorial add a publisher with different topic. Here is example code. I haven't executed it but it should be simple enough to understand.

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

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

int main(int argc, char **argv)
{

  ros::init(argc, argv, "listener");   
  ros::NodeHandle n;   
  ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
  ros::Publisher chatter_pub = n.advertise<std_msgs::String>("different_chatter", 1000);

  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;
  }


  ros::spin();

  return 0;
}

Now you can write the second chatter listening to "different_chatter" and publishing to "chatter" topics. Hope this helps,

Karthik