| Feature |
ROS1 |
ROS2 |
| Parameter Storage |
Global parameter server (centralized) |
Node-specific parameters (distributed) |
| Lifetime |
Exists as long as ROS master is running |
Exists only as long as the node is running |
| Parameter Namespacing |
/global_param # Global parameter
/node_name/param_name # Node parameter
|
# All parameters are node-specific
# Access using node name and parameter name
/node_name: # Node
param_name # Parameter
|
| Declaring Parameters |
Not required, parameters can be created on-the-fly |
Must be declared before use (with exceptions) |
| C++ API (Setting Parameters) |
// Global parameter
ros::param::set("/global_param", value);
// Node parameter
ros::NodeHandle nh;
nh.setParam("param_name", value);
|
// Must declare first
this->declare_parameter("param_name", default_value);
// Then set
this->set_parameter(rclcpp::Parameter("param_name", new_value));
// Or set parameters atomically (multiple at once)
std::vector params = {
rclcpp::Parameter("param1", value1),
rclcpp::Parameter("param2", value2)
};
this->set_parameters(params);
|
| C++ API (Getting Parameters) |
// Global parameter
int value;
if (ros::param::get("/global_param", value)) {
// Parameter exists and was retrieved
}
// Node parameter
ros::NodeHandle nh;
int value;
if (nh.getParam("param_name", value)) {
// Parameter exists and was retrieved
}
// With default value
int value = 42; // Default
nh.param("param_name", value, 42);
|
// Must declare first (with default)
this->declare_parameter("param_name", default_value);
// Then get
int value = this->get_parameter("param_name").as_int();
// Alternative approach
int value;
this->get_parameter("param_name", value);
// Getting multiple parameters
auto parameters = this->get_parameters({"param1", "param2"});
|
| Python API (Setting Parameters) |
# Global parameter
rospy.set_param('/global_param', value)
# Node parameter
rospy.set_param('~param_name', value)
|
# Must declare first
self.declare_parameter('param_name', default_value)
# Then set
self.set_parameters([rclpy.Parameter('param_name',
value=new_value)])
|
| Python API (Getting Parameters) |
# Global parameter
value = rospy.get_param('/global_param')
# Node parameter
value = rospy.get_param('~param_name')
# With default value
value = rospy.get_param('~param_name', default_value)
|
# Must declare first (with default)
self.declare_parameter('param_name', default_value)
# Then get
value = self.get_parameter('param_name').value
|
| Command Line Setting |
# Global parameter
rosparam set /global_param value
# Node parameter
rosparam set /node_name/param_name value
# Set from launch file
<param name="param_name" value="value" />
|
# Set parameter when starting a node
ros2 run package_name node_name --ros-args -p param_name:=value
# Set parameter for a running node
ros2 param set /node_name param_name value
# Set from launch file (Python)
Node(
package='package_name',
executable='node_name',
name='node_name',
parameters=[{'param_name': value}]
)
|
| Parameter Change Callbacks |
Separate package called dynamic_reconfigure needed
|
// C++
auto param_callback =
[](const std::vector & params) {
rcl_interfaces::msg::SetParametersResult result;
result.successful = true;
for (const auto & param : params) {
// Process changed parameter
}
return result;
};
callback_handle_ = add_on_set_parameters_callback(param_callback);
# Python
def param_callback(params):
for param in params:
if param.name == 'my_param':
# Process changed parameter
return SetParametersResult(successful=True)
self.add_on_set_parameters_callback(param_callback)
|
| Parameter Files |
# YAML structure
param1: value1
param2: value2
# Load from command line
rosparam load file.yaml /namespace
# Load from launch file
<rosparam file="$(find pkg)/config/params.yaml"
command="load" />
|
# YAML structure
node_name:
ros__parameters:
param1: value1
param2: value2
# Load from command line
ros2 run package_name node_name --ros-args --params-file path/to/file.yaml
# Load from launch file (Python)
Node(
...,
parameters=[yaml_file_path]
)
|
| Command Line Listing |
# List all parameters
rosparam list
# List parameters in a namespace
rosparam list /namespace
|
# List all parameters of all nodes
ros2 param list
# List parameters of a specific node
ros2 param list /node_name
|
| Parameter Types |
int, float, string, boolean, list, dictionary
|
boolean, integer, double, string, byte array, boolean array,
integer array, double array, string array
|
| Global Parameter Solution |
Built-in global parameter server
|
Create a dedicated parameter server node
|
留言
張貼留言