collision_distance.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  if (phi.rows() != dim_) ThrowNamed("Wrong size of phi!");
39  phi.setZero();
40  Eigen::MatrixXd J(dim_, dim_);
41  UpdateInternal(x, phi, J, false);
42 }
43 
45 {
46  if (phi.rows() != dim_) ThrowNamed("Wrong size of phi!");
47  phi.setZero();
48  J.setZero();
49  UpdateInternal(x, phi, J, true);
50 }
51 
53 {
54  if (!scene_->AlwaysUpdatesCollisionScene())
55  cscene_->UpdateCollisionObjectTransforms();
56 
57  // For all robot links: Get all collision distances, sort by distance, and process the closest.
58  for (int i = 0; i < dim_; ++i)
59  {
60  std::vector<CollisionProxy> proxies = cscene_->GetCollisionDistance(controlled_joint_to_collision_link_map_[robot_joints_[i]], check_self_collision_);
61  if (proxies.size() == 0)
62  {
63  // phi(i) = 0;
64  // J.row(i).setZero();
65  continue;
66  }
67 
68  CollisionProxy& closest_proxy = closest_proxies_[i];
69  closest_proxy.distance = std::numeric_limits<double>::max();
70  for (const auto& tmp_proxy : proxies)
71  {
72  const bool is_robot_to_robot = (tmp_proxy.e1->is_robot_link || tmp_proxy.e1->closest_robot_link.lock()) && (tmp_proxy.e2->is_robot_link || tmp_proxy.e2->closest_robot_link.lock());
73  const double& margin = is_robot_to_robot ? robot_margin_ : world_margin_;
74  if ((tmp_proxy.distance - margin) < closest_proxy.distance)
75  {
76  closest_proxy = tmp_proxy;
77  closest_proxy.distance -= margin;
78  }
79  }
80 
81  phi(i) = closest_proxy.distance;
82 
83  if (updateJacobian)
84  {
85  KDL::Frame arel = KDL::Frame(closest_proxy.e1->frame.Inverse(KDL::Vector(
86  closest_proxy.contact1(0), closest_proxy.contact1(1), closest_proxy.contact1(2))));
87  KDL::Frame brel = KDL::Frame(closest_proxy.e2->frame.Inverse(KDL::Vector(
88  closest_proxy.contact2(0), closest_proxy.contact2(1), closest_proxy.contact2(2))));
89 
90  Eigen::MatrixXd tmpJ = scene_->GetKinematicTree().Jacobian(
91  closest_proxy.e1, arel, nullptr, KDL::Frame());
92  J.row(i) += (closest_proxy.normal1.transpose() * tmpJ.topRows<3>());
93  tmpJ = scene_->GetKinematicTree().Jacobian(closest_proxy.e2, brel, nullptr,
94  KDL::Frame());
95  J.row(i) -= (closest_proxy.normal1.transpose() * tmpJ.topRows<3>());
96  }
97  }
98 
99  J *= -1; // yup, this is was wrong since forever.
100 }
101 
103 {
104  scene_ = scene;
105  Initialize();
106 }
107 
109 {
110  cscene_ = scene_->GetCollisionScene();
111  check_self_collision_ = parameters_.CheckSelfCollision;
112  world_margin_ = parameters_.WorldMargin;
113  robot_margin_ = parameters_.RobotMargin;
114 
115  // Get names of all controlled joints and their corresponding child links
116  robot_joints_ = scene_->GetControlledJointNames();
117  controlled_joint_to_collision_link_map_ = scene_->GetControlledJointToCollisionLinkMap();
118  dim_ = static_cast<int>(robot_joints_.size());
120  if (debug_)
121  {
122  HIGHLIGHT_NAMED("Collision Distance", "Dimension: " << dim_
123  << " - CheckSelfCollision: " << check_self_collision_
124  << "World Margin: " << world_margin_
125  << " Robot Margin: " << robot_margin_);
126  }
127 }
128 
130 } // namespace exotica
std::vector< std::string > robot_joints_
void AssignScene(ScenePtr scene) override
Eigen::Ref< Eigen::VectorXd > VectorXdRef
std::vector< CollisionProxy > closest_proxies_
void Update(Eigen::VectorXdRefConst x, Eigen::VectorXdRef phi) override
REGISTER_TASKMAP_TYPE("CollisionDistance", exotica::CollisionDistance)
std::shared_ptr< Scene > ScenePtr
#define ThrowNamed(m)
Eigen::Vector3d contact1
Eigen::Vector3d contact2
void UpdateInternal(Eigen::VectorXdRefConst x, Eigen::VectorXdRef phi, Eigen::MatrixXdRef J, bool updateJacobian=true)
const Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
#define HIGHLIGHT_NAMED(name, x)
Eigen::Ref< Eigen::MatrixXd > MatrixXdRef
std::map< std::string, std::vector< std::string > > controlled_joint_to_collision_link_map_
std::shared_ptr< KinematicElement > e2
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