Search code examples
pythonc++roboticsros

ROS Service and Message


I am using ROS system and I am a beginner. I came across Service and Message (srv and msgs) in ROS. From the ros wiki, I manage to understand like msgs are used to define the type of message been passed and service is about request and response. Please correct me if I got this wrong.

Nevertheless, I am unable to understand when to use them. I thought that maybe it would be useful if I have modules written in C++ and other Processing Modules in Python than perhaps I can use srv and msgs for communicating between the 2 modules. However, than ROS also have publisher and subscriber system which could be used instead?

Secondly, when we use srv than only we need to define the msgs or either can be independently be used?


Solution

  • First of all, let me point you here:

    http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29

    It's the first example given by Ros tutorials.

    Publishers and subscribers are entities used to achieve node intercommunication. Publishers post messages to a channel, while subscribers listen to such channels to acquire messages possibly sent by publishers. Publishers post to channel using messages and which hold different kind of data depending on what is trying to be achieved. Subscribers take these messages and automatically execute a callback function to achieve something useful with the data.

    Now let me point you here:

    http://wiki.ros.org/ROS/Tutorials/WritingServiceClient%28c%2B%2B%29

    It is a similar example, but this time, services are used to achieve communication. The things with messages is that, once the node publishes a message, the whole communication thing is over and the node continues with the rest of its job. On the other hand, client (they use services) communicate with servers; they publish a message to a server, and then wait for a response by that server.

    Examples:

    1. Imagine you have two nodes: the first one creates and publishes random int numbers and the second one takes these numbers and publishes their squares on the screen. You can imagine that we only need messages there: the first node doesn't have to wait for an answer from the second node. The first node will use a publisher to provide the integers, while the second node will use a subscriber to get those integers and execute a function which will post their squares.
    2. Imagine you want to implement a tic-tac-toe game with ROS: now you'd like to have 3 nodes: the 2 of them are identical and they represent the two players, while the third one would be the AI handling the board. Now the AI publishes the board to a player, but it also needs to wait for answer; that answer would be the move that current player would choose. That's why services would have to be used here.

    If you make sure you understand a simple program with messages and a simple program with services, then you can expand your knowledge by creating examples, like the one presented above. Only then will you be able to fully grasp it with full detail.