-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkfkernelsketch
139 lines (89 loc) · 2.8 KB
/
kfkernelsketch
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
#include "BRAM-uio-driver/src/bram_uio.h"
#include "KalmanFilterKernel-driver/src/xkalmanfilterkernel.h"
#define INPUT_FILENAME "input.txt"
#define OUTPUT_FILENAME "output.txt"
#define DELIMITER ','
#define SAMPLE_FREQ 10 // Hz
#define DT 0.1
#define BRAM_SIZE 2048
#define N_SAMPLES 300
#define N_STATE_VARS 6
#define N_CTRL_VARS 3
#define N_MEAS_VARS 3
#include <stdint.h>
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
/*
vector<vector<float>> parseFile(string filename) {
vector <vector <float> > data;
ifstream infile( INPUT_FILENAME );
bool first_line = true;
while(infile) {
string s;
if (!getline(infile, s)) break;
if (first_line) {
first_line = false;
continue;
}
istringstream ss(s);
vector<float> record;
while(ss) {
string s;
if(!getline(ss,s,DELIMITER)) break;
record.push_back(stof(s));
}
data.push_back(record);
}
if (!infile.eof()) {
cerr << "Fooey!\n";
}
return data;
}
*/
void writeDataToFile(float dout[N_SAMPLES*N_STATE_VARS], string filename) {
ofstream f;
f.open(filename);
f << "x_hat_0,x_hat_1,x_hat_2,x_hat_3,x_hat_4,x_hat_5\n";
for (int i = 0; i < N_SAMPLES; i++) {
for (int j = 0; j < N_STATE_VARS; j++) {
f << dout[i*N_STATE_VARS+j];
if (j < N_STATE_VARS-1) {
f << ",";
}
}
if (i < N_SAMPLES-1) {
f << "\n";
}
}
}
int main(int argc, char *argv){
// API of bram from LEC4 bram uio driver. Also i suppose the data is automatically written to and from the BRAMs as this api is taken from the IP file of Kalman kernel
// initialization
//BRAM bram0
//BRAM bram1
XKalmanfilterkernel kf_kernel;
XKalmanfilterkernel_Initialize(&kf_kernel, "KalmanFilterKernel");
#include "data.h" //din is declared in here so later in the code we are supposed to read from BRAM (unclear part)
vector<vector<float>> data_vec = parseFile(INPUT_FILENAME);
//float din[N_SAMPLES*(N_MEAS_VARS+N_CTRL_VARS)];
float dout[N_SAMPLES*N_STATE_VARS];
// setting parameters
XKalmanfilterkernel_Set_q(&kf_kernel, u32 Data); // maybe 0.05 in place of u32 Data
XKalmanfilterkernel_Set_r(&kf_kernel, u32 Data); // maybe 0.95 ...
if(XKalmanfilterkernel_IsIdle(&kf_kernel)){ //returns: return (Data >> 1) & 0x1; so not sure here
XKalmanfilterkernel_Start(&kf_kernel);
}
while(XKalmanfilterkernel_IsDone(&kf_kernel)){ //again not sure as to how to wait until its finished
sleep(10); //some time probably?
}
if(XKalmanfilterkernel_IsIdle(&kf_kernel)){ //returns: return (Data >> 1) & 0x1; so not sure here
//Method to read the data from from BRAM1, maybe some function like ParseData, but im not sure
writeDataToFile(dout, OUTPUT_FILENAME); // BRAM outputs to file
}
XKalmanfilterkernel_Release(&kf_kernel);
return 0;
}