LabJack U3 and Matlab - Sinusoidal signal | LabJack
 

LabJack U3 and Matlab - Sinusoidal signal

9 posts / 0 new
Last post
Victor
victorpbr's picture
LabJack U3 and Matlab - Sinusoidal signal

Hello,

I'm trying to use LabJack to create a sinusoidal signal that runs for long periods of time.

What I've done so far was creating a vector with the voltages I want at each point, and then Adding the request for the DAC output each of these voltages along with a request of Wait time. I used a for loop to add the requests, and then send the command to execute all the requests. I all worked fine

However, I need to run it for long periods of time (say 3-5 hours) and I'm not certain if adding requests for each moment of time and then executing this is the best way to do it, as it takes a long time to add all the requests and I dont know if the hardware has enough memory to deal with all this.

I thought of using a while loop that adds the request and execute at each iteration, however this imposes some 'delays' due to the time Matlab take between each iteration.

I have a LJ tick a LJ U3 HV and LV at the laboratory I'm working. Can somebody guive me some guidance regarding this matter?

In few words,I want a way to generate a sinosoidal signal that is able to run for long periods of time using matlab.

Thank you for the attention

LabJack Support
labjack support's picture
Check out the waveform

Check out the waveform generation app note if you have not seen it already:

https://labjack.com/support/app-notes/waveform-generation

What update rate do you require?  That is, how fast do you need to do each update of the DAC?

Victor
victorpbr's picture
I've read the link, it was

I've read the link, it was really helpfull. However I still have some questions.

The update rate I need Ideally, as high as possible, at least 10 Hz, as I'm using it to control a servo motor and I need the movement as smooth as possible, so if there is a way to get higher update rates would be great. I also need it to run continuously for long periods of time.

On the app note says that using the DAC port has a much slower update rate than using the digital output. I have in hands also a LJTickDAC, do you think using it I would be able achieve higher update rates? If so, how would I implement it?

My final goal would be to achieve a signal that is kept constant for some time and then begin to change sinusoidally. 

LabJack Support
labjack support's picture
10 Hz is no problem for the

10 Hz is no problem for the "Software Timing ..." technique described on the Waveform Generation app note.  You just make a loop in Matlab that iterates every 100ms, and in that loop call eDAC to update your DAC.  If you need more Matlab related help about that let us know.

The update rate of a DO or DAC is about the same (~0.6ms) per Section 3.1.  What's different is how quickly the output reacts changes after the U3 gets the new command.  If you change a DO from low to high, it takes just nanoseconds to rise from 0V to 3.3V.  The DAC lines, however have filters that limit their bandwidth to about 16 Hz which corresponds to a time constant of about 62ms.  That means that if you command the DAC to change from 0V to 5V, it will take it about 62ms for the voltage to get 63% of the way to the new value (1 time constant = 63%, 2 time constants = 87%, 3 time constants = 95%, and 5 time constants = 99.3%).

Since you are only updating the DAC at 10Hz, I'm guessing your output frequencies are 1Hz or less and the 16Hz bandwidth of the DACs will be fine, but if you want to use the LJTick-DAC that is no problem as that just takes a little longer to update than a DO or DAC.

Victor
victorpbr's picture
Thank you for your reply.

Thank you for your reply.

I'm very new to this hardware so I would apreciate the Matlab Help. I'm currently doing a for loop which add the requests for the output voltage, followed by a wait time. And after all the requests have been added through the for loop I execute them with the GoOne command. Is that the correct aproach?

The problem with this approach is that I have limitation on the amount of request I can send to the hardware before executing (I'm assuming that). So for long periods of running time this would not be suitable. Ideally my Output update rate would be of at least 10 Hz (not 1 Hz) , so what would be the best way to code that?

I thought of something like

while true

add set DAC voltage X

add wait X

Go

end

However I may have some alterations due to the time Matlab would take to run each iteration. Is there any better way?

LabJack Support
labjack support's picture
"I'm currently doing a for

"I'm currently doing a for loop which add the requests for the output voltage, followed by a wait time. And after all the requests have been added through the for loop I execute them with the GoOne command. Is that the correct aproach?"

I'm assuming you mean something like:

while X = voltages
    add set DAC voltage X
    add wait 100ms
end
GoOne

The GoOne call will wait for all the AddRequest operations to be performed on the U3, including the waits/delays. Note that a Feedback command packet can only fit 64 bytes of data, so if the requests exceed that amount the UD library it will split out the requests across multiple command packets.

For a 10 Hz update rate, we recommend trying out the software delay method:

while X = voltage
    add set DAC voltage X
    GoOne
    pause(.100)
end

The method you provided pseudocode for is similar though you have the U3 hardware do the delay, but for a 100 ms delay the software delay should be adequate. Keep in mind the USB command/response delays, which are documented here:

https://labjack.com/support/datasheets/u3/operation/command-response

If you are updating the DAC once per GoOne call, refer to the delay when "# AIN" is 0.

Are there other MATLAB operations that you are performing in your loop?

Victor
victorpbr's picture
Ok, I understand.

Ok, I understand.

However, I think that using software delay would be far more unreliable than using hardware delay as for each iteration I'd have to take into account the time took to add the request execute it, and the internal time of matlab loop. And Ideally I'd like to have the highest update rate possible, this 10Hz value is just a baseline.

You said that I can fit up to 64 bytes of data using the U3, is this amount higher on other models? Also, how much data a set DAC and WAIT consume? In other words, how many "packets" of set DAC and WAIT can I send to the U3 before sending the Go ?

Victor
victorpbr's picture
Yes,

Yes,

Currently what I'm doing, is creating a long vector, say

time=1:0.1:3600;  Time sampled at 10Hz for one hour

Voltage=Constant+sin(time)

for k=1:length(Voltage)

add Request set DAC Voltage(k)

add Request Wait 0.1 s

end

so this adds all the requests, and then after that I send GoOne, so my code runs without any software delay. However I'm constrained to a maximum number of requests I can send to the LabJack before executing the command as it has only 64 bit memory. Also as you said I cant get any output frequencies higher than 16 Hz on the U3. 

With that in mind I have a few questions.

1- What is the maximum number os instructions I can send to the U3 before sending the GoOne (assuming I'm doing exactly as stated above) ,so what would be my maximum running time when running a 10 Hz sinusoidal signal.

2- Does the LabJack T7 gives me more memory and/or a higher cutoff frequency than 16 Hz?

3- Assuming I'd use software delay on the while loop as an alternative to  hardware change, would not be better to use a precision timer to take into account the time spend by the add requests and Go commands? something like this:

sampling_time=0.1;

time_elapsed=1;

tic

while true

if time_elasped>=0.1

add Request set voltage

Go one

tic

end

time_elapsed=toc;

end

LabJack Support
labjack support's picture
1 and "You said that I can

1 and "You said that I can fit up to 64 bytes of data using the U3, is this amount higher on other models? Also, how much data a set DAC and WAIT consume? In other words, how many "packets" of set DAC and WAIT can I send to the U3 before sending the Go ?":

This is the common USB endpoint size (64 bytes) on our devices. On the T7, over Ethernet the max. packet size is 1040 bytes.

For the 64 byte Feedback command on the U3, 57 bytes are for the commands. Setting DAC0 or 1 (16-bit) takes 3 bytes and the wait/delay is typically 4 bytes, so 8 DAC updates and 8 delays in one Feedback command/response packets.

If using the LJTick-DAC on the U3 for your analog output it is recommended to use software timing for the delay. Each LJTick-DAC update (DACA and DACB) requires one command/response packets, and cannot be combined in the same command packet as the hardware delay. This is due to the hardware delay being done in the Feedback command/response, and the LJTick-DAC update uses the I2C command/response.

2. This is partially answered above with the 1040 byte size over Ethernet. Note the T7 uses Modbus for communications. Hardware delays, LJTick-DAC and DAC0/1 commands can be performed in one command packet. Someone will respond back on the "higher cutoff frequency than 16 Hz" part of your question.

3. Yes, for best software timing precision you should account for the time of the LabJack delays (the GoOne call). You can do it the way you are doing it. You can also look into using the MATLAB Timer object which could update the the voltage every 100 milliseconds.

One potential issue I see in your code is that you are not resetting your start time (tic) after going over 100 milliseconds, so consider:

sampling_time=0.1;
time_elapsed=1;

%The first start time
tic
while true
    if time_elasped>=0.1
        %Reset the start time for next 100 millisecond timer
        tic
        add Request set voltage
        Go one
        tic
    end
    time_elapsed=toc;
end