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

Revision history [back]

click to hide/show revision 1
initial version

So you are basically locking for a publisher & subscriber in one single node for your PC. Actually is is a mixture of the Writing Publisher Subscriber Tutorial.

#include "ros/ros.h"
#include "std_msgs/UInt8.h"
#include "std_msgs/Bool.h"

ros::Publisher chatter_pub;


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

  std_msgs::Bool msgOut;
  msgOut.data = false;
  if (msg->data > 15)
     msgOut.data = true;

  chatter_pub.publish(msgOut);

}

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

  ros::NodeHandle nh;

  ros::Subscriber sub = nh.subscribe("active_listener", 1, chatterCallback);
  chatter_pub = nh.advertise<std_msgs::Bool>("chatter_within_subscriber", 1);

  ros::spin();

  return 0;
}

Hope it helps and is helpful enough despite the removed comments. Greetings, krl