-
Notifications
You must be signed in to change notification settings - Fork 0
/
Oscilloscope.m
122 lines (107 loc) · 3.65 KB
/
Oscilloscope.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
%{
Name: Christiaan Reurslag
Studentnummer: s1495089
Description: Plot the data from the Arduino in real Time and logs the
data in a txt file.
MATLAB R2016b
%}
%Close all figures and clear workspace and command window
clear all
close all
clc
NChannels = 4;
%WindowSize = 10000*10*6; %Last 10 seconds are visible in figure
colours = {[0, 0.4470, 0.7410], [0.8500, 0.3250, 0.0980], [0.9290, 0.6940, 0.1250], ...
[0.4940, 0.1840, 0.5560], [0.4660, 0.6740, 0.1880], [0.3010, 0.7450, 0.9330], ...
[0.6350, 0.0780, 0.1840], [0.25, 0.25, 0.25]}; %Define the colors of the lines used to plot the data
Arduino=serial('COM4','BaudRate',115200,'TimeOut',inf);
fopen(Arduino); %Initiate arduino communication
%Create figure with labels and grid
figure('units','normalized','outerposition',[0 0.04 1 0.95]);
subplot(2,2,1)
title('Charge current (A)')
hold on
grid on
grid minor
xlabel('Time [ms]')
subplot(2,2,2)
title('Battery voltage (V)')
hold on
grid on
grid minor
xlabel('Time [ms]')
subplot(2,2,3)
title('PWM signal')
hold on
grid on
grid minor
xlabel('Time [ms]')
subplot(2,2,4)
title('Temperature (graden C)')
hold on
grid on
grid minor
xlabel('Time [ms]')
%Open textfile
fileId = fopen('LogPlotData.txt','w');
fprintf(fileId,'Data recieved from Arduino\r\n');
j = 0; %Count number of measurements
sampling = true; %When true, Matlab looks for new data send from the Arduino
while(sampling)
start_message = fread(Arduino,1,'uint16'); %Read data as integer (1 time 2 bytes (16 bits))
if(start_message == 65535) %Start sign recieved from Arduino?
j = j + 1;
for k = 1:NChannels
subplot(2,2,k)
h(k) = animatedline;
h(k).Color = colours{1}; %Set colour of line
end
fprintf(fileId,'\r\nMeasurement number: %u\r\n', j);
for k = 1:NChannels
fprintf(fileId,'\tValue%u',k);
end
fprintf(fileId,'\r\n');
i = 0; %Count number of samples taken
SensorReading = zeros(1,NChannels,'uint16');
disp('Start sampling')
while(true)
SensorReading(1) = fread(Arduino,1,'uint16');
switch SensorReading(1)
case 65279 %Stop measurement
disp('Stop sampling')
break
case 65023 %Stop Matlab
sampling = false;
disp('Stop MATLAB')
break
end
for k = 2:NChannels
SensorReading(k) = fread(Arduino,1,'uint16'); %Read data as integer (2 bytes)
end
Time = fread(Arduino,1,'uint32');%Read time as unsigned integer (4 bytes)
SensorData = double(SensorReading) / 1023 * 5;
plotData = zeros(1,NChannels);
plotData(1) = (SensorData(2) / 6.8) + (SensorData(3) / 21000);
plotData(2) = 24 - (SensorData(3) / 21000 * (21000 + 98000));
plotData(3) = SensorData(1) * 1023 / 5;
plotData(4) = (SensorData(4) - 0.5) * 100;
%Update texfile
fprintf(fileId,'%u',Time);
%Update graph
for k = 1:NChannels
addpoints(h(k),Time,plotData(k))
%Update texfile
fprintf(fileId,'\t%.8f',plotData(k));
end
fprintf(fileId,'\r\n');
i = i + 1;
%xlim([Time-WindowSize,Time])
drawnow update
end
elseif(start_message == 65023)
sampling = false;
disp('Stop MATLAB')
end
end
fclose(Arduino); %End communication with arduino
fclose(fileId); %Close textfile