moveit2
The MoveIt Motion Planning Framework for ROS 2.
chomp_cost.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Mrinal Kalakrishnan */
36 
39 
40 #include <eigen3/Eigen/LU>
41 
42 using namespace Eigen;
43 using namespace std;
44 
45 namespace chomp
46 {
47 ChompCost::ChompCost(const ChompTrajectory& trajectory, int /* joint_number */,
48  const std::vector<double>& derivative_costs, double ridge_factor)
49 {
50  int num_vars_all = trajectory.getNumPoints();
51  int num_vars_free = num_vars_all - 2 * (DIFF_RULE_LENGTH - 1);
52  MatrixXd diff_matrix = MatrixXd::Zero(num_vars_all, num_vars_all);
53  quad_cost_full_ = MatrixXd::Zero(num_vars_all, num_vars_all);
54 
55  // construct the quad cost for all variables, as a sum of squared differentiation matrices
56  double multiplier = 1.0;
57  for (unsigned int i = 0; i < derivative_costs.size(); ++i)
58  {
59  multiplier *= trajectory.getDiscretization();
60  diff_matrix = getDiffMatrix(num_vars_all, &DIFF_RULES[i][0]);
61  quad_cost_full_ += (derivative_costs[i] * multiplier) * (diff_matrix.transpose() * diff_matrix);
62  }
63  quad_cost_full_ += MatrixXd::Identity(num_vars_all, num_vars_all) * ridge_factor;
64 
65  // extract the quad cost just for the free variables:
66  quad_cost_ = quad_cost_full_.block(DIFF_RULE_LENGTH - 1, DIFF_RULE_LENGTH - 1, num_vars_free, num_vars_free);
67 
68  // invert the matrix:
69  quad_cost_inv_ = quad_cost_.inverse();
70 
71  // cout << quad_cost_inv_ << endl;
72 }
73 
74 Eigen::MatrixXd ChompCost::getDiffMatrix(int size, const double* diff_rule) const
75 {
76  MatrixXd matrix = MatrixXd::Zero(size, size);
77  for (int i = 0; i < size; ++i)
78  {
79  for (int j = -DIFF_RULE_LENGTH / 2; j <= DIFF_RULE_LENGTH / 2; ++j)
80  {
81  int index = i + j;
82  if (index < 0)
83  continue;
84  if (index >= size)
85  continue;
86  matrix(i, index) = diff_rule[j + DIFF_RULE_LENGTH / 2];
87  }
88  }
89  return matrix;
90 }
91 
92 double ChompCost::getMaxQuadCostInvValue() const
93 {
94  return quad_cost_inv_.maxCoeff();
95 }
96 
97 void ChompCost::scale(double scale)
98 {
99  double inv_scale = 1.0 / scale;
100  quad_cost_inv_ *= inv_scale;
101  quad_cost_ *= scale;
102  quad_cost_full_ *= scale;
103 }
104 
105 ChompCost::~ChompCost() = default;
106 } // namespace chomp
Represents a discretized joint-space trajectory for CHOMP.
double getDiscretization() const
Gets the discretization time interval of the trajectory.
size_t getNumPoints() const
Gets the number of points in the trajectory.