11 if (!gnh.
hasParam(
"move_group/controller_list"))
17 gnh.
getParam(
"move_group/controller_list", controller_list);
18 for (
int i = 0; i < controller_list.
size(); i++)
20 if (!controller_list[i].hasMember(
"name"))
22 ROS_ERROR(
"name must be specifed for each controller.");
25 if (!controller_list[i].hasMember(
"joints"))
27 ROS_ERROR(
"joints must be specifed for each controller.");
33 std::string name = std::string(controller_list[i][
"name"]);
35 std::string action_ns = std::string(
"");
36 if (controller_list[i].hasMember(
"action_ns"))
38 action_ns = std::string(controller_list[i][
"action_ns"]);
43 ROS_ERROR_STREAM(
"joints for controller " << name <<
" is not specified as an array");
46 auto joints = controller_list[i][
"joints"];
48 std::string type = std::string(
"FollowJointTrajectory");
49 if (!controller_list[i].hasMember(
"type"))
51 ROS_WARN_STREAM(
"type is not specifed for controller " << name <<
", using default FollowJointTrajectory");
53 type = std::string(controller_list[i][
"type"]);
54 if (type !=
"FollowJointTrajectory")
62 for (
int j = 0; j <
cinfo_map_[name].joints.size(); ++j)
64 cinfo_map_[name].joints[j] = std::string(joints[j]);
69 ROS_ERROR_STREAM(
"Caught unknown exception while parsing controller information");
86 if (not use_action_ && intermittent_)
88 ROS_WARN(
"'intermittent' param should be true with 'use_action'. Assuming to use action'");
96 ROS_ERROR(
"get_controller_list faild. Aborted.");
103 auto cinfo = it->second;
105 for (
int i=0; i<cinfo.joints.size(); i++)
116 ros::topic::waitForMessage<sensor_msgs::JointState>(
"joint_states");
123 auto controller_name = it->first;
124 auto controller_info = it->second;
125 auto action_name = controller_name +
"/" + controller_info.action_ns;
131 auto controller_name = it->first;
132 auto controller_info = it->second;
133 auto action_name = controller_name +
"/" + controller_info.action_ns;
151 auto controller_name = it->first;
152 traj_pubs_[controller_name] = gnh.
advertise<trajectory_msgs::JointTrajectory>(controller_name +
"/command", 10);
170 auto controller_name = it->first;
201 moveit_msgs::GetPositionFK fk;
203 fk.request.header.frame_id = msg->header.frame_id;
205 fk.request.fk_link_names.clear();
206 fk.request.fk_link_names.push_back(msg->link_name);
211 if(fk.response.error_code.val != moveit_msgs::MoveItErrorCodes::SUCCESS)
214 ROS_WARN(
"****FK error %d", fk.response.error_code.val);
217 if (fk.response.pose_stamped.size() != 1)
219 for (
int i=0; i<fk.response.pose_stamped.size(); i++)
228 ROS_ERROR(
"Failed to call service /computte_fk");
236 moveit_msgs::GetPositionIK ik;
238 ik.request.ik_request.group_name = msg->group_name;
239 ik.request.ik_request.ik_link_name = msg->link_name;
240 ik.request.ik_request.robot_state.joint_state =
joint_state_;
241 ik.request.ik_request.avoid_collisions = msg->avoid_collisions;
244 geometry_msgs::PoseStamped ref_pose;
246 ref_pose.header.frame_id = msg->header.frame_id;
248 ref_pose.pose.position.x = act_pose.position.x + msg->linear_delta.x;
249 ref_pose.pose.position.y = act_pose.position.y + msg->linear_delta.y;
250 ref_pose.pose.position.z = act_pose.position.z + msg->linear_delta.z;
255 double angle =
sqrt(msg->angular_delta.x*msg->angular_delta.x +
256 msg->angular_delta.y*msg->angular_delta.y +
257 msg->angular_delta.z*msg->angular_delta.z);
259 if (fabs(angle) < DBL_EPSILON)
265 axis.
setX(msg->angular_delta.x/angle);
266 axis.
setY(msg->angular_delta.y/angle);
267 axis.
setZ(msg->angular_delta.z/angle);
274 quaternionTFToMsg(q_ref, ref_pose.pose.orientation);
276 ik.request.ik_request.pose_stamped = ref_pose;
282 ROS_ERROR(
"Failed to call service /compute_ik");
285 if (ik.response.error_code.val != moveit_msgs::MoveItErrorCodes::SUCCESS)
287 ROS_WARN(
"****IK error %d", ik.response.error_code.val);
293 auto state = ik.response.solution.joint_state;
294 geometry_msgs::PoseStamped pose_check;
298 for (
int i=0; i<state.name.size(); i++)
304 double e = fabs(state.position[i] -
joint_state_.position[j]);
313 if (error >
M_PI / 2)
321 auto controller_name = it->first;
322 auto joint_names = it->second.joints;
324 std::vector<double> positions, velocities, accelerations;
326 positions.resize(joint_names.size());
327 velocities.resize(joint_names.size());
328 accelerations.resize(joint_names.size());
330 for (
int i=0; i<joint_names.size(); i++)
332 size_t index = std::distance(state.name.begin(),
333 std::find(state.name.begin(),
334 state.name.end(), joint_names[i]));
335 if (index == state.name.size())
337 ROS_WARN_STREAM(
"Cannot find joint " << joint_names[i] <<
" in IK solution");
339 positions[i] = state.position[index];
341 accelerations[i] = 0;
343 trajectory_msgs::JointTrajectoryPoint point;
344 point.positions = positions;
345 point.velocities = velocities;
346 point.accelerations = accelerations;
351 control_msgs::FollowJointTrajectoryGoal goal;
353 goal.trajectory.header.frame_id =
"base_link";
354 goal.trajectory.joint_names = joint_names;
355 goal.trajectory.points.push_back(point);
361 trajectory_msgs::JointTrajectory traj;
363 traj.header.frame_id =
"base_link";
364 traj.joint_names = joint_names;
365 traj.points.push_back(point);
382 if (msg->name.empty() || msg->name.size() != msg->position.size())
384 ROS_WARN(
"Invalid JointState message");
388 for (
int i=0; i<msg->name.size(); i++)
399 int main(
int argc,
char **argv)
actionlib::SimpleActionClient< control_msgs::FollowJointTrajectoryAction > TrajClient
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
ros::ServiceClient ik_client_
ros::Subscriber jog_frame_sub_
TFSIMD_FORCE_INLINE void setX(tfScalar x)
TFSIMD_FORCE_INLINE void setY(tfScalar y)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
std::map< std::string, double > joint_map_
int get_controller_list()
void jog_frame_cb(jog_msgs::JogFrameConstPtr msg)
Callback function for the topic jog_frame.
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
TFSIMD_FORCE_INLINE void setZ(tfScalar z)
bool call(MReq &req, MRes &res)
geometry_msgs::PoseStamped pose_stamped_
void joint_state_cb(sensor_msgs::JointStateConstPtr msg)
Callback function for the topic joint_state.
int main(int argc, char **argv)
Main function of the node.
ros::ServiceClient fk_client_
static void quaternionMsgToTF(const geometry_msgs::Quaternion &msg, Quaternion &bt)
std::map< std::string, Controller > cinfo_map_
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
ros::Subscriber joint_state_sub_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
#define ROS_WARN_STREAM(args)
std::map< std::string, ros::Publisher > traj_pubs_
#define ROS_INFO_STREAM(args)
bool getParam(const std::string &key, std::string &s) const
ROSCPP_DECL void shutdown()
bool hasParam(const std::string &key) const
#define ROS_ERROR_STREAM(args)
ROSCPP_DECL void spinOnce()
void setRotation(const Vector3 &axis, const tfScalar &angle)
std::vector< std::string > exclude_joints_
sensor_msgs::JointState joint_state_
std::map< std::string, TrajClient * > traj_clients_