mj-kdl-wrapper  0.1.0
MuJoCo + KDL bridge for robot kinematics and dynamics
Loading...
Searching...
No Matches
ex_init.cpp
Go to the documentation of this file.
1/* ex_init.cpp (MJCF)
2 * Basic example: load the Kinova GEN3 from MuJoCo Menagerie MJCF,
3 * set the arm to home pose, and run the MuJoCo simulate UI.
4 *
5 * Requires third_party/menagerie submodule.
6 *
7 * Usage:
8 * ex_init_mjcf [--headless]
9 *
10 * With --headless exits after printing basic model information. */
11
13
14#include <kdl/chaindynparam.hpp>
15#include <kdl/chainfksolverpos_recursive.hpp>
16
17#include <filesystem>
18#include <iomanip>
19#include <iostream>
20#include <string>
21
22static constexpr double kHomePose[7] = { 0.0, 0.2618, 3.1416, -2.2689, 0.0, 0.9599, 1.5708 };
23
24namespace fs = std::filesystem;
25static fs::path repo_root() { return fs::path(__FILE__).parent_path().parent_path().parent_path(); }
26
27int main(int argc, char *argv[])
28{
29 bool headless = false;
30 for (int i = 1; i < argc; ++i)
31 if (std::string(argv[i]) == "--headless") headless = true;
32
33 const fs::path root = repo_root();
34 if (!fs::exists(root / "third_party/menagerie")) {
35 std::cerr << "third_party/menagerie/ not found - run:\n"
36 " cmake -B build -DFETCH_MENAGERIE=ON\n";
37 return 1;
38 }
39
40 const std::string mjcf = (root / "third_party/menagerie/kinova_gen3/gen3.xml").string();
41
44 r.path = mjcf.c_str();
45 sc.robots.push_back(r);
46
47 mjModel *model = nullptr;
48 mjData *data = nullptr;
49 mj_kdl::Robot robot;
50 if (!mj_kdl::build_scene(&model, &data, &sc)) {
51 std::cerr << "build_scene() failed\n";
52 return 1;
53 }
54 if (!mj_kdl::init_robot_from_mjcf(&robot, model, data, "base_link", "bracelet_link")) {
55 std::cerr << "init_robot_from_mjcf() failed\n";
56 mj_kdl::destroy_scene(model, data);
57 return 1;
58 }
59
60 unsigned n = static_cast<unsigned>(robot.n_joints);
61 std::cout << "nq=" << model->nq << " nu=" << model->nu << " nbody=" << model->nbody
62 << " n_joints=" << n << "\n";
63 for (unsigned i = 0; i < n; ++i)
64 std::cout << " joint[" << i << "] = " << robot.joint_names[i] << "\n";
65
66 KDL::JntArray q_home(n);
67 for (unsigned i = 0; i < n; ++i) q_home(i) = kHomePose[i];
68
69 KDL::ChainFkSolverPos_recursive fk(robot.chain);
70 KDL::ChainDynParam dyn(robot.chain, KDL::Vector(0.0, 0.0, -9.81));
71 KDL::Frame ee;
72 fk.JntToCart(q_home, ee);
73 std::cout << std::fixed << std::setprecision(4);
74 std::cout << "EE at home: [" << ee.p.x() << ", " << ee.p.y() << ", " << ee.p.z() << "]\n";
75
76 if (headless) {
77 mj_kdl::cleanup(&robot);
78 mj_kdl::destroy_scene(model, data);
79 return 0;
80 }
81
82 mj_kdl::set_joint_pos(&robot, q_home);
84
85 mj_kdl::Viewer viewer;
86 if (!mj_kdl::init_window_sim(&viewer, &robot)) {
87 std::cerr << "init_window_sim() failed\n";
88 mj_kdl::cleanup(&robot);
89 mj_kdl::destroy_scene(model, data);
90 return 1;
91 }
92
93 KDL::JntArray q(n), g(n);
94 while (mj_kdl::tick(&viewer, model, data)) {
95 mj_kdl::update(&robot);
96 for (unsigned i = 0; i < n; ++i) q(i) = robot.jnt_pos_msr[i];
97 dyn.JntToGravity(q, g);
98 for (unsigned i = 0; i < n; ++i) robot.jnt_trq_cmd[i] = g(i);
99 }
100
101 mj_kdl::cleanup(&viewer);
102 mj_kdl::cleanup(&robot);
103 mj_kdl::destroy_scene(model, data);
104 return 0;
105}
int main(int argc, char *argv[])
Definition ex_init.cpp:27
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 set_joint_pos(Robot *r, const KDL::JntArray &q, bool call_forward=true)
void update(Robot *r)
bool build_scene(mjModel **out_model, mjData **out_data, const SceneSpec *spec)
void destroy_scene(mjModel *model, mjData *data)
bool init_window_sim(Viewer *v, Robot *r, const char *title="MuJoCo")
bool tick(Viewer *v, Robot *r)
std::vector< double > jnt_pos_msr
std::vector< double > jnt_trq_cmd
std::vector< std::string > joint_names
std::vector< RobotSpec > robots