phase_durations.cc
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c) 2018, Alexander W. Winkler. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 
7 * Redistributions of source code must retain the above copyright notice, this
8  list of conditions and the following disclaimer.
9 
10 * Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 * Neither the name of the copyright holder nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ******************************************************************************/
29 
31 
32 #include <numeric> // std::accumulate
33 
35 #include <towr/variables/spline.h> // for Spline::GetSegmentID()
36 
37 
38 namespace towr {
39 
40 
42  const VecDurations& timings,
43  bool is_first_phase_in_contact,
44  double min_duration,
45  double max_duration)
46  // -1 since last phase-duration is not optimized over, but comes from total time
47  :VariableSet(timings.size()-1, id::EESchedule(ee))
48 {
49  durations_ = timings;
50  t_total_ = std::accumulate(timings.begin(), timings.end(), 0.0);
51  phase_duration_bounds_ = ifopt::Bounds(min_duration, max_duration);
52  initial_contact_state_ = is_first_phase_in_contact;
53 }
54 
55 void
57 {
58  observers_.push_back(o);
59 }
60 
61 void
63 {
64  for (auto& spline : observers_)
65  spline->UpdatePolynomialDurations();
66 }
67 
70 {
71  VectorXd x(GetRows());
72 
73  for (int i=0; i<x.rows(); ++i)
74  x(i) = durations_.at(i);
75 
76  return x;
77 }
78 
79 void
81 {
82  for (int i=0; i<GetRows(); ++i)
83  durations_.at(i) = x(i);
84 
85  durations_.back() = t_total_ - x.sum();
86  assert(durations_.back()>0);
88 }
89 
90 PhaseDurations::VecBound
92 {
93  VecBound bounds;
94 
95  for (int i=0; i<GetRows(); ++i)
96  bounds.push_back(phase_duration_bounds_);
97 
98  return bounds;
99 }
100 
103 {
104  return durations_;
105 }
106 
107 bool
109 {
110  int phase_id = Spline::GetSegmentID(t, durations_);
111  return phase_id%2 == 0? initial_contact_state_ : !initial_contact_state_;
112 }
113 
114 PhaseDurations::Jacobian
116  const VectorXd& dx_dT,
117  const VectorXd& xd) const
118 {
119  int n_dim = xd.rows();
120  Eigen::MatrixXd jac = Eigen::MatrixXd::Zero(n_dim, GetRows());
121 
122  bool in_last_phase = (current_phase == durations_.size()-1);
123 
124  // duration of current phase expands and compressed spline
125  if (!in_last_phase)
126  jac.col(current_phase) = dx_dT;
127 
128  for (int phase=0; phase<current_phase; ++phase) {
129  // each previous durations shifts spline along time axis
130  jac.col(phase) = -1*xd;
131 
132  // in last phase previous duration cause expansion/compression of spline
133  // as final time is fixed.
134  if (in_last_phase)
135  jac.col(phase) -= dx_dT;
136  }
137 
138  // convert to sparse, but also regard 0.0 as non-zero element, because
139  // could turn nonzero during the course of the program
140  // as durations change and t_global falls into different spline
141  return jac.sparseView(1.0, -1.0);
142 }
143 
144 } /* namespace towr */
145 
146 
ifopt::Bounds phase_duration_bounds_
Base class to receive up-to-date values of the ContactSchedule.
Eigen::VectorXd VectorXd
static std::string EESchedule(uint ee)
virtual VectorXd GetValues() const override
bool IsContactPhase(double t) const
Whether the endeffector is in contact with the environment.
VecBound GetBounds() const override
Jacobian GetJacobianOfPos(int phase, const VectorXd &dx_dT, const VectorXd &xd) const
How a change in the phase durations affect the position of a spline.
VecDurations GetPhaseDurations() const
static int GetSegmentID(double t_global, const VecTimes &durations)
Definition: spline.cc:49
void UpdateObservers() const
PhaseDurations(EndeffectorID ee, const VecDurations &initial_durations, bool is_first_phase_in_contact, double min_phase_duration, double max_phase_duration)
Constructs a variable set for a specific endeffector.
std::vector< double > VecDurations
virtual void SetVariables(const VectorXd &x) override
Sets the phase durations from pure Eigen optimization variables.
bool initial_contact_state_
true if first phase in contact
void AddObserver(PhaseDurationsObserver *const spline)
Adds observer that is updated every time new variables are set.
std::vector< PhaseDurationsObserver * > observers_


towr_core
Author(s): Alexander W. Winkler
autogenerated on Sat Apr 7 2018 02:15:57