Skip to content

Commit

Permalink
[Documentation] Add simple pub-sub tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
ipa-nhg committed Nov 20, 2023
1 parent f2be608 commit a97653d
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 3 deletions.
134 changes: 134 additions & 0 deletions docu/Example_PubSub.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Tutorial: How to create a simple Publisher-Subscriber example.

In this tutorial we just want to show how the models can be used to define ROS nodes.
For this we will see how two simple nodes can be defined, one that acts as a publisher of a "Hello World!" message and another that receives it as a subscriber.

This tutorial is fictitious, it is not based on any existing ROS node. The ROS example is reflected in the models of the example [pub_sub_ros2](https://github.com/ipa-nhg/ros-model-examples/tree/main/pub_sub_ros2).

The first step is to create a project to hold the models.

To import this as a project in the RosTooling you can create a new modeling project. By File -> New -> Project -> General -> Project. Then you must give a name to the project, for example "Example" press next, and add as project reference "de.fraunhofer.ipa.ros.communication.objects". We recommend to create a folder called "rosnodes" within the project.

![alt text](images/pubsub_tutorial1.gif)

Once the project is created, you can create a new file my File -> New -> Other -> File. The file shall be created under the folder "rosnodes" and it must have the extension ".ros2", for example **publisher.ros2**. By creating a file type .ros2, Eclipse will convert the project to a Xtext project. Then copy the previous content to the new file.

![alt text](images/pubsub_tutorial2.gif)

Following the format of the [Ros model](RosModelDescription.md) we can now create a new Ros package, that contains a node with a publisher.

The first line of the model must contain the name of the package, as part of the grammar of the model ":" will close the name definition line:

```
publisher_package:
```

Then we need the artifact, a runnable to execute the node. As every YAML format file we have to add identantion to the secon line. Then pressing the keys "Ctrl"+Space bar the menu will suggest us as an option the text "artifacts:". We select it and go to the next line.

In the third line we must add a douple identation, we will create an object under artifacts. The next object will be a name for the artifact, for example "pub_artifact":

```
publisher:
artifacts:
pub_artifact:
```

Similar to the artifact creation, we need to create a node, with the grammar "node: **NODENAME**":

```
publisher:
artifacts:
pub_artifact:
node: pub_node
```
Now that we have the node, we can define the interfaces that offers this node as inputs and output to connect to it. In our case we want to create a publisher, which the type **String** .

![alt text](images/pubsub_tutorial3.gif)


The models will look similar to:

```
publisher:
artifacts:
pub_artifact:
node: pub_node
publishers:
my_pub:
type: "std_msgs/msg/String"
```

In the same way we can create a subscriber:

![alt text](images/pubsub_tutorial4.gif)

This new subscriber model, will look like:

```
subscriber:
artifacts:
sub_artifact:
node: sub_node
subscribers:
my_sub:
type: "std_msgs/msg/String"
```

Now that we have already the components we can compose them. For that we have to create a new .rossystem file. Again go to File -> New -> Other -> File. The new file must have as extension .rossystem.

In [RosSystem description](RosSystemModelDescription.md) we explain the format of a system and the editor will support you to write the model properly.

The first that must be given is a name and then a ":" is required. In the next line you must add identation and you can press the keys "Ctrl" + Space bar for help.
Then we will define the nodes that compose the system. Here under "from" we will link our previously created nodes, the grammar for the references is **PackageName.NodeName**.

![alt text](images/pubsub_tutorial5.gif)

So far our file looks like:
```
my_system:
nodes:
publisher:
from: "publisher.pub_node"
subscriber:
from: "subscriber.sub_node"
```

Now, we want to expose the ports to be connected. For that we have to define the interfaces under the nodes. And again we have to reference the created interfaces, with the grammar **NodeName::InterfaceName**.

![alt text](images/pubsub_tutorial6.gif)

And the model is updated to:
```
my_system:
nodes:
publisher:
from: "publisher.pub_node"
interfaces:
- HelloWorldPublisher: pub-> "pub_artifact::my_pub"
subscriber:
from: "subscriber.sub_node"
interfaces:
- HelloWorldSubscriber: sub-> "sub_artifact::my_sub"
```
The last step is to create the connection between the two components. For that we will use the connections description, under the system description. The connections must be done between interfaces instances described within the models, otherwise the model validator will give an error.

![alt text](images/pubsub_tutorial7.gif)

And the model is updated to:
```
my_system:
nodes:
publisher:
from: "publisher.pub_node"
interfaces:
- HelloWorldPublisher: pub-> "pub_artifact::my_pub"
subscriber:
from: "subscriber.sub_node"
interfaces:
- HelloWorldSubscriber: sub-> "sub_artifact::my_sub"
connections:
-[ HelloWorldPublisher, HelloWorldSubscriber]
```

With this very basic example we hope you have understood how the models can be used and what kind of attributes they allow to represent.
6 changes: 3 additions & 3 deletions docu/Example_Turtlesim.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Hands-on example for the Turtlesim node

to learn ROS one of the first tutoials that everyone does is turtlesim.
To learn ROS one of the first tutoials that everyone does is turtlesim.

Basically it is a small graphical interface where appears a turtle that I can move using the speed commands of any robotic base in ROS. [Official TurtleSim tutorial](https://docs.ros.org/en/foxy/Tutorials/Beginner-CLI-Tools/Introducing-Turtlesim/Introducing-Turtlesim.html)

Expand Down Expand Up @@ -58,7 +58,7 @@ To import this as a project in the RosTooling you can create a new modeling proj

Once the project is created, you can create a new file my File -> New -> Other -> File. We recommend to give as name to the file the name of the package and its must have the extension .ros2, this means the new file should be called **turtlesim.ros2**. By creating a file type .ros2, Eclipse will convert the project to a Xtext project. Then copy the previous content to the new file.

Now that we have already the components we can compose them. For that ww have to create a new .rossystem file. Again go to File -> New -> Other -> File. The new file must have as extension .rossystem.
Now that we have already the components we can compose them. For that we have to create a new .rossystem file. Again go to File -> New -> Other -> File. The new file must have as extension .rossystem.

In [RosSystem description](RosSystemModelDescription.md) we explain the format of a system and the editor will support you to write the model properly.

Expand All @@ -77,7 +77,7 @@ turtlesim_system:
from: "turtlesim.turtle_teleop_key"
```

Now, we want to expose the ports to be connected. This means the subcriber of the velocity command of the turtle and the publisher from the keyboard teleop:
Now, we want to expose the ports to be connected. This means the subscriber of the velocity command of the turtle and the publisher from the keyboard teleop:

![alt text](images/turtlesim_tutorial2.gif)

Expand Down
3 changes: 3 additions & 0 deletions docu/images/pubsub_tutorial1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docu/images/pubsub_tutorial2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docu/images/pubsub_tutorial3.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docu/images/pubsub_tutorial4.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docu/images/pubsub_tutorial5.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docu/images/pubsub_tutorial6.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docu/images/pubsub_tutorial7.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a97653d

Please sign in to comment.