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

Design Question: Where Does The Business Logic Go?

asked 2013-11-01 07:56:34 -0600

trianta2 gravatar image

updated 2013-11-14 10:55:17 -0600

tfoote gravatar image

Hi Everyone,

I'm trying to design ROS nodes using best practice... the problem is I'm still trying to figure out what best practice is.

Let's say I've made a complex C++ class that does image recognition using OpenCV, Poco, and whatever libraries I could get my hands on.

Question #1:

Would it make more sense to keep the contents of a given ROS node to a minimum - just the boiler plate code - and then instantiate an object of this complex class inside the node (this way I could just use the class's interface and reduce the complexity)? Or would it make more sense to expand the logic of the class into one or more ROS nodes?

Question #2:

How efficient is it to use ROS node services to perform complex computation? For example, let's say I have an image that will get processed in a FOR loop 100 times, each time with a different set of parameters. Should I:

  1. Keep this looped process within one class within a single node and have it do all the work.
  2. Make a node that does the computation once, but instantiate 100 nodes to do the job?

Is there a latency between nodes that would not make this computationally practical? Does the communication between nodes suffer if the data being sent is large?

Thank you for your help!

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
12

answered 2013-11-01 15:32:15 -0600

mirzashah gravatar image

updated 2013-11-01 15:47:27 -0600

1) Your first question is a general software engineering/software architecture design problem and applies to more than just ROS. There are both costs and benefits to any abstraction you choose to solve a problem. Nodes provide encapsulation, isolation, modularity, robustness, the ability to run across a network -- but they come at the cost of spawning an OS-level process plus the cost of serializing/transport/deserializing messages. In general, if an idea is "big enough" (you be the judge of that), make it into a node. If you run into a problem where the interprocess communication overhead of ROS is too much on the same host...then you can always refactor and aggregate nodes together. If you're using C++, you also have the option of using nodelets which runs a node as a thread within a single process.

2) For your second question, option 1 makes more sense. Nodes are operating system processes...it's not "free" to create one in terms of resources. Also you have to send the image information to each node via ROS publishers/subscribers...again not free. You're serializing messages, sending them over TCP, deserializing them. This is contrast to spawning 100 threads within your process and having them share pointers. With the case of image processing, it would be a very bad idea to have 100 nodes subscribed to an image stream! Each node would have a P2P connection to the camera driver -- image streams are usually in the 10s of MB/s or higher then * 100.

I'm kind of getting the gut feeling that you're thinking of ROS nodes as primarily a means to parallelize computation. It is used for that in a sense, but high performance concurrent computing is not the point. Rather the point is to break down various aspects of a robot's software stack into modular components that can be reused, easily reasoned about, and isolated from each other. You could just write your software as one giant process with a single thread for each "node"...but if one thread segfaults, the entire thing goes down. As ROS nodes are processes, if one crashes, it won't bring down another node and can just start itself back up. Also ROS nodes allow multi-programming language development. You can write your image processor in C++ and I can then write a visualizer in Python to view your output -- I'll be able to talk to your C++ program through ROS topics. If you're interesting in computational node graphs, check out another cool project that came out of Willow Garage called "ecto" -- http://wiki.ros.org/ecto

To give you an understanding of how many nodes are usually running on a system...a PR2 runs in the ballpark of 150 nodes. These nodes include camera drivers, servo drivers, navigation algorithms, laser drivers. A Turtlebot2 runs around half that. Not saying there's a target number for a given robot or application, but trying to give you a ... (more)

edit flag offensive delete link more
2

answered 2013-11-01 18:36:27 -0600

Thomas gravatar image

updated 2013-11-01 18:36:39 -0600

To know what granularity is good for your nodes, keep in mind that a ROS node first job is to make processes communicate together. Having multiple processes allow you to have several independent algorithms running in parallel. However, processes are heavy objects and you cannot have a lot of them at the same time. To run thousands of tasks in parallel alternative techniques such as threading or coroutine are more suitable.

On top of that there is an overhead when you are communicating data from one node to another. ROS relies on the network to do this. This induces additional computation compared to a single-process algorithm.

To reduce this overhead and reduce the number of concurrent processes running, ROS supports nodelets. Nodelets are a way to aggregate several ROS nodes into one single process. This allow you to run as many nodes as you want into one process while removing the communication overhead.

However, using this model still has a cost as implementing nodelets is more challenging than challenging a normal node. In this case, think about the development cost of your application, the additional feature that ROS may or may not provide you and try to avoid over-engineer your solution.

edit flag offensive delete link more
0

answered 2021-03-22 01:59:00 -0600

GabrielLuke gravatar image

updated 2021-03-30 23:09:02 -0600

I've been considering writing my own tutorials because the ones on the wiki are sorely lacking. However, you can learn a lot by looking at and expanding the code given in the tutorials.

The Gazebo simulator is the way to go if you want to work with a virtual robot in ROS. It allows you to build a robot with virtual sensors and program it to respond to those sensors in the same way it would in the real world. It's a very strong tool that the DARPA robotics challenge teams use often. Notice that the Stage simulator (stage ros) is a 2D version that is much easier to use. However, since Gazebo is much more common and well-documented, I'd suggest starting there.

If you've learned the fundamentals of ROS programming (i.e., you know what the code in the tutorials does and can teleoperate a turtle by Fahad khan), check out the gazebosim tutorials; they've recently been modified and should be quite nice now. You may either follow the tutorials in sequence or go in the following order:

  1. Figure out how to incorporate a simple wheeled robot into the gazebo. Write some code to respond to a joystick or keypress by sending commands to the virtual robot. In Gazebo, teleoperate the robot.
  2. Attach a laser scanning sensor or a bumper to your virtual robot's world, as well as some obstacles. As you teleoperate the sensor around, you can see the output of the sensor published in ROS.
  3. Now that you've created the structure, it's time to create the robot's brains. There's a lot of navigation work already done for a robot with a laser scanner that you can more-or-less plug and play from the move base navigation stack. Alternatively, you will build your own intelligence and habits. When you're not bound by costly hardware, the sky's the limit.
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2013-11-01 07:56:34 -0600

Seen: 1,195 times

Last updated: Mar 30 '21