/
outlierFilterTdoaSteps.c
156 lines (127 loc) · 4.58 KB
/
outlierFilterTdoaSteps.c
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
/**
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie control firmware
*
* Copyright (C) 2011-2023 Bitcraze AB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* outlierFilter.c: Outlier rejection filter for the LPS system
*/
#include <math.h>
#include "outlierFilterTdoaSteps.h"
#include "stabilizer_types.h"
#include "log.h"
#include "debug.h"
static bool isDistanceDiffSmallerThanDistanceBetweenAnchors(const tdoaMeasurement_t* tdoa);
// The step TDoA outlier filter is deprecated and will be removed
#define BUCKET_ACCEPTANCE_LEVEL 3
#define MAX_BUCKET_FILL 10
#define FILTER_CLOSE_DELAY_COUNT 30
static float acceptanceLevel = 0.0;
static float errorDistance;
static int filterCloseDelayCounter = 0;
static int previousFilterIndex = 0;
typedef struct {
float acceptanceLevel;
int bucket;
} filterLevel_t;
#define FILTER_LEVELS 5
#define FILTER_NONE FILTER_LEVELS
filterLevel_t filterLevels[FILTER_LEVELS] = {
{.acceptanceLevel = 0.4},
{.acceptanceLevel = 0.8},
{.acceptanceLevel = 1.2},
{.acceptanceLevel = 1.6},
{.acceptanceLevel = 2.0},
};
static float distanceSq(const point_t* a, const point_t* b);
static float sq(float a) {return a * a;}
static void addToBucket(filterLevel_t* filter);
static void removeFromBucket(filterLevel_t* filter);
static int updateBuckets(float errorDistance);
bool outlierFilterTdoaValidateSteps(const tdoaMeasurement_t* tdoa, const float error, const vector_t* jacobian, const point_t* estPos) {
bool sampleIsGood = false;
if (isDistanceDiffSmallerThanDistanceBetweenAnchors(tdoa)) {
float errorBaseDistance = sqrtf(powf(jacobian->x, 2) + powf(jacobian->y, 2) + powf(jacobian->z, 2));
errorDistance = fabsf(error / errorBaseDistance);
int filterIndex = updateBuckets(errorDistance);
if (filterIndex > previousFilterIndex) {
filterCloseDelayCounter = FILTER_CLOSE_DELAY_COUNT;
} else if (filterIndex < previousFilterIndex) {
if (filterCloseDelayCounter > 0) {
filterCloseDelayCounter--;
filterIndex = previousFilterIndex;
}
}
previousFilterIndex = filterIndex;
if (filterIndex == FILTER_NONE) {
// Lost tracking, open up to let the kalman filter converge
acceptanceLevel = 100.0;
sampleIsGood = true;
} else {
acceptanceLevel = filterLevels[filterIndex].acceptanceLevel;
if (errorDistance < acceptanceLevel) {
sampleIsGood = true;
}
}
}
return sampleIsGood;
}
static bool isDistanceDiffSmallerThanDistanceBetweenAnchors(const tdoaMeasurement_t* tdoa) {
float anchorDistanceSq = distanceSq(&tdoa->anchorPositions[0], &tdoa->anchorPositions[1]);
float distanceDiffSq = sq(tdoa->distanceDiff);
return (distanceDiffSq < anchorDistanceSq);
}
static float distanceSq(const point_t* a, const point_t* b) {
return sq(a->x - b->x) + sq(a->y - b->y) + sq(a->z - b->z);
}
static void addToBucket(filterLevel_t* filter) {
if (filter->bucket < MAX_BUCKET_FILL) {
filter->bucket++;
}
}
static void removeFromBucket(filterLevel_t* filter) {
if (filter->bucket > 0) {
filter->bucket--;
}
}
static int updateBuckets(float errorDistance) {
int filterIndex = FILTER_NONE;
for (int i = FILTER_LEVELS - 1; i >= 0; i--) {
filterLevel_t* filter = &filterLevels[i];
if (errorDistance < filter->acceptanceLevel) {
removeFromBucket(filter);
} else {
addToBucket(filter);
}
if (filter->bucket < BUCKET_ACCEPTANCE_LEVEL) {
filterIndex = i;
}
}
return filterIndex;
}
LOG_GROUP_START(outlierf)
LOG_ADD(LOG_INT32, bucket0, &filterLevels[0].bucket)
LOG_ADD(LOG_INT32, bucket1, &filterLevels[1].bucket)
LOG_ADD(LOG_INT32, bucket2, &filterLevels[2].bucket)
LOG_ADD(LOG_INT32, bucket3, &filterLevels[3].bucket)
LOG_ADD(LOG_INT32, bucket4, &filterLevels[4].bucket)
LOG_ADD(LOG_FLOAT, accLev, &acceptanceLevel)
LOG_ADD(LOG_FLOAT, errD, &errorDistance)
LOG_GROUP_STOP(outlierf)