eProsima Fast DDS Python is a Python binding for the eProsima Fast DDS C++ library.
This is a work in progress, but ultimately the goal is having the complete Fast DDS API available in Python.
Two packages are available in this repository: the proper Python binding, fastdds_python, and the examples, fastdds_python_examples.
This tutorial shows how to build Fast DDS Python using colcon, a command line tool to build sets of software packages.
To do so, colcon and vcstool need to be installed:
pip install -U colcon-common-extensions vcstoolFast DDS Python depends on Fast DDS and Fast CDR. For simplicity, this tutorial will build these dependencies alongside the binding itself. More advanced users can build or link to this packages separately.
Install Fast DDS dependencies running:
sudo apt update
sudo apt install -y \
libasio-dev \
libtinyxml2-devAdditionally, Fast DDS Python also depends on SWIG and python3-dev. Install these dependencies running:
sudo apt update
sudo apt install -y \
swig \
libpython3-dev# Change directory to the location where the colcon workspace will be created
cd <path_to_ws>
# Create workspace directory
mkdir -p fastdds_python_ws/src
cd fastdds_python_ws
# Get workspace setup file
wget https://raw.githubusercontent.com/eProsima/Fast-DDS-python/main/fastdds_python.repos
# Download repositories
vcs import src < fastdds_python.repos
# Build the workspace
colcon buildPlease, refer to colcon documentation for more information, such as building only one of the packages.
This project is on the very early stages of development, and there are many features not available yet. These include, but are not restricted to:
- QoS modification is not supported on python. It is possible to create a QoS object with the default constructor
or retrieve it with the
get_qosmethods of the entities, but it is not possible to modify the QoS values. If you need to use non-default QoS, please use XML configuration files.
The Fast DDS functionality is contained in the fastdds module, so you will need to include that module in your script. You will also need to create the python binding for your data type and include its module. This example will guide you through these steps in a simple example.
Create an IDL file with the description of your data type and save it as HelloWorld.idl:
struct HelloWorld
{
unsigned long index;
string message;
};
Use Fast DDS gen to generate the necessary files from this IDL. If you installed Fast DDS Python using the fastdds_python.repos file, you will find Fast DDS Gen in the src file. Do not forget to use the -python option to create the files needed for the python binding:
fastddsgen -python HelloWorld.idlNow use the generated CMakeFile to compile the data type and create the python binding:
mkdir HelloWorld_build
cd HelloWorld_build
cmake ..
cmake --build .This will create a HelloWorld.py file with a HelloWorld module that you will need to add to your script.
Import the fastdds and the HelloWorld modules and follow the usual steps to create a DataWriter:
import fastdds
import HelloWorld
domain = 5;
factory = fastdds.DomainParticipantFactory.get_instance()
participant_qos = fastdds.DomainParticipantQos()
factory.get_default_participant_qos(participant_qos)
participant = factory.create_participant(domain, participant_qos)
topic_data_type = HelloWorld.HelloWorldPubSubType()
topic_data_type.setName("HelloWorldDataType")
type_support = fastdds.TypeSupport(topic_data_type)
participant.register_type(type_support)
topic_qos = fastdds.TopicQos()
participant.get_default_topic_qos(topic_qos)
topic = self.participant.create_topic("myTopic", topic_data_type.getName(), topic_qos)
publisher_qos = fastdds.PublisherQos()
participant.get_default_publisher_qos(publisher_qos)
publisher = participant.create_publisher(publisher_qos)
writer_qos = fastdds.DataWriterQos()
publisher.get_default_datawriter_qos(writer_qos)
writer = self.publisher.create_datawriter(topic, writer_qos)You can publish a sample the same way you would do it in C++:
data = HelloWorld.HelloWorld()
data.message("Hello World")
data.index(0)
writer.write(data)Import the fastdds and the HelloWorld modules and follow the usual steps to create a DataReader:
import fastdds
import HelloWorld
domain = 5;
factory = fastdds.DomainParticipantFactory.get_instance()
participant_qos = fastdds.DomainParticipantQos()
factory.get_default_participant_qos(participant_qos)
participant = factory.create_participant(domain, participant_qos)
topic_data_type = HelloWorld.HelloWorldPubSubType()
topic_data_type.setName("HelloWorldDataType")
type_support = fastdds.TypeSupport(topic_data_type)
participant.register_type(type_support)
topic_qos = fastdds.TopicQos()
participant.get_default_topic_qos(topic_qos)
topic = participant.create_topic("myTopic", topic_data_type.getName(), topic_qos)
subscriber_qos = fastdds.SubscriberQos()
participant.get_default_subscriber_qos(subscriber_qos)
subscriber = participant.create_subscriber(subscriber_qos)
reader_qos = fastdds.DataReaderQos()
subscriber.get_default_datareader_qos(reader_qos)
reader = subscriber.create_datareader(topic, reader_qos)You can read a sample the same way you would do it in C++:
info = fastdds.SampleInfo()
data = HelloWorld.HelloWorld()
reader.take_next_sample(data, info)
print("Received {message} : {index}".format(message=data.message(), index=data.index()))