Ask Your Question
0

ROS 2 Sharing data/resources between class and pub and sub

asked 2019-06-20 21:40:12 -0600

neuromancer2701 gravatar image

So I have written a serial protocol C++ lib to talk to a couple of VESC motor controllers. I want to design a single class that will manage subscribing to the Twist message and publish custom message with all of the motor data that I have retrieved over the serial ports.

My initial idea is to have a timer that will take a class variable(RPM calculated from the Twist) then send that to the controller over the serial ports. Then at every nth call retrieve the motor data and store that in a set of class variables.

If both pub and sub classes are members of the managing class, how do I get the correct data to and from them. Do I need to put any protections around the data so that they don't step on each other?

Part of the reasoning for this to be contain all the interactions with the VESC controllers in a single node because the license will have to be GPL 3.0 to match the VESC code.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-06-21 06:11:59 -0600

gvdhoorn gravatar image

Do I need to put any protections around the data so that they don't step on each other?

This is going to sound pedantic, but: how would you do this in a multi-threaded setup without any ROS involved?


Thing is: using ROS doesn't necessarily influence this: whenever there is a potential for conflicts (ie: lost updates, concurrent writes, etc) when accessing a shared resource from multiple threads of execution it would be prudent to protect access to that resource with appropriate measures (locks, mutexes, etc).

Adding ROS here doesn't necessarily change any of that: Subscribers and Publishers don't directly access any data structures internal to your class(es)/node(s): your message and/or timer callbacks do that. That is code you write, so you control it. Adding appropriate guards to avoid problems could be done there.

ROS 1 and ROS 2 -- at a high level -- offer similar threading models: either single or multi-threaded event processing (spinners in ROS 1, executors in ROS 2).

In ROS 1 however, if you write the node, you embed the choice you made on which threading model will be used in your code. Users of your node would have to change the code to change it (and at that point they become responsible for making sure everything keeps working correctly).

ROS 2 -- through composition -- allows users more control over node execution and scheduling. There is potential for your code being used in multi-threaded executors, or in single threaded ones. As such, you can't avoid thinking about thread-safety of access to shared resources I believe.


But I believe there is no real difference between writing something that is thread-safe for use without ROS or with it. In the end it's the same, and I believe the same techniques can be used to deal with it.

edit flag offensive delete link more

Comments

PS: you may already have done so, but there are a few existing implementations of ROS<->VESC integrations that may be worth taking a look at.

One would be blinky-robot/vesc_driver. One of the last commits there would be completely overhaul mutual exclusion. I'm not sure whether that is completely related to your issue, but it seems at least like it deals with similar issues.

gvdhoorn gravatar imagegvdhoorn ( 2019-06-21 06:14:34 -0600 )edit

I just didn't know since ROS 2 added a threaded model did they add some sort of mechanism for sharing data between those threads.

Thanks

neuromancer2701 gravatar imageneuromancer2701 ( 2019-06-21 07:28:57 -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

1 follower

Stats

Asked: 2019-06-20 21:40:12 -0600

Seen: 117 times

Last updated: Jun 21