Yet Another Robot Platform
This example demonstrates how to use the port the portmonitor object at both side of a connection to encode and decode the data. The port '/write' from 'yarp write' module is connected to the '/read' port of 'yarp read' using two portmonitors plugged into the sender and receiver side. Both portmonitors load the same Lua script (
'codec.lua') which can act as data coder or decoder depending on the which side of the connection it is attached.
$ cd $YARP_ROOT/example/portmonitor/coder_decoder $ yarp write /write
$ cd $YARP_ROOT/example/portmonitor/coder_decoder $ yarp read /read
$ yarp connect /write /read tcp+send.portmonitor+type.lua+file.codec
Now if you write something in the 'sender' terminal, you will see the original text is encoded with a simple base64 encoder and transmitted to the receiver. For example:
[sender terminal] Hello [receiver terminal] "SGVsbG8="
Now try to plug the `codec.lua' to the receiver side too.
$ yarp connect /write /read tcp+send.portmonitor+file.codec+recv.portmonitor+file.codec
You will see the data gets decoded and the original text will be shown in the receiver terminal:
[sender terminal] Hello [receiver terminal] "Hello"
Notice that codec.lua acts as coder or decoder depending to which side of the connection it is attached. This is checked in the
\section coder_decoder_scripts Scripts \subsection coder_decoder_codec codec.lua
-- loading lua-yarp binding library require("yarp") -- -- create is called when the port monitor is created -- @return Boolean -- PortMonitor.create = function(options) isCoder = (options:find("sender_side"):asInt() == 1) if isCoder == true then print("codec.lua: I will encode whatever i get!") else print("codec.lua: I will decode whatever i get!") end return true; end -- -- 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() == nil then print("codec.lua: got wrong data type (expected type Bottle)") 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() data = bt:toString() bt:clear() if isCoder == true then bt:fromString(enc(data)) else bt:fromString(dec(data)) end return thing end