Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

Setting up 2 counters and 2 timers in matlab

matlab timer counter quadrature

  • Please log in to reply
2 replies to this topic

#1 adran

adran
  • Members
  • 3 posts

Posted 25 March 2015 - 10:50 AM

Hi

 

I'm trying to setup a U3-LV to acquire in MATLAB 2013b 64 bit from 1 quadrature rotary encoder (http://www.farnell.c...ets/1702880.pdf) and process 2 simple counter channels.  This seems to be working ok in the U3 test panel (image of config attached) in that I get values changing on both timer channels when I rotate the rotary encoder and both counter increment with TTL pulses.

 

When I try to port this config to Matlab by adapting the example code I get a not very helpful (to me) exception which "showErrorMessage" doesn't handle:

MATLAB:NET:CLRException:MethodInvoke

Message: Error in the application. Source: LJUDDotNet HelpLink: 

 

My code is below + attached and if anyone can spot what I'm doing wrong it'd be much appreciated!  Also is there any sort of nice matlab wrapper class available to set these configurations in a more compact way?

 

Best wishes

 

Adam

 

===================

 

 
clc %Clear the MATLAB command window
clear %Clear MATLAB variables
 
ljasm = NET.addAssembly('LJUDDotNet'); %Make the UD .NET assembly visible in MATLAB
ljudObj = LabJack.LabJackUD.LJUD;
 
try
    %Read and display the UD version.
    disp(['UD Driver Version = ' num2str(ljudObj.GetDriverVersion())])
    
    %Open the first found LabJack U3.
    [ljerror, ljhandle] = ljudObj.OpenLabJack(LabJack.LabJackUD.DEVICE.U3, LabJack.LabJackUD.CONNECTION.USB, '0', true, 0);
    
    %Start by using the pin_configuration_reset IOType so that all
    %pin assignments are in the factory default condition.
    ljudObj.ePut(ljhandle, LabJack.LabJackUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0);
    
    %First requests to configure the timer and counter.  These will be
    %done with and add/go/get block.
    
    %Set the timer/counter pin offset to 4, which will put the first
    %timer/counter on FIO4.
    ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.TIMER_COUNTER_PIN_OFFSET, 4, 0, 0);
    
    
    %% Configure Timers (used for QUAD input)
    %Enable 2 timers.  Will use FIO4 + FI05
    ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.NUMBER_TIMERS_ENABLED, 2, 0, 0);
    LJ_tmQUAD = ljudObj.StringToConstant('LJ_tmQUAD');
    % Configure common timer clock
    LJ_tc48MHZ_DIV = ljudObj.StringToConstant('LJ_tc48MHZ_DIV');
    ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.TIMER_CLOCK_BASE, LJ_tc48MHZ_DIV, 0, 0);
    %Set the divisor to 48 so the actual timer clock is 1 MHz.
    ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.TIMER_CLOCK_DIVISOR, 48, 0, 0);
    %Configure Timer0 as quad 
    ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_TIMER_MODE, 0, LJ_tmQUAD, 0, 0);
    %Configure Timer1 as quad 
    ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_TIMER_MODE, 1, LJ_tmQUAD, 0, 0);
    %Execute the requests.
    ljudObj.GoOne(ljhandle);
    
    %% Configure Counters (TTL pulse detectors)
    % Enable 2 counters (counter 0 and 1)
    ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_COUNTER_ENABLE, 0, 1, 0, 0);
    ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_COUNTER_ENABLE, 1, 1, 0, 0);
    
    %% Read Timers
    [ljerror, timer0Value] = ljudObj.eGet(ljhandle, LabJack.LabJackUD.IO.GET_TIMER, 0, 0, 0);
    [ljerror, timer1Value] = ljudObj.eGet(ljhandle, LabJack.LabJackUD.IO.GET_TIMER, 1, 0, 0);
    
    %% Read Counters
    [ljerror, counter0Value] = ljudObj.eGet(ljhandle, LabJack.LabJackUD.IO.GET_COUNTER, 0, 0, 0);
    [ljerror, counter1Value] = ljudObj.eGet(ljhandle, LabJack.LabJackUD.IO.GET_COUNTER, 1, 0, 0);
    
catch e
    showErrorMessage(e)
end

Attached Thumbnails

  • U3 test panel.JPG

Attached Files



#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 25 March 2015 - 01:03 PM

I would expect the showErrorMessage function to show the UD driver error code unless it is a .NET or MATLAB related error, but it looks like you are using a clock base with a divisor which makes counter 0 unavailable. The error is most likely happening when you try to enable counter 0. If you need to use both counters, use a clock base without a divisor. For example, change:

ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.TIMER_CLOCK_BASE, LJ_tc48MHZ_DIV, 0, 0);
%Set the divisor to 48 so the actual timer clock is 1 MHz.

ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.TIMER_CLOCK_DIVISOR, 48, 0, 0);
to (like configured in your LJControlPanel picture):
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.TIMER_CLOCK_BASE, LJ_tc48MHZ, 0, 0);

Also, put your "ljudObj.GoOne(ljhandle)" after you enable both your timer and counters.

 

There isn't a MATLAB wrapper class that we provide, but you could look into using the timer/counter easy functions which are more compact:

 

http://labjack.com/s...rs-guide/4.2.21

http://labjack.com/s...rs-guide/4.2.22

 

To see how these functions look in MATLAB, use the methodsview call on the LJUD class after your NET.addAssembly call:

 

methodsview(LabJack.LabJackUD.LJUD)

 



#3 adran

adran
  • Members
  • 3 posts

Posted 26 March 2015 - 03:20 AM

Brilliant thanks - working version attached just in case it's useful to any Matlabbers out there.

 

Adam

Attached Files





Also tagged with one or more of these keywords: matlab, timer, counter, quadrature

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users