27 #include <gtest/gtest.h>
33 TEST(TestCartesianTrajectorySegment, SamplingBeyondBoundariesIsSafe)
39 start.v_dot.x() = 1.0;
40 start.w_dot.x() = 1.0;
41 double start_time = 0;
49 double end_time = 5.0;
57 seg.sample(-1.0, sample_lower);
59 EXPECT_DOUBLE_EQ(
start.p.x(), sample_lower.
p.x());
60 EXPECT_DOUBLE_EQ(0, sample_lower.
v.x());
61 EXPECT_DOUBLE_EQ(0, sample_lower.
w.x());
62 EXPECT_DOUBLE_EQ(0, sample_lower.
v_dot.x());
63 EXPECT_DOUBLE_EQ(0, sample_lower.
w_dot.x());
66 seg.sample(6.0, sample_lower);
68 EXPECT_DOUBLE_EQ(end.
p.x(), sample_lower.
p.x());
69 EXPECT_DOUBLE_EQ(0, sample_lower.
v.x());
70 EXPECT_DOUBLE_EQ(0, sample_lower.
w.x());
71 EXPECT_DOUBLE_EQ(0, sample_lower.
v_dot.x());
72 EXPECT_DOUBLE_EQ(0, sample_lower.
w_dot.x());
75 TEST(TestCartesianTrajectorySegment, DoubleConversionIsIdentity)
98 std::stringstream before;
99 std::stringstream after;
104 EXPECT_EQ(before.str(), after.str());
112 EXPECT_EQ(before.str(), after.str());
115 TEST(TestCartesianTrajectorySegment, NansYieldEmptySplineVelocities)
126 c.
v.x() = std::nan(
"0");
129 s.position.push_back(1.0);
130 s.position.push_back(2.0);
131 s.position.push_back(3.0);
132 s.position.push_back(4.0);
133 s.position.push_back(5.0);
134 s.position.push_back(6.0);
135 s.position.push_back(7.0);
136 std::stringstream expected;
140 std::stringstream actual;
142 EXPECT_EQ(expected.str(), actual.str());
145 TEST(TestCartesianTrajectorySegment, ConvertToSplineState)
149 cartesian_state.
p.x() = 0.1;
150 cartesian_state.
p.y() = 0.2;
151 cartesian_state.
p.z() = 0.3;
152 cartesian_state.
q.w() = 0.0;
153 cartesian_state.
q.x() = -0.707107;
154 cartesian_state.
q.y() = -0.707107;
155 cartesian_state.
q.z() = 0.0;
157 cartesian_state.
v[0] = 0.1;
158 cartesian_state.
v[1] = 0.1;
159 cartesian_state.
v[2] = 0.1;
160 cartesian_state.
w[0] = 0.1;
161 cartesian_state.
w[1] = 0.1;
162 cartesian_state.
w[2] = 0.1;
164 cartesian_state.
v_dot[0] = 0.1;
165 cartesian_state.
v_dot[1] = 0.1;
166 cartesian_state.
v_dot[2] = 0.1;
167 cartesian_state.
w_dot[0] = 0.1;
168 cartesian_state.
w_dot[1] = 0.1;
169 cartesian_state.
w_dot[2] = 0.1;
173 expected_spline_state.
position.push_back(0.1);
174 expected_spline_state.
position.push_back(0.2);
175 expected_spline_state.
position.push_back(0.3);
176 expected_spline_state.
position.push_back(0.0);
177 expected_spline_state.
position.push_back(-0.707107);
178 expected_spline_state.
position.push_back(-0.707107);
179 expected_spline_state.
position.push_back(0.0);
181 expected_spline_state.
velocity.push_back(0.1);
182 expected_spline_state.
velocity.push_back(0.1);
183 expected_spline_state.
velocity.push_back(-0.0999999);
184 expected_spline_state.
velocity.push_back(0.0707107);
185 expected_spline_state.
velocity.push_back(0.0353553);
186 expected_spline_state.
velocity.push_back(-0.0353553);
187 expected_spline_state.
velocity.push_back(0.0);
191 expected_spline_state.
acceleration.push_back(-0.0999999);
192 expected_spline_state.
acceleration.push_back(0.0707107);
193 expected_spline_state.
acceleration.push_back(0.0406586);
194 expected_spline_state.
acceleration.push_back(-0.030052);
197 std::stringstream expected;
198 expected << expected_spline_state;
201 std::stringstream actual;
202 actual << actual_spline_state;
204 EXPECT_EQ(expected.str(), actual.str());
207 TEST(TestCartesianTrajectorySegment, ConvertToCartesianState)
211 spline_state.
position.push_back(0.1);
212 spline_state.
position.push_back(0.2);
213 spline_state.
position.push_back(0.3);
214 spline_state.
position.push_back(0.0);
215 spline_state.
position.push_back(-0.707107);
216 spline_state.
position.push_back(-0.707107);
217 spline_state.
position.push_back(0.0);
219 spline_state.
velocity.push_back(0.1);
220 spline_state.
velocity.push_back(0.1);
221 spline_state.
velocity.push_back(0.1);
223 spline_state.
velocity.push_back(0.05);
224 spline_state.
velocity.push_back(0.05);
225 spline_state.
velocity.push_back(0.05);
237 expected_cartesian_state.
p.x() = 0.1;
238 expected_cartesian_state.
p.y() = 0.2;
239 expected_cartesian_state.
p.z() = 0.3;
240 expected_cartesian_state.
q.w() = 0;
241 expected_cartesian_state.
q.x() = -0.707107;
242 expected_cartesian_state.
q.y() = -0.707107;
243 expected_cartesian_state.
q.z() = 0.0;
245 expected_cartesian_state.
v[0] = 0.1;
246 expected_cartesian_state.
v[1] = 0.1;
247 expected_cartesian_state.
v[2] = -0.1;
248 expected_cartesian_state.
w[0] = -0.0707107;
249 expected_cartesian_state.
w[1] = 0.0707107;
250 expected_cartesian_state.
w[2] = 0.0;
252 expected_cartesian_state.
v_dot[0] = 0.1;
253 expected_cartesian_state.
v_dot[1] = 0.1;
254 expected_cartesian_state.
v_dot[2] = -0.1;
255 expected_cartesian_state.
w_dot[0] = -0.0913173;
256 expected_cartesian_state.
w_dot[1] = 0.0701041;
257 expected_cartesian_state.
w_dot[2] = 0.0;
259 std::stringstream expected;
260 expected << expected_cartesian_state;
263 std::stringstream actual;
264 actual << actual_cartesian_state;
266 EXPECT_EQ(expected.str(), actual.str());
269 TEST(TestCartesianTrajectorySegment, interpolationOfOrientation)
277 auto compute_cartesian_state = [](
auto& cartesian_state,
const auto& time) {
279 double angle = sin(omega * time);
282 Eigen::Matrix3d rot_z = Eigen::Matrix3d::Zero();
283 rot_z(0, 0) = cos(
angle);
284 rot_z(0, 1) = -sin(
angle);
285 rot_z(1, 0) = sin(
angle);
286 rot_z(1, 1) = cos(
angle);
290 Eigen::Matrix3d rot_base = Eigen::Matrix3d::Zero();
291 rot_base(0, 1) = 1.0;
292 rot_base(1, 0) = 1.0;
293 rot_base(2, 2) = -1.0;
294 rot_z = rot_base * rot_z;
297 Eigen::Quaterniond q(rot_z);
299 cartesian_state.p.x() = 0.0;
300 cartesian_state.p.y() = 0.0;
301 cartesian_state.p.z() = 0.0;
303 cartesian_state.q.w() = q.w();
304 cartesian_state.q.x() = q.x();
305 cartesian_state.q.y() = q.y();
306 cartesian_state.q.z() = q.z();
308 cartesian_state.v[0] = 0.0;
309 cartesian_state.v[1] = 0.0;
310 cartesian_state.v[2] = 0.0;
312 cartesian_state.w[0] = 0.0;
313 cartesian_state.w[1] = 0.0;
314 cartesian_state.w[2] = omega * cos(omega * time);
316 cartesian_state.w = rot_base * cartesian_state.w;
318 cartesian_state.v_dot[0] = 0.0;
319 cartesian_state.v_dot[0] = 0.0;
320 cartesian_state.v_dot[0] = 0.0;
322 cartesian_state.w_dot[0] = 0.0;
323 cartesian_state.w_dot[1] = 0.0;
324 cartesian_state.w_dot[2] = (-omega * omega) * sin(omega * time);
326 cartesian_state.w_dot = rot_base * cartesian_state.w_dot;
329 double start_time = 0.0;
330 double end_time = 1.0;
334 compute_cartesian_state(start_state, start_time);
337 compute_cartesian_state(end_state, end_time);
343 segment.sample(0.3, sampled_state);
346 compute_cartesian_state(expected_state, 0.3);
350 EXPECT_NEAR(expected_state.
q.x(), sampled_state.
q.x(), eps);
351 EXPECT_NEAR(expected_state.
q.y(), sampled_state.
q.y(), eps);
352 EXPECT_NEAR(expected_state.
q.z(), sampled_state.
q.z(), eps);
353 EXPECT_NEAR(expected_state.
q.w(), sampled_state.
q.w(), eps);
356 EXPECT_NEAR(expected_state.
w[0], sampled_state.
w[0], eps);
357 EXPECT_NEAR(expected_state.
w[1], sampled_state.
w[1], eps);
358 EXPECT_NEAR(expected_state.
w[2], sampled_state.
w[2], eps);
361 EXPECT_NEAR(expected_state.
w_dot[0], sampled_state.
w_dot[0], eps);
362 EXPECT_NEAR(expected_state.
w_dot[1], sampled_state.
w_dot[1], eps);
363 EXPECT_NEAR(expected_state.
w_dot[2], sampled_state.
w_dot[2], eps);
366 int main(
int argc,
char** argv)
368 testing::InitGoogleTest(&argc, argv);
369 return RUN_ALL_TESTS();