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-" + ee_motion)
39 {
40  ee_motion_id_ = ee_motion;
41  terrain_ = terrain;
42 }
43 
44 void
46 {
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 
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  auto nodes = ee_motion_->GetNodes();
95  int row = 0;
96  for (int id : node_ids_) {
97  int idx = ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(id, kPos, Z));
98  jac.coeffRef(row, idx) = 1.0;
99 
100  Vector3d p = nodes.at(id).p();
101  for (auto dim : {X,Y}) {
102  int idx = ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(id, kPos, dim));
103  jac.coeffRef(row, idx) = -terrain_->GetDerivativeOfHeightWrt(To2D(dim), p.x(), p.y());
104  }
105  row++;
106  }
107  }
108 }
109 
110 } /* namespace towr */
VecBound GetBounds() const override
static Dim2D To2D(Dim3D dim)
static const Bounds BoundZero
Composite::Ptr VariablesPtr
Eigen::VectorXd VectorXd
VectorXd GetValues() const override
Semantic information associated with a scalar node value.
std::string ee_motion_id_
the name of the endeffector variable set.
void SetRows(int num_rows)
NodesVariablesPhaseBased::Ptr ee_motion_
the position of the endeffector.
Eigen::SparseMatrix< double, Eigen::RowMajor > Jacobian
HeightMap::Ptr terrain_
the height map of the current terrain.
void FillJacobianBlock(std::string var_set, Jacobian &) const override
Nodes that are associated to either swing or stance phases.
std::vector< int > node_ids_
the indices of the nodes constrained.
std::vector< Bounds > VecBound
int GetRows() const
std::shared_ptr< HeightMap > Ptr
Definition: height_map.h:73
TerrainConstraint(const HeightMap::Ptr &terrain, std::string ee_motion_id)
Constructs a terrain constraint.
void InitVariableDependedQuantities(const VariablesPtr &x) override
Eigen::VectorXd VectorXd


towr
Author(s): Alexander W. Winkler
autogenerated on Fri Apr 2 2021 02:14:16