Tech Toolbox
  • Please Visit https://ftc-tech-toolbox.vercel.app/ for the new tech toolbox!!
    • Introduction
    • Choosing an IDE
    • Creating an OpMode
    • Motors and Encoders
    • Servos
    • Gamepad Controls
    • Drive Systems
    • Lynx Modules
    • Telemetry
    • Wireless Download
    • The Sleep Command
  • Please Visit the New Link
    • Tank Drive / Skid Steer (Part 1)
    • Mecanum Drive (Part 1)
    • Turrets
    • Linear Slides
    • Kicker
    • Active Intake / Sweepers
    • Flywheels / Shooters
  • Please Visit the new Link
    • Base Class (Step 1)
    • Module Classes (Step 2)
    • OpMode Classes (Step 3)
  • This domain is now depreciated and is no longer updated!
  • This domain is now depreciated and is no longer updated!
    • What is Localization?
    • General Odometry Logic
    • Tank (No Deadwheels)
    • Mecanum (No Deadwheels)
    • Deadwheel Odometry (Mecanum and Tank)
    • VSLAM
  • This domain is now depreciated and is no longer updated!
    • What is Control Theory?
    • Custom PID Loops
    • Essential Control Theory Concepts
    • Resources for Learning Advanced Control Theory
  • This domain is now depreciated and is no longer updated! Please visit this domain for the new TT!
    • Introduction
    • Mecanum Drive (Part 2)
    • Tank Drive (Part 2)
    • Introduction to Pure Pursuit
    • Pure Pursuit: Mecanum
    • Pure Pursuit: Tank
    • Advanced Pure Pursuit
    • Guided Vector Fields
    • Autonomous Movement Libraries
  • Sensors
    • IMU
    • Color Sensors
      • Advanced Sensing Code
    • Distance Sensors
    • Touch Sensor
  • Computer Vision
    • Setting up Camera/Intro to Computer Vision Tools
      • Intro to OpenCV
      • Vuforia
    • Streaming Frames
    • Color Thresholding
    • April Tags
    • Linear Regression
    • Machine Learning Toolchain
    • Object Distance Estimation
    • Object Tracking / Driving to an Object
    • Computer Vision Simulators
  • Simulators
    • Beta8397 Simulator
    • VRS
  • Cool Codebases
Powered by GitBook
On this page
  • Least Squares Regression Line
  • Correlation Coefficient
  • Slope
  • Online LSRL Calculators
  • Use Case: Setting Shooter Powers Via Linear Regression:
  1. Computer Vision

Linear Regression

Using the Least Squares Regression Line

PreviousApril TagsNextMachine Learning Toolchain

Last updated 1 year ago

Oftentimes when looking to detect an object, a linear regression model is used to estimate the position of the object away from the robot.

Least Squares Regression Line

Linear Regression uses the Least Squares Regression line to estimate the pixel offsets of an object and attribute it to how far the robot is from the object. This is found by collecting multiple points and creating a regression line for the data.

A LSRL gives a predicted y value based on the input x value.

This is the formula for a Linear Regression line, where y hat is the predicted value, x is the input, a is the y intercept, and beta is the slope.

Correlation Coefficient

The correlation coefficient, denoted r, shows the degree of correlation between the points on the plot, a higher correlation coefficient means the Linear Regression Line is more accurate.

Slope

The slope is the b or beta value in the linear regression equation, it can be calculated once the correlation coefficient, r, has been found.

Note

The x mean and the y mean are always on the Linear Regression Line, therefore we can derive

Since we have the slope, simply plugging in the y bar and the x bar will give you your y-intercept, or a value. This will give you the final Least Squares Regression Line Equation.

Online LSRL Calculators

If you do not want to do all the math in your code, there are online calculators that you can use do form your linear regression line for you.

You would first measure an object at multiple different points, measuring the input and the output that you are looking for. Such as distance in inches, and pixel displacement. Then you would put all of those values into a online calculator which would generate the LSRL for you.

Use Case: Setting Shooter Powers Via Linear Regression:

When attempting to shoot an object at a certain distance away from a target, knowing what power to set can often be a mystery without performing intensive calculations. However, by making use of linear regression, we can easily calculate the correct power that one must apply to the flywheel motor to hit the target.

Note that in order to attain a distance reading, you must have an accurate localization system that returns the robot's current position. You must also know the position of the target from the robot's origin point. From there, calculate the x and y displacement of the robot and simply apply the Pythagorean Theorem or Distance Formula to find the distance.

double distance = getDistance(); //replace getDistancce() with the robot's distance to a target
double yInt = 0.5; //a value in LSRL equation
double slope = 2; //b value in LSRL equation

double bx = slope * distance;
double power = bx + yInt;

shooter.setPower(power);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

Here, you would put the shooter at different distances and determine the power needed to get the object in the goal, then plug that data into a linear regression calculator to get yInt and slope.

y^=a+β(x)\hat{y} = a + \beta(x)y^​=a+β(x)
r=1n−1Σ(xi−xˉsx)(yi−yˉsy)r = \frac{1}{n-1}\Sigma(\frac{x_{i} - \bar{x}}{s_{x}})(\frac{y_{i} - \bar{y}}{s_{y}})r=n−11​Σ(sx​xi​−xˉ​)(sy​yi​−yˉ​​)
β=rsysx\beta = r\frac{s_{y}}{s_{x}}β=rsx​sy​​
yˉ=a+βxˉ\bar{y} = a + \beta\bar{x}yˉ​=a+βxˉ
Linear regression calculator
Logo
Graph of Least Squares Regression Line(LSRL)
Drawing
Drawing