How to publish union values using luafile??

3 posts / 0 new
Last post
Offline
Last seen: 8 years 5 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
Offline
Last seen: 2 months 1 week ago
Joined: 06/02/2010
Posts: 177

Hello Sneha,

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

Union.xml

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

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

sendUnion.lua

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
myWriter:write();

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

getUnion.lua:

local myReader = CONTAINER.READER['MySubscriber::MyReader']
myReader:take()
for i, sample in ipairs(myReader.samples) do
  if (not myReader.infos[i].valid_data) then
    print("\t invalid data!")
  else
    -- 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)
  end
end

 

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

rtiddsprototyper -cfgFile Union.xml -luaFile sendUnion.lua

and:

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!

  Gianpiero

v

Offline
Last seen: 8 years 5 months ago
Joined: 10/16/2015
Posts: 3

Thank you so much :)