dynamic_constraint.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/constraints/dynamic_constraint.h>
00031 
00032 #include <towr/variables/variable_names.h>
00033 #include <towr/variables/cartesian_dimensions.h>
00034 
00035 namespace towr {
00036 
00037 DynamicConstraint::DynamicConstraint (const DynamicModel::Ptr& m,
00038                                       double T, double dt,
00039                                       const SplineHolder& spline_holder)
00040     :TimeDiscretizationConstraint(T, dt, "dynamic")
00041 {
00042   model_ = m;
00043 
00044   // link with up-to-date spline variables
00045   base_linear_  = spline_holder.base_linear_;
00046   base_angular_ = EulerConverter(spline_holder.base_angular_);
00047   ee_forces_    = spline_holder.ee_force_;
00048   ee_motion_    = spline_holder.ee_motion_;
00049 
00050   SetRows(GetNumberOfNodes()*k6D);
00051 }
00052 
00053 int
00054 DynamicConstraint::GetRow (int k, Dim6D dimension) const
00055 {
00056   return k6D*k + dimension;
00057 }
00058 
00059 void
00060 DynamicConstraint::UpdateConstraintAtInstance(double t, int k, VectorXd& g) const
00061 {
00062   UpdateModel(t);
00063   g.segment(GetRow(k,AX), k6D) = model_->GetDynamicViolation();
00064 }
00065 
00066 void
00067 DynamicConstraint::UpdateBoundsAtInstance(double t, int k, VecBound& bounds) const
00068 {
00069   for (auto dim : AllDim6D)
00070     bounds.at(GetRow(k,dim)) = ifopt::BoundZero;
00071 }
00072 
00073 void
00074 DynamicConstraint::UpdateJacobianAtInstance(double t, int k, std::string var_set,
00075                                             Jacobian& jac) const
00076 {
00077   UpdateModel(t);
00078 
00079   int n = jac.cols();
00080   Jacobian jac_model(k6D,n);
00081 
00082   // sensitivity of dynamic constraint w.r.t base variables.
00083   if (var_set == id::base_lin_nodes) {
00084     Jacobian jac_base_lin_pos = base_linear_->GetJacobianWrtNodes(t,kPos);
00085     Jacobian jac_base_lin_acc = base_linear_->GetJacobianWrtNodes(t,kAcc);
00086 
00087     jac_model = model_->GetJacobianWrtBaseLin(jac_base_lin_pos,
00088                                               jac_base_lin_acc);
00089   }
00090 
00091   if (var_set == id::base_ang_nodes) {
00092     jac_model = model_->GetJacobianWrtBaseAng(base_angular_, t);
00093   }
00094 
00095   // sensitivity of dynamic constraint w.r.t. endeffector variables
00096   for (int ee=0; ee<model_->GetEECount(); ++ee) {
00097     if (var_set == id::EEForceNodes(ee)) {
00098       Jacobian jac_ee_force = ee_forces_.at(ee)->GetJacobianWrtNodes(t,kPos);
00099       jac_model = model_->GetJacobianWrtForce(jac_ee_force, ee);
00100     }
00101 
00102     if (var_set == id::EEMotionNodes(ee)) {
00103       Jacobian jac_ee_pos = ee_motion_.at(ee)->GetJacobianWrtNodes(t,kPos);
00104       jac_model = model_->GetJacobianWrtEEPos(jac_ee_pos, ee);
00105     }
00106 
00107     if (var_set == id::EESchedule(ee)) {
00108       Jacobian jac_f_dT = ee_forces_.at(ee)->GetJacobianOfPosWrtDurations(t);
00109       jac_model += model_->GetJacobianWrtForce(jac_f_dT, ee);
00110 
00111       Jacobian jac_x_dT = ee_motion_.at(ee)->GetJacobianOfPosWrtDurations(t);
00112       jac_model +=  model_->GetJacobianWrtEEPos(jac_x_dT, ee);
00113     }
00114   }
00115 
00116   jac.middleRows(GetRow(k,AX), k6D) = jac_model;
00117 }
00118 
00119 void
00120 DynamicConstraint::UpdateModel (double t) const
00121 {
00122   auto com = base_linear_->GetPoint(t);
00123 
00124   Eigen::Matrix3d w_R_b = base_angular_.GetRotationMatrixBaseToWorld(t);
00125   Eigen::Vector3d omega = base_angular_.GetAngularVelocityInWorld(t);
00126   Eigen::Vector3d omega_dot = base_angular_.GetAngularAccelerationInWorld(t);
00127 
00128   int n_ee = model_->GetEECount();
00129   std::vector<Eigen::Vector3d> ee_pos;
00130   std::vector<Eigen::Vector3d> ee_force;
00131   for (int ee=0; ee<n_ee; ++ee) {
00132     ee_force.push_back(ee_forces_.at(ee)->GetPoint(t).p());
00133     ee_pos.push_back(ee_motion_.at(ee)->GetPoint(t).p());
00134   }
00135 
00136   model_->SetCurrent(com.p(), com.a(), w_R_b, omega, omega_dot, ee_force, ee_pos);
00137 }
00138 
00139 } /* namespace towr */


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