12#include <gtest/gtest.h>
19static constexpr double kHomePose[7] = { 0.0, 0.2618, 3.1416, -2.2689, 0.0, 0.9599, 1.5708 };
20static constexpr double kTargetPose[7] = { 0.3, 0.5, 2.9, -2.0, 0.3, 1.2, 1.3 };
21static constexpr double kMotionDuration = 1.5;
22static constexpr double kSettleTime = 0.5;
23static constexpr double kErrTol = 0.05;
25namespace fs = std::filesystem;
26static fs::path repo_root() {
return fs::path(__FILE__).parent_path().parent_path(); }
40 std::string arm_mjcf = (
root_ /
"third_party/menagerie/kinova_gen3/gen3.xml").
string();
41 if (!fs::exists(arm_mjcf)) {
42 GTEST_SKIP() << arm_mjcf <<
" not found";
54 KDL::JntArray q_home(
n_);
55 for (
unsigned i = 0; i <
n_; ++i) q_home(i) = kHomePose[i];
75 const double t_start = data_->time;
76 const double t_end = t_start + kMotionDuration + kSettleTime;
78 while (data_->time < t_end) {
80 double alpha = std::clamp((data_->time - t_start) / kMotionDuration, 0.0, 1.0);
81 for (
unsigned i = 0; i < n_; ++i)
82 s_.jnt_pos_cmd[i] = kHomePose[i] + alpha * (kTargetPose[i] - kHomePose[i]);
87 for (
unsigned i = 0; i < n_; ++i)
88 max_err = std::max(max_err, std::abs(kTargetPose[i] - s_.jnt_pos_msr[i]));
90 EXPECT_LE(max_err, kErrTol);
97 for (
unsigned i = 0; i < n_; ++i) s_.jnt_pos_cmd[i] = 1e9;
100 for (
unsigned i = 0; i < n_; ++i) {
101 const int ci = s_.kdl_to_mj_ctrl[i];
102 if (ci < 0)
continue;
103 if (!model_->actuator_ctrllimited[ci])
continue;
104 double lo = model_->actuator_ctrlrange[2 * ci];
105 double hi = model_->actuator_ctrlrange[2 * ci + 1];
106 EXPECT_LE(data_->ctrl[ci], hi + 1e-12)
107 <<
"ctrl[" << ci <<
"] exceeds ctrlrange upper bound";
108 EXPECT_GE(data_->ctrl[ci], lo - 1e-12)
109 <<
"ctrl[" << ci <<
"] below ctrlrange lower bound";
117 const int dof0 = s_.kdl_to_mj_dof[0];
118 data_->qfrc_applied[dof0] = 5.0;
120 EXPECT_DOUBLE_EQ(data_->qfrc_applied[dof0], 5.0)
121 <<
"update() clobbered qfrc_applied in POSITION mode";
124int main(
int argc,
char *argv[])
126 testing::InitGoogleTest(&argc, argv);
127 return RUN_ALL_TESTS();
bool init_robot_from_mjcf(Robot *r, mjModel *model, mjData *data, const char *base_body, const char *tip_body, const char *prefix="", const ToolFrameSpec *tool=nullptr)
void set_joint_pos(Robot *r, const KDL::JntArray &q, bool call_forward=true)
bool build_scene(mjModel **out_model, mjData **out_data, const SceneSpec *spec)
void destroy_scene(mjModel *model, mjData *data)
std::vector< double > jnt_pos_cmd
std::vector< RobotSpec > robots
int main(int argc, char *argv[])
TEST_F(MjcfPosCtrlTest, TrajectoryTracking)