当前位置:网站首页>Use and programming method of ros-8 parameters

Use and programming method of ros-8 parameters

2022-07-05 08:45:00 m0_ forty-six million ninety-three thousand eight hundred and t

One 、 Concept

ROS Master There is a parameter server in Paramester Server, It is a global dictionary , It's for keeping Each node Configuration parameters between .

Two 、rosparam

For the parameters of each node , We can rosparam XXX And so on , Modify and other operations , The specific operation can be input through the terminal rosparam Command view .

Try not to open any nodes ,ros Global parameters in the system .

  Then I open the little turtle node , The global parameters in the current system are :

You can see , After opening the little turtle node, there are three more parameters , These three parameters are used to set the background plate of the little turtle RGB It's worth it .

Pay attention to each background Namespace before , In the future, whether you view or modify it, you should bring this namespace .

Can pass get Check the instructions of :

2.1 Modify the global parameters through the command line

Now let's try to modify these parameters on the command line :

  Let me use it first. rosparam set take b Value changed to 0, And then use rosparam get see b Value discovery changes take effect . But the background color of the little turtle is still this blue , There is no change . So here we need to pay attention to , Changing parameters on the command line is not real-time , Therefore, if the parameter is modified with the command line , You need to resend the parameter change request , The parameter change request command is as follows :

 2.2 Save the global parameters of the current system

have access to rosparam dump file name .yaml To save the global parameters of the current system , It should be noted that the default path for saving files is the home directory

  After the save .yaml The documents are as follows :

  We can also be here .yaml Modify parameter values in .

for example : I put background_g Change it to 255

  After saving the file , Need to pass through rosparam load file name .yaml Load the modified file , Then send the parameter change request again .

2.3 Set the global parameters through the program

Create in the current workspace learning_parameter Function pack , And write the following c++ Program parameter_config:

/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/

/**
 *  This routine sets / Read the parameters in the turtle routine 
 */
#include <string>
#include <ros/ros.h>
#include <std_srvs/Empty.h>

int main(int argc, char **argv)
{
	int red, green, blue;

    // ROS Node initialization 
    ros::init(argc, argv, "parameter_config");

    //  Create node handle 
    ros::NodeHandle node;

    //  Read background color parameters 
	ros::param::get("/turtlesim/background_r", red);
	ros::param::get("/turtlesim/background_g", green);
	ros::param::get("/turtlesim/background_b", blue);

	ROS_INFO("Get Backgroud Color[%d, %d, %d]", red, green, blue);

	//  Set the background color parameters 
	ros::param::set("/turtlesim/background_r", 255);
	ros::param::set("/turtlesim/background_g", 255);
	ros::param::set("/turtlesim/background_b", 255);

	ROS_INFO("Set Backgroud Color[255, 255, 255]");

    //  Read background color parameters 
	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 Backgroud Color[%d, %d, %d]", red, green, blue);

	//  Call the service , Refresh background color 
	ros::service::waitForService("/clear");
	ros::ServiceClient clear_background = node.serviceClient<std_srvs::Empty>("/clear");
	std_srvs::Empty srv;
	clear_background.call(srv);
	
	sleep(1);

    return 0;
}

After writing the program , After compiling , restart roscore, Run the little turtle node , And then run the program

The operation results are as follows ;

原网站

版权声明
本文为[m0_ forty-six million ninety-three thousand eight hundred and t]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140540130570.html