30 #include <gtest/gtest.h>
48 template<
typename T> T
intersect(
const T& v1,
const T &v2)
51 std::vector<typename T::value_type> sorted1(v1.begin(), v1.end()), sorted2(v2.begin(), v2.end());
54 std::sort(sorted1.begin(), sorted1.end());
55 std::sort(sorted2.begin(), sorted2.end());
57 std::set_intersection(sorted1.begin(), sorted1.end(), sorted2.begin(), sorted2.end(), std::back_inserter(res));
84 template<
typename Interface>
void registerHandle(Interface &iface,
bool can_switch)
86 if(can_switch)
interfaces_.insert(hardware_interface::internal::demangledTypeName<Interface>() );
87 iface.registerHandle(
typename Interface::ResourceHandleType(
jsh_, &
dummy_));
102 std::map<std::string, JointSharedPtr>
joints_;
107 joints_.insert(std::make_pair(name, j));
120 j->registerHandle(
pji_,
true);
121 j->registerHandle(
vji_,
true);
122 j->registerHandle(
eji_,
false);
125 j->registerHandle(
pji_,
true);
126 j->registerHandle(
vji_,
false);
127 j->registerHandle(
eji_,
true);
130 j->registerHandle(
pji_,
false);
131 j->registerHandle(
vji_,
true);
132 j->registerHandle(
eji_,
true);
140 bool prepareSwitch(
const std::list<hardware_interface::ControllerInfo>& start_list,
141 const std::list<hardware_interface::ControllerInfo>& stop_list)
override
144 if(!RobotHW::prepareSwitch(start_list, stop_list))
146 ROS_ERROR(
"Something is wrong with RobotHW");
150 if(!
intersect(start_list, stop_list).empty())
159 for (
const auto& controller : start_list)
161 if (controller.claimed_resources.size() != 1)
163 ROS_FATAL(
"We expect controllers to claim resoures from only one interface. This should never happen!");
167 for (
const auto& resource : iface_res.
resources)
170 if(iface_res.
hardware_interface ==
"hardware_interface::EffortJointInterface" && resource ==
"j_pe") j_pe_e =
true;
171 else if(iface_res.
hardware_interface ==
"hardware_interface::VelocityJointInterface" && resource ==
"j_ve") j_ve_v =
true;
189 return !(j_pe_e && j_ve_v);
191 void doSwitch(
const std::list<hardware_interface::ControllerInfo> &start_list,
const std::list<hardware_interface::ControllerInfo> &stop_list)
override
193 RobotHW::doSwitch(start_list, stop_list);
195 std::map<std::string, std::string> switches;
196 for (
const auto& controller : stop_list)
199 stopped_.push_back(controller.name);
201 for (
const auto& resource : iface_res.
resources)
203 switches[resource] =
"";
206 for (
const auto& controller : start_list)
209 started_.push_back(controller.name);
211 for (
const auto& resource : iface_res.
resources)
216 for (
const auto& to_switch : switches)
218 joints_[to_switch.first]->doSwitch(to_switch.second);
245 std::vector<std::string> joints;
246 if(!controller_nh.
getParam(
"joints", joints))
248 ROS_ERROR(
"Could not parse joint names");
251 std::set<std::string> resources(joints.begin(), joints.end());
253 claimed_resources.assign(1, iface_res);
264 void add(
const std::string type)
266 classes.insert(std::make_pair(
"Dummy" + type +
"Controller",
"hardware_interface::" + type));
271 add(
"EffortJointInterface");
272 add(
"PositionJointInterface");
273 add(
"VelocityJointInterface");
281 std::vector<std::string> v;
282 for (
const auto& declared_class :
classes)
284 v.push_back(declared_class.first);
306 TEST(SwitchInterfacesTest, SwitchInterfaces)
329 std::vector<std::string>
start, stop;
330 start.push_back(
"invalid_group_pos");
331 ASSERT_FALSE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
334 std::vector<std::string>
start, stop;
335 start.push_back(
"group_pos");
336 start.push_back(
"group_vel");
337 ASSERT_FALSE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
340 std::vector<std::string>
start, stop;
341 start.push_back(
"group_pos");
342 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
343 ASSERT_TRUE(cm.
switchController(stop,
start, controller_manager_msgs::SwitchControllerRequest::STRICT));
346 std::vector<std::string>
start, stop, next_start;
347 start.push_back(
"group_pos");
348 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
350 next_start.push_back(
"group_pos");
351 ASSERT_TRUE(cm.
switchController(next_start,
start, controller_manager_msgs::SwitchControllerRequest::STRICT));
353 ASSERT_TRUE(cm.
switchController(stop, next_start, controller_manager_msgs::SwitchControllerRequest::STRICT));
356 std::vector<std::string>
start, stop, next_start;
357 start.push_back(
"group_pos");
358 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
360 next_start.push_back(
"another_group_pos");
361 ASSERT_TRUE(cm.
switchController(next_start,
start, controller_manager_msgs::SwitchControllerRequest::STRICT));
363 ASSERT_TRUE(cm.
switchController(stop, next_start, controller_manager_msgs::SwitchControllerRequest::STRICT));
366 std::vector<std::string>
start, stop;
367 start.push_back(
"group_vel");
368 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
369 ASSERT_TRUE(cm.
switchController(stop,
start, controller_manager_msgs::SwitchControllerRequest::STRICT));
372 std::vector<std::string>
start, stop;
373 start.push_back(
"group_eff");
374 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
375 ASSERT_TRUE(cm.
switchController(stop,
start, controller_manager_msgs::SwitchControllerRequest::STRICT));
379 std::vector<std::string>
start, stop, next_start;
380 start.push_back(
"group_eff");
381 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
383 next_start.push_back(
"group_vel");
384 ASSERT_TRUE(cm.
switchController(next_start,
start, controller_manager_msgs::SwitchControllerRequest::STRICT));
386 ASSERT_FALSE(cm.
switchController(
start, next_start, controller_manager_msgs::SwitchControllerRequest::STRICT));
388 ASSERT_TRUE(cm.
switchController(stop, next_start, controller_manager_msgs::SwitchControllerRequest::STRICT));
392 std::vector<std::string>
start, stop;
393 start.push_back(
"single_pos");
394 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
395 ASSERT_TRUE(cm.
switchController(stop,
start, controller_manager_msgs::SwitchControllerRequest::STRICT));
398 std::vector<std::string>
start, stop;
399 start.push_back(
"single_pos");
400 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
401 ASSERT_TRUE(cm.
switchController(stop,
start, controller_manager_msgs::SwitchControllerRequest::STRICT));
404 std::vector<std::string>
start, stop;
405 start.push_back(
"single_pos");
406 start.push_back(
"group_eff");
407 ASSERT_FALSE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
410 std::vector<std::string>
start, stop;
411 start.push_back(
"single_eff");
412 start.push_back(
"group_vel");
413 ASSERT_FALSE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
416 std::vector<std::string>
start, stop;
417 start.push_back(
"single_pos");
418 start.push_back(
"group_vel");
419 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
420 ASSERT_TRUE(cm.
switchController(stop,
start, controller_manager_msgs::SwitchControllerRequest::STRICT));
423 std::vector<std::string>
start, stop;
424 start.push_back(
"single_pos");
425 start.push_back(
"group_vel");
426 start.push_back(
"totally_random_name");
427 ASSERT_FALSE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
428 ASSERT_FALSE(cm.
switchController(stop,
start, controller_manager_msgs::SwitchControllerRequest::STRICT));
430 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::BEST_EFFORT));
431 ASSERT_TRUE(cm.
switchController(stop,
start, controller_manager_msgs::SwitchControllerRequest::BEST_EFFORT));
434 std::vector<std::string>
start, stop;
435 start.push_back(
"single_pos");
436 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
441 ASSERT_TRUE(cm.
switchController(stop,
start, controller_manager_msgs::SwitchControllerRequest::STRICT));
444 std::vector<std::string>
start, stop;
445 stop.push_back(
"single_pos");
446 ASSERT_FALSE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
447 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::BEST_EFFORT));
450 std::vector<std::string>
start, stop;
451 start.push_back(
"single_pos");
452 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
453 ASSERT_FALSE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::STRICT));
454 ASSERT_TRUE(cm.
switchController(
start, stop, controller_manager_msgs::SwitchControllerRequest::BEST_EFFORT));
455 ASSERT_TRUE(cm.
switchController(stop,
start, controller_manager_msgs::SwitchControllerRequest::STRICT));
460 int main(
int argc,
char **argv)
462 testing::InitGoogleTest(&argc, argv);
463 ros::init(argc, argv,
"controller_manager_switch_test");
467 int ret = RUN_ALL_TESTS();