28static constexpr double kHomePose[7] = { 0.0, 0.2618, 3.1416, -2.2689, 0.0, 0.9599, 1.5708 };
47int main(
int argc,
char *argv[])
49 std::string outmp4 =
"sim.mp4";
51 for (
int i = 1; i < argc; ++i) {
52 std::string a(argv[i]);
53 if (a.size() >= 4 && a.substr(a.size() - 4) ==
".mp4")
56 res = parse_resolution(a);
59 const fs::path root = repo_root();
60 if (!fs::exists(root /
"third_party/menagerie")) {
61 std::cerr <<
"third_party/menagerie/ not found - run:\n"
62 " cmake -B build -DFETCH_MENAGERIE=ON\n";
66 const std::string mjcf = (root /
"third_party/menagerie/kinova_gen3/gen3.xml").
string();
71 r.
path = mjcf.c_str();
74 mjModel *model =
nullptr;
75 mjData *data =
nullptr;
77 std::cerr <<
"build_scene() failed\n";
83 std::cerr <<
"init_robot_from_mjcf() failed\n";
88 unsigned n =
static_cast<unsigned>(robot.
n_joints);
89 KDL::ChainDynParam dyn(robot.
chain, KDL::Vector(0.0, 0.0, sc.
gravity_z));
92 KDL::JntArray q_home(n);
93 for (
unsigned i = 0; i < n; ++i) q_home(i) = kHomePose[i];
98 KDL::JntArray q(n), g(n);
99 dyn.JntToGravity(q_home, g);
100 for (
unsigned i = 0; i < n; ++i) robot.
jnt_trq_cmd[i] = g(i);
105 std::cerr <<
"init_video_recorder() failed -- is EGL available and ffmpeg installed?\n";
112 vr.
cam.elevation = -20.0;
113 vr.
cam.distance = 1.8;
114 vr.
cam.lookat[2] = 0.5;
116 const int total_steps =
static_cast<int>(kDuration / model->opt.timestep);
117 const double step_per_deg = 360.0 / total_steps;
118 const int steps_per_frame = std::max(1,
static_cast<int>(1.0 / (kFps * model->opt.timestep)));
120 std::cout <<
"Recording " << kDuration <<
" s to " << outmp4 <<
" (" << total_steps
121 <<
" steps, " << kFps <<
" fps)...\n";
123 for (
int step = 0; step < total_steps; ++step) {
126 for (
unsigned i = 0; i < n; ++i) q(i) = robot.
jnt_pos_msr[i];
127 dyn.JntToGravity(q, g);
128 for (
unsigned i = 0; i < n; ++i) robot.
jnt_trq_cmd[i] = g(i);
132 if (step % steps_per_frame == 0) {
133 vr.
cam.azimuth = std::fmod(step * step_per_deg, 360.0);
135 std::cerr <<
"record_frame() failed at step " << step <<
"\n";
143 std::cout <<
"Saved: " << outmp4 <<
"\n";
bool init_video_recorder(VideoRecorder *vr, mjModel *model, const char *out_path, int width=1280, int height=720, int fps=60)
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)