Jump to content

Contributions:BCI2000SimulinkConnector

From BCI2000 Wiki

Synopsis

Location

http://www.bci2000.org/svn/trunk/src/contrib/ExternalLinks/Simulink

Versioning

Author

mellinger@neurotechcenter.org

Source Code Revisions

  • Initial development: 7669
  • Tested under: 7669

Functional Description

The BCI2000 Simulink connector is a means to send signal data from BCI2000 into a Simulink model. It consists of a BCI2000 filter that listens for connections on a TCP port, and a Simulink S-function that connects to that port and reads data from it. As an optimization in case of a machine-local TCP connection, signal data is transmitted through a shared memory block instead of the TCP connection. Still, the TCP connection is necessary to synchronize arrival of new data on the BCI2000 side with consumption on the Simulink side.

Building the Simulink Connector

The BCI2000 Simulink connector consists of two projects: A BCI2000 SignalProcessing module, called SimulinkConnectorSignalProcessing, and a Simullink S-function mex file, called BCI2000OnlineLink.

Building the S-function mex file relies on a Matlab installtion being present, and thus requires enabling through a CMake switch.

In your BCI2000 build folder, execute Configure.sh.bat. Then, navigate to BUILD_CONTRIB and make sure it is checked. Likewise, navigate to BUILD_SIMULINK_MEX_FILES, and make sure it is checked. Then, click "Generate" in the CMake GUI to re-generate the BCI2000 project files (Makefile or Solution file).

In case you have multiple versions of Matlab installed, CMake will pick a random one. To control which Matlab version is used, set the Matlab_ROOT variable to the root directory of the desired version (i.e., the directory that has "Matlab R…" in its name).

Similarly, if you have the Matlab Compiler Runtime (MCR) installed, CMake may pick it but will fail to identify the Simulink include directory, which is not present in the MCR installation. If this happens, set the Matlab_ROOT variable to the root directory of a full Matlab installation, and re-generate project files.

Once built, Simulink related files are located in BCI2000/prog/Simulink, and the BCI2000/batch directory contains a batch file called SimulinkConnector.bat.

Using the Minimal Example

BCI2000 comes with a minimal example Simulink model, containing just the BCI2000OnlineLink block, and a terminator that absorbs the incoming signal.

To try the BCI2000 Simulink Connector, navigate to the BCI2000/prog/Simulink directory using the Matlab file explorer. Then, double-click the MinimalExample.slx Simulink model file. The BCI2000 signal has already been selected for viewing, so the test reduces to the following three steps:

  • Double-click BCI2000/batch/SimulinkConnector.bat in your OSes file explorer. BCI2000 will start and display status information in the system log.
  • In the MinimalExample window, click the arrow in the green circle to run the model. By default, it will run for 10 seconds in real time, reading data from BCI2000.
  • Once the run has terminated, you may view the data log by clicking the data viewer icon right to the green arrow. You will need to expand the "BCI2000" signal, and select channels for viewing.

Adding BCI2000 your own model

To add the BCI2000 block to your own model, open the BCI2000.mdl library in Matlab, and drag-and-drop the BCI2000 block into your model window. Then, connect its output signal as you see fit.

Changing the IP:Port setting

To change the port or the IP address for connecting to BCI2000, double-click the BCI2000 block in Simulink. Under "S-function parameters", you see a single entry, which is by default 'localhost:1905' (take care of the single quotes—without them, Simulink will try to find a variable with name "localhost"). Modify as desired, and close the window.

On the BCI2000 side, the equivalent setting is stored in the "SimulinkConnectorAddress" parameter on the "Connector" tab:

See also

Contributions:BCI2000PresentationLink, Programming Reference:SignalSharingDemo Signal Processing, Programming Reference:MatlabFilter