force_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/force_constraint.h>
00031 
00032 #include <towr/variables/variable_names.h>
00033 
00034 namespace towr {
00035 
00036 
00037 ForceConstraint::ForceConstraint (const HeightMap::Ptr& terrain,
00038                                   double force_limit,
00039                                   EE ee)
00040     :ifopt::ConstraintSet(kSpecifyLater, "force-" + id::EEForceNodes(ee))
00041 {
00042   terrain_ = terrain;
00043   fn_max_  = force_limit;
00044   mu_      = terrain->GetFrictionCoeff();
00045   ee_      = ee;
00046 
00047   n_constraints_per_node_ = 1 + 2*k2D; // positive normal force + 4 friction pyramid constraints
00048 }
00049 
00050 void
00051 ForceConstraint::InitVariableDependedQuantities (const VariablesPtr& x)
00052 {
00053   ee_force_  = x->GetComponent<NodesVariablesPhaseBased>(id::EEForceNodes(ee_));
00054   ee_motion_ = x->GetComponent<NodesVariablesPhaseBased>(id::EEMotionNodes(ee_));
00055 
00056   pure_stance_force_node_ids_ = ee_force_->GetIndicesOfNonConstantNodes();
00057 
00058   int constraint_count = pure_stance_force_node_ids_.size()*n_constraints_per_node_;
00059   SetRows(constraint_count);
00060 }
00061 
00062 Eigen::VectorXd
00063 ForceConstraint::GetValues () const
00064 {
00065   VectorXd g(GetRows());
00066 
00067   int row=0;
00068   auto force_nodes = ee_force_->GetNodes();
00069   for (int f_node_id : pure_stance_force_node_ids_) {
00070     int phase  = ee_force_->GetPhase(f_node_id);
00071     Vector3d p = ee_motion_->GetValueAtStartOfPhase(phase); // doesn't change during stance phase
00072     Vector3d n = terrain_->GetNormalizedBasis(HeightMap::Normal, p.x(), p.y());
00073     Vector3d f = force_nodes.at(f_node_id).p();
00074 
00075     // unilateral force
00076     g(row++) = f.transpose() * n; // >0 (unilateral forces)
00077 
00078     // frictional pyramid
00079     Vector3d t1 = terrain_->GetNormalizedBasis(HeightMap::Tangent1, p.x(), p.y());
00080     g(row++) = f.transpose() * (t1 - mu_*n); // t1 < mu*n
00081     g(row++) = f.transpose() * (t1 + mu_*n); // t1 > -mu*n
00082 
00083     Vector3d t2 = terrain_->GetNormalizedBasis(HeightMap::Tangent2, p.x(), p.y());
00084     g(row++) = f.transpose() * (t2 - mu_*n); // t2 < mu*n
00085     g(row++) = f.transpose() * (t2 + mu_*n); // t2 > -mu*n
00086   }
00087 
00088   return g;
00089 }
00090 
00091 ForceConstraint::VecBound
00092 ForceConstraint::GetBounds () const
00093 {
00094   VecBound bounds;
00095 
00096   for (int f_node_id : pure_stance_force_node_ids_) {
00097     bounds.push_back(ifopt::Bounds(0.0, fn_max_)); // unilateral forces
00098     bounds.push_back(ifopt::BoundSmallerZero); // f_t1 <  mu*n
00099     bounds.push_back(ifopt::BoundGreaterZero); // f_t1 > -mu*n
00100     bounds.push_back(ifopt::BoundSmallerZero); // f_t2 <  mu*n
00101     bounds.push_back(ifopt::BoundGreaterZero); // f_t2 > -mu*n
00102   }
00103 
00104   return bounds;
00105 }
00106 
00107 void
00108 ForceConstraint::FillJacobianBlock (std::string var_set,
00109                                     Jacobian& jac) const
00110 {
00111   if (var_set == ee_force_->GetName()) {
00112     int row = 0;
00113     for (int f_node_id : pure_stance_force_node_ids_) {
00114       // unilateral force
00115       int phase   = ee_force_->GetPhase(f_node_id);
00116       Vector3d p  = ee_motion_->GetValueAtStartOfPhase(phase); // doesn't change during phase
00117       Vector3d n  = terrain_->GetNormalizedBasis(HeightMap::Normal,   p.x(), p.y());
00118       Vector3d t1 = terrain_->GetNormalizedBasis(HeightMap::Tangent1, p.x(), p.y());
00119       Vector3d t2 = terrain_->GetNormalizedBasis(HeightMap::Tangent2, p.x(), p.y());
00120 
00121       for (auto dim : {X,Y,Z}) {
00122         int idx = ee_force_->GetOptIndex(NodesVariables::NodeValueInfo(f_node_id, kPos, dim));
00123 
00124         int row_reset=row;
00125 
00126         jac.coeffRef(row_reset++, idx) = n(dim);              // unilateral force
00127         jac.coeffRef(row_reset++, idx) = t1(dim)-mu_*n(dim);  // f_t1 <  mu*n
00128         jac.coeffRef(row_reset++, idx) = t1(dim)+mu_*n(dim);  // f_t1 > -mu*n
00129         jac.coeffRef(row_reset++, idx) = t2(dim)-mu_*n(dim);  // f_t2 <  mu*n
00130         jac.coeffRef(row_reset++, idx) = t2(dim)+mu_*n(dim);  // f_t2 > -mu*n
00131       }
00132 
00133       row += n_constraints_per_node_;
00134     }
00135   }
00136 
00137 
00138   if (var_set == ee_motion_->GetName()) {
00139     int row = 0;
00140     auto force_nodes = ee_force_->GetNodes();
00141     for (int f_node_id : pure_stance_force_node_ids_) {
00142       int phase  = ee_force_->GetPhase(f_node_id);
00143       int ee_node_id = ee_motion_->GetNodeIDAtStartOfPhase(phase);
00144 
00145       Vector3d p = ee_motion_->GetValueAtStartOfPhase(phase); // doesn't change during pahse
00146       Vector3d f = force_nodes.at(f_node_id).p();
00147 
00148       for (auto dim : {X_,Y_}) {
00149         Vector3d dn  = terrain_->GetDerivativeOfNormalizedBasisWrt(HeightMap::Normal, dim, p.x(), p.y());
00150         Vector3d dt1 = terrain_->GetDerivativeOfNormalizedBasisWrt(HeightMap::Tangent1, dim, p.x(), p.y());
00151         Vector3d dt2 = terrain_->GetDerivativeOfNormalizedBasisWrt(HeightMap::Tangent2, dim, p.x(), p.y());
00152 
00153         int idx = ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(ee_node_id, kPos, dim));
00154         int row_reset=row;
00155 
00156         // unilateral force
00157         jac.coeffRef(row_reset++, idx) = f.transpose()*dn;
00158 
00159         // friction force tangent 1 derivative
00160         jac.coeffRef(row_reset++, idx) = f.transpose()*(dt1-mu_*dn);
00161         jac.coeffRef(row_reset++, idx) = f.transpose()*(dt1+mu_*dn);
00162 
00163         // friction force tangent 2 derivative
00164         jac.coeffRef(row_reset++, idx) = f.transpose()*(dt2-mu_*dn);
00165         jac.coeffRef(row_reset++, idx) = f.transpose()*(dt2+mu_*dn);
00166       }
00167 
00168       row += n_constraints_per_node_;
00169     }
00170   }
00171 }
00172 
00173 } /* namespace towr */


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