Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"/home/dev-user/workspace/src/components/plan/rrt",
"/home/dev-user/workspace/src/components/plan/rrt_star",
"/home/dev-user/workspace/src/components/plan/rrt_star_bidirectional",
"/home/dev-user/workspace/src/components/prediction",
"/home/dev-user/workspace/src/components/search/kd_tree",
"/home/dev-user/workspace/src/components/sensors",
"/home/dev-user/workspace/src/components/sensors/gnss",
Expand Down Expand Up @@ -119,6 +120,7 @@
"/home/dev-user/workspace/src/simulations/path_tracking/pure_pursuit_path_tracking",
"/home/dev-user/workspace/src/simulations/path_tracking/rear_wheel_feedback_tracking",
"/home/dev-user/workspace/src/simulations/path_tracking/stanley_path_tracking",
"/home/dev-user/workspace/src/simulations/perception/cv_ctrv_motion_prediction",
"/home/dev-user/workspace/src/simulations/perception/lidar_obstacle_sensing",
"/home/dev-user/workspace/src/simulations/perception/point_cloud_rectangle_fitting",
"/home/dev-user/workspace/src/simulations/perception/point_cloud_search",
Expand Down
4 changes: 4 additions & 0 deletions doc/object_tracking/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ This chapter is planned. This page lists what it will cover and the simulations

Track the detected objects over time and estimate how they move ([#116](https://github.com/ShisatoYano/AutonomousVehicleControlBeginnersGuide/issues/116)).

## Sections

- 11.1 CV vs CTRV motion prediction: [`cv_ctrv_motion_prediction`](/src/simulations/perception/cv_ctrv_motion_prediction)

## Before this chapter

[Chapter 6](/doc/localization/index.md), [Chapter 9](/doc/perception/index.md).
2 changes: 2 additions & 0 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"src/components/plan/rrt",
"src/components/plan/rrt_star",
"src/components/plan/rrt_star_bidirectional",
"src/components/prediction",
"src/components/search/kd_tree",
"src/components/sensors",
"src/components/sensors/gnss",
Expand Down Expand Up @@ -84,6 +85,7 @@
"src/simulations/path_tracking/pure_pursuit_path_tracking",
"src/simulations/path_tracking/rear_wheel_feedback_tracking",
"src/simulations/path_tracking/stanley_path_tracking",
"src/simulations/perception/cv_ctrv_motion_prediction",
"src/simulations/perception/lidar_obstacle_sensing",
"src/simulations/perception/point_cloud_rectangle_fitting",
"src/simulations/perception/point_cloud_search",
Expand Down
126 changes: 126 additions & 0 deletions src/components/prediction/motion_prediction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
"""
motion_prediction.py

Author: Lenin Mookiah
"""

import numpy as np
import numpy.typing as npt


YAW_RATE_EPSILON_RPS = 1.0e-6


def _prediction_times(horizon_s: float, step_s: float) -> npt.NDArray[np.float64]:
"""Return evenly spaced times ending exactly at ``horizon_s``.

Both arguments must be finite and positive, and ``horizon_s`` must be a
whole multiple of ``step_s`` within a relative tolerance of ``1e-9``.
The return shape is ``(N,)``, where ``N = horizon_s / step_s``, with values
``step_s, 2 * step_s, ..., horizon_s``.
"""

if not np.isfinite(horizon_s):
raise ValueError("horizon_s must be finite")
if not np.isfinite(step_s):
raise ValueError("step_s must be finite")
if horizon_s <= 0.0:
raise ValueError("horizon_s must be greater than 0")
if step_s <= 0.0:
raise ValueError("step_s must be greater than 0")
ratio = horizon_s / step_s
rounded_ratio = round(ratio)
if rounded_ratio < 1 or not np.isclose(
ratio, rounded_ratio, rtol=1.0e-9, atol=0.0
):
raise ValueError("step_s must divide horizon_s and be <= horizon_s")

number_steps = int(rounded_ratio)
times_s = np.arange(1, number_steps + 1, dtype=np.float64) * step_s
times_s[-1] = horizon_s
return times_s


def predict_cv(
x_m: float,
y_m: float,
yaw_rad: float,
speed_mps: float,
horizon_s: float,
step_s: float,
) -> tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]:
"""Predict positions with the constant velocity (CV) model.

The model keeps speed and yaw constant:

x(t) = x_m + speed_mps * cos(yaw_rad) * t
y(t) = y_m + speed_mps * sin(yaw_rad) * t

The returned x and y arrays both have shape ``(N,)``, where
``N = round(horizon_s / step_s)``. ``horizon_s`` must be a whole multiple
of ``step_s`` within a relative tolerance of ``1e-9``. The arrays contain
predictions at ``step_s, 2 * step_s, ..., horizon_s``, with the last time
set exactly to ``horizon_s``; the current position at ``t=0`` is not
included. Position is in meters, yaw in radians, speed in meters per
second, and time in seconds.

Prediction uncertainty, including propagation of track covariance, is
outside the scope of this model.
Position, yaw, and speed are not checked; NaN gives NaN predictions.
"""

times_s = _prediction_times(horizon_s, step_s)
predicted_x_m = x_m + speed_mps * np.cos(yaw_rad) * times_s
predicted_y_m = y_m + speed_mps * np.sin(yaw_rad) * times_s
return predicted_x_m, predicted_y_m


def predict_ctrv(
x_m: float,
y_m: float,
yaw_rad: float,
speed_mps: float,
yaw_rate_rps: float,
horizon_s: float,
step_s: float,
) -> tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]:
"""Predict positions with the constant turn rate and velocity model.

For a nonzero yaw rate ``omega = yaw_rate_rps``, the CTRV equations are:

x(t) = x_m + (speed_mps / omega)
* (sin(yaw_rad + omega * t) - sin(yaw_rad))
y(t) = y_m + (speed_mps / omega)
* (cos(yaw_rad) - cos(yaw_rad + omega * t))

Positive yaw rate means counter-clockwise turning. When
``abs(yaw_rate_rps) < YAW_RATE_EPSILON_RPS``, the equations can lose
numerical precision through division by a very small number, so the
function falls back to the CV model.

The returned x and y arrays both have shape ``(N,)``, where
``N = round(horizon_s / step_s)``. ``horizon_s`` must be a whole multiple
of ``step_s`` within a relative tolerance of ``1e-9``. The arrays contain
predictions at ``step_s, 2 * step_s, ..., horizon_s``, with the last time
set exactly to ``horizon_s``; the current position at ``t=0`` is not
included. Position is in meters, yaw in radians, speed in meters per
second, yaw rate in radians per second, and time in seconds.

Prediction uncertainty, including propagation of track covariance, is
outside the scope of this model.
Position, yaw, speed, and yaw rate are not checked; NaN gives NaN predictions.
"""

if abs(yaw_rate_rps) < YAW_RATE_EPSILON_RPS:
return predict_cv(x_m, y_m, yaw_rad, speed_mps, horizon_s, step_s)

times_s = _prediction_times(horizon_s, step_s)
future_yaw_rad = yaw_rad + yaw_rate_rps * times_s
turning_radius_m = speed_mps / yaw_rate_rps
predicted_x_m = x_m + turning_radius_m * (
np.sin(future_yaw_rad) - np.sin(yaw_rad)
)
predicted_y_m = y_m + turning_radius_m * (
np.cos(yaw_rad) - np.cos(future_yaw_rad)
)
return predicted_x_m, predicted_y_m
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading