1
$\begingroup$

I am trying to have a parameter client set the parameters of a node in a timer callback.But unfortunately the parameters are not set during the callback the code is as follows. i also tested with a callback group(below),but it also doesn't seem to work.

class TimerParamClient : public rclcpp::Node
{
public:
  TimerParamClient()
    : Node("timer_param_client")
  {


    sample_node1_cl      = std::make_shared<rclcpp::AsyncParametersClient>(this, "sample_node1");
    sample_node1_cl     = std::make_shared<rclcpp::AsyncParametersClient>(this, "sample_node2");
    timer_             = this->create_wall_timer(500ms, std::bind(&TimerParamClient::execute, this));
  }

 
  void execute()
  {
   
    if (isTrue())
    {
      sample_node1_cl ->set_parameters({rclcpp::Parameter("param1", 1.0)});
      sample_node2_cl ->set_parameters({rclcpp::Parameter("param2", 1.0)});
    }
  }

private:

  rclcpp::TimerBase::SharedPtr timer_;
  rclcpp::AsyncParametersClient::SharedPtr move_param_cl;
  rclcpp::AsyncParametersClient::SharedPtr stop_param_cl;

};
int main(int argc, char** argv)
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<TimerParamClient>());
  rclcpp::shutdown();
  return 0;
}

with callback groups

class TimerParamClient : public rclcpp::Node
{
public:
  TimerParamClient()
    : Node("timer_param_client")
  {
   client_cb_group_ = this->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
timer_cb_group_ = this->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);

    sample_node1_cl      = std::make_shared<rclcpp::AsyncParametersClient>(this, "sample_node1",rmw_qos_profile_default,client_cb_group_);

    sample_node2_cl      = std::make_shared<rclcpp::AsyncParametersClient>(this, "sample_node2",rmw_qos_profile_default,client_cb_group_);

    timer_             = this->create_wall_timer(500ms, std::bind(&TimerParamClient::execute, this),timer_cb_group_);
  }

 
  void execute()
  {
   
    if (isTrue())
    {
      node1->set_parameters({rclcpp::Parameter("param1", 1.0)});
      node2->set_parameters({rclcpp::Parameter("param2", 1.0)});
    }
  }

private:

  rclcpp::TimerBase::SharedPtr timer_;
  rclcpp::AsyncParametersClient::SharedPtr sample_node1_cl;
  rclcpp::AsyncParametersClient::SharedPtr sample_node2_cl;
  rclcpp::CallbackGroup::SharedPtr client_cb_group_;
  rclcpp::CallbackGroup::SharedPtr timer_cb_group_;

};
int main(int argc, char** argv)
{
  rclcpp::init(argc, argv);
  auto client_node = std::make_shared<TimerParamClient>();
  rclcpp::executors::MultiThreadedExecutor executor;
  executor.add_node(client_node);

  RCLCPP_INFO(client_node->get_logger(), "Starting client node, shut down with CTRL-C");
  executor.spin();
  RCLCPP_INFO(client_node->get_logger(), "Keyboard interrupt, shutting down.\n");

  rclcpp::shutdown();
  return 0;
}

What seems to be the problem here or are there any alternatives to this ?

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.