Keypoints
- Parameter server
rosparam
command- Example
rosparam
commandrosparam
command is used to get and set parameters in the parameter server. rosparam list
: list all parameters in the parameter serverrosparam get /parameter_name
: get the value of the parameterrosparam set /parameter_name value
: set the value of the parameterrosparam dump file_name
: dump all parameters in the parameter server to a file in current directoryrosparam load file_name
: load parameters from a file to the parameter serverrosparam delete /parameter_name
: delete the parameter #include <ros/ros.h>
#include <std_srvs/Empty.h>
int main(int argc, char **argv)
{
int red, green, blue;
ros::init(argc, argv, "parameter_config");
ros::NodeHandle nh;
ros::param::get("/turtlesim/background_r", red);
ros::param::get("/turtlesim/background_g", green);
ros::param::get("/turtlesim/background_b", blue);
ROS_INFO("Get background color: R-%d, G-%d, B-%d", red, green, blue);
ros::param::set("/turtlesim/background_r", 255);
ros::param::set("/turtlesim/background_g", 255);
ros::param::set("/turtlesim/background_b", 255);
ROS_INFO("Set Background Color to [255, 255, 255]");
ros::param::get("/turtlesim/background_r", red);
ros::param::get("/turtlesim/background_g", green);
ros::param::get("/turtlesim/background_b", blue);
ROS_INFO("Re-get background color: R-%d, G-%d, B-%d", red, green, blue);
// below section equals to `rosservice call /clear "{}"` in terminal
ros::service::waitForService("/clear");
ros::ServiceClient clear_background_ = nh.serviceClient<std_srvs::Empty>("/clear");
std_srvs::Empty srv;
clear_background_.call(srv);
sleep(1);
return 0;
}
add_executable(parameter_config src/parameter_config.cpp)
target_link_libraries(parameter_config ${catkin_LIBRARIES})
catkin_make
source devel/setup.bash
roscore
rosrun turtlesim turtlesim_node
rosrun learning_parameter parameter_config
When set rosparam in terminal like /turtlesim/background_r 255, rosservice call /clear "{}"
is needed to refresh the background color.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rospy
import sys
from std_srvs.srv import Empty
def parameter_config():
rospy.init_node('parameter_config', anonymous=True)
red = rospy.get_param('/turtlesim/background_r')
green = rospy.get_param('/turtlesim/background_g')
blue = rospy.get_param('/turtlesim/background_b')
rospy.loginfo("Get background color: R-%d, G-%d, B-%d", red, green, blue)
rospy.set_param('/turtlesim/background_r', 255)
rospy.set_param('/turtlesim/background_g', 255)
rospy.set_param('/turtlesim/background_b', 255)
rospy.loginfo("Set Background Color to [255, 255, 255]")
red = rospy.get_param('/turtlesim/background_r')
green = rospy.get_param('/turtlesim/background_g')
blue = rospy.get_param('/turtlesim/background_b')
rospy.loginfo("Re-get background color: R-%d, G-%d, B-%d", red, green, blue)
rospy.wait_for_service('/clear')
try:
clear_background_ = rospy.ServiceProxy('/clear', Empty)
clear_background_()
except rospy.ServiceException as e:
print("Service call failed: %s"%e)
if __name__ == '__main__':
parameter_config()