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/variables/phase_durations.h>
00031
00032 #include <numeric>
00033
00034 #include <towr/variables/variable_names.h>
00035 #include <towr/variables/spline.h>
00036
00037
00038 namespace towr {
00039
00040
00041 PhaseDurations::PhaseDurations (EndeffectorID ee,
00042 const VecDurations& timings,
00043 bool is_first_phase_in_contact,
00044 double min_duration,
00045 double max_duration)
00046
00047 :VariableSet(timings.size()-1, id::EESchedule(ee))
00048 {
00049 durations_ = timings;
00050 t_total_ = std::accumulate(timings.begin(), timings.end(), 0.0);
00051 phase_duration_bounds_ = ifopt::Bounds(min_duration, max_duration);
00052 initial_contact_state_ = is_first_phase_in_contact;
00053 }
00054
00055 void
00056 PhaseDurations::AddObserver (PhaseDurationsObserver* const o)
00057 {
00058 observers_.push_back(o);
00059 }
00060
00061 void
00062 PhaseDurations::UpdateObservers () const
00063 {
00064 for (auto& spline : observers_)
00065 spline->UpdatePolynomialDurations();
00066 }
00067
00068 Eigen::VectorXd
00069 PhaseDurations::GetValues () const
00070 {
00071 VectorXd x(GetRows());
00072
00073 for (int i=0; i<x.rows(); ++i)
00074 x(i) = durations_.at(i);
00075
00076 return x;
00077 }
00078
00079 void
00080 PhaseDurations::SetVariables (const VectorXd& x)
00081 {
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 assert(t_total_>x.sum());
00093
00094 for (int i=0; i<GetRows(); ++i)
00095 durations_.at(i) = x(i);
00096
00097
00098 durations_.back() = t_total_ - x.sum();
00099 UpdateObservers();
00100 }
00101
00102 PhaseDurations::VecBound
00103 PhaseDurations::GetBounds () const
00104 {
00105 VecBound bounds;
00106
00107 for (int i=0; i<GetRows(); ++i)
00108 bounds.push_back(phase_duration_bounds_);
00109
00110 return bounds;
00111 }
00112
00113 PhaseDurations::VecDurations
00114 PhaseDurations::GetPhaseDurations() const
00115 {
00116 return durations_;
00117 }
00118
00119 bool
00120 PhaseDurations::IsContactPhase (double t) const
00121 {
00122 int phase_id = Spline::GetSegmentID(t, durations_);
00123 return phase_id%2 == 0? initial_contact_state_ : !initial_contact_state_;
00124 }
00125
00126 PhaseDurations::Jacobian
00127 PhaseDurations::GetJacobianOfPos (int current_phase,
00128 const VectorXd& dx_dT,
00129 const VectorXd& xd) const
00130 {
00131 int n_dim = xd.rows();
00132 Eigen::MatrixXd jac = Eigen::MatrixXd::Zero(n_dim, GetRows());
00133
00134 bool in_last_phase = (current_phase == durations_.size()-1);
00135
00136
00137 if (!in_last_phase)
00138 jac.col(current_phase) = dx_dT;
00139
00140 for (int phase=0; phase<current_phase; ++phase) {
00141
00142 jac.col(phase) = -1*xd;
00143
00144
00145
00146 if (in_last_phase)
00147 jac.col(phase) -= dx_dT;
00148 }
00149
00150
00151
00152
00153 return jac.sparseView(1.0, -1.0);
00154 }
00155
00156 }
00157
00158