Skip to content

Commit 6fa81f5

Browse files
committed
pbio/test/trajectory: Check speed and position change.
1 parent 165808b commit 6fa81f5

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

lib/pbio/test/src/trajectory.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ static void walk_trajectory(pbio_trajectory_t *trj) {
8888
}
8989

9090
// Loop over all trajectory points and assert results.
91-
for (uint32_t t = 1; t < duration; t += 500) {
91+
const uint32_t increment = 50;
92+
for (uint32_t t = increment; t < duration; t += increment) {
9293

9394
// Get current reference.
9495
uint32_t now = t + time_start;
@@ -97,11 +98,33 @@ static void walk_trajectory(pbio_trajectory_t *trj) {
9798
// Current time should match
9899
tt_want_int_op(ref_now.time, ==, now);
99100

101+
// Check movement between samples.
102+
int32_t movement = pbio_angle_diff_mdeg(&ref_now.position, &ref_prev.position);
103+
int32_t movement_expected = (ref_now.speed + ref_prev.speed) / 2 * (increment / 10000.0f);
104+
int32_t movement_threshold = ref_now.speed == ref_prev.speed ? 1000 : 5000;
105+
tt_want(pbio_math_abs(movement - movement_expected) < movement_threshold);
106+
107+
// Check speed change between samples, but only within time segments.
108+
// To find out, compare starting point of segments of both samples.
109+
pbio_trajectory_reference_t last_vertex_now;
110+
pbio_trajectory_get_last_vertex(trj, now, &last_vertex_now);
111+
112+
pbio_trajectory_reference_t last_vertex_prev;
113+
pbio_trajectory_get_last_vertex(trj, now - increment, &last_vertex_prev);
114+
115+
// Now we can compare the speeds.
116+
if (ref_now.acceleration == ref_prev.acceleration &&
117+
last_vertex_now.time == last_vertex_prev.time) {
118+
int32_t delta = ref_now.speed - ref_prev.speed;
119+
int32_t delta_expected = ref_now.acceleration * (increment / 10000.0f);
120+
tt_want(pbio_math_abs(delta - delta_expected) < 3000);
121+
}
122+
100123
bool same_speed_dir = pbio_math_sign(ref_now.speed) == pbio_math_sign(ref_prev.speed);
101124
bool same_accel_dir = pbio_math_sign(ref_now.acceleration) == pbio_math_sign(ref_prev.acceleration);
102125

103126
// If the speed and acceleration direction was the same between two
104-
// samples, we can test that the position increment is correct.
127+
// samples, we can test that the position increment direction is correct.
105128
if (same_speed_dir && same_accel_dir) {
106129

107130
// Position increment should match speed direction.

0 commit comments

Comments
 (0)