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

roscpp: Get number of publishers without subscribing

asked 2015-01-22 14:33:36 -0500

ahendrix gravatar image

I'm writing a library where I'd like to be able to get the number of publishers and subscribers on a topic, without subscribing to that topic.

Are there any functions in roscpp which can make this query directly, or do I need to make the xmlrpc calls and interpret the results myself?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2015-01-26 12:24:55 -0500

ahendrix gravatar image

I ended up doing this by calling the master API directly.

std::string node_name = ros::this_node::getName();
XmlRpc::XmlRpcValue request(node_name);
XmlRpc::XmlRpcValue response;
XmlRpc::XmlRpcValue payload;
bool success = ros::master::execute("getSystemState", request,
    response, payload, false);

ROS_ASSERT(payload.getType() == XmlRpc::XmlRpcValue::TypeArray);
ROS_ASSERT(payload.size() == 3);

bool published = false;
bool subscribed = false;

XmlRpc::XmlRpcValue & publishers = payload[0];
ROS_ASSERT(publishers.getType() == XmlRpc::XmlRpcValue::TypeArray);
for(int i=0; i<publishers.size(); i++) {
  XmlRpc::XmlRpcValue & topic = publishers[i];
  ROS_ASSERT(topic.getType() == XmlRpc::XmlRpcValue::TypeArray);
  ROS_ASSERT(topic[0].getType() == XmlRpc::XmlRpcValue::TypeString);
  std::string topic_name = topic[0];
  if(topic_name == response_topic_) {
    published = true;
  }
}

XmlRpc::XmlRpcValue & subscribers = payload[1];
ROS_ASSERT(subscribers.getType() == XmlRpc::XmlRpcValue::TypeArray);
for(int i=0; i<subscribers.size(); i++) {
  XmlRpc::XmlRpcValue & topic = subscribers[i];
  ROS_ASSERT(topic.getType() == XmlRpc::XmlRpcValue::TypeArray);
  ROS_ASSERT(topic[0].getType() == XmlRpc::XmlRpcValue::TypeString);
  std::string topic_name = topic[0];
  if(topic_name == request_topic_) {
    subscribed = true;
  }
}
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-01-22 14:33:36 -0500

Seen: 619 times

Last updated: Jan 26 '15