ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

How to write a function in ROS2 ?

asked 2018-04-26 11:58:56 -0600

aks gravatar image

I created a new package and updated the CMakeList.txt and Package.xml file.

Now i need to write a simple function : output = input*time and display it . Do i need to use service for this or could the publisher and subscriber be extended without any need for service?

How to use the ROS::time() API ?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2018-04-26 12:39:15 -0600

William gravatar image

The closest analog to a function in ROS (either 1 or 2) is a Service, because you get a response (like a function return value) along with data in the request (like a function's arguments) and they are automatically associated (each response is associated with one request).

If you don't require an association between the input data and some transformed output data, then using two publish/subscribe pairs will also work. These have the benefit of easier introspection (either the requests or responses can be observed by more that one entity).

You'll have to decide which pattern is more appropriate for your use case.

Since you're using ROS 2, I recommend you look at the topic and service examples for clues as to how to implement each case:

edit flag offensive delete link more


Is it possible in ROS to multiply a Time with float and store it in a float and publish that ? something like : std_msgs::Float32 msg; = 2*ros::Time::now() If not, then just publish the time topic_publisher(ros::Time::now(). I tried both but unable to build.

aks gravatar image aks  ( 2018-04-27 06:29:06 -0600 )edit

Currently the time class is pretty primitive. The only thing you can get is integer nanoseconds, see: I'd recommend something like this for now: = (2 * ros::Time::now()).nanoseconds() / 1e9;

William gravatar image William  ( 2018-04-27 11:47:37 -0600 )edit

Or = (ros::Time::now().nanoseconds() / 1e9) * 2; depending on how you want to handle overflow and loss of precision.

William gravatar image William  ( 2018-04-27 11:48:26 -0600 )edit

@☻William Thanks for the suggestion but somehow i want the time stamp being displayed at the console. Something like : Publishing at 09:00:01 and i guess this will only give me a float or an integer value. Do you think using std_msgs::Time would be the right approach ?

aks gravatar image aks  ( 2018-05-02 02:01:08 -0600 )edit

@William Basically I want the wall clock time as the ouptut (with or without being multiplied by a factor)

aks gravatar image aks  ( 2018-05-02 02:12:28 -0600 )edit

I'd recommend using std::chrono 's date utilities to format it: or just divide the nanoseconds by 1e9.

William gravatar image William  ( 2018-05-02 02:21:23 -0600 )edit

You already suggested division by 1e9 in your previous comment. But that would also yield a floating value and not the timeclock.

aks gravatar image aks  ( 2018-05-02 02:36:12 -0600 )edit

As I said, if you want something nicer than floating point seconds, then you can use the standard library to get pretty printed times. The ROS message version of time, i.e. builtin_msgs::msg::Time, will not help you print it nicer.

William gravatar image William  ( 2018-05-02 02:44:39 -0600 )edit

Your Answer

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

Add Answer

Question Tools



Asked: 2018-04-26 11:58:56 -0600

Seen: 211 times

Last updated: Apr 26 '18