-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refs #13459. Add Python getting started Signed-off-by: Ricardo González Moreno <[email protected]> * Refs #13459. Fix video Signed-off-by: Ricardo González Moreno <[email protected]> * Refs #13639. Fix tests Signed-off-by: Ricardo González Moreno <[email protected]> * Refs #13639. Fix warnings Signed-off-by: Ricardo González Moreno <[email protected]> * Apply suggestions from code review Co-authored-by: jsantiago-eProsima <[email protected]> Signed-off-by: Ricardo González Moreno <[email protected]> * Apply suggestions from code review Co-authored-by: jsantiago-eProsima <[email protected]> Signed-off-by: Ricardo González Moreno <[email protected]> * Apply suggestions from code review Co-authored-by: jsantiago-eProsima <[email protected]> Signed-off-by: Ricardo González Moreno <[email protected]> * Refs #13639. Apply suggestions on C++ example Signed-off-by: Ricardo González Moreno <[email protected]> * Apply suggestions from code review Co-authored-by: jsantiago-eProsima <[email protected]> Signed-off-by: Ricardo González Moreno <[email protected]> * Refs #13639. Fix error Signed-off-by: Ricardo González Moreno <[email protected]> * Refs #13639. Fix too long lines Signed-off-by: Ricardo González Moreno <[email protected]> Co-authored-by: jsantiago-eProsima <[email protected]>
- Loading branch information
Showing
20 changed files
with
622 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
HelloWorld Publisher | ||
""" | ||
from threading import Condition | ||
import time | ||
|
||
import fastdds | ||
import HelloWorld | ||
|
||
DESCRIPTION = """HelloWorld Publisher example for Fast DDS python bindings""" | ||
USAGE = ('python3 HelloWorldPublisher.py') | ||
|
||
class WriterListener (fastdds.DataWriterListener) : | ||
def __init__(self, writer) : | ||
self._writer = writer | ||
super().__init__() | ||
|
||
|
||
def on_publication_matched(self, datawriter, info) : | ||
if (0 < info.current_count_change) : | ||
print ("Publisher matched subscriber {}".format(info.last_subscription_handle)) | ||
self._writer._cvDiscovery.acquire() | ||
self._writer._matched_reader += 1 | ||
self._writer._cvDiscovery.notify() | ||
self._writer._cvDiscovery.release() | ||
else : | ||
print ("Publisher unmatched subscriber {}".format(info.last_subscription_handle)) | ||
self._writer._cvDiscovery.acquire() | ||
self._writer._matched_reader -= 1 | ||
self._writer._cvDiscovery.notify() | ||
self._writer._cvDiscovery.release() | ||
|
||
|
||
class Writer: | ||
|
||
|
||
def __init__(self): | ||
self._matched_reader = 0 | ||
self._cvDiscovery = Condition() | ||
self.index = 0 | ||
|
||
factory = fastdds.DomainParticipantFactory.get_instance() | ||
self.participant_qos = fastdds.DomainParticipantQos() | ||
factory.get_default_participant_qos(self.participant_qos) | ||
self.participant = factory.create_participant(0, self.participant_qos) | ||
|
||
self.topic_data_type = HelloWorld.HelloWorldPubSubType() | ||
self.topic_data_type.setName("HelloWorld") | ||
self.type_support = fastdds.TypeSupport(self.topic_data_type) | ||
self.participant.register_type(self.type_support) | ||
|
||
self.topic_qos = fastdds.TopicQos() | ||
self.participant.get_default_topic_qos(self.topic_qos) | ||
self.topic = self.participant.create_topic("HelloWorldTopic", self.topic_data_type.getName(), self.topic_qos) | ||
|
||
self.publisher_qos = fastdds.PublisherQos() | ||
self.participant.get_default_publisher_qos(self.publisher_qos) | ||
self.publisher = self.participant.create_publisher(self.publisher_qos) | ||
|
||
self.listener = WriterListener(self) | ||
self.writer_qos = fastdds.DataWriterQos() | ||
self.publisher.get_default_datawriter_qos(self.writer_qos) | ||
self.writer = self.publisher.create_datawriter(self.topic, self.writer_qos, self.listener) | ||
|
||
|
||
def write(self): | ||
data = HelloWorld.HelloWorld() | ||
data.message("Hello World") | ||
data.index(self.index) | ||
self.writer.write(data) | ||
print("Sending {message} : {index}".format(message=data.message(), index=data.index())) | ||
self.index = self.index + 1 | ||
|
||
|
||
def wait_discovery(self) : | ||
self._cvDiscovery.acquire() | ||
print ("Writer is waiting discovery...") | ||
self._cvDiscovery.wait_for(lambda : self._matched_reader != 0) | ||
self._cvDiscovery.release() | ||
print("Writer discovery finished...") | ||
|
||
|
||
def run(self): | ||
self.wait_discovery() | ||
for x in range(10) : | ||
time.sleep(1) | ||
self.write() | ||
self.delete() | ||
|
||
|
||
def delete(self): | ||
factory = fastdds.DomainParticipantFactory.get_instance() | ||
self.participant.delete_contained_entities() | ||
factory.delete_participant(self.participant) | ||
|
||
|
||
if __name__ == '__main__': | ||
print('Starting publisher.') | ||
writer = Writer() | ||
writer.run() | ||
exit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
HelloWorld Subscriber | ||
""" | ||
import signal | ||
|
||
import fastdds | ||
import HelloWorld | ||
|
||
DESCRIPTION = """HelloWorld Subscriber example for Fast DDS python bindings""" | ||
USAGE = ('python3 HelloWorldSubscriber.py') | ||
|
||
# To capture ctrl+C | ||
def signal_handler(sig, frame): | ||
print('Interrupted!') | ||
|
||
class ReaderListener(fastdds.DataReaderListener): | ||
|
||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
|
||
def on_subscription_matched(self, datareader, info) : | ||
if (0 < info.current_count_change) : | ||
print ("Subscriber matched publisher {}".format(info.last_publication_handle)) | ||
else : | ||
print ("Subscriber unmatched publisher {}".format(info.last_publication_handle)) | ||
|
||
|
||
def on_data_available(self, reader): | ||
info = fastdds.SampleInfo() | ||
data = HelloWorld.HelloWorld() | ||
reader.take_next_sample(data, info) | ||
|
||
print("Received {message} : {index}".format(message=data.message(), index=data.index())) | ||
|
||
|
||
class Reader: | ||
|
||
|
||
def __init__(self): | ||
factory = fastdds.DomainParticipantFactory.get_instance() | ||
self.participant_qos = fastdds.DomainParticipantQos() | ||
factory.get_default_participant_qos(self.participant_qos) | ||
self.participant = factory.create_participant(0, self.participant_qos) | ||
|
||
self.topic_data_type = HelloWorld.HelloWorldPubSubType() | ||
self.topic_data_type.setName("HelloWorld") | ||
self.type_support = fastdds.TypeSupport(self.topic_data_type) | ||
self.participant.register_type(self.type_support) | ||
|
||
self.topic_qos = fastdds.TopicQos() | ||
self.participant.get_default_topic_qos(self.topic_qos) | ||
self.topic = self.participant.create_topic("HelloWorldTopic", self.topic_data_type.getName(), self.topic_qos) | ||
|
||
self.subscriber_qos = fastdds.SubscriberQos() | ||
self.participant.get_default_subscriber_qos(self.subscriber_qos) | ||
self.subscriber = self.participant.create_subscriber(self.subscriber_qos) | ||
|
||
self.listener = ReaderListener() | ||
self.reader_qos = fastdds.DataReaderQos() | ||
self.subscriber.get_default_datareader_qos(self.reader_qos) | ||
self.reader = self.subscriber.create_datareader(self.topic, self.reader_qos, self.listener) | ||
|
||
|
||
def delete(self): | ||
factory = fastdds.DomainParticipantFactory.get_instance() | ||
self.participant.delete_contained_entities() | ||
factory.delete_participant(self.participant) | ||
|
||
|
||
def run(self): | ||
signal.signal(signal.SIGINT, signal_handler) | ||
print('Press Ctrl+C to stop') | ||
signal.pause() | ||
self.delete() | ||
|
||
|
||
if __name__ == '__main__': | ||
print('Creating subscriber.') | ||
reader = Reader() | ||
reader.run() | ||
exit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.. |DataReaderListener::on_data_available-python-api| replace:: :py:func:`on_data_available()<fastdds.DataReaderListener.on_data_available>` | ||
.. |DataReaderListener::on_subscription_matched-python-api| replace:: :py:func:`on_subscription_matched()<fastdds.DataReaderListener.on_subscription_matched>` | ||
|
||
.. |DataWriterListener-python-api| replace:: :py:class:`DataWriterListener<fastdds.DataWriterListener>` | ||
.. |DataWriterListener::on_publication_matched-python-api| replace:: :py:func:`on_publication_matched()<fastdds.DataWriterListener.on_publication_matched>` | ||
|
||
.. |DomainParticipantFactory-python-api| replace:: :py:class:`DomainParticipantFactory<fastdds.DomainParticipantFactory>` | ||
|
||
.. |MatchedStatus-python-api| replace:: :py:class:`MatchedStatus<fastdds.MatchedStatus>` | ||
|
||
.. |SampleInfo-python-api| replace:: :py:class:`SampleInfo<fastdds.SampleInfo>` |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ application. | |
|
||
definitions | ||
simple_app/simple_app | ||
simple_python_app/simple_python_app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.