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-Constraint-" + ee_motion)
37 {
38  ee_motion_id_ = ee_motion;
39 }
40 
41 void
43 {
44  ee_motion_ = x->GetComponent<PhaseNodes>(ee_motion_id_);
45 
46  pure_swing_node_ids_ = ee_motion_->GetIndicesOfNonConstantNodes();
47 
48  // constrain xy position and velocity of every swing node
49  // add +1 per node if swing in apex is constrained
50  int constraint_count = pure_swing_node_ids_.size()*2*k2D;
51 
52  SetRows(constraint_count);
53 }
54 
57 {
58  VectorXd g(GetRows());
59 
60  int row = 0;
61  auto nodes = ee_motion_->GetNodes();
62  for (int node_id : pure_swing_node_ids_) {
63 
64  // assumes two splines per swingphase and starting and ending in stance
65  auto curr = nodes.at(node_id);
66 
67  Vector2d prev = nodes.at(node_id-1).p().topRows<k2D>();
68  Vector2d next = nodes.at(node_id+1).p().topRows<k2D>();
69 
70  Vector2d distance_xy = next - prev;
71  Vector2d xy_center = prev + 0.5*distance_xy;
72  Vector2d des_vel_center = distance_xy/t_swing_avg_; // linear interpolation not accurate
73  for (auto dim : {X,Y}) {
74  g(row++) = curr.p()(dim) - xy_center(dim);
75  g(row++) = curr.v()(dim) - des_vel_center(dim);
76  }
77  // g(row++) = curr.pos.z() - swing_height_in_world_;
78  }
79 
80  return g;
81 }
82 
83 SwingConstraint::VecBound
85 {
86  return VecBound(GetRows(), ifopt::BoundZero);
87 }
88 
89 void
91  Jacobian& jac) const
92 {
93  if (var_set == ee_motion_->GetName()) {
94 
95  int row = 0;
96  for (int node_id : pure_swing_node_ids_) {
97 
98  for (auto dim : {X,Y}) {
99  // position constraint
100  jac.coeffRef(row, ee_motion_->Index(node_id, kPos, dim)) = 1.0; // current node
101  jac.coeffRef(row, ee_motion_->Index(node_id+1, kPos, dim)) = -0.5; // next node
102  jac.coeffRef(row, ee_motion_->Index(node_id-1, kPos, dim)) = -0.5; // previous node
103  row++;
104 
105  // velocity constraint
106  jac.coeffRef(row, ee_motion_->Index(node_id, kVel, dim)) = 1.0; // current node
107  jac.coeffRef(row, ee_motion_->Index(node_id+1, kPos, dim)) = -1.0/t_swing_avg_; // next node
108  jac.coeffRef(row, ee_motion_->Index(node_id-1, kPos, dim)) = +1.0/t_swing_avg_; // previous node
109  row++;
110  }
111  // jac.coeffRef(row, ee_motion_->Index(i, kPos, Z)) = 1.0; // current node
112  // row++;
113  }
114  }
115 }
116 
117 } /* namespace towr */
Nodes that are associated to either swing or stance phases.
Definition: phase_nodes.h:46
PhaseNodes::Ptr ee_motion_
virtual void InitVariableDependedQuantities(const VariablesPtr &x) override
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.
static constexpr int k2D
VectorXd GetValues() const override
std::vector< int > pure_swing_node_ids_
VecBound GetBounds() const override


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