Ask Your Question
0

How to write a function in ROS2 ?

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

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
0

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

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

Comments

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; msg.data = 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 imageaks ( 2018-04-27 06:29:06 -0500 )edit

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

William gravatar imageWilliam ( 2018-04-27 11:47:37 -0500 )edit

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

William gravatar imageWilliam ( 2018-04-27 11:48:26 -0500 )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 imageaks ( 2018-05-02 02:01:08 -0500 )edit

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

aks gravatar imageaks ( 2018-05-02 02:12:28 -0500 )edit

I'd recommend using std::chrono 's date utilities to format it: http://en.cppreference.com/w/cpp/chrono or just divide the nanoseconds by 1e9.

William gravatar imageWilliam ( 2018-05-02 02:21:23 -0500 )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 imageaks ( 2018-05-02 02:36:12 -0500 )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 imageWilliam ( 2018-05-02 02:44:39 -0500 )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

2 followers

Stats

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

Seen: 98 times

Last updated: Apr 26 '18