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

Create custom message with min and max value for a specific field

asked 2022-04-23 06:42:26 -0500

Michdo93 gravatar image


maybe a very bad question but I haven't seen a solution anywhere. I want to create a range for an int8 from where to where the numbers I transfer are allowed to go.

I tried it with an enumeration:

# Percentage value for dimmers

# Possible states for Dimmer
int8 0=0
int8 1=1
int8 2=2
int8 3=3
int8 4=4
int8 5=5
int8 6=6
int8 7=7
int8 8=8
int8 9=9
int8 10=10

int8 state # Dimmer state enumerated above
Header header
string item # the name of the item

I am sure that this is not the right solution. I get the following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ubuntu/catkin_ws/devel/lib/python3/dist-packages/openhab_msgs/msg/", line 8, in <module>
    from ._DimmerState import *
  File "/home/ubuntu/catkin_ws/devel/lib/python3/dist-packages/openhab_msgs/msg/", line 141
    0 = 0

I know that enumerations don't make sense with numbers, of course. I want to use only the numbers from 0 to 100 at the end. In the example above there was only a section from 0 to 10.

Does anyone have an idea? Thanks in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-04-23 07:00:19 -0500

ljaniec gravatar image

updated 2022-04-23 12:16:46 -0500

I think you can use constants in ROS messages:

They don't map explicitly to higher-level enum types in programming languages, but they generally let you deal with the same issues. E.g.

string eStop            # Enum {autoAck, manual, remote, none} Acknowledge-Type of eStop: 
                        # autoAck: autoacknowledgeable e-stop is activated e.g. by bumper or protective field
                        # manual: e-stop has to be acknowledged manually at the vehicle 
                        # remote: facility estop has to be acknowledged remotely 
                        # none: no e-stop activated
bool fieldViolation     # Protective field violation. True: field is violated False: field is not violated 

# Enums for eStop
string AUTO_ACK=autoAck
string MANUAL=manual
string REMOTE=remote
string NONE=none

In your case, there is also a naming error - fields cannot just have numbers as their name:

Field names must be translated by message generators to several target languages, so we restrict field names to be an alphabetical character followed by any mixture of alphanumeric and underscores, i.e. [a-zA-Z][a-zA-Z1-9_]*. It is recommended that you avoid using field names that correspond to keywords in common languages -- although those names are legal, they create confusion as to how a field name is translated.

from 2.1.2 there:

I want to use only the numbers from 0 to 100 at the end. In the example above there was only a section from 0 to 10.

If you want to filter published messages by a range of accepted values, this should be done on the publisher/subscriber side, e.g. in the callback. AFAIK there is no filtering mechanism for this in ROS1 by message definition (and even in ROS2 it's only for the size of a bounded dynamic array: a bounded dynamic array is described by the suffix [<=N] where N is the maximum size of the array, not its values).

edit flag offensive delete link more


I changed it to int8 PERCENTAGE0=0 as example.

Michdo93 gravatar image Michdo93  ( 2022-04-23 07:18:14 -0500 )edit

@Michdo93 As @ljaniec said, ros has no mechanism to impose the limits you want on an int8. All nodes which receive this message should range check the state value to determine if it is valid.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2022-04-23 10:12:42 -0500 )edit

Question Tools


Asked: 2022-04-23 06:42:26 -0500

Seen: 416 times

Last updated: Apr 23 '22