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-Constraint-" + 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<PhaseNodes>(id::EEForceNodes(ee_));
00054 ee_motion_ = x->GetComponent<PhaseNodes>(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
00071 int phase = ee_force_->GetPhase(f_node_id);
00072 Vector3d p = ee_motion_->GetValueAtStartOfPhase(phase);
00073 Vector3d n = terrain_->GetNormalizedBasis(HeightMap::Normal, p.x(), p.y());
00074 Vector3d f = force_nodes.at(f_node_id).p();
00075
00076
00077 g(row++) = f.transpose() * n;
00078
00079
00080 Vector3d t1 = terrain_->GetNormalizedBasis(HeightMap::Tangent1, p.x(), p.y());
00081 g(row++) = f.transpose() * (t1 - mu_*n);
00082 g(row++) = f.transpose() * (t1 + mu_*n);
00083
00084 Vector3d t2 = terrain_->GetNormalizedBasis(HeightMap::Tangent2, p.x(), p.y());
00085 g(row++) = f.transpose() * (t2 - mu_*n);
00086 g(row++) = f.transpose() * (t2 + mu_*n);
00087 }
00088
00089 return g;
00090 }
00091
00092 ForceConstraint::VecBound
00093 ForceConstraint::GetBounds () const
00094 {
00095 VecBound bounds;
00096
00097 for (int f_node_id : pure_stance_force_node_ids_) {
00098 bounds.push_back(ifopt::Bounds(0.0, fn_max_));
00099 bounds.push_back(ifopt::BoundSmallerZero);
00100 bounds.push_back(ifopt::BoundGreaterZero);
00101 bounds.push_back(ifopt::BoundSmallerZero);
00102 bounds.push_back(ifopt::BoundGreaterZero);
00103 }
00104
00105 return bounds;
00106 }
00107
00108 void
00109 ForceConstraint::FillJacobianBlock (std::string var_set,
00110 Jacobian& jac) const
00111 {
00112 if (var_set == ee_force_->GetName()) {
00113
00114 int row = 0;
00115 for (int f_node_id : pure_stance_force_node_ids_) {
00116
00117
00118 int phase = ee_force_->GetPhase(f_node_id);
00119 Vector3d p = ee_motion_->GetValueAtStartOfPhase(phase);
00120 Vector3d n = terrain_->GetNormalizedBasis(HeightMap::Normal, p.x(), p.y());
00121 Vector3d t1 = terrain_->GetNormalizedBasis(HeightMap::Tangent1, p.x(), p.y());
00122 Vector3d t2 = terrain_->GetNormalizedBasis(HeightMap::Tangent2, p.x(), p.y());
00123
00124 for (auto dim : {X,Y,Z}) {
00125 int idx = ee_force_->Index(f_node_id, kPos, dim);
00126
00127 int row_reset=row;
00128
00129 jac.coeffRef(row_reset++, idx) = n(dim);
00130 jac.coeffRef(row_reset++, idx) = t1(dim)-mu_*n(dim);
00131 jac.coeffRef(row_reset++, idx) = t1(dim)+mu_*n(dim);
00132 jac.coeffRef(row_reset++, idx) = t2(dim)-mu_*n(dim);
00133 jac.coeffRef(row_reset++, idx) = t2(dim)+mu_*n(dim);
00134 }
00135
00136 row += n_constraints_per_node_;
00137 }
00138 }
00139
00140
00141 if (var_set == ee_motion_->GetName()) {
00142
00143 int row = 0;
00144 auto force_nodes = ee_force_->GetNodes();
00145 for (int f_node_id : pure_stance_force_node_ids_) {
00146
00147 int phase = ee_force_->GetPhase(f_node_id);
00148 int ee_node_id = ee_motion_->GetNodeIDAtStartOfPhase(phase);
00149
00150 Vector3d p = ee_motion_->GetValueAtStartOfPhase(phase);
00151 Vector3d f = force_nodes.at(f_node_id).p();
00152
00153 for (auto dim : {X_,Y_}) {
00154
00155 Vector3d dn = terrain_->GetDerivativeOfNormalizedBasisWrt(HeightMap::Normal, dim, p.x(), p.y());
00156 Vector3d dt1 = terrain_->GetDerivativeOfNormalizedBasisWrt(HeightMap::Tangent1, dim, p.x(), p.y());
00157 Vector3d dt2 = terrain_->GetDerivativeOfNormalizedBasisWrt(HeightMap::Tangent2, dim, p.x(), p.y());
00158
00159 int idx = ee_motion_->Index(ee_node_id, kPos, dim);
00160 int row_reset=row;
00161
00162
00163 jac.coeffRef(row_reset++, idx) = f.transpose()*dn;
00164
00165
00166 jac.coeffRef(row_reset++, idx) = f.transpose()*(dt1-mu_*dn);
00167 jac.coeffRef(row_reset++, idx) = f.transpose()*(dt1+mu_*dn);
00168
00169
00170 jac.coeffRef(row_reset++, idx) = f.transpose()*(dt2-mu_*dn);
00171 jac.coeffRef(row_reset++, idx) = f.transpose()*(dt2+mu_*dn);
00172 }
00173
00174 row += n_constraints_per_node_;
00175 }
00176 }
00177 }
00178
00179 }