pyDDS (a Python 3 wrapper for RTI DDS.)
Overview
To use this wrapper, you must compile your generated topics (the output of
rtiddsgen
) into a shared library. This will be a .so
on Linux, .DLL
on
windows and .dylib
on OS X. The location of this library and the RTI libraries
nddsc
and nddscore
must be in the library search path (LD_LIBRARY_PATH
on
Linux, PATH
on windows, and DYLD_LIBRARY_PATH
on OS X).
Use
Lets say you have a topic that looks like:
#ifndef MY_DDS
#define MY_DDS
module my {
module dds {
enum my_enum {
mode_1,
mode_2,
mode_3
};
struct my_custom_topic
{
string name; //@key
string value;
my_enum mode;
};
};
};
#endif // MY_DDS
Once this topic is generated (rtiddsgen), compiled, and packaged as a shared library, you can use it with this library.
####Subscribe:####
Lets say you just want to print out a representation of the topic data. Lets
also say that your topic library is called libmy_topics.so
and the location
of this library is in LD_LIBRARY_PATH
. To subscribe:
import dds
import json
import time
def print_repr(data):
print json.dumps(data, indent=4)
dds_instance = dds.DDS('my_topics')
topic = dds_instance.get_topic('my.dds.my_custom_topic')
topic.subscribe(print_repr)
while True:
time.sleep(100)
That's all there is to basic topic subscription! All data samples are delivered to the callback as a python dictionary. Also of note: the callback will occur in a separate thread, so you must take that into consideration for any non thread-safe operations.
If desired, you can also specify a few other options:
- instance revoked A publisher can revoke a topic instance. To be notified
of these events, specify a callback with the keyword argument
instance_revoked_cb
- liveliness lost If a publisher goes down, or the network connection
fails, DDS can notify subscribers that a publisher seems to have gone down.
To be notified of this condition, specify the keyword argument
liveliness_lost_cb
- content filtering Sometimes you are only interested in topic data that
matches some condition. Instead of examining all the topic data samples and
throwing out the ones you don't want, you can tell DDS to only send you
samples that match a specified filter. For example, say you only want samples
that have
mode == mode_2
. This can be accomplished by specifying the keyword argumentfilter_expression="mode MATCH 'mode_3'"
. See the docs for more details.
Subscriptions can also be canceled by calling topic.unsubscribe()
####Publish:####
To publish a data sample, you simply construct a python dictionary that matches the structure of the topic. e.g.:
import dds
dds_instance = dds.DDS('my_topics')
topic = dds_instance.get_topic('my.dds.my_custom_topic')
sample = {
'name': 'my key name',
'value': 'whatever value you want',
'mode': 'mode_2'
}
topic.publish(sample)
The dictionary does not have to be complete. i.e. it can be sparsely specified.
This:
sample = {
'name': 'my key name',
'mode': 'mode_2'
}
is a valid topic sample that can be successfully published. If topic fields are not specified, they will be filled in with default values depending on the type. Strings will be populated with the empty string, number types will get zero, enums will get the first enum value, etc.
A publisher can also 'revoke' a topic. If a topic has keyed fields (the
// @key
decoration in the IDL) then there can be multiple instances of the
topic on the DDS but simultaneously. To revoke a particular instance, call
topic.dispose(sample)
where sample has the keyed fields specified to match the
topic instance you wish to revoke.
For more detailed documentation, see the inline docs in dds.py