当前位置:网站首页>Ros2 - use of parameters
Ros2 - use of parameters
2022-04-22 13:03:00 【Fan Ziqi】
ROS2—— Use of parameters
Last time when it comes to , Organization Give everyone free 2 A hamburger , one day Organization In business , Hamburgers are suddenly in short supply , The leader decided to change the rules temporarily , After that, everyone can only get one hamburger . But the node is already running , How to change this value ? This requires parameters parameters
Parameter Introduction
Parameter is the configuration parameter value of the node . You can think of parameters as part of node configuration . The parameter is an integer , Floating point numbers , Boolean value , Strings and lists . stay ROS2 in , Each node has its own parameters . All parameters are dynamically reconfigurable , And based on ROS2 Service built .
In this case , The number of hamburgers per person can be Organization A parameter of the node .
below , We will modify this service program , Change the number of hamburgers per capita by modifying the parameters
Modify the service program
newly build Organization_with_parameter.cpp file
Code from Organization.cpp To modify , Use... In different places // CHANGE: Marked out , Please compare yourself with the code in the previous section .
#include "rclcpp/rclcpp.hpp"
#include "service_interfaces/srv/calculate.hpp"
using std::placeholders::_1;
using std::placeholders::_2;
class Organization : public rclcpp::Node
{
public:
Organization() : Node("Organization"), NumOfAll(100)
{
RCLCPP_INFO(this->get_logger(), " Hello everyone , We are an enthusiastic organization , We only give poorer Hair hamburger .");
callback_group_organization = this->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
Organization_Server = this->create_service<service_interfaces::srv::Calculate>("Calculate",
std::bind(&Organization::organization_callback,this,_1,_2),
rmw_qos_profile_services_default,
callback_group_organization);
// CHANGE: Declare parameters
this->declare_parameter<int>("NumOfEachPerson", NumOfEachPerson);
}
private:
size_t NumOfAll;
// CHANGE: State how many hamburgers each person gets , The default is 2
int NumOfEachPerson = 2;
rclcpp::CallbackGroup::SharedPtr callback_group_organization;
rclcpp::Service<service_interfaces::srv::Calculate>::SharedPtr Organization_Server;
void organization_callback(const service_interfaces::srv::Calculate::Request::SharedPtr request,
const service_interfaces::srv::Calculate::Response::SharedPtr response)
{
if(request->status == "Poorer")
{
RCLCPP_INFO(this->get_logger(), " Received a message from %s Request , He has %d personal .", request->status.c_str(), request->num_of_people);
// CHANGE: Update parameters
this->get_parameter("NumOfEachPerson", NumOfEachPerson);
// CHANGE: Calculate the number of hamburgers to be given , Given by parameters
unsigned int NumOfRequired = request->num_of_people * NumOfEachPerson;
if(NumOfRequired > NumOfAll)
{
RCLCPP_INFO(this->get_logger(), " At present, there are only %d A hamburger ! Not enough points , Please come back tomorrow .", NumOfRequired);
response->success = false;
}
else
{
NumOfAll -= NumOfRequired;
response->num_of_hamburger = NumOfRequired;
response->success = true;
RCLCPP_INFO(this->get_logger(), " Successfully send out %d A hamburger ~ Remnant %d A hamburger ", NumOfRequired, NumOfAll);
}
}
else
{
response->success = false;
response->num_of_hamburger = 0;
RCLCPP_INFO(this->get_logger(), " Received an illegal request , This man is %s, Not qualified to send hamburgers .", request->status.c_str());
}
}
};
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
auto node = std::make_shared<Organization>();
rclcpp::executors::MultiThreadedExecutor exector;
exector.add_node(node);
exector.spin();
rclcpp::shutdown();
return 0;
}
Cmakelist.txt
add to :
add_executable(Organization_with_parameters_node src/Organization_with_parameters.cpp)
ament_target_dependencies(Organization_with_parameters_node rclcpp service_interfaces)
add to :Organization_with_parameters_node
install(TARGETS
...
Organization_with_parameters_node
DESTINATION lib/${PROJECT_NAME}
)
package.xml
No need to modify
compile
--packages-select Specify compile customer_and_kfc Function pack
colcon build --packages-select poor_and_organization
Refresh the environment
source ~/.bashrc
function
Create a new terminal window , Run... With parameters Organization Server node
ros2 run poor_and_organization Organization_with_parameters_node
Create another terminal , function Poor Client node
At first, each person received two hamburgers , Run the client directly :
ros2 run poor_and_organization Poor_node Poorer 5
Organization Server side : Successfully issued 10 A hamburger
Poor client : Successfully received 10 A hamburger
At this point, you need to modify the parameters , Another terminal , function :
ros2 param set /Organization NumOfEachPerson 1
The following prompt is success
Run the client again
ros2 run poor_and_organization Poor_node Poorer 50
Poor client : 50 I received 50 A hamburger
Organization Server side : Successfully issued 50 A hamburger
thus it can be seen , The parameters were successfully modified , But the parameters at this time will not be retained , After the node where the parameter is located is restarted, it will be restored to the initial value . If a node has many parameters , After modification, how should I save the current parameters for the next call ? Please read on .
Parameters common commands
Use ros2 param
Check the parameter list
ros2 param list
It can also be specific to a node
ros2 param list /Organization
View parameter description
ros2 param describe /Organization NumOfEachPerson
Get parameter value
ros2 param get <node_name> <parameter_name>
ros2 param get /Organization NumOfEachPerson
Set the parameter value
ros2 param set <node_name> <parameter_name> <value>
ros2 param set /Organization NumOfEachPerson 1
Get the parameter value again , It has changed :
Save parameters
ros2 param dump <node_name>
ros2 param dump /Organization
In the root directory of the current terminal , You can see that an Organization.yaml file , Open this file
/Organization:
ros__parameters:
NumOfEachPerson: 1
use_sim_time: false
With this parameter file , You can set the parameters of the node through this file
Load parameters
Load parameters after the node is started
ros2 param load <node_name> <parameter_file>
ros2 param load /Organization ./Organization.yaml
The following information is returned to indicate successful loading :
Load parameters before node startup
ros2 run <package_name> <executable_name> --ros-args --params-file <file_name>
ros2 run poor_and_organization Organization_with_parameters_node --ros-args --params-file ./Organization.yaml
Please try to get the current NumOfEachPerson Value as an exercise .
版权声明
本文为[Fan Ziqi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221300407668.html
边栏推荐
- STM32CubeMX重定向printf输出至串口
- 如何成为开源数据库开发人员?
- 柔性印刷电路板(PCB)的设计方法、类型等详细解析
- C#之SQL数据库操作(源码)
- 动态规划 字符的最短距离
- sprintf格式化字符串
- How to batch delete worksheets in Excel
- The keys of redis have become backup and the values have disappeared. Why?
- What happens when the weight of the model and loss or intermediate variable become Nan
- ROS机器人学习——TF坐标变换
猜你喜欢

Digital business cloud electronic bidding system solution - standardize the political procurement process and improve work efficiency

学习笔记——数字化工厂 4.21

When doing correlation analysis, how to exclude singular value outliers to increase the accuracy of correlation analysis

ROS机器人学习——麦克纳姆轮运动学解算

Vnpy importing CSV data locally

ORB_ Slam3 learning: introduction to tracking thread

Opencv project 1-ocr recognition

NoSQL调查 PART3:开放源码的失败

STM32CubeMX重定向printf输出至串口

How to use colormaps and customize your favorite colorbar?
随机推荐
Day code 300 lines learning notes day 47
数商云:数字化采购浪潮下,企业如何实现局部突破,小步快跑
生态 | 万里数据库与溢信科技完成兼容认证
vnpy本地導入csv數據
JS random color
ROS2——手把手教你编写一个服务
Redis本地连接查看数据
动态规划 字符的最短距离
Type requirements for parameters pT1 and pT2 of OpenCV function line()
Leetcode 695. Maximum area of the island
RT-Thread配置SPI-Flash(W25Q256)
Verify whether the form is empty
Leetcode 118, Yanghui triangle
C custom button implementation source code
Explain in detail why the number of pixels with a gray value of 255 calculated by the opencv histogram calculation function calchist() is 0
MATLAB的二维线图绘图函数plot()实例积累
MPU6050-DMP读不出数据
The keys of redis have become backup and the values have disappeared. Why?
Altium Designer导出Gerber文件的一般步骤
柔性印刷电路板(PCB)的设计方法、类型等详细解析