dynamic_constraint.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 
34 
35 namespace towr {
36 
38  double T, double dt,
39  const SplineHolder& spline_holder)
40  :TimeDiscretizationConstraint(T, dt, "dynamic")
41 {
42  model_ = m;
43 
44  // link with up-to-date spline variables
45  base_linear_ = spline_holder.base_linear_;
47  ee_forces_ = spline_holder.ee_force_;
48  ee_motion_ = spline_holder.ee_motion_;
49 
51 }
52 
53 int
54 DynamicConstraint::GetRow (int k, Dim6D dimension) const
55 {
56  return k6D*k + dimension;
57 }
58 
59 void
61 {
62  UpdateModel(t);
63  g.segment(GetRow(k,AX), k6D) = model_->GetDynamicViolation();
64 }
65 
66 void
67 DynamicConstraint::UpdateBoundsAtInstance(double t, int k, VecBound& bounds) const
68 {
69  for (auto dim : AllDim6D)
70  bounds.at(GetRow(k,dim)) = ifopt::BoundZero;
71 }
72 
73 void
74 DynamicConstraint::UpdateJacobianAtInstance(double t, int k, std::string var_set,
75  Jacobian& jac) const
76 {
77  UpdateModel(t);
78 
79  int n = jac.cols();
80  Jacobian jac_model(k6D,n);
81 
82  // sensitivity of dynamic constraint w.r.t base variables.
83  if (var_set == id::base_lin_nodes) {
84  Jacobian jac_base_lin_pos = base_linear_->GetJacobianWrtNodes(t,kPos);
85  Jacobian jac_base_lin_acc = base_linear_->GetJacobianWrtNodes(t,kAcc);
86 
87  jac_model = model_->GetJacobianWrtBaseLin(jac_base_lin_pos,
88  jac_base_lin_acc);
89  }
90 
91  if (var_set == id::base_ang_nodes) {
92  jac_model = model_->GetJacobianWrtBaseAng(base_angular_, t);
93  }
94 
95  // sensitivity of dynamic constraint w.r.t. endeffector variables
96  for (int ee=0; ee<model_->GetEECount(); ++ee) {
97  if (var_set == id::EEForceNodes(ee)) {
98  Jacobian jac_ee_force = ee_forces_.at(ee)->GetJacobianWrtNodes(t,kPos);
99  jac_model = model_->GetJacobianWrtForce(jac_ee_force, ee);
100  }
101 
102  if (var_set == id::EEMotionNodes(ee)) {
103  Jacobian jac_ee_pos = ee_motion_.at(ee)->GetJacobianWrtNodes(t,kPos);
104  jac_model = model_->GetJacobianWrtEEPos(jac_ee_pos, ee);
105  }
106 
107  if (var_set == id::EESchedule(ee)) {
108  Jacobian jac_f_dT = ee_forces_.at(ee)->GetJacobianOfPosWrtDurations(t);
109  jac_model += model_->GetJacobianWrtForce(jac_f_dT, ee);
110 
111  Jacobian jac_x_dT = ee_motion_.at(ee)->GetJacobianOfPosWrtDurations(t);
112  jac_model += model_->GetJacobianWrtEEPos(jac_x_dT, ee);
113  }
114  }
115 
116  jac.middleRows(GetRow(k,AX), k6D) = jac_model;
117 }
118 
119 void
121 {
122  auto com = base_linear_->GetPoint(t);
123 
124  Eigen::Matrix3d w_R_b = base_angular_.GetRotationMatrixBaseToWorld(t);
125  Eigen::Vector3d omega = base_angular_.GetAngularVelocityInWorld(t);
126  Eigen::Vector3d omega_dot = base_angular_.GetAngularAccelerationInWorld(t);
127 
128  int n_ee = model_->GetEECount();
129  std::vector<Eigen::Vector3d> ee_pos;
130  std::vector<Eigen::Vector3d> ee_force;
131  for (int ee=0; ee<n_ee; ++ee) {
132  ee_force.push_back(ee_forces_.at(ee)->GetPoint(t).p());
133  ee_pos.push_back(ee_motion_.at(ee)->GetPoint(t).p());
134  }
135 
136  model_->SetCurrent(com.p(), com.a(), w_R_b, omega, omega_dot, ee_force, ee_pos);
137 }
138 
139 } /* namespace towr */
Constraints evaluated at discretized times along a trajectory.
static constexpr int k6D
static const Bounds BoundZero
DynamicConstraint(const DynamicModel::Ptr &model, double T, double dt, const SplineHolder &spline_holder)
Construct a Dynamic constraint.
void UpdateModel(double t) const
Updates the model with the current state and forces.
void UpdateConstraintAtInstance(double t, int k, VectorXd &g) const override
Sets the constraint value a specific time t, corresponding to node k.
EulerConverter base_angular_
angular base state
NodeSpline::Ptr base_linear_
lin. base pos/vel/acc in world frame
static std::string EESchedule(uint ee)
void UpdateJacobianAtInstance(double t, int k, std::string, Jacobian &) const override
Sets Jacobian rows at a specific time t, corresponding to node k.
static const Dim6D AllDim6D[]
Vector3d GetAngularAccelerationInWorld(double t) const
Converts Euler angles, rates and rate derivatives o angular accelerations.
Builds splines from node values (pos/vel) and durations.
Definition: spline_holder.h:47
Converts Euler angles and derivatives to angular quantities.
NodeSpline::Ptr base_linear_
Definition: spline_holder.h:71
std::shared_ptr< DynamicModel > Ptr
Definition: dynamic_model.h:68
void SetRows(int num_rows)
static std::string EEMotionNodes(uint ee)
std::vector< NodeSpline::Ptr > ee_force_
Definition: spline_holder.h:75
Eigen::SparseMatrix< double, Eigen::RowMajor > Jacobian
NodeSpline::Ptr base_angular_
Definition: spline_holder.h:72
std::vector< NodeSpline::Ptr > ee_forces_
endeffector forces in world frame.
Vector3d GetAngularVelocityInWorld(double t) const
Converts Euler angles and Euler rates to angular velocities.
void UpdateBoundsAtInstance(double t, int k, VecBound &bounds) const override
Sets upper/lower bound a specific time t, corresponding to node k.
std::vector< NodeSpline::Ptr > ee_motion_
endeffector position in world frame.
MatrixSXd GetRotationMatrixBaseToWorld(double t) const
Converts the Euler angles at time t to a rotation matrix.
static std::string EEForceNodes(uint ee)
static const std::string base_ang_nodes
DynamicModel::Ptr model_
the dynamic model (e.g. Centroidal)
std::vector< Bounds > VecBound
std::vector< NodeSpline::Ptr > ee_motion_
Definition: spline_holder.h:74
static const std::string base_lin_nodes
Eigen::VectorXd VectorXd
int GetRow(int k, Dim6D dimension) const
The row in the overall constraint for this evaluation time.


towr
Author(s): Alexander W. Winkler
autogenerated on Sat Apr 13 2019 02:28:00