[ROS Q&A] 150 – How to publish data changed in Dynamic Reconfigure

[ROS Q&A] 150 - How to publish data changed in Dynamic Reconfigure

Written by Marco Arruda

27/08/2018

 

This video shows how to use dynamic reconfigure client to change or receive updates about dynamic parameter value updates inside your node. Using a dynamic reconfigure client allows you to interact with these parameters outside the server node.

Step 0. Create a project in ROS Development Studio(ROSDS)

ROSDS helps you follow our tutorial in a fast pace without dealing without setting up an environment locally. If you haven’t had an account yet, you can create a free account here. Let’s create a new project and call it dynamic_reconfigure.

Step 1. Create the package

We’ll put our code in a new package called mypkg

cd ~/catkin_ws/src
catkin_create_pkg mypkg rospy
cd mypkg
mkdir cfg
mkdir scripts

We’ll create a Tutorials.cfg file under the cfg folder with the following content.

#!/usr/bin/env python
PACKAGE = "dynamic_tutorials"

from dynamic_reconfigure.parameter_generator_catkin import *

gen = ParameterGenerator()

gen.add("int_param",    int_t,    0, "An Integer parameter", 50,  0, 100)
gen.add("double_param", double_t, 0, "A double parameter",    .5, 0,   1)
gen.add("str_param",    str_t,    0, "A string parameter",  "Hello World")
gen.add("bool_param",   bool_t,   0, "A Boolean parameter",  True)

size_enum = gen.enum([ gen.const("Small",      int_t, 0, "A small constant"),
                       gen.const("Medium",     int_t, 1, "A medium constant"),
                       gen.const("Large",      int_t, 2, "A large constant"),
                       gen.const("ExtraLarge", int_t, 3, "An extra large constant")],
                     "An enum to set size")

gen.add("size", int_t, 0, "A size parameter which is edited via an enum", 1, 0, 3, edit_method=size_enum)

exit(gen.generate(PACKAGE, "dynamic_tutorials", "Tutorials"))

We also need a server.py file under the scripts folder to publish these parameters.

#!/usr/bin/env python

import rospy

from dynamic_reconfigure.server import Server
from dynamic_tutorials.cfg import TutorialsConfig

def callback(config, level):
    rospy.loginfo("""Reconfigure Request: {int_param}, {double_param},\ 
          {str_param}, {bool_param}, {size}""".format(**config))
    return config

if __name__ == "__main__":
    rospy.init_node("mypkg", anonymous = True)

    srv = Server(TutorialsConfig, callback)
    rospy.spin()

Step 2. rqt_reconfigure tool

Then we can launch the rqt_reconfigure tool from another shell

rosrun rqt_reconfigure rqt_reconfigure

Go to Tools->Graphical tool to open the GUI.

Since the parameters have not been published yet, you’ll see nothing on it.

Let’s run the server

rosrun mypkg server.py

You’ll see the parameters are in the gui now.

Step 3. Client

If you want to get the updated value from the tool, you’ll need a client. Let’s create a client.py under the scripts folder.

#!/usr/bin/env python

import rospy

import dynamic_reconfigure.client

def callback(config):
    rospy.loginfo("Config set to {int_param}, {double_param}, {str_param}, {bool_param}, {size}".format(**config))

if __name__ == "__main__":
    rospy.init_node("dynamic_client")

    client = dynamic_reconfigure.client.Client("mypkg", timeout=30, config_callback=callback)

    r = rospy.Rate(0.1)
    x = 0
    b = False
    while not rospy.is_shutdown():
        x = x+1
        if x>10:
            x=0
        b = not b
        #client.update_configuration({"int_param":x, "double_param":(1/(x+1)), "str_param":str(rospy.get_rostime()), "bool_param":b, "size":1})
        r.sleep()

After giving the file permission, you can run it with

rosrun mypkg client.py

You should receive the parameter now. If you want to update the parameters with the client. Please uncomment the client.update_configuration line in the script.

Want to learn more?

If you are interested in learning ROS, please check our ROS Basic in 5 Days course.

 

Edit by: Tony Huang


RELATED LINKS

Robot Ignite Academy
ROS Development Studio (ROSDS)
Original question

We Love Feedback!

Did you like this video? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.

Topics:
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

1 Comment

  1. zbt

    hello, I’m having a problem while running “rosrun rqt_reconfigure rqt_reconfigure”. When I run in it in the terminal it says “Segmentation fault (core dumped)”. I don’t know what is causing the problem. I need your help. help me, please!

    Reply

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This