c++ - 使用 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;
}
解决方案
对您的代码稍作修改即可。试试这个:
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;
}
推荐阅读
- javascript - 如何更改多个数组对象的索引?
- kotlin - Kotlin flatten + 嵌套对象的映射
- java - Java ME 或 Java SE(Raspberry Pi 零)
- java - 为什么引用在内存中有大小,即使它没有引用此代码示例中的对象?
- linux - 在 apropos 命令中计算单词
- reactjs - componentDidUpdate 比 setState 中的实际值落后一步
- typescript - 将 JSON 作为类型导入
- javascript - 如何在 Inputmask 插件中设置范围?
- python - 如何将变量的名称作为字符串获取?
- python - 从 url 请求中保存多个 jpg 文件