Skip to content

Add CV and CTRV motion prediction of other vehicles - #475

Open
leninworld wants to merge 1 commit into
ShisatoYano:mainfrom
leninworld:feat/cv-ctrv-motion-prediction-432
Open

leninworld wants to merge 1 commit into
ShisatoYano:mainfrom
leninworld:feat/cv-ctrv-motion-prediction-432

Conversation

@leninworld

Copy link
Copy Markdown
Contributor

This PR fixes issue #432.

Summary

Adds physics-based motion prediction of other vehicles for Chapter 11 with two models, constant velocity (CV) and constant turn rate and velocity (CTRV), and a simulation that compares them with where three moving obstacles really go.

  • src/components/prediction/motion_prediction.py:
    • predict_cv(x_m, y_m, yaw_rad, speed_mps, horizon_s, step_s): x(t) = x + v cos(θ) t, y(t) = y + v sin(θ) t.
    • predict_ctrv(x_m, y_m, yaw_rad, speed_mps, yaw_rate_rps, horizon_s, step_s): x(t) = x + (v/ω)(sin(θ + ωt) - sin θ), y(t) = y + (v/ω)(cos θ - cos(θ + ωt)). For |ω| < 1e-6 rad/s it uses CV, because dividing by a very small ω loses precision. Positive yaw rate turns counter-clockwise.
    • Both return the predicted x and y as arrays of shape (N,) at step_s, 2 step_s, ..., horizon_s (N = horizon_s / step_s; the current position at t = 0 is not included). A non-finite or non-positive time, or a horizon that isn't a whole multiple of the step, raises ValueError. The docstrings note that prediction uncertainty (the track covariance) is out of scope.
  • src/simulations/perception/cv_ctrv_motion_prediction/cv_ctrv_motion_prediction.py: three Obstacles at 3 m/s in separate parts of the plot, using their true states (obstacle.state, obstacle.yaw_rate_rps), so it doesn't depend on the tracker of Multi-object tracking with a Kalman filter and nearest neighbor data association #379:
    • Straight: yaw rate 0.
    • Constant turn: yaw rate 0.45 rad/s.
    • Stops turning: turns left at π/8 rad/s until its heading reaches 90° (at 4.0 s, half of the 8 s run), then the script sets its yaw_rate_rps to 0. The stop is decided from the heading, not from a time counter, so it happens at the same place in the animation and in the error calculation.
    • At every step the 3 s CV (dashed) and CTRV (solid) predictions are drawn from each obstacle, with its travelled path in a light colour. Supports --save-gif; the GIF is committed next to the script. Running the script prints the error table below.
  • test/test_motion_prediction.py: CV worked out by hand; CTRV with ω = 0 equals CV; CTRV with ω ≠ 0 stays on the circle of radius v/ω around its centre; a tiny ω matches CV; output shape and times (30 steps of 0.1 s end at 3.0 s; 0.1 + 0.2 against 0.3 gives one step); the ValueError cases (zero/negative, non-multiple, inf/NaN).
  • test/test_cv_ctrv_motion_prediction.py: runs main() with show_plot = False and checks the error comparison below.
  • doc/object_tracking/index.md: a ## Sections list with "11.1 CV vs CTRV motion prediction" (doc/WRITING_GUIDE.md names comparison sections "A vs B"). Please change the number if you want it elsewhere.
  • pyrightconfig.json / .devcontainer/devcontainer.json regenerated with generate_pyrightconfig.py.

Closes #432

Comparing CV and CTRV

Predictions made at t0 = 3.5 s from each obstacle's true state, compared with its true position 1, 2 and 3 s later (0.1 s steps):

Obstacle Model 1 s [m] 2 s [m] 3 s [m]
Straight CV 0.00 0.00 0.00
Straight CTRV 0.00 0.00 0.00
Constant turn CV 0.60 2.51 5.59
Constant turn CTRV 0.07 0.13 0.19
Stops turning CV 0.41 1.00 1.59
Stops turning CTRV 0.18 1.34 3.61

CV and CTRV are the same on the straight road; CTRV wins in the constant turn; for the obstacle that stops turning at 4.0 s, CTRV is better at 1 s but keeps turning afterwards and overshoots, so CV wins at 2 and 3 s. The small CTRV error in the constant turn comes from the simulation's vehicle model (State keeps the heading constant during each 0.1 s step), not from the prediction.

How I checked it works

  • pytest -q test/test_motion_prediction.py test/test_cv_ctrv_motion_prediction.py test/test_book_chapters.py test/test_generate_example_gallery.py: 24 passed (Python 3.12). The two new test files fail on main (module not found).
  • Full suite pytest -q: 315 passed.
  • Ran the simulation with --save-gif and looked at the frames at 3.5 s, 4.2 s and 8.0 s: the three obstacles and their predictions stay apart and inside the plot, and CTRV follows the constant turn. At 3.5 s the third obstacle's CTRV prediction still curves left, although the obstacle goes straight from 4.0 s on; from 4.2 s its yaw rate is 0 and both predictions lie on its straight path.
  • doc/EXAMPLES.md is unchanged (the gallery workflow regenerates it after merge).

Images

CV vs CTRV motion prediction

The GIF is also in src/simulations/perception/cv_ctrv_motion_prediction/, so the gallery picks it up.

Book

  • This PR doesn't change code that the book describes, or I updated those pages

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Motion prediction of other vehicles with constant velocity (CV) and constant turn rate (CTRV) models

1 participant