gait_generator.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/initialization/gait_generator.h>
00031 
00032 #include <cassert>
00033 #include <numeric>   // std::accumulate
00034 #include <algorithm> // std::transform
00035 
00036 #include <towr/initialization/monoped_gait_generator.h>
00037 #include <towr/initialization/biped_gait_generator.h>
00038 #include <towr/initialization/quadruped_gait_generator.h>
00039 
00040 namespace towr {
00041 
00042 
00043 GaitGenerator::Ptr
00044 GaitGenerator::MakeGaitGenerator(int leg_count)
00045 {
00046   switch (leg_count) {
00047     case 1: return std::make_shared<MonopedGaitGenerator>();   break;
00048     case 2: return std::make_shared<BipedGaitGenerator>();     break;
00049     case 4: return std::make_shared<QuadrupedGaitGenerator>(); break;
00050     default: assert(false); break; // Error: Not implemented
00051   }
00052 }
00053 
00054 GaitGenerator::VecTimes
00055 GaitGenerator::GetPhaseDurations (double t_total, EE ee) const
00056 {
00057   // scale total time tu t_total
00058   std::vector<double> durations;
00059   for (auto d : GetNormalizedPhaseDurations(ee))
00060     durations.push_back(d*t_total);
00061 
00062   return durations;
00063 }
00064 
00065 GaitGenerator::VecTimes
00066 GaitGenerator::GetNormalizedPhaseDurations (EE ee) const
00067 {
00068   auto v = GetPhaseDurations().at(ee); // shorthand
00069   double total_time = std::accumulate(v.begin(), v.end(), 0.0);
00070   std::transform(v.begin(), v.end(), v.begin(),
00071                  [total_time](double t_phase){ return t_phase/total_time;});
00072 
00073   return v;
00074 }
00075 
00076 GaitGenerator::FootDurations
00077 GaitGenerator::GetPhaseDurations () const
00078 {
00079   int n_ee = contacts_.front().size();
00080   VecTimes d_accumulated(n_ee, 0.0);
00081 
00082   FootDurations foot_durations(n_ee);
00083   for (int phase=0; phase<contacts_.size()-1; ++phase) {
00084     ContactState curr = contacts_.at(phase);
00085     ContactState next = contacts_.at(phase+1);
00086 
00087     for (int ee=0; ee<curr.size(); ++ee) {
00088       d_accumulated.at(ee) += times_.at(phase);
00089 
00090       // if contact will change in next phase, so this phase duration complete
00091       bool contacts_will_change = curr.at(ee) != next.at(ee);
00092       if (contacts_will_change)  {
00093         foot_durations.at(ee).push_back(d_accumulated.at(ee));
00094         d_accumulated.at(ee) = 0.0;
00095       }
00096     }
00097   }
00098 
00099   // push back last phase
00100   for (int ee=0; ee<contacts_.back().size(); ++ee)
00101     foot_durations.at(ee).push_back(d_accumulated.at(ee) + times_.back());
00102 
00103 
00104   return foot_durations;
00105 }
00106 
00107 bool
00108 GaitGenerator::IsInContactAtStart (EE ee) const
00109 {
00110   return contacts_.front().at(ee);
00111 }
00112 
00113 void
00114 GaitGenerator::SetGaits (const std::vector<Gaits>& gaits)
00115 {
00116   contacts_.clear();
00117   times_.clear();
00118 
00119   for (Gaits g : gaits) {
00120     auto info = GetGait(g);
00121 
00122     std::vector<double>       t = info.first;
00123     std::vector<ContactState> c = info.second;
00124     assert(t.size() == c.size()); // make sure every phase has a time
00125 
00126     times_.insert      (times_.end(), t.begin(), t.end());
00127     contacts_.insert(contacts_.end(), c.begin(), c.end());
00128   }
00129 }
00130 
00131 GaitGenerator::GaitInfo
00132 GaitGenerator::RemoveTransition (const GaitInfo& g) const
00133 {
00134   GaitInfo new_gait = g;
00135 
00136   // remove the final transition between strides
00137   // but ensure that last step duration is not cut off
00138   new_gait.first.pop_back();
00139   new_gait.first.back() += g.first.back();
00140 
00141   new_gait.second.pop_back();
00142 
00143   return new_gait;
00144 }
00145 
00146 } /* namespace towr */
00147 
00148 


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