Robotics StackExchange | Archived questions

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

Hi,

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/__init__.py", line 8, in <module>
    from ._DimmerState import *
  File "/home/ubuntu/catkin_ws/devel/lib/python3/dist-packages/openhab_msgs/msg/_DimmerState.py", 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.

Asked by Michdo93 on 2022-04-23 06:42:26 UTC

Comments

Answers

I think you can use constants in ROS messages:

http://www.ros.org/wiki/msg#Constants

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: https://wiki.ros.org/msg

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).

Asked by ljaniec on 2022-04-23 07:00:19 UTC

Comments

I changed it to int8 PERCENTAGE0=0 as example.

Asked by Michdo93 on 2022-04-23 07:18:14 UTC

@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.

Asked by Mike Scheutzow on 2022-04-23 10:12:42 UTC