## For instruction on writing tutorials ## http://www.ros.org/wiki/WritingTutorials #################################### ##FILL ME IN #################################### ## for a custom note with links: ## note = ## for the canned note of "This tutorial assumes that you have completed the previous tutorials:" just add the links ## note.0= [[dynamic_reconfigure/Tutorials/HowToWriteYourFirstCfgFile|How to Write Your First cfg File]] ## descriptive title for the tutorial ## title = Setting up Dynamic Reconfigure for a Node(cpp) ## multi-line description to be displayed in search ## description = How to make a node dynamically reconfigureable in C++. ## the next tutorial description (optional) ## next = ## links to next tutorial (optional) ## next.0.link= [[dynamic_reconfigure/Tutorials/UsingTheDynamicReconfigurePythonClient| Using the Dynamic Reconfigure Python Client]] ## next.1.link= ## what level user is this tutorial for ## level=IntermediateCategory ## keywords = #################################### <> <> = The Code = Since we have already created a cfg file in [[dynamic_reconfigure/Tutorials/HowToWriteYourFirstCfgFile|the last tutorial]] we'll dive right in to the code. Go back to our dynamic_tutorials package, create a new directory called nodes, and create a file called server.cpp. Drop the following code into server.cpp: {{{#!cplusplus #include #include #include 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 server; dynamic_reconfigure::Server::CallbackType f; f = boost::bind(&callback, _1, _2); server.setCallback(f); ROS_INFO("Spinning node"); ros::spin(); return 0; } }}} = The Breakdown = Let take a closer look at server.cpp. {{{#!cplusplus #include #include #include }}} Here we just include the necessary header files for our node. Take note of {{{dynamic_tutorials/TutorialConfig.h}}} this is the header file generated by dynamic_reconfigure from our config file. {{{#!cplusplus 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); } }}} This is the callback that will get called when the dynamic_reconfigure server is sent a new configuration. It takes two parameters, the first is the new config. The second is the level, which is the result of ORing together all of level values of the parameters that have changed. What you want to do with the level value is entirely up to you, for now it is unnecessary. All we are going to do in the callback is print out the configuration. {{{#!cplusplus int main(int argc, char **argv) { ros::init(argc, argv, "dynamic_tutorials"); dynamic_reconfigure::Server server; }}} In our main function we simply initialize our node and define the dynamic_reconfigure server, passing it our config type. As long as the server lives (in this case until the end of main()), the node listens to reconfigure requests. {{{#!cplusplus dynamic_reconfigure::Server::CallbackType f; f = boost::bind(&callback, _1, _2); server.setCallback(f); }}} Next we define a variable to represent our callback and then send it to the server. Now when the server gets a reconfiguration request it will call our callback function. '''NOTE:'''If the callback if a member function of class use `f = boost::bind(&callback, x, _1, _2)` instead, where `x` is an instance of the class (or `this` if called from inside the class). {{{#!cplusplus ROS_INFO("Spinning node"); ros::spin(); return 0; } }}} Lastly we spin the node. = Run It! = Compile the node(Don't forget to add the executable in {{{CMakeLists.txt}}}!). Rosrun it and the launch the [[rqt_reconfigure]] (or [[reconfigure_gui]] if you're on `fuerte` or earlier) using: {{{ $ rosrun rqt_reconfigure rqt_reconfigure $ rosrun dynamic_reconfigure reconfigure_gui ## On Fuerte or earlier }}} You should see a window that looks something like this pop up. {{attachment:Screenshot-Reconfigure.png}} And you're done! You've just built your first dynamically reconfigure-able node. ## AUTOGENERATED DO NOT DELETE ## TutorialCategory ## DynamicReconfigureCategory ## FILL IN THE STACK TUTORIAL CATEGORY HERE