36 template <
class KINEMATICS,
bool FLOATING_BASE,
size_t STATE_DIM,
size_t CONTROL_DIM>
37 class TermTaskspacePoseCG :
public optcon::TermBase<STATE_DIM, CONTROL_DIM, double, double>
40 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
50 static const size_t AD_PARAMETER_DIM = STATE_DIM + CONTROL_DIM + 3 + 9 + 1;
52 using BASE = optcon::TermBase<STATE_DIM, CONTROL_DIM, double, double>;
54 using DerivativesCppadJIT = core::DerivativesCppadJIT<AD_PARAMETER_DIM, 1>;
55 using CGScalar =
typename DerivativesCppadJIT::CG_SCALAR;
59 using state_matrix_t = Eigen::Matrix<double, STATE_DIM, STATE_DIM>;
61 using control_state_matrix_t = Eigen::Matrix<double, CONTROL_DIM, STATE_DIM>;
65 TermTaskspacePoseCG() =
default;
68 TermTaskspacePoseCG(
size_t eeInd,
69 const Eigen::Matrix3d&
Qpos,
71 const core::StateVector<3>& w_pos_des,
72 const Eigen::Quaterniond& w_q_des,
73 const std::string& name =
"TermTaskSpace",
74 bool evalControlDerivatives =
true)
75 : BASE(name), eeInd_(eeInd), Q_pos_(Qpos), Q_rot_(Qrot), evalControlDerivatives_(evalControlDerivatives)
77 setReferencePosition(w_pos_des);
78 setReferenceOrientation(w_q_des);
83 TermTaskspacePoseCG(
size_t eeInd,
84 const Eigen::Matrix3d&
Qpos,
87 const std::string& name =
"TermTaskSpace",
88 bool evalControlDerivatives =
true)
90 : TermTaskspacePoseCG(eeInd,
93 rbdPose.position().toImplementation(),
94 rbdPose.getRotationQuaternion().toImplementation(),
96 evalControlDerivatives)
101 TermTaskspacePoseCG(
size_t eeInd,
102 const Eigen::Matrix3d&
Qpos,
104 const core::StateVector<3>& w_pos_des,
105 const Eigen::Matrix3d& eulerXyz,
106 const std::string& name =
"TermTaskSpace",
107 bool evalControlDerivatives =
true)
109 : TermTaskspacePoseCG(eeInd,
117 evalControlDerivatives)
122 TermTaskspacePoseCG(
size_t eeInd,
123 const Eigen::Matrix3d&
Qpos,
125 const std::string& name =
"TermTaskSpace",
126 bool evalControlDerivatives =
false)
127 : BASE(name), eeInd_(eeInd), Q_pos_(Qpos), Q_rot_(Qrot), evalControlDerivatives_(evalControlDerivatives)
130 Eigen::Quaterniond w_q_des(0.0, 0.0, 0.0, 1.0);
131 setReferenceOrientation(w_q_des);
132 setReferencePosition(core::StateVector<3>::Zero());
137 TermTaskspacePoseCG(
const std::string& configFile,
const std::string& termName,
bool verbose =
false)
139 loadConfigFile(configFile, termName,
verbose);
143 TermTaskspacePoseCG(
const TermTaskspacePoseCG& arg)
146 kinematics_(KINEMATICS()),
149 costFun_(arg.costFun_),
150 adParameterVector_(arg.adParameterVector_),
151 evalControlDerivatives_(arg.evalControlDerivatives_)
153 derivativesCppadJIT_ = std::shared_ptr<DerivativesCppadJIT>(arg.derivativesCppadJIT_->clone());
157 virtual ~TermTaskspacePoseCG() {}
159 virtual TermTaskspacePoseCG<KINEMATICS, FLOATING_BASE, STATE_DIM, CONTROL_DIM>* clone()
const override 161 return new TermTaskspacePoseCG(*
this);
168 costFun_ = [&](
const Eigen::Matrix<CGScalar, AD_PARAMETER_DIM, 1>& inputParams) {
169 return evalLocal<CGScalar>(inputParams);
173 derivativesCppadJIT_ =
174 std::shared_ptr<DerivativesCppadJIT>(
new DerivativesCppadJIT(costFun_, AD_PARAMETER_DIM, 1));
178 settings.createJacobian_ =
true;
179 settings.createHessian_ =
true;
181 std::cout <<
"compiling JIT for TermTaskspacePoseCG" << std::endl;
182 derivativesCppadJIT_->compileJIT(settings,
"TermTaskspacePoseCGcosts");
186 void setCurrentStateAndControl(
const Eigen::Matrix<double, STATE_DIM, 1>& x,
187 const Eigen::Matrix<double, CONTROL_DIM, 1>& u,
190 adParameterVector_.template segment<STATE_DIM>(0) = x;
191 adParameterVector_.template segment<CONTROL_DIM>(STATE_DIM) = u;
192 adParameterVector_(AD_PARAMETER_DIM - 1) = t;
197 virtual double evaluate(
const Eigen::Matrix<double, STATE_DIM, 1>& x,
198 const Eigen::Matrix<double, CONTROL_DIM, 1>& u,
199 const double& t)
override 201 setCurrentStateAndControl(x, u, t);
202 return derivativesCppadJIT_->forwardZero(adParameterVector_)(0);
207 virtual core::StateVector<STATE_DIM> stateDerivative(
const core::StateVector<STATE_DIM>& x,
208 const core::ControlVector<CONTROL_DIM>& u,
209 const double& t)
override 211 setCurrentStateAndControl(x, u, t);
212 Eigen::Matrix<double, 1, AD_PARAMETER_DIM> jacTot = derivativesCppadJIT_->jacobian(adParameterVector_);
213 return jacTot.template leftCols<STATE_DIM>().transpose();
217 virtual core::ControlVector<CONTROL_DIM> controlDerivative(
const core::StateVector<STATE_DIM>& x,
218 const core::ControlVector<CONTROL_DIM>& u,
219 const double& t)
override 221 if (evalControlDerivatives_)
223 setCurrentStateAndControl(x, u, t);
224 Eigen::Matrix<double, 1, AD_PARAMETER_DIM> jacTot = derivativesCppadJIT_->jacobian(adParameterVector_);
225 return jacTot.template block<1, CONTROL_DIM>(0, STATE_DIM).transpose();
228 return core::ControlVector<CONTROL_DIM>::Zero();
232 virtual state_matrix_t stateSecondDerivative(
const core::StateVector<STATE_DIM>& x,
233 const core::ControlVector<CONTROL_DIM>& u,
234 const double& t)
override 236 setCurrentStateAndControl(x, u, t);
237 Eigen::Matrix<double, 1, 1> w(1.0);
238 Eigen::MatrixXd hesTot = derivativesCppadJIT_->hessian(adParameterVector_, w);
239 return hesTot.template block<STATE_DIM, STATE_DIM>(0, 0);
243 virtual control_matrix_t controlSecondDerivative(
const core::StateVector<STATE_DIM>& x,
244 const core::ControlVector<CONTROL_DIM>& u,
245 const double& t)
override 247 if (evalControlDerivatives_)
249 setCurrentStateAndControl(x, u, t);
250 Eigen::Matrix<double, 1, 1> w(1.0);
251 Eigen::MatrixXd hesTot = derivativesCppadJIT_->hessian(adParameterVector_, w);
252 return hesTot.template block<CONTROL_DIM, CONTROL_DIM>(STATE_DIM, STATE_DIM);
255 return control_matrix_t::Zero();
259 virtual control_state_matrix_t stateControlDerivative(
const core::StateVector<STATE_DIM>& x,
260 const core::ControlVector<CONTROL_DIM>& u,
261 const double& t)
override 263 if (evalControlDerivatives_)
265 setCurrentStateAndControl(x, u, t);
266 Eigen::Matrix<double, 1, 1> w(1.0);
267 Eigen::MatrixXd hesTot = derivativesCppadJIT_->hessian(adParameterVector_, w);
268 return hesTot.template block<CONTROL_DIM, STATE_DIM>(STATE_DIM, 0);
271 return control_state_matrix_t::Zero();
276 void loadConfigFile(
const std::string& filename,
const std::string& termName,
bool verbose =
false)
override 279 std::cout <<
"Loading TermTaskspacePoseCG from file " << filename << std::endl;
286 Eigen::Matrix<double, 3, 1> w_p_ref;
288 setReferencePosition(w_p_ref);
295 std::cout <<
"trying to load euler angles" << std::endl;
300 Eigen::Quaternion<double> quat_des(Eigen::AngleAxisd(eulerXyz(0), Eigen::Vector3d::UnitX()) *
301 Eigen::AngleAxisd(eulerXyz(1), Eigen::Vector3d::UnitY()) *
302 Eigen::AngleAxisd(eulerXyz(2), Eigen::Vector3d::UnitZ()));
303 setReferenceOrientation(quat_des);
305 std::cout <<
"Read desired Euler Angles Xyz as = \n" << eulerXyz.transpose() << std::endl;
306 }
catch (
const std::exception& e)
308 throw std::runtime_error(
309 "Failed to load TermTaskspacePoseCG, could not find a desired end effector orientation in file.");
314 std::cout <<
"Read eeId as eeId = \n" << eeInd_ << std::endl;
315 std::cout <<
"Read Q_pos as Q_pos = \n" << Q_pos_ << std::endl;
316 std::cout <<
"Read Q_rot as Q_rot = \n" << Q_rot_ << std::endl;
317 std::cout <<
"Read x_des as x_des = \n" << getReferencePosition().transpose() << std::endl;
318 std::cout <<
"Read evalControlDerivatives as evalControlDerivatives = \n" 319 << evalControlDerivatives_ << std::endl;
328 const Eigen::Matrix<double, 3, 1> getReferencePosition()
const 330 return adParameterVector_.template segment<3>(STATE_DIM + CONTROL_DIM);
334 void setReferencePosition(Eigen::Matrix<double, 3, 1> w_p_ref)
336 adParameterVector_.template segment<3>(STATE_DIM + CONTROL_DIM) = w_p_ref;
340 const Eigen::Quaterniond getReferenceOrientation()
const 342 return Eigen::Quaterniond(extractReferenceRotationMatrix(adParameterVector_));
346 void setReferenceOrientation(
const Eigen::Matrix<double, 3, 3>& w_R_ref)
349 const Eigen::Matrix<double, 9, 1> matVectorized(
350 Eigen::Map<
const Eigen::Matrix<double, 9, 1>>(w_R_ref.data(), 9));
351 adParameterVector_.template segment<9>(STATE_DIM + CONTROL_DIM + 3) = matVectorized;
355 void setReferenceOrientation(
const Eigen::Quaterniond& w_q_des)
357 setReferenceOrientation(w_q_des.normalized().toRotationMatrix());
374 virtual RBDStateTpl setStateFromVector(
const Eigen::Matrix<CGScalar, STATE_DIM, 1>& x)
376 return setStateFromVector_specialized<FLOATING_BASE>(x);
381 template <
typename SC>
382 const Eigen::Matrix<SC, 3, 3> extractReferenceRotationMatrix(
383 const Eigen::Matrix<SC, AD_PARAMETER_DIM, 1>& parameterVector)
const 385 Eigen::Matrix<SC, 9, 1> matVectorized = parameterVector.template segment<9>(STATE_DIM + CONTROL_DIM + 3);
386 Eigen::Matrix<SC, 3, 3> w_R_ee(Eigen::Map<Eigen::Matrix<SC, 3, 3>>(matVectorized.data(), 3, 3));
391 template <
typename SC>
392 const Eigen::Matrix<SC, STATE_DIM, 1> extractStateVector(
393 const Eigen::Matrix<SC, AD_PARAMETER_DIM, 1>& parameterVector)
const 395 return parameterVector.template segment<STATE_DIM>(0);
399 template <
typename SC>
400 const Eigen::Matrix<SC, 3, 1> extractReferencePosition(
401 const Eigen::Matrix<SC, AD_PARAMETER_DIM, 1>& parameterVector)
const 403 return parameterVector.template segment<3>(STATE_DIM + CONTROL_DIM);
408 RBDStateTpl setStateFromVector_specialized(
const Eigen::Matrix<CGScalar, -1, 1>& x,
409 typename std::enable_if<FB, bool>::type =
true)
411 RBDStateTpl rbdState;
412 rbdState.fromStateVectorEulerXyz(x);
418 RBDStateTpl setStateFromVector_specialized(
const Eigen::Matrix<CGScalar, -1, 1>& x,
419 typename std::enable_if<!FB, bool>::type =
true)
421 RBDStateTpl rbdState;
422 rbdState.joints().toImplementation() = x.head(STATE_DIM);
428 template <
typename SC>
429 Eigen::Matrix<SC, 1, 1> evalLocal(
const Eigen::Matrix<SC, AD_PARAMETER_DIM, 1>& adParams)
432 Eigen::Matrix<SC, STATE_DIM, 1> x = extractStateVector(adParams);
433 Eigen::Matrix<SC, 3, 1> w_p_ref = extractReferencePosition(adParams);
434 Eigen::Matrix<SC, 3, 3> w_R_ref = extractReferenceRotationMatrix<SC>(adParams);
437 RBDState<KINEMATICS::NJOINTS, SC> rbdState = setStateFromVector(x);
440 Eigen::Matrix<SC, 3, 1> xCurr =
441 kinematics_.getEEPositionInWorld(eeInd_, rbdState.basePose(), rbdState.jointPositions()).toImplementation();
442 Eigen::Matrix<SC, 3, 1> xDiff = xCurr - w_p_ref;
446 SC pos_cost = (xDiff.transpose() * Q_pos_.template cast<SC>() * xDiff)(0, 0);
450 Eigen::Matrix<SC, 3, 3> ee_rot =
451 kinematics_.getEERotInWorld(eeInd_, rbdState.basePose(), rbdState.jointPositions());
457 Eigen::Matrix<SC, 3, 3> ee_R_diff = w_R_ref.transpose() * ee_rot;
460 SC rot_cost = (SC)Q_rot_ * (ee_R_diff - Eigen::Matrix<SC, 3, 3>::Identity()).squaredNorm();
462 Eigen::Matrix<SC, 1, 1> result;
463 result(0, 0) = (pos_cost + rot_cost);
471 KINEMATICS kinematics_;
474 Eigen::Matrix3d Q_pos_;
480 std::shared_ptr<DerivativesCppadJIT> derivativesCppadJIT_;
483 typename DerivativesCppadJIT::FUN_TYPE_CG costFun_;
486 Eigen::Matrix<double, AD_PARAMETER_DIM, 1> adParameterVector_;
491 bool evalControlDerivatives_;
Eigen::Matrix< double, nControls, nControls > control_matrix_t
Cored::Vector3 Vector3d
Definition: rbd.h:137
tpl::RigidBodyPose< double > RigidBodyPose
Definition: RigidBodyPose.h:426
Definition: eigen_traits.h:23
joint states and base states
Definition: RBDState.h:27
bool verbose
Definition: rbdJITtests.h:15
void loadScalarCF(const std::string &filename, const std::string &scalarName, SCALAR &scalar, const std::string &termName="")
double Qrot
Definition: rbdJITtests.h:28
const Eigen::Matrix< double, 3, 3 > Qpos
Definition: rbdJITtests.h:27
Eigen::Matrix< double, nStates, nStates > state_matrix_t
void loadMatrixCF(const std::string &filename, const std::string &matrixName, Eigen::Matrix< SCALAR, ROW, COL > &matrix, const std::string &termName="")