Repository navigation
Expand file tree
/
Copy pathvirtual_gantry_plugin.cpp
More file actions
305 lines (261 loc) · 10.8 KB
/
Copy pathvirtual_gantry_plugin.cpp
File metadata and controls
305 lines (261 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
#include "mujoco_ros2_control_plugins/virtual_gantry_plugin.hpp"
#include <algorithm>
#include <cmath>
#include <vector>
#include <mujoco/mujoco.h>
#include <pluginlib/class_list_macros.hpp>
namespace mujoco_ros2_control_plugins
{
bool VirtualGantryPlugin::init(rclcpp::Node::SharedPtr node, const mjModel* model, mjData* /*data*/)
{
node_ = node;
// Parameters live in the parent node's "mujoco_plugins.<plugin_name>.*" namespace,
// declared via automatically_declare_parameters_from_overrides.
const std::string prefix = "mujoco_plugins." + node->get_sub_namespace() + ".";
auto params = node->get_node_parameters_interface();
auto get_double = [&](const std::string& key, double& out) {
if (params->has_parameter(prefix + key))
{
out = params->get_parameter(prefix + key).get_parameter_value().get<double>();
}
};
auto get_string = [&](const std::string& key, std::string& out) {
if (params->has_parameter(prefix + key))
{
out = params->get_parameter(prefix + key).get_parameter_value().get<std::string>();
}
};
get_string("body_name", body_name_);
get_double("kp_pos", kp_pos_);
get_double("kd_pos", kd_pos_);
get_double("anchor_z", anchor_z_world_);
if (params->has_parameter(prefix + "body_offset"))
{
const auto v = params->get_parameter(prefix + "body_offset").get_parameter_value().get<std::vector<double>>();
if (v.size() == 3)
{
body_offset_ = { { v[0], v[1], v[2] } };
}
}
body_id_ = mj_name2id(model, mjOBJ_BODY, body_name_.c_str());
if (body_id_ < 0)
{
RCLCPP_ERROR(node_->get_logger(), "VirtualGantryPlugin: body '%s' not found", body_name_.c_str());
return false;
}
RCLCPP_INFO(node_->get_logger(),
"VirtualGantryPlugin: body '%s' (id=%d), anchor_z=%.2f, "
"offset=[%.3f,%.3f,%.3f], kp=%.0f, kd=%.0f",
body_name_.c_str(), body_id_, anchor_z_world_, body_offset_[0], body_offset_[1], body_offset_[2], kp_pos_,
kd_pos_);
enable_srv_ = node_->create_service<std_srvs::srv::SetBool>(
"set_gantry_enabled",
[this](const std_srvs::srv::SetBool::Request::SharedPtr req, std_srvs::srv::SetBool::Response::SharedPtr resp) {
std::lock_guard<std::mutex> lock(state_mutex_);
const bool was_enabled = enabled_;
enabled_ = req->data;
if (enabled_ && !was_enabled)
{
spawn_pos_captured_ = false; // re-anchor above current attach position
}
resp->success = true;
resp->message = enabled_ ? "Gantry enabled" : "Gantry disabled";
RCLCPP_INFO(node_->get_logger(), "%s", resp->message.c_str());
});
// Snapshot the current toggle counter so the first update() doesn't misfire.
last_toggle_count_ = toggle_counter_.load(std::memory_order_relaxed);
return true;
}
void VirtualGantryPlugin::update(const mjModel* /*model*/, mjData* data)
{
std::lock_guard<std::mutex> lock(state_mutex_);
// --- Keyboard toggle ('G' key) -------------------------------------------
const int tc = toggle_counter_.load(std::memory_order_acquire);
const int delta = tc - last_toggle_count_;
if (delta != 0)
{
last_toggle_count_ = tc;
if (delta & 1)
{
enabled_ = !enabled_;
if (enabled_)
{
spawn_pos_captured_ = false; // re-anchor above current position on next step
}
RCLCPP_INFO(node_->get_logger(), "VirtualGantryPlugin: %s via keyboard", enabled_ ? "enabled" : "disabled");
}
}
// --- Rope length adjustment ('[' / ']' keys) -------------------------------
const int scroll_ticks = rope_length_ticks_.exchange(0, std::memory_order_acquire);
if (scroll_ticks != 0)
{
if (enabled_)
{
rope_length_ = std::max(0.1, rope_length_ + scroll_ticks * 0.005);
RCLCPP_INFO(node_->get_logger(), "VirtualGantryPlugin: rope_length=%.3f m", rope_length_);
}
else
{
RCLCPP_WARN(node_->get_logger(), "VirtualGantryPlugin: gantry disabled — rope length unchanged");
}
}
// --- Compute attachment point in world frame ------------------------------
// attach_pos = body CoM + rotation_matrix * body_offset
const double* xpos = &data->xpos[body_id_ * 3];
const double* xmat = &data->xmat[body_id_ * 9];
double attach_pos[3];
for (int i = 0; i < 3; ++i)
{
attach_pos[i] = xpos[i] + xmat[i * 3 + 0] * body_offset_[0] + xmat[i * 3 + 1] * body_offset_[1] +
xmat[i * 3 + 2] * body_offset_[2];
}
// --- Capture anchor on first step after (re-)enable ----------------------
if (!spawn_pos_captured_)
{
// Anchor XY follows the attachment point; Z is fixed in world frame.
anchor_pos_ = { { attach_pos[0], attach_pos[1], anchor_z_world_ } };
// Rope is just taut at spawn: length = vertical gap between anchor and attachment.
rope_length_ = std::abs(anchor_z_world_ - attach_pos[2]);
spawn_pos_captured_ = true;
RCLCPP_INFO(node_->get_logger(), "VirtualGantryPlugin: anchor at [%.3f, %.3f, %.3f], rope_length=%.3f m",
anchor_pos_[0], anchor_pos_[1], anchor_pos_[2], rope_length_);
}
// Rope vector from anchor to attachment point.
const double dx = attach_pos[0] - anchor_pos_[0];
const double dy = attach_pos[1] - anchor_pos_[1];
const double dz = attach_pos[2] - anchor_pos_[2];
const double rope_dist = std::sqrt(dx * dx + dy * dy + dz * dz);
// --- Rope constraint force ------------------------------------------------
// Clear previously applied forces before writing new values.
for (int i = 0; i < 6; ++i)
{
data->xfrc_applied[body_id_ * 6 + i] = 0.0;
}
if (!enabled_ || rope_dist < 1e-6 || rope_dist <= rope_length_)
{
// Rope is slack or gantry disabled: reset FD state so first taut step has no stale spike.
rope_dist_prev_ = -1.0;
rope_dist_dot_ = 0.0;
last_update_time_ = data->time;
return;
}
// Finite-difference rope-extension rate (taut branch only), EMA-smoothed.
// Raw d(rope_dist)/dt over 2 ms is noisy due to contact/joint vibrations; the
// EMA (α≈0.2, τ≈10 ms) keeps low-frequency fall/bounce dynamics while
// filtering out high-frequency noise that would otherwise cause large damp spikes.
if (rope_dist_prev_ >= 0.0 && last_update_time_ >= 0.0)
{
const double dt = data->time - last_update_time_;
if (dt > 1e-9)
{
const double raw_dot = (rope_dist - rope_dist_prev_) / dt;
const double alpha = std::min(1.0, dt / 0.01); // τ = 10 ms
rope_dist_dot_ = alpha * raw_dot + (1.0 - alpha) * rope_dist_dot_;
}
}
rope_dist_prev_ = rope_dist;
last_update_time_ = data->time;
const double rope_dir[3] = { dx / rope_dist, dy / rope_dist, dz / rope_dist };
// Spring always pulls toward anchor when taut; damping only resists extension.
// Bidirectional damping (max(0, spring+damp)) silences the spring when contracting
// fast, which creates an asymmetric energy cycle and growing oscillations.
const double extension = rope_dist - rope_length_;
const double damp = (rope_dist_dot_ > 0.0) ? kd_pos_ * rope_dist_dot_ : 0.0;
const double tension = kp_pos_ * extension + damp;
// Force toward anchor (opposite of rope_dir).
const double Fx = -tension * rope_dir[0];
const double Fy = -tension * rope_dir[1];
const double Fz = -tension * rope_dir[2];
data->xfrc_applied[body_id_ * 6 + 0] = Fx;
data->xfrc_applied[body_id_ * 6 + 1] = Fy;
data->xfrc_applied[body_id_ * 6 + 2] = Fz;
// Torque correction for off-CoM attachment: τ = offset_world × F.
const double ox = xmat[0] * body_offset_[0] + xmat[1] * body_offset_[1] + xmat[2] * body_offset_[2];
const double oy = xmat[3] * body_offset_[0] + xmat[4] * body_offset_[1] + xmat[5] * body_offset_[2];
const double oz = xmat[6] * body_offset_[0] + xmat[7] * body_offset_[1] + xmat[8] * body_offset_[2];
data->xfrc_applied[body_id_ * 6 + 3] = oy * Fz - oz * Fy;
data->xfrc_applied[body_id_ * 6 + 4] = oz * Fx - ox * Fz;
data->xfrc_applied[body_id_ * 6 + 5] = ox * Fy - oy * Fx;
}
void VirtualGantryPlugin::reset()
{
std::lock_guard<std::mutex> lock(state_mutex_);
spawn_pos_captured_ = false;
rope_dist_prev_ = -1.0;
rope_dist_dot_ = 0.0;
last_update_time_ = -1.0;
}
void VirtualGantryPlugin::update_visualization(const mjModel* /*model*/, const mjData* data, mjvScene* scene)
{
std::lock_guard<std::mutex> lock(state_mutex_);
if (!enabled_ || !spawn_pos_captured_ || body_id_ < 0 || !data || !data->xpos || !data->xmat || !scene ||
!scene->geoms || scene->ngeom >= scene->maxgeom)
{
return;
}
const double* xpos = &data->xpos[body_id_ * 3];
const double* xmat = &data->xmat[body_id_ * 9];
mjtNum attach_pos[3];
for (int i = 0; i < 3; ++i)
{
attach_pos[i] = xpos[i] + xmat[i * 3 + 0] * body_offset_[0] + xmat[i * 3 + 1] * body_offset_[1] +
xmat[i * 3 + 2] * body_offset_[2];
}
const mjtNum anchor_pos[3] = { anchor_pos_[0], anchor_pos_[1], anchor_pos_[2] };
const float rope_rgba[4] = { 0.1f, 0.95f, 0.2f, 1.0f };
mjvGeom* geom = &scene->geoms[scene->ngeom++];
mjv_initGeom(geom, mjGEOM_CAPSULE, nullptr, nullptr, nullptr, rope_rgba);
geom->category = mjCAT_DECOR;
mjv_connector(geom, mjGEOM_CAPSULE, 0.0125, anchor_pos, attach_pos);
if (scene->ngeom >= scene->maxgeom)
{
return;
}
const mjtNum anchor_size[3] = { 0.04, 0.0, 0.0 };
const float anchor_rgba[4] = { 1.0f, 0.0f, 0.0f, 1.0f };
mjvGeom* anchor_geom = &scene->geoms[scene->ngeom++];
mjv_initGeom(anchor_geom, mjGEOM_SPHERE, anchor_size, anchor_pos, nullptr, anchor_rgba);
anchor_geom->category = mjCAT_DECOR;
}
bool VirtualGantryPlugin::on_key(int key, int /*scancode*/, int action, int /*mods*/)
{
// GLFW constants (stable values matching glfw3.h; avoids adding glfw as a plugin dependency).
constexpr int kPress = 1;
constexpr int kRepeat = 2;
constexpr int kKeyG = 71;
constexpr int kKeyLeftBracket = 91;
constexpr int kKeyRightBracket = 93;
if (action != kPress && action != kRepeat)
{
return false;
}
if (key == kKeyG && action == kPress)
{
toggle_counter_.fetch_add(1, std::memory_order_release);
return true;
}
if (key == kKeyLeftBracket)
{
rope_length_ticks_.fetch_add(-1, std::memory_order_release);
return true;
}
if (key == kKeyRightBracket)
{
rope_length_ticks_.fetch_add(1, std::memory_order_release);
return true;
}
return false;
}
void VirtualGantryPlugin::cleanup()
{
// Hold state_mutex_ so any in-flight service callback (which also takes the lock and
// dereferences node_) finishes before we tear down the service and node references.
std::lock_guard<std::mutex> lock(state_mutex_);
enable_srv_.reset();
node_.reset();
}
} // namespace mujoco_ros2_control_plugins
PLUGINLIB_EXPORT_CLASS(mujoco_ros2_control_plugins::VirtualGantryPlugin,
mujoco_ros2_control_plugins::MuJoCoROS2ControlPluginBase)