-
Notifications
You must be signed in to change notification settings - Fork 2
/
liveView.m
173 lines (149 loc) · 3.91 KB
/
liveView.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
close all;
clear all;
clc;
global running = false;
global replay = false;
global view = 1;
global fileName = "data.csv";
global filePath = -1;
global initView;
global updateView;
global views;
global data = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function stop()
global running data;
running = false;
data = [];
end
function setMode(bool)
global replay
replay = bool;
end
function setView(idx)
global view;
view = idx;
end
function setFile(handle)
global fileName filePath;
[file, folder] = uigetfile('*.csv','Select the source file');
if ~file
return;
end
set(handle, "string", file);
fileName = file;
filePath = folder;
end
function file = openFile(measureName)
global fileName filePath;
if filePath == -1
fprintf("No source file selected\n");
file = -1;
return;
end
file = fopen([filePath fileName]);
if file == -1
fprintf("Could not open %s\n", fileName);
end
end
function liveHandle = initCallbackUi()
backHandle = uicontrol(
"string", "<--",
"units", "normalized",
"position", [0 1 0 0],
"callback", @stop
);
set(backHandle, "units", "centimeters");
pos = get(backHandle, "position");
set(backHandle, "position", [0 pos(2)-1 1 1]);
liveHandle = uicontrol(
"units", "normalized",
"position", [1 1 0 0]
);
set(liveHandle, "units", "centimeters");
pos = get(liveHandle, "position");
set(liveHandle, "position", [pos(1)-1 pos(2)-1 1 1]);
set(liveHandle, "backgroundcolor", "red");
end
function busyWait(dt)
global running;
c = dt/0.01;
while(running && c > 0)
pause(.01);
--c;
end
end
function loop()
global running replay view initView updateView;
idx = view;
t0 = 0;
running = true;
clf();
handles = initView{1,idx}();
liveHandle = initCallbackUi();
refresh();
file = openFile();
if file == -1
running = false;
elseif !replay
fseek(file, 0, SEEK_END());
end
offlineCount = 0;
while (running)
line = fgetl(file);
if !replay && line == -1
fseek(file, ftell(file));
if ++offlineCount == 50
set(liveHandle, "backgroundcolor", "red");
end
pause(.01);
elseif !replay
X = textscan(line, "%f", "Delimiter", ","){1,1}';
offlineCount = 0;
set(liveHandle, "backgroundcolor", "green");
updateView{1,idx}(X, handles);
drawnow();
elseif replay && line == -1
stop();
else
X = textscan(line, "%f", "Delimiter", ","){1,1}';
T = X(2);
dt = (t0!=0)*(T-t0);
t0 = T;
busyWait(dt/1000);
updateView{1,idx}(X, handles);
drawnow();
end
end
if file != -1
fclose(file);
end
if isfigure(1)
clf();
initMain();
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
initView = {@initTrain, @initSonar, @initBox, @init3dPos};
updateView = {@updateTrain, @updateSonar, @updateBox, @update3dPos};
views = {"train tracking", "sonar range", "3d orientation", "3d position"};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Script
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
graphics_toolkit("qt");
fig = figure(
"name", "Sensor fusion live view",
"numbertitle", "off",
"menubar", "none",
"resize", "off",
"units", "normalized",
"position", [.3 .3 .3 .3],
"deletefcn", @stop
);
running = false;
initMain();
waitfor(fig);