Skip to content

Commit

Permalink
Python tutorial updates.
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Agüero <[email protected]>
  • Loading branch information
caguero committed Aug 29, 2024
1 parent 47c01d4 commit 8195034
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions tutorials/06_python_support.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ In this tutorial, we are going to show the functionalities implemented in Python
These features are brought up by creating bindings from the C++ implementation
using pybind11. It is important to note that not all of C++ features are available
yet, on this tutorial we will go over the most relevant features. For more information,
refer to the [__init__.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/src/__init__.py)
refer to the [__init__.py](https://github.com/gazebosim/gz-transport/blob/gz-transport14/python/src/__init__.py)
file which is a wrapper for all the bindings.

For this tutorial, we will create two nodes that communicate via messages. One node
Expand All @@ -24,24 +24,24 @@ cd ~/gz_transport_tutorial

## Publisher

Download the [publisher.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/examples/publisher.py) file within the `gz_transport_tutorial`
Download the [publisher.py](https://github.com/gazebosim/gz-transport/blob/gz-transport14/python/examples/publisher.py) file within the `gz_transport_tutorial`
folder and open it with your favorite editor:

\snippet python/examples/publisher.py complete

### Walkthrough

```{.py}
from gz.msgs10.stringmsg_pb2 import StringMsg
from gz.msgs10.vector3d_pb2 import Vector3d
from gz.transport13 import Node
from gz.msgs11.stringmsg_pb2 import StringMsg
from gz.msgs11.vector3d_pb2 import Vector3d
from gz.transport14 import Node
```

The library `gz.transport13` contains all the Gazebo Transport elements that can be
The library `gz.transport14` contains all the Gazebo Transport elements that can be
used in Python. The final API we will use is contained inside the class `Node`.

The lines `from gz.msgs10.stringmsg_pb2 import StringMsg` and `from gz.msgs10.vector3d_pb2 import Vector3d`
includes the generated protobuf code that we are going to use for our messages.
The lines `from gz.msgs11.stringmsg_pb2 import StringMsg` and `from gz.msgs11.vector3d_pb2 import Vector3d`
include the generated protobuf code that we are going to use for our messages.
We are going to publish two types of messages: `StringMsg` and `Vector3d` protobuf
messages.

Expand Down Expand Up @@ -89,20 +89,20 @@ each topic. The method *publish()* sends a message to all the subscribers.

## Subscriber

Download the [subscriber.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/examples/subscriber.py)
Download the [subscriber.py](https://github.com/gazebosim/gz-transport/blob/gz-transport14/python/examples/subscriber.py)
file into the `gz_transport_tutorial` folder and open it with your favorite editor:

\snippet python/examples/subscriber.py complete

### Walkthrough

```{.py}
from gz.msgs10.stringmsg_pb2 import StringMsg
from gz.msgs10.vector3d_pb2 import Vector3d
from gz.transport13 import Node
from gz.msgs11.stringmsg_pb2 import StringMsg
from gz.msgs11.vector3d_pb2 import Vector3d
from gz.transport14 import Node
```

Just as before, we are importing the `Node` class from the `gz.transport13` library
Just as before, we are importing the `Node` class from the `gz.transport14` library
and the generated code for the `StringMsg` and `Vector3d` protobuf messages.

```{.py}
Expand Down Expand Up @@ -226,9 +226,9 @@ in several places (publisher and subscribers).

We developed a couple of examples that demonstrate this particular issue. Take
a look at a publisher and subscriber (whithin the same node) that have race
conditions triggered in the [data_race_without_mutex.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/examples/data_race_without_mutex.py) file. The proposed solution to this
conditions triggered in the [data_race_without_mutex.py](https://github.com/gazebosim/gz-transport/blob/gz-transport14/python/examples/data_race_without_mutex.py) file. The proposed solution to this
issue is to use the `threading` library, you can see the same example with a mutex
in the [data_race_with_mutex.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/examples/data_race_with_mutex.py) file.
in the [data_race_with_mutex.py](https://github.com/gazebosim/gz-transport/blob/gz-transport14/python/examples/data_race_with_mutex.py) file.

You can run any of those examples by just doing the following in a terminal:
```{.sh}
Expand All @@ -251,8 +251,8 @@ of messages published per topic.
We can declare the throttling option using the following code :

```{.py}
from gz.msgs10.stringmsg_pb2 import StringMsg
from gz.transport13 import Node, AdvertiseMessageOptions
from gz.msgs11.stringmsg_pb2 import StringMsg
from gz.transport14 import Node, AdvertiseMessageOptions
# Create a transport node and advertise a topic with throttling enabled.
node = Node()
Expand Down Expand Up @@ -296,8 +296,8 @@ second from that particular topic.
We can declare the throttling option using the following code :

```{.py}
from gz.msgs10.stringmsg_pb2 import StringMsg
from gz.transport13 import Node, SubscribeOptions
from gz.msgs11.stringmsg_pb2 import StringMsg
from gz.transport14 import Node, SubscribeOptions
def stringmsg_cb(msg: StringMsg):
print("Received StringMsg: [{}]".format(msg.data))
Expand Down Expand Up @@ -337,7 +337,7 @@ topic without having to modify the publisher and create a new log.
We can declare the topic remapping option using the following code:

```{.py}
from gz.transport13 import Node, NodeOptions
from gz.transport14 import Node, NodeOptions
# Create a transport node and remap a topic.
nodeOpts = NodeOptions()
Expand Down Expand Up @@ -367,19 +367,19 @@ The command `gz log playback` also supports the notion of topic remapping. Run

## Service Requester

Download the [requester.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/examples/requester.py)
Download the [requester.py](https://github.com/gazebosim/gz-transport/blob/gz-transport14/python/examples/requester.py)
file into the `gz_transport_tutorial` folder and open it with your favorite editor:

\snippet python/examples/requester.py complete

### Walkthrough

```{.py}
from gz.msgs10.stringmsg_pb2 import StringMsg
from gz.transport13 import Node
from gz.msgs11.stringmsg_pb2 import StringMsg
from gz.transport14 import Node
```

Just as before, we are importing the `Node` class from the `gz.transport13`
Just as before, we are importing the `Node` class from the `gz.transport14`
library and the generated code for the `StringMsg` protobuf message.

```{.py}
Expand Down Expand Up @@ -408,7 +408,7 @@ result and response of the request in some variables and printing them out.

Unfortunately, this feature is not available on Python at the moment. However,
we can use a service responser created in C++ and make a request to it from a
code in Python. Taking that into account, we will use the [response.cc](https://github.com/gazebosim/gz-transport/blob/gz-transport13/example/responser.cc) file as the service responser.
code in Python. Taking that into account, we will use the [response.cc](https://github.com/gazebosim/gz-transport/blob/gz-transport14/example/responser.cc) file as the service responser.

## Running the examples

Expand Down

0 comments on commit 8195034

Please sign in to comment.