Ask Your Question
0

C++ plugin cannot get parameter that has been uploaded to param server later

asked 2020-08-13 09:04:34 -0500

kump gravatar image

updated 2020-08-13 13:23:42 -0500

I want to load parameters from the ROS param server in the Gazebo model plugin. I do not have any problem with getting the parameters thet were set in the launch files, but I run a node that uploads a list of parameters to the rosparam server later in the run. I can see and read the parameters using terminal, but the gazebo plugin does not see the new parameters (I check for them inside the plugin on every update).

Can you provide any insight into why is this happening?

EDIT

I am accessing the parameters from gazebo plugin using

std::string param_name;
this->rosNode->getParam("/param_name", param_name);

EDIT 2

code snippet

namespace gazebo {
  // Constructor
  MyPlugin::MyPlugin() {}

  // Default plugin init call.
  void MyPlugin::Init() {}

  void MyPlugin::Load(physics::ModelPtr _model,
                                  sdf::ElementPtr _sdf) {

    this->updateConnection = event::Events::ConnectWorldUpdateBegin(
            boost::bind(&MyPlugin::OnUpdate, this));

    // Initialize ros, if it has not already been initialized.
    std::string node_name = std::string("my_node");
    if (!ros::isInitialized()) {
      int argc = 0;
      char **argv = nullptr;

      ros::init(argc, argv, node_name,
                ros::init_options::NoSigintHandler);
    }

    // Create ROS node.
    this->rosNode.reset(new ros::NodeHandle(node_name));

    ros::AsyncSpinner spinner(2); // Use 2 threads
    spinner.start(); // spin() will not return until the node has been shutdown
  }


  void MyPlugin::loadParameters()
  {
    this->parameters_loaded = true;
    std::string parameter_from_launch, parameter_from_node;
    if (! this->rosNode->getParam("/parameter_from_launch", parameter_from_launch))
    {
      gzmsg << "ROS parameter '/parameter_from_launch' was not found \n";
      this->parameters_loaded = false;
    }
    gzmsg << "Parameter from launch: " << parameter_from_launch << "\n";
    if (! this->rosNode->getParam("/parameter_from_node", parameter_from_node))
    {
      gzmsg << "ROS parameter /parameter_from_node was not found. \n";
      this->parameters_loaded = false;
    }
    gzmsg << "Parameter from node: " << parameter_from_node << "\n";

  }


  void MyPlugin::OnUpdate() {
    if(!this->parameters_loaded){
      MyPlugin::loadParameters();
    }
  }
}

the parameter_from_launch is found on the first try and prints the correct value. The "ROS parameter /parameter_from_node was not found. \n" keeps being printed and the gzmsg << "Parameter from node: " << parameter_from_node << "\n" returns an empty string

edit retag flag offensive close merge delete

Comments

but the gazebo plugin does not see the new parameters

What do you mean with the gazebo plugin ? The parameters of a Gazebo plugin are set in the urdf/xacro file which is parsed in the launch file or when you call the plugin in the world file. If your parameters change you have to reload the plugin.

Delb gravatar image Delb  ( 2020-08-13 09:10:10 -0500 )edit

@Delb If I initiate a ros node inside the gazebo plugin, it does not get the realtime access to rosparams?

kump gravatar image kump  ( 2020-08-13 09:18:16 -0500 )edit

I've never done that so I don't know but if you've managed to start a ros node inside the plugin then to update the parameters you need to define them as dynamic parameters. When doing that you get a callback which will be triggered each time you change one of the dynamic parameters.

If you only change the parameter on the parameter server you would need to have an infinite loop always checking the parameter value (getParam ) to detect when it has changed.

Delb gravatar image Delb  ( 2020-08-13 09:26:48 -0500 )edit

What do you mean with the gazebo plugin ?

I have no experiences with ROS plugins, so I dont know if there is any difference. I write pluggins in C++ that I register as gazebo_model_plugin using

 GZ_REGISTER_MODEL_PLUGIN(StateSamplingPlugin)

Which probably just makes it known to the gazebo system. It's probably the same like any ROS plugin. But I don't really know.

kump gravatar image kump  ( 2020-08-13 09:28:31 -0500 )edit

Alright I wasn't sure if you were referring to a specific plugin or a custom one.

Delb gravatar image Delb  ( 2020-08-13 09:32:13 -0500 )edit

@Delb, If I understand correctly, then that is what I am doing. On each update I call

rosNode->getParam('param_name', param)

But even so the parameter is not found in the plugin, while I can find it using the terminal (rosparam get <param>) at the same time.

kump gravatar image kump  ( 2020-08-13 09:32:15 -0500 )edit

What do you get if you print the value of the param in your plugin ?

On each update

What update ? Are you sure you function is correctly called ?

Delb gravatar image Delb  ( 2020-08-13 09:35:39 -0500 )edit

Can you share the code snippet where you upload the parameters and than run the node? That can give an insight.

Tahir M. gravatar image Tahir M.  ( 2020-08-13 12:21:04 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-08-14 07:32:08 -0500

kump gravatar image

updated 2020-08-14 07:34:49 -0500

Ok, the reason the this->rosNode->getParam("/parameter_from_node", parameter_from_node) returned false was because I set the variable parameter_from_nodeas string while the /parameter_from_node parameter is a float number. I expected all the parameters on the param server to be managed as strings but that is apparently not the case. The /parameter_from_launch happend to actually be a string, so it went through correctly.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-08-13 09:04:34 -0500

Seen: 147 times

Last updated: Aug 14 '20