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-" + 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<NodesVariablesPhaseBased>(ee_motion_id_);
00045 
00046   pure_swing_node_ids_ = ee_motion_->GetIndicesOfNonConstantNodes();
00047 
00048   // constrain xy position and velocity of every swing node
00049   int constraint_count =  pure_swing_node_ids_.size()*Node::n_derivatives*k2D;
00050 
00051   SetRows(constraint_count);
00052 }
00053 
00054 Eigen::VectorXd
00055 SwingConstraint::GetValues () const
00056 {
00057   VectorXd g(GetRows());
00058 
00059   int row = 0;
00060   auto nodes = ee_motion_->GetNodes();
00061   for (int node_id : pure_swing_node_ids_) {
00062     // assumes two splines per swingphase and starting and ending in stance
00063     auto curr = nodes.at(node_id);
00064 
00065     Vector2d prev = nodes.at(node_id-1).p().topRows<k2D>();
00066     Vector2d next = nodes.at(node_id+1).p().topRows<k2D>();
00067 
00068     Vector2d distance_xy    = next - prev;
00069     Vector2d xy_center      = prev + 0.5*distance_xy;
00070     Vector2d des_vel_center = distance_xy/t_swing_avg_; // linear interpolation not accurate
00071     for (auto dim : {X,Y}) {
00072       g(row++) = curr.p()(dim) - xy_center(dim);
00073       g(row++) = curr.v()(dim) - des_vel_center(dim);
00074     }
00075   }
00076 
00077   return g;
00078 }
00079 
00080 SwingConstraint::VecBound
00081 SwingConstraint::GetBounds () const
00082 {
00083   return VecBound(GetRows(), ifopt::BoundZero);
00084 }
00085 
00086 void
00087 SwingConstraint::FillJacobianBlock (std::string var_set,
00088                                     Jacobian& jac) const
00089 {
00090   if (var_set == ee_motion_->GetName()) {
00091     int row = 0;
00092     for (int node_id : pure_swing_node_ids_) {
00093       for (auto dim : {X,Y}) {
00094         // position constraint
00095         jac.coeffRef(row, ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(node_id,   kPos, dim))) =  1.0;  // current node
00096         jac.coeffRef(row, ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(node_id+1, kPos, dim))) = -0.5;  // next node
00097         jac.coeffRef(row, ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(node_id-1, kPos, dim))) = -0.5;  // previous node
00098         row++;
00099 
00100         // velocity constraint
00101         jac.coeffRef(row, ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(node_id,   kVel, dim))) =  1.0;              // current node
00102         jac.coeffRef(row, ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(node_id+1, kPos, dim))) = -1.0/t_swing_avg_; // next node
00103         jac.coeffRef(row, ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(node_id-1, kPos, dim))) = +1.0/t_swing_avg_; // previous node
00104         row++;
00105       }
00106     }
00107   }
00108 }
00109 
00110 } /* namespace towr */


towr
Author(s): Alexander W. Winkler
autogenerated on Mon Apr 15 2019 02:42:32