smooth_collision_distance.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2017, 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 {
38 {
39  if (phi.rows() != dim_) ThrowNamed("Wrong size of phi!");
40  phi.setZero();
41  Eigen::MatrixXd J(dim_, x.size());
42  UpdateInternal(x, phi, J, false);
43 }
44 
48 {
49  if (phi.rows() != dim_) ThrowNamed("Wrong size of phi!");
50  phi.setZero();
51  J.setZero();
52  UpdateInternal(x, phi, J, true);
53 }
54 
58  bool updateJacobian)
59 {
60  if (!scene_->AlwaysUpdatesCollisionScene())
61  cscene_->UpdateCollisionObjectTransforms();
62 
63  // 1) Create vector to store CollisionProxy
64  std::vector<CollisionProxy> proxies;
65 
66  // 2) For each robot link, check against each robot link
68  {
69  AppendVector(proxies, cscene_->GetRobotToRobotCollisionDistance(robot_margin_));
70  }
71 
72  // 3) For each robot link, check against each environment link
73  AppendVector(proxies, cscene_->GetRobotToWorldCollisionDistance(world_margin_));
74 
75  // 4) Compute d, J
76  double& d = phi(0);
77  {
78  KDL::Frame arel, brel;
79  Eigen::MatrixXd J_a(6, scene_->GetKinematicTree().GetNumControlledJoints()), J_b(6, scene_->GetKinematicTree().GetNumControlledJoints());
80  for (const auto& proxy : proxies)
81  {
82  bool is_robot_to_robot = (proxy.e1 != nullptr && proxy.e2 != nullptr) && (proxy.e1->is_robot_link || proxy.e1->closest_robot_link.lock()) && (proxy.e2->is_robot_link || proxy.e2->closest_robot_link.lock());
83  double& margin = is_robot_to_robot ? robot_margin_ : world_margin_;
84 
85  if (proxy.distance < margin)
86  {
87  // Cost
88  d += std::pow((1. - proxy.distance / margin), linear_ ? 1 : 2);
89 
90  if (updateJacobian)
91  {
92  // Jacobian
93  if (proxy.e1 != nullptr)
94  {
95  arel = KDL::Frame(proxy.e1->frame.Inverse(KDL::Vector(proxy.contact1.x(), proxy.contact1.y(), proxy.contact1.z())));
96  J_a = scene_->GetKinematicTree().Jacobian(proxy.e1, arel, nullptr, KDL::Frame());
97  }
98  else
99  {
100  arel = arel.Identity();
101  J_a.setZero();
102  }
103 
104  if (proxy.e2 != nullptr)
105  {
106  brel = KDL::Frame(proxy.e2->frame.Inverse(KDL::Vector(proxy.contact2.x(), proxy.contact2.y(), proxy.contact2.z())));
107  J_b = scene_->GetKinematicTree().Jacobian(proxy.e2, brel, nullptr, KDL::Frame());
108  }
109  else
110  {
111  brel = brel.Identity();
112  J_b.setZero();
113  }
114 
115  if (!linear_)
116  {
117  J += (2. / (margin * margin)) * (proxy.normal1.transpose() * J_a.topRows<3>());
118  J -= (2. / (margin * margin)) * (proxy.normal1.transpose() * J_b.topRows<3>());
119  }
120  else
121  {
122  J += 1 / margin * (proxy.normal1.transpose() * J_a.topRows<3>());
123  J -= 1 / margin * (proxy.normal1.transpose() * J_b.topRows<3>());
124  }
125  }
126  }
127  }
128  }
129 }
130 
132 {
133  scene_ = scene;
134  Initialize();
135 }
136 
138 {
139  cscene_ = scene_->GetCollisionScene();
140  world_margin_ = parameters_.WorldMargin;
141  robot_margin_ = parameters_.RobotMargin;
142  linear_ = parameters_.Linear;
143  check_self_collision_ = parameters_.CheckSelfCollision;
144 
145  if (robot_margin_ == 0.0 || world_margin_ == 0.0) ThrowPretty("Setting the margin to zero is a bad idea. It will NaN.");
146 
147  if (debug_) HIGHLIGHT_NAMED("Smooth Collision Distance",
148  "World Margin: " << world_margin_ << " Robot Margin: " << robot_margin_ << "\t Linear: " << linear_);
149 }
150 
152 } // namespace exotica
d
void AssignScene(ScenePtr scene) override
Eigen::Ref< Eigen::VectorXd > VectorXdRef
#define ThrowPretty(m)
std::shared_ptr< Scene > ScenePtr
#define ThrowNamed(m)
void Update(Eigen::VectorXdRefConst x, Eigen::VectorXdRef phi) override
const Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
void UpdateInternal(Eigen::VectorXdRefConst x, Eigen::VectorXdRef phi, Eigen::MatrixXdRef J, bool updateJacobian=true)
#define HIGHLIGHT_NAMED(name, x)
Eigen::Ref< Eigen::MatrixXd > MatrixXdRef
static Frame Identity()
REGISTER_TASKMAP_TYPE("SmoothCollisionDistance", exotica::SmoothCollisionDistance)
void AppendVector(std::vector< Val > &orig, const std::vector< Val > &extra)


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