pydrake.lcm

class pydrake.lcm.DrakeLcm

Bases: pydrake.lcm.DrakeLcmInterface

A wrapper around a real LCM instance.

See allow_network “DRAKE_ALLOW_NETWORK” for an environment variable option to disable LCM network traffic (i.e., only allowing memq:// URLs).

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pydrake.lcm.DrakeLcm) -> None

Constructs using LCM’s default URL (either the default hard-coded URL, or else LCM_DEFAULT_URL environment variable if it is set).

  1. __init__(self: pydrake.lcm.DrakeLcm, lcm_url: str) -> None

Constructs using the given URL. If empty, it will use the default URL as per the no-argument constructor.

  1. __init__(self: pydrake.lcm.DrakeLcm, params: pydrake.lcm.DrakeLcmParams) -> None

Constructs using the given parameters.

class pydrake.lcm.DrakeLcmInterface

A pure virtual interface that enables LCM to be mocked.

Because it must be pure, in general it will receive breaking API changes without notice. Users should not subclass this interface directly, but rather use one of the existing subclasses such as DrakeLcmBase instead.

Similarly, method arguments will receive breaking API changes without notice. Users should not call this interface directly, but rather use drake::lcm::Publish() or drake::lcm::Subscribe() instead.

__init__(*args, **kwargs)
get_lcm_url(self: pydrake.lcm.DrakeLcmInterface) str

Returns a URL describing the transport of this LCM interface.

When the URL refers to a transport offered by LCM itself (e.g., memq or udpm), then this function must return the conventional URL spelling. If the implementation of DrakeLcmInterface is using a non-standard back end, the result implementation-defined.

In either case, it is always formatted using URI syntax rules per the RFC(s).

HandleSubscriptions(self: pydrake.lcm.DrakeLcmInterface, timeout_millis: int) int

Invokes the HandlerFunction callbacks for all subscriptions’ pending messages. If timeout_millis is >0, blocks for up to that long until at least one message is handled.

Returns

the number of messages handled, or 0 on timeout.

Raises

RuntimeError when a subscribed handler throws.

Publish(self: pydrake.lcm.DrakeLcmInterface, channel: str, buffer: bytes, time_sec: Optional[float] = None) None

Most users should use the drake::lcm::Publish() free function, instead of this interface method.

Publishes an LCM message on channel channel.

Parameter channel:

The channel on which to publish the message. Must not be the empty string.

Parameter data:

A buffer containing the serialized bytes of the message to publish.

Parameter data_size:

The length of @data in bytes.

Parameter time_sec:

Time in seconds when the publish event occurred. If unknown, use nullopt or a default-constructed optional.

Subscribe(self: pydrake.lcm.DrakeLcmInterface, channel: str, handler: Callable[[bytes], None]) None

Most users should use the drake::lcm::Subscribe() free function or the drake::lcm::Subscriber wrapper class, instead of this interface method.

Subscribes to an LCM channel without automatic message decoding. The handler will be invoked when a message arrives on channel channel.

The handler should never throw an exception, because it is indirectly called from C functions.

Parameter channel:

The channel to subscribe to. Must not be the empty string. To use a regex, see SubscribeMultichannel().

Returns

the object used to manage the subscription if that is supported, or else nullptr if not supported. The unsubscribe-on-delete default is False. Refer to the DrakeSubscriptionInterface class overview for details.

SubscribeAllChannels(self: pydrake.lcm.DrakeLcmInterface, handler: Callable[[str, bytes], None]) None

Subscribe to all channels; this is useful for logging and redirecting LCM traffic without regard to its content.

SubscribeMultichannel(self: pydrake.lcm.DrakeLcmInterface, regex: str, handler: Callable[[str, bytes], None]) None

Subscribes to all channels whose name matches the given regular expression. The regex is treated as an anchored “match” not a “search”, i.e., it must match the entire channel name. The specific regular expression grammar is left unspecified, so it’s best to use only patterns that have identical semantics in all grammars, e.g., ".*".

class pydrake.lcm.DrakeLcmParams

The set of parameters for configuring DrakeLcm.

__init__(self: pydrake.lcm.DrakeLcmParams, **kwargs) None
property channel_suffix

The custom LCM channel name suffix for this DrakeLcm instance (optional).

When provided, calls to DrakeLcm::Publish() or DrakeLcm::Subscribe() will append this string to the channel name requested for publish or subscribe.

For example, with the channel_suffix set to “_ALT” a call to Publish(&drake_lcm, "FOO", message) will transmit on the network using the channel name “FOO_ALT”, and a call to Subscribe(&lcm, "BAR", handler) will only call the handler for messages received on the “BAR_ALT” channel name.

Simiarly, DrakeLcm::SubscribeMultichannel() and DrakeLcm::SubscribeAllChannels() only subscribe to network messages that end with the suffix. A network message on a non-matching channel name (e.g., “QUUX”) will silently discarded. The DrakeLcmInterface::MultichannelHandlerFunction callback will be passed the unadaorned channel name as its first argument (e.g., “FOO” or “BAR”), not “FOO_ALT”, etc.

property defer_initialization

(Advanced) Controls whether or not LCM’s background receive thread will be launched immediately during the constructor (when false) or deferred until the first time it’s needed (when true). This can be useful if the scheduling configuration for new threads varies between the construction time and first use.

property lcm_url

The URL for DrakeLcm communication. If empty, DrakeLcm will use the default URL per the DrakeLcm::DrakeLcm() no-argument constructor.

class pydrake.lcm.Subscriber(lcm, channel, lcm_type)

Bases: object

Subscribes to and stores a copy of the most recent message on a given channel, for some message type. This class does NOT provide any mutex behavior for multi-threaded locking; it should only be used in cases where the governing DrakeLcmInterface.HandleSubscriptions is called from the same thread that owns all copies of this object.

__init__(lcm, channel, lcm_type)

Creates a subscriber and subscribes to channel on lcm.

Parameters
  • lcm – LCM service instance (a pydrake.lcm.DrakeLcmInterface).

  • channel – LCM channel name.

  • lcm_type – Python class generated by lcmgen.

clear()