relacs

Relaxed Electrophysiological Data Acquisition,
Control, and Stimulation

... enjoy your recordings

Comedi Plugins

Interfaces to the Comedi data acquisition library for normal (streaming) analog input and output.

AnalogOutput Plugins:
ComediAnalogOutput:

Interface for accessing analog output of a daq-board via comedi.

*:

Interface for accessing analog output of a daq-board via comedi.

ComediDigitalIO [DigitalIO]

Interface for accessing digital I/O lines of a daq-board via comedi.

ComediNIPFI [Device]

Controlls the PFI pins of a NI M-series daq-board via comedi.

ComediRouting [Device]

Route a signal to a channel of a subdevice.

* []

DigitalIO Plugins:
ComediDigitalIO:

Interface for accessing digital I/O lines of a daq-board via comedi.

Device Plugins:
ComediNIPFI:

Controlls the PFI pins of a NI M-series daq-board via comedi.

ComediRouting:

Route a signal to a channel of a subdevice.

Comedi Plugins

Interfaces to the Comedi data acquisition library for normal (streaming) analog input and output.Comedi (linux control and measurement device interface) provides hardware drivers to many data acquisition boards with a common library interface. The RELACS Comedi plugins access data acquisition boards by means of the Comedi library.

For general information about Comedi and a list of supported devices see http://www.comedi.org

Content

Install comedi

On recent linux distributions comedi modules are already in the Linux kernel. You only need to install the comedilib library. On Debian:

sudo apt-get install libcomedi-dev

On Fedora as root:

dnf install comedilib-devel

and to set the permissions as described in Setting up udev permissions for Comedi device files. If your data acquisition board needs to be calibrated, you also need to install comedi_calibrate. This seems not to be available as a package, so you have to compile it yourself. See Comedi_calibrate for details.

If this does not work (some older versions did not provide some functions needed by the RELACS comedi plugins), or if you require the most recent version of comedi, you should reinstall the comedi drivers and the comedi library from scratch as described in the following sections.

You should install the comedi drivers and library prior to configuring RELACS, so that ./configure can detect the presence of the comedi library. If you install comedi later, simply run ./configure and make again to detect comedi and compile the corresponding plugins for RELACS.

Linux kernel

In order to be able to install the Comedi drivers you need to have a kernel source tree that matches your running kernel image. Either compile your own kernel from scratch (section Compile your own kernel , takes some time), or make sure that you get a matching source tree for your kernel image (section Install sources for an existing kernel).

Install sources for an existing kernel

  1. Get the soure and headers that matches your kernel. On Ubuntu/Debian you get the kernel source and header files matching your running kernel image by executing as root

    cd /usr/src
    apt-get install dpkg-dev
    apt-get install linux-headers-`uname -r`
    apt-get source linux-image-`uname -r`

    If apt-get source tells you "E: You must put some 'source' URIs in your sources.list" then you need to enable the source repositories for the package manager. For this open with your favorite editor /etc/apt/sources.list and the *.list files in /etc/apt/sources.list.d/. Remove the comments in front of the lines that start with "deb-src". Then run

    apt-get update

    to resynchronize the package index files with the new repositories. Now apt-get source should be able to download the kernel sources.

    If apt-get source tells you "E: Unable to find a source package" then check which kernel sources are available:

    apt-cache search linux-source
    uname -r

    choose the one that is closest to your installed kernel (uname -r), install and unpack that one:

    apt-get install linux-source-3.13.0
    tar xf linux-source-3.13.0.tar.bz2
  2. After downloading the kernel source create a link to the kernel source:

    ln -sfn <kernel-source> linux

    where <kernel-source> is the kernel source installed by apt-get, e.g. a directory with a name like linux-source-3.13.0

    Note that often the kernel source directory that you installed with apt-get has a different name than the name of the currently running kernel that uname -r returns. Use ls -rtl to list the directories in the /usr/src folder. The last one is the one that was added by apt-get source. Now you can cd into the kernel source directory

    cd linux
  3. Copy the kernel configuration file of your kernel image to the source:
    cp /boot/config-`uname -r` .config
  4. Make sure the file Module.symvers is also in your source. On Ubuntu/Debian you find it in the corresponding linux-headers directory:
    cp ../linux-headers-`uname -r`/Module.symvers .
  5. Run
    make silentoldconfig
    make prepare
    make scripts
    Hit 'enter' to all questions of make silentoldconfig.

Compile your own kernel

  1. Get a vanilla kernel from http://www.kernel.org, e.g. http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.38.8.tar.bz2, place the tar file in /usr/src and unpack it as root with
    cd /usr/src
    tar xjvf linux-2.6.38.8.tar.bz2
    (replace linux-2.6.38.8 by the directory name of your new kernel source). Create a softlink to your kernel source
    ln -sfn linux-2.6.38.8 linux
    cd linux
    and make sure the make, gcc, libncurses-dev, and zlib1g-dev packages are installed:
    apt-get install make gcc libncurses-dev zlib1g-dev
  2. Reuse your current configuration (replace XXX by the name of your running kernel):
    cp /boot/config-XXX .config
    make oldconfig
    Hit enter to all the (probably many) questions.
  3. Steps 3. to 6. describe the normal way to compile a linux kernel. On Debian systems there is an elegant alternative that is described in step 9. below. First, you configure your kernel by calling
    make menuconfig
    Possible settings you might want to change are:
    • Processor type and features:
      • Select your Subarchitecture Type (PC-Compatible) - this option does not exist for newer kernels
      • Select the right Processor family (you find the name of your processor by looking at /proc/cpuinfo, check "help" to find out, which Processor family you have to select)
      • Select "Preemption Model (Preemptible kernel (Low-Latency Desktop))"
    Leave the configuration dialog by pressing "Exit" until you are asked "Save kernel config?". Select "Yes".
  4. Then compile and install (as root) the kernel
    make && make modules && make modules_install && make install
    This takes quite a while.
  5. Finally you might need to create a RAM disk image file, if make install did not create a /boot/initrd-2.6.38.8 file already:
    cd /boot
    update-initramfs -c -k 2.6.38.8
  6. Check the grub menu whether your new kernel was added.
    • With grub v2 (default since Debian Lenny) simply run
      update-grub
      the output of update-grub should mention your new kernel image. You can configure grub v2 by editing the file /etc/default/grub . To get the boot menu that you need to select the kernel you want to boot you should for example unset the GRUB_HIDDEN_TIMEOUT and set the GRUB_TIMEOUT appropriately in that file. For example:
      #GRUB_HIDDEN_TIMEOUT=0
      #GRUB_HIDDEN_TIMEOUT_QUIET=true
      GRUB_TIMEOUT=10
      Also, if ou do not like that grub puts your rtai kernel into a subsection called 'Previous linux versions', then comment out the following lines in /etc/grub.d/10_linux
      # if [ "$list" ] && ! $in_submenu; then
      # echo "submenu "Previous Linux versions" {"
      # in_submenu=:
      # fi
      After editing run update-grub to apply the changes.
    • With grub v1 (default up-to Debian Lenny) there should be an entry like this in /boot/grub/menu.lst:
      title Debian GNU/Linux, kernel 2.6.28.9
      root (hd0,0)
      kernel /boot/vmlinuz-2.6.28.9 root=/dev/sda1 ro quiet
      initrd /boot/initrd.img-2.6.28.9
  7. Reboot the computer into the new kernel.
    reboot
  8. Install third party kernel modules as well, like for example the ones for your graphics board. For your nvidia graphics board, download the driver from http://www.nvidia.com/Download/index.aspx?lang=en-us. Install it by calling
    sh NVIDIA-Linux-x86_64-190.42-pkg2.run
    If you want to install the nvidia driver for a second kernel, then call
    sh NVIDIA-Linux-x86_64-190.42-pkg2.run -k 2.6.28.9-rtai -K
    (replace 2.6.28.9-rtai by the name of the kernel where you want to install the nvidia driver, i.e. the string 'uname -r' would return if that kernel is running).
  9. On a Debian system there is an alternative to steps 3. - 6. for compiling a linux kernel. See http://wiki.linuxcnc.org/cgi-bin/wiki.pl?Debian_Lenny_Compile_RTAI for more details. Do as root:
    1. Get the kernel-package
      apt-get install kernel-package libncurses-dev
    2. Configure and build the kernel:
      export CONCURRENCY_LEVEL=2 # set here the number of CPUs you have to speed things up!
      make-kpkg clean
      make-kpkg --initrd --revision 1.0 --config menuconfig kernel-image kernel-headers kernel-source
      You can also supply a -j option to tell make how many processes can be run in parallel. See step 3. for hints on the configuration options.
    3. Install the kernel
      cd ..
      dpkg -i linux-image-2.6.38.8_1.0_amd64.deb linux-headers-2.6.38.8_1.0_amd64.deb
    4. continue with step 6.

Comedi

Kernel versions 2.6.30 onwards have an experimental in-tree Comedi implementation in the driver "staging" area. You can check whether it works for you by following the instructions of point 11 below. However, we recommend installing a current version of the comedi drivers.

Here are all commands that you can throw into a shell to completely install comedi:

# install all required packages:
apt-get -y install gcc g++ git autoconf automake libtool bison flex libgsl0-dev libboost-program-options-dev
# download comedi sources:
cd /usr/local/src
git clone https://github.com/Linux-Comedi/comedi.git
git clone https://github.com/Linux-Comedi/comedilib.git
git clone https://github.com/Linux-Comedi/comedi_calibrate.git
rm -r /lib/modules/`uname -r`/kernel/drivers/staging/comedi
# comedi:
cd /usr/local/src/comedi
./autogen.sh
./configure
make -j$(grep -c "^processor" /proc/cpuinfo)
make install
depmod -a
cp include/linux/comedi.h /usr/include/linux/
cp include/linux/comedilib.h /usr/include/linux/
# comedilib:
cd /usr/local/src/comedilib
./autogen.sh
./configure --prefix=/usr --sysconfdir=/etc
make -j$(grep -c "^processor" /proc/cpuinfo)
make install
# comedi_calibrate:
cd /usr/local/src/comedi_calibrate
autoreconf -v -i
./configure --prefix=/usr --sysconfdir=/etc
make
make install
# setup udev permissions:
groupadd --system iocard
echo 'KERNEL=="comedi*", MODE="0660", GROUP="iocard"' > /etc/udev/rules.d/95-comedi.rules
udevadm trigger
usermod $USER -a -G iocard # or: adduser $USER iocard

In the following the necessary steps are described in detail:

  1. Download the current Comedi sources from the Git repository to /usr/local/src as root.
    apt-get install git
    cd /usr/local/src
    git clone https://github.com/Linux-Comedi/comedi.git
    git clone https://github.com/Linux-Comedi/comedilib.git
    git clone https://github.com/Linux-Comedi/comedi_calibrate.git
    Or download the current Git snapshots from http://www.comedi.org/download.html to /usr/local/src and unpack them
    cd /usr/local/src
    tar xzvf comedi-HEAD-*.tar.gz
    tar xzvf comedilib-HEAD-*.tar.gz
    tar xzvf comedi_calibrate-HEAD-*.tar.gz
  2. You may need to install the packages: autoconf, libtool.
    apt-get install autoconf automake libtool
  3. Make sure that you are really running the kernel for which you want to install Comedi! You can check with
    uname -r
  4. Remove the Comedi staging modules to avoid conflicts:
    rm -r /lib/modules/`uname -r`/kernel/drivers/staging/comedi
  5. Change to the comedi directory:
    cd /usr/local/src/comedi
  6. Generate the configuration script by calling
    ./autogen.sh
    You may ignore warnings and reminders.
  7. Configure simply by calling
    ./configure
    in case you want to install Comedi into your currently running kernel. If /usr/src/linux is not your running kernel, then call
    ./configure --with-linuxsrcdir=/usr/src/linux
    with /usr/src/linux replaced by the path to the sources of the kernel for which you want to install Comedi.
  8. Compile
    make -j$(grep -c "^processor" /proc/cpuinfo)
    and install (as root) the comedi kernel modules to your kernel
    make install
    depmod -a
  9. For some strange reason, the include files for kcomedilib linux/comedilib.h and linux/comedi.h are not installed by Comedi to [prefix]/include/linux . These files are needed for compiling the kernel modules that use Comedi. Also, RTAI expects Comedi include files in [prefix]/include/linux. Therefore you need to copy the Comedi include files to [prefix]/include/linux yourself:
    cp include/linux/comedi.h /usr/include/linux/
    cp include/linux/comedilib.h /usr/include/linux/
  10. Check loading the Comedi module:
    modprobe comedi
    If you do not get any error message, then the Comedi kernel modules were successfully compiled and can be loaded into the kernel.
  11. Check autodetection of your data acquisition board:
    udevadm trigger
    ls /dev/comedi*
    In case the ls command shows something like
    /dev/comedi0 /dev/comedi0_subd1 /dev/comedi0_subd12
    /dev/comedi0_subd0 /dev/comedi0_subd11 /dev/comedi0_subd2
    i.e. some comedi* character device files, your board probably was detected. Run
    comedi_board_info /dev/comedi0
    (or comedi_test -t info -f /dev/comedi0) to see more details about your data acquisition board (note: both comedi_board_info and comedi_test are part of comedilib, see below). In case no /dev/comedi* device file shows up or comedi_test reported an error (E: comedi_open("/dev/comedi0"): No such device), your data acquisition board needs to be configured manually, using something like
    comedi_config /dev/comedi0 ni_mio_cs
    that can be put into the /etc/init.d/boot.local file, so that the board is configured during startup of the system. For this you also need to tell comedi to create a /dev/comedi0 device file for you. Add the following line to /etc/modprobe.conf or /etc/modprobe.d/comedi.conf (the latter you have to create if the directory /etc/modprobe.d exists):
    options comedi comedi_num_legacy_minors=4
    See also http://www.comedi.org/doc/install.html#cardconfiguration .

Comedilib

  1. You may need to install the packages: autoconf, libtool, bison, flex.
    apt-get install bison flex
  2. Change to the comedilib directory as root:
    cd /usr/local/src/comedilib
  3. Generate the configuration script by calling
    ./autogen.sh
    You may ignore warnings and reminders.
  4. Configure comedilib to be installed in /usr/lib and /usr/include
    ./configure --prefix=/usr --sysconfdir=/etc
    If you want to have Comedlib to be installed somewhere else, specify another prefix. You may want to read ./configure –help as well. Pay attention to warnings and errors! You might need to install additional software first.
  5. Compile and install (as root)
    make -j$(grep -c "^processor" /proc/cpuinfo)
    make install
  6. In case make fails with this error message:
    libtool: link: gcc -std=gnu99 -I../include -I../include -Wall -g -O2 -o .libs/comedi_test comedi_test-bufconfig.o comedi_test-cmd_1.o comedi_test-cmd_2.o comedi_test-cmd_3.o comedi_test-info.o comedi_test-insn_read.o comedi_test-insn_read_time.o comedi_test-inttrig.o comedi_test-lib.o comedi_test-main.o comedi_test-mmap.o comedi_test-mode0_read.o comedi_test-select.o ../lib/.libs/libcomedi.so -lm
    ../lib/.libs/libcomedi.so: undefined reference to `nearbyint'
    ../lib/.libs/libcomedi.so: undefined reference to `floor'
    collect2: error: ld returned 1 exit status
    make[2]: *** [comedi_test] Error 1
    make[2]: Leaving directory `/usr/local/src/comedilib/testing'
    make[1]: *** [all-recursive] Error 1
    make[1]: Leaving directory `/usr/local/src/comedilib'
    make: *** [all] Error 2
    then add the following line
    libcomedi_la_LIBADD = -lm
    to lib/Makefile.am after the line libcomedi_la_LDFLAGS. Then run
    make clean
    ./autogen.sh
    ./configure --prefix=/usr --sysconfdir=/etc
    make -j$(grep -c "^processor" /proc/cpuinfo)
    make install
  7. You might further test your board using
    comedi_test -t cmd_continuous

Comedi_calibrate

  1. You may need to install the packages: autoconf, libtool, libgsl0-dev, libboost-program-options-dev.
    apt-get install libgsl0-dev libboost-program-options-dev
  2. Change to the comedi_calibrate directory as root:
    cd /usr/local/src/comedi_calibrate
  3. Generate the configuration script by calling
    autoreconf -v -i
    You may ignore warnings and reminders.
  4. Configure
    ./configure --prefix=/usr --sysconfdir=/etc
  5. Compile and install (as root)
    make
    make install

Setting up udev permissions for Comedi device files

Once comedi is installed you also need to make sure that you (the user) have appropriate permissions for accessing the comedi device files. That is you need to be able to have read and write access to the /dev/comedi* files. One elegant way to achieve this to create a new group and tell udev to add the comedi device files to this group.

  1. Login as root.
  2. Create a new group "iocard":
    groupadd --system iocard
  3. Add udev rules to the /etc/udev/rules.d directory:
    echo 'KERNEL=="comedi*", MODE="0660", GROUP="iocard"' > /etc/udev/rules.d/95-comedi.rules
  4. Run
    udevadm trigger
    to apply your new udev rules
  5. Add users to the "iocard" group:
    usermod <username> -a -G iocard # or: adduser <username> iocard
    After the next login, <username> is a member of the iocard group and and has sufficient permissions to access the data acquisition boards through Comedi.
Note
You need to log out and log in first to be member of the iocard group.

Resize Comedi buffer

To set the Comedi internal buffers to 640kB do as root:

comedi_config -r /dev/comedi0
comedi_config --read-buffer 640 --write-buffer 640 /dev/comedi0 ni_pcimio

Maybe this should go in /etc/rc.local or to some udev postprocessing rule.

Calibrating analog input and output

For calibrating analog input and output you should run comedi_calibrate once in a while. comedi_calibrate writes a file to /usr/var/lib/comedi/calibrations that contains the calibration parameter. The ComediAnalogInput and ComediAnalogOutput plugins read this file and apply the calibration.

For hardware calibrated boards (like NI E-Series boards) do

comedi_calibrate --reset --calibrate -f /dev/comedi0

For software calibrated boards (like NI M-Series boards) do

comedi_soft_calibrate -f /dev/comedi0

You may want to read the man page of comedi_calibrate.

Last modified: Wed Jun 13 16:31:33 CEST 2018 Valid HTML 4.01 Transitional