parameters.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/parameters.h>
00031 #include <towr/variables/cartesian_dimensions.h>
00032 
00033 #include <algorithm>
00034 #include <numeric>      // std::accumulate
00035 #include <math.h>       // fabs
00036 #include <cassert>
00037 
00038 namespace towr {
00039 
00040 Parameters::Parameters ()
00041 {
00042   // constructs optimization variables
00043   duration_base_polynomial_ = 0.1;
00044   force_polynomials_per_stance_phase_ = 3;
00045   ee_polynomials_per_swing_phase_ = 2; // so step can at least lift leg
00046 
00047   // parameters related to specific constraints (only used when it is added as well)
00048   force_limit_in_normal_direction_ = 1000;
00049   dt_constraint_range_of_motion_ = 0.08;
00050   dt_constraint_dynamic_ = 0.1;
00051   dt_constraint_base_motion_ = duration_base_polynomial_/4.; // only for base RoM constraint
00052   bound_phase_duration_ = std::make_pair(0.2, 1.0);  // used only when optimizing phase durations, so gait
00053 
00054   // a minimal set of basic constraints
00055   constraints_.push_back(Terrain);
00056   constraints_.push_back(Dynamic); //Ensures that the dynamic model is fullfilled at discrete times.
00057   constraints_.push_back(BaseAcc); // so accelerations don't jump between polynomials
00058   constraints_.push_back(EndeffectorRom); //Ensures that the range of motion is respected at discrete times.
00059   constraints_.push_back(Force); // ensures unilateral forces and inside the friction cone.
00060   constraints_.push_back(Swing); // creates smoother swing motions, not absolutely required.
00061 
00062   // optional costs to e.g penalize endeffector forces
00063   // costs_.push_back({ForcesCostID, 1.0}); weighed by 1.0 relative to other costs
00064 
00065   // bounds on final 6DoF base state
00066   bounds_final_lin_pos_ = {X,Y};
00067   bounds_final_lin_vel_ = {X,Y,Z};
00068   bounds_final_ang_pos_ = {X,Y,Z};
00069   bounds_final_ang_vel_ = {X,Y,Z};
00070 
00071   // additional restrictions are set directly on the variables in nlp_factory,
00072   // such as e.g. initial and endeffector,...
00073 }
00074 
00075 void
00076 
00077 Parameters::OptimizePhaseDurations ()
00078 {
00079   constraints_.push_back(TotalTime);
00080 }
00081 
00082 Parameters::VecTimes
00083 Parameters::GetBasePolyDurations () const
00084 {
00085   std::vector<double> base_spline_timings_;
00086   double dt = duration_base_polynomial_;
00087   double t_left = GetTotalTime ();
00088 
00089   double eps = 1e-10; // since repeated subtraction causes inaccuracies
00090   while (t_left > eps) {
00091     double duration = t_left>dt?  dt : t_left;
00092     base_spline_timings_.push_back(duration);
00093 
00094     t_left -= dt;
00095   }
00096 
00097   return base_spline_timings_;
00098 }
00099 
00100 int
00101 Parameters::GetPhaseCount(EEID ee) const
00102 {
00103   return ee_phase_durations_.at(ee).size();
00104 }
00105 
00106 int
00107 Parameters::GetEECount() const
00108 {
00109   return ee_in_contact_at_start_.size();
00110 }
00111 
00112 double
00113 Parameters::GetTotalTime () const
00114 {
00115   std::vector<double> T_feet;
00116 
00117   for (const auto& v : ee_phase_durations_)
00118     T_feet.push_back(std::accumulate(v.begin(), v.end(), 0.0));
00119 
00120   // safety check that all feet durations sum to same value
00121   double T = T_feet.empty()? 0.0 : T_feet.front(); // take first foot as reference
00122   for (double Tf : T_feet)
00123     assert(fabs(Tf - T) < 1e-6);
00124 
00125   return T;
00126 }
00127 
00128 bool
00129 Parameters::IsOptimizeTimings () const
00130 {
00131   // if total time is constrained, then timings are optimized
00132   ConstraintName c = TotalTime;
00133   auto v = constraints_; // shorthand
00134   return std::find(v.begin(), v.end(), c) != v.end();
00135 }
00136 
00137 } // namespace towr


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