forked from IceyFL/Aimmy-V2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PredictionManager.cs
52 lines (42 loc) · 1.46 KB
/
PredictionManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using Accord.Statistics.Running;
using System;
namespace AimmyWPF
{
internal class PredictionManager
{
public struct Detection
{
public int X;
public int Y;
public DateTime Timestamp;
}
private KalmanFilter2D kalmanFilter;
private DateTime lastUpdateTime;
public PredictionManager()
{
kalmanFilter = new KalmanFilter2D();
lastUpdateTime = DateTime.UtcNow;
}
public void UpdateKalmanFilter(Detection detection)
{
var currentTime = DateTime.UtcNow;
kalmanFilter.Push(detection.X, detection.Y);
lastUpdateTime = currentTime;
}
public Detection GetEstimatedPosition()
{
// Current estimated position
double currentX = kalmanFilter.X;
double currentY = kalmanFilter.Y;
// Current velocity
double velocityX = kalmanFilter.XAxisVelocity;
double velocityY = kalmanFilter.YAxisVelocity;
// Calculate time since last update
double timeStep = (DateTime.UtcNow - lastUpdateTime).TotalSeconds;
// Predict next position based on current position and velocity
double predictedX = currentX + velocityX * timeStep;
double predictedY = currentY + velocityY * timeStep;
return new Detection { X = (int)predictedX, Y = (int)predictedY };
}
}
}