-
Notifications
You must be signed in to change notification settings - Fork 5
/
osc_server_muse.m
194 lines (168 loc) · 5.77 KB
/
osc_server_muse.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
%OSC_SERVER_MUSE
% 1. Executes the application muse-io.exe (Installed with the Muse SDK)
% https://sites.google.com/a/interaxon.ca/muse-developer-site/home
% 2. Reads the OSC packages from the muse-io.exe
% 3. Shows the configuration of the Muse headband
% 4. Plots EEG and acceleration data in online
%
% More than one Muse headband can be used at the same time, each one will be
% managed by a different instance of this script. In order to do this
% change the TCP port (variable port in the code) to have a
% different TCP port for each Muse.
%
% Raymundo Cassani
% November 2014
% %%%%%%%%%%%%%%%%%%%%
clear all;
close all;
% Check if the Instrumentation Control Toolbox is present
tbName = 'Instrument Control Toolbox';
verInfo = ver;
tbFlag = any(strcmp(tbName, {verInfo.Name}));
% Obtain release year
release = sscanf(version('-release'),'%d%s');
releaseYear = release(1);
% OCS Paths
% Note that these paths dependes of the muse-io.exe version, in this case
% V3.4.0
oscPathV3_4_0{1,1} = '/muse/eeg';
oscPathV3_4_0{1,2} = 'fff';
oscPathV3_4_0{2,1} = '/muse/acc';
oscPathV3_4_0{2,2} = 'fff';
oscPathV3_4_0{3,1} = '/muse/config';
oscPathV3_4_0{3,2} = 's';
% Server parameters
% These parameters configure where the data fom muse-io.exe will be sent
ip = '0.0.0.0'; %Localhost
port = 5000; %TCP Port (default port is 5000)
timeoutSec = 10; %In seconds
% Starts muse-io.exe
% Preset 14 set the Muse headset to deliver 4 channels:
% {'TP9'; 'FP1'; 'FP2'; 'TP10'}
system(['start "Running: muse-io.exe --preset 14" "C:\Program Files (x86)\Muse\muse-io.exe" --preset 14 --osc osc.tcp://localhost:' num2str(port)]);
% This flag (tcpFlag) indicates if the TCP connection will be done using the
% TCP/IP objects(tcpFlag == true) Instrumentation Control Toolbox and Relase >2011 are needed;
% OR using
% Java ServerSocker (tcpFlag == false)
tcpFlag = tbFlag && releaseYear > 2011;
if tcpFlag
tcpServer=tcpip(ip, port, 'NetworkRole', 'server');
tcpServer.InputBufferSize = 5000;
tcpServer.Timeout = timeoutSec;
%Open a connection. This will not return until a connection is received.
fopen(tcpServer);
else
import java.net.ServerSocket
import java.net.InetSocketAddress
import java.io.*
serverISA = InetSocketAddress(ip, port);
serverSSocket = ServerSocket(port,0,serverISA.getAddress);
serverSSocket.setSoTimeout(timeoutSec*1000);
%Open a connection. This will not return until a connection is received.
serverSocket = serverSSocket.accept;
serverInputStream = serverSocket.getInputStream();
serverDIS = DataInputStream(serverInputStream);
end
%Size of buffers to Read EEG ACC
%As the EEG output sampling frequency is 220Hz
fse = 220;
fsa = 50;
secBuffer = 10;
eegName = {'TP9'; 'FP1'; 'FP2'; 'TP10'};
eegBuffer = zeros([fse*secBuffer,numel(eegName)]);
accName = {'F/B'; 'U/D'; 'R/L'};
accBuffer = zeros([fsa*secBuffer,numel(accName)]);
eegCounter = 0;
plot1 = true;
conf1 = true;
figure()
while true
if tcpFlag
try %Catch Matlab error
a = fread(tcpServer, 4); %How large is the package (# bytes)
catch err;
break
end
bytesToRead = double(swapbytes(typecast(uint8(a),'int32')));
try %Catch Matlab error
bytesData = fread(tcpServer,bytesToRead);
catch err;
break
end
else %Utilising Java Classes
for ind = 1:4 %How large is the package (# bytes)
try
a(ind) = DISread(serverDIS,'uint8');
catch e %catch "Java exception occurred"
break
end
end
bytesToRead = double(swapbytes(typecast(uint8(a),'int32')));
bytesData = zeros(bytesToRead,1);
for ind = 1:bytesToRead
try
bytesData(ind) = DISread(serverDIS,'uint8');
catch e; %catch "Java exception occurred"
break
end
end
if ind ~= bytesToRead
break
end
end
[oscPath, oscTag, oscData] = splitOscMessage(bytesData);
data = oscFormat(oscTag,oscData);
switch oscPath
case oscPathV3_4_0{1,1} %The message contains EEG data
eegBuffer = [eegBuffer(2:end, :); cell2mat(data)];
eegCounter = eegCounter+1;
case oscPathV3_4_0{2,1} %The message contains Acceleration data
accBuffer = [accBuffer(2:end, :); cell2mat(data)];
case oscPathV3_4_0{3,1} %The message contains Configuration data
%Show configuration
if conf1
conf = data{1}(2:end-2);
Ctmp = textscan(conf,'%s','delimiter',',');
C = strrep( Ctmp{1}, '"','');
msgbox(C,'Muse Configuration' );
conf1 = false;
end
otherwise
%Do nothing
%More cases can be added to treat other paths
end
%Plot every 44 EEG samples approx 200ms
if eegCounter == 44
if plot1
subplot(2,1,1);
time = 0:1/fse:secBuffer-1/fse;
h1 = plot(time,eegBuffer);
legend(eegName, 'Location','EastOutside');
xlabel('Time (s)')
ylabel('Voltage (uV)')
subplot(2,1,2);
time = 0:1/fsa:secBuffer-1/fsa;
h2= plot(time,accBuffer);
xlabel('Time (s)')
ylabel('Acceleration (mG)')
legend(h2, accName, 'Location','EastOutside');
plot1 = false;
else
cell1 = (num2cell(eegBuffer,1))';
set(h1,{'ydata'},cell1);
cell2 = (num2cell(accBuffer,1))';
set(h2,{'ydata'},cell2);
end
drawnow;
eegCounter = 0;
end % if eegCounter
end %while true
if tcpFlag
fclose(tcpServer);
delete(tcpServer);
else
serverSocket.close();
serverSSocket.close();
end
display('End of Acquisition');