swing_constraint.cc
Go to the documentation of this file.
00001 /******************************************************************************
00002 Copyright (c) 2018, Alexander W. Winkler. All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 
00010 * Redistributions in binary form must reproduce the above copyright notice,
00011   this list of conditions and the following disclaimer in the documentation
00012   and/or other materials provided with the distribution.
00013 
00014 * Neither the name of the copyright holder nor the names of its
00015   contributors may be used to endorse or promote products derived from
00016   this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00022 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00024 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00025 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00027 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 ******************************************************************************/
00029 
00030 #include <towr/constraints/swing_constraint.h>
00031 #include <towr/variables/cartesian_dimensions.h>
00032 
00033 namespace towr {
00034 
00035 SwingConstraint::SwingConstraint (std::string ee_motion)
00036     :ConstraintSet(kSpecifyLater, "Swing-Constraint-" + ee_motion)
00037 {
00038   ee_motion_id_ = ee_motion;
00039 }
00040 
00041 void
00042 towr::SwingConstraint::InitVariableDependedQuantities (const VariablesPtr& x)
00043 {
00044   ee_motion_ = x->GetComponent<PhaseNodes>(ee_motion_id_);
00045 
00046   pure_swing_node_ids_ = ee_motion_->GetIndicesOfNonConstantNodes();
00047 
00048   // constrain xy position and velocity of every swing node
00049   // add +1 per node if swing in apex is constrained
00050   int constraint_count =  pure_swing_node_ids_.size()*2*k2D;
00051 
00052   SetRows(constraint_count);
00053 }
00054 
00055 Eigen::VectorXd
00056 SwingConstraint::GetValues () const
00057 {
00058   VectorXd g(GetRows());
00059 
00060   int row = 0;
00061   auto nodes = ee_motion_->GetNodes();
00062   for (int node_id : pure_swing_node_ids_) {
00063 
00064     // assumes two splines per swingphase and starting and ending in stance
00065     auto curr = nodes.at(node_id);
00066 
00067     Vector2d prev = nodes.at(node_id-1).p().topRows<k2D>();
00068     Vector2d next = nodes.at(node_id+1).p().topRows<k2D>();
00069 
00070     Vector2d distance_xy    = next - prev;
00071     Vector2d xy_center      = prev + 0.5*distance_xy;
00072     Vector2d des_vel_center = distance_xy/t_swing_avg_; // linear interpolation not accurate
00073     for (auto dim : {X,Y}) {
00074       g(row++) = curr.p()(dim) - xy_center(dim);
00075       g(row++) = curr.v()(dim) - des_vel_center(dim);
00076     }
00077     //      g(row++) = curr.pos.z() - swing_height_in_world_;
00078   }
00079 
00080   return g;
00081 }
00082 
00083 SwingConstraint::VecBound
00084 SwingConstraint::GetBounds () const
00085 {
00086   return VecBound(GetRows(), ifopt::BoundZero);
00087 }
00088 
00089 void
00090 SwingConstraint::FillJacobianBlock (std::string var_set,
00091                                     Jacobian& jac) const
00092 {
00093   if (var_set == ee_motion_->GetName()) {
00094 
00095     int row = 0;
00096     for (int node_id : pure_swing_node_ids_) {
00097 
00098       for (auto dim : {X,Y}) {
00099         // position constraint
00100         jac.coeffRef(row, ee_motion_->Index(node_id,   kPos, dim)) =  1.0;  // current node
00101         jac.coeffRef(row, ee_motion_->Index(node_id+1, kPos, dim)) = -0.5;  // next node
00102         jac.coeffRef(row, ee_motion_->Index(node_id-1, kPos, dim)) = -0.5;  // previous node
00103         row++;
00104 
00105         // velocity constraint
00106         jac.coeffRef(row, ee_motion_->Index(node_id,   kVel, dim)) =  1.0;              // current node
00107         jac.coeffRef(row, ee_motion_->Index(node_id+1, kPos, dim)) = -1.0/t_swing_avg_; // next node
00108         jac.coeffRef(row, ee_motion_->Index(node_id-1, kPos, dim)) = +1.0/t_swing_avg_; // previous node
00109         row++;
00110       }
00111       //        jac.coeffRef(row, ee_motion_->Index(i, kPos, Z)) =  1.0;  // current node
00112       //        row++;
00113     }
00114   }
00115 }
00116 
00117 } /* namespace towr */


towr_core
Author(s): Alexander W. Winkler
autogenerated on Mon Apr 9 2018 03:12:44