Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <towr/initialization/gait_generator.h>
00031
00032 #include <cassert>
00033 #include <numeric>
00034 #include <algorithm>
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;
00051 }
00052 }
00053
00054 GaitGenerator::VecTimes
00055 GaitGenerator::GetPhaseDurations (double t_total, EE ee) const
00056 {
00057
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);
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
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
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());
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
00137
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 }
00147
00148