moveit2
The MoveIt Motion Planning Framework for ROS 2.
kinematic_terms.cpp
Go to the documentation of this file.
1 #include <Eigen/Geometry>
2 #include <boost/format.hpp>
3 
4 #include <trajopt_sco/expr_ops.hpp>
5 #include <trajopt_sco/modeling_utils.hpp>
6 
8 
9 using namespace std;
10 using namespace sco;
11 using namespace Eigen;
12 
13 namespace trajopt_interface
14 {
15 VectorXd CartPoseErrCalculator::operator()(const VectorXd& dof_vals) const
16 {
17  // TODO: create the actual error function from information in planning scene
18  VectorXd err;
19  return err;
20 }
21 
22 VectorXd JointVelErrCalculator::operator()(const VectorXd& var_vals) const
23 {
24  assert(var_vals.rows() % 2 == 0);
25  // var_vals = (theta_t1, theta_t2, theta_t3 ... 1/dt_1, 1/dt_2, 1/dt_3 ...)
26  int half = static_cast<int>(var_vals.rows() / 2);
27  int num_vels = half - 1;
28  // (x1-x0)*(1/dt)
29  VectorXd vel = (var_vals.segment(1, num_vels) - var_vals.segment(0, num_vels)).array() *
30  var_vals.segment(half + 1, num_vels).array();
31 
32  // Note that for equality terms tols are 0, so error is effectively doubled
33  VectorXd result(vel.rows() * 2);
34  result.topRows(vel.rows()) = -(upper_tol_ - (vel.array() - target_));
35  result.bottomRows(vel.rows()) = lower_tol_ - (vel.array() - target_);
36  return result;
37 }
38 
39 MatrixXd JointVelJacobianCalculator::operator()(const VectorXd& var_vals) const
40 {
41  // var_vals = (theta_t1, theta_t2, theta_t3 ... 1/dt_1, 1/dt_2, 1/dt_3 ...)
42  int num_vals = static_cast<int>(var_vals.rows());
43  int half = num_vals / 2;
44  int num_vels = half - 1;
45  MatrixXd jac = MatrixXd::Zero(num_vels * 2, num_vals);
46 
47  for (int i = 0; i < num_vels; ++i)
48  {
49  // v = (j_i+1 - j_i)*(1/dt)
50  // We calculate v with the dt from the second pt
51  int time_index = i + half + 1;
52  jac(i, i) = -1.0 * var_vals(time_index);
53  jac(i, i + 1) = 1.0 * var_vals(time_index);
54  jac(i, time_index) = var_vals(i + 1) - var_vals(i);
55  // All others are 0
56  }
57 
58  // bottom half is negative velocities
59  jac.bottomRows(num_vels) = -jac.topRows(num_vels);
60 
61  return jac;
62 }
63 
64 } // namespace trajopt_interface