点击(此处)折叠或打开
-
#include <rclcpp/rclcpp.hpp>
-
#include <chrono>
-
#include <string>
-
#include <functional>
-
-
using namespace std::chrono_literals;
-
-
class ParametersClass: public rclcpp::Node
-
{
-
public:
-
ParametersClass()
-
: Node("parameter_node")
-
{
- this->declare_parameter<std::string>("my_parameter", "world");
-
timer_ = this->create_wall_timer(
-
1000ms, std::bind(&ParametersClass::respond, this));
-
}
-
void respond()
-
{
-
this->get_parameter("my_parameter", parameter_string_);
-
RCLCPP_INFO(this->get_logger(), "Hello %s", parameter_string_.c_str());
-
}
-
private:
-
std::string parameter_string_;
-
rclcpp::TimerBase::SharedPtr timer_;
-
};
-
-
int main(int argc, char** argv)
-
{
-
rclcpp::init(argc, argv);
-
rclcpp::spin(std::make_shared<ParametersClass>());
-
rclcpp::shutdown();
-
return 0;
- }
[INFO] [parameter_node]: Hello world
在控制台手动改写参数
点击(此处)折叠或打开
-
首先确保
-
ros2 run cpp_parameters parameter_node 已经运行
-
-
ros2 param list
-
ros2 param set /parameter_node 参数名 参数值
通过 launch文件修改参数
点击(此处)折叠或打开
-
from launch import LaunchDescription
-
from launch_ros.actions import Node
-
-
def generate_launch_description():
-
return LaunchDescription([
-
Node(
-
package="cpp_parameters",
-
executable="parameter_node",
-
name="custom_parameter_node",
-
output="screen",
-
emulate_tty=True,
-
parameters=[
-
{"my_parameter": "earth"}
-
]
-
)
-
])