Annotate ROS enum fields
ROS messages do not have built-in support for enums, but there are two ways Foxglove can treat constant values as enums: using a separate enum message, or with inline annotations.
Separate enum messages
Traditionally, ROS messages would declare enums by placing named constants in a message definition with a field of the same type. For example, a PrimaryColor
message might be defined as follows:
# In color_msgs/PrimaryColor.msg:
uint8 RED=1
uint8 YELLOW=2
uint8 BLUE=3
uint8 data
Then another message type could use the PrimaryColor
type for one of its fields:
# In color_msgs/Object.msg
color_msgs/PrimaryColor color
Foxglove will see that PrimaryColor
defines the constants for RED
, YELLOW
, and BLUE
adjacent to the data
field, and they all have the matching uint8
type, so it will display the named constants alongside the raw value. For example, in the Raw Messages and State Transitions panels:
Separate enum messages require extra indirection when accessing the value, such as .color.data
. Use inline annotations to avoid this issue.
Inline enum annotations
Foxglove also allows you to annotate enum fields without introducing a separate message for indirection.
To annotate an enum field, convert it to its primitive type (such as uint8
). Then, add another field named with a _foxglove_enum
suffix that uses the enum schema, on the line before the field definition:
# In color_msgs/Object.msg:
color_msgs/PrimaryColor color_foxglove_enum
uint8 color
Double underscores (__foxglove_enum
) are also supported, although they are only valid syntax in ROS 1.
You can now remove the data
field from the enum message, so it has no fields. In ROS 1, the annotation will have no impact on serialization since the enum message is now empty. In ROS 2, empty messages do introduce an extra padding byte in serialization.
# In color_msgs/PrimaryColor.msg:
uint8 RED=1
uint8 YELLOW=2
uint8 BLUE=3
Now, the color value is stored directly in .color
rather than .color.data
. Foxglove will recognize the annotation and display the PrimaryColor
constant names in the Raw Messages and State Transitions panels: