7#include <gtest/gtest.h>
11#include <kdl/chainfksolverpos_recursive.hpp>
12#include <kdl/chaindynparam.hpp>
18static constexpr double kHomePose[7] = { 0.0, 0.2618, 3.1416, -2.2689, 0.0, 0.9599, 1.5708 };
20namespace fs = std::filesystem;
21static fs::path repo_root() {
return fs::path(__FILE__).parent_path().parent_path(); }
24 const char *name,
double x,
double y,
double hx,
double hy,
double hz,
25 float r,
float g,
float b,
double surface_z
32 .size = { hx, hy, hz },
33 .pos = { x, y, surface_z + hz },
34 .rgba = { r, g, b, 1.0f },
39 const char *name,
double x,
double y,
double radius,
40 float r,
float g,
float b,
double surface_z
47 .size = { radius, 0.0, 0.0 },
48 .pos = { x, y, surface_z + radius },
49 .rgba = { r, g, b, 1.0f },
64 std::unique_ptr<KDL::ChainFkSolverPos_recursive>
fk_;
65 std::unique_ptr<KDL::ChainDynParam>
dyn_;
72 fs::path root = repo_root();
73 mjcf_ = (root /
"third_party/menagerie/kinova_gen3/gen3.xml").
string();
74 if (!fs::exists(
mjcf_)) {
75 GTEST_SKIP() <<
mjcf_ <<
" not found";
78 std::string table_mjcf = (root /
"src/examples/assets/table.xml").
string();
81 double surface_z = 0.7;
83 std::vector<mj_kdl::SceneObject> objects;
86 .mjcf_path = table_mjcf,
87 .pos = { 0.0, 0.0, surface_z },
92 make_box(
"red_cube", 0.35, 0.10, 0.03, 0.03, 0.03, 1.0f, 0.2f, 0.2f, surface_z)
95 make_box(
"green_cube", 0.35, -0.10, 0.03, 0.03, 0.03, 0.2f, 1.0f, 0.2f, surface_z)
98 make_box(
"blue_cube", 0.35, 0.30, 0.04, 0.04, 0.04, 0.2f, 0.2f, 1.0f, surface_z)
101 make_sphere(
"orange_sphere", -0.20, 0.20, 0.035, 1.0f, 0.55f, 0.0f, surface_z)
104 make_sphere(
"purple_sphere", -0.20, -0.20, 0.025, 0.7f, 0.0f, 0.9f, surface_z)
114 KDL::Frame world_T_table_top;
117 EXPECT_NEAR(world_T_table_top.p.z(), surface_z, 1e-9);
123 fk_ = std::make_unique<KDL::ChainFkSolverPos_recursive>(
s_.
chain);
127 for (
unsigned i = 0; i <
n_; ++i)
q_home_(i) = kHomePose[i];
142 fk_->JntToCart(q_home_, ee_init);
145 KDL::JntArray q(n_), g(n_);
147 dyn_->JntToGravity(q_home_, g);
148 for (
unsigned j = 0; j < n_; ++j) s_.jnt_trq_cmd[j] = g(j);
149 for (
int i = 0; i < 500; ++i) {
151 for (
unsigned j = 0; j < n_; ++j) q(j) = s_.jnt_pos_msr[j];
152 dyn_->JntToGravity(q, g);
153 for (
unsigned j = 0; j < n_; ++j) s_.jnt_trq_cmd[j] = g(j);
157 KDL::JntArray q_end(n_);
159 for (
unsigned j = 0; j < n_; ++j) q_end(j) = s_.jnt_pos_msr[j];
160 fk_->JntToCart(q_end, ee_end);
161 double drift = (ee_init.p - ee_end.p).Norm();
163 ASSERT_LE(drift, 0.001) <<
"drift " << drift * 1000.0 <<
" mm exceeds 1 mm threshold";
171 double surface_z = 0.7;
173 make_box(
"yellow_cube", 0.0, 0.4, 0.03, 0.03, 0.03, 1.0f, 1.0f, 0.0f, surface_z);
176 <<
"scene_add_object() returned false";
179 <<
"scene_remove_object() returned false";
193 int nq_before = env.
model->nq;
195 double surface_z = 0.7;
197 make_box(
"yellow_cube", 0.0, 0.4, 0.03, 0.03, 0.03, 1.0f, 1.0f, 0.0f, surface_z);
200 EXPECT_GT(env.
model->nq, nq_before) <<
"nq should grow after adding a free object";
201 EXPECT_EQ(s_.n_joints, 7) <<
"robot chain should still have 7 joints after rebuild";
202 EXPECT_EQ(s_.model, env.
model) <<
"robot model pointer should be updated";
205 EXPECT_EQ(env.
model->nq, nq_before) <<
"nq should return to original after removal";
206 EXPECT_EQ(s_.n_joints, 7) <<
"robot chain should still have 7 joints after removal";
212int main(
int argc,
char *argv[])
214 testing::InitGoogleTest(&argc, argv);
215 return RUN_ALL_TESTS();
mj_kdl::SceneObject table_obj_
std::unique_ptr< KDL::ChainDynParam > dyn_
std::unique_ptr< KDL::ChainFkSolverPos_recursive > fk_
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 get_site_frame(const mjModel *model, mjData *data, const char *site_name, KDL::Frame *out)
bool build_scene(mjModel **out_model, mjData **out_data, const SceneSpec *spec)
bool scene_add_object(mjModel **model, mjData **data, SceneSpec *spec, const SceneObject &obj)
bool scene_remove_object(mjModel **model, mjData **data, SceneSpec *spec, const std::string &name)
void destroy_scene(mjModel *model, mjData *data)
std::string scene_object_site_name(const SceneObject &obj, const char *site_name)
void env_add_robot(Env *env, Robot *robot)
std::vector< RobotSpec > robots
std::vector< SceneObject > objects
int main(int argc, char *argv[])
TEST_F(TableSceneTest, GravityCompDrift)