Skip to content

Commit 8ad8d35

Browse files
author
Claudio Bandera
committed
Added demo package for rosparam_handler. Closes cbandera/rosparam_handler#5
1 parent 3a3b8b4 commit 8ad8d35

12 files changed

+255
-0
lines changed

CMakeLists.txt

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
cmake_minimum_required(VERSION 2.8.3)
2+
project(rosparam_handler_tutorial)
3+
find_package(catkin REQUIRED COMPONENTS roscpp rosparam_handler dynamic_reconfigure )
4+
5+
# set compiler flags
6+
include(CheckCXXCompilerFlag)
7+
CHECK_CXX_COMPILER_FLAG("-std=c++14" Cpp14CompilerFlag)
8+
if (${Cpp14CompilerFlag})
9+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
10+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "17")
11+
#no additional flag is required
12+
else()
13+
message(FATAL_ERROR "Compiler does not have c++14 support. Use at least g++4.9 or Visual Studio 2013 and newer.")
14+
endif()
15+
16+
# Generate parameter files
17+
generate_ros_parameter_files(cfg/Demo.params)
18+
19+
# Create package
20+
catkin_package()
21+
22+
# Node
23+
add_executable(demo src/demo/demo.cpp src/demo/demo_node.cpp)
24+
add_dependencies(demo ${catkin_EXPORTED_TARGETS} rosparam_handler_tutorial_gencfg)
25+
target_link_libraries(demo ${catkin_LIBRARIES})
26+
install(TARGETS demo
27+
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
28+
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
29+
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
30+
)
31+
32+
# Nodelet
33+
add_library(demo_nodelet src/demo/demo.cpp src/demo/demo_nodelet.cpp)
34+
target_link_libraries(demo_nodelet ${catkin_LIBRARIES})
35+
install(TARGETS demo_nodelet LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION})

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# rosparam_handler_tutorial
2+
3+
This holds the code for the [rosparam_handler](https://github.com/cbandera/rosparam_handler)-Tutorials.
4+
Compile the code, source your workspace and run
5+
6+
```shell
7+
roslaunch rosparam_handler_tutorial demo_node.launch
8+
```
9+
or run it as a nodelet
10+
11+
```shell
12+
roslaunch rosparam_handler_tutorial demo_nodelet.launch
13+
```

cfg/Demo.params

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python
2+
from rosparam_handler.parameter_generator_catkin import *
3+
gen = ParameterGenerator()
4+
5+
gen.add("rate", paramtype="int",description="Rate for timer", default=2, min=1, max=10, configurable=True)
6+
7+
# Parameters with different types
8+
gen.add("int_param", paramtype="int", description="An Integer parameter")
9+
gen.add("double_param", paramtype="double",description="A double parameter")
10+
gen.add("str_param", paramtype="std::string", description="A string parameter", default="Hello World")
11+
gen.add("bool_param", paramtype="bool", description="A Boolean parameter")
12+
gen.add("vector_param", paramtype="std::vector<double>", description="A vector parameter")
13+
gen.add("map_param", paramtype="std::map<std::string,std::string>", description="A map parameter")
14+
15+
# Default min and max values
16+
gen.add("weight", paramtype="double",description="Weight can not be negative", min=0.0)
17+
gen.add("age", paramtype="int",description="Normal age of a human is inbetween 0 and 100", min=0, max=100)
18+
gen.add("default_param", paramtype="std::string",description="Parameter with default value", default="Hello World")
19+
20+
# Constant and configurable parameters
21+
gen.add("optimal_parameter", paramtype="double", description="Optimal parameter, can not be set via rosparam", default=10, constant=True)
22+
gen.add("configurable_parameter", paramtype="double", description="This parameter can be set via dynamic_reconfigure", configurable=True)
23+
24+
# Defining the namespace
25+
#gen.add("global_parameter", paramtype="std::string", description="This parameter is defined in the global namespace", global_scope=True)
26+
27+
# Full signature
28+
gen.add("dummy", paramtype="double", description="My Dummy parameter", level=0, edit_method="", default=5.2, min=0, max=10, configurable=True, global_scope=False, constant=False)
29+
30+
# Add an enum:
31+
gen.add_enum("my_enum", description="My first self written enum", entry_strings=["Small", "Medium", "Large", "ExtraLarge"], default="Medium")
32+
33+
#Syntax : Package, Node, Config Name(The final name will be MyDummyConfig)
34+
exit(gen.generate("rosparam_handler_tutorial", "demo_node", "Demo"))

launch/demo_node.launch

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<launch>
2+
3+
<arg name="config" default="$(find rosparam_handler_tutorial)/launch/params/demo_parameters.yaml" />
4+
5+
<node pkg="rosparam_handler_tutorial" type="demo" name="demo" output="screen">
6+
<rosparam command="load" file="$(arg config)"/>
7+
</node>
8+
9+
<node name="reconfigure_gui" pkg="rqt_reconfigure" type="rqt_reconfigure" />
10+
11+
</launch>

launch/demo_nodelet.launch

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<launch>
2+
3+
<arg name="config" default="$(find rosparam_handler_tutorial)/launch/params/demo_parameters.yaml"/>
4+
<arg name="nodelet_manager_name" default="nodelet_manager_demo"/>
5+
<arg name="start_nodelet_manager" default="true"/>
6+
<arg name="nodelet_name" default="demo"/>
7+
8+
<!-- Nodelet manager (if enabled) -->
9+
<node pkg="nodelet" type="nodelet" name="$(arg nodelet_manager_name)" args="manager" output="screen"
10+
if="$(arg start_nodelet_manager)"/>
11+
12+
<!-- Demo -->
13+
<node pkg="nodelet" type="nodelet" name="$(arg nodelet_name)"
14+
args="load rosparam_handler_tutorial/DemoNodelet $(arg nodelet_manager_name)" output="screen" required="true">
15+
<rosparam command="load" file="$(arg config)"/>
16+
</node>
17+
18+
19+
<node name="reconfigure_gui" pkg="rqt_reconfigure" type="rqt_reconfigure" />
20+
21+
22+
</launch>

launch/params/demo_parameters.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use this file to specify parameters, that do not have default values.
2+
# YAML Files can also be helpfull if you want to save parameters for different use-cases.
3+
# In any way: Default values should go into the .mrtcfg file!
4+
#
5+
# Use 'key: value' pairs, e.g.
6+
# string: 'foo'
7+
# integer: 1234
8+
# float: 1234.5
9+
# boolean: true
10+
# vector: [1.0, 2.0, 3.4]
11+
# map: {"a": "b", "c": "d"}
12+
13+
int_param: 1
14+
double_param: 12.2
15+
bool_param: True
16+
vector_param: [1.0, 2.0, 3.4]
17+
map_param: {"a": "b", "c": "d"}
18+
weight: 2.3
19+
age: 1
20+
configurable_parameter: 10.

nodelet_plugins.xml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<library path="lib/libdemo_nodelet">
2+
<class name="rosparam_handler_tutorial/DemoNodelet"
3+
type="rosparam_handler_tutorial::DemoNodelet" base_class_type="nodelet::Nodelet">
4+
<description></description>
5+
</class>
6+
</library>

package.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<package format="2">
2+
<name>rosparam_handler_tutorial</name>
3+
<version>0.0.1</version>
4+
<description>A demo node for the rosparam_handler package</description>
5+
6+
<license>BSD</license>
7+
8+
<maintainer email="[email protected]">Claudio Bandera</maintainer>
9+
<author email="[email protected]">Claudio Bandera</author>
10+
<url type="repository">https://github.com/cbandera/rosparam_handler_tutorial.git</url>
11+
12+
<buildtool_depend>catkin</buildtool_depend>
13+
<build_depend>rosparam_handler</build_depend>
14+
<depend>dynamic_reconfigure</depend>
15+
<depend>roscpp</depend>
16+
<depend>roslib</depend>
17+
<depend>nodelet</depend>
18+
<export>
19+
<nodelet plugin="${prefix}/nodelet_plugins.xml" /><!-- Other tools can request additional information be placed here -->
20+
</export>
21+
22+
</package>

src/demo/demo.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "demo.hpp"
2+
3+
namespace rosparam_handler_tutorial {
4+
5+
Demo::Demo(ros::NodeHandle node_handle, ros::NodeHandle private_node_handle)
6+
: reconfigSrv_{private_node_handle}, params_{private_node_handle} {
7+
8+
/**
9+
* Initialization
10+
*/
11+
params_.fromParamServer();
12+
13+
/**
14+
* Set up timer and dynamic reconfigure server
15+
*/
16+
timer_ = private_node_handle.createTimer(ros::Duration(1. / params_.rate), &Demo::timerCallback, this);
17+
reconfigSrv_.setCallback(boost::bind(&Demo::reconfigureRequest, this, _1, _2));
18+
}
19+
20+
/*
21+
* Use const ConstPtr for your callbacks.
22+
* The 'const' assures that you can not edit incoming messages.
23+
* The Ptr type guarantees zero copy transportation within nodelets.
24+
*/
25+
void Demo::timerCallback(const ros::TimerEvent& event) {
26+
ROS_INFO_STREAM("Timer callback. configurable_parameter = " << params_.configurable_parameter
27+
<< ". Enum: " << params_.my_enum);
28+
}
29+
30+
/**
31+
* This callback is called whenever a change was made in the dynamic_reconfigure window
32+
*/
33+
void Demo::reconfigureRequest(DemoConfig& config, uint32_t level) {
34+
params_.fromConfig(config);
35+
timer_.setPeriod(ros::Duration(1. / params_.rate));
36+
ROS_WARN_STREAM("Parameter update:\n" << params_);
37+
}
38+
39+
} // namespace rosparam_handler_tutorial

src/demo/demo.hpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include <dynamic_reconfigure/server.h>
4+
#include <ros/ros.h>
5+
#include "rosparam_handler_tutorial/DemoParameters.h"
6+
7+
namespace rosparam_handler_tutorial {
8+
9+
class Demo {
10+
public:
11+
Demo(ros::NodeHandle, ros::NodeHandle);
12+
13+
private:
14+
DemoParameters params_;
15+
16+
ros::Timer timer_;
17+
dynamic_reconfigure::Server<DemoConfig> reconfigSrv_; // Dynamic reconfiguration service
18+
19+
void timerCallback(const ros::TimerEvent& event);
20+
void reconfigureRequest(DemoConfig&, uint32_t);
21+
};
22+
23+
} // namespace rosparam_handler_tutorial

src/demo/demo_node.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "demo.hpp"
2+
3+
int main(int argc, char* argv[]) {
4+
5+
ros::init(argc, argv, "demo_node");
6+
7+
rosparam_handler_tutorial::Demo demo(ros::NodeHandle(), ros::NodeHandle("~"));
8+
9+
ros::spin();
10+
return 0;
11+
}

src/demo/demo_nodelet.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "demo.hpp"
2+
#include <nodelet/nodelet.h>
3+
#include <pluginlib/class_list_macros.h>
4+
5+
namespace rosparam_handler_tutorial {
6+
7+
class DemoNodelet : public nodelet::Nodelet {
8+
9+
virtual void onInit();
10+
boost::shared_ptr<Demo> m_;
11+
};
12+
13+
void DemoNodelet::onInit() {
14+
m_.reset(new Demo(getNodeHandle(), getPrivateNodeHandle()));
15+
}
16+
17+
} // namespace rosparam_handler_tutorial
18+
19+
PLUGINLIB_DECLARE_CLASS(rosparam_handler_tutorial, DemoNodelet, rosparam_handler_tutorial::DemoNodelet, nodelet::Nodelet);

0 commit comments

Comments
 (0)