Code to estimate and simulate the trajectory of a ball undergoing projectile motion.
Trajectory is modeled using linear drag (see Wiki)
The estimator in misc/estimate_ball_trajectory.py assumes the ball follows a linear-drag model:
- Velocity:
dv/dt = g - β v - Position:
dp/dt = v
For a time offset Δt = t - t₀, the closed-form solution becomes:
p(t) = p₀ + A(Δt, β) v₀ + B(Δt, β) gv(t) = exp(-β Δt) v₀ + A(Δt, β) gA = (1 - exp(-β Δt)) / βB = Δt/β - (1 - exp(-β Δt)) / β²
When β → 0, the model smoothly reduces to the standard ballistic solution (A → Δt, B → 0.5 Δt²).
The fit_trajectory method of the BallTrajectoryEstimator class in misc/estimate_ball_trajectory.py fits the parameters p₀, v₀, and drag β using timestamped position measurements (Time, TX, TY, TZ). The implemented method is:
- Data validation: requires at least N recent samples with finite values and sufficient upward
Zvelocity before fitting. - Solve for β = 0 baseline: computes a no-drag solution for comparison.
- Optimize drag coefficient: performs a 1D search over
β(bounded betweenbeta_minandbeta_max) with SciPy'sminimize_scalar. - Variable projection for
p₀andv₀: for each candidateβ, solves the linear systemp = p₀ + A v₀ + B gvia least squares to obtain the bestp₀andv₀. - Select best fit: keeps the
(p₀, v₀, β)combination with the lowest sum of squared residuals and stores it for prediction.
Predicted positions use the stored parameters and the same closed-form equations to evaluate p(t) for any desired timestamps.
- Included is
BallTrajectoryEstimator_KalmanFilterinmisc/estimate_ball_trajectory.py, that estimates trajectories using a Linear Kalman Filter and a gravity only model.