Using the BIOPAC MP150 in Python

BIOPAC Systems Inc. is a company that produces machines that can test several physiological processes. These include electroencephalography (EEG), electrooculography (EOG), electromyography (EMG), respiratory function, temperature, and heart rate. In addition, they can be used to test grip force!

One of BIOPAC’s machines is the MP150. After buying this expensive piece of equipment, you can buy add-on modules to test some of the functions listed above. These modules can be clicked into the MP150, which can then transmit data collected from the module to a computer. BIOPAC offers some software to read out the signal that the MP150 transmits, but you can also program custom scripts to do cool stuff with the MP150’s data.

This post will teach you how to write those cool scripts in Python. The demonstrations will rely on the PyGaze toolbox, but communications with the MP150 do not rely on this. So you could use your own favourite experiment package (PyGame or PsychoPy, for example). This example will focus on a dynamometer, which is a module that can measure grip force. It provides a handle that participants can squeeze, which earned it the nickname ‘squeezie’ in our lab. However, as far as I can tell, the presented software should work with any other module too.

DYNAMOMETER IMAGE

Dynamometers were used in Chong et al.’s study on decision making in Parkinson’s patients.

The above image is from Chong et al. (2015), a study that used dynamometers on healthy people, and on Parkinson’s disease patients on and off their medication. They used squeezing the dynamometer as a manipulation of physical effort. They demonstrate that Parkinson’s patients were less willing to invest effort if they could obtain a low reward for it. On dopaminergic medication, patients were willing to invest even more effort than healthy participants when the rewards were high.

MPy150

BIOPAC provides a DLL (a dynamic-link library) that can be used to communicate with the MP150 on Windows (sorry, not other operating systems supported). It’s called mpdev.dll, and you should have gotten it with your system. If not, no worries: you can download it from my GitHub page, along with the other code presented on this page (see the Downloads section below).

Setting up

The first thing to do, is to import the libraries that you need. For this guide, I will assume that you have PyGaze installed. Create a new folder, and within that folder two new scripts. Call the first constants.py, and the second experiment.py. Then, download the MPy150 zip archive from GitHub. Unzip it, and copy libmpdev.py and mpdev.dll to the folder that contains constants.py and experiment.py

Now that you have the file structure correct, it’s time to start doing some programming. Open constants.py, and define the display type (the underlying package that PyGaze uses for visuals) and the display size (your monitor’s resolution):

Now open experiment.py, and import the classes that you are going to use (these will automatically find and import your constants.py file):

The final step is to initialise a few instances: MPy150 to collect data from the MP150, a Screen for drawing text on, and the Display for showing your Screen:

Getting Samples

If you want to make what is shown of the Screen depend on the input of your MP150, for example if you want to visualise grip force or EEG signal, you will need to actively obtain samples from the MP150. A sample is a single data point, that can contain information on up to sixteen channels (one channel per module). MPy150 is currently configured to only report the first three, but you can easily change this in the source code. (Change the 0s to 1s in line 125.)

To get a sample from the MP150, you can use the sample method. In this example, you will get samples from the MP150 and display their values on the computer monitor. This means you will have to continuously get new samples, and update the display. The easiest way of implementing this in code, is by using a while loop. You can make such a loop go on forever, or until you press a key. The idea here is to make it run until you press Escape. The code looks like this:

This while loop will run until you press Escape. It will show the sample’s value in white letters (on the centre of the Screen, the default text position). And it will update the background to be brighter red if you squeeze the MP150’s dynamometer harder.

Closing Down

After using the MPy150 instance, you need to close it to shut down the connection with the MP150. You also need to close the Display, which will close down the experiment. The following code will do this:

And that’s it! Your whole experiment finished! For another example, that shows you how to record from the MP150 to a log file, see this MPy150 test script on my GitHub.

Downloads

The MPy150 class is completely free and open source. It is hosted on my GitHub page, from where you can also download it. The download is an archive that includes libmpdev.py and mpdev.dll. To use them, unzip the archive and place the files in your PyGaze directory, or in the same directory as your script.

After placing libmpdev.py and mpdev.dll in your PyGaze directory, you can import MPy150 with the following code:

If you placed libmpdev.py and mpdev.dll in the directory of your script, include the following:

5 Comments:

  1. What units are the samples? I assume kg?

    • That’s a very good question! I’m afraid it’s not kilograms, but more-or-less arbitrary units (I think it’s the voltage output from the module). You’ll have to calibrate your system to know what output values correspond to what pressure, for example by placing a known weight on the handle and reading out the difference in the BIOPAC’s output.

  2. It doesn’t seem to work for channels 4-16. I changed the 0s to 1s in the channel list, but there seems to be other places that it is hardcoded to get only 3 samples

  3. I’m a newbie here. How does the code changes if used in Psychopy?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">