Robotics StackExchange | Archived questions

How to read yaml file correctly when spawning models?

Hello, I want to read the yaml file when spawning a model. But the model plugin cannot read parameters while they can be displayed in a terminal. So how to read yaml file correctly when spawning models?

Part of the launch file:

  <node name="sdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen" args="-sdf -model lunarveh -file $(find lunarveh_description)/models/model.sdf">
    <rosparam file="$(find lunarveh_description)/config/wheel_pid.yaml" command="load" />
  </node>

Part of the plugin:

  std::vector<float> read_front_wheel_pid, read_rear_wheel_pid;

  this->n.getParam("front_wheel_pid", read_front_wheel_pid);
  this->n.getParam("rear_wheel_pid", read_rear_wheel_pid);

  if (this->n.getParam("rear_wheel_pid", read_rear_wheel_pid))
    ROS_INFO("get value: %f", read_rear_wheel_pid[0]);
  else
    ROS_INFO("did not get value");

The yaml file is:

front_wheel_pid: [50.0, 0.5, 0.0]
rear_wheel_pid: [1000.0, 0.0, 0.0]

When running it shows:

[ INFO] [1627436039.252123233]: did not get value

But I can read the parameters in a terminal:

mu@mu-TM1703:~/mu$ rosparam get /sdf_spawner/front_wheel_pid
- 50.0
- 0.5
- 0.0

I use Ubuntu 20.04, ROS Noetic, Gazebo 11. Thank you!

Asked by muronglindong on 2021-07-27 21:09:11 UTC

Comments

Answers

Now I can answer my own question. It's a stupid mistake and it seems to create a name space called "/sdf_spawner" while spawning model. So the solution is clear: Modify the plugin as below:

  std::vector<float> read_front_wheel_pid, read_rear_wheel_pid;

  this->n.getParam("/sdf_spawner/front_wheel_pid", read_front_wheel_pid);
  this->n.getParam("/sdf_spawner/rear_wheel_pid", read_rear_wheel_pid);

  if (this->n.getParam("/sdf_spawner/rear_wheel_pid", read_rear_wheel_pid))
    ROS_INFO("get value: %f", read_rear_wheel_pid[0]);
  else
    ROS_INFO("did not get value");

Asked by muronglindong on 2021-08-03 10:48:06 UTC

Comments