読者です 読者をやめる 読者になる 読者になる

空飛ぶロボットのつくりかた

ロボットをつくるために必要な技術をまとめます。ロボットの未来についても考えたりします。

パラチュン用dynamic_reconfigureの使い方

dynamic_reconfigureの使い方

  • Tutorial.cfgファイルの作成
#!/usr/bin/env python
PACKAGE = "dynamic_tutorials" # package name

from dynamic_reconfigure.parameter_generator_catkin import *

gen = ParameterGenerator()

gen.add("int_param", int_t, 0, "An Integer parameter", 50, 0, 100) # name , type , level ,description , default ,min ,max
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")) # 2:package_name 3:cfg_file_name
  • 実行可能な状態に
chmod a+x cfg/Tutorials.cfg
  • CmakeLists.txtに以下を追加
#add dynamic reconfigure api
find_package(catkin REQUIRED dynamic_reconfigure)
generate_dynamic_reconfigure_options(relative_path_to_file1 relative_path_to_file2 ...)
# make sure configure headers are build before node using them
add_dependencies(example_node dynamic_tutorials_gencfg) 
  • package.xmlにもgenerate_dynamic_reconfigureを追記

nodeの作り方

python

#!/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("dynamic_tutorials", anonymous = True)

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

参考

# ディクショナリで指定する場合はアンパックすればよい
>>> d = {"name": "John", "age": 20}
>>> "My name is {name}. I'm {age} years old.".format(**d) #アンパックしてキーワード引数に
"My name is John. I'm 20 years old."
  • configは辞書?
dict = {"yamada":75, "endou":82}
value =  dict["yamada"]

C++

#include <ros/ros.h>

#include <dynamic_reconfigure/server.h>
#include <dynamic_tutorials/TutorialsConfig.h>

void callback(dynamic_tutorials::TutorialsConfig &config, uint32_t level) {
  ROS_INFO("Reconfigure Request: %d %f %s %s %d", 
            config.int_param, config.double_param, 
            config.str_param.c_str(), 
            config.bool_param?"True":"False", 
            config.size);
}

int main(int argc, char **argv) {
  ros::init(argc, argv, "dynamic_tutorials");

  dynamic_reconfigure::Server<dynamic_tutorials::TutorialsConfig> server;
  dynamic_reconfigure::Server<dynamic_tutorials::TutorialsConfig>::CallbackType f;

  f = boost::bind(&callback, _1, _2);
  server.setCallback(f);

  ROS_INFO("Spinning node");
  ros::spin();
  return 0;
}
  • 実行
$ rosrun rqt_reconfigure rqt_reconfigure

参考

ja/dynamic_reconfigure/Tutorials/HowToWriteYourFirstCfgFile - ROS Wiki

qiita.com