33 template <
class KINEMATICS,
bool FB,
size_t STATE_DIM,
size_t CONTROL_DIM>
34 class TermTaskspacePose :
public optcon::TermBase<STATE_DIM, CONTROL_DIM, double, ct::core::ADCGScalar>
37 using SCALAR = ct::core::ADCGScalar;
38 using BASE = optcon::TermBase<STATE_DIM, CONTROL_DIM, double, ct::core::ADCGScalar>;
42 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
45 TermTaskspacePose() {}
47 TermTaskspacePose(
size_t eeInd,
48 const Eigen::Matrix<double, 3, 3>&
Qpos,
51 const std::string& name =
"TermTaskSpace")
53 : TermTaskspacePose(eeInd,
56 rbdPose.position().toImplementation(),
57 rbdPose.getRotationQuaternion().toImplementation(),
62 TermTaskspacePose(
size_t eeInd,
63 const Eigen::Matrix<double, 3, 3>&
Qpos,
65 const core::StateVector<3, double>& w_pos_des,
66 const Eigen::Quaternion<double>& w_q_des,
67 const std::string& name =
"TermTaskSpace")
73 w_R_ref_(w_q_des.normalized().toRotationMatrix())
78 TermTaskspacePose(
size_t eeInd,
79 const Eigen::Matrix<double, 3, 3>&
Qpos,
81 const core::StateVector<3, double>& w_pos_des,
82 const Eigen::Matrix<double, 3, 1>& eulerXyz,
83 const std::string& name =
"TermTaskSpace")
85 : TermTaskspacePose(eeInd,
98 TermTaskspacePose(std::string& configFile,
const std::string& termName,
bool verbose =
false)
100 loadConfigFile(configFile, termName,
verbose);
104 TermTaskspacePose(
const TermTaskspacePose& arg)
107 kinematics_(KINEMATICS()),
110 w_p_ref_(arg.w_p_ref_),
111 w_R_ref_(arg.w_R_ref_)
116 virtual ~TermTaskspacePose() {}
118 virtual TermTaskspacePose<KINEMATICS, FB, STATE_DIM, CONTROL_DIM>* clone()
const override 120 return new TermTaskspacePose(*
this);
124 virtual SCALAR evaluate(
const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
125 const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
128 return evalLocal<SCALAR>(x, u, t);
132 virtual ct::core::ADCGScalar evaluateCppadCg(
const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
133 const core::ControlVector<CONTROL_DIM, ct::core::ADCGScalar>& u,
134 ct::core::ADCGScalar t)
override 136 return evalLocal<ct::core::ADCGScalar>(x, u, t);
140 void loadConfigFile(
const std::string& filename,
const std::string& termName,
bool verbose =
false)
override 143 std::cout <<
"Loading TermTaskspacePose from file " << filename << std::endl;
153 std::cout <<
"trying to load euler angles" << std::endl;
158 Eigen::Quaternion<double> quat_des(Eigen::AngleAxisd(eulerXyz(0), Eigen::Vector3d::UnitX()) *
159 Eigen::AngleAxisd(eulerXyz(1), Eigen::Vector3d::UnitY()) *
160 Eigen::AngleAxisd(eulerXyz(2), Eigen::Vector3d::UnitZ()));
161 w_R_ref_ = quat_des.toRotationMatrix();
163 std::cout <<
"Read desired Euler Angles Xyz as = \n" << eulerXyz.transpose() << std::endl;
164 }
catch (
const std::exception& e)
166 throw std::runtime_error(
167 "Failed to load TermTaskspacePose, could not find a desired end effector orientation in file.");
172 std::cout <<
"Read eeId as eeId = \n" << eeInd_ << std::endl;
173 std::cout <<
"Read Q_pos as Q_pos = \n" << Q_pos_ << std::endl;
174 std::cout <<
"Read Q_rot as Q_rot = \n" << Q_rot_ << std::endl;
175 std::cout <<
"Read x_des as x_des = \n" << w_p_ref_.transpose() << std::endl;
181 const Eigen::Matrix<double, 3, 1>& getReferencePosition()
const {
return w_p_ref_; }
183 const Eigen::Quaterniond getReferenceOrientation()
const {
return Eigen::Quaterniond(w_R_ref_); }
185 void setReferencePosition(
const Eigen::Matrix<double, 3, 1>& w_p_ref) { w_p_ref_ = w_p_ref; }
187 void setReferenceOrientation(
const Eigen::Matrix<double, 3, 3> w_R_ref) { w_R_ref_ = w_R_ref; }
202 virtual RBDStateTpl setStateFromVector(
const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x)
204 return setStateFromVector_specialized<FB>(x);
210 RBDStateTpl setStateFromVector_specialized(
const Eigen::Matrix<SCALAR, -1, 1>& x,
211 typename std::enable_if<T, bool>::type =
true)
213 RBDStateTpl rbdState;
214 rbdState.fromStateVectorEulerXyz(x);
221 RBDStateTpl setStateFromVector_specialized(
const Eigen::Matrix<SCALAR, -1, 1>& x,
222 typename std::enable_if<!T, bool>::type =
true)
224 RBDStateTpl rbdState;
225 rbdState.joints().toImplementation() = x.head(STATE_DIM);
230 template <
typename SC>
231 SC evalLocal(
const Eigen::Matrix<SC, STATE_DIM, 1>& x,
const Eigen::Matrix<SC, CONTROL_DIM, 1>& u,
const SC& t)
233 using Matrix3Tpl = Eigen::Matrix<SC, 3, 3>;
236 RBDState<KINEMATICS::NJOINTS, SC> rbdState = setStateFromVector(x);
239 Eigen::Matrix<SC, 3, 1> xDiff =
240 kinematics_.getEEPositionInWorld(eeInd_, rbdState.basePose(), rbdState.jointPositions())
241 .toImplementation() -
242 w_p_ref_.template cast<SC>();
245 SC pos_cost = (xDiff.transpose() * Q_pos_.template cast<SC>() * xDiff)(0, 0);
249 Matrix3Tpl ee_rot = kinematics_.getEERotInWorld(eeInd_, rbdState.basePose(), rbdState.jointPositions());
255 Matrix3Tpl ee_rot_diff = w_R_ref_.template cast<SC>().transpose() * ee_rot;
258 (SC)Q_rot_ * (ee_rot_diff - Matrix3Tpl::Identity()).squaredNorm();
260 return pos_cost + rot_cost;
267 KINEMATICS kinematics_;
270 Eigen::Matrix<double, 3, 3> Q_pos_;
276 Eigen::Matrix<double, 3, 1> w_p_ref_;
279 Eigen::Matrix<double, 3, 3> w_R_ref_;
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
CppAD::AD< CppAD::cg::CG< double > > SCALAR
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
void loadMatrixCF(const std::string &filename, const std::string &matrixName, Eigen::Matrix< SCALAR, ROW, COL > &matrix, const std::string &termName="")