-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathNet.Filtering.Kalman
More file actions
137 lines (130 loc) · 5.55 KB
/
MathNet.Filtering.Kalman
File metadata and controls
137 lines (130 loc) · 5.55 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.IO.Ports;
using MathNet.Filtering.Kalman;
using Matrix = MathNet.Numerics.LinearAlgebra.Matrix<double>;
namespace Kalman_Filter_Nuget
{
class Program
{
static void Main(string[] args)
{
SerialPort port = new SerialPort("COM3", 19200);
String CurrentReading = "ABCD";
Matrix initial = Matrix.Build.DenseOfArray(new double[,] {
{0},
{0},
{0},
{0},
{0},
{0},
{0},
{0},
{0} });
Matrix covariance = Matrix.Build.DenseOfArray(new double[,] {
{0.1,0,0,0,0,0,0,0,0},
{0,0.1,0,0,0,0,0,0,0},
{0,0,0.1,0,0,0,0,0,0},
{0,0,0,0.1,0,0,0,0,0},
{0,0,0,0,0.1,0,0,0,0},
{0,0,0,0,0,0.1,0,0,0},
{0,0,0,0,0,0,0.1,0,0},
{0,0,0,0,0,0,0,0.1,0},
{0,0,0,0,0,0,0,0,0.1 } });
DiscreteKalmanFilter kalman = new DiscreteKalmanFilter(initial, covariance);
double[] IndividualValues = { 0, 0, 0, 0, 0, 0 };
String[] SubStrings;
double x = 0, y = 0, z = 0, vx = 0, t2 = 0, x1, y1, z1, time, velx, vely, velz, accx, accy, accz;
bool correctRead;
int comaCount = 0;
char[] test;
port.ReadTimeout = 2000;
port.Close();
if (!port.IsOpen)
port.Open();
while (t2 <= 20000000000)
{
CurrentReading = port.ReadLine();
test = CurrentReading.ToCharArray();
correctRead = true;
comaCount = 0;
for (int i = test.Length - 1; i >= 0; i--)
{
if (Char.IsNumber(test[i]) == false & test[i] != ',' & test[i] != '.' & Char.IsWhiteSpace(test[i]) == false & test[i] != '-')
{
correctRead = false;
}
if (test[i] == ',')
{
comaCount = comaCount + 1;
}
}
if (correctRead == true & comaCount == 4)
{
SubStrings = CurrentReading.Split(',');
x1 = Convert.ToSingle(SubStrings[0]);
y1 = Convert.ToSingle(SubStrings[1]);
z1 = Convert.ToSingle(SubStrings[2]);
time = Convert.ToSingle(SubStrings[3]);
velx = (x1 - x) / (time - t2);
vely = (y1 - y) / (time - t2);
velz = (z1 - z) / (time - t2);
accx = velx / (time - t2);
accy = vely / (time - t2);
accz = velz / (time - t2);
//Dummy Data : Generate a very noisy position estimate
Matrix res = Matrix.Build.DenseOfArray(new double[,] {
{x1,0,0,0,0,0,0,0,0},
{0,y1,0,0,0,0,0,0,0},
{0,0,z1,0,0,0,0,0,0},
{0,0,0,velx,0,0,0,0,0},
{0,0,0,0,vely,0,0,0,0},
{0,0,0,0,0,velz,0,0,0},
{0,0,0,0,0,0,accx,0,0},
{0,0,0,0,0,0,0,accy,0},
{0,0,0,0,0,0,0,0,accz } });
Matrix Meassurement = Matrix.Build.DenseOfArray(new double[,] {
{0.1},
{0.1},
{0.1},
{0.1},
{0.1},
{0.1},
{0.1},
{0.1},
{0.1} });
Matrix Noise = Matrix.Build.DenseOfArray(new double[,] {
{0.2321119684f,0,0,0,0,0,0,0,0},
{0,0.00086436f,0,0,0,0,0,0,0},
{0,0,0.22648081f,0,0,0,0,0,0},
{0,0,0,0.00002701125f,0,0,0,0,0},
{0,0,0,0,0.000000059536f,0,0,0,0},
{0,0,0,0,0,0.000001946025f,0,0,0},
{0,0,0,0,0,0,0.0000008441015625f,0,0},
{0,0,0,0,0,0,0,0.0000000018605f,0},
{0,0,0,0,0,0,0,0,0.00000006081328125f } });
//veryfied
Matrix startetrans = Matrix.Build.DenseOfArray(new double[,] {
{1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,0,1,0},
{0,0,0,0,0,0,0,0,1 } });
kalman.Predict(startetrans, kalman.Cov);
kalman.Update(Meassurement, Noise, covariance);
x1 += kalman.State.At(0, 0);
y1 += kalman.State.At(1, 0);
z1 += kalman.State.At(2, 0);
Console.WriteLine(x1 +","+ y1 +"," +z1);
t2 = time;
x = x1;
y = y1;
z = z1;
}
}
}
}
}