-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_showcase.cpp
257 lines (197 loc) · 6.67 KB
/
main_showcase.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <Arduino.h>
#include <TFT_eSPI.h>
#include <SPI.h>
#include <BasicLinearAlgebra.h>
using namespace BLA;
// screen setup and naming
TFT_eSPI tft = TFT_eSPI();
const long sampletime = 5;
const long samplePrintTime = 10;
struct KalmanFilter
{
// Sensor setup
int pin;
int reading = 0;
// phi parameters
const float M = 13.8384;
const float phi_af = 0.9979;
const float phi_ar = 0.9777;
const float phi_ra = 0.0589;
const float phi_fa = 0.9987;
struct state {
BLA::Matrix<2, 2> P = {1., 0., 0., 1.};
BLA::Matrix<2, 1> x = {0.,0.};
};
// initializing matrixes
state prediction;
state estimate;
BLA::Matrix<2, 2> F = {1., 0.0, 0., 0.};
BLA::Matrix<1, 1> y = {0.};
BLA::Matrix<1, 1> z = {0.};
BLA::Matrix<1, 1> z_prev = {0.};
BLA::Matrix<1, 1> S = {1.};
BLA::Matrix<2, 1> K = {0., 0.};
const BLA::Matrix<2, 2> Q = {0.05, 0.1, 0.1, 0.4}; // variance of movement - unchanged
const BLA::Matrix<1, 1> R = {0.005}; // Variance of measurement - unchanged
const BLA::Matrix<1, 2> H = {1, 0.0};
const BLA::Matrix<2, 1> B = {phi_ra * M, 0};
const BLA::Matrix<2, 2> I = {1., 0., 0., 1.};
void read()
{
z_prev = z;
reading = analogRead(pin);
z(0) = {static_cast<float>(13.8384*reading)/4095};
}
void predict() {
bool flagDer = (z(0) - z_prev(0))/sampletime > derThresh;
bool flagLow = z(0) > threshold;
if (flagDer and flagLow) {
F = FPressed;
prediction.x = (F * estimate.x) + (B);
}
else {
F = FReleased;
prediction.x = (F * estimate.x);
}
prediction.P = (F * estimate.P * ~F) + (Q);
}
void update()
{
// Calculate residual
y = z - (H * prediction.x);
// Calculate innovation matrix
S = (H * prediction.P * ~H) + R;
// Calculate the kalman gain
K = (prediction.P * ~H) * Invert(S);
// Calculate the estimates and state covariance matrix
estimate.x = prediction.x + (K * y);
estimate.P = (I - (K * H)) * prediction.P * ~(I - (K * H)) + ((K * R) * ~K);
}
void readPredUpd()
{
read();
predict();
update();
}
};
// make true to display the raw sensordata in red
const bool displayRaw = true;
// make false to not display the filtered sensors
const bool displayFiltered = true;
// make true/false to enable/disable sensors
const bool topsens = true;
// timeline of measurement for display
int topPixels[4];
int topMidPixels[4];
int botMidPixels[4];
int botPixels[4];
// current graphing pixel
int i = 0;
// scale of display
const int scales[4] = {130, 1000, 2000, 4095};
int picker = 0;
// TTGO builtin buttons
int button1Pin = 0;
int button2Pin = 35;
void topDisplay();
KalmanFilter topSensor;
KalmanFilter topMidSensor;
KalmanFilter botMidSensor;
KalmanFilter botSensor;
void setup()
{
// Screen startup
tft.init();
// Pin assignment
topSensor.pin = 26;
topMidSensor.pin = 25;
botMidSensor.pin = 33;
botSensor.pin = 32;
}
void loop()
{ // readings and predictions
if (millis() - prevSample >= sampletime) {
topSensor.readPredUpd();
topMidSensor.readPredUpd();
botMidSensor.readPredUpd();
botSensor.readPredUpd();
prevSample = millis();
}
// print for datacollection
if((millis() - sampleStartTime) >= samplePrintTime){
//static_cast<float>
Serial.print(String(static_cast<float>(topSensor.estimate.x(0))));
Serial.print(",");
Serial.print(String(static_cast<float>(topMidSensor.estimate.x(0))));
Serial.print(",");
Serial.print(String(static_cast<float>(botMidSensor.estimate.x(0))));
Serial.print(",");
Serial.print(String(static_cast<float>(botSensor.estimate.x(0))));
Serial.print(";");
sampleStartTime = millis();
}
/*
if (millis() - prevprint >= 20) {
//Serial.print("x musselmasstop: "); Serial.print(topSensor.estimate.x(1)); Serial.print(" ");
//Serial.print("x pred: "); Serial.print(topSensor.prediction.x(0)); Serial.print(" ");
Serial.print("x pred musselmasstop: "); Serial.print(topSensor.prediction.x(1)); Serial.print(" ");
//Serial.print("y top: "); Serial.print(topSensor.y(0)); Serial.print(" ");
//Serial.print("P top: "); Serial.print(sqrt(topSensor.estimate.P(0))*5); Serial.print(" ");
//Serial.print("P bot: "); Serial.print(-sqrt(topSensor.estimate.P(0))*5); Serial.print(" ");
Serial.print("x_top: "); Serial.print(topSensor.estimate.x(0)); Serial.print(" ");
Serial.print("z_top: "); Serial.print(topSensor.z(0)); Serial.print(" ");
//Serial.print("x_topMid: "); Serial.print(topMidSensor.estimate.x(0)); Serial.print(" ");
//Serial.print("z_topMid: "); Serial.print(topMidSensor.z(0)); Serial.print(" ");
//Serial.print("x_botMid: "); Serial.print(botMidSensor.estimate.x(0)); Serial.print(" ");
//Serial.print("z_botMid: "); Serial.print(botMidSensor.z(0)); Serial.print(" ");
//Serial.print("x_bot: "); Serial.print(botSensor.estimate.x(0)); Serial.print(" ");
//Serial.print("z_bot: "); Serial.print(botSensor.z(0)); Serial.print(" ");
Serial.println("uT");
prevprint = millis();
}*/
/*
// debuggingprinting
Serial.println(i);
Serial << "predicted Z value: " << topSensor.z << '\n';
Serial << "predicted X value: " << topSensor.estimate.x << '\n';
Serial << "predicted P value: " << topSensor.estimate.P << '\n';
Serial << "predicted P_pred : " << topSensor.prediction.P << '\n';
Serial << "predicted K value: " << topSensor.K << '\n';
Serial << "predicted S value: " << topSensor.S << '\n';
i++;
*/
// grafing
if (millis() - prevprint >= 20) {
// scrolling thru screen
i++;
if (i > 241) {
i = 0;
tft.fillScreen(TFT_BLACK);
}
// display the picked scale and the raw and filtered bottomsensor in numbers
tft.drawString("Scale: " + String(scales[picker]),155,0);
// top sensor
if (topsens) {
Display();
}
prevprint = millis();
}
}
void Display()
{
if (displayFiltered)
{
topPixels[1] = topPixels[0];
topPixels[0] = map((4095*topSensor.estimate.x(0))/13.8384, 0, scales[picker], 130, 1);
tft.drawLine(i, topPixels[0], i, topPixels[1], TFT_BLUE);
topMidPixels[1] = topMidPixels[0];
topMidPixels[0] = map((4095*topMidSensor.estimate.x(0))/13.8384, 0, scales[picker], 130, 1);
tft.drawLine(i, topMidPixels[0], i, topMidPixels[1], TFT_GREENYELLOW);
botMidPixels[1] = botMidPixels[0];
botMidPixels[0] = map((4095*botMidSensor.estimate.x(0))/13.8384, 0, scales[picker], 130, 1);
tft.drawLine(i, botMidPixels[0], i, botMidPixels[1], TFT_ORANGE);
botPixels[1] = botPixels[0];
botPixels[0] = map((4095*botSensor.estimate.x(0))/13.8384, 0, scales[picker], 130, 1);
tft.drawLine(i, botPixels[0], i, botPixels[1], TFT_RED);
}
}