variable_size_collision_distance.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020, University of Oxford
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 
32 REGISTER_TASKMAP_TYPE("VariableSizeCollisionDistance", exotica::VariableSizeCollisionDistance);
33 
34 namespace exotica
35 {
37 {
38  if (phi.rows() != static_cast<int>(dim_)) ThrowNamed("Wrong size of phi!");
39  Eigen::MatrixXd J;
40  UpdateInternal(x, phi, J, false);
41 }
42 
44 {
45  if (phi.rows() != static_cast<int>(dim_)) ThrowNamed("Wrong size of phi!");
46  if (J.rows() != static_cast<int>(dim_) || J.cols() != scene_->GetKinematicTree().GetNumControlledJoints()) ThrowNamed("Wrong size of Jacobian!");
47  UpdateInternal(x, phi, J, true);
48 }
49 
51 {
52  std::vector<CollisionProxy> proxies = cscene_->GetRobotToWorldCollisionDistance(world_margin_);
53 
54  // Figure out if dim_ or size of proxies is larger:
55  if (proxies.size() > dim_) WARNING("Too many proxies!");
56  int max_dim = std::min(proxies.size(), dim_);
57 
58  KDL::Frame arel, brel;
59  Eigen::MatrixXd J_a(6, scene_->GetKinematicTree().GetNumControlledJoints()), J_b(6, scene_->GetKinematicTree().GetNumControlledJoints());
60  for (int i = 0; i < max_dim; ++i)
61  {
62  const CollisionProxy& proxy = proxies[i];
63 
64  if (proxy.distance > world_margin_)
65  {
66  // Zero. Do nothing.
67  }
68  else
69  {
70  phi(i) = world_margin_ - proxy.distance;
71 
72  if (updateJacobian)
73  {
74  // Jacobian
75  if (proxy.e1 != nullptr)
76  {
77  arel = KDL::Frame(proxy.e1->frame.Inverse(KDL::Vector(proxy.contact1.x(), proxy.contact1.y(), proxy.contact1.z())));
78  J_a = scene_->GetKinematicTree().Jacobian(proxy.e1, arel, nullptr, KDL::Frame());
79  }
80  else
81  {
82  arel = arel.Identity();
83  J_a.setZero();
84  }
85 
86  if (proxy.e2 != nullptr)
87  {
88  brel = KDL::Frame(proxy.e2->frame.Inverse(KDL::Vector(proxy.contact2.x(), proxy.contact2.y(), proxy.contact2.z())));
89  J_b = scene_->GetKinematicTree().Jacobian(proxy.e2, brel, nullptr, KDL::Frame());
90  }
91  else
92  {
93  brel = brel.Identity();
94  J_b.setZero();
95  }
96 
97  J.row(i) += proxy.normal1.transpose() * J_a.topRows<3>();
98  J.row(i) -= proxy.normal1.transpose() * J_b.topRows<3>();
99  }
100  }
101  }
102 }
103 
105 {
106  scene_ = scene;
107  Initialize();
108 }
109 
111 {
112  cscene_ = scene_->GetCollisionScene();
113  world_margin_ = parameters_.WorldMargin;
114 
115  dim_ = static_cast<int>(parameters_.Dimension);
116  if (dim_ <= 0) ThrowNamed("Dimension needs to be greater than equal to 1, given: " << dim_);
117 
118  if (debug_)
119  {
120  HIGHLIGHT_NAMED("Variable Size Collision Distance", "Dimension: " << dim_ << " - World Margin: " << world_margin_);
121  }
122 }
123 
125 } // namespace exotica
Eigen::Ref< Eigen::VectorXd > VectorXdRef
std::shared_ptr< Scene > ScenePtr
#define ThrowNamed(m)
Eigen::Vector3d contact1
Eigen::Vector3d contact2
VariableSizeCollisionDistanceInitializer parameters_
const Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
#define HIGHLIGHT_NAMED(name, x)
Eigen::Ref< Eigen::MatrixXd > MatrixXdRef
REGISTER_TASKMAP_TYPE("VariableSizeCollisionDistance", exotica::VariableSizeCollisionDistance)
void UpdateInternal(Eigen::VectorXdRefConst x, Eigen::VectorXdRef phi, Eigen::MatrixXdRef J, bool updateJacobian=true)
void Update(Eigen::VectorXdRefConst x, Eigen::VectorXdRef phi) override
std::shared_ptr< KinematicElement > e2
#define WARNING(x)
std::shared_ptr< KinematicElement > e1
Eigen::Vector3d normal1


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