7#include <gtest/gtest.h>
12#include <kdl/chainfksolverpos_recursive.hpp>
13#include <kdl/chaindynparam.hpp>
19static constexpr double kHomePose[7] = { 0.0, 0.2618, 3.1416, -2.2689, 0.0, 0.9599, 1.5708 };
21namespace fs = std::filesystem;
25static constexpr double kCubeMass = 0.1;
26static constexpr double kSphereMass = 0.05;
27static constexpr double kPickFriction[3] = { 0.8, 0.02, 0.001 };
30 const char *name,
double x,
double y,
double hx,
double hy,
double hz,
31 float r,
float g,
float b,
double surface_z
38 .size = { hx, hy, hz },
39 .pos = { x, y, surface_z + hz },
40 .rgba = { r, g, b, 1.0f },
42 .friction = { kPickFriction[0], kPickFriction[1], kPickFriction[2] },
47 const char *name,
double x,
double y,
double radius,
48 float r,
float g,
float b,
double surface_z
55 .size = { radius, 0.0, 0.0 },
56 .pos = { x, y, surface_z + radius },
57 .rgba = { r, g, b, 1.0f },
59 .friction = { kPickFriction[0], kPickFriction[1], kPickFriction[2] },
75 std::unique_ptr<KDL::ChainFkSolverPos_recursive>
fk_;
76 std::unique_ptr<KDL::ChainDynParam>
dyn_;
84 if (!fs::exists(
mjcf_)) {
85 GTEST_SKIP() <<
mjcf_ <<
" not found";
93 const double surface_z = 0.7;
97 .mjcf_path = table_mjcf,
98 .pos = { 0.0, 0.0, surface_z },
103 std::vector<mj_kdl::SceneObject> objects;
105 objects.push_back(make_box(
"red_cube", 0.35, 0.10, 0.03, 0.03, 0.03, 1.0f, 0.2f, 0.2f, surface_z));
106 objects.push_back(make_box(
"green_cube", 0.35, -0.10, 0.03, 0.03, 0.03, 0.2f, 1.0f, 0.2f, surface_z));
107 objects.push_back(make_box(
"blue_cube", 0.35, 0.30, 0.04, 0.04, 0.04, 0.2f, 0.2f, 1.0f, surface_z));
108 objects.push_back(make_sphere(
"orange_sphere", -0.20, 0.20, 0.035, 1.0f, 0.55f, 0.0f, surface_z));
109 objects.push_back(make_sphere(
"purple_sphere", -0.20, -0.20, 0.025, 0.7f, 0.0f, 0.9f, surface_z));
111 spec_.objects = objects;
112 spec_.timestep = 0.002;
113 spec_.gravity_z = -9.81;
114 spec_.add_floor =
true;
115 spec_.add_skybox =
true;
118 .path =
mjcf_.c_str(),
124 KDL::Frame world_T_table_top;
126 EXPECT_NEAR(world_T_table_top.p.z(), surface_z, 1e-9);
130 n_ =
static_cast<unsigned>(
s_.n_joints);
132 fk_ = std::make_unique<KDL::ChainFkSolverPos_recursive>(
s_.chain);
133 dyn_ = std::make_unique<KDL::ChainDynParam>(
s_.chain, KDL::Vector(0, 0,
spec_.gravity_z));
136 for (
unsigned i = 0; i <
n_; ++i)
q_home_(i) = kHomePose[i];
151 fk_->JntToCart(q_home_, ee_init);
154 KDL::JntArray q(n_), g(n_);
156 dyn_->JntToGravity(q_home_, g);
157 for (
unsigned j = 0; j < n_; ++j) s_.jnt_trq_cmd[j] = g(j);
158 for (
int i = 0; i < 500; ++i) {
160 for (
unsigned j = 0; j < n_; ++j) q(j) = s_.jnt_pos_msr[j];
161 dyn_->JntToGravity(q, g);
162 for (
unsigned j = 0; j < n_; ++j) s_.jnt_trq_cmd[j] = g(j);
166 KDL::JntArray q_end(n_);
168 for (
unsigned j = 0; j < n_; ++j) q_end(j) = s_.jnt_pos_msr[j];
169 fk_->JntToCart(q_end, ee_end);
170 double drift = (ee_init.p - ee_end.p).Norm();
172 ASSERT_LE(drift, 0.001) <<
"drift " << drift * 1000.0 <<
" mm exceeds 1 mm threshold";
180 const double surface_z = 0.7;
182 make_box(
"yellow_cube", 0.0, 0.4, 0.03, 0.03, 0.03, 1.0f, 1.0f, 0.0f, surface_z);
185 <<
"scene_add_object() returned false";
188 <<
"scene_remove_object() returned false";
202 int nq_before = env.
model->nq;
204 const double surface_z = 0.7;
206 make_box(
"yellow_cube", 0.0, 0.4, 0.03, 0.03, 0.03, 1.0f, 1.0f, 0.0f, surface_z);
209 EXPECT_GT(env.
model->nq, nq_before) <<
"nq should grow after adding a free object";
210 EXPECT_EQ(s_.n_joints, 7) <<
"robot chain should still have 7 joints after rebuild";
211 EXPECT_EQ(s_.model, env.
model) <<
"robot model pointer should be updated";
214 EXPECT_EQ(env.
model->nq, nq_before) <<
"nq should return to original after removal";
215 EXPECT_EQ(s_.n_joints, 7) <<
"robot chain should still have 7 joints after removal";
221int main(
int argc,
char *argv[])
223 testing::InitGoogleTest(&argc, argv);
224 return RUN_ALL_TESTS();
mj_kdl::SceneObject table_obj_
std::string table_mount_site_
std::unique_ptr< KDL::ChainDynParam > dyn_
std::unique_ptr< KDL::ChainFkSolverPos_recursive > fk_
void env_add_robot(Env *env, Robot *robot)
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)
std::string find_asset(const fs::path &relative)
std::string find_menagerie_model(const fs::path &relative)
int main(int argc, char *argv[])
TEST_F(TableSceneTest, GravityCompDrift)