ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to set attributes of complex messages

asked 2022-05-10 05:53:16 -0500

Hi all,

I'm trying to publish in a topic a complex message. For example, i have a message of type "Energy" that have the fields of another custom messages, as show bellow. I try using setattr but when i want to set, for example, the "power" attribute it gives me an error. Anyone has any idea?

Thanks for your help!


Message structure:

Energy:
    Header:
        stamp:
            sec (int32)
            nanosec (uint32)
        frame_id (string)
    Consumption:
        power (int32)
        Duration:
            min (int32)
            sec (int32)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-05-10 06:47:39 -0500

updated 2022-05-10 06:47:54 -0500

Assuming that you are using python. You hould be able to set your message with something similar to below;

....
energy = Energy()
energy.header.frame_id = "some_frame"
energy.consumption.power = 90
energy_msg_pub.publish(energy)
...
edit flag offensive delete link more

Comments

Thank you for the reply ! Is it possible do it in a dynamic way ? That is if i have diferente custom messages can i do it in a for cycle and set the atributes dynamically ?

Francisco_Santiago gravatar image Francisco_Santiago  ( 2022-05-10 08:03:05 -0500 )edit

I am not sure I understand what you mean by dynamic, at any point of time you can create and populate a ROS message programmatically

Fetullah Atas gravatar image Fetullah Atas  ( 2022-05-10 08:05:18 -0500 )edit

For example, i have 3 custom messages (Energy, Light, Pressure) that inside have diferent fields in each one. My question is if i can create a script that populate that messages reciving for example a dictionary. So that way i can create a messages of type any type without doing it "hard coded". I tried using "setattr" but when i want to set "powe" i cannot doing "setattr(Energy, "Consuption.power", 90)"

Francisco_Santiago gravatar image Francisco_Santiago  ( 2022-05-10 08:10:10 -0500 )edit

I am not familiar with setattr and from my experience it isn't commonly used to populate ROS messages.

Fetullah Atas gravatar image Fetullah Atas  ( 2022-05-10 08:51:52 -0500 )edit

Ok. I will try the way you said . Thank you so much for the help !

Francisco_Santiago gravatar image Francisco_Santiago  ( 2022-05-10 09:31:15 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-05-10 05:53:16 -0500

Seen: 129 times

Last updated: May 10 '22