|
| 1 | +#include "drake/multibody/meshcat/meshcat_mouse_spring.h" |
| 2 | + |
| 3 | +#include <cmath> |
| 4 | +#include <optional> |
| 5 | +#include <utility> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +#include "drake/common/drake_throw.h" |
| 9 | +#include "drake/multibody/math/spatial_algebra.h" |
| 10 | +#include "drake/multibody/plant/externally_applied_spatial_force.h" |
| 11 | + |
| 12 | +namespace drake { |
| 13 | +namespace multibody { |
| 14 | +namespace meshcat { |
| 15 | + |
| 16 | +using Eigen::Vector3d; |
| 17 | +using geometry::Meshcat; |
| 18 | +using math::RigidTransform; |
| 19 | +using systems::Context; |
| 20 | +using systems::DiagramBuilder; |
| 21 | + |
| 22 | +namespace { |
| 23 | + |
| 24 | +// Replicates MultibodyPlant's body-frame naming (see GetScopedName in |
| 25 | +// multibody_plant.cc) and converts the "::" model-instance separators into "/" |
| 26 | +// the same way MeshcatVisualizer does, so the result matches the body's path in |
| 27 | +// the Meshcat scene tree. |
| 28 | +std::string BodyFramePathSegment(const MultibodyPlant<double>& plant, |
| 29 | + const RigidBody<double>& body) { |
| 30 | + std::string name; |
| 31 | + const ModelInstanceIndex model_instance = body.model_instance(); |
| 32 | + if (model_instance != world_model_instance() && |
| 33 | + model_instance != default_model_instance()) { |
| 34 | + name = plant.GetModelInstanceName(model_instance) + "::" + body.name(); |
| 35 | + } else { |
| 36 | + name = body.name(); |
| 37 | + } |
| 38 | + // MultibodyPlant declares frames with SceneGraph using "::"; |
| 39 | + // MeshcatVisualizer replaces those with "/" to expose the full tree. |
| 40 | + for (size_t pos = 0; (pos = name.find("::", pos)) != std::string::npos;) { |
| 41 | + name.replace(pos, 2, "/"); |
| 42 | + pos += 1; |
| 43 | + } |
| 44 | + return name; |
| 45 | +} |
| 46 | + |
| 47 | +} // namespace |
| 48 | + |
| 49 | +MeshcatMouseSpring::MeshcatMouseSpring(std::shared_ptr<Meshcat> meshcat, |
| 50 | + const MultibodyPlant<double>* plant, |
| 51 | + double stiffness) |
| 52 | + : systems::LeafSystem<double>(), |
| 53 | + meshcat_(std::move(meshcat)), |
| 54 | + plant_(plant), |
| 55 | + stiffness_(stiffness) { |
| 56 | + DRAKE_THROW_UNLESS(meshcat_ != nullptr); |
| 57 | + DRAKE_THROW_UNLESS(plant_ != nullptr); |
| 58 | + DRAKE_THROW_UNLESS(plant_->is_finalized()); |
| 59 | + DRAKE_THROW_UNLESS(stiffness_ >= 0.0); |
| 60 | + |
| 61 | + BuildPathToBodyMap(*plant_); |
| 62 | + |
| 63 | + body_poses_input_port_ = |
| 64 | + this->DeclareAbstractInputPort( |
| 65 | + "body_poses", Value<std::vector<RigidTransform<double>>>()) |
| 66 | + .get_index(); |
| 67 | + body_spatial_velocities_input_port_ = |
| 68 | + this->DeclareAbstractInputPort( |
| 69 | + "body_spatial_velocities", |
| 70 | + Value<std::vector<SpatialVelocity<double>>>()) |
| 71 | + .get_index(); |
| 72 | + spatial_forces_output_port_ = |
| 73 | + this->DeclareAbstractOutputPort( |
| 74 | + "spatial_forces", |
| 75 | + std::vector<ExternallyAppliedSpatialForce<double>>{}, |
| 76 | + &MeshcatMouseSpring::CalcSpatialForces) |
| 77 | + .get_index(); |
| 78 | +} |
| 79 | + |
| 80 | +MeshcatMouseSpring::~MeshcatMouseSpring() = default; |
| 81 | + |
| 82 | +void MeshcatMouseSpring::BuildPathToBodyMap( |
| 83 | + const MultibodyPlant<double>& plant) { |
| 84 | + for (BodyIndex index(0); index < plant.num_bodies(); ++index) { |
| 85 | + if (index == plant.world_body().index()) continue; |
| 86 | + const RigidBody<double>& body = plant.get_body(index); |
| 87 | + // MeshcatVisualizer publishes each body's geometry under a node named by |
| 88 | + // the body's scoped frame name (with "::" replaced by "/"), e.g. |
| 89 | + // "/drake/<prefix>/my_model/my_body/<geometry>". We key on just the scoped |
| 90 | + // name ("my_model/my_body") and match it within the dragged path below. |
| 91 | + path_to_body_[BodyFramePathSegment(plant, body)] = index; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +void MeshcatMouseSpring::CalcSpatialForces( |
| 96 | + const Context<double>& context, |
| 97 | + std::vector<ExternallyAppliedSpatialForce<double>>* forces) const { |
| 98 | + forces->clear(); |
| 99 | + const std::optional<Meshcat::ObjectDrag> drag = meshcat_->GetObjectDrag(); |
| 100 | + if (!drag.has_value()) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + // Find the body whose scoped frame name appears as a run of path segments |
| 105 | + // in the dragged object's Meshcat path. The path looks like |
| 106 | + // "/drake/<vis_prefix>/.../<model>/<body>/<geometry>...", so we look for |
| 107 | + // the scoped name ("<model>/<body>") bounded by '/' on both sides. Matching |
| 108 | + // this way is independent of which visualization layer (illustration, |
| 109 | + // proximity, inertia, ...) was clicked. Among matches we keep the longest |
| 110 | + // (most specific) scoped name. |
| 111 | + BodyIndex body_index; |
| 112 | + size_t best_len = 0; |
| 113 | + const std::string& drag_path = drag->path; |
| 114 | + for (const auto& [segment, index] : path_to_body_) { |
| 115 | + if (segment.size() <= best_len) continue; |
| 116 | + const std::string needle = "/" + segment; |
| 117 | + for (size_t pos = drag_path.find(needle); pos != std::string::npos; |
| 118 | + pos = drag_path.find(needle, pos + 1)) { |
| 119 | + const size_t after = pos + needle.size(); |
| 120 | + if (after == drag_path.size() || drag_path[after] == '/') { |
| 121 | + best_len = segment.size(); |
| 122 | + body_index = index; |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + if (!body_index.is_valid()) { |
| 128 | + // The dragged object doesn't belong to a movable body of this plant. |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + const auto& X_WB_all = |
| 133 | + get_body_poses_input_port().Eval<std::vector<RigidTransform<double>>>( |
| 134 | + context); |
| 135 | + const auto& V_WB_all = |
| 136 | + get_body_spatial_velocities_input_port() |
| 137 | + .Eval<std::vector<SpatialVelocity<double>>>(context); |
| 138 | + const RigidTransform<double>& X_WB = X_WB_all[body_index]; |
| 139 | + const SpatialVelocity<double>& V_WB = V_WB_all[body_index]; |
| 140 | + |
| 141 | + // The attachment point A (anchor) and the cursor target T, in world. |
| 142 | + const Vector3d& p_WA = drag->anchor_in_world; |
| 143 | + const Vector3d& p_WT = drag->target_in_world; |
| 144 | + |
| 145 | + // The anchor expressed in the body frame, where the force is applied. |
| 146 | + const Vector3d p_BoBq_B = X_WB.inverse() * p_WA; |
| 147 | + |
| 148 | + // The world velocity of the attachment point, for damping. |
| 149 | + const Vector3d p_BoA_W = p_WA - X_WB.translation(); |
| 150 | + const Vector3d v_WA = V_WB.Shift(p_BoA_W).translational(); |
| 151 | + |
| 152 | + // Mass-scaled spring + damper force: scaling by the body's mass makes the |
| 153 | + // translational response frequency (sqrt(stiffness)) and damping ratio |
| 154 | + // independent of mass. |
| 155 | + // TODO(vincekurtz): consider using composite mass instead of body mass. |
| 156 | + const double mass = plant_->get_body(body_index).default_mass(); |
| 157 | + const Vector3d f_W = |
| 158 | + mass * stiffness_ * (p_WT - p_WA) - mass * std::sqrt(stiffness_) * v_WA; |
| 159 | + |
| 160 | + ExternallyAppliedSpatialForce<double> force; |
| 161 | + force.body_index = body_index; |
| 162 | + force.p_BoBq_B = p_BoBq_B; |
| 163 | + force.F_Bq_W = SpatialForce<double>(Vector3d::Zero(), f_W); |
| 164 | + forces->push_back(force); |
| 165 | +} |
| 166 | + |
| 167 | +MeshcatMouseSpring& MeshcatMouseSpring::AddToBuilder( |
| 168 | + DiagramBuilder<double>* builder, const MultibodyPlant<double>* plant, |
| 169 | + std::shared_ptr<Meshcat> meshcat, double stiffness) { |
| 170 | + DRAKE_THROW_UNLESS(builder != nullptr); |
| 171 | + DRAKE_THROW_UNLESS(plant != nullptr); |
| 172 | + auto& spring = *builder->AddSystem<MeshcatMouseSpring>(std::move(meshcat), |
| 173 | + plant, stiffness); |
| 174 | + spring.set_name("meshcat_mouse_spring"); |
| 175 | + builder->Connect(plant->get_body_poses_output_port(), |
| 176 | + spring.get_body_poses_input_port()); |
| 177 | + builder->Connect(plant->get_body_spatial_velocities_output_port(), |
| 178 | + spring.get_body_spatial_velocities_input_port()); |
| 179 | + builder->Connect(spring.get_spatial_forces_output_port(), |
| 180 | + plant->get_applied_spatial_force_input_port()); |
| 181 | + return spring; |
| 182 | +} |
| 183 | + |
| 184 | +} // namespace meshcat |
| 185 | +} // namespace multibody |
| 186 | +} // namespace drake |
0 commit comments