首页 > 解决方案 > 使用 ros 在代码中发出切换控制器

问题描述

我正在尝试在代码中切换控制器,但它一直返回 false,我不知道为什么,这是代码:

 ros::ServiceClient switch_controller = n.serviceClient<controller_manager_msgs::SwitchController>("egm/controller_manager/switch_controller");

std::vector<std::string> start_controller;
      start_controller.push_back("joint_group_velocity_controller");
      std::vector<std::string> stop_controller;
      stop_controller.push_back("");
      switch_controller_req.start_controllers = start_controller;
      switch_controller_req.stop_controllers = stop_controller;
      switch_controller_req.strictness = 1;
      switch_controller_req.start_asap = false;
      switch_controller_req.timeout = 0.0;
      ros::service::waitForService("egm/controller_manager/switch_controller", ros::Duration(5));
      success = stop_rapid.call(switch_controller_req,switch_controller_resp);
      if (success)
      {
        ROS_INFO_STREAM("Controller switch correctly");

      }
      else
      {
        ROS_ERROR_STREAM("Error occured trying to switch controller");
        return 0;
      }

标签: c++linuxcontrolsros

解决方案


对您的代码稍作修改即可。试试这个:

 ros::ServiceClient switch_controller = n.serviceClient<controller_manager_msgs::SwitchController>("egm/controller_manager/switch_controller");

std::vector<std::string> start_controller;
      start_controller.push_back("joint_group_velocity_controller");
      std::vector<std::string> stop_controller;
      stop_controller.push_back("");
      controller_manager_msgs::SwitchController switch_controller_req;
      switch_controller_req.request.start_controllers = start_controller;
      switch_controller_req.request.stop_controllers = stop_controller;
      switch_controller_req.request.strictness = 1;
      switch_controller_req.request.start_asap = false;
      ros::service::waitForService("egm/controller_manager               /switch_controller", ros::Duration(5));
        switch_controller.call(switch_controller_req);
      if (switch_controller_req.response.ok)
      {
        ROS_INFO_STREAM("Controller switch correctly");

      }
      else
      {
        ROS_ERROR_STREAM("Error occured trying to switch controller");
        return 0;
      }

推荐阅读