Mujoco KDL Wrapper  0.1.0
MuJoCo + KDL bridge for robot kinematics and dynamics
Loading...
Searching...
No Matches
ex_table_scene.cpp
Go to the documentation of this file.
1/* ex_table_scene.cpp (MJCF)
2 * Kinova GEN3 + Robotiq 2F-85 gripper on a table with a few free objects,
3 * loaded from MuJoCo Menagerie MJCF.
4 *
5 * The arm runs KDL gravity compensation; the gripper cycles open/closed every 3 s.
6 *
7 * Requires third_party/menagerie submodule.
8 *
9 * Usage:
10 * ex_table_scene_mjcf [--headless]
11 *
12 * With --headless runs 500 steps and prints final EE drift. */
13
15
16#include <kdl/chaindynparam.hpp>
17#include <kdl/chainfksolverpos_recursive.hpp>
18
19#include <cmath>
20#include <filesystem>
21#include <iomanip>
22#include <iostream>
23#include <string>
24
25static constexpr double kHomePose[7] = { 0.0, 0.2618, 3.1416, -2.2689, 0.0, 0.9599, 1.5708 };
26
27namespace fs = std::filesystem;
28static fs::path repo_root() { return fs::path(__FILE__).parent_path().parent_path().parent_path(); }
29
30static mj_kdl::SceneObject make_box(
31 const char *name,
32 double x,
33 double y,
34 double hx,
35 double hy,
36 double hz,
37 float r,
38 float g,
39 float b,
40 double surface_z
41)
42{
44 o.name = name;
46 o.size[0] = hx;
47 o.size[1] = hy;
48 o.size[2] = hz;
49 o.pos[0] = x;
50 o.pos[1] = y;
51 o.pos[2] = surface_z + hz;
52 o.rgba[0] = r;
53 o.rgba[1] = g;
54 o.rgba[2] = b;
55 o.rgba[3] = 1.0f;
56 return o;
57}
58
59static mj_kdl::SceneObject make_sphere(
60 const char *name,
61 double x,
62 double y,
63 double radius,
64 float r,
65 float g,
66 float b,
67 double surface_z
68)
69{
71 o.name = name;
73 o.size[0] = radius;
74 o.pos[0] = x;
75 o.pos[1] = y;
76 o.pos[2] = surface_z + radius;
77 o.rgba[0] = r;
78 o.rgba[1] = g;
79 o.rgba[2] = b;
80 o.rgba[3] = 1.0f;
81 return o;
82}
83
84int main(int argc, char *argv[])
85{
86 bool headless = false;
87 for (int i = 1; i < argc; ++i)
88 if (std::string(argv[i]) == "--headless") headless = true;
89
90 const fs::path root = repo_root();
91 if (!fs::exists(root / "third_party/menagerie")) {
92 std::cerr << "third_party/menagerie/ not found - run:\n"
93 " cmake -B build -DFETCH_MENAGERIE=ON\n";
94 return 1;
95 }
96
97 const std::string mjcf = (root / "third_party/menagerie/kinova_gen3/gen3.xml").string();
98 const std::string grp_mjcf = (root / "third_party/menagerie/robotiq_2f85/2f85.xml").string();
99 const std::string table_mjcf = (root / "src/examples/assets/table.xml").string();
100
102 const double surface_z = 0.7;
103
105 gs.mjcf_path = grp_mjcf.c_str();
106 gs.attach_to = "bracelet_link";
107 gs.prefix = "g_";
108 gs.pos[2] = -0.061525;
109 gs.euler[0] = 180.0;
110
112 r.path = mjcf.c_str();
113 r.pos[2] = surface_z;
114 r.attachments.push_back(gs);
115 sc.robots.push_back(r);
116
118 .name = "table",
119 .mjcf_path = table_mjcf,
120 .pos = { 0.0, 0.0, surface_z },
121 .fixed = true,
122 };
123 sc.objects.push_back(table);
124 sc.objects.push_back(
125 make_box("red_cube", 0.35, 0.10, 0.03, 0.03, 0.03, 1.0f, 0.2f, 0.2f, surface_z)
126 );
127 sc.objects.push_back(
128 make_box("green_cube", 0.35, -0.10, 0.03, 0.03, 0.03, 0.2f, 1.0f, 0.2f, surface_z)
129 );
130 sc.objects.push_back(
131 make_box("blue_cube", 0.35, 0.30, 0.04, 0.04, 0.04, 0.2f, 0.2f, 1.0f, surface_z)
132 );
133 sc.objects.push_back(
134 make_sphere("orange_sphere", -0.20, 0.20, 0.035, 1.0f, 0.55f, 0.0f, surface_z)
135 );
136 sc.objects.push_back(
137 make_sphere("purple_sphere", -0.20, -0.20, 0.025, 0.7f, 0.0f, 0.9f, surface_z)
138 );
139
140 /* Static scene cameras. The Kinova MJCF also contributes a "wrist" camera;
141 * all of them are enumerated by get_camera_names() after build_scene(). */
142 sc.cameras.push_back(mj_kdl::CameraSpec{
143 .name = "overview",
144 .pos = { 0.0, -0.6, 1.6 }, // in front of and above the table
145 .euler = { 34.0, 0.0, 0.0 }, // tilt down toward table
146 .fovy = 55.0,
147 });
148 sc.cameras.push_back(mj_kdl::CameraSpec{
149 .name = "side",
150 .pos = { -1.0, 0.0, 1.1 }, // left side, arm height
151 .euler = { 0.0, -68.0, 0.0 }, // pitch down toward robot base
152 .fovy = 50.0,
153 });
154
155 mjModel *model = nullptr;
156 mjData *data = nullptr;
157 mj_kdl::Robot robot;
158 if (!mj_kdl::build_scene(&model, &data, &sc)) {
159 std::cerr << "build_scene() failed\n";
160 return 1;
161 }
162
163 KDL::Frame world_T_table_top;
164 const std::string table_top_site = mj_kdl::scene_object_site_name(table, "table_top");
165 if (!mj_kdl::get_site_frame(model, data, table_top_site.c_str(), &world_T_table_top)) {
166 std::cerr << "table_top site not found\n";
167 mj_kdl::destroy_scene(model, data);
168 return 1;
169 }
170 std::cout << "table top z = " << world_T_table_top.p.z() << "\n";
171
172 std::cout << "cameras:";
173 for (const auto &name : mj_kdl::get_camera_names(model))
174 std::cout << " " << name;
175 std::cout << "\n";
176
177 const mj_kdl::ToolFrameSpec tool{ .tool_body = "g_base", .tcp_site = "g_pinch" };
178
180 &robot, model, data, "base_link", "bracelet_link", "", &tool
181 )) {
182 std::cerr << "init_robot_from_mjcf() failed\n";
183 mj_kdl::destroy_scene(model, data);
184 return 1;
185 }
186
187 unsigned n = static_cast<unsigned>(robot.n_joints);
188 KDL::ChainFkSolverPos_recursive fk(robot.chain);
189 KDL::ChainDynParam dyn(robot.chain, KDL::Vector(0.0, 0.0, sc.gravity_z));
190
191 KDL::JntArray q_home(n);
192 for (unsigned i = 0; i < n; ++i) q_home(i) = kHomePose[i];
193
194 int fingers_act = mj_name2id(model, mjOBJ_ACTUATOR, "g_fingers_actuator");
195
197
198 mj_kdl::Env env;
199 env.spec = sc;
200 env.model = model;
201 env.data = data;
202 mj_kdl::env_add_robot(&env, &robot);
203
204 env.on_reset = [&](mj_kdl::ResetContext *) {
205 mj_kdl::set_joint_pos(&robot, q_home, false);
206 if (fingers_act >= 0) data->ctrl[fingers_act] = 255.0;
207 };
208
209 mj_kdl::reset(&env);
210
211 KDL::JntArray q(n), g(n);
212 auto ctrl_step = [&]() {
213 mj_kdl::update(&robot);
214 for (unsigned i = 0; i < n; ++i) q(i) = robot.jnt_pos_msr[i];
215 dyn.JntToGravity(q, g);
216 for (unsigned i = 0; i < n; ++i) robot.jnt_trq_cmd[i] = g(i);
217 if (fingers_act >= 0)
218 data->ctrl[fingers_act] = (std::fmod(data->time, 6.0) < 3.0) ? 255.0 : 0.0;
219 };
220
221 if (headless) {
222 KDL::Frame ee_start;
223 fk.JntToCart(q_home, ee_start);
224
225 for (int step = 0; step < 500; ++step) {
226 ctrl_step();
227 mj_kdl::step(&robot);
228 }
229
230 KDL::JntArray q_end(n);
231 for (unsigned i = 0; i < n; ++i) q_end(i) = robot.jnt_pos_msr[i];
232 KDL::Frame ee_end;
233 fk.JntToCart(q_end, ee_end);
234 double drift = (ee_start.p - ee_end.p).Norm();
235 std::cout << "EE drift after 500 steps: " << std::fixed << std::setprecision(3)
236 << drift * 1000.0 << " mm\n";
237 } else {
238 mj_kdl::Viewer viewer;
239 if (!mj_kdl::init_window_sim(&viewer, &robot)) {
240 std::cerr << "init_window_sim() failed\n";
241 mj_kdl::cleanup(&robot);
242 mj_kdl::destroy_scene(model, data);
243 return 1;
244 }
245
246 double prev_sim_time = data->time;
247 while (true) {
248 if (data->time < prev_sim_time - 1e-6) mj_kdl::reset(&env);
249 prev_sim_time = data->time;
250 ctrl_step();
251 if (!mj_kdl::step(&robot)) break;
252 }
253
254 mj_kdl::cleanup(&viewer);
255 }
256
257 mj_kdl::cleanup(&robot);
258 mj_kdl::destroy_scene(model, data);
259 return 0;
260}
int main(int argc, char *argv[])
bool step(Robot *s)
void cleanup(Robot *r)
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)
void update(Robot *r)
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)
void destroy_scene(mjModel *model, mjData *data)
std::vector< std::string > get_camera_names(const mjModel *model)
std::string scene_object_site_name(const SceneObject &obj, const char *site_name)
bool init_window_sim(Viewer *v, Robot *r, const char *title="MuJoCo")
void env_add_robot(Env *env, Robot *robot)
ResetInfo reset(Env *env, const ResetOptions *options=nullptr)
ResetHook on_reset
std::vector< AttachmentSpec > attachments
std::vector< double > jnt_pos_msr
std::vector< double > jnt_trq_cmd
std::vector< RobotSpec > robots
std::vector< SceneObject > objects
std::vector< CameraSpec > cameras