28static constexpr double kHomePose[7] = { 0.0, 0.2618, 3.1416, -2.2689, 0.0, 0.9599, 1.5708 };
29static constexpr double kTargetPose[7] = { 0.3, 0.5, 2.9, -2.0, 0.3, 1.2, 1.3 };
31static constexpr double kKv = 2.0;
32static constexpr double kMaxVel = 0.6;
33static constexpr double kTol = 0.01;
35namespace fs = std::filesystem;
36static fs::path repo_root() {
return fs::path(__FILE__).parent_path().parent_path().parent_path(); }
38int main(
int argc,
char *argv[])
40 bool headless =
false;
41 for (
int i = 1; i < argc; ++i)
42 if (std::string(argv[i]) ==
"--headless") headless =
true;
44 const fs::path root = repo_root();
45 if (!fs::exists(root /
"third_party/menagerie")) {
46 std::cerr <<
"third_party/menagerie/ not found - run:\n"
47 " cmake -B build -DFETCH_MENAGERIE=ON\n";
51 const std::string mjcf = (root /
"third_party/menagerie/kinova_gen3/gen3.xml").
string();
55 r.
path = mjcf.c_str();
58 mjModel *model =
nullptr;
59 mjData *data =
nullptr;
61 std::cerr <<
"build_scene() failed\n";
67 std::cerr <<
"init_robot_from_mjcf() failed\n";
72 unsigned n =
static_cast<unsigned>(robot.
n_joints);
74 KDL::JntArray q_home(n);
75 for (
unsigned i = 0; i < n; ++i) q_home(i) = kHomePose[i];
79 const double dt = model->opt.timestep;
82 auto reset_to_home = [&]() {
83 mj_resetData(model, data);
85 mj_forward(model, data);
87 for (
unsigned i = 0; i < n; ++i) { robot.
jnt_pos_cmd[i] = kHomePose[i]; }
93 auto ctrl_step = [&]() {
99 for (
unsigned i = 0; i < n; ++i) {
100 double err = kTargetPose[i] - robot.
jnt_pos_msr[i];
101 max_err = std::max(max_err, std::abs(err));
102 double vel = std::clamp(kKv * err, -kMaxVel, kMaxVel);
106 if (max_err < kTol) {
113 const double timeout = 5.0;
114 while (data->time < timeout && !arrived) {
119 double max_err = 0.0;
120 for (
unsigned i = 0; i < n; ++i)
121 max_err = std::max(max_err, std::abs(kTargetPose[i] - robot.
jnt_pos_msr[i]));
122 std::cout <<
"max joint error: " << std::fixed << std::setprecision(4) << max_err
123 <<
" rad (" << (arrived ?
"converged" :
"timeout") <<
")\n";
127 std::cerr <<
"init_window_sim() failed\n";
133 double prev_sim_time = data->time;
135 if (data->time < prev_sim_time - 1e-6) reset_to_home();
136 prev_sim_time = data->time;
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_pos_cmd
std::vector< RobotSpec > robots