7.ROS中的参数通信
文章目录
- 7.ROS中的参数通信
- 7.1使用CLI工具调整参数
- 7.2参数通信之rclcpp实现
- 7.2.1创建节点
- 7.2.2rclcpp参数API
- Reference
ROS2中的参数是由键值对组成的,参数可以实现动态调整。
7.1使用CLI工具调整参数
启动turtlesim
功能包的环境
ros2 run turtlesim turtlesim_node
- 查看当前节点下的参数
ros2 param list
Output:
/turtlesim:background_bbackground_gbackground_rqos_overrides./parameter_events.publisher.depthqos_overrides./parameter_events.publisher.durabilityqos_overrides./parameter_events.publisher.historyqos_overrides./parameter_events.publisher.reliabilityuse_sim_time
- 可以详细查看每一个参数的含义
ros2 param describe <node_name> <param_name>
ros2 param describe /turtlesim background_r
Output:
Parameter name: background_rType: integerDescription: Red channel of the background colorConstraints:Min value: 0Max value: 255Step: 1
- 查看每个参数的值
ros2 param get <node_name> <param_name>
ros2 param get /turtlesim background_r
Output:
Integer value is: 69
- 设置参数的值
ros2 param set <node_name> <param_name> <value>
ros2 param set /turtlesim background_r 10
Output:
Set parameter successful
- 存储当前的所有参数
ros2 param dump <node_name>
会保存一个<node_name>.yaml
文件到当前终端的路径中,然后我们载入这个文件就能够加载所有的参数了。
- 加载参数文件
ros2 param load <node_name> <param_path>
7.2参数通信之rclcpp实现
7.2.1创建节点
ROS2中的日志类型分为5个等级,分别是
RCLCPP_DEBUG(this->get_logger(), "This is DEBUG info!");
RCLCPP_INFO(this->get_logger(), "This is INFO info!");
RCLCPP_WARN(this->get_logger(), "This is WARN info!");
RCLCPP_ERROR(this->get_logger(), "This is ERROR info!");
RCLCPP_FATAL(this->get_logger(), "This is FATAL info!");
我们可以对日志的级别进行过滤,从而查看我们想看的信息
this->get_logger().set_level(log_level);
我们可以通过参数通信来控制查看日志的等级,从而实现日志消息的过滤。
首先创建一个功能包和测试节点,
ros2 pkg create example_service_rclcpp --build-type ament_cmake --dependencies rclcpp --license Apache-2.0
parameters_basic.cpp
#include "rclcpp/rclcpp.hpp"class ParametersBasicNode: public rclcpp::Node{
public:ParametersBasicNode(std::string name): Node(name){// 启动节点RCLCPP_INFO(this->get_logger(), "Node: %s has been launched", name.c_str());}
private:};int main(int argc, char * argv[]){// 初始化rclcpprclcpp::init(argc, argv);// 创建节点auto node = std::make_shared<ParametersBasicNode>("parameters_basic");// 关闭节点rclcpp::spin(node);rclcpp::shutdown();return 0;
}
CMakeLists.txt
add_executable(parameters_basic src/parameters_basic.cpp)
ament_target_dependencies(parameters_basic rclcpp)install(TARGETS parameters_basic DESTINATION lib/${PROJECT_NAME}
)
7.2.2rclcpp参数API
相关的API可以参考:rclcpp: rclcpp: ROS Client Library for C++ (ros2.org)
使用参数来控制日志的级别,完整程序
parameters_basic.cpp
#include "rclcpp/rclcpp.hpp"/*# declare_parameter 声明和初始化一个参数# describe_parameter(name) 通过参数名字获取参数的描述# get_parameter 通过参数名字获取一个参数# set_parameter 设置参数的值
*/class ParametersBasicNode: public rclcpp::Node{
public:ParametersBasicNode(std::string name): Node(name){// 启动节点RCLCPP_INFO(this->get_logger(), "Node: %s has been launched", name.c_str());this->declare_parameter("rcl_log_level", 0);this->get_parameter("rcl_log_level", log_level);this->get_logger().set_level((rclcpp::Logger::Level)log_level);using namespace std::literals::chrono_literals;timer_ = this->create_wall_timer(500ms, std::bind(&ParametersBasicNode::timer_callback, this));}
private:int log_level;rclcpp::TimerBase::SharedPtr timer_;void timer_callback(){this->get_parameter("rcl_log_level", log_level);this->get_logger().set_level(rclcpp::Logger::Level(log_level));std::cout << "=========================================" << std::endl;RCLCPP_DEBUG(this->get_logger(), "This is DEBUG!");RCLCPP_INFO(this->get_logger(), "This is INFO!");RCLCPP_WARN(this->get_logger(), "This is WARN!");RCLCPP_ERROR(this->get_logger(), "This is ERROR!");RCLCPP_FATAL(this->get_logger(), "This is FATAL!");std::cout << "=========================================" << std::endl;}};int main(int argc, char * argv[]){// 初始化rclcpprclcpp::init(argc, argv);// 创建节点auto node = std::make_shared<ParametersBasicNode>("parameters_basic");// 关闭节点rclcpp::spin(node);rclcpp::shutdown();return 0;
}
source ./install/setup.bash
ros2 run example_parameters_rclcpp parameters_basic --ros-args -p rcl_log_level:=10
运行效果如下:
也可以动态设置参数
ros2 param list
ros2 param set /parameters_basic rcl_log_level 20
如下:
Reference
[1]d2lros2
[2]ROS2 Tutorial Official