ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How could I switch ros controllers using ros controller_manager/switch_controller service in c++

asked 2020-03-13 10:12:17 -0500

Hi,

Right now, I can switch position controllers and torque controllers by running the following commands in a terminal.

rosservice call /myrobot/controller_manager/switch_controller "{start_controllers: ['joint1_torque_controller'],stop_controllers: [joint1_position_controller], strictness: 2}"

But, the question is how to implement that in my c++ code. I find something here. It is ros::service::call(const std::string & service_name, MReq & req, MRes & res ). The question become how to convert "{start_controllers: ['joint1_torque_controller'],stop_controllers: [joint1_position_controller], strictness: 2}" to be a MReq.

Thanks in advance

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-08-26 09:00:05 -0500

renowator gravatar image

updated 2021-08-31 08:59:26 -0500

Hello sir,

You need to use request responce messages specific to ros_control package, in particular the controller_manager/switch_controller service . These are defined here: https://github.com/ros-controls/ros_c...

The code then would look something like:

#include "controller_manager_msgs/SwitchController.h"
#include <ros/ros.h>

bool switchController(...){
controller_manager_msgs::SwitchControllerRequest req;
controller_manager_msgs::SwitchControllerResponse res;
//Choose which controller to load/unload
req.start_controllers = ...;
req.stop_controllers = ...;
req.strictness = 1;
req.start_asap = true;
req.timeout = 2.;
ros::service::call("/myrobot/controller_manager/switch_controller", res, req);
return res.ok;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-03-13 10:12:17 -0500

Seen: 620 times

Last updated: Aug 31 '21