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
std::vector< KinematicFrameRequest > frames_
Definition: task_map.h:87
void InstantiateObject(const Initializer &init)
Definition: object.h:71
Eigen::Ref< Eigen::VectorXd > VectorXdRef
Definition: conversions.h:54
ScenePtr scene_
Definition: task_map.h:88
std::shared_ptr< Scene > ScenePtr
Definition: scene.h:246
virtual void AssignScene(ScenePtr scene)
Definition: task_map.cpp:37
virtual int TaskSpaceJacobianDim()
Definition: task_map.h:73
#define ThrowNamed(m)
Definition: exception.h:42
virtual void Update(Eigen::VectorXdRefConst q, Eigen::VectorXdRef phi)=0
const Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
Convenience wrapper for storing references to sub-matrices/vectors.
Definition: conversions.h:52
Eigen::Ref< Hessian > HessianRef
Definition: conversions.h:160
Eigen::Ref< Eigen::MatrixXd > MatrixXdRef
Definition: conversions.h:55
std::vector< KinematicFrameRequest > GetFrames() const
Definition: task_map.cpp:57
virtual int TaskSpaceDim()=0
KDL::Frame GetFrame(Eigen::VectorXdRefConst val)
Definition: conversions.cpp:52
virtual void InstantiateBase(const Initializer &init)
Definition: task_map.cpp:42


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sat Apr 10 2021 02:34:49