ROS parameter server implementation (8) C++, Python implementation

content

Introduction

theoretical model

 C++ implementation

Implement parameter server new (modified) parameters

 Implementing parameter server queries

 Parameter server delete implementation

 Python implementation

Implement server new modification parameters 

 Implement server query parameters

Parameter server deletion implementation

Common commands

rosnode 

rostopic 

 rosmsg

rosservice

rossrv

 snarled



Introduction

The parameter server is mainly used to realize data sharing between different nodes in ROS. The parameter server is equivalent to a common container independent of all nodes. Data can be stored in the container and called by different nodes. Of course, different nodes can also store data in it. The typical application scenarios of the parameter server are as follows:

 When the navigation is implemented, path planning will be carried out, such as: global path planning, design a rough path from the starting point to the target point. Local path planning, which will generate the travel path from time to time according to the current road conditions

In the above scenario, the parameter server is used for global path planning and local path planning:

  • When planning the path, we need to refer to the size of the car. We can store these size information in the parameter server. Both the global path planning node and the local path planning node can call these parameters from the parameter server.

The parameter server is generally suitable for some application scenarios where data sharing exists.

 

theoretical model

 

 It is not efficient because it uses the rpc protocol

 

 

iso8601 dates are date data

 C++ implementation

 New feature pack

 

 Then press enter to depend on roscpp rospy std_msgs

Implement parameter server new (modified) parameters

then create a new file

 Write the following code

Modify CMakeLists

 

then compile

Then start the terminal and execute 

 

You can see that the parameters are passed in

 

 Parameter information can also be found

Next, modify the parameters

You can see that the keys are the same

So the later overwriting the previous key value actually implements the modification

 test

was changed to 0.2

 Implementing parameter server queries

 create a new file

 Then modify the CMakeLists

Then enter the following code to test the query function

 compile

then test

 test getparam

 open terminal

 test getparamnames

 open terminal

successful traversal

 

 

 test ros::param

 terminal

 Parameter server delete implementation

create a new file

 

 Change setting

 Copy the code below

/* 
    参数服务器操作之删除_C++实现:

    ros::NodeHandle
        deleteParam("键")
        根据键删除参数,删除成功,返回 true,否则(参数不存在),返回 false

    ros::param
        del("键")
        根据键删除参数,删除成功,返回 true,否则(参数不存在),返回 false


*/
#include "ros/ros.h"


int main(int argc, char *argv[])
{   
    setlocale(LC_ALL,"");
    ros::init(argc,argv,"delete_param");

    ros::NodeHandle nh;
    bool r1 = nh.deleteParam("nh_int");
    ROS_INFO("nh 删除结果:%d",r1);

    bool r2 = ros::param::del("param_int");
    ROS_INFO("param 删除结果:%d",r2);

    return 0;
}

Then

ctrl shift b compile

successfully deleted

 Python implementation

new folder new file

Implement server new modification parameters 

Copy the code below

#! /usr/bin/env python
"""
    参数服务器操作之新增与修改(二者API一样)_Python实现:
"""

import rospy

if __name__ == "__main__":
    rospy.init_node("set_update_paramter_p")

    # 设置各种类型参数
    rospy.set_param("p_int",10)
    rospy.set_param("p_double",3.14)
    rospy.set_param("p_bool",True)
    rospy.set_param("p_string","hello python")
    rospy.set_param("p_list",["hello","haha","xixi"])
    rospy.set_param("p_dict",{"name":"hulu","age":8})

    # 修改
    rospy.set_param("p_int",100)

Change setting

Then add execute permission

 

 then compile

then start the command line

 

You can see that the operation was successful 

 Implement server query parameters

create a new file

 

 Add executable permission

 Configure CMakeLists

 then compile

Then open terminal to test

 

 test was successful

Parameter server deletion implementation

create a new file

 Copy the code below

#! /usr/bin/env python
"""
    参数服务器操作之删除_Python实现:
    rospy.delete_param("键")
    键存在时,可以删除成功,键不存在时,会抛出异常
"""
import rospy

if __name__ == "__main__":
    rospy.init_node("delete_param_p")

    try:
        rospy.delete_param("p_int")
    except Exception as e:
        rospy.loginfo("删除失败")

 Add execute permission

Configure CMakeLists

 

 compile

open terminal

Successfully deleted

Common commands

rosnode 

 

 

rostopic 

 

 

 rosmsg

 

 

Open message publishers and subscribers

 

This is to see what type of message is being used 

 

rosservice

 

 

 

Above is the list of messages

 

here is as client

Note to enter the type message type after the node

Then press Tab to complete

Then modify the message to publish

 

 

Here is the display node information

Then the type inside is the one to be used to complete the above

rossrv

 

 The following is to show all service messages

 

 

Display the srv message of the corresponding node

Note: If the message is customized, you must enter the workspace first, and then refresh the environment variables to find it

 snarled

 

 

data in parameter server

Added a parameter

 

 

 delete parameter

 

created a yaml file 

which includes the parameters we set

Turn off roscore and restart

 

 

At this point, you can see that the parameters width and length are released.

I want to load it back

 The above is released back from yaml

 

and then read again

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324332953&siteId=291194637