-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGPSKalmanForm.cpp
188 lines (151 loc) · 5.29 KB
/
GPSKalmanForm.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//---------------------------------------------------------------------------
#include <vcl.h>
#include <System.IniFiles.hpp>
#pragma hdrstop
#include "GPSKalman.h"
#include "GPSKalmanForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TGPSKalmanFilter *GPSKalmanFilter;
//---------------------------------------------------------------------------
__fastcall TGPSKalmanFilter::TGPSKalmanFilter(TComponent* Owner)
: TForm(Owner)
{
m_pFilteredTrack = NULL;
m_pCurrentTrack = NULL;
TIniFile *pini = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));
RValue->Text = AnsiString(pini->ReadFloat("Kalman", "R", 15.0));
SaveSpeed->Checked = pini->ReadBool("Kalman", "SaveSpeed", true);
SaveAlt->Checked = pini->ReadBool("Kalman", "SaveAlt", true);
delete pini;
}
//---------------------------------------------------------------------------
void __fastcall TGPSKalmanFilter::m_OkClick(TObject *Sender)
{
double dRValue = RValue->Text.ToDouble();
bool bSaveSpeed = SaveSpeed->Checked;
bool bSaveAlt = SaveAlt->Checked;
ProgressBar->Max = m_pCurrentTrack->PointCount * 3 - 1;
ProgressBar->Position = 0;
TIniFile *pini = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));
pini->WriteFloat("Kalman", "R", dRValue);
pini->WriteBool("Kalman", "SaveSpeed", bSaveSpeed);
if(SaveAlt->Enabled) pini->WriteBool("Kalman", "SaveAlt", bSaveAlt);
delete pini;
m_pFilteredTrack = new GPXTrack;
double *pAcc = new double[m_pCurrentTrack->PointCount];
double *pAltSpd = new double[m_pCurrentTrack->PointCount];
double *pAltAcc = new double[m_pCurrentTrack->PointCount];
double dAvgAcc = 0.0;
double dAvgAltAcc = 0.0;
pAcc[0] = 0.0;
pAltSpd[0] = 0.0;
pAltAcc[0] = 0.0;
for(int i = 1; i < m_pCurrentTrack->PointCount; i++)
{
ProgressBar->Position = ProgressBar->Position + 1;
pAcc[i] = pAltSpd[i] = pAltAcc[i] = 0.0;
double dT = m_pCurrentTrack->Points[i]->m_dTime - m_pCurrentTrack->Points[i - 1]->m_dTime;
if(dT > 0.0)
{
pAcc[i] = (m_pCurrentTrack->Points[i]->m_dSpeed - m_pCurrentTrack->Points[i - 1]->m_dSpeed) / dT;
if(!m_pCurrentTrack->m_bNoAlt)
{
pAltSpd[i] = (m_pCurrentTrack->Points[i]->m_dAlt - m_pCurrentTrack->Points[i - 1]->m_dAlt) / dT;
pAltAcc[i] = (pAltSpd[i] - pAltSpd[i - 1]) / dT;
}
}
dAvgAcc += pAcc[i];
if(!m_pCurrentTrack->m_bNoAlt) dAvgAltAcc += pAltAcc[i];
}
dAvgAcc /= (double)m_pCurrentTrack->PointCount;
if(!m_pCurrentTrack->m_bNoAlt) dAvgAltAcc /= (double)m_pCurrentTrack->PointCount;
double dSigma2 = 0.0;
double dAltSigma2 = 0.0;
for(int i = 0; i < m_pCurrentTrack->PointCount; i++)
{
ProgressBar->Position = ProgressBar->Position + 1;
double dA = pAcc[i] - dAvgAcc;
dSigma2 += (dA * dA);
if(!m_pCurrentTrack->m_bNoAlt)
{
dA = pAltAcc[i] - dAvgAltAcc;
dAltSigma2 += (dA * dA);
}
}
dSigma2 /= (double)m_pCurrentTrack->PointCount;
if(!m_pCurrentTrack->m_bNoAlt) dAltSigma2 /= (double)m_pCurrentTrack->PointCount;
GPSKalman2D *pKalman = new GPSKalman2D();
pKalman->R = dRValue; pKalman->Sigma2 = dSigma2;
if(!m_pCurrentTrack->m_bNoAlt) pKalman->AltSigma = dAltSigma2;
pKalman->FilterAlt = !m_pCurrentTrack->m_bNoAlt;
for(int i = 0; i < m_pCurrentTrack->PointCount; i++)
{
ProgressBar->Position = ProgressBar->Position + 1;
Application->ProcessMessages();
PGPXTrackPoint pPoint = m_pCurrentTrack->Points[i];
double dTime = pPoint->m_dTime;
double dLat = pPoint->m_dLat;
double dLon = pPoint->m_dLon;
double dSpeed = pPoint->m_dSpeed;
double dCourse = pPoint->m_dCourse;
double dAlt = pPoint->m_dAlt;
double dAltSpeed = pAltSpd[i];
pKalman->Filter(dTime, dLat, dLon, dSpeed, dCourse, dAlt, dAltSpeed);
PGPXTrackPoint pFilteredPoint = m_pFilteredTrack->NewPoint;
pFilteredPoint->SetPoint(pPoint);
pFilteredPoint->m_dLat = dLat;
pFilteredPoint->m_dLon = dLon;
if(bSaveSpeed)
{
pFilteredPoint->m_dSpeed = dSpeed;
pFilteredPoint->m_dCourse = dCourse;
}
if(bSaveAlt)
pFilteredPoint->m_dAlt = dAlt;
m_pFilteredTrack->AddPoint(pFilteredPoint);
}
delete [] pAcc;
delete [] pAltAcc;
delete [] pAltSpd;
delete pKalman;
ProgressBar->Position = 0;
}
//---------------------------------------------------------------------------
void __fastcall TGPSKalmanFilter::SetPCurrentTrack(PGPXTrack value)
{
if(m_pCurrentTrack != value) {
m_pCurrentTrack = value;
m_pFilteredTrack = NULL;
if(m_pCurrentTrack)
{
if(m_pCurrentTrack->m_bNoAlt)
{
SaveAlt->Checked = false;
SaveAlt->Enabled = false;
}
else
{
SaveAlt->Enabled = true;
TIniFile *pini = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));
SaveAlt->Checked = pini->ReadBool("Kalman", "SaveAlt", true);
delete pini;
}
}
}
}
PGPXTrack __fastcall TGPSKalmanFilter::GetPCurrentTrack()
{
return m_pFilteredTrack;
}
void __fastcall TGPSKalmanFilter::ValueKeyPress(TObject *Sender,
char &Key)
{
if((Key >= '0' && Key <= '9') ||
Key == VK_BACK ||
Key == VK_RETURN ||
Key == '.') {}
else Key = 0;
}
//---------------------------------------------------------------------------