23 #include <sensor_msgs/JointState.h> 31 ROS_ERROR(
"Parameter 'joint_names' not set");
37 ROS_ERROR(
"Parameter 'chain_base_link' not set");
43 ROS_ERROR(
"Parameter 'chain_tip_link' not set");
51 ROS_ERROR(
"Failed to construct kdl tree");
58 ROS_ERROR(
"Failed to initialize kinematic chain");
80 double roll, pitch, yaw;
84 geometry_msgs::TransformStamped offset_transform_msg;
92 message =
"Failed to lookupTransform pointing_frame: " + std::string(ex.what());
103 message =
"Preempted after lookup 'pointing_frame'";
116 offset.
M.
GetRPY(roll, pitch, yaw);
121 KDL::Chain chain_base, chain_lookat, chain_full;
123 double mod_neg_factor = 1.0;
124 switch (goal->pointing_axis_type)
126 case cob_lookat_action::LookAtGoal::X_POSITIVE:
129 case cob_lookat_action::LookAtGoal::Y_POSITIVE:
132 case cob_lookat_action::LookAtGoal::Z_POSITIVE:
135 case cob_lookat_action::LookAtGoal::X_NEGATIVE:
137 mod_neg_factor = -1.0;
139 case cob_lookat_action::LookAtGoal::Y_NEGATIVE:
141 mod_neg_factor = -1.0;
143 case cob_lookat_action::LookAtGoal::Z_NEGATIVE:
145 mod_neg_factor = -1.0;
148 ROS_ERROR(
"PointingAxisType %d not defined! Using default: 'X_POSITIVE'!", goal->pointing_axis_type);
155 KDL::Segment offset_link(
"offset_link", offset_joint, offset);
159 KDL::Joint lookat_lin_joint(
"lookat_lin_joint", lookat_lin_joint_type);
160 KDL::Segment lookat_rotx_link(
"lookat_rotx_link", lookat_lin_joint);
164 KDL::Segment lookat_roty_link(
"lookat_roty_link", lookat_rotx_joint);
168 KDL::Segment lookat_rotz_link(
"lookat_rotz_link", lookat_roty_joint);
172 KDL::Segment lookat_focus_frame(
"lookat_focus_frame", lookat_rotz_joint);
181 if (goal->base_active)
183 chain_full = chain_base;
205 geometry_msgs::TransformStamped transform_in_msg;
213 message =
"Failed to lookupTransform target_frame: " + std::string(ex.what());
224 message =
"Preempted after lookup 'target_frame'";
238 p_in.
M.
GetRPY(roll, pitch, yaw);
239 ROS_DEBUG_STREAM(
"p_in.p: " << p_in.
p.
x() <<
", " << p_in.
p.
y() <<
", " << p_in.
p.
z());
240 ROS_DEBUG_STREAM(
"p_in.rot: " << roll <<
", " << pitch <<
", " << yaw);
245 ROS_DEBUG_STREAM(
"q_init: " << q_init.data);
246 ROS_DEBUG_STREAM(
"q_out: " << q_out.data);
267 p_out.
M.
GetRPY(roll, pitch, yaw);
268 ROS_DEBUG_STREAM(
"p_out.p: " << p_out.
p.
x() <<
", " << p_out.
p.
y() <<
", " << p_out.
p.
z());
269 ROS_DEBUG_STREAM(
"p_out.rot: " << roll <<
", " << pitch <<
", " << yaw);
284 ROS_DEBUG_STREAM(
"p_diff: " << v_diff.
x() <<
", " << v_diff.
y() <<
", " << v_diff.
z());
285 ROS_DEBUG_STREAM(
"NORM v_diff: " << v_diff.
Norm());
289 ROS_DEBUG_STREAM(
"P_IN !Equal P_OUT");
293 unsigned int k = (goal->base_active) ? 1 : 0;
319 if (goal->base_active)
325 p_out_main_offset = p_out_main*offset;
329 ROS_DEBUG_STREAM(
"q_out_main: " << q_out_main.data);
330 p_out_main.
M.
GetRPY(roll, pitch, yaw);
331 ROS_DEBUG_STREAM(
"p_out_main.p: " << p_out_main.
p.
x() <<
", " << p_out_main.
p.
y() <<
", " << p_out_main.
p.
z());
332 ROS_DEBUG_STREAM(
"p_out_main.rot: " << roll <<
", " << pitch <<
", " << yaw);
333 offset.
M.
GetRPY(roll, pitch, yaw);
334 ROS_DEBUG_STREAM(
"offset.p: " << offset.
p.
x() <<
", " << offset.
p.
y() <<
", " << offset.
p.
z());
335 ROS_DEBUG_STREAM(
"offset.rot: " << roll <<
", " << pitch <<
", " << yaw);
336 p_out_main_offset.
M.
GetRPY(roll, pitch, yaw);
337 ROS_DEBUG_STREAM(
"p_out_main_offset.p: " << p_out_main_offset.
p.
x() <<
", " << p_out_main_offset.
p.
y() <<
", " << p_out_main_offset.
p.
z());
338 ROS_DEBUG_STREAM(
"p_out_main_offset.rot: " << roll <<
", " << pitch <<
", " << yaw);
339 tip2target.
M.
GetRPY(roll, pitch, yaw);
340 ROS_DEBUG_STREAM(
"tip2target.p: " << tip2target.
p.
x() <<
", " << tip2target.
p.
y() <<
", " << tip2target.
p.
z());
341 ROS_DEBUG_STREAM(
"tip2target.rot: " << roll <<
", " << pitch <<
", " << yaw);
345 double q_lookat_lin = 0.0;
346 switch (goal->pointing_axis_type)
348 case cob_lookat_action::LookAtGoal::X_POSITIVE:
349 q_lookat_lin = tip2target.
p.
x();
350 tip2target_test.
p.
x(0.0);
352 case cob_lookat_action::LookAtGoal::Y_POSITIVE:
353 q_lookat_lin = tip2target.
p.
y();
354 tip2target_test.
p.
y(0.0);
356 case cob_lookat_action::LookAtGoal::Z_POSITIVE:
357 q_lookat_lin = tip2target.
p.
z();
358 tip2target_test.
p.
z(0.0);
360 case cob_lookat_action::LookAtGoal::X_NEGATIVE:
361 q_lookat_lin = tip2target.
p.
x();
362 tip2target_test.
p.
x(0.0);
364 case cob_lookat_action::LookAtGoal::Y_NEGATIVE:
365 q_lookat_lin = tip2target.
p.
y();
366 tip2target_test.
p.
y(0.0);
368 case cob_lookat_action::LookAtGoal::Z_NEGATIVE:
369 q_lookat_lin = tip2target.
p.
z();
370 tip2target_test.
p.
z(0.0);
373 ROS_ERROR(
"PointingAxisType %d not defined! Using default: 'X_POSITIVE'!", goal->pointing_axis_type);
374 q_lookat_lin = tip2target.
p.
x();
375 tip2target_test.
p.
x(0.0);
380 ROS_DEBUG_STREAM(
"q_lookat_lin: " << q_lookat_lin);
381 if (mod_neg_factor*q_lookat_lin < 0.0)
384 message =
"q_lookat_lin is negative";
393 ROS_DEBUG_STREAM(
"tip2target_test: " << tip2target_test.
p.
x() <<
", " << tip2target_test.
p.
y() <<
", " << tip2target_test.
p.
z());
394 ROS_DEBUG_STREAM(
"tip2target_test.p.Norm: " << tip2target_test.
p.
Norm());
395 if (tip2target_test.
p.
Norm() > 0.1)
398 message =
"Tip2Target is not lookat-conform";
407 control_msgs::FollowJointTrajectoryGoal fjt_goal;
411 trajectory_msgs::JointTrajectoryPoint traj_point;
415 traj_point.velocities.push_back(0.0);
416 traj_point.accelerations.push_back(0.0);
422 component_name.erase(std::remove(component_name.begin(), component_name.end(),
'/'), component_name.end());
423 float default_vel, default_acc;
424 nh_.
param<
float>(
"/script_server/"+component_name+
"/default_vel", default_vel, 0.1);
425 nh_.
param<
float>(
"/script_server/"+component_name+
"/default_acc", default_acc, 1.0);
427 sensor_msgs::JointStateConstPtr js = ros::topic::waitForMessage<sensor_msgs::JointState>(
"/"+component_name+
"/joint_states",
ros::Duration(1.0));
430 std::vector<double> d_pos;
431 for (
unsigned int i=0; i<std::min(js->position.size(), traj_point.positions.size()); i++)
433 d_pos.push_back(std::fabs(js->position[i]-traj_point.positions[i]));
435 double d_max = *std::max_element(d_pos.begin(), d_pos.end());
436 double t1 = default_vel / default_acc;
437 double s1 = default_acc / 2*std::pow(t1,2);
440 double s2 = d_max - 2*s1;
441 double t2 = s2 / default_vel;
446 t = std::sqrt(d_max / default_acc);
449 double time_from_start = std::max(t,0.4);
451 fjt_goal.trajectory.points.push_back(traj_point);
473 ROS_DEBUG_STREAM(
"FJT-Goal: " << fjt_goal);
476 move_base_msgs::MoveBaseGoal mbl_goal;
477 if (goal->base_active)
482 geometry_msgs::TransformStamped base_transform_msg;
485 base_transform_msg =
tf_buffer_->lookupTransform(
"odom_combined",
"base_link",
ros::Time(0));
490 message =
"Failed to lookupTransform base_link: " + std::string(ex.what());
501 message =
"Preempted after lookup 'base_link'";
512 base_target.
M.
GetRPY(roll, pitch, yaw);
513 ROS_DEBUG_STREAM(
"base_target.p: " << base_target.
p.
x() <<
", " << base_target.
p.
y() <<
", " << base_target.
p.
z());
514 ROS_DEBUG_STREAM(
"base_target.rot: " << roll <<
", " << pitch <<
", " << yaw);
516 mbl_goal.target_pose.header.frame_id =
"odom_combined";
517 mbl_goal.target_pose.header.stamp =
ros::Time(0);
518 mbl_goal.target_pose.pose.position.x = base_target.
p.
x();
519 mbl_goal.target_pose.pose.position.y = base_target.
p.
y();
520 mbl_goal.target_pose.pose.position.z = base_target.
p.
z();
521 base_target.
M.
GetQuaternion(mbl_goal.target_pose.pose.orientation.x,
522 mbl_goal.target_pose.pose.orientation.y,
523 mbl_goal.target_pose.pose.orientation.z,
524 mbl_goal.target_pose.pose.orientation.w);
525 ROS_DEBUG_STREAM(
"MBL-Goal: " << mbl_goal);
531 message =
"FJT server not connected";
541 message =
"MBL server not connected";
554 bool fjt_finished =
false;
556 bool mbl_finished =
false;
564 message =
"FJT State: " + fjt_state.
toString();
568 if (!mbl_finished && goal->base_active)
572 message =
"MBJ State: " + mbl_state.
toString();
578 ((mbl_finished && goal->base_active) || !goal->base_active))
580 if (goal->base_active)
582 message =
"Action finished:\n\tFJT-State: " + fjt_state.
toString() +
", FJT-ErrorCode: " + std::to_string(
fjt_ac_->
getResult()->error_code) +
"\n\tMBL-State: " + mbl_state.
toString();
586 message =
"Action finished:\n\tFJT-State: " + fjt_state.
toString() +
", FJT-ErrorCode: " + std::to_string(
fjt_ac_->
getResult()->error_code);
616 message =
"Preempted during execution";
625 message =
"Lookat finished successful.";
IMETHOD doubleVel diff(const doubleVel &a, const doubleVel &b, double dt=1.0)
std::shared_ptr< KDL::ChainFkSolverPos_recursive > fk_solver_pos_
bool waitForResult(const ros::Duration &timeout=ros::Duration(0, 0))
void addSegment(const Segment &segment)
std::shared_ptr< tf2_ros::Buffer > tf_buffer_
std::shared_ptr< KDL::ChainIkSolverPos_LMA > ik_solver_pos_
std::string chain_base_link_
std::string chain_tip_link_
actionlib::SimpleActionClient< control_msgs::FollowJointTrajectoryAction > * fjt_ac_
std::shared_ptr< tf2_ros::TransformListener > tf_listener_
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
std::vector< std::string > joint_names_
ros::Duration buffer_duration_
bool isServerConnected() const
void GetQuaternion(double &x, double &y, double &z, double &w) const
double Norm(double eps=epsilon) const
std::string toString() const
IMETHOD bool Equal(const FrameAcc &r1, const FrameAcc &r2, double eps=epsilon)
void setPreempted(const Result &result=Result(), const std::string &text=std::string(""))
static Rotation RPY(double roll, double pitch, double yaw)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
cob_lookat_action::LookAtResult lookat_res_
void transformMsgToKDL(const geometry_msgs::Transform &m, KDL::Frame &k)
const std::string & getNamespace() const
void addChain(const Chain &chain)
bool isPreemptRequested()
void goalCB(const cob_lookat_action::LookAtGoalConstPtr &goal)
unsigned int getNrOfJoints() const
def normalize_angle(angle)
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
void GetRPY(double &roll, double &pitch, double &yaw) const
actionlib::SimpleActionClient< move_base_msgs::MoveBaseAction > * mbl_ac_
std::shared_ptr< KDL::ChainFkSolverPos_recursive > fk_solver_pos_main_
void setAborted(const Result &result=Result(), const std::string &text=std::string(""))
bool getChain(const std::string &chain_root, const std::string &chain_tip, Chain &chain) const
void sendGoal(const Goal &goal, SimpleDoneCallback done_cb=SimpleDoneCallback(), SimpleActiveCallback active_cb=SimpleActiveCallback(), SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback())
KDL_PARSER_PUBLIC bool treeFromParam(const std::string ¶m, KDL::Tree &tree)
bool getParam(const std::string &key, std::string &s) const
SimpleClientGoalState getState() const
#define ROS_ERROR_STREAM(args)
ResultConstPtr getResult() const
actionlib::SimpleActionServer< cob_lookat_action::LookAtAction > * lookat_as_