ROS Q&A | How to load ROS parameters in Runtime

Written by Marco Arruda

13/09/2017

 

In this video, we are showing how to get the updated values of a parameter in runtime.

 

Step 1. Create a project in ROS Development Studio(ROSDS)

We can do any ROS development we want easily, without setting up environment locally in ROSDS and that’s the reason why we will use ROSDS for this tutorial. If you haven’t had an account yet. You can create one here for free now. After logging in, let’s begin our journey by clicking on the create new project and call it test_parameter_server.

Step 2. Create a new package for testing

Now, let’s create a new package for testing with the following command

cd ~/catkin_ws/src
catkin_create_pkg testing_params roscpp

Then we create a new file called testing_params_node.cpp under /tesing_params/src with copying and pasting the following code.

#include "ros/ros.h"
#include <string>

int main(int argc, char **argv) {
  ros::init(argc,argv,"my_node");

  ros::NodeHandle nh;

  ros::Rate rate(1);

  std::string param1;

  if(nh.getParam("param1",param1)) {
    ROS_INFO("param ok!");
  }
  
  while(ros::ok) {
    ROS_INFO("param1: [%s]" , param1.c_str());
    
    ros::spinOnce();

    rate.sleep();
  }  

  return 0;
}

To compile the code, we’ll uncomment these two parts in /testing_params/CMakeLists.txt

...
add_executable(${PROJECT_NAME}_node src/testing_params_node.cpp)
...
target_link_libraries(${PROJECT_NAME}_node
  ${catkin_LIBRARIES}
)
...

Then we run the following command under catkin_ws to compile the code.

cd ~/catkin_ws
catkin_make
source devel/setup.bash

After compile, you can run the node with rosrun testing_params testing params_node

NOTICE: We no longer run ROS master for you in the backend, please launch it with the command roscore in a new terminal.

You should get param1: [] as output since we haven’t assigned any value to it yet.

Step 3. Get the updated parameters at runtime

Let’s assign a value to param1 by typing this command in a new terminal.

rosparam set param1 "hello from terminal"

However, the output is still []. If we check it with the command rosparam get param1 , you can see the value is “hello from terminal”.

To update it, we have to add one line in the testing_params_node.cpp then compile and run again.

...
  while(ros::ok){ 
    nh.getParam("param1",param1); //Add this line
...

Now the output gets updated whenever you change the parameters.

You can also create a params.yaml file under /testing_params/ to assign the parameters. For example:

param1: "hello from file!"

Then you have to run rosparam load params.yaml to load the parameter.

 

Edit by Tony Huang

 

 

Topics:
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

7 Comments

  1. Jcapua

    kahit 1500 lang

    Reply
  2. GAzzer

    800 diamonds.

    Reply
  3. benedict

    benedict
    nfjhesfaje
    f[segarg
    gdrg
    dsgds
    grdsgr
    dgrdgrdsrgdgrdsrg
    r g
    se

    Reply

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This