Skip to Navigation

MATLAB Examples (Windows UD)

MATLAB drivers and examples version 1.32. See the PDF included in the download zip. Tested on MATLAB 7.0, but should work with 6.5 or newer.

This should work for most 32-bit versions of MATLAB. Due to how MATLAB handles calls to .dlls, additional steps are needed for work with 64-bit MATLAB. In addition to needing the 64-bit LabJackUD.dll you must also have a set of compiler tools to call the functions.  

See this for an example of how to set that up with MATLAB 7.7

For MATLAB R2010a you can get more information here for 32-bit versions and here for 64-bit versions.

For MATLAB R2011b you can get more information here for 32-bit versions and here for 64-bit versions.

File attachment: 
tags:

Comments

#1

Hello,

Could you please help me with Labjack U6 access in Matlab Linux?

I found this tread, which is about Matlab and Mac, which only creates access

through the lower part of the driver (exodriver):

http://forums.labjack.com/index.php?showtopic=5068

It is pretty helpfull, but i also want the higher level functions like in the u6.c.

For this i compiled the u6.c to a library (.so) and loaded that one into Matlab.

Then i am able to e.g. change digital 0 out from low to high with the following Matlab call:

calllib( 'libu6', 'eDO', handle, 0, 1 );

or read the state of digital 1 input with:

calllib( 'libu6', 'eDI', handle, 1, state );

However if i want to use the analog stuf, but then i need to get the calibration values first.

E.g.

calibrationinfo.prodID = 0;

 

calibrationinfo.hiRes = 0;

calibrationinfo.ccConstants = zeros(1,40);

if( calllib( 'libu6', 'getCalibrationInfo', handle, calibrationinfo ) ~= 0 )

   error( 'problem with getting calibrationinfo' );

end    

But whatever i try the cells in the calibrationinfo struct stay empty.

 

So there is something going wrong with passing the calibrationinfo struct

back to the Matlab environment.

Can you help me out?

Regards,

Andre

 

 

#2

Try using libstruct to create the struct and see if that helps.  Here's the libstruct and calllib documentation (provides libstruct examples):

http://www.mathworks.com/help/techdoc/ref/libstruct.html

http://www.mathworks.com/help/techdoc/ref/calllib.html

#3

Hi all,

Here an example Matlab script for linux using a compiled version of the u6.c as shared library to get access to the high level functions like eAIN and eDAC.

Have fun

Andre

 

 

% Andre, March 27, 2012

% Version 1.2

% Tested on 32 bit Ubuntu 10.10 Linux

% Tested with MatLab 7.10.0.499 (R2010a)

 

% Use the higher level u6 functions, like eAIN and eDAC, with ONE LabJack U6.

 

% Loading the u6 library generates quite some warnings, but it looks like

% everything works just fine.

 

clear all;

ljud_Constants;

 

my_reload = 0;

if my_reload ~= 0

   disp( 'unload libraries' );

   unloadlibrary('liblabjackusb');

   unloadlibrary('libu6');

end

 

% Load exodriver, which contains low level functions like read and write

if ~ libisloaded('liblabjackusb')

   disp( 'load labjack library' );

 

   header = '/usr/local/include/labjackusb.h';

   ljlib = '/usr/local/lib/liblabjackusb.so';

   [notfound,warnings]=loadlibrary(ljlib, header);

end

 

% Load u6 library, which contains high level functions like eAIN and eDAC

if ~ libisloaded('libu6')

   disp( 'load u6 library' );

 

   % Created shared libary with the following command:

   % gcc -Wall -g -fPIC -shared /usr/local/lib/liblabjackusb.so -Wl,-soname,libu6.so -o libu6.so u6.c

   u6header = '/home/andre/matlab/u6.h';

   u6ljlib = '/home/andre/matlab/libu6.so';

   [notfound,warnings]=loadlibrary(u6ljlib, u6header);

   % Andre: this loadlibary gives a lot of WARNINGS!!

   % disp( warnings );

end

 

% Show available functions with arguments of both libraries:

% libfunctionsview liblabjackusb

% libfunctionsview libu6

 

% Check LabJack library version:

if calllib( 'liblabjackusb', 'LJUSB_GetLibraryVersion' ) ~= 2.0

   warning( 'This implementation is only tested with library version 2.0' );

end

 

% Check if there is exactly ONE LabJack attached:

if calllib( 'liblabjackusb', 'LJUSB_GetDevCount', LJ_dtU6 ) ~= 1

   error( 'There should be exactly ONE LabJack U6 device attached' );

end

 

% Open the device and verify the returned handle:

handle = calllib( 'liblabjackusb', 'LJUSB_OpenDevice', 1, 0, LJ_dtU6 );

[handle_valid, tmp] = calllib( 'liblabjackusb', 'LJUSB_IsHandleValid', handle );

if ~handle_valid error( 'LabJack handle is invalid' ); end

 

% Get calibration information, required for the ADC and DAC:

[state, tmp, cali] = calllib( 'libu6', 'getCalibrationInfo', handle, [] );

if state ~= 0 error( 'Cannot get calibration info' ); end

 

% Check if the calibration struct contains appropriate data:

if cali.prodID ~= LJ_dtU6 error( 'Calibration struct is invalid' ); end

 

% Get ADC value from AIN0 to AIN14 and Labjack temperature:

for chan=1:15,

   [state, tmp, tmp2, value(chan)] = calllib( 'libu6', 'eAIN', handle, cali, chan-1, 15, 0, LJ_rgBIP10V, 0, 0, 0, 0, 0 );

   if state ~= 0 error( 'Problem getting eAIN' ); end

end

 

fprintf( 'LabJack temperature: %.2fC\n', value(15)-273.15 );

 

fprintf( 'ADC val:  ' );

for chan=1:14,

   fprintf( ' %4.2f', value(chan) );

end

fprintf( '\n' );

 

% Now determine the mean, maximum and minimum for each channel for a number of samples

measure = zeros( 100, 6 ); % number of samples, number of channels

fprintf( 'Collect %d samples for %d ADC inputs (interleaved)\n', size(measure,1), size(measure,2) );

for ii = 1:size(measure,1),

   for chan = 1:size(measure,2),

      [state, tmp, tmp2, measure(ii,chan)] = calllib( 'libu6', 'eAIN', handle, cali, chan-1, 15, 0, LJ_rgBIP10V, 0, 0, 0, 0, 0 );

      if state ~= 0 error( 'Problem getting eAIN' ); end

   end

end

 

fprintf( 'ADC mean: ' );

for chan = 1:size(measure,2),

   fprintf( ' %4.2f', mean(measure( :, chan) ) );

end

fprintf( '\n' );

 

fprintf( 'ADC max:  ' );

for chan = 1:size(measure,2),

   fprintf( ' %4.2f', max(measure( :, chan) ) );

end

fprintf( '\n' );

 

fprintf( 'ADC min:  ' );

for chan = 1:size(measure,2),

   fprintf( ' %4.2f', min(measure( :, chan) ) );

end

fprintf( '\n' );

 

plot(measure);

title( '\bf{\it{LabJack U6 ADC Measurement}}' );

 

legend_str = {};

for chan = 1:size(measure,2),

   legend_str( chan ) = {['AIN ', num2str( chan ) ]};

end

 

legend( legend_str );

xlabel( 'Time [samples]' );

ylabel( 'Amplitude [V]' );

grid on;

 

% Test the DAC by setting 2.55V on DAC 0 and 1.80V on DAC 1

[state, tmp, tmp2] = calllib( 'libu6', 'eDAC', handle, cali, 0, 2.55, 0, 0, 0 );

if state ~= 0 error( 'Problem setting eDAC' ); end

[state, tmp, tmp2] = calllib( 'libu6', 'eDAC', handle, cali, 1, 1.80, 0, 0, 0 );

if state ~= 0 error( 'Problem setting eDAC' ); end

 

% Digital out FIO, set FIO 0 to high, set FIO 2 to low

[state, tmp] = calllib( 'libu6', 'eDO', handle, 0, 1 );

if state ~= 0 error( 'Problem getting eD0' ); end

 

[state, tmp] = calllib( 'libu6', 'eDO', handle, 2, 0 );

if state ~= 0 error( 'Problem getting eD0' ); end

 

% Digital in FIO, get FIO 1 (should be high) and FIO 3 (should be low)

% Asume FIO 0 is connected to FIO 1 and FIO 2 is connected with FIO 3

[state, tmp, fio1] = calllib( 'libu6', 'eDI', handle, 1, 0 );

if state ~= 0 error( 'Problem getting eDI' ); end

if fio1 ~= 1 warning( 'Should be high if connected to FIO 0' ); end

 

[state, tmp, fio3] = calllib( 'libu6', 'eDI', handle, 3, 0 );

if state ~= 0 error( 'Problem with getting eDI' ); end

if fio3 ~= 0 warning( 'Should be high if connected to FIO 2' ); end

 

% All done, close LabJack:

calllib( 'liblabjackusb', 'LJUSB_CloseDevice', handle );

 

fprintf( 'All done!\n' );

 

#4

Hi thanks for your answer,

I could pass an empty array as input, and get the calibration struct

from one of the return values.

See example .m code below:

% Get calibration information, required for the ADC and DAC:

 

[state, tmp, cali] = calllib( 'libu6', 'getCalibrationInfo', handle, [] );

% Get ADC value from AIN0:

[state, tmp, tmp2, value] = calllib( 'libu6', 'eAIN', handle, cali, 0, 15, 0, LJ_rgBIP10V, 0, 0, 0, 0, 0 );

Except for a bunch of warnings when loading the u6 library in Matlab, everything

works amazingly.

Thanks again,

Andre

 

#5

I noticed in the U3_Simple_Logger.m file that you don't preinitialize the size of the array for Error, AINX, and FIOX.  This will dramatically decrease the speed of data acquisition as the array gets larger.  Since you are already requesting the 'time' and 'dt' variables, the following lines should be added at the beginning of the program to increase program speed for longer tests.

  • Error(1,1:time/dt)=0;
  • AIN0(1,1:time/dt)=-100;
  • AIN1(1,1:time/dt)=-100;
  • AIN2(1,1:time/dt)=-100;
  • AIN3(1,1:time/dt)=-100;
  • FIO0(1,1:time/dt)=-100;
  • FIO1(1,1:time/dt)=-100;

It may be desirable to initialize values to -100 in case samples are missed (these values could get filtered later).

- A concerned citizen with Matlab expertise

 

#6

Thank you for your suggestion and we will be sure to fix that in the examples as soon as possible.

 

-- Labjack Support