How can I use the 'level' in dynamic_reconfigure [closed]

asked 2021-12-27 05:58:05 -0500

Laura gravatar image

The callback gives me two params, the config and the uint32_t level, I guess that the level reflect which params has been changed, but how can I use the number search the Corresponding parameters in the config using C++, except writing codes likeswitch(level) { case 1:... } .Does there any method like using config.param_level? (Maybe what I described is not very clear, I hope you can understand what I mean)

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Laura
close date 2021-12-29 06:57:40.785465

Comments

Hi @Laura, interesting question. After digging a little, I found this prior answer and discussion: https://answers.ros.org/question/2101...

The level parameter is a bit mask, so one way to find what values are set to 1, you could use and bitwise operations instead.

osilva gravatar image osilva  ( 2021-12-27 10:27:38 -0500 )edit

@osilva Thank you very much for your answer. I also saw the previous post before this question. However, I don't quite understand the explanation of "level is a bit mask". Could you please explain it in detail? When I created the .cfg file, I use code like gen.add(name, level, default, min, max), for example I set level=11, and i found the callback gives me the level=11 when I changed the corresponding parameter, but how to consider the level from the view "level is a bit mask"?

Laura gravatar image Laura  ( 2021-12-27 11:03:03 -0500 )edit

A bit mask is basically a binary representation using 1s and 0s as switch values like ON and OFF. So for example if you have a byte(8bits) 0000 0000, you will say all your 8 bits of the byte are set to 0 or OFF. If you perform and OR operation between bytes, only those bits that are different will set to 1, thus is a quick way to find out what has changed instead of checking each parameter. For more information on bitwise operations: https://www.clivemaxfield.com/masking...

osilva gravatar image osilva  ( 2021-12-27 11:12:15 -0500 )edit

Below an explanation from

For example if i have 3 parameters, level value can be set to "0,1,2" for the 3 parameters in the cfg file.so the 0,1,2 bits of variable "level" will be set depending on what parameter has been changed in rqt reconfigure. If parameter1 has been changed, the corresponding bit in the "level" will be set to 1. This variable "level" is passed in the reconfigure callback. So we know which variable is changed, and update accordingly, instead of updating all of them.

https://answers.ros.org/answers/34131...

osilva gravatar image osilva  ( 2021-12-27 11:13:53 -0500 )edit

Thank you very much, @osilva, but I found ,for example, using code like gen.add("a", level=4, default, min, max), when I changed parameter "a" in rqt_reconfigure, I expected the callback gives me level=0x00000010 (Hexadecimal), which is equal to 16 (Decimal), because I think it will changed the 4th bit in the level (total 32 bits), but actually it gives me the level = 4 (Decimal).Did the system automatically help me perform the bit operation process?

Laura gravatar image Laura  ( 2021-12-27 21:51:30 -0500 )edit

I think is even more basic when you take a look at the source code:

  def _calc_level(self, config1, config2):
        level = 0
        for param in extract_params(self.type.config_description):
            if config1[param['name']] != config2[param['name']]:
                level |= param['level']

        return level

https://github.com/ros/dynamic_reconf...

So yes it’s parsing and returning the level value

osilva gravatar image osilva  ( 2021-12-28 05:11:19 -0500 )edit

Thank you for trying to fully understand. I believe it will be good for you to document as an answer as prior answers does not go to this level of detail

osilva gravatar image osilva  ( 2021-12-28 05:13:42 -0500 )edit

@osilva,Thank you very much for your patient answers, by viewing the source code I now fully understand how it works. Thank you again for taking such a long time to answer my confusion!

Laura gravatar image Laura  ( 2021-12-28 05:44:24 -0500 )edit