[Documentation] [TitleIndex] [WordIndex

Message generation

Like all ROS Client Libraries, rospy takes msg files and generates Python source code for them. The pattern for this is:

Similarly, srv files also have Python source code generated. The pattern for this is:

The source for the generated files are in the src directory of package_name.

Thus, to use the std_msgs/String message in your code you would use one of the following import statements:

import std_msgs.msg
msg = std_msgs.msg.String()

or

from std_msgs.msg import String
msg = String()

Message initialization

There are three ways to initialize a new Message instance: in-order arguments (*args), keyword arguments (**kwds), no arguments.

No arguments

In-order arguments (*args):

Keyword arguments (**kwds)

Each style has benefits and trade-offs. The keywords style is the recommended approach. It is resilient to many types of msg changes (new fields, field re-ordering) and is often more concise than other approaches. The in-order style is beneficial when you want your code to be brittle to msg changes, such as with regressions tests or other code that you wish to notice message changes. The no arguments style requires more lines of code but is useful if your message has embedded Message instances: after instantiation, these embedded messages will have been instantiated and you can do direct field assignments into them (e.g. setting the timestamp of a Header).


2024-11-16 17:44