Dynamic reconfiguration in ROS

1. Create package

1
2
cd ~/catkin_ws/src
catkin_create_pkg rospy

2. cfg

1
2
3
4
cd ~/catkin_ws/src/dynamic_reconfig_test
mkdir config
cd config
gedit DynamicParam.cfg

write down as follows:

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env python
PACKAGE = "dynamic_reconfig_test"

from dynamic_reconfigure.parameter_generator_catkin import *

gen = ParameterGenerator()

# 'name', 'type', 'level', 'description', 'default', 'min', 'max
gen.add("test_parameter", int_t, 0, "A test parameter", 0, 0, 100)

# PACKAGE는 위에서 명시해 주었고, 두 번째 인자는 node명, 세 번째 인자는 생성되는 파일 앞에 붙는 접두어
exit(gen.generate(PACKAGE, "dynamic_reconfig_test", "DynamicParam"))

allow to write

1
chmod a+x config/DynamicParam.cfg

3. setup.py

1
2
cd ~/catkin_ws/src/dynamic_reconfig_test
gedit setup.py

write down as follows:

1
2
3
4
5
6
7
8
from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

d = generate_distutils_setup(
   packages=['dynamic_reconfig_test'],
   package_dir={'': 'src'})

setup(**d)

4. CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
cmake_minimum_required(VERSION 3.0.2)
project(dynamic_reconfig_test)

find_package(catkin REQUIRED COMPONENTS
  rospy
  dynamic_reconfigure
)

catkin_python_setup()

generate_dynamic_reconfigure_options(
  config/DynamicParam.cfg
)

catkin_package(
# INCLUDE_DIRS include
# LIBRARIES dynamic_reconfig_test
# CATKIN_DEPENDS rospy
# DEPENDS system_lib
)

include_directories(
  ${catkin_INCLUDE_DIRS}
)

install(
  PROGRAMS src/dynamic_reconfig_test/dynamic_reconfig_test.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
  )

install(DIRECTORY config DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION})

5. Node

1
2
cd ~/catkin_ws/src/dynamic_reconfig_test/src/dynamic_reconfig_test
gedit dynamic_reconfig_test

write down as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/python

import rospy
from dynamic_reconfigure.server import Server
from dynamic_reconfig_test.cfg import DynamicParamConfig

def callback(config, level):
    rospy.loginfo("Dynamic parameter: {}".format(config.test_parameter))
    return config

def main():
    rospy.init_node("dynamic_reconfig_test", anonymous=True)

    reconfigure_server = Server(DynamicParamConfig, callback=callback)

    rospy.loginfo("initialize the dynamic_reconfig_test node...")

    rospy.spin()

if __name__ == "__main__":
    main()

build

1
2
cd ~/catkin_ws
carkin_make

import error handling

import dynamic_reconfig_test 를 했을때, 라이브러리 경로 (~catkin_ws/devel/lib/python2.7/dist-packages/dynamic_reconfig_test/init.py) 를 읽어야하는데 node의 경로 (~catkin_ws/src/dynamic_reconfig_test/dynamic_reconfig_test.py)를 읽어서 빌드된 parameter 파일을 읽지못해서 발생하는 문제이다. 아래와 같이 라이브러리 경로를 직접 system path에 넣어주면 된다.

1
2
3
import sys
sys.path.append('/home/swyoo/usaywook/catkin_ws_for_vilab/devel/lib/python2.7/dist-packages/dynamic_reconfig_test')
from cfg import DynamicParamConfig

6. Execution

teminal 1

1
roscore

teminal 2

1
rosrun dynamic_reconfig_test dynamic_reconfig_test.py

terminal 3

1
rqt

Plugins -> Configuration -> Dynamic Reconfigure

result as follows.

7. using predefined values

how to set parameters with predefined values

modify cfg file as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python

PACKAGE = "dynamic_reconfig_test"

from dynamic_reconfigure.parameter_generator_catkin import *

gen = ParameterGenerator()

# gen.add("test_parameter", int_t, 0, "A test parameter", 0, 0, 100)
test_enum = gen.enum([
     gen.const("Low", int_t, 0, "A low value"),
     gen.const("Med", int_t, 50, "A medium vale"),
     gen.const("High", int_t, 100, "A high value")],
     "Test enum values")

gen.add("test_parameter", int_t, 0, "A test parameter", 0, 0, 100, edit_method=test_enum)

exit(gen.generate(PACKAGE, "dynamic_reconfig_test", "DynamicParam"))

build

1
2
cd ~/catkin_ws
carkin_make

execution

result as follows.

Reference

[c++패키지경우] https://enssionaut.com/board_robotics/1221

Tags:

Categories:

Updated:

Leave a comment