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/constraints/force_constraint.h>
00031
00032 #include <towr/variables/variable_names.h>
00033
00034 namespace towr {
00035
00036
00037 ForceConstraint::ForceConstraint (const HeightMap::Ptr& terrain,
00038 double force_limit,
00039 EE ee)
00040 :ifopt::ConstraintSet(kSpecifyLater, "force-" + id::EEForceNodes(ee))
00041 {
00042 terrain_ = terrain;
00043 fn_max_ = force_limit;
00044 mu_ = terrain->GetFrictionCoeff();
00045 ee_ = ee;
00046
00047 n_constraints_per_node_ = 1 + 2*k2D;
00048 }
00049
00050 void
00051 ForceConstraint::InitVariableDependedQuantities (const VariablesPtr& x)
00052 {
00053 ee_force_ = x->GetComponent<NodesVariablesPhaseBased>(id::EEForceNodes(ee_));
00054 ee_motion_ = x->GetComponent<NodesVariablesPhaseBased>(id::EEMotionNodes(ee_));
00055
00056 pure_stance_force_node_ids_ = ee_force_->GetIndicesOfNonConstantNodes();
00057
00058 int constraint_count = pure_stance_force_node_ids_.size()*n_constraints_per_node_;
00059 SetRows(constraint_count);
00060 }
00061
00062 Eigen::VectorXd
00063 ForceConstraint::GetValues () const
00064 {
00065 VectorXd g(GetRows());
00066
00067 int row=0;
00068 auto force_nodes = ee_force_->GetNodes();
00069 for (int f_node_id : pure_stance_force_node_ids_) {
00070 int phase = ee_force_->GetPhase(f_node_id);
00071 Vector3d p = ee_motion_->GetValueAtStartOfPhase(phase);
00072 Vector3d n = terrain_->GetNormalizedBasis(HeightMap::Normal, p.x(), p.y());
00073 Vector3d f = force_nodes.at(f_node_id).p();
00074
00075
00076 g(row++) = f.transpose() * n;
00077
00078
00079 Vector3d t1 = terrain_->GetNormalizedBasis(HeightMap::Tangent1, p.x(), p.y());
00080 g(row++) = f.transpose() * (t1 - mu_*n);
00081 g(row++) = f.transpose() * (t1 + mu_*n);
00082
00083 Vector3d t2 = terrain_->GetNormalizedBasis(HeightMap::Tangent2, p.x(), p.y());
00084 g(row++) = f.transpose() * (t2 - mu_*n);
00085 g(row++) = f.transpose() * (t2 + mu_*n);
00086 }
00087
00088 return g;
00089 }
00090
00091 ForceConstraint::VecBound
00092 ForceConstraint::GetBounds () const
00093 {
00094 VecBound bounds;
00095
00096 for (int f_node_id : pure_stance_force_node_ids_) {
00097 bounds.push_back(ifopt::Bounds(0.0, fn_max_));
00098 bounds.push_back(ifopt::BoundSmallerZero);
00099 bounds.push_back(ifopt::BoundGreaterZero);
00100 bounds.push_back(ifopt::BoundSmallerZero);
00101 bounds.push_back(ifopt::BoundGreaterZero);
00102 }
00103
00104 return bounds;
00105 }
00106
00107 void
00108 ForceConstraint::FillJacobianBlock (std::string var_set,
00109 Jacobian& jac) const
00110 {
00111 if (var_set == ee_force_->GetName()) {
00112 int row = 0;
00113 for (int f_node_id : pure_stance_force_node_ids_) {
00114
00115 int phase = ee_force_->GetPhase(f_node_id);
00116 Vector3d p = ee_motion_->GetValueAtStartOfPhase(phase);
00117 Vector3d n = terrain_->GetNormalizedBasis(HeightMap::Normal, p.x(), p.y());
00118 Vector3d t1 = terrain_->GetNormalizedBasis(HeightMap::Tangent1, p.x(), p.y());
00119 Vector3d t2 = terrain_->GetNormalizedBasis(HeightMap::Tangent2, p.x(), p.y());
00120
00121 for (auto dim : {X,Y,Z}) {
00122 int idx = ee_force_->GetOptIndex(NodesVariables::NodeValueInfo(f_node_id, kPos, dim));
00123
00124 int row_reset=row;
00125
00126 jac.coeffRef(row_reset++, idx) = n(dim);
00127 jac.coeffRef(row_reset++, idx) = t1(dim)-mu_*n(dim);
00128 jac.coeffRef(row_reset++, idx) = t1(dim)+mu_*n(dim);
00129 jac.coeffRef(row_reset++, idx) = t2(dim)-mu_*n(dim);
00130 jac.coeffRef(row_reset++, idx) = t2(dim)+mu_*n(dim);
00131 }
00132
00133 row += n_constraints_per_node_;
00134 }
00135 }
00136
00137
00138 if (var_set == ee_motion_->GetName()) {
00139 int row = 0;
00140 auto force_nodes = ee_force_->GetNodes();
00141 for (int f_node_id : pure_stance_force_node_ids_) {
00142 int phase = ee_force_->GetPhase(f_node_id);
00143 int ee_node_id = ee_motion_->GetNodeIDAtStartOfPhase(phase);
00144
00145 Vector3d p = ee_motion_->GetValueAtStartOfPhase(phase);
00146 Vector3d f = force_nodes.at(f_node_id).p();
00147
00148 for (auto dim : {X_,Y_}) {
00149 Vector3d dn = terrain_->GetDerivativeOfNormalizedBasisWrt(HeightMap::Normal, dim, p.x(), p.y());
00150 Vector3d dt1 = terrain_->GetDerivativeOfNormalizedBasisWrt(HeightMap::Tangent1, dim, p.x(), p.y());
00151 Vector3d dt2 = terrain_->GetDerivativeOfNormalizedBasisWrt(HeightMap::Tangent2, dim, p.x(), p.y());
00152
00153 int idx = ee_motion_->GetOptIndex(NodesVariables::NodeValueInfo(ee_node_id, kPos, dim));
00154 int row_reset=row;
00155
00156
00157 jac.coeffRef(row_reset++, idx) = f.transpose()*dn;
00158
00159
00160 jac.coeffRef(row_reset++, idx) = f.transpose()*(dt1-mu_*dn);
00161 jac.coeffRef(row_reset++, idx) = f.transpose()*(dt1+mu_*dn);
00162
00163
00164 jac.coeffRef(row_reset++, idx) = f.transpose()*(dt2-mu_*dn);
00165 jac.coeffRef(row_reset++, idx) = f.transpose()*(dt2+mu_*dn);
00166 }
00167
00168 row += n_constraints_per_node_;
00169 }
00170 }
00171 }
00172
00173 }