-
Notifications
You must be signed in to change notification settings - Fork 162
/
rate_limiting_tests.cpp
556 lines (497 loc) · 29.1 KB
/
rate_limiting_tests.cpp
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
// Copyright (c) 2023 Franka Robotics GmbH
// Use of this source code is governed by the Apache-2.0 license, see LICENSE
#include <gtest/gtest.h>
#include <Eigen/Dense>
#include <franka/rate_limiting.h>
#include "helpers.h"
using namespace franka;
const double kNoLimit{std::numeric_limits<double>::max()};
const double kNoLowerLimit{std::numeric_limits<double>::lowest()};
std::array<double, 7> kJointsNoLimit{
{kNoLimit, kNoLimit, kNoLimit, kNoLimit, kNoLimit, kNoLimit, kNoLimit}};
std::array<double, 7> kJointsNoLowerLimit{{kNoLowerLimit, kNoLowerLimit, kNoLowerLimit,
kNoLowerLimit, kNoLowerLimit, kNoLowerLimit,
kNoLowerLimit}};
template <int size>
std::array<double, size> integrateOneSample(std::array<double, size> last_value,
std::array<double, size> derivative,
double delta_t) {
std::array<double, size> result{};
for (size_t i = 0; i < size; i++) {
result[i] = last_value[i] + derivative[i] * delta_t;
}
return result;
}
template <int size>
std::array<double, size> differentiateOneSample(std::array<double, size> value,
std::array<double, size> last_value,
double delta_t) {
std::array<double, size> result{};
for (size_t i = 0; i < size; i++) {
result[i] = (value[i] - last_value[i]) / delta_t;
}
return result;
}
std::array<double, 16> integrateOneSample(std::array<double, 16> last_pose,
std::array<double, 6> twist,
double delta_t) {
Eigen::Affine3d pose(Eigen::Matrix4d::Map(last_pose.data()));
Eigen::Map<Eigen::Matrix<double, 6, 1>> dx(twist.data());
Eigen::Matrix3d omega_skew;
omega_skew << 0, -dx[5], dx[4], dx[5], 0, -dx[3], -dx[4], dx[3], 0;
pose.linear() << pose.rotation() + omega_skew * pose.rotation() * delta_t;
pose.translation() << pose.translation() + dx.head(3) * delta_t;
std::array<double, 16> pose_after_integration{};
Eigen::Map<Eigen::Matrix4d>(&pose_after_integration[0], 4, 4) = pose.matrix();
return pose_after_integration;
}
bool violatesLimits(double desired_value, double max_value) {
return std::abs(desired_value) > max_value;
}
bool violatesLimits(std::array<double, 7> values, std::array<double, 7> max_values) {
bool violates_limits = false;
for (size_t i = 0; i < 7 && !violates_limits; i++) {
violates_limits = violates_limits || violatesLimits(values[i], max_values[i]);
}
return violates_limits;
}
bool violatesRateLimits(std::array<double, 7> max_derivatives,
std::array<double, 7> values,
std::array<double, 7> last_desired_values,
double delta_t) {
return violatesLimits(differentiateOneSample<7>(values, last_desired_values, delta_t),
max_derivatives);
}
bool violatesRateLimits(std::array<double, 7> max_values,
std::array<double, 7> max_derivatives,
std::array<double, 7> max_dderivatives,
std::array<double, 7> values,
std::array<double, 7> last_values,
std::array<double, 7> last_dvalues,
double delta_t) {
std::array<double, 7> desired_derivatives =
differentiateOneSample<7>(values, last_values, delta_t);
return violatesLimits(values, max_values) ||
violatesRateLimits(max_derivatives, values, last_values, delta_t) ||
violatesRateLimits(max_dderivatives, desired_derivatives, last_dvalues, delta_t);
}
bool violatesRateLimits(double max_translational_dx,
double max_translational_ddx,
double max_translational_dddx,
double max_rotational_dx,
double max_rotational_ddx,
double max_rotational_dddx,
std::array<double, 6> cmd_dx,
std::array<double, 6> O_dP_EE_c,
std::array<double, 6> O_ddP_EE_c,
double delta_t) {
Eigen::Map<Eigen::Matrix<double, 6, 1>> dx(cmd_dx.data());
Eigen::Map<Eigen::Matrix<double, 6, 1>> last_dx(O_dP_EE_c.data());
Eigen::Map<Eigen::Matrix<double, 6, 1>> last_ddx(O_ddP_EE_c.data());
Eigen::Matrix<double, 6, 1> ddx = (dx - last_dx) / delta_t;
Eigen::Matrix<double, 6, 1> dddx = (ddx - last_ddx) / delta_t;
return violatesLimits(dx.head(3).norm(), max_translational_dx) ||
violatesLimits(ddx.head(3).norm(), max_translational_ddx) ||
violatesLimits(dddx.head(3).norm(), max_translational_dddx) ||
violatesLimits(dx.tail(3).norm(), max_rotational_dx) ||
violatesLimits(ddx.tail(3).norm(), max_rotational_ddx) ||
violatesLimits(dddx.tail(3).norm(), max_rotational_dddx);
}
std::array<double, 7> generateValuesIntoLimits(std::array<double, 7> last_cmd_values,
std::array<double, 7> max_derivatives,
double eps,
double delta_t) {
std::array<double, 7> cmd_value{};
for (size_t i = 0; i < 7; i++) {
// Make sure that the integration yields a value into limits
cmd_value[i] = last_cmd_values[i] + (max_derivatives[i] - std::min(std::max(std::abs(eps), 0.0),
2.0 * max_derivatives[i])) *
delta_t;
}
return cmd_value;
}
std::array<double, 7> generateValuesOutsideLimits(std::array<double, 7> last_cmd_values,
std::array<double, 7> max_derivatives,
double eps,
double delta_t) {
std::array<double, 7> cmd_value{};
for (size_t i = 0; i < 7; i++) {
// Make sure that diff yields a value outside limits
cmd_value[i] =
last_cmd_values[i] + (max_derivatives[i] + std::max(std::abs(eps), kLimitEps)) * delta_t;
}
return cmd_value;
}
std::array<double, 6> generateValuesIntoLimits(std::array<double, 6> last_cmd_values,
double max_translational_derivative,
double max_rotational_derivative,
double eps,
double delta_t) {
Eigen::Map<Eigen::Matrix<double, 6, 1>> last_values(last_cmd_values.data());
Eigen::Matrix<double, 6, 1> values;
Eigen::Vector3d unit_vector(1.0, 0.0, 0.0);
std::array<double, 6> result;
values.head(3) << last_values.head(3) + unit_vector *
(max_translational_derivative -
std::min(std::max(std::abs(eps), 0.0),
2.0 * max_translational_derivative)) *
delta_t;
values.tail(3) << last_values.tail(3) + unit_vector *
(max_rotational_derivative -
std::min(std::max(std::abs(eps), 0.0),
2.0 * max_rotational_derivative)) *
delta_t;
Eigen::Matrix<double, 6, 1>::Map(&result[0], 6) = values;
return result;
}
std::array<double, 6> generateValuesOutsideLimits(std::array<double, 6> last_cmd_values,
double max_translational_derivative,
double max_rotational_derivative,
double eps,
double delta_t) {
Eigen::Map<Eigen::Matrix<double, 6, 1>> last_values(last_cmd_values.data());
Eigen::Matrix<double, 6, 1> values;
Eigen::Vector3d unit_vector(1.0, 0.0, 0.0);
std::array<double, 6> result;
values.head(3) << last_values.head(3) +
unit_vector *
(max_translational_derivative + std::max(std::abs(eps), kLimitEps)) *
delta_t;
values.tail(3) << last_values.tail(3) +
unit_vector *
(max_rotational_derivative + std::max(std::abs(eps), kLimitEps)) *
delta_t;
Eigen::Matrix<double, 6, 1>::Map(&result[0], 6) = values;
return result;
}
TEST(RateLimiting, MaxDerivative) {
std::array<double, 7> max_derivatives{{100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0}};
std::array<double, 7> last_cmd_values{{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
double eps{1e-2};
// Desired values are into limits and unchanged after limitRate
std::array<double, 7> values_into_limits =
generateValuesIntoLimits(last_cmd_values, max_derivatives, eps, kDeltaT);
ASSERT_FALSE(violatesRateLimits(max_derivatives, values_into_limits, last_cmd_values, kDeltaT));
EXPECT_EQ(values_into_limits, limitRate(max_derivatives, values_into_limits, last_cmd_values));
// Desired values are outside limits and limited after limitRate
std::array<double, 7> values_outside_limits =
generateValuesOutsideLimits(last_cmd_values, max_derivatives, eps, kDeltaT);
std::array<double, 7> limited_values =
limitRate(max_derivatives, values_outside_limits, last_cmd_values);
ASSERT_TRUE(violatesRateLimits(max_derivatives, values_outside_limits, last_cmd_values, kDeltaT));
EXPECT_NE(values_outside_limits, limited_values);
EXPECT_FALSE(violatesRateLimits(max_derivatives, limited_values, last_cmd_values, kDeltaT));
}
TEST(RateLimiting, JointVelocity) {
std::array<double, 7> last_cmd_velocity{{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
std::array<double, 7> last_cmd_acceleration{{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
std::array<double, 7> max_acceleration{{10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0}};
std::array<double, 7> max_jerk{{100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0}};
double eps{1e-2};
// Desired values are into limits and unchanged after limitRate (jerk)
std::array<double, 7> joint_velocity_into_limits = integrateOneSample<7>(
last_cmd_velocity, generateValuesIntoLimits(last_cmd_acceleration, max_jerk, eps, kDeltaT),
kDeltaT);
ASSERT_FALSE(violatesRateLimits(kJointsNoLimit, kJointsNoLimit, max_jerk,
joint_velocity_into_limits, last_cmd_velocity,
last_cmd_acceleration, kDeltaT));
EXPECT_EQ(joint_velocity_into_limits,
limitRate(kJointsNoLimit, kJointsNoLowerLimit, kJointsNoLimit, max_jerk,
joint_velocity_into_limits, last_cmd_velocity, last_cmd_acceleration));
// Desired values are into limits and unchanged after limitRate (acceleration)
joint_velocity_into_limits =
generateValuesIntoLimits(last_cmd_velocity, max_acceleration, eps, kDeltaT);
ASSERT_FALSE(violatesRateLimits(kJointsNoLimit, max_acceleration, kJointsNoLimit,
joint_velocity_into_limits, last_cmd_velocity,
last_cmd_acceleration, kDeltaT));
EXPECT_EQ(joint_velocity_into_limits,
limitRate(kJointsNoLimit, kJointsNoLowerLimit, max_acceleration, kJointsNoLimit,
joint_velocity_into_limits, last_cmd_velocity, last_cmd_acceleration));
// Desired values are outside limits (jerk violation) and limited after limitRate
std::array<double, 7> joint_velocity_outside_limits = integrateOneSample<7>(
last_cmd_velocity, generateValuesOutsideLimits(last_cmd_acceleration, max_jerk, eps, kDeltaT),
kDeltaT);
std::array<double, 7> limited_joint_velocity =
limitRate(kJointsNoLimit, kJointsNoLowerLimit, kJointsNoLimit, max_jerk,
joint_velocity_outside_limits, last_cmd_velocity, last_cmd_acceleration);
ASSERT_TRUE(violatesRateLimits(kJointsNoLimit, kJointsNoLimit, max_jerk,
joint_velocity_outside_limits, last_cmd_velocity,
last_cmd_acceleration, kDeltaT));
EXPECT_NE(joint_velocity_outside_limits, limited_joint_velocity);
EXPECT_FALSE(violatesRateLimits(kJointsNoLimit, kJointsNoLimit, max_jerk, limited_joint_velocity,
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
// Desired values are outside limits (acceleration violation) and limited after limitRate
joint_velocity_outside_limits =
generateValuesOutsideLimits(last_cmd_velocity, max_acceleration, eps, kDeltaT);
limited_joint_velocity =
limitRate(kJointsNoLimit, kJointsNoLowerLimit, max_acceleration, kJointsNoLimit,
joint_velocity_outside_limits, last_cmd_velocity, last_cmd_acceleration);
ASSERT_TRUE(violatesRateLimits(kJointsNoLimit, max_acceleration, kJointsNoLimit,
joint_velocity_outside_limits, last_cmd_velocity,
last_cmd_acceleration, kDeltaT));
EXPECT_NE(joint_velocity_outside_limits, limited_joint_velocity);
EXPECT_FALSE(violatesRateLimits(kJointsNoLimit, max_acceleration, kJointsNoLimit,
limited_joint_velocity, last_cmd_velocity, last_cmd_acceleration,
kDeltaT));
}
TEST(RateLimiting, JointPosition) {
std::array<double, 7> last_cmd_position{{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
std::array<double, 7> last_cmd_velocity{{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
std::array<double, 7> last_cmd_acceleration{{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
std::array<double, 7> max_acceleration{{10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0}};
std::array<double, 7> max_jerk{{100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0}};
double eps{1e-2};
// Desired values are into limits and unchanged after limitRate (jerk)
std::array<double, 7> joint_position_into_limits = integrateOneSample<7>(
last_cmd_position,
integrateOneSample<7>(last_cmd_velocity,
generateValuesIntoLimits(last_cmd_acceleration, max_jerk, eps, kDeltaT),
kDeltaT),
kDeltaT);
ASSERT_FALSE(violatesRateLimits(
kJointsNoLimit, kJointsNoLimit, max_jerk,
differentiateOneSample<7>(joint_position_into_limits, last_cmd_position, kDeltaT),
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
EXPECT_EQ(joint_position_into_limits,
limitRate(kJointsNoLimit, kJointsNoLowerLimit, kJointsNoLimit, max_jerk,
joint_position_into_limits, last_cmd_position, last_cmd_velocity,
last_cmd_acceleration));
// Desired values are into limits and unchanged after limitRate (acceleration)
joint_position_into_limits = integrateOneSample<7>(
last_cmd_position,
generateValuesIntoLimits(last_cmd_velocity, max_acceleration, eps, kDeltaT), kDeltaT);
ASSERT_FALSE(violatesRateLimits(
kJointsNoLimit, max_acceleration, kJointsNoLimit,
differentiateOneSample<7>(joint_position_into_limits, last_cmd_position, kDeltaT),
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
EXPECT_EQ(joint_position_into_limits,
limitRate(kJointsNoLimit, kJointsNoLowerLimit, max_acceleration, kJointsNoLimit,
joint_position_into_limits, last_cmd_position, last_cmd_velocity,
last_cmd_acceleration));
// Desired values are outside limits (jerk violation) and limited after limitRate
std::array<double, 7> joint_position_outside_limits = integrateOneSample<7>(
last_cmd_position,
integrateOneSample<7>(
last_cmd_velocity,
generateValuesOutsideLimits(last_cmd_acceleration, max_jerk, eps, kDeltaT), kDeltaT),
kDeltaT);
std::array<double, 7> limited_joint_position = limitRate(
kJointsNoLimit, kJointsNoLowerLimit, kJointsNoLimit, max_jerk, joint_position_outside_limits,
last_cmd_position, last_cmd_velocity, last_cmd_acceleration);
ASSERT_TRUE(violatesRateLimits(
kJointsNoLimit, kJointsNoLimit, max_jerk,
differentiateOneSample<7>(joint_position_outside_limits, last_cmd_position, kDeltaT),
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
EXPECT_NE(joint_position_outside_limits, limited_joint_position);
EXPECT_FALSE(violatesRateLimits(
kJointsNoLimit, kJointsNoLimit, max_jerk,
differentiateOneSample<7>(limited_joint_position, last_cmd_position, kDeltaT),
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
// Desired values outside limits (acceleration violation) and limited after limitRate
joint_position_outside_limits = integrateOneSample<7>(
last_cmd_position,
generateValuesOutsideLimits(last_cmd_velocity, max_acceleration, eps, kDeltaT), kDeltaT);
limited_joint_position = limitRate(kJointsNoLimit, kJointsNoLowerLimit, max_acceleration,
kJointsNoLimit, joint_position_outside_limits,
last_cmd_position, last_cmd_velocity, last_cmd_acceleration);
ASSERT_TRUE(violatesRateLimits(
kJointsNoLimit, max_acceleration, kJointsNoLimit,
differentiateOneSample<7>(joint_position_outside_limits, last_cmd_position, kDeltaT),
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
EXPECT_NE(joint_position_outside_limits, limited_joint_position);
EXPECT_FALSE(violatesRateLimits(
kJointsNoLimit, max_acceleration, kJointsNoLimit,
differentiateOneSample<7>(limited_joint_position, last_cmd_position, kDeltaT),
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
}
TEST(RateLimiting, CartesianVelocity) {
std::array<double, 6> last_cmd_velocity{{0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
std::array<double, 6> last_cmd_acceleration{{0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
double max_translational_acceleration{10.0};
double max_translational_jerk{100.0};
double max_rotational_acceleration{5.0};
double max_rotational_jerk{50.0};
double eps{1e-2};
// Desired values are into limits and unchanged after limitRate (rotational and translational
// jerk)
std::array<double, 6> cartesian_velocity_into_limits =
integrateOneSample<6>(last_cmd_velocity,
generateValuesIntoLimits(last_cmd_acceleration, max_translational_jerk,
max_rotational_jerk, eps, kDeltaT),
kDeltaT);
ASSERT_FALSE(violatesRateLimits(kNoLimit, kNoLimit, max_translational_jerk, kNoLimit, kNoLimit,
max_rotational_jerk, cartesian_velocity_into_limits,
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
EXPECT_EQ(
cartesian_velocity_into_limits,
limitRate(kNoLimit, kNoLimit, max_translational_jerk, kNoLimit, kNoLimit, max_rotational_jerk,
cartesian_velocity_into_limits, last_cmd_velocity, last_cmd_acceleration));
// Desired values are into limits and unchanged after limitRate (rotational and translational
// acceleration)
cartesian_velocity_into_limits = generateValuesIntoLimits(
last_cmd_velocity, max_translational_acceleration, max_rotational_acceleration, eps, kDeltaT);
ASSERT_FALSE(violatesRateLimits(
kNoLimit, max_translational_acceleration, kNoLimit, kNoLimit, max_rotational_acceleration,
kNoLimit, cartesian_velocity_into_limits, last_cmd_velocity, last_cmd_acceleration, kDeltaT));
EXPECT_EQ(cartesian_velocity_into_limits,
limitRate(kNoLimit, max_translational_acceleration, kNoLimit, kNoLimit,
max_rotational_acceleration, kNoLimit, cartesian_velocity_into_limits,
last_cmd_velocity, last_cmd_acceleration));
// Desired values are outside limits (rotational and translational jerk violation) and limited
// after limitRate
std::array<double, 6> cartesian_velocity_outside_limits = integrateOneSample<6>(
last_cmd_velocity,
generateValuesOutsideLimits(last_cmd_acceleration, max_translational_jerk,
max_rotational_jerk, eps, kDeltaT),
kDeltaT);
std::array<double, 6> limited_cartesian_velocity =
limitRate(kNoLimit, kNoLimit, max_translational_jerk, kNoLimit, kNoLimit, max_rotational_jerk,
cartesian_velocity_outside_limits, last_cmd_velocity, last_cmd_acceleration);
ASSERT_TRUE(violatesRateLimits(kNoLimit, kNoLimit, max_translational_jerk, kNoLimit, kNoLimit,
max_rotational_jerk, cartesian_velocity_outside_limits,
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
EXPECT_NE(cartesian_velocity_outside_limits, limited_cartesian_velocity);
EXPECT_FALSE(violatesRateLimits(kNoLimit, kNoLimit, max_translational_jerk, kNoLimit, kNoLimit,
max_rotational_jerk, limited_cartesian_velocity,
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
// Desired values are outside limits (rotational and translational acceleration violation) and
// limited after limitRate
cartesian_velocity_outside_limits = generateValuesOutsideLimits(
last_cmd_velocity, max_translational_acceleration, max_rotational_acceleration, eps, kDeltaT);
limited_cartesian_velocity = limitRate(
kNoLimit, max_translational_acceleration, kNoLimit, kNoLimit, max_rotational_acceleration,
kNoLimit, cartesian_velocity_outside_limits, last_cmd_velocity, last_cmd_acceleration);
ASSERT_TRUE(violatesRateLimits(kNoLimit, max_translational_acceleration, kNoLimit, kNoLimit,
max_rotational_acceleration, kNoLimit,
cartesian_velocity_outside_limits, last_cmd_velocity,
last_cmd_acceleration, kDeltaT));
EXPECT_NE(cartesian_velocity_outside_limits, limited_cartesian_velocity);
EXPECT_FALSE(violatesRateLimits(kNoLimit, max_translational_acceleration, kNoLimit, kNoLimit,
max_rotational_acceleration, kNoLimit, limited_cartesian_velocity,
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
}
TEST(RateLimiting, CartesianPose) {
std::array<double, 16> last_cmd_pose{
{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0}};
std::array<double, 6> last_cmd_velocity{{0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
std::array<double, 6> last_cmd_acceleration{{0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
double max_translational_acceleration{10.0};
double max_translational_jerk{100.0};
double max_rotational_acceleration{5.0};
double max_rotational_jerk{50.0};
double eps{1e-2};
// Desired values are into limits and unchanged after limitRate (rotational and translational
// jerk)
std::array<double, 16> cartesian_pose_into_limits = integrateOneSample(
last_cmd_pose,
integrateOneSample<6>(last_cmd_velocity,
generateValuesIntoLimits(last_cmd_acceleration, max_translational_jerk,
max_rotational_jerk, eps, kDeltaT),
kDeltaT),
kDeltaT);
ASSERT_FALSE(violatesRateLimits(
kNoLimit, kNoLimit, max_translational_jerk, kNoLimit, kNoLimit, max_rotational_jerk,
differentiateOneSample(cartesian_pose_into_limits, last_cmd_pose, kDeltaT), last_cmd_velocity,
last_cmd_acceleration, kDeltaT));
std::array<double, 16> cartesian_pose_limited = limitRate(
kNoLimit, kNoLimit, max_translational_jerk, kNoLimit, kNoLimit, max_rotational_jerk,
cartesian_pose_into_limits, last_cmd_pose, last_cmd_velocity, last_cmd_acceleration);
for (size_t i = 0; i < cartesian_pose_into_limits.size(); i++) {
EXPECT_NEAR(cartesian_pose_into_limits[i], cartesian_pose_limited[i], 1e-6);
}
// Desired values are into limits and unchanged after limitRate (rotational and translational
// acceleration)
cartesian_pose_into_limits =
integrateOneSample(last_cmd_pose,
generateValuesIntoLimits(last_cmd_velocity, max_translational_acceleration,
max_rotational_acceleration, eps, kDeltaT),
kDeltaT);
ASSERT_FALSE(violatesRateLimits(
kNoLimit, max_translational_acceleration, kNoLimit, kNoLimit, max_rotational_acceleration,
kNoLimit, differentiateOneSample(cartesian_pose_into_limits, last_cmd_pose, kDeltaT),
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
cartesian_pose_limited =
limitRate(kNoLimit, max_translational_acceleration, kNoLimit, kNoLimit,
max_rotational_acceleration, kNoLimit, cartesian_pose_into_limits, last_cmd_pose,
last_cmd_velocity, last_cmd_acceleration);
for (size_t i = 0; i < cartesian_pose_into_limits.size(); i++) {
EXPECT_NEAR(cartesian_pose_into_limits[i], cartesian_pose_limited[i], 1e-6);
}
// Desired values are outside limits (rotational and translational jerk violation) and limited
// after limitRate
std::array<double, 16> cartesian_pose_outside_limits = integrateOneSample(
last_cmd_pose,
integrateOneSample<6>(
last_cmd_velocity,
generateValuesOutsideLimits(last_cmd_acceleration, max_translational_jerk,
max_rotational_jerk, eps, kDeltaT),
kDeltaT),
kDeltaT);
std::array<double, 16> limited_cartesian_pose = limitRate(
kNoLimit, kNoLimit, max_translational_jerk, kNoLimit, kNoLimit, max_rotational_jerk,
cartesian_pose_outside_limits, last_cmd_pose, last_cmd_velocity, last_cmd_acceleration);
ASSERT_TRUE(violatesRateLimits(
kNoLimit, kNoLimit, max_translational_jerk, kNoLimit, kNoLimit, max_rotational_jerk,
differentiateOneSample(cartesian_pose_outside_limits, last_cmd_pose, kDeltaT),
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
EXPECT_NE(cartesian_pose_outside_limits, limited_cartesian_pose);
EXPECT_FALSE(violatesRateLimits(
kNoLimit, kNoLimit, max_translational_jerk, kNoLimit, kNoLimit, max_rotational_jerk,
differentiateOneSample(limited_cartesian_pose, last_cmd_pose, kDeltaT), last_cmd_velocity,
last_cmd_acceleration, kDeltaT));
// Desired values are outside limits (rotational and translational acceleration violation) and
// limited after limitRate
cartesian_pose_outside_limits = integrateOneSample(
last_cmd_pose,
generateValuesOutsideLimits(last_cmd_velocity, max_translational_acceleration,
max_rotational_acceleration, eps, kDeltaT),
kDeltaT);
limited_cartesian_pose =
limitRate(kNoLimit, max_translational_acceleration, kNoLimit, kNoLimit,
max_rotational_acceleration, kNoLimit, cartesian_pose_outside_limits, last_cmd_pose,
last_cmd_velocity, last_cmd_acceleration);
ASSERT_TRUE(violatesRateLimits(
kNoLimit, max_translational_acceleration, kNoLimit, kNoLimit, max_rotational_acceleration,
kNoLimit, differentiateOneSample(cartesian_pose_outside_limits, last_cmd_pose, kDeltaT),
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
EXPECT_NE(cartesian_pose_outside_limits, limited_cartesian_pose);
EXPECT_FALSE(violatesRateLimits(
kNoLimit, max_translational_acceleration, kNoLimit, kNoLimit, max_rotational_acceleration,
kNoLimit, differentiateOneSample(limited_cartesian_pose, last_cmd_pose, kDeltaT),
last_cmd_velocity, last_cmd_acceleration, kDeltaT));
}
TEST(RateLimiting, CartesianPoseIntegrationAndDifferentation) {
std::array<double, 16> last_cmd_pose{
{0.0, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0}};
std::array<double, 6> last_cmd_velocity{{1.0, 2.0, 3.0, 0.4, 0.5, 0.3}};
std::array<double, 16> cartesian_pose =
integrateOneSample(last_cmd_pose, last_cmd_velocity, kDeltaT);
auto twist = differentiateOneSample(cartesian_pose, last_cmd_pose, kDeltaT);
for (int i = 0; i < 6; ++i) {
ASSERT_NEAR(twist[i], last_cmd_velocity[i], 1e-6);
}
}
TEST(RateLimiting, PositionBasedVelocityLimitBoundaryCheck) {
{
const std::array<double, 7> q_lower_limits = {-2.9007, -1.8361, -2.9007, -3.0770,
-2.8763, 0.4398, -3.0508};
const std::array<double, 7> dq_upper_limits = {2.62, 2.62, 2.62, 2.62, 5.26, 4.18, 5.26};
const std::array<double, 7> dq_lower_limits = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
auto dq_max = computeUpperLimitsJointVelocity(q_lower_limits);
EXPECT_LT(dq_max, dq_upper_limits);
auto dq_min = computeLowerLimitsJointVelocity(q_lower_limits);
EXPECT_GT(dq_min, dq_lower_limits);
EXPECT_GT(dq_max, dq_min);
}
{
const std::array<double, 7> q_upper_limits = {2.9007, 1.8361, 2.9007, -0.1169,
2.8763, 4.6216, 3.0508};
const std::array<double, 7> dq_upper_limits = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
const std::array<double, 7> dq_lower_limits = {-2.62, -2.62, -2.62, -2.62, -5.26, -4.18, -5.26};
auto dq_max = computeUpperLimitsJointVelocity(q_upper_limits);
EXPECT_LT(dq_max, dq_upper_limits);
auto dq_min = computeLowerLimitsJointVelocity(q_upper_limits);
EXPECT_GT(dq_min, dq_lower_limits);
EXPECT_GT(dq_max, dq_min);
}
}