tg = slrt;

%%
% if tg.Connected == "Yes"
%     if tg.Status == "stopped"
%         %% Load the application
%         tg.load('test_cercalo');
% 
%         %% Run the application
%         tg.start;
%         pause(10);
%         tg.stop;
%     end
% end

%%
f = SimulinkRealTime.openFTP(tg);
mget(f, 'data/data_001.dat');
close(f);

%% Convert the Data
data = SimulinkRealTime.utils.getFileScopeData('data/data_001.dat').data;

t  = data(:, end);
uh = data(:, 1); % Voltage sent to cercalo in horizontal direction
uv = data(:, 2); % [...] in vertical direction
xh = data(:, 3); % Measured horizontal position of the beam by the 4QD
xv = data(:, 4); % [...] vertical position
cuh = data(:, 3); % Voltage of the cercalo's inductors used for horizontal
cuv = data(:, 4); % [...] vertical

save('mat/data_001.mat', 't', 'uh', 'uv', 'xh', 'xv', 'cuh', 'cuv');

%% Plot the data
figure;
hold on;
plot(t, uh);
plot(t, uv);
hold off
xlabel('Time [s]');
ylabel('Voltage [V]');

figure;
hold on;
plot(t, xh);
plot(t, xv);
hold off
xlabel('Time [s]');
ylabel('Voltage [V]');
