Making a configuration file
Source codeThese files tell how the GUI window must be made, what all it must consist of and options for all the things that it must consist of. This file has a ".cfg" extension, is stored in a directory named "cfg" inside the package and is programmed using Python (not C++). Here's how you go about making a configuration file:
- Create a folder named cfg inside the package if there isn't one yet. Use the following commands on a terminal:
roscd intro_pkg1
mkdir cfg
cd cfg
- Create a file named Sample.cfg in the cfg folder.
- You can pick any name. We'll see later how to program it.
- Make the ".cfg" file executable. Run the following on the terminal that we just opened:
chmod +x Sample.cfg
cd ../
- Open the CMakeLists.txt file of the package and make the following amendments:
- Add dynamic_reconfigure to the list inside function find_packages.
- Scroll down to "Declare ROS dynamic reconfigure parameters" section and uncomment the function "generate_dynamic_reconfigure_options" and add the line "cfg/Sample.cfg" inside it (basically give the path and fullname of the configuration file).
- Open the package.xml file of the package and add the following lines at appropriate places:
- <build_depend>dynamic_reconfigure</build_depend>
- <build_export_depend>dynamic_reconfigure</build_export_depend>
- <exec_depend>dynamic_reconfigure</exec_depend>
- Write the following in the file "Sample.cfg". There's a sample on GitHub, here. Here's how you code a configuration file:
- Since it's written in python, write the first line that every ROS python script has ("#!/usr/bin/env python").
- Create a variable named PACKAGE and assign it the package name. This is required for the header file that you're going to import in the next step.
- Import all files from dynamic_reconfigure.parameter_generator_catkin. These contain code for parameter generators, they can be found locally on your system at the path "/opt/ros/melodic/lib/python2.7/dist-packages/dynamic_reconfigure".
- Create a ParameterGenerator object
- Use the add member function to add parameters. Pass it the following arguments:
- name: Name of the parameter to be displayed. Do not use reserved keywords here, though it's a string.
- paramtype: The data type of the parameter, this is either int_t, double_t, str_t, or bool_t
- level: A bitmask which will later be passed to the dynamic reconfigure callback. When the callback is called all of the level values for parameters that have been changed are ORed together and the resulting value is passed to the callback. We keep it 0 for most uses.
- description string: A description of the parameter
- default value of the parameter
- min and max values of the parameter. By default they're none and should be none for String and Bool parameters.
- edit_method is the way we want to edit the parameter. List (dropdown) or standard. Don't assign it if not a dropdown.
- To add dropdown menus, use the enum member function. Pass it the following arguments:
- List of constants defined using const member function. Pass every entry the following parameters:
- Description message for the entire list
- After the enumerated list is created, use the add member function to add the list. Assign the list created to the edit method.
- End the config file using the generate function. Pass it the following parameters:
- pkgname: Package name
- nodename: Node name of the configuration file
- name: Name prefix to generate headers. It's suffixed with "Config" and a ".h" for C++ and ".py" for Python
Source codeCMakeLists.txt filepackage.xml fileOfficial pageBuilding the configuration file
- Open a terminal
- Build the package. Run the following commands:
cd ~/ROS_workspaces/ros_ws/
catkin_make
After the entire process has finished, you'll observe that the following files have been created:
- ros_ws/devel/include/intro_pkg1/SampleConfig.h
- ros_ws/devel/lib/python2.7/dist-packages/intro_pkg1/cfg/SampleConfig.py
These are header file used by nodes that want to use these particular dynamic reconfiguration parameters.