Starting a Node with parameter in console

asked 2021-12-10 09:08:00 -0500

str00pw4f3l gravatar image

Hi there,

I've been trying to start a ROS2 node with parameters but i can't figure it out. I'm working with ROS2 dashing with C++.

I have a node that reads my GPS location with an executable named "GPS_node" and i would like to be able to tell the node on startup how often it should publish the location on a topic. The code for publishing on that topic is shown below:

     GPSInfo_publisher_ = this->create_publisher<mtr_messages::msg::Gpsinfo>("GPSinfo", 10);
        GPSInfotimer_ = this->create_wall_timer(
           std::chrono::milliseconds(time) , std::bind(&GPS::getGPSinfo, this));

This works fine. But i would like to be able to set the interger "time" in the create_wall_timer on startup.

I've tried to use a launch file as well as a yaml file. both don't seem to change the value.

I know i should first declare the parameter in my code and i should use get_parameter. I do this in the code below:

 this->declare_parameter<int>("trigger", 1000);

        rclcpp::Parameter t;
            this->get_parameter("trigger", t);

            int time = t.as_int();

To run the launch file i've used the command:

$ros2 launch mtr_hardware mtr_hardware_launch.py

To run the node with the yaml file i've used:

$ros2 run mtr_hardware GPS_node __params:param.yaml

Does anyone happen to know what i am doing wrong? i'll place my yaml and launch file attempts below. Thanks in advance.

yaml:

mtr_hardware:  # optional
  GPS_node:
    ros__parameters:
      trigger: 5000

Launch file:

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package="mtr_hardware",
            node_executable="GPS_node",
            output="screen",
            parameters=[
                {"trigger": 1000}
            ]
        )

    ])
edit retag flag offensive close merge delete