How to write a function in ROS2 ?
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 ?
Asked by aks on 2018-04-26 11:58:56 UTC
Answers
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:
- https://github.com/ros2/demos/tree/master/demo_nodes_cpp/src/topics
- https://github.com/ros2/demos/tree/master/demo_nodes_cpp/src/services
Asked by William on 2018-04-26 12:39:15 UTC
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.
Asked by aks on 2018-04-27 06:29:06 UTC
Currently the time class is pretty primitive. The only thing you can get is integer nanoseconds, see: http://docs.ros2.org/ardent/api/rclcpp/classrclcpp_1_1_time.html I'd recommend something like this for now: msg.data = (2 * ros::Time::now()).nanoseconds() / 1e9;
Asked by William on 2018-04-27 11:47:37 UTC
Or msg.data = (ros::Time::now().nanoseconds() / 1e9) * 2;
depending on how you want to handle overflow and loss of precision.
Asked by William on 2018-04-27 11:48:26 UTC
@☻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 ?
Asked by aks on 2018-05-02 02:01:08 UTC
@William Basically I want the wall clock time as the ouptut (with or without being multiplied by a factor)
Asked by aks on 2018-05-02 02:12:28 UTC
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
.
Asked by William on 2018-05-02 02:21:23 UTC
You already suggested division by 1e9
in your previous comment. But that would also yield a floating value and not the timeclock.
Asked by aks on 2018-05-02 02:36:12 UTC
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.
Asked by William on 2018-05-02 02:44:39 UTC
Comments