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-Constraint-" + 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<PhaseNodes>(id::EEForceNodes(ee_));
00054   ee_motion_ = x->GetComponent<PhaseNodes>(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 
00071     int phase  = ee_force_->GetPhase(f_node_id);
00072     Vector3d p = ee_motion_->GetValueAtStartOfPhase(phase); // doesn't change during stance phase
00073     Vector3d n = terrain_->GetNormalizedBasis(HeightMap::Normal, p.x(), p.y());
00074     Vector3d f = force_nodes.at(f_node_id).p();
00075 
00076     // unilateral force
00077     g(row++) = f.transpose() * n; // >0 (unilateral forces)
00078 
00079     // frictional pyramid
00080     Vector3d t1 = terrain_->GetNormalizedBasis(HeightMap::Tangent1, p.x(), p.y());
00081     g(row++) = f.transpose() * (t1 - mu_*n); // t1 < mu*n
00082     g(row++) = f.transpose() * (t1 + mu_*n); // t1 > -mu*n
00083 
00084     Vector3d t2 = terrain_->GetNormalizedBasis(HeightMap::Tangent2, p.x(), p.y());
00085     g(row++) = f.transpose() * (t2 - mu_*n); // t2 < mu*n
00086     g(row++) = f.transpose() * (t2 + mu_*n); // t2 > -mu*n
00087   }
00088 
00089   return g;
00090 }
00091 
00092 ForceConstraint::VecBound
00093 ForceConstraint::GetBounds () const
00094 {
00095   VecBound bounds;
00096 
00097   for (int f_node_id : pure_stance_force_node_ids_) {
00098     bounds.push_back(ifopt::Bounds(0.0, fn_max_)); // unilateral forces
00099     bounds.push_back(ifopt::BoundSmallerZero); // f_t1 <  mu*n
00100     bounds.push_back(ifopt::BoundGreaterZero); // f_t1 > -mu*n
00101     bounds.push_back(ifopt::BoundSmallerZero); // f_t2 <  mu*n
00102     bounds.push_back(ifopt::BoundGreaterZero); // f_t2 > -mu*n
00103   }
00104 
00105   return bounds;
00106 }
00107 
00108 void
00109 ForceConstraint::FillJacobianBlock (std::string var_set,
00110                                     Jacobian& jac) const
00111 {
00112   if (var_set == ee_force_->GetName()) {
00113 
00114     int row = 0;
00115     for (int f_node_id : pure_stance_force_node_ids_) {
00116 
00117       // unilateral force
00118       int phase   = ee_force_->GetPhase(f_node_id);
00119       Vector3d p  = ee_motion_->GetValueAtStartOfPhase(phase); // doesn't change during phase
00120       Vector3d n  = terrain_->GetNormalizedBasis(HeightMap::Normal,   p.x(), p.y());
00121       Vector3d t1 = terrain_->GetNormalizedBasis(HeightMap::Tangent1, p.x(), p.y());
00122       Vector3d t2 = terrain_->GetNormalizedBasis(HeightMap::Tangent2, p.x(), p.y());
00123 
00124       for (auto dim : {X,Y,Z}) {
00125         int idx = ee_force_->Index(f_node_id, kPos, dim);
00126 
00127         int row_reset=row;
00128 
00129         jac.coeffRef(row_reset++, idx) = n(dim);              // unilateral force
00130         jac.coeffRef(row_reset++, idx) = t1(dim)-mu_*n(dim);  // f_t1 <  mu*n
00131         jac.coeffRef(row_reset++, idx) = t1(dim)+mu_*n(dim);  // f_t1 > -mu*n
00132         jac.coeffRef(row_reset++, idx) = t2(dim)-mu_*n(dim);  // f_t2 <  mu*n
00133         jac.coeffRef(row_reset++, idx) = t2(dim)+mu_*n(dim);  // f_t2 > -mu*n
00134       }
00135 
00136       row += n_constraints_per_node_;
00137     }
00138   }
00139 
00140 
00141   if (var_set == ee_motion_->GetName()) {
00142 
00143     int row = 0;
00144     auto force_nodes = ee_force_->GetNodes();
00145     for (int f_node_id : pure_stance_force_node_ids_) {
00146 
00147       int phase  = ee_force_->GetPhase(f_node_id);
00148       int ee_node_id = ee_motion_->GetNodeIDAtStartOfPhase(phase);
00149 
00150       Vector3d p = ee_motion_->GetValueAtStartOfPhase(phase); // doesn't change during pahse
00151       Vector3d f = force_nodes.at(f_node_id).p();
00152 
00153       for (auto dim : {X_,Y_}) {
00154 
00155         Vector3d dn  = terrain_->GetDerivativeOfNormalizedBasisWrt(HeightMap::Normal, dim, p.x(), p.y());
00156         Vector3d dt1 = terrain_->GetDerivativeOfNormalizedBasisWrt(HeightMap::Tangent1, dim, p.x(), p.y());
00157         Vector3d dt2 = terrain_->GetDerivativeOfNormalizedBasisWrt(HeightMap::Tangent2, dim, p.x(), p.y());
00158 
00159         int idx = ee_motion_->Index(ee_node_id, kPos, dim);
00160         int row_reset=row;
00161 
00162         // unilateral force
00163         jac.coeffRef(row_reset++, idx) = f.transpose()*dn;
00164 
00165         // friction force tangent 1 derivative
00166         jac.coeffRef(row_reset++, idx) = f.transpose()*(dt1-mu_*dn);
00167         jac.coeffRef(row_reset++, idx) = f.transpose()*(dt1+mu_*dn);
00168 
00169         // friction force tangent 2 derivative
00170         jac.coeffRef(row_reset++, idx) = f.transpose()*(dt2-mu_*dn);
00171         jac.coeffRef(row_reset++, idx) = f.transpose()*(dt2+mu_*dn);
00172       }
00173 
00174       row += n_constraints_per_node_;
00175     }
00176   }
00177 }
00178 
00179 } /* namespace towr */


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