37 #include <boost/foreach.hpp>
47 bool CombinedRobotHWSim::initSim(
const std::string& robot_namespace,
ros::NodeHandle model_nh,
48 gazebo::physics::ModelPtr parent_model,
const urdf::Model*
const urdf_model,
49 std::vector<transmission_interface::TransmissionInfo> transmissions)
52 std::vector<std::string> robots;
53 std::string param_name =
"robot_hardware";
54 if (!model_nh.
getParam(param_name, robots))
61 BOOST_FOREACH (std::string& name, robots)
63 ROS_DEBUG(
"Will load robot HW '%s'", name.c_str());
71 catch (std::exception
const& e)
73 ROS_ERROR(
"Exception thrown while constructing nodehandle for robot HW with name '%s':\n%s", name.c_str(),
83 ROS_ERROR(
"Could not load robot HW '%s' because the type was not specified. Did you load the robot HW "
84 "configuration on the parameter server (namespace: '%s')?",
92 robot_hw = class_loader_.createInstance(type);
96 ROS_ERROR(
"Could not load class %s: %s", type.c_str(), ex.what());
101 ROS_DEBUG(
"Initializing robot HW '%s'", name.c_str());
104 robot_hw->initSim(robot_namespace, c_nh, parent_model, urdf_model, transmissions);
106 catch (std::exception& e)
108 ROS_ERROR(
"Exception thrown while initializing robot HW %s.\n%s", name.c_str(), e.what());
113 robot_hw_list_.push_back(robot_hw);
114 this->registerInterfaceManager(robot_hw.get());
115 ROS_DEBUG(
"Successfully load robot HW '%s'", name.c_str());
121 bool CombinedRobotHWSim::prepareSwitch(
const std::list<hardware_interface::ControllerInfo>& start_list,
122 const std::list<hardware_interface::ControllerInfo>& stop_list)
127 std::list<hardware_interface::ControllerInfo> filtered_start_list;
128 std::list<hardware_interface::ControllerInfo> filtered_stop_list;
130 filterControllerList(start_list, filtered_start_list, hw);
131 filterControllerList(stop_list, filtered_stop_list, hw);
133 if (!hw->prepareSwitch(filtered_start_list, filtered_stop_list))
139 void CombinedRobotHWSim::doSwitch(
const std::list<hardware_interface::ControllerInfo>& start_list,
140 const std::list<hardware_interface::ControllerInfo>& stop_list)
145 std::list<hardware_interface::ControllerInfo> filtered_start_list;
146 std::list<hardware_interface::ControllerInfo> filtered_stop_list;
149 filterControllerList(start_list, filtered_start_list, hw);
150 filterControllerList(stop_list, filtered_stop_list, hw);
152 hw->doSwitch(filtered_start_list, filtered_stop_list);
161 hw->readSim(time, period);
170 hw->writeSim(time, period);
174 void CombinedRobotHWSim::filterControllerList(
const std::list<hardware_interface::ControllerInfo>& list,
175 std::list<hardware_interface::ControllerInfo>& filtered_list,
178 filtered_list.clear();
179 for (std::list<hardware_interface::ControllerInfo>::const_iterator it = list.begin(); it != list.end(); ++it)
182 filtered_controller.
name = it->name;
183 filtered_controller.
type = it->type;
185 if (it->claimed_resources.empty())
187 filtered_list.push_back(filtered_controller);
190 for (std::vector<hardware_interface::InterfaceResources>::const_iterator res_it = it->claimed_resources.begin();
191 res_it != it->claimed_resources.end(); ++res_it)
195 std::vector<std::string> r_hw_ifaces = robot_hw->getNames();
197 std::vector<std::string>::iterator if_name =
199 if (if_name == r_hw_ifaces.end())
204 std::vector<std::string> r_hw_iface_resources =
206 std::set<std::string> filtered_resources;
207 for (std::set<std::string>::const_iterator ctrl_res = res_it->resources.begin();
208 ctrl_res != res_it->resources.end(); ++ctrl_res)
210 std::vector<std::string>::iterator res_name =
211 std::find(r_hw_iface_resources.begin(), r_hw_iface_resources.end(), *ctrl_res);
212 if (res_name != r_hw_iface_resources.end())
214 filtered_resources.insert(*ctrl_res);
217 filtered_iface_resources.
resources = filtered_resources;
220 filtered_list.push_back(filtered_controller);