mj-kdl-wrapper  0.1.0
MuJoCo + KDL bridge for robot kinematics and dynamics
Loading...
Searching...
No Matches
test_init.cpp
Go to the documentation of this file.
1/* test_init.cpp
2 * Loads the Kinova GEN3 MJCF (menagerie gen3.xml), runs 100 simulation steps,
3 * and verifies basic model properties are consistent.
4 * Self-skips when third_party/menagerie is absent. */
5
7#include "test_utils.hpp"
8
9#include <gtest/gtest.h>
10
11#include <iostream>
12#include <string>
13#include <filesystem>
14
15static constexpr double kHomePose[7] = { 0.0, 0.2618, 3.1416, -2.2689, 0.0, 0.9599, 1.5708 };
16
17namespace fs = std::filesystem;
18static fs::path repo_root() { return fs::path(__FILE__).parent_path().parent_path(); }
19
20class InitTest : public testing::Test
21{
22 protected:
23 mjModel *model_ = nullptr;
24 mjData *data_ = nullptr;
26
27 void SetUp() override
28 {
29 fs::path root = repo_root();
30 if (!fs::exists(root / "third_party/menagerie")) {
31 GTEST_SKIP() << "third_party/menagerie/ not found";
32 return;
33 }
34
35 std::string mjcf = (root / "third_party/menagerie/kinova_gen3/gen3.xml").string();
36
39 r.path = mjcf.c_str();
40 sc.robots.push_back(r);
41
42 ASSERT_TRUE(mj_kdl::build_scene(&model_, &data_, &sc)) << "build_scene() returned false";
43 ASSERT_TRUE(mj_kdl::init_robot_from_mjcf(&s, model_, data_, "base_link", "bracelet_link"))
44 << "init_robot_from_mjcf() returned false";
45 }
46
47 void TearDown() override
48 {
51 }
52};
53
54TEST_F(InitTest, BasicDOF)
55{
56 EXPECT_EQ(s.n_joints, 7) << "expected 7 KDL joints, got " << s.n_joints;
57 TEST_INFO("nq=" << s.model->nq << " nv=" << s.model->nv << " kdl_joints=" << s.n_joints);
58}
59
60TEST_F(InitTest, SimulationAdvance)
61{
62 unsigned n = static_cast<unsigned>(s.n_joints);
63 KDL::JntArray q_home(n);
64 for (unsigned i = 0; i < n; ++i) q_home(i) = kHomePose[i];
65 mj_kdl::set_joint_pos(&s, q_home);
66 mj_forward(s.model, s.data);
67
68 const double t0 = s.data->time;
69 mj_kdl::step_n(&s, 100);
70 ASSERT_TRUE(s.data->time > t0) << "simulation time did not advance after 100 steps";
71 TEST_INFO("sim_time=" << s.data->time);
72}
73
74int main(int argc, char *argv[])
75{
76 testing::InitGoogleTest(&argc, argv);
77 return RUN_ALL_TESTS();
78}
mjModel * model_
Definition test_init.cpp:23
mjData * data_
Definition test_init.cpp:24
mj_kdl::Robot s
Definition test_init.cpp:25
void SetUp() override
Definition test_init.cpp:27
void TearDown() override
Definition test_init.cpp:47
bool init_robot_from_mjcf(Robot *r, mjModel *model, mjData *data, const char *base_body, const char *tip_body, const char *prefix="", const char *tool_body=nullptr)
void cleanup(Robot *r)
void step_n(Robot *s, int n)
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< RobotSpec > robots
int main(int argc, char *argv[])
Definition test_init.cpp:74
TEST_F(InitTest, BasicDOF)
Definition test_init.cpp:54
#define TEST_INFO(msg)