joint_velocity_limit.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018, Wolfgang Merkt
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
31 
33 
34 namespace exotica
35 {
37 {
38  kinematics.resize(2); // Request kinematics for current x_t and x_{t-1}
39 }
40 
42 
44 {
45  scene_ = scene;
46  Initialize();
47 }
48 
50 {
51  double percent = static_cast<double>(parameters_.SafePercentage);
52 
53  N = scene_->GetKinematicTree().GetNumControlledJoints();
54  dt_ = std::abs(parameters_.dt);
55  if (dt_ == 0.0)
56  ThrowNamed("Timestep dt needs to be greater than 0");
57 
58  if (parameters_.MaximumJointVelocity.rows() == 1)
59  {
60  limits_.setOnes(N);
61  limits_ *= std::abs(static_cast<double>(parameters_.MaximumJointVelocity(0)));
62  }
63  else if (parameters_.MaximumJointVelocity.rows() == N)
64  {
65  limits_ = parameters_.MaximumJointVelocity.cwiseAbs();
66  }
67  else
68  {
69  ThrowNamed("Maximum joint velocity vector needs to be either of size 1 or N, but got " << parameters_.MaximumJointVelocity.rows());
70  }
71 
72  tau_ = percent * limits_;
73 
74  if (debug_) HIGHLIGHT_NAMED("JointVelocityLimit", "dt=" << dt_ << ", q̇_max=" << limits_.transpose() << ", τ=" << tau_.transpose());
75 }
76 
78 {
79  return N;
80 }
81 
83 {
84  if (kinematics.size() != 2) ThrowNamed("Wrong size of kinematics - requires 2, but got " << kinematics.size());
85  if (phi.rows() != N) ThrowNamed("Wrong size of phi!");
86  if (!x.isApprox(kinematics[0].X)) ThrowNamed("The internal kinematics.X and passed state reference x do not match!\n x=" << std::setprecision(6) << x.transpose() << "\n X=" << kinematics[0].X.transpose() << "\n diff=" << (x - kinematics[0].X).transpose());
87 
88  phi.setZero();
89  Eigen::VectorXd x_diff = (1 / dt_) * (kinematics[0].X - kinematics[1].X);
90  for (int i = 0; i < N; ++i)
91  {
92  if (x_diff(i) < -limits_(i) + tau_(i))
93  {
94  phi(i) = x_diff(i) + limits_(i) - tau_(i);
95  if (debug_) HIGHLIGHT_NAMED("JointVelocityLimit", "Lower limit exceeded (joint=" << i << "): " << x_diff(i) << " < (-" << limits_(i) << "+" << tau_(i) << ")");
96  }
97  if (x_diff(i) > limits_(i) - tau_(i))
98  {
99  phi(i) = x_diff(i) - limits_(i) + tau_(i);
100  if (debug_) HIGHLIGHT_NAMED("JointVelocityLimit", "Upper limit exceeded (joint=" << i << "): " << x_diff(i) << " > (" << limits_(i) << "-" << tau_(i) << ")");
101  }
102  }
103 }
104 
106 {
107  if (jacobian.rows() != N || jacobian.cols() != N) ThrowNamed("Wrong size of jacobian! " << N);
108  Update(x, phi);
109  jacobian = (1 / dt_) * Eigen::MatrixXd::Identity(N, N);
110  for (int i = 0; i < N; ++i)
111  if (phi(i) == 0.0)
112  jacobian(i, i) = 0.0;
113 }
114 } // namespace exotica
double dt_
Timestep between subsequent time-steps (in s)
Eigen::VectorXd tau_
Joint velocity limits tolerance.
Eigen::VectorXd limits_
Joint velocity limits (absolute, in rads/s)
Eigen::Ref< Eigen::VectorXd > VectorXdRef
void Update(Eigen::VectorXdRefConst x, Eigen::VectorXdRef phi) override
Joint Velocity Limit taskmap for time-indexed problems. Penalisations of joint velocity limit violati...
std::shared_ptr< Scene > ScenePtr
#define ThrowNamed(m)
void AssignScene(ScenePtr scene) override
const Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
#define HIGHLIGHT_NAMED(name, x)
Eigen::Ref< Eigen::MatrixXd > MatrixXdRef
REGISTER_TASKMAP_TYPE("JointVelocityLimit", exotica::JointVelocityLimit)


exotica_core_task_maps
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sat Apr 10 2021 02:36:09