Thunderbots Project
Loading...
Searching...
No Matches
ball_filter.h
1#pragma once
2
3#include <boost/circular_buffer.hpp>
4#include <optional>
5
6#include "software/geom/line.h"
7#include "software/geom/point.h"
8#include "software/geom/rectangle.h"
9#include "software/sensor_fusion/filter/vision_detection.h"
10#include "software/time/timestamp.h"
11#include "software/world/ball.h"
12
31{
32 public:
33 // The min and max sizes of the ball detection buffer.
34 // As the ball slows down, the buffer size will approach the MAX_BUFFER_SIZE.
35 // As the ball speeds up, the buffer size will approach the MIN_BUFFER_SIZE.
36 static constexpr unsigned int MIN_BUFFER_SIZE = 4;
37 static constexpr unsigned int MAX_BUFFER_SIZE = 10;
38 // If the estimated ball speed is less than this value, the largest possible buffer
39 // will be used by the filter
40 static constexpr double MIN_BUFFER_SIZE_VELOCITY_MAGNITUDE = 0.5;
41 // If the estimated ball speed is greater than this value, the smallest possible
42 // buffer will be used by the filter
43 static constexpr double MAX_BUFFER_SIZE_VELOCITY_MAGNITUDE = 4.0;
44 // The extra amount beyond the ball's max speed that we treat ball detections as valid
45 static constexpr double MAX_ACCEPTABLE_BALL_SPEED_BUFFER = 2.0;
46 // The maximum error threshold to considering using the generated linear regression
47 // TODO (#2752): Investigate different values of error threshold
48 static constexpr double LINEAR_REGRESSION_ERROR_THRESHOLD = 1000.0;
49
53 explicit BallFilter();
54
66 std::optional<Ball> estimateBallState(
67 const std::vector<BallDetection>& new_ball_detections,
68 const Rectangle& filter_area);
69
70 private:
74 struct BallVelocityEstimate
75 {
76 Vector average_velocity;
77 double average_velocity_magnitude;
78 // The average of the max velocity magnitude and min velocity magnitude
79 double min_max_magnitude_average;
80 };
81
85 struct LinearRegressionResults
86 {
87 Line regression_line;
88 double regression_error;
89 };
90
102 void addNewDetectionsToBuffer(std::vector<BallDetection> new_ball_detections,
103 const Rectangle& filter_area);
104
114 static std::optional<Ball> estimateBallStateFromBuffer(
115 boost::circular_buffer<BallDetection> ball_detections);
116
129 static std::optional<size_t> getAdjustedBufferSize(
130 boost::circular_buffer<BallDetection> ball_detections);
131
143 static LinearRegressionResults calculateLineOfBestFit(
144 boost::circular_buffer<BallDetection> ball_detections);
145
156 static LinearRegressionResults calculateLinearRegression(
157 boost::circular_buffer<BallDetection> ball_detections);
158
170 static Point estimateBallPosition(
171 boost::circular_buffer<BallDetection> ball_detections,
172 const Line& regression_line);
173
187 static std::optional<BallVelocityEstimate> estimateBallVelocity(
188 boost::circular_buffer<BallDetection> ball_detections,
189 const std::optional<Line>& ball_regression_line = std::nullopt);
190
191 boost::circular_buffer<BallDetection> ball_detection_buffer;
192};
Definition ball_filter.h:31
BallFilter()
Definition ball_filter.cpp:14
std::optional< Ball > estimateBallState(const std::vector< BallDetection > &new_ball_detections, const Rectangle &filter_area)
Definition ball_filter.cpp:16
Definition line.h:10
Definition point.h:14
Definition rectangle.h:10
Definition vector.h:12