forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrajectory_follower_test.cc
166 lines (133 loc) · 6.22 KB
/
trajectory_follower_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
#include "drake/automotive/trajectory_follower.h"
#include <vector>
#include <gtest/gtest.h>
#include "drake/common/extract_double.h"
#include "drake/systems/analysis/simulator.h"
#include "drake/systems/framework/test_utilities/scalar_conversion.h"
namespace drake {
namespace automotive {
namespace {
static constexpr double kTol = 1e-12;
static constexpr double kSpeed = 10.;
using Eigen::Quaternion;
using Eigen::Vector3d;
using systems::rendering::FrameVelocity;
using systems::rendering::PoseVector;
GTEST_TEST(TrajectoryFollowerTest, Topology) {
const std::vector<double> times{0., 1.};
std::vector<Quaternion<double>> rotations{Quaternion<double>::Identity(),
Quaternion<double>::Identity()};
std::vector<Vector3d> translations{Vector3d::Zero(), Vector3d::Zero()};
const TrajectoryFollower<double> follower(
Trajectory::Make(times, rotations, translations));
ASSERT_EQ(0, follower.get_num_input_ports());
ASSERT_EQ(3, follower.get_num_output_ports());
const auto& state_output = follower.state_output();
EXPECT_EQ(systems::kVectorValued, state_output.get_data_type());
EXPECT_EQ(SimpleCarStateIndices::kNumCoordinates, state_output.size());
const auto& pose_output = follower.pose_output();
EXPECT_EQ(systems::kVectorValued, pose_output.get_data_type());
EXPECT_EQ(PoseVector<double>::kSize, pose_output.size());
const auto& velocity_output = follower.velocity_output();
EXPECT_EQ(systems::kVectorValued, velocity_output.get_data_type());
EXPECT_EQ(FrameVelocity<double>::kSize, velocity_output.size());
ASSERT_FALSE(follower.HasAnyDirectFeedthrough());
}
GTEST_TEST(TrajectoryFollowerTest, Outputs) {
using std::cos;
using std::sin;
struct TestCase {
double heading;
double distance;
};
const std::vector<TestCase> test_cases{
{0., 1.}, // BR
{M_PI_2, 1.}, // BR
{-M_PI_2, 1.}, // BR
{0.125 * M_PI, 10.}, // BR
{0.125 * M_PI, 1.}, // BR
{0.125 * M_PI, 1.},
};
for (const auto& it : test_cases) {
const Quaternion<double> start_rotation{
math::RollPitchYaw<double>(0., 0., it.heading).ToQuaternion()};
const Vector3d start_translation({5., 10., 0.});
const Quaternion<double> end_rotation = start_rotation;
const Vector3d end_translation{
start_translation +
math::RotationMatrix<double>(start_rotation).matrix() *
Vector3d{it.distance, 0., 0.}};
const std::vector<Quaternion<double>> rotations{start_rotation,
end_rotation};
const std::vector<Vector3d> translations{start_translation,
end_translation};
const std::vector<double> times{0., it.distance / kSpeed};
const Trajectory trajectory =
Trajectory::Make(times, rotations, translations);
const TrajectoryFollower<double> follower(trajectory);
// Check that the outputs are correct over the entirety of the trajectory.
systems::Simulator<double> simulator(follower);
systems::Context<double>& context = simulator.get_mutable_context();
std::unique_ptr<systems::SystemOutput<double>> outputs =
follower.AllocateOutput();
simulator.Initialize();
const double end_time = it.distance / kSpeed;
for (double time = 0.; time <= end_time; time += 0.1) {
simulator.StepTo(time);
const double scalar =
std::min(std::max(0.0, (time * kSpeed) / it.distance), 1.);
const double position = scalar * it.distance;
Eigen::Translation<double, 3> expected_translation(
start_translation +
Vector3d{cos(it.heading) * position, sin(it.heading) * position, 0.});
follower.CalcOutput(context, outputs.get());
// Tests the state output.
const auto state = dynamic_cast<const SimpleCarState<double>*>(
outputs->get_vector_data(follower.state_output().get_index()));
EXPECT_EQ(SimpleCarStateIndices::kNumCoordinates, state->size());
EXPECT_NEAR(expected_translation.x(), state->x(), kTol);
EXPECT_NEAR(expected_translation.y(), state->y(), kTol);
EXPECT_NEAR(it.heading, state->heading(), kTol);
EXPECT_DOUBLE_EQ(kSpeed, state->velocity());
// Tests the PoseVector output.
const auto pose = dynamic_cast<const PoseVector<double>*>(
outputs->get_vector_data(follower.pose_output().get_index()));
ASSERT_NE(nullptr, pose);
EXPECT_EQ(PoseVector<double>::kSize, pose->size());
EXPECT_NEAR(expected_translation.x(),
pose->get_translation().translation().x(), kTol);
EXPECT_NEAR(expected_translation.y(),
pose->get_translation().translation().y(), kTol);
EXPECT_NEAR(cos(it.heading / 2), pose->get_rotation().w(), kTol);
EXPECT_NEAR(sin(it.heading / 2), pose->get_rotation().z(), kTol);
// Tests the FrameVelocity output.
const auto velocity = dynamic_cast<const FrameVelocity<double>*>(
outputs->get_vector_data(follower.velocity_output().get_index()));
ASSERT_NE(nullptr, velocity);
EXPECT_EQ(FrameVelocity<double>::kSize, velocity->size());
EXPECT_NEAR(kSpeed * cos(it.heading),
velocity->get_velocity().translational().x(), kTol);
EXPECT_NEAR(kSpeed * sin(it.heading),
velocity->get_velocity().translational().y(), kTol);
}
}
}
GTEST_TEST(TrajectoryFollowerTest, ToAutoDiff) {
const std::vector<double> times{0., 1.};
std::vector<Quaternion<double>> rotations{Quaternion<double>::Identity(),
Quaternion<double>::Identity()};
std::vector<Vector3d> translations{Vector3d::Zero(), Vector3d::Zero()};
const TrajectoryFollower<double> follower(
Trajectory::Make(times, rotations, translations));
EXPECT_TRUE(is_autodiffxd_convertible(follower,
[&](const auto& autodiff_dut) {
auto context = autodiff_dut.CreateDefaultContext();
auto output = autodiff_dut.AllocateOutput();
// Check that the public methods can be called without exceptions.
autodiff_dut.CalcOutput(*context, output.get());
}));
EXPECT_TRUE(is_symbolic_convertible(follower));
}
} // namespace
} // namespace automotive
} // namespace drake