kinematic_extension_lookat.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <string>
19 #include <vector>
20 #include <limits>
21 #include <algorithm>
22 #include <tf_conversions/tf_kdl.h>
25 
26 /* BEGIN KinematicExtensionLookat ********************************************************************************************/
28 {
31  if (!kdl_parser::treeFromParam("robot_description", tree))
32  {
33  ROS_ERROR("Failed to construct kdl tree");
34  return false;
35  }
36 
37  KDL::Chain chain_main;
39  if (chain_main.getNrOfJoints() == 0)
40  {
41  ROS_ERROR("Failed to initialize kinematic chain");
42  return false;
43  }
44 
45  KDL::Joint::JointType lookat_lin_joint_type = KDL::Joint::None;
47  {
48  case X_POSITIVE:
49  lookat_lin_joint_type = KDL::Joint::TransX;
50  break;
51  case Y_POSITIVE:
52  lookat_lin_joint_type = KDL::Joint::TransY;
53  break;
54  case Z_POSITIVE:
55  lookat_lin_joint_type = KDL::Joint::TransZ;
56  break;
57  case X_NEGATIVE:
58  lookat_lin_joint_type = KDL::Joint::TransX;
59  ROS_ERROR("X_NEGATIVE axis_type not supported");
60  return false;
61  case Y_NEGATIVE:
62  lookat_lin_joint_type = KDL::Joint::TransY;
63  ROS_ERROR("Y_NEGATIVE axis_type not supported");
64  return false;
65  case Z_NEGATIVE:
66  lookat_lin_joint_type = KDL::Joint::TransZ;
67  ROS_ERROR("Z_NEGATIVE axis_type not supported");
68  return false;
69  default:
70  ROS_ERROR("LookatAxisType %d not defined! Using default: 'X_POSITIVE'!", params_.lookat_offset.lookat_axis_type);
71  lookat_lin_joint_type = KDL::Joint::TransX;
72  break;
73  }
74 
75  KDL::Frame offset;
76  try
77  {
78  tf::StampedTransform offset_transform;
80  tf::transformTFToKDL(offset_transform, offset);
81  }
82  catch (tf::TransformException& ex)
83  {
84  ROS_ERROR("LookatAction: %s", ex.what());
85  ROS_WARN_STREAM("Using 'lookat_offset' instead");
88  }
89 
90  //fixed pointing offset
91  KDL::Joint offset_joint("offset_joint", KDL::Joint::None);
92  KDL::Segment offset_link("offset_link", offset_joint, offset);
93  chain_ext_.addSegment(offset_link);
94 
95  //lookat chain
96  KDL::Joint lookat_lin_joint("lookat_lin_joint", lookat_lin_joint_type);
97  KDL::Segment lookat_rotx_link("lookat_rotx_link", lookat_lin_joint);
98  chain_ext_.addSegment(lookat_rotx_link);
99  limits_ext_max_.push_back(std::numeric_limits<double>::max());
100  limits_ext_min_.push_back(-std::numeric_limits<double>::max());
101  limits_ext_vel_.push_back(5.0);
102  limits_ext_acc_.push_back(std::numeric_limits<double>::max());
103 
104  KDL::Joint lookat_rotx_joint("lookat_rotx_joint", KDL::Joint::RotX);
105  KDL::Segment lookat_roty_link("lookat_roty_link", lookat_rotx_joint);
106  chain_ext_.addSegment(lookat_roty_link);
107  // limits_ext_max_.push_back(M_PI);
108  // limits_ext_min_.push_back(-M_PI);
109  limits_ext_max_.push_back(std::numeric_limits<double>::max());
110  limits_ext_min_.push_back(-std::numeric_limits<double>::max());
111  // limits_ext_vel_.push_back(std::numeric_limits<double>::max());
112  limits_ext_vel_.push_back(M_PI);
113  limits_ext_acc_.push_back(std::numeric_limits<double>::max());
114 
115  KDL::Joint lookat_roty_joint("lookat_roty_joint", KDL::Joint::RotY);
116  KDL::Segment lookat_rotz_link("lookat_rotz_link", lookat_roty_joint);
117  chain_ext_.addSegment(lookat_rotz_link);
118  // limits_ext_max_.push_back(M_PI);
119  // limits_ext_min_.push_back(-M_PI);
120  limits_ext_max_.push_back(std::numeric_limits<double>::max());
121  limits_ext_min_.push_back(-std::numeric_limits<double>::max());
122  // limits_ext_vel_.push_back(std::numeric_limits<double>::max());
123  limits_ext_vel_.push_back(M_PI);
124  limits_ext_acc_.push_back(std::numeric_limits<double>::max());
125 
126  KDL::Joint lookat_rotz_joint("lookat_rotz_joint", KDL::Joint::RotZ);
127  KDL::Segment lookat_focus_frame("lookat_focus_frame", lookat_rotz_joint);
128  chain_ext_.addSegment(lookat_focus_frame);
129  // limits_ext_max_.push_back(M_PI);
130  // limits_ext_min_.push_back(-M_PI);
131  limits_ext_max_.push_back(std::numeric_limits<double>::max());
132  limits_ext_min_.push_back(-std::numeric_limits<double>::max());
133  // limits_ext_vel_.push_back(std::numeric_limits<double>::max());
134  limits_ext_vel_.push_back(M_PI);
135  limits_ext_acc_.push_back(std::numeric_limits<double>::max());
136 
137  chain_full_ = chain_main;
139 
142 
143  this->ext_dof_ = 4;
152 
153  integrator_.reset(new SimpsonIntegrator(ext_dof_, 0.2)); // default smoothing: 0.2
154 
156  timer_.start();
157 
158  return true;
159 }
160 
162 {
164  boost::mutex::scoped_lock lock(mutex_);
166 
167  jnt2jac_->JntToJac(joint_states_full_.current_q_ , jac_full);
168 
169  return jac_full;
170 }
171 
173 {
174  boost::mutex::scoped_lock lock(mutex_);
175  unsigned int chain_dof = joint_states.current_q_.rows();
180 
181  for (unsigned int i = 0; i< chain_dof; i++)
182  {
183  joint_states_full_.current_q_(i) = joint_states.current_q_(i);
184  joint_states_full_.last_q_(i) = joint_states.last_q_(i);
185  joint_states_full_.current_q_dot_(i) = joint_states.current_q_dot_(i);
186  joint_states_full_.last_q_dot_(i) = joint_states.last_q_dot_(i);
187  }
188  for (unsigned int i = 0; i < ext_dof_; i++)
189  {
190  joint_states_full_.current_q_(chain_dof + i) = this->joint_states_ext_.current_q_(i);
191  joint_states_full_.last_q_(chain_dof + i) = this->joint_states_ext_.last_q_(i);
194  }
195 
196  return joint_states_full_;
197 }
198 
200 {
201  LimiterParams lp = limiter_params;
202  for (unsigned int i = 0; i < ext_dof_; i++)
203  {
204  lp.limits_max.push_back(limits_ext_max_[i]);
205  lp.limits_min.push_back(limits_ext_min_[i]);
206  lp.limits_vel.push_back(limits_ext_vel_[i]);
207  lp.limits_acc.push_back(limits_ext_acc_[i]);
208  }
209  return lp;
210 }
211 
213 {
215  boost::mutex::scoped_lock lock(mutex_);
216  std::vector<double> pos;
217  std::vector<double> vel;
218 
219  for (unsigned int i = 0; i < ext_dof_; i++)
220  {
221  joint_states_ext_.current_q_dot_(i) = q_dot_ik(params_.dof + i);
222  }
223 
224  if (integrator_->updateIntegration(joint_states_ext_.current_q_dot_, joint_states_ext_.current_q_, pos, vel))
225  {
226  for (unsigned int i = 0; i < ext_dof_; i++)
227  {
230  joint_states_ext_.current_q_(i) = std::max(pos[i], 0.1); // do not look backwards
231  joint_states_ext_.current_q_dot_(i) = vel[i];
232  }
233  }
234 }
235 
237 {
238  boost::mutex::scoped_lock lock(mutex_);
239  KDL::Frame focus_frame;
240 
241  fk_solver_ext_->JntToCart(joint_states_ext_.current_q_, focus_frame);
242 
243  tf::Transform transform;
244  tf::transformKDLToTF(focus_frame, transform);
245  br_.sendTransform(tf::StampedTransform(transform, ros::Time::now(), params_.chain_tip_link, "lookat_focus_frame"));
246 }
247 
248 /* END KinematicExtensionLookat ********************************************************************************************/
std::vector< double > limits_min
virtual KDL::Jacobian adjustJacobian(const KDL::Jacobian &jac_chain)
boost::shared_ptr< KDL::ChainJntToJacSolver > jnt2jac_
tf::TransformListener tf_listener_
boost::shared_ptr< KDL::ChainFkSolverPos_recursive > fk_solver_ext_
unsigned int rows() const
void addSegment(const Segment &segment)
void transformKDLToTF(const KDL::Frame &k, tf::Transform &t)
void start()
static Rotation Quaternion(double x, double y, double z, double w)
std::vector< double > limits_ext_vel_
std::vector< double > limits_ext_min_
Rotation M
std::vector< double > limits_acc
tf::TransformBroadcaster br_
void transformTFToKDL(const tf::Transform &t, KDL::Frame &k)
const TwistControllerParams & params_
void sendTransform(const StampedTransform &transform)
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
tree
void addChain(const Chain &chain)
unsigned int getNrOfJoints() const
std::vector< double > limits_ext_max_
#define ROS_WARN_STREAM(args)
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
void resize(unsigned int newSize)
std::vector< double > limits_max
boost::shared_ptr< SimpsonIntegrator > integrator_
bool getChain(const std::string &chain_root, const std::string &chain_tip, Chain &chain) const
void SetToZero(Jacobian &jac)
KDL_PARSER_PUBLIC bool treeFromParam(const std::string &param, KDL::Tree &tree)
virtual void processResultExtension(const KDL::JntArray &q_dot_ik)
static Time now()
virtual LimiterParams adjustLimiterParams(const LimiterParams &limiter_params)
std::vector< double > limits_ext_acc_
virtual JointStates adjustJointStates(const JointStates &joint_states)
#define ROS_ERROR(...)
std::vector< double > limits_vel
void broadcastFocusFrame(const ros::TimerEvent &event)


cob_twist_controller
Author(s): Felix Messmer , Marco Bezzon , Christoph Mark , Francisco Moreno
autogenerated on Thu Apr 8 2021 02:40:00