Mujoco KDL Wrapper  0.1.0
MuJoCo + KDL bridge for robot kinematics and dynamics
Loading...
Searching...
No Matches
test_mjcf_vel_ctrl.cpp
Go to the documentation of this file.
1/* test_mjcf_vel_ctrl.cpp
2 * Joint velocity control on the Kinova GEN3 (MJCF).
3 *
4 * gen3.xml has high-gain position actuators (kp=2000, kv=100).
5 * Velocity control is implemented in POSITION mode by integrating the
6 * desired velocity into a position setpoint each step:
7 *
8 * vel_des[i] = clamp(Kv * (target[i] - q[i]), -maxVel, maxVel)
9 * pos_cmd[i] += vel_des[i] * dt
10 *
11 * The position servo tracks the advancing setpoint, making the joint
12 * follow the commanded velocity profile. */
13
15
16#include <gtest/gtest.h>
17
18#include <algorithm>
19#include <cmath>
20#include <filesystem>
21#include <string>
22
23static constexpr double kHomePose[7] = { 0.0, 0.2618, 3.1416, -2.2689, 0.0, 0.9599, 1.5708 };
24static constexpr double kTargetPose[7] = { 0.3, 0.5, 2.9, -2.0, 0.3, 1.2, 1.3 };
25static constexpr double kKv = 2.0; // P gain [rad/s per rad error]
26static constexpr double kMaxVel = 0.6; // velocity clamp [rad/s]
27static constexpr double kTol = 0.02; // convergence tolerance [rad]
28static constexpr double kTimeout = 5.0; // max simulation time [s]
29
30namespace fs = std::filesystem;
31static fs::path repo_root() { return fs::path(__FILE__).parent_path().parent_path(); }
32
33class MjcfVelCtrlTest : public testing::Test
34{
35 protected:
36 fs::path root_;
37 mjModel *model_ = nullptr;
38 mjData *data_ = nullptr;
40 unsigned n_ = 0;
41
42 void SetUp() override
43 {
44 root_ = repo_root();
45 std::string arm_mjcf = (root_ / "third_party/menagerie/kinova_gen3/gen3.xml").string();
46 if (!fs::exists(arm_mjcf)) {
47 GTEST_SKIP() << arm_mjcf << " not found";
48 return;
49 }
50
52 sc.robots.push_back(mj_kdl::RobotSpec{ .path = arm_mjcf.c_str(), .attachments = {} });
53
54 ASSERT_TRUE(mj_kdl::build_scene(&model_, &data_, &sc));
55 ASSERT_TRUE(mj_kdl::init_robot_from_mjcf(&s_, model_, data_, "base_link", "bracelet_link"));
56
57 n_ = static_cast<unsigned>(s_.n_joints);
58
59 KDL::JntArray q_home(n_);
60 for (unsigned i = 0; i < n_; ++i) q_home(i) = kHomePose[i];
61 mj_kdl::set_joint_pos(&s_, q_home);
62
63 // POSITION mode: initialise pos_cmd to home so the servo starts settled.
65 for (unsigned i = 0; i < n_; ++i) { s_.jnt_pos_cmd[i] = kHomePose[i]; }
67 }
68
69 void TearDown() override
70 {
71 if (model_) {
74 }
75 }
76};
77
79{
80 const double dt = model_->opt.timestep;
81 bool arrived = false;
82
83 while (data_->time < kTimeout && !arrived) {
84 mj_kdl::update(&s_); // reads sensors, applies prev pos_cmd to servo
85
86 double max_err = 0.0;
87 for (unsigned i = 0; i < n_; ++i) {
88 double err = kTargetPose[i] - s_.jnt_pos_msr[i];
89 max_err = std::max(max_err, std::abs(err));
90 double vel = std::clamp(kKv * err, -kMaxVel, kMaxVel);
91 s_.jnt_pos_cmd[i] += vel * dt; // integrate velocity into position setpoint
92 }
93
94 if (max_err < kTol) {
95 arrived = true;
96 // Freeze setpoint at current position to avoid overshoot.
97 for (unsigned i = 0; i < n_; ++i) s_.jnt_pos_cmd[i] = s_.jnt_pos_msr[i];
98 }
99
100 mj_kdl::step(&s_);
101 }
102
103 double max_err = 0.0;
104 for (unsigned i = 0; i < n_; ++i)
105 max_err = std::max(max_err, std::abs(kTargetPose[i] - s_.jnt_pos_msr[i]));
106
107 EXPECT_TRUE(arrived) << "velocity controller did not converge within " << kTimeout << " s";
108 EXPECT_LE(max_err, kTol * 2);
109}
110
111int main(int argc, char *argv[])
112{
113 testing::InitGoogleTest(&argc, argv);
114 return RUN_ALL_TESTS();
115}
void TearDown() override
void SetUp() override
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 build_scene(mjModel **out_model, mjData **out_data, const SceneSpec *spec)
void destroy_scene(mjModel *model, mjData *data)
std::vector< double > jnt_pos_cmd
std::vector< RobotSpec > robots
int main(int argc, char *argv[])
TEST_F(MjcfVelCtrlTest, Convergence)