-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
meshcat_visualizer_test.cc
223 lines (188 loc) · 8.22 KB
/
meshcat_visualizer_test.cc
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
#include "drake/geometry/meshcat_visualizer.h"
#include <gtest/gtest.h>
#include "drake/common/find_resource.h"
#include "drake/common/test_utilities/expect_throws_message.h"
#include "drake/multibody/parsing/parser.h"
#include "drake/multibody/plant/multibody_plant.h"
#include "drake/systems/analysis/simulator.h"
#include "drake/systems/framework/diagram_builder.h"
#include "drake/systems/primitives/constant_vector_source.h"
namespace drake {
namespace geometry {
namespace {
// The tests in this file require a dependency on MultibodyPlant. One could
// implement the tests without that dependency, but not without duplicating (or
// sharing) a significant amount of setup code like we see in
// DrakeVisualizerTest.
//
// Relative to DrakeVisualizerTest, this file does not have to provide test
// coverage for the message passing, nor for the variety of geometry types.
// Those are covered by the tests for Meshcat. Here we only aim to demonstrate
// that we call Meshcat correctly from MeshcatVisualizer.
class MeshcatVisualizerWithIiwaTest : public ::testing::Test {
protected:
MeshcatVisualizerWithIiwaTest() : meshcat_(std::make_shared<Meshcat>()) {}
void SetUpDiagram(MeshcatVisualizerParams params = {}) {
systems::DiagramBuilder<double> builder;
auto [plant, scene_graph] =
multibody::AddMultibodyPlantSceneGraph(&builder, 0.001);
plant_ = &plant;
scene_graph_ = &scene_graph;
multibody::Parser(plant_).AddModelFromFile(
FindResourceOrThrow("drake/manipulation/models/iiwa_description/urdf/"
"iiwa14_spheres_collision.urdf"));
plant.WeldFrames(plant.world_frame(),
plant.GetBodyByName("base").body_frame());
plant.Finalize();
auto zero_torque =
builder.template AddSystem<systems::ConstantVectorSource<double>>(
Eigen::VectorXd::Zero(7));
builder.Connect(zero_torque->get_output_port(),
plant.get_actuation_input_port());
visualizer_ = &MeshcatVisualizer<double>::AddToBuilder(
&builder, scene_graph, meshcat_, std::move(params));
diagram_ = builder.Build();
context_ = diagram_->CreateDefaultContext();
}
std::shared_ptr<Meshcat> meshcat_;
multibody::MultibodyPlant<double>* plant_{};
SceneGraph<double>* scene_graph_{};
const MeshcatVisualizer<double>* visualizer_{};
std::unique_ptr<systems::Diagram<double>> diagram_{};
std::unique_ptr<systems::Context<double>> context_{};
};
TEST_F(MeshcatVisualizerWithIiwaTest, BasicTest) {
SetUpDiagram();
EXPECT_EQ(&visualizer_->meshcat(), meshcat_.get());
EXPECT_FALSE(meshcat_->HasPath("/drake/visualizer/iiwa14"));
diagram_->Publish(*context_);
EXPECT_TRUE(meshcat_->HasPath("/drake/visualizer/iiwa14"));
for (int link = 0; link < 8; link++) {
EXPECT_NE(meshcat_->GetPackedTransform(
fmt::format("/drake/visualizer/iiwa14/iiwa_link_{}", link)),
"");
}
// Confirm that the transforms change after running a simulation.
const std::string packed_X_W7 =
meshcat_->GetPackedTransform("visualizer/iiwa14/iiwa_link_7");
systems::Simulator<double> simulator(*diagram_);
simulator.AdvanceTo(0.1);
EXPECT_NE(meshcat_->GetPackedTransform("visualizer/iiwa14/iiwa_link_7"),
packed_X_W7);
}
TEST_F(MeshcatVisualizerWithIiwaTest, Roles) {
MeshcatVisualizerParams params;
for (Role role : {Role::kProximity, Role::kIllustration, Role::kPerception}) {
params.role = role;
SetUpDiagram(params);
EXPECT_FALSE(meshcat_->HasPath("visualizer/iiwa14/iiwa_link_7"));
diagram_->Publish(*context_);
EXPECT_TRUE(meshcat_->HasPath("visualizer/iiwa14/iiwa_link_7"));
meshcat_->Delete();
}
params.role = Role::kUnassigned;
DRAKE_EXPECT_THROWS_MESSAGE(SetUpDiagram(params),
".*Role::kUnassigned.*");
}
TEST_F(MeshcatVisualizerWithIiwaTest, Prefix) {
MeshcatVisualizerParams params;
// Absolute path.
params.prefix = "/foo";
SetUpDiagram(params);
EXPECT_FALSE(meshcat_->HasPath("/foo/iiwa14"));
diagram_->Publish(*context_);
EXPECT_TRUE(meshcat_->HasPath("/foo/iiwa14"));
EXPECT_FALSE(meshcat_->HasPath("/drake/visualizer"));
// Relative path.
params.prefix = "foo";
EXPECT_FALSE(meshcat_->HasPath("/drake/foo/iiwa14"));
SetUpDiagram(params);
diagram_->Publish(*context_);
EXPECT_TRUE(meshcat_->HasPath("/drake/foo/iiwa14"));
EXPECT_FALSE(meshcat_->HasPath("/drake/visualizer"));
}
TEST_F(MeshcatVisualizerWithIiwaTest, DeletePrefixOnInitialization) {
MeshcatVisualizerParams params;
params.delete_prefix_on_initialization_event = true;
SetUpDiagram(params);
// Scribble a transform onto the scene tree beneath the visualizer prefix.
meshcat_->SetTransform("/drake/visualizer/my_random_path",
math::RigidTransformd());
EXPECT_TRUE(meshcat_->HasPath("/drake/visualizer/my_random_path"));
{ // Send an initialization event.
auto events = diagram_->AllocateCompositeEventCollection();
diagram_->GetInitializationEvents(*context_, events.get());
diagram_->Publish(*context_, events->get_publish_events());
}
// Confirm that my scribble was deleted.
EXPECT_FALSE(meshcat_->HasPath("/drake/visualizer/my_random_path"));
// Repeat, but this time with delete prefix disabled.
params.delete_prefix_on_initialization_event = false;
SetUpDiagram(params);
meshcat_->SetTransform("/drake/visualizer/my_random_path",
math::RigidTransformd());
{ // Send an initialization event.
auto events = diagram_->AllocateCompositeEventCollection();
diagram_->GetInitializationEvents(*context_, events.get());
diagram_->Publish(*context_, events->get_publish_events());
}
// Confirm that my scribble remains.
EXPECT_TRUE(meshcat_->HasPath("/drake/visualizer/my_random_path"));
}
TEST_F(MeshcatVisualizerWithIiwaTest, DeletePrefix) {
SetUpDiagram();
diagram_->Publish(*context_);
EXPECT_TRUE(meshcat_->HasPath("/drake/visualizer"));
visualizer_->DeletePrefix();
EXPECT_FALSE(meshcat_->HasPath("/drake/visualizer"));
}
TEST_F(MeshcatVisualizerWithIiwaTest, ScalarConversion) {
SetUpDiagram();
auto ad_diagram = diagram_->ToAutoDiffXd();
auto ad_context = ad_diagram->CreateDefaultContext();
ad_diagram->Publish(*ad_context);
}
GTEST_TEST(MeshcatVisualizerTest, MultipleModels) {
auto meshcat = std::make_shared<Meshcat>();
systems::DiagramBuilder<double> builder;
auto [plant, scene_graph] =
multibody::AddMultibodyPlantSceneGraph(&builder, 0.001);
std::string urdf = FindResourceOrThrow(
"drake/manipulation/models/iiwa_description/urdf/"
"iiwa14_no_collision.urdf");
auto iiwa0 = multibody::Parser(&plant).AddModelFromFile(urdf);
plant.WeldFrames(plant.world_frame(),
plant.GetBodyByName("base", iiwa0).body_frame());
auto iiwa1 = multibody::Parser(&plant).AddModelFromFile(urdf, "second_iiwa");
plant.WeldFrames(plant.world_frame(),
plant.GetBodyByName("base", iiwa1).body_frame());
plant.Finalize();
auto zero_torque = builder.AddSystem<systems::ConstantVectorSource>(
Eigen::VectorXd::Zero(7));
builder.Connect(zero_torque->get_output_port(),
plant.get_actuation_input_port(iiwa0));
builder.Connect(zero_torque->get_output_port(),
plant.get_actuation_input_port(iiwa1));
// Use the query_output_port version of AddToBuilder.
const auto& visualizer = MeshcatVisualizer<double>::AddToBuilder(
&builder, scene_graph.get_query_output_port(), meshcat);
auto diagram = builder.Build();
auto context = diagram->CreateDefaultContext();
EXPECT_EQ(&visualizer.meshcat(), meshcat.get());
EXPECT_FALSE(meshcat->HasPath("/drake/visualizer/iiwa14"));
EXPECT_FALSE(meshcat->HasPath("/drake/visualizer/second_iiwa"));
diagram->Publish(*context);
EXPECT_TRUE(meshcat->HasPath("/drake/visualizer/iiwa14"));
EXPECT_TRUE(meshcat->HasPath("/drake/visualizer/second_iiwa"));
for (int link = 0; link < 8; link++) {
EXPECT_NE(meshcat->GetPackedTransform(
fmt::format("/drake/visualizer/iiwa14/iiwa_link_{}", link)),
"");
EXPECT_NE(meshcat->GetPackedTransform(fmt::format(
"/drake/visualizer/second_iiwa/iiwa_link_{}", link)),
"");
}
}
} // namespace
} // namespace geometry
} // namespace drake