swing_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 
32 
33 namespace towr {
34 
35 SwingConstraint::SwingConstraint (std::string ee_motion)
36  :ConstraintSet(kSpecifyLater, "swing-" + ee_motion)
37 {
38  ee_motion_id_ = ee_motion;
39 }
40 
41 void
43 {
45 
46  pure_swing_node_ids_ = ee_motion_->GetIndicesOfNonConstantNodes();
47 
48  // constrain xy position and velocity of every swing node
49  int constraint_count = pure_swing_node_ids_.size()*Node::n_derivatives*k2D;
50 
51  SetRows(constraint_count);
52 }
53 
56 {
57  VectorXd g(GetRows());
58 
59  int row = 0;
60  auto nodes = ee_motion_->GetNodes();
61  for (int node_id : pure_swing_node_ids_) {
62  // assumes two splines per swingphase and starting and ending in stance
63  auto curr = nodes.at(node_id);
64 
65  Vector2d prev = nodes.at(node_id-1).p().topRows<k2D>();
66  Vector2d next = nodes.at(node_id+1).p().topRows<k2D>();
67 
68  Vector2d distance_xy = next - prev;
69  Vector2d xy_center = prev + 0.5*distance_xy;
70  Vector2d des_vel_center = distance_xy/t_swing_avg_; // linear interpolation not accurate
71  for (auto dim : {X,Y}) {
72  g(row++) = curr.p()(dim) - xy_center(dim);
73  g(row++) = curr.v()(dim) - des_vel_center(dim);
74  }
75  }
76 
77  return g;
78 }
79 
82 {
84 }
85 
86 void
88  Jacobian& jac) const
89 {
90  if (var_set == ee_motion_->GetName()) {
91  int row = 0;
92  for (int node_id : pure_swing_node_ids_) {
93  for (auto dim : {X,Y}) {
94  // position constraint
95  jac.coeffRef(row, ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(node_id, kPos, dim))) = 1.0; // current node
96  jac.coeffRef(row, ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(node_id+1, kPos, dim))) = -0.5; // next node
97  jac.coeffRef(row, ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(node_id-1, kPos, dim))) = -0.5; // previous node
98  row++;
99 
100  // velocity constraint
101  jac.coeffRef(row, ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(node_id, kVel, dim))) = 1.0; // current node
102  jac.coeffRef(row, ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(node_id+1, kPos, dim))) = -1.0/t_swing_avg_; // next node
103  jac.coeffRef(row, ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(node_id-1, kPos, dim))) = +1.0/t_swing_avg_; // previous node
104  row++;
105  }
106  }
107  }
108 }
109 
110 } /* namespace towr */
void InitVariableDependedQuantities(const VariablesPtr &x) override
static const Bounds BoundZero
Composite::Ptr VariablesPtr
void FillJacobianBlock(std::string var_set, Jacobian &) const override
Eigen::Vector2d Vector2d
Eigen::VectorXd VectorXd
SwingConstraint(std::string ee_motion_id)
Links the swing constraint with current foot variables.
Semantic information associated with a scalar node value.
static constexpr int k2D
VectorXd GetValues() const override
NodesVariablesPhaseBased::Ptr ee_motion_
void SetRows(int num_rows)
std::vector< int > pure_swing_node_ids_
Eigen::SparseMatrix< double, Eigen::RowMajor > Jacobian
VecBound GetBounds() const override
Nodes that are associated to either swing or stance phases.
static const int n_derivatives
value and first derivative.
Definition: state.h:109
std::vector< Bounds > VecBound
int GetRows() const
Eigen::VectorXd VectorXd


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