14#include <kdl/chaindynparam.hpp>
15#include <kdl/chainfksolverpos_recursive.hpp>
22static constexpr double kHomePose[7] = { 0.0, 0.2618, 3.1416, -2.2689, 0.0, 0.9599, 1.5708 };
24namespace fs = std::filesystem;
25static fs::path repo_root() {
return fs::path(__FILE__).parent_path().parent_path().parent_path(); }
27int main(
int argc,
char *argv[])
29 bool headless =
false;
30 for (
int i = 1; i < argc; ++i)
31 if (std::string(argv[i]) ==
"--headless") headless =
true;
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";
40 const std::string mjcf = (root /
"third_party/menagerie/kinova_gen3/gen3.xml").
string();
44 r.
path = mjcf.c_str();
47 mjModel *model =
nullptr;
48 mjData *data =
nullptr;
51 std::cerr <<
"build_scene() failed\n";
55 std::cerr <<
"init_robot_from_mjcf() failed\n";
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";
66 KDL::JntArray q_home(n);
67 for (
unsigned i = 0; i < n; ++i) q_home(i) = kHomePose[i];
69 KDL::ChainFkSolverPos_recursive fk(robot.
chain);
70 KDL::ChainDynParam dyn(robot.
chain, KDL::Vector(0.0, 0.0, -9.81));
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";
87 std::cerr <<
"init_window_sim() failed\n";
93 KDL::JntArray q(n), g(n);
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);
int main(int argc, char *argv[])
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 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)
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