-
Notifications
You must be signed in to change notification settings - Fork 41
/
JointFeatures_TEST.cc
1527 lines (1265 loc) · 50.3 KB
/
JointFeatures_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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2019 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <dart/dynamics/BodyNode.hpp>
#include <dart/dynamics/Skeleton.hpp>
#include <dart/simulation/World.hpp>
#include <gtest/gtest.h>
#include <iostream>
#include <ignition/physics/FindFeatures.hh>
#include <ignition/plugin/Loader.hh>
#include <ignition/physics/RequestEngine.hh>
#include <ignition/math/Angle.hh>
#include <ignition/math/eigen3/Conversions.hh>
// Features
#include <ignition/physics/FrameSemantics.hh>
#include <ignition/physics/ForwardStep.hh>
#include <ignition/physics/FreeGroup.hh>
#include <ignition/physics/FreeJoint.hh>
#include <ignition/physics/FixedJoint.hh>
#include <ignition/physics/GetEntities.hh>
#include <ignition/physics/Joint.hh>
#include <ignition/physics/RevoluteJoint.hh>
#include <ignition/physics/dartsim/World.hh>
#include <ignition/physics/sdf/ConstructModel.hh>
#include <ignition/physics/sdf/ConstructWorld.hh>
#include <limits>
#include <sdf/Model.hh>
#include <sdf/Root.hh>
#include <sdf/World.hh>
#include "ignition/physics/Geometry.hh"
#include "test/Utils.hh"
using namespace ignition;
using TestFeatureList = ignition::physics::FeatureList<
physics::dartsim::RetrieveWorld,
physics::AttachFixedJointFeature,
physics::DetachJointFeature,
physics::SetJointTransformFromParentFeature,
physics::ForwardStep,
physics::FreeJointCast,
physics::SetFreeGroupWorldPose,
physics::GetBasicJointState,
physics::GetEntities,
physics::GetJointTransmittedWrench,
physics::JointFrameSemantics,
physics::LinkFrameSemantics,
physics::RevoluteJointCast,
physics::SetBasicJointState,
physics::SetJointVelocityCommandFeature,
physics::sdf::ConstructSdfModel,
physics::sdf::ConstructSdfWorld,
physics::SetJointPositionLimitsFeature,
physics::SetJointVelocityLimitsFeature,
physics::SetJointEffortLimitsFeature
>;
using TestEnginePtr = physics::Engine3dPtr<TestFeatureList>;
class JointFeaturesFixture : public ::testing::Test
{
protected: void SetUp() override
{
ignition::plugin::Loader loader;
loader.LoadLib(dartsim_plugin_LIB);
ignition::plugin::PluginPtr dartsim =
loader.Instantiate("ignition::physics::dartsim::Plugin");
this->engine =
ignition::physics::RequestEngine3d<TestFeatureList>::From(dartsim);
ASSERT_NE(nullptr, this->engine);
}
protected: TestEnginePtr engine;
};
// Test setting joint commands and verify that the joint type is set accordingly
// and that the commanded velocity is acheived
TEST_F(JointFeaturesFixture, JointSetCommand)
{
sdf::Root root;
const sdf::Errors errors = root.Load(TEST_WORLD_DIR "test.world");
ASSERT_TRUE(errors.empty()) << errors.front();
const std::string modelName{"double_pendulum_with_base"};
const std::string jointName{"upper_joint"};
auto world = this->engine->ConstructWorld(*root.WorldByIndex(0));
auto model = world->GetModel(modelName);
auto joint = model->GetJoint(jointName);
dart::simulation::WorldPtr dartWorld = world->GetDartsimWorld();
ASSERT_NE(nullptr, dartWorld);
const dart::dynamics::SkeletonPtr skeleton =
dartWorld->getSkeleton(modelName);
ASSERT_NE(nullptr, skeleton);
const auto *dartBaseLink = skeleton->getBodyNode("base");
ASSERT_NE(nullptr, dartBaseLink);
const auto *dartJoint = skeleton->getJoint(jointName);
// Default actuatore type
EXPECT_EQ(dart::dynamics::Joint::FORCE, dartJoint->getActuatorType());
// Test joint velocity command
physics::ForwardStep::Output output;
physics::ForwardStep::State state;
physics::ForwardStep::Input input;
// Expect negative joint velocity after 1 step without joint command
world->Step(output, state, input);
EXPECT_LT(joint->GetVelocity(0), 0.0);
// Check that invalid velocity commands don't cause collisions to fail
for (std::size_t i = 0; i < 1000; ++i)
{
joint->SetForce(0, std::numeric_limits<double>::quiet_NaN());
// expect the position of the pendulum to stay aabove ground
world->Step(output, state, input);
EXPECT_NEAR(0.0, dartBaseLink->getWorldTransform().translation().z(), 1e-3);
}
joint->SetVelocityCommand(0, 1);
world->Step(output, state, input);
// Setting a velocity command changes the actuator type to SERVO
EXPECT_EQ(dart::dynamics::Joint::SERVO, dartJoint->getActuatorType());
const std::size_t numSteps = 10;
for (std::size_t i = 0; i < numSteps; ++i)
{
// Call SetVelocityCommand before each step
joint->SetVelocityCommand(0, 1);
world->Step(output, state, input);
EXPECT_NEAR(1.0, joint->GetVelocity(0), 1e-6);
}
for (std::size_t i = 0; i < numSteps; ++i)
{
// expect joint to freeze in subsequent steps without SetVelocityCommand
world->Step(output, state, input);
EXPECT_NEAR(0.0, joint->GetVelocity(0), 1e-6);
}
// Check that invalid velocity commands don't cause collisions to fail
for (std::size_t i = 0; i < 1000; ++i)
{
joint->SetVelocityCommand(0, std::numeric_limits<double>::quiet_NaN());
// expect the position of the pendulum to stay aabove ground
world->Step(output, state, input);
EXPECT_NEAR(0.0, dartBaseLink->getWorldTransform().translation().z(), 1e-3);
}
}
TEST_F(JointFeaturesFixture, JointSetPositionLimitsWithForceControl)
{
sdf::Root root;
const sdf::Errors errors = root.Load(TEST_WORLD_DIR "test.world");
ASSERT_TRUE(errors.empty()) << errors.front();
auto world = this->engine->ConstructWorld(*root.WorldByIndex(0));
auto model = world->GetModel("simple_joint_test");
auto joint = model->GetJoint("j1");
physics::ForwardStep::Output output;
physics::ForwardStep::State state;
physics::ForwardStep::Input input;
world->Step(output, state, input);
auto pos = joint->GetPosition(0);
joint->SetMinPosition(0, pos - 0.1);
joint->SetMaxPosition(0, pos + 0.1);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetForce(0, 100);
world->Step(output, state, input);
}
EXPECT_NEAR(pos + 0.1, joint->GetPosition(0), 1e-3);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetForce(0, -100);
world->Step(output, state, input);
}
EXPECT_NEAR(pos - 0.1, joint->GetPosition(0), 1e-3);
joint->SetMinPosition(0, pos - 0.5);
joint->SetMaxPosition(0, pos + 0.5);
for (std::size_t i = 0; i < 300; ++i)
{
joint->SetForce(0, 100);
world->Step(output, state, input);
}
EXPECT_NEAR(pos + 0.5, joint->GetPosition(0), 1e-2);
for (std::size_t i = 0; i < 300; ++i)
{
joint->SetForce(0, -100);
world->Step(output, state, input);
}
EXPECT_NEAR(pos - 0.5, joint->GetPosition(0), 1e-2);
joint->SetMinPosition(0, -math::INF_D);
joint->SetMaxPosition(0, math::INF_D);
joint->SetPosition(0, pos);
for (std::size_t i = 0; i < 300; ++i)
{
joint->SetForce(0, 100);
world->Step(output, state, input);
}
EXPECT_LT(pos + 0.5, joint->GetPosition(0));
}
#if DART_VERSION_AT_LEAST(6, 10, 0)
TEST_F(JointFeaturesFixture, JointSetVelocityLimitsWithForceControl)
{
sdf::Root root;
const sdf::Errors errors = root.Load(TEST_WORLD_DIR "test.world");
ASSERT_TRUE(errors.empty()) << errors.front();
auto world = this->engine->ConstructWorld(*root.WorldByIndex(0));
auto model = world->GetModel("simple_joint_test");
auto joint = model->GetJoint("j1");
physics::ForwardStep::Output output;
physics::ForwardStep::State state;
physics::ForwardStep::Input input;
world->Step(output, state, input);
joint->SetMinVelocity(0, -0.25);
joint->SetMaxVelocity(0, 0.5);
for (std::size_t i = 0; i < 10; ++i)
{
joint->SetForce(0, 1000);
world->Step(output, state, input);
}
EXPECT_NEAR(0.5, joint->GetVelocity(0), 1e-6);
for (std::size_t i = 0; i < 10; ++i)
{
joint->SetForce(0, -1000);
world->Step(output, state, input);
}
EXPECT_NEAR(-0.25, joint->GetVelocity(0), 1e-6);
// set minimum velocity above zero
joint->SetMinVelocity(0, 0.25);
for (std::size_t i = 0; i < 10; ++i)
{
joint->SetForce(0, 0);
world->Step(output, state, input);
}
EXPECT_NEAR(0.25, joint->GetVelocity(0), 1e-6);
for (std::size_t i = 0; i < 10; ++i)
{
// make sure the minimum velocity is kept even without velocity commands
world->Step(output, state, input);
}
EXPECT_NEAR(0.25, joint->GetVelocity(0), 1e-6);
joint->SetMinVelocity(0, -0.25);
joint->SetPosition(0, 0);
joint->SetVelocity(0, 0);
for (std::size_t i = 0; i < 10; ++i)
{
joint->SetForce(0, 0);
world->Step(output, state, input);
}
EXPECT_NEAR(0, joint->GetVelocity(0), 1e-6);
joint->SetMinVelocity(0, -math::INF_D);
joint->SetMaxVelocity(0, math::INF_D);
for (std::size_t i = 0; i < 10; ++i)
{
joint->SetForce(0, 1000);
world->Step(output, state, input);
}
EXPECT_LT(0.5, joint->GetVelocity(0));
}
TEST_F(JointFeaturesFixture, JointSetEffortLimitsWithForceControl)
{
sdf::Root root;
const sdf::Errors errors = root.Load(TEST_WORLD_DIR "test.world");
ASSERT_TRUE(errors.empty()) << errors.front();
auto world = this->engine->ConstructWorld(*root.WorldByIndex(0));
auto model = world->GetModel("simple_joint_test");
auto joint = model->GetJoint("j1");
physics::ForwardStep::Output output;
physics::ForwardStep::State state;
physics::ForwardStep::Input input;
world->Step(output, state, input);
auto pos = joint->GetPosition(0);
joint->SetMinEffort(0, -1e-6);
joint->SetMaxEffort(0, 1e-6);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetForce(0, 1);
world->Step(output, state, input);
}
EXPECT_NEAR(pos, joint->GetPosition(0), 1e-3);
EXPECT_NEAR(0, joint->GetVelocity(0), 1e-6);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetForce(0, -1);
world->Step(output, state, input);
}
EXPECT_NEAR(pos, joint->GetPosition(0), 1e-3);
EXPECT_NEAR(0, joint->GetVelocity(0), 1e-6);
joint->SetMinEffort(0, -80);
joint->SetMaxEffort(0, 80);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetForce(0, 1);
world->Step(output, state, input);
}
EXPECT_LT(pos, joint->GetPosition(0));
EXPECT_LT(0, joint->GetVelocity(0));
joint->SetMinEffort(0, -math::INF_D);
joint->SetMaxEffort(0, math::INF_D);
joint->SetPosition(0, 0);
joint->SetVelocity(0, 0);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetForce(0, 1);
world->Step(output, state, input);
}
EXPECT_LT(pos, joint->GetPosition(0));
EXPECT_LT(0, joint->GetVelocity(0));
}
TEST_F(JointFeaturesFixture, JointSetCombinedLimitsWithForceControl)
{
sdf::Root root;
const sdf::Errors errors = root.Load(TEST_WORLD_DIR "test.world");
ASSERT_TRUE(errors.empty()) << errors.front();
auto world = this->engine->ConstructWorld(*root.WorldByIndex(0));
auto model = world->GetModel("simple_joint_test");
auto joint = model->GetJoint("j1");
physics::ForwardStep::Output output;
physics::ForwardStep::State state;
physics::ForwardStep::Input input;
world->Step(output, state, input);
auto pos = joint->GetPosition(0);
joint->SetMinPosition(0, pos - 0.1);
joint->SetMaxPosition(0, pos + 0.1);
joint->SetMinVelocity(0, -0.25);
joint->SetMaxVelocity(0, 0.5);
joint->SetMinEffort(0, -1e-6);
joint->SetMaxEffort(0, 1e-6);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetForce(0, 100);
world->Step(output, state, input);
}
EXPECT_NEAR(pos, joint->GetPosition(0), 1e-2);
EXPECT_NEAR(0, joint->GetVelocity(0), 1e-6);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetForce(0, -100);
world->Step(output, state, input);
}
EXPECT_NEAR(pos, joint->GetPosition(0), 1e-2);
EXPECT_NEAR(0, joint->GetVelocity(0), 1e-6);
joint->SetMinEffort(0, -500);
joint->SetMaxEffort(0, 1000);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetForce(0, 1000);
world->Step(output, state, input);
}
// 0.05 because we go 0.1 s with max speed 0.5
EXPECT_NEAR(pos + 0.05, joint->GetPosition(0), 1e-2);
EXPECT_NEAR(0.5, joint->GetVelocity(0), 1e-6);
for (std::size_t i = 0; i < 200; ++i)
{
joint->SetForce(0, 1000);
world->Step(output, state, input);
}
EXPECT_NEAR(pos + 0.1, joint->GetPosition(0), 1e-2);
EXPECT_NEAR(0, joint->GetVelocity(0), 1e-6);
joint->SetPosition(0, pos);
EXPECT_NEAR(pos, joint->GetPosition(0), 1e-2);
joint->SetMinVelocity(0, -1);
joint->SetMaxVelocity(0, 1);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetForce(0, 1000);
world->Step(output, state, input);
}
EXPECT_NEAR(pos + 0.1, joint->GetPosition(0), 1e-2);
EXPECT_NEAR(1, joint->GetVelocity(0), 1e-6);
joint->SetPosition(0, pos);
EXPECT_NEAR(pos, joint->GetPosition(0), 1e-2);
joint->SetMinPosition(0, -1e6);
joint->SetMaxPosition(0, 1e6);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetForce(0, 1000);
world->Step(output, state, input);
}
EXPECT_LT(pos + 0.1, joint->GetPosition(0));
EXPECT_NEAR(1, joint->GetVelocity(0), 1e-6);
}
// TODO(anyone): position limits do not work very well with velocity control
// bug https://github.com/dartsim/dart/issues/1583
// resolved in DART 6.11.0
TEST_F(JointFeaturesFixture, DISABLED_JointSetPositionLimitsWithVelocityControl)
{
sdf::Root root;
const sdf::Errors errors = root.Load(TEST_WORLD_DIR "test.world");
ASSERT_TRUE(errors.empty()) << errors.front();
const std::string modelName{"simple_joint_test"};
const std::string jointName{"j1"};
auto world = this->engine->ConstructWorld(*root.WorldByIndex(0));
auto model = world->GetModel(modelName);
auto joint = model->GetJoint(jointName);
physics::ForwardStep::Output output;
physics::ForwardStep::State state;
physics::ForwardStep::Input input;
world->Step(output, state, input);
auto pos = joint->GetPosition(0);
joint->SetMinPosition(0, pos - 0.1);
joint->SetMaxPosition(0, pos + 0.1);
for (std::size_t i = 0; i < 1000; ++i)
{
joint->SetVelocityCommand(0, 1);
world->Step(output, state, input);
if (i % 500 == 499)
{
EXPECT_NEAR(pos + 0.1, joint->GetPosition(0), 1e-2);
EXPECT_NEAR(0, joint->GetVelocity(0), 1e-6);
}
}
}
TEST_F(JointFeaturesFixture, JointSetVelocityLimitsWithVelocityControl)
{
sdf::Root root;
const sdf::Errors errors = root.Load(TEST_WORLD_DIR "test.world");
ASSERT_TRUE(errors.empty()) << errors.front();
auto world = this->engine->ConstructWorld(*root.WorldByIndex(0));
auto model = world->GetModel("simple_joint_test");
auto joint = model->GetJoint("j1");
physics::ForwardStep::Output output;
physics::ForwardStep::State state;
physics::ForwardStep::Input input;
joint->SetMinVelocity(0, -0.1);
joint->SetMaxVelocity(0, 0.1);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetVelocityCommand(0, 1);
world->Step(output, state, input);
}
EXPECT_NEAR(0.1, joint->GetVelocity(0), 1e-6);
for (std::size_t i = 0; i < 10; ++i)
{
joint->SetVelocityCommand(0, 0.1);
world->Step(output, state, input);
}
EXPECT_NEAR(0.1, joint->GetVelocity(0), 1e-6);
for (std::size_t i = 0; i < 10; ++i)
{
joint->SetVelocityCommand(0, -0.025);
world->Step(output, state, input);
}
EXPECT_NEAR(-0.025, joint->GetVelocity(0), 1e-6);
for (std::size_t i = 0; i < 10; ++i)
{
joint->SetVelocityCommand(0, -1);
world->Step(output, state, input);
}
EXPECT_NEAR(-0.1, joint->GetVelocity(0), 1e-6);
joint->SetMinVelocity(0, -math::INF_D);
joint->SetMaxVelocity(0, math::INF_D);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetVelocityCommand(0, 1);
world->Step(output, state, input);
}
EXPECT_NEAR(1, joint->GetVelocity(0), 1e-6);
}
TEST_F(JointFeaturesFixture, JointSetEffortLimitsWithVelocityControl)
{
sdf::Root root;
const sdf::Errors errors = root.Load(TEST_WORLD_DIR "test.world");
ASSERT_TRUE(errors.empty()) << errors.front();
auto world = this->engine->ConstructWorld(*root.WorldByIndex(0));
auto model = world->GetModel("simple_joint_test");
auto joint = model->GetJoint("j1");
physics::ForwardStep::Output output;
physics::ForwardStep::State state;
physics::ForwardStep::Input input;
joint->SetMinEffort(0, -1e-6);
joint->SetMaxEffort(0, 1e-6);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetVelocityCommand(0, 1);
world->Step(output, state, input);
}
EXPECT_NEAR(0, joint->GetVelocity(0), 1e-6);
joint->SetMinEffort(0, -80);
joint->SetMaxEffort(0, 80);
for (std::size_t i = 0; i < 100; ++i)
{
joint->SetVelocityCommand(0, -1);
world->Step(output, state, input);
}
EXPECT_NEAR(-1, joint->GetVelocity(0), 1e-6);
joint->SetMinEffort(0, -math::INF_D);
joint->SetMaxEffort(0, math::INF_D);
for (std::size_t i = 0; i < 10; ++i)
{
joint->SetVelocityCommand(0, -100);
world->Step(output, state, input);
}
EXPECT_NEAR(-100, joint->GetVelocity(0), 1e-6);
}
TEST_F(JointFeaturesFixture, JointSetCombinedLimitsWithVelocityControl)
{
sdf::Root root;
const sdf::Errors errors = root.Load(TEST_WORLD_DIR "test.world");
ASSERT_TRUE(errors.empty()) << errors.front();
auto world = this->engine->ConstructWorld(*root.WorldByIndex(0));
auto model = world->GetModel("simple_joint_test");
auto joint = model->GetJoint("j1");
// Test joint velocity command
physics::ForwardStep::Output output;
physics::ForwardStep::State state;
physics::ForwardStep::Input input;
joint->SetMinVelocity(0, -0.5);
joint->SetMaxVelocity(0, 0.5);
joint->SetMinEffort(0, -1e-6);
joint->SetMaxEffort(0, 1e-6);
for (std::size_t i = 0; i < 1000; ++i)
{
joint->SetVelocityCommand(0, 1);
world->Step(output, state, input);
}
EXPECT_NEAR(0, joint->GetVelocity(0), 1e-6);
joint->SetMinEffort(0, -1e6);
joint->SetMaxEffort(0, 1e6);
for (std::size_t i = 0; i < 1000; ++i)
{
joint->SetVelocityCommand(0, -1);
world->Step(output, state, input);
}
EXPECT_NEAR(-0.5, joint->GetVelocity(0), 1e-6);
}
#endif
// Test detaching joints.
TEST_F(JointFeaturesFixture, JointDetach)
{
sdf::Root root;
const sdf::Errors errors = root.Load(TEST_WORLD_DIR "test.world");
ASSERT_TRUE(errors.empty()) << errors.front();
const std::string modelName{"double_pendulum_with_base"};
const std::string upperJointName{"upper_joint"};
const std::string lowerJointName{"lower_joint"};
const std::string upperLinkName{"upper_link"};
const std::string lowerLinkName{"lower_link"};
auto world = this->engine->ConstructWorld(*root.WorldByIndex(0));
auto model = world->GetModel(modelName);
auto upperLink = model->GetLink(upperLinkName);
auto lowerLink = model->GetLink(lowerLinkName);
auto upperJoint = model->GetJoint(upperJointName);
auto lowerJoint = model->GetJoint(lowerJointName);
// test Cast*Joint functions
EXPECT_NE(nullptr, upperJoint->CastToRevoluteJoint());
EXPECT_NE(nullptr, lowerJoint->CastToRevoluteJoint());
EXPECT_EQ(nullptr, upperJoint->CastToFreeJoint());
EXPECT_EQ(nullptr, lowerJoint->CastToFreeJoint());
dart::simulation::WorldPtr dartWorld = world->GetDartsimWorld();
ASSERT_NE(nullptr, dartWorld);
const dart::dynamics::SkeletonPtr skeleton =
dartWorld->getSkeleton(modelName);
ASSERT_NE(nullptr, skeleton);
const auto *dartUpperLink = skeleton->getBodyNode(upperLinkName);
const auto *dartLowerLink = skeleton->getBodyNode(lowerLinkName);
EXPECT_EQ("RevoluteJoint", dartUpperLink->getParentJoint()->getType());
EXPECT_EQ("RevoluteJoint", dartLowerLink->getParentJoint()->getType());
const math::Pose3d initialUpperLinkPose(1, 0, 2.1, -IGN_PI/2, 0, 0);
const math::Pose3d initialLowerLinkPose(1.25, 1, 2.1, -2, 0, 0);
EXPECT_EQ(initialUpperLinkPose,
math::eigen3::convert(dartUpperLink->getWorldTransform()));
EXPECT_EQ(initialLowerLinkPose,
math::eigen3::convert(dartLowerLink->getWorldTransform()));
// detach lower joint
lowerJoint->Detach();
EXPECT_EQ("FreeJoint", dartLowerLink->getParentJoint()->getType());
EXPECT_NE(nullptr, lowerJoint->CastToFreeJoint());
EXPECT_EQ(nullptr, lowerJoint->CastToRevoluteJoint());
// Detach() can be called again though it has no effect
lowerJoint->Detach();
EXPECT_EQ("FreeJoint", dartLowerLink->getParentJoint()->getType());
EXPECT_NE(nullptr, lowerJoint->CastToFreeJoint());
EXPECT_EQ(nullptr, lowerJoint->CastToRevoluteJoint());
// expect poses to remain unchanged
EXPECT_EQ(initialUpperLinkPose,
math::eigen3::convert(dartUpperLink->getWorldTransform()));
EXPECT_EQ(initialLowerLinkPose,
math::eigen3::convert(dartLowerLink->getWorldTransform()));
physics::ForwardStep::Output output;
physics::ForwardStep::State state;
physics::ForwardStep::Input input;
const std::size_t numSteps = 10;
for (std::size_t i = 0; i < numSteps; ++i)
{
// step forward and expect lower link to fall
world->Step(output, state, input);
// expect upper link to rotate
EXPECT_LT(upperJoint->GetVelocity(0), 0.0);
// expect lower link to fall down without rotating
math::Vector3d lowerLinkLinearVelocity =
math::eigen3::convert(dartLowerLink->getLinearVelocity());
EXPECT_NEAR(0.0, lowerLinkLinearVelocity.X(), 1e-10);
EXPECT_NEAR(0.0, lowerLinkLinearVelocity.Y(), 1e-10);
EXPECT_GT(0.0, lowerLinkLinearVelocity.Z());
math::Vector3d lowerLinkAngularVelocity =
math::eigen3::convert(dartLowerLink->getAngularVelocity());
EXPECT_EQ(math::Vector3d::Zero, lowerLinkAngularVelocity);
}
// now detach the upper joint too, and ensure that velocity is preserved
math::Pose3d upperLinkPose =
math::eigen3::convert(dartUpperLink->getWorldTransform());
math::Vector3d upperLinkLinearVelocity =
math::eigen3::convert(dartUpperLink->getLinearVelocity());
math::Vector3d upperLinkAngularVelocity =
math::eigen3::convert(dartUpperLink->getAngularVelocity());
// sanity check on velocity values
EXPECT_LT(1e-5, upperLinkLinearVelocity.Z());
EXPECT_GT(-0.03, upperLinkAngularVelocity.X());
EXPECT_NEAR(0.0, upperLinkLinearVelocity.X(), 1e-6);
EXPECT_NEAR(0.0, upperLinkLinearVelocity.Y(), 1e-6);
EXPECT_NEAR(0.0, upperLinkAngularVelocity.Y(), 1e-6);
EXPECT_NEAR(0.0, upperLinkAngularVelocity.Z(), 1e-6);
upperJoint->Detach();
EXPECT_EQ("FreeJoint", dartUpperLink->getParentJoint()->getType());
EXPECT_NE(nullptr, upperJoint->CastToFreeJoint());
EXPECT_EQ(nullptr, upperJoint->CastToRevoluteJoint());
EXPECT_EQ(upperLinkPose,
math::eigen3::convert(dartUpperLink->getWorldTransform()));
EXPECT_EQ(upperLinkLinearVelocity,
math::eigen3::convert(dartUpperLink->getLinearVelocity()));
EXPECT_EQ(upperLinkAngularVelocity,
math::eigen3::convert(dartUpperLink->getAngularVelocity()));
}
/////////////////////////////////////////////////
// Attach a fixed joint between links that belong to different models
TEST_F(JointFeaturesFixture, JointAttachDetach)
{
sdf::Root root;
const sdf::Errors errors =
root.Load(TEST_WORLD_DIR "joint_across_models.sdf");
ASSERT_TRUE(errors.empty()) << errors.front();
auto world = this->engine->ConstructWorld(*root.WorldByIndex(0));
dart::simulation::WorldPtr dartWorld = world->GetDartsimWorld();
ASSERT_NE(nullptr, dartWorld);
const std::string modelName1{"M1"};
const std::string modelName2{"M2"};
const std::string bodyName{"body"};
auto model1 = world->GetModel(modelName1);
auto model2 = world->GetModel(modelName2);
auto model1Body = model1->GetLink(bodyName);
auto model2Body = model2->GetLink(bodyName);
const dart::dynamics::SkeletonPtr skeleton1 =
dartWorld->getSkeleton(modelName1);
const dart::dynamics::SkeletonPtr skeleton2 =
dartWorld->getSkeleton(modelName2);
ASSERT_NE(nullptr, skeleton1);
ASSERT_NE(nullptr, skeleton2);
auto *dartBody1 = skeleton1->getBodyNode(bodyName);
auto *dartBody2 = skeleton2->getBodyNode(bodyName);
ASSERT_NE(nullptr, dartBody1);
ASSERT_NE(nullptr, dartBody2);
const math::Pose3d initialModel1Pose(0, 0, 0.25, 0, 0, 0);
const math::Pose3d initialModel2Pose(0, 0, 3.0, 0, 0, 0);
EXPECT_EQ(initialModel1Pose,
math::eigen3::convert(dartBody1->getWorldTransform()));
EXPECT_EQ(initialModel2Pose,
math::eigen3::convert(dartBody2->getWorldTransform()));
physics::ForwardStep::Output output;
physics::ForwardStep::State state;
physics::ForwardStep::Input input;
const std::size_t numSteps = 100;
for (std::size_t i = 0; i < numSteps; ++i)
{
world->Step(output, state, input);
// Expect the model1 to stay at rest (since it's on the ground) and model2
// to start falling
math::Vector3d body1LinearVelocity =
math::eigen3::convert(dartBody1->getLinearVelocity());
math::Vector3d body2LinearVelocity =
math::eigen3::convert(dartBody2->getLinearVelocity());
EXPECT_NEAR(0.0, body1LinearVelocity.Z(), 1e-7);
// Negative z velocity
EXPECT_GT(0.0, body2LinearVelocity.Z());
}
const auto poseParent = dartBody1->getTransform();
const auto poseChild = dartBody2->getTransform();
auto poseParentChild = poseParent.inverse() * poseChild;
auto fixedJoint = model2Body->AttachFixedJoint(model1Body);
// AttachFixedJoint snaps the child body to the origin of the parent, so we
// set a transform on the joint to keep the transform between the two bodies
// the same as it was before they were attached
fixedJoint->SetTransformFromParent(poseParentChild);
// The name of the link obtained using the ign-physics API should remain the
// same even though AttachFixedJoint renames the associated BodyNode.
EXPECT_EQ(bodyName, model2Body->GetName());
for (std::size_t i = 0; i < numSteps; ++i)
{
world->Step(output, state, input);
// Expect the model1 to remain at rest and model2
// to stop moving
math::Vector3d body1LinearVelocity =
math::eigen3::convert(dartBody1->getLinearVelocity());
math::Vector3d body2LinearVelocity =
math::eigen3::convert(dartBody2->getLinearVelocity());
// TODO(arjo): Investigate the drop in tolerance
EXPECT_NEAR(0.0, body1LinearVelocity.Z(), 1e-5);
EXPECT_NEAR(0.0, body2LinearVelocity.Z(), 1e-5);
}
// now detach joint and expect model2 to start moving again
fixedJoint->Detach();
// The name of the link obtained using the ign-physics API should remain the
// same even though Detach renames the associated BodyNode.
EXPECT_EQ(bodyName, model2Body->GetName());
for (std::size_t i = 0; i < numSteps; ++i)
{
world->Step(output, state, input);
// Expect the model1 to remain at rest and model2
// to start moving again
math::Vector3d body1LinearVelocity =
math::eigen3::convert(dartBody1->getLinearVelocity());
math::Vector3d body2LinearVelocity =
math::eigen3::convert(dartBody2->getLinearVelocity());
EXPECT_NEAR(0.0, body1LinearVelocity.Z(), 1e-7);
// Negative z velocity
EXPECT_GT(0.0, body2LinearVelocity.Z());
}
// After a while, body2 should reach the ground and come to a stop
for (std::size_t i = 0; i < 1000; ++i)
{
world->Step(output, state, input);
}
EXPECT_NEAR(0.0, dartBody2->getLinearVelocity().z(), 1e-3);
}
/////////////////////////////////////////////////
// Essentially what happens is there are two floating boxes and a box in the
// middle that's resting. We start the system out by creating the two
// fixed joints between the boxes resting on the big box. The middle box will
// now have two parents. However there should be no movement as the middle box
// will be holding the other two boxes that are floating in mid air. We run
// this for 100 steps to make sure that there is no movement. This is because
// the middle box is holding on to the two side boxes. Then we release the
// joints the two boxes should fall away.
TEST_F(JointFeaturesFixture, JointAttachMultiple)
{
sdf::Root root;
const sdf::Errors errors =
root.Load(TEST_WORLD_DIR "joint_constraint.sdf");
ASSERT_TRUE(errors.empty()) << errors.front();
auto world = this->engine->ConstructWorld(*root.WorldByIndex(0));
dart::simulation::WorldPtr dartWorld = world->GetDartsimWorld();
ASSERT_NE(nullptr, dartWorld);
const std::string modelName1{"M1"};
const std::string modelName2{"M2"};
const std::string modelName3{"M3"};
const std::string bodyName{"link"};
auto model1 = world->GetModel(modelName1);
auto model2 = world->GetModel(modelName2);
auto model3 = world->GetModel(modelName3);
auto model1Body = model1->GetLink(bodyName);
auto model2Body = model2->GetLink(bodyName);
auto model3Body = model3->GetLink(bodyName);
const dart::dynamics::SkeletonPtr skeleton1 =
dartWorld->getSkeleton(modelName1);
const dart::dynamics::SkeletonPtr skeleton2 =
dartWorld->getSkeleton(modelName2);
const dart::dynamics::SkeletonPtr skeleton3 =
dartWorld->getSkeleton(modelName3);
ASSERT_NE(nullptr, skeleton1);
ASSERT_NE(nullptr, skeleton2);
ASSERT_NE(nullptr, skeleton3);
auto *dartBody1 = skeleton1->getBodyNode(bodyName);
auto *dartBody2 = skeleton2->getBodyNode(bodyName);
auto *dartBody3 = skeleton3->getBodyNode(bodyName);
ASSERT_NE(nullptr, dartBody1);
ASSERT_NE(nullptr, dartBody2);
ASSERT_NE(nullptr, dartBody3);
const math::Pose3d initialModel1Pose(0, -0.2, 0.45, 0, 0, 0);
const math::Pose3d initialModel2Pose(0, 0.2, 0.45, 0, 0, 0);
const math::Pose3d initialModel3Pose(0, 0.6, 0.45, 0, 0, 0);
EXPECT_EQ(initialModel1Pose,
math::eigen3::convert(dartBody1->getWorldTransform()));
EXPECT_EQ(initialModel2Pose,
math::eigen3::convert(dartBody2->getWorldTransform()));
EXPECT_EQ(initialModel3Pose,
math::eigen3::convert(dartBody3->getWorldTransform()));
physics::ForwardStep::Output output;
physics::ForwardStep::State state;
physics::ForwardStep::Input input;
// Create the joints
auto fixedJoint1 = model2Body->AttachFixedJoint(model1Body);
auto fixedJoint2 = model2Body->AttachFixedJoint(model3Body);
{
const auto poseParent = dartBody1->getTransform();
const auto poseChild = dartBody2->getTransform();
auto poseParentChild = poseParent.inverse() * poseChild;
fixedJoint1->SetTransformFromParent(poseParentChild);
}
{
const auto poseParent = dartBody3->getTransform();
const auto poseChild = dartBody2->getTransform();
auto poseParentChild = poseParent.inverse() * poseChild;
fixedJoint2->SetTransformFromParent(poseParentChild);
}
const std::size_t numSteps = 100;
for (std::size_t i = 0; i < numSteps; ++i)
{
world->Step(output, state, input);
// Expect the model1 to stay at rest
// (since it's held in place by the joints)
math::Vector3d body1LinearVelocity =
math::eigen3::convert(dartBody1->getLinearVelocity());
math::Vector3d body2LinearVelocity =
math::eigen3::convert(dartBody2->getLinearVelocity());
math::Vector3d body3LinearVelocity =
math::eigen3::convert(dartBody3->getLinearVelocity());
EXPECT_NEAR(0.0, body1LinearVelocity.Z(), 1e-7);
EXPECT_NEAR(0.0, body2LinearVelocity.Z(), 1e-7);
EXPECT_NEAR(0.0, body3LinearVelocity.Z(), 1e-7);
}
// Detach the joints. M1 and M3 should fall as there is now nothing stopping
// them from falling.
fixedJoint1->Detach();
fixedJoint2->Detach();
for (std::size_t i = 0; i < numSteps; ++i)
{
world->Step(output, state, input);
// Expect the model1 to stay at rest (since it's on the ground) and model2
// to start falling
math::Vector3d body1LinearVelocity =