-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathVANTAGE_main.m
334 lines (282 loc) · 11 KB
/
VANTAGE_main.m
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
%% The Signals
wokeSig='NUC is Woke';
manifestSig='Manifest is Sent';
standbySig='NUC is Standby';
initiateSig='CubeSat is Deploying';
resultsSig='Results is Done';
%% Set up sockets (sig=signals, data=data, PI is server)
t_sig = tcpip('169.254.146.34', 4013, 'NetworkRole', 'client');
t_data = tcpip('169.254.146.34', 4014, 'NetworkRole', 'client');
%% Pi is written to 32768 size bytes (32768 was chosen arbitrarily(favorite number), max size unknown)
%Set Output Buffer Size
t_sig.OutputBufferSize=32768;
t_data.OutputBufferSize=32768;
% Open Sockets
fopen(t_sig);
fopen(t_data);
%% Tell PI that NUC is awake
send(t_sig,wokeSig);
pause(0.2)
%% Wait for PI to send the manifest
standby(t_sig,manifestSig);
%% Recieve Manifest, write to ManifestID file
ManifestID=fopen('Manifesto.json','w');
manifest=receive(t_data);
fwrite(ManifestID,manifest);
fclose(ManifestID);
%% Tell PI that NUC is ready to take Data
send(t_sig,standbySig);
%% Wait for PI to tell NUC to start
standby(t_sig,initiateSig);
%% Start sensor automation
% File Definitions
% High-level directory of all TOF information. This should contain the
% executable data capture script and the desired location to save point
% cloud outputs.
tofDirName = '/home/vantage/Documents/githere/VANTAGE/Automation/TOF_Automation/examples/';
tofDirData = '/home/vantage/Documents/githere/VANTAGE/Data/TOF_Automated/';
tofFlush = strcat(tofDirData,'pointcloud*');
tofScript = strcat(tofDirName,'TOF_Data_Capture.py');
opticalDirName = '/home/vantage/Documents/githere/VANTAGE/Automation/OpticalCam_Automation/';
opticalDirImages = '/home/vantage/Documents/githere/VANTAGE/Data/AutomatedImageCapture/';
opticalDirData = '/home/vantage/Documents/githere/VANTAGE/Data/Optical_Automated/';
opticalFlush = strcat(opticalDirData,'VANTAGEOp*');
opticalScript = strcat(opticalDirName,'Optical_Data_Capture.py');
% Flush all TOF point clouds detected in output directory
unix(sprintf('rm %s',tofFlush));
% Flush all optical images detected in output directory
unix(sprintf('rm %s',opticalFlush));
% TOF Data Collection
% This command executes a bash command, which will begin data capture using
% the TOF sensor, and save point clouds to an output directory.
unix(sprintf('python %s &',tofScript));
% Optical Camera Data Collection
% This command executes a bash command, which will begin data capture using
% the optical camera, and save images to an output directory.
unix(sprintf('python %s &',opticalScript));
%% Start le epic post-processing PogChamp
import VANTAGE.PostProcessing.*
import VANTAGE.PostProcessing.Validate
%%%%%% Modular config direcs
configDirecNameModular = 'config/ModularTest_4_9/Test*';
%%%%%% 100m config direcs
configDirecName100m = 'config/3_25_100m/Test*';
%%%%%% Simulation config direcs
configDirecNameSim085 = 'config/Simulation/_085/Sample*';
configDirecNameSim030 = 'config/Simulation/_030/Sample*';
configDirecNameSim140 = 'config/Simulation/_140/Sample*';
configDirecNameSim195 = 'config/Simulation/_195/Sample*';
configDirecNameSim250 = 'config/Simulation/_250/Sample*';
%%%%%% Define desired test type here
testType = 'Modular';
switch testType
case 'Modular'
configDirecName = configDirecNameModular;
testType = 'ModularTest_4_9';
case '100m'
configDirecName = configDirecName100m;
testType = '3_25_100m';
case 'Simulation_085'
configDirecName = configDirecNameSim085;
testType = 'Simulation/_085';
case 'Simulation_030'
configDirecName = configDirecNameSim030;
testType = 'Simulation/_030';
case 'Simulation_140'
configDirecName = configDirecNameSim140;
testType = 'Simulation/_140';
case 'Simulation_195'
configDirecName = configDirecNameSim195;
testType = 'Simulation/_195';
case 'Simulation_250'
configDirecName = configDirecNameSim250;
testType = 'Simulation/_250';
end
configfiles = dir(configDirecName);
for iter = 1:numel(configfiles)
%%% Housekeeping and Allocation
close all;
rng(99);
configfile = [pwd '/config/' testType '/' configfiles(iter).name];
%%% Filenames and configurables
manifestFilename = strcat(configfile,'/Manifest.json');
SensorData = jsondecode(fileread(strcat(configfile,'/Sensors.json')));
Model = VANTAGE.PostProcessing.Model(manifestFilename,configfile,true);
fileLims = [1 inf];
Model.Deployer = Model.TOF.TOFProcessing(SensorData,...
Model.Deployer,'presentResults',0,'fileLims',fileLims,'showDebugPlots',0);
% Process truth data
Truth = Model.Truth_VCF;
% Compute results
Model.ComputeStateOutput();
Validator = Validate(configfile,Model,true);
CubeSatFitted = cell(Model.Deployer.numCubesats,1);
TruthFitted = cell(Model.Deployer.numCubesats,1);
AbsoluteError = cell(Model.Deployer.numCubesats,1);
XError = cell(Model.Deployer.numCubesats,1);
YError = cell(Model.Deployer.numCubesats,1);
ZError = cell(Model.Deployer.numCubesats,1);
data_t = Model.Deployer.CubesatArray(1).time(end);
t_fit = linspace(0,data_t);
for i=1:Model.Deployer.numCubesats
CubeSat = Model.Deployer.CubesatArray(i);
CubeSatFitted{i} = Validator.fitCubeSatTraj(CubeSat.centroids_VCF,CubeSat.time,'CS',t_fit,Model);
TruthFitted{i} = interp1(Model.Truth_VCF.t,Model.Truth_VCF.Cubesat(i).pos,t_fit,'linear');
[AbsoluteError{i},XError{i},YError{i},ZError{i}] = ...
Validator.ProcessError(CubeSatFitted{i},TruthFitted{i});
end
% Save fitted results for error analysis later
if strcmpi(testType,'ModularTest_4_9')
dataFolder = 'Data/Results/matFiles/ModularTest_4_9/';
folderString = Model.Deployer.TruthFileName;
tmp = split(folderString,'/');
testNumber = tmp{3};
elseif strcmpi(testType,'100m')
dataFolder = 'Data/Results/matFiles/100m/';
folderString = Model.Deployer.TruthFileName;
tmp = split(folderString,'/');
testNumber = tmp{3};
elseif strcmpi(testType,'Simulation_030')
dataFolder = 'Data/Results/matFiles/Simulation_4_15_030/';
tmp = split(SensorData.TOFData,'/');
testNumber = tmp{5};
elseif strcmpi(testType,'Simulation_085')
dataFolder = 'Data/Results/matFiles/Simulation_4_15_085/';
tmp = split(SensorData.TOFData,'/');
testNumber = tmp{5};
elseif strcmpi(testType,'Simulation_140')
dataFolder = 'Data/Results/matFiles/Simulation_4_15_140/';
tmp = split(SensorData.TOFData,'/');
testNumber = tmp{5};
elseif strcmpi(testType,'Simulation_195')
dataFolder = 'Data/Results/matFiles/Simulation_4_15_195/';
tmp = split(SensorData.TOFData,'/');
testNumber = tmp{5};
elseif strcmpi(testType,'Simulation_250')
dataFolder = 'Data/Results/matFiles/Simulation_4_15_250/';
tmp = split(SensorData.TOFData,'/');
testNumber = tmp{5};
else
error('invalid test type in Deployer.TruthFileName')
end
mkdir(dataFolder)
mkdir([dataFolder 'data/']);
save([pwd '/' dataFolder 'data/CSData' testNumber '.mat'],'CubeSatFitted');
save([pwd '/' dataFolder 'data/TruthData' testNumber '.mat'],'TruthFitted');
save([pwd '/' dataFolder 'data/AbsErrorData' testNumber '.mat'],'AbsoluteError');
save([pwd '/' dataFolder 'data/XErrorData' testNumber '.mat'],'XError');
save([pwd '/' dataFolder 'data/YErrorData' testNumber '.mat'],'YError');
save([pwd '/' dataFolder 'data/ZErrorData' testNumber '.mat'],'ZError');
save([pwd '/' dataFolder 'data/CSTime' testNumber '.mat'],'t_fit');
end
%%%% Results processing
switch testType
case 'ModularTest_4_9'
configDirecName = configDirecNameModular;
testType = 'ModularTest_4_9';
testDef = 'Modular';
case '100m'
configDirecName = configDirecName100m;
testType = '3_25_100m';
testDef = '100m';
case 'Simulation_085'
configDirecName = configDirecNameSim085;
testType = 'Simulation/_085';
testDef = 'Sim085';
case 'Simulation_030'
configDirecName = configDirecNameSim030;
testType = 'Simulation/_030';
testDef = 'Sim030';
case 'Simulation_140'
configDirecName = configDirecNameSim140;
testType = 'Simulation/_140';
testDef = 'Sim140';
case 'Simulation_195'
configDirecName = configDirecNameSim195;
testType = 'Simulation/_195';
testDef = 'Sim195';
case 'Simulation_250'
configDirecName = configDirecNameSim195;
testType = 'Simulation/_250';
testDef = 'Sim250';
end
configfiles = dir(configDirecName);
Validator.ErrorAnalysis(Model,SensorData,testDef);
matFileDirectory = [pwd '/Data/Results/matFiles'];
Validator.GenerateOutputFiles(matFileDirectory);
%Validator.masterPlotter(matFileDirectory);
%{
Directories for Richard
MANIFEST LOCATION:
/home/vantage/Documents/githere/VANTAGE/config/ModularTest_4_9/Test5/Manifest.json
OUTPUT FILE LOCATION:
/home/vantage/Documents/githere/VANTAGE/Data/Results/matFiles/Modular/jsonOut/Test5_Modular.json
IMAGE DIRECTORY LOCATION:
/home/vantage/Documents/githere/VANTAGE/Data/ModularTest_4_9/Test5/Optical/VANTAGEOp20194_8_23_52_48_933.png
/home/vantage/Documents/githere/VANTAGE/Data/ModularTest_4_9/Test5/Optical/VANTAGEOp20194_8_23_52_49_293.png
%}
%% write results and input results filename and format in this code
% the file here is the results file that the post processsing writes to
fid=fopen('config_simulation_template_2_25_Josh_ToF_Calibration_tube2_truth_data.json','r');
resultsfile=fread(fid);
fclose(fid);
%% Tell PI NUC is sending results
send(t_sig,resultsSig);
pause(0.2); % helps me visualize whats going on
%% Send Pi the results
send(t_data,resultsfile);
%% close sockets
fclose(t_sig)
fclose(t_data)
%% housekeeping
clear all
clc
% Turn off code here
% PepegaHands
%% functions used (If you can find premade functions, use them. I couldn't find any)
% function standby. NUC is on "standby" until the "signal" is received
function output = standby(t,signal)
while(1==1)
if (t.BytesAvailable >0)
received_data=fread(t,t.BytesAvailable);
received_data=char(received_data');
if(received_data(1:length(signal))==signal)
break;
end
end
end
end
% function receive. NUC "receives" data and returns the data
function output = receive(t)
data=[];
t_b=0; % t_b is fun
while(1==1)
t_b=(t_b+t_b)/2; % this is really fucking critical, DONT touch it
if (t.BytesAvailable >0)
received_data=fread(t,t.BytesAvailable);
data_temp=[data , received_data'];
data=data_temp;
t_b=0;
else
t_b=t_b+1;
end
if(t_b==100)
break;
end
end
output=data;
end
% function send. NUC "sends" the "data" to the PI as a string
function output = send(t,data)
buffcount=1;
buffsize=t.OutputBufferSize;
buffcount1=buffcount+buffsize;
while(buffcount1 < length(data))
data_buff=data(buffcount:buffcount1-1);
fwrite(t,data_buff);
buffcount=buffcount1;
buffcount1=buffcount+buffsize;
end
fwrite(t,data(buffcount:end));
pause(0.2)
end