Skip to content

Commit 2454560

Browse files
committed
pbio/trajectory: Fix negative duration.
Fix incorrect acceleration for rounded speed values, causing negative duration. Also assert more time values for easier debugging. See pybricks/support#786
1 parent e0e5dd4 commit 2454560

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/pbio/src/trajectory.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ static pbio_error_t pbio_trajectory_new_forward_angle_command(pbio_trajectory_t
450450
trj->th1 = intersect_ramp(trj->th3, thf, trj->a0, trj->a2);
451451
trj->th2 = trj->th1;
452452
trj->w1 = bind_w0(0, trj->a0, trj->th1 - thf);
453+
454+
// If w0 and w1 are very close, the previously determined
455+
// acceleration sign may be wrong after rounding errors, so update.
456+
trj->a0 = trj->w0 < trj->w1 ? accel : -accel;
453457
}
454458
}
455459

@@ -466,6 +470,13 @@ static pbio_error_t pbio_trajectory_new_forward_angle_command(pbio_trajectory_t
466470
if (trj->th2 < trj->th1 || trj->th3 < trj->th2) {
467471
return PBIO_ERROR_FAILED;
468472
}
473+
474+
// Assert times are valid.
475+
assert_time(trj->t1);
476+
assert_time(trj->t2);
477+
assert_time(trj->t3);
478+
assert_time(trj->t2 - trj->t1);
479+
assert_time(trj->t3 - trj->t2);
469480
if (trj->t1 < 0 || trj->t2 - trj->t1 < 0 || trj->t3 - trj->t2 < 0) {
470481
return PBIO_ERROR_FAILED;
471482
}
@@ -652,6 +663,7 @@ void pbio_trajectory_get_reference(pbio_trajectory_t *trj, uint32_t time_ref, pb
652663

653664
// Time within maneuver since start.
654665
int32_t time = TO_TRAJECTORY_TIME(time_ref - trj->start.time);
666+
assert_time(time);
655667

656668
// Get angle, speed, and acceleration along reference
657669
int32_t th, w, a;

0 commit comments

Comments
 (0)