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

read topic without callback

asked 2014-05-27 11:11:11 -0500

crpizarr gravatar image

Is there a way to read the last message of a topic wihout using a callback? I mean, the tutorials teach to read a topic assigning a callback in a node, so that whenever a topic updates, the callback is executed. I want to read a topic when the user clicks a button, just once, something like this:

read_topic("/my_topic")

that returns the current data in the topic.

Thanks

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
3

answered 2014-05-27 12:50:41 -0500

ahendrix gravatar image

You can get the next message in a topic using ros::topic::waitForMessage().

Note that internally this creates a subscriber on the topic you're interested in, waits for the next message to arrive, and then closes itself down and returns that message to you, so it's a fairly heavy operation, and requires that the topic it's listening to is either a latched topic, or is published frequently.

edit flag offensive delete link more

Comments

Do you know if there is an aquivalent in Python?

DNiebuhr gravatar image DNiebuhr  ( 2021-02-22 08:27:17 -0500 )edit
0

answered 2014-11-15 11:39:55 -0500

yuyu gravatar image

This could be a way around? Example code

#include ros/ros.h>
#include boost/thread.hpp>
#include tf/transform_broadcaster.h>
#include turtlesim/Pose.h>

tf::Transform tfA;
tf::Transform tfB;
static boost::mutex mutex;

void topicACallback(const turtlesim::PoseConstPtr& msg){
  tf::Transform transform;
  transform.setOrigin( tf::Vector3(msg->x, msg->y, 0.0) );
  tf::Quaternion q;
  q.setRPY(0, 0, msg->theta);
  transform.setRotation(q);
  mutex.lock();
  tfA = transform;
  mutex.unlock();
}

void topicBCallback(const turtlesim::PoseConstPtr& msg){
  tf::Transform transform;
  transform.setOrigin( tf::Vector3(msg->x, msg->y, 0.0) );
  tf::Quaternion q;
  q.setRPY(0, 0, msg->theta);
  transform.setRotation(q);
  mutex.lock();
  tfB = transform;
  mutex.unlock();
}

void timerCallback(const ros::TimerEvent& event){
  tf::Transform tfA_local,tfB_local;
  mutex.lock();
  tfA_local = tfA;
  tfB_local = tfB;
  mutex.unlock();
  ROS_INFO("doing the rest of computation");
}

int main(int argc, char** argv){
  ros::init(argc, argv, "test");
  ros::NodeHandle node;
  ros::Subscriber sub1 = node.subscribe("/turtle1/pose", 10, &topicACallback);
  ros::Subscriber sub2 = node.subscribe("/turtle2/pose", 10, &topicBCallback);
  ros::Timer timer = node.createTimer(ros::Duration(0.1),timerCallback);
  ros::spin();
  return 0;
};
edit flag offensive delete link more
2

answered 2014-05-27 11:38:20 -0500

updated 2014-05-27 11:38:38 -0500

What you're looking for is a polled topic which was proposed a while ago, but unfortunately there is currently no plan for it to be implemented.

You can get the same effect by having a variable that gets updated with the most recent message on the topic every time the callback is called, and then read that variable whenever you need the topic.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-05-27 11:11:11 -0500

Seen: 11,172 times

Last updated: Nov 15 '14