How to publish union values using luafile??

3 posts / 0 new
Last post
Last seen: 6 years 2 months ago
Joined: 10/16/2015
Posts: 3
How to publish union values using luafile??

I am not able to publish data using luafile for union.  Don't know how to set _d value for union.

Can anybody give me an example

gianpiero's picture
Last seen: 4 months 1 week ago
Joined: 06/02/2010
Posts: 176

Hello Sneha,

Let's assume your type is a union like this:


  <union name="AUnionType">
    <discriminator type="long"/>
      <caseDiscriminator value="0"/>
      <member type="long" name="aLong"/>
      <caseDiscriminator value="1"/>
      <member type="short" name="aShort"/>
      <caseDiscriminator value="2"/>
      <member optional="true" type="long" name="aLongOpt"/>

To write a union just set the field you want to and write:


local myWriter = CONTAINER.WRITER['MyPublisher::MyWriter']
-- The union has 3 cases:
-- * aLong
-- * aShort
-- * aLongOpt
-- If you want to set aShort just set that field by name, the
-- discriminator will be set automatically:
myWriter.instance['aShort'] = 3

To read it, first get the name of the selected field, then access it:


local myReader = CONTAINER.READER['MySubscriber::MyReader']
for i, sample in ipairs(myReader.samples) do
  if (not myReader.infos[i].valid_data) then
    print("\t invalid data!")
    -- If you are trying to retrieve a Union,
    -- first you have to find out wich field has been sent:
    local sentFieldName = sample['#']
    -- once you have the name you can just get the value like this:
    local value = sample[sentFieldName];
    print('I received ' .. value .. ' for ' .. sentFieldName)


I attached a zip file here with the example. To run it open to terminal and type:

rtiddsprototyper -cfgFile Union.xml -luaFile sendUnion.lua


rtiddsprototyper -cfgFile Union.xml -luaFile getUnion.lua

You can find more information in the Prototyper Getting Started Guide here.

Please let me know if this answers your questions!



Last seen: 6 years 2 months ago
Joined: 10/16/2015
Posts: 3

Thank you so much :)