terrain_constraint.cc
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c) 2018, Alexander W. Winkler. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 
7 * Redistributions of source code must retain the above copyright notice, this
8  list of conditions and the following disclaimer.
9 
10 * Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 * Neither the name of the copyright holder nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ******************************************************************************/
29 
31 
32 
33 namespace towr {
34 
35 
37  std::string ee_motion)
38  :ConstraintSet(kSpecifyLater, "Terrain-Constraint-" + ee_motion)
39 {
40  ee_motion_id_ = ee_motion;
41  terrain_ = terrain;
42 }
43 
44 void
46 {
47  ee_motion_ = x->GetComponent<PhaseNodes>(ee_motion_id_);
48 
49  // skip first node, b/c already constrained by initial stance
50  for (int id=1; id<ee_motion_->GetNodes().size(); ++id)
51  node_ids_.push_back(id);
52 
53  int constraint_count = node_ids_.size();
54  SetRows(constraint_count);
55 }
56 
59 {
60  VectorXd g(GetRows());
61 
62  auto nodes = ee_motion_->GetNodes();
63  int row = 0;
64  for (int id : node_ids_) {
65  Vector3d p = nodes.at(id).p();
66  g(row++) = p.z() - terrain_->GetHeight(p.x(), p.y());
67  }
68 
69  return g;
70 }
71 
72 TerrainConstraint::VecBound
74 {
75  VecBound bounds(GetRows());
76  double max_distance_above_terrain = 1e20; // [m]
77 
78  int row = 0;
79  for (int id : node_ids_) {
80  if (ee_motion_->IsConstantNode(id))
81  bounds.at(row) = ifopt::BoundZero;
82  else
83  bounds.at(row) = ifopt::Bounds(0.0, max_distance_above_terrain);
84  row++;
85  }
86 
87  return bounds;
88 }
89 
90 void
91 TerrainConstraint::FillJacobianBlock (std::string var_set, Jacobian& jac) const
92 {
93  if (var_set == ee_motion_->GetName()) {
94 
95  auto nodes = ee_motion_->GetNodes();
96  int row = 0;
97  for (int id : node_ids_) {
98 
99  jac.coeffRef(row, ee_motion_->Index(id, kPos, Z)) = 1.0;
100 
101  Vector3d p = nodes.at(id).p();
102  for (auto dim : {X,Y})
103  jac.coeffRef(row, ee_motion_->Index(id, kPos, dim)) = -terrain_->GetDerivativeOfHeightWrt(To2D(dim), p.x(), p.y());
104 
105  row++;
106  }
107  }
108 }
109 
110 } /* namespace towr */
VecBound GetBounds() const override
Nodes that are associated to either swing or stance phases.
Definition: phase_nodes.h:46
static Dim2D To2D(Dim3D dim)
Eigen::VectorXd VectorXd
VectorXd GetValues() const override
std::string ee_motion_id_
the name of the endeffector variable set.
HeightMap::Ptr terrain_
the height map of the current terrain.
void FillJacobianBlock(std::string var_set, Jacobian &) const override
std::vector< int > node_ids_
the indices of the nodes constrained.
PhaseNodes::Ptr ee_motion_
the position of the endeffector.
std::shared_ptr< HeightMap > Ptr
Definition: height_map.h:55
TerrainConstraint(const HeightMap::Ptr &terrain, std::string ee_motion_id)
Constructs a terrain constraint.
virtual void InitVariableDependedQuantities(const VariablesPtr &x) override


towr_core
Author(s): Alexander W. Winkler
autogenerated on Sun Apr 8 2018 02:18:53