The Lua LCM API
The Lua API wraps the LCM C API, and is meant to mirror its functionality and organization. The bulk of the Lua API is represented by the LCM userdata, which basically wraps lcm_t and related functions.
LCM Userdata
The LCM userdata manages an internal lcm_t and any number of subscriptions.
Initializer
Methods
new
Parameters
provider
Optional. The LCM provider.
Return Values
The new LCM userdata.
Description
This is the userdata initializer; it creates a new userdata.
Example Code
local lcm = require('lcm')
local lc = lcm.lcm.new()
local lc2 = lcm.lcm.new('udpm://239.255.76.67:7667?ttl=1')
publish
Parameters
channel
The channel to publish to.message
The encoded message to send.
Return Values
(None.)
Description
This method publishes a message to a channel. If the message cannot be published, an error is raised.
Example Code
local lcm = require('lcm')
local msg_t = require('msg_t') -- or any other message type
local lc = lcm.lcm.new()
local msg = msg_t:new()
local encoded_msg = msg:encode()
lc:publish('somechannel', encoded_msg)
subscribe
Parameters
channel
The channel to subscribe to.handler
The callback.
Return Values
The subscription reference.
Description
This method creates a subscription to a channel. There may be multiple subscriptions per channel. Creating a subscription involves registering a callback, which is invoked once per received message on the specified channel. The callback is invoked during calls to handle or timedhandle.
Notice that this function does not return an actual subscription, but a reference to one. This function returns an integer which is used to index an internal table of subscriptions. The lifetime of the internal subscription is not dependent on the reference, so subscriptions cannot be garbage collected. Subscriptions can only be removed by being unsubscribed.
Example Code
local lcm = require('lcm')
local msg_t = require('msg_t') -- or any other message type
local lc = lcm.lcm.new()
local function handler(channel, encoded_msg)
local msg = msg_t.decode(encoded_msg)
-- ...
end
local sub = lc:subscribe('somechannel', handler)
unsubscribe
Parameters
sub
The subscription reference to unsubscribe.
Return Values
(None.)
Description
The method removes a subscription created by subscribe. Also note that all subscriptions are automatically unsubscribed when the LCM userdata is garbage collected.
Example Code
local lcm = require('lcm')
local msg_t = require('msg_t') -- or any other message type
local lc = lcm.lcm.new()
local function handler(channel, encoded_msg)
local msg = msg_t.decode(encoded_msg)
-- ...
end
local sub = lc:subscribe('somechannel', handler)
--- ...
lc:unsubscribe(sub)
handle
Parameters
(None.)
Return Values
(None.)
Description
Waits for an incomming message, and dispatches handler callbacks as necessary. This method will block indefinitely until a message is received. When a message is received, all of the handler callbacks for the message’s channel are invoked, in the same order they were subscribed.
Example Code
local lcm = require('lcm')
local lc = lcm.lcm.new()
lc:handle()
handle_timeout
Parameters
time
The time to block, in milliseconds.
Return Values
A boolean:
true
if a message was received and handled,false
otherwise.
Description
This method is like the normal handle except it only blocks for a specified amount of time.
Example Code
local lcm = require('lcm')
local lc = lcm.lcm.new()
local ok = lc:handle_timeout(500)
if not ok then
print('timed out!')
end
timedhandle (Deprecated)
This function is deprecated! Please use handle_timeout instead!
Parameters
time
The time to block, in seconds.
Return Values
A boolean:
true
if a message was received and handled,false
otherwise.
Description
This method is like the normal handle except it only blocks for a specified amount of time.
Example Code
local lcm = require('lcm')
local lc = lcm.lcm.new()
local ok = lc:timedhandle(0.5)
if not ok then
print('timed out!')
end