task_map.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018-2020, University of Edinburgh, 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 
30 #include <exotica_core/task_map.h>
31 
32 #include <exotica_core/frame_initializer.h>
33 #include <exotica_core/task_map_initializer.h>
34 
35 namespace exotica
36 {
38 {
39  scene_ = scene;
40 }
41 
43 {
45  TaskMapInitializer MapInitializer(init);
46  is_used = true;
47 
48  frames_.clear();
49 
50  for (Initializer& eff : MapInitializer.EndEffector)
51  {
52  FrameInitializer frame(eff);
53  frames_.push_back(KinematicFrameRequest(frame.Link, GetFrame(frame.LinkOffset), frame.Base, GetFrame(frame.BaseOffset)));
54  }
55 }
56 
57 std::vector<KinematicFrameRequest> TaskMap::GetFrames() const
58 {
59  return frames_;
60 }
61 
63 {
64  if (jacobian.rows() != TaskSpaceDim() && jacobian.cols() != q.rows())
65  ThrowNamed("Jacobian dimension mismatch!");
66 
67  if (scene_ == nullptr)
68  {
69  ThrowNamed("Scene is not initialised!");
70  }
71 
72  // Set constants
73  constexpr double h = 1e-6;
74  constexpr double h_inverse = 1.0 / h;
75 
76  // Compute x/phi using forward mapping (no jacobian)
77  Update(q, phi);
78 
79  // Setup for gradient estimate
80  Eigen::VectorXd q_backward(q.size()), phi_backward(TaskSpaceDim());
81 
82  // Backward finite differencing
83  for (int i = 0; i < jacobian.cols(); ++i)
84  {
85  // Compute and set x_backward as model state
86  q_backward = q;
87  q_backward(i) -= h;
88  scene_->GetKinematicTree().Update(q_backward);
89 
90  // Compute phi_backward using forward mapping (no jacobian)
91  Update(q_backward, phi_backward);
92 
93  // Compute gradient estimate
94  jacobian.col(i).noalias() = h_inverse * (phi - phi_backward);
95  }
96 
97  // Reset model state
98  scene_->GetKinematicTree().Update(q);
99 }
100 
102 {
103  Update(q, phi, jacobian);
104  const int ndq = scene_->get_has_quaternion_floating_base() ? scene_->get_num_positions() - 1 : scene_->get_num_positions();
105  for (int i = 0; i < TaskSpaceJacobianDim(); ++i)
106  {
107  Eigen::Block<Eigen::Ref<Eigen::MatrixXd>> jacobian_row = jacobian.block(i, 0, 1, ndq);
108  hessian(i).topLeftCorner(ndq, ndq).noalias() = jacobian_row.transpose() * jacobian_row;
109  }
110 }
111 
113 {
114  // WARNING("x,u update not implemented - defaulting to q update.");
115  Update(x.head(scene_->get_num_positions()), phi);
116 }
117 
119 {
120  // WARNING("x,u update not implemented - defaulting to q update.");
121  const int ndq = scene_->get_has_quaternion_floating_base() ? scene_->get_num_positions() - 1 : scene_->get_num_positions();
122  Update(x.head(ndq), phi, dphi_dx.topLeftCorner(TaskSpaceJacobianDim(), ndq));
123 }
124 
126 {
127  // WARNING("x,u update not implemented - defaulting to q update.");
128  const int ndq = scene_->get_has_quaternion_floating_base() ? scene_->get_num_positions() - 1 : scene_->get_num_positions();
129  Update(x.head(ndq), phi, dphi_dx.topLeftCorner(TaskSpaceJacobianDim(), ndq), ddphi_ddx);
130 }
131 
132 } // namespace exotica
exotica::Object::InstantiateObject
void InstantiateObject(const Initializer &init)
Definition: object.h:71
exotica::TaskMap::TaskSpaceJacobianDim
virtual int TaskSpaceJacobianDim()
Definition: task_map.h:73
exotica::GetFrame
KDL::Frame GetFrame(Eigen::VectorXdRefConst val)
Definition: conversions.cpp:52
exotica::TaskMap::is_used
bool is_used
Definition: task_map.h:84
exotica::KinematicFrameRequest
Definition: kinematic_tree.h:83
exotica
Definition: collision_scene.h:46
exotica::TaskMap::GetFrames
std::vector< KinematicFrameRequest > GetFrames() const
Definition: task_map.cpp:57
exotica::ScenePtr
std::shared_ptr< Scene > ScenePtr
Definition: scene.h:246
Eigen::VectorXdRef
Eigen::Ref< Eigen::VectorXd > VectorXdRef
Definition: conversions.h:55
Eigen::VectorXdRefConst
const typedef Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
Convenience wrapper for storing references to sub-matrices/vectors.
Definition: conversions.h:53
exotica::TaskMap::AssignScene
virtual void AssignScene(ScenePtr scene)
Definition: task_map.cpp:37
exotica::TaskMap::scene_
ScenePtr scene_
Definition: task_map.h:88
Eigen::MatrixXdRef
Eigen::Ref< Eigen::MatrixXd > MatrixXdRef
Definition: conversions.h:56
exotica::HessianRef
Eigen::Ref< Hessian > HessianRef
Definition: conversions.h:161
exotica::Initializer
Definition: property.h:70
exotica::TaskMap::Update
virtual void Update(Eigen::VectorXdRefConst q, Eigen::VectorXdRef phi)=0
exotica::TaskMap::frames_
std::vector< KinematicFrameRequest > frames_
Definition: task_map.h:87
init
void init(const M_string &remappings)
exotica::TaskMap::InstantiateBase
virtual void InstantiateBase(const Initializer &init)
Definition: task_map.cpp:42
x
double x
task_map.h
exotica::TaskMap::TaskSpaceDim
virtual int TaskSpaceDim()=0
ThrowNamed
#define ThrowNamed(m)
Definition: exception.h:42


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Fri Aug 2 2024 08:43:02