Skip to content

Commit 5b3dd3d

Browse files
nycratwilliamckhapre-commit-ci-lite[bot]
authored
Fix ball filter linear regression relative error bug (UBC-Thunderbots#3538)
* Fix ball filter linear regression using abs error * Add comments that regression error is RMSE * Fix typo * Fix pass defender check for kick * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: williamckha <williamha@outlook.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent a36cb87 commit 5b3dd3d

3 files changed

Lines changed: 17 additions & 15 deletions

File tree

src/software/ai/hl/stp/tactic/pass_defender/pass_defender_fsm.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "software/ai/evaluation/intercept.h"
55
#include "software/ai/hl/stp/tactic/move_primitive.h"
66
#include "software/geom/algorithms/closest_point.h"
7+
#include "software/geom/algorithms/contains.h"
78

89
PassDefenderFSM::PassDefenderFSM(
910
std::shared_ptr<const TbotsProto::AiConfig> ai_config_ptr)
@@ -18,6 +19,12 @@ bool PassDefenderFSM::passStarted(const Update& event)
1819
event.control_params.position_to_block_from.x() - ball_position.x(),
1920
event.control_params.position_to_block_from.y() - ball_position.y());
2021

22+
// Make sure ball is within playing area
23+
if (!contains(event.common.world_ptr->field().fieldLines(), ball_position))
24+
{
25+
return false;
26+
}
27+
2128
bool pass_started = event.common.world_ptr->ball().hasBallBeenKicked(
2229
ball_receiver_point_vector.orientation(), MIN_PASS_SPEED,
2330
MAX_PASS_ANGLE_DIFFERENCE);

src/software/sensor_fusion/filter/ball_filter.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <Eigen/Dense>
44
#include <algorithm>
5-
#include <limits>
65
#include <vector>
76

87
#include "shared/constants.h"
@@ -264,17 +263,9 @@ BallFilter::LinearRegressionResults BallFilter::calculateLinearRegression(
264263
A.bdcSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);
265264
// How to calculate the error is from
266265
// https://eigen.tuxfamily.org/dox/group__TutorialLinearAlgebra.html
267-
double regression_error = std::numeric_limits<double>::max();
268-
269-
if ((A * regression_vector - b).norm() == 0 && b.norm() == 0)
270-
{
271-
regression_error = 0;
272-
}
273-
if (b.norm() != 0)
274-
{
275-
regression_error =
276-
(A * regression_vector - b).norm() / (b.norm()); // norm() is L2 norm
277-
}
266+
// NOTE: using absolute error instead of relative because coordinates
267+
// values should not affect error, also handles divide by 0 error
268+
double regression_error = (A * regression_vector - b).norm(); // norm() is L2 norm
278269

279270
// Find 2 points on the regression line that we solved for, and use this to construct
280271
// our own Line class

src/software/sensor_fusion/filter/ball_filter.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class BallFilter
4343
static constexpr double MAX_BUFFER_SIZE_VELOCITY_MAGNITUDE = 4.0;
4444
// The extra amount beyond the ball's max speed that we treat ball detections as valid
4545
static constexpr double MAX_ACCEPTABLE_BALL_SPEED_BUFFER = 2.0;
46-
// The maximum error threshold to considering using the generated linear regression
46+
// The maximum root mean squared error threshold to considering using the generated
47+
// linear regression.
4748
// TODO (#2752): Investigate different values of error threshold
4849
static constexpr double LINEAR_REGRESSION_ERROR_THRESHOLD = 1000.0;
4950

@@ -85,6 +86,7 @@ class BallFilter
8586
struct LinearRegressionResults
8687
{
8788
Line regression_line;
89+
// Regression error is root mean squared error
8890
double regression_error;
8991
};
9092

@@ -131,7 +133,8 @@ class BallFilter
131133

132134
/**
133135
* Given a buffer of ball detections, returns the line of best fit through
134-
* the detection positions, and calculate the error of this regression.
136+
* the detection positions, and calculate the root mean squared error of this
137+
* regression.
135138
* Note: also considers vertical lines.
136139
*
137140
* @throws std::invalid_argument if ball_detections has less than 2 elements
@@ -145,7 +148,8 @@ class BallFilter
145148

146149
/**
147150
* Given a list of ball detections, use linear regression to find a line of best fit
148-
* through the ball positions, and calculate the error of this regression.
151+
* through the ball positions, and calculate the root mean squared error of this
152+
* regression.
149153
*
150154
* @throws std::invalid_argument if ball_detections has less than 2 elements
151155
*

0 commit comments

Comments
 (0)