YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
An example which shows how to use a Lua script to modify incoming data in an input port
+ Collaboration diagram for An example which shows how to use a Lua script to modify incoming data in an input port:

Description

This example demonstrates how to simply use the port monitor carrier to modify data going through a connection. The port '/write' from 'yarp write' module is connected to the '/read' port of 'yarp read' using a portmonitor plugged into the receiver side. The portmoniotr loads a Lua script ('bot_modifier.lua') in which we access and modify the data going through through the port.

Requirements

Running the example

Now if you write something in the 'sender' terminal, you will see the text "modified from Lua" will be added to the original message. For example:

[sender terminal]
 Hello
[receiver terminal]
 Hello "modified from Lua"

As it is constrained in this PortMonitor.accept() method, If you type "ignore", the word will never be delivered to the input port.

Scripts

bot_modifier.lua

-- loading lua-yarp binding library
require("yarp")
--
-- accept is called when the port receives new data
-- @param thing The Things abstract data type
-- @return Boolean
-- if false is returned, the data will be ignored
-- and update() will never be called
PortMonitor.accept = function(thing)
if thing:asBottle():toString() == "ignore" then
return false
end
return true
end
--
-- update is called when the port receives new data
-- @param thing The Things abstract data type
-- @return Things
PortMonitor.update = function(thing)
bt = thing:asBottle()
bt:addString("modified from Lua :)")
return thing
end