Trajectory Prediction - Linear Regression

Hi,

If I know the coordinate of the puck within a period of time, I can calculate the change of the puck's y-coordinate in respect to its x-coordinate. Plotting multiple puck's positions in a graph shows the puck's direction of travel and most importantly, I can estimate a future position using the line of best fit, also called regression line.

Below is an example of how I can predict the trajectory of the puck using equation: Y = mX + b

Where;
  • Y = mean of all y-coordinates
  • X = mean of all x-coordinates
  • m = slope of the line
  • b = intercept point of line in the Y-axis


Applying the knowledge gained of calculating linear regression using the least-square method and wonderful tutorials online, I have created a function to calculate m and b to predict a y-coordinate given an x-coordinate.


Video of softbots playing with each other using two different methods

The video below shows the paddles moving autonomously. Note that the paddle on the left is moving by following the y-coordinate of the ball, as described in the last post. Whilst, the paddle on the right is using linear regression to move at a calculated position.

Code explained:

1. Calculate the x- and y-coordinate of the ball
2. Stores coordinates in separate lists
3. If puck's x-coordinate passes a specified x-coordinate, pass lists to function to calculate m and b.
4. Use calculated values to predict the y-coordinate of the puck at a specified x-coordinate and move to position.

Note: if puck hits the border, lists are cleared.

Immanuel

Comments

Popular posts from this blog

Introduction

Test - Relationship between Accuracy and Angle