C++ plugin cannot get parameter that has been uploaded to param server later
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
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 If I initiate a ros node inside the gazebo plugin, it does not get the realtime access to rosparams?
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.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
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.
Alright I wasn't sure if you were referring to a specific plugin or a custom one.
@Delb, If I understand correctly, then that is what I am doing. On each update I call
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.What do you get if you print the value of the param in your plugin ?
What update ? Are you sure you function is correctly called ?
Can you share the code snippet where you upload the parameters and than run the node? That can give an insight.