Jump to content

How to configure timed loop?


Recommended Posts

Hi,

I have built a VI to log mechanical tensile tests. At first I used while loops to make the reading and appended all values to an array that is written to a text file when user push STOP.

I realised that the array becomes very large very fast so I got the idea to use a timed loop that appends every value during operation, it will not fill up the memory as in the case above.

But I don't really understand how the timed loop works, so please take a look at the VI and tell me how to config it.

Download File:post-6790-1163664241.vi

Link to comment
...But I don't really understand how the timed loop works, so please take a look at the VI and tell me how to config it.

Hi,

Some comments about this GUI:

1. Using a timed loop with period set to 1ms, containing event structures with timeout set to 10ms, makes no sense. In this case the Event structures will probably time out in most cases, so the actual loop period will be above 20ms.

2. You are using local variables to create your log-data, and this could lead to race conditions, i.e. your log data can contain values before OR after the actual value update (and you really don't know which). Try to avoid local variables and go with the flow...

3. The X- and Y-scale properties are written in every iteration, even if no change has been made.

Suggestions:

1. Put the logging and all User inputs (except maybe the STOP) in a separate loop and send data between the loops using queues.

2. Avoid local variables if you can, you might not get what you expect due to race conditions.

3. Try to make the code more compact, as it is right now I have to scroll back and forth (using a resolution of 1400*1050).

4. You should also consider to fetch more than one sample at the time from the DAQmx (by setting the "number of samples per channel" to a positive value), this way you can get the loop rate down, but still acquire samples at the correect rate (100Hz). The current setup will return all samples gathered so far. If you set # of channels to 10, you can actually get rid of the timed loop and still have a loop rate at 10Hz.

I'm sorry I didn't answer you question regarding the setup of the timed loop, but I hope I have given you an alternative approach.

The reason is that a timed loop doesn't really add something in this case, because the timing can be controlled through the DAQmx calls. Timed loops are useful when you want to get good SW timing, but in this case you have the timing of the DAQ boards to work with.

/J

Link to comment

OK, very good help... Thanks!

But I have troubles to get the event handler structures to work properly. It seems like they only "runs" just after the stop button is pushed.

I have removed the timed loop, and local variables.

The DAQmx read VI is set to read 10 samples at each loop iteration. The task is set to sample in continuous mode with a rate of 100 Hz and samples to read is set to 100. This do result in that 10 samples/sec in log file (it is sufficient for now). But why doesn't 100 samples get written to the log file?

There is a 1 ms delay in each while loop.

Download File:post-6790-1163758270.vi

Link to comment
...I have removed the timed loop, and local variables....

The DAQmx read VI is set to read 10 samples at each loop iteration. The task is set to sample in continuous mode with a rate of 100 Hz and samples to read is set to 100. This do result in that 10 samples/sec in log file (it is sufficient for now). But why doesn't 100 samples get written to the log file?...

The attachment is password protected :blink:

/J

Link to comment
The attachment is password protected :blink:

It is supplied on the frontpanel (why this method Johan)

But why do you have 2 event structures in one loop?

The time-out will make sure the loop will finish but there will be a pile up of events (eg. one event loop gets every 5 ms. an event but must wait 10 ms on the other event case)

And the right-event case looks like a set off radio-buttons, use the native form of this. Or use an array, have a value change event and XOR the old and new value and write this a value property (NOT signalling)

The same goes for the left.

For a quick start start the task before the while loop!

Now in continious mode the number of samples means almost nothing, it is used for setting the buffer size (see DAQmx help).

Every run 10 samples are written.

Could you show the text file created?

Ton

Link to comment
It is supplied on the frontpanel (why this method Johan)

Ooops, need to put on my glasses :wacko:

Now in continious mode the number of samples means almost nothing, it is used for setting the buffer size (see DAQmx help).

Every run 10 samples are written.

If you initialize DAQmx beforehand (setting buffers, mode etc.), and then call DAQmx read with # of samples specifed. Then I belive that DAQmx read will wait until the buffer contains the requested number of samples or until timeout.

The DAQmx read VI is set to read 10 samples at each loop iteration. The task is set to sample in continuous mode with a rate of 100 Hz and samples to read is set to 100. This do result in that 10 samples/sec in log file (it is sufficient for now). But why doesn't 100 samples get written to the log file?

I actually don't see where you specified the acquisition mode, rate, buffer size etc (MAX?).

You really should start the task before entering the acquisition loop.

The reason why the upper loop has to be finished before entering the lower loop, is that the "Stop Program" wire, is going out of the upper loop and into the lower loop, and this actually means that the event structures will only be read once (as you noticed). Instead read the "Stop Program" button in an Event structure, and remove the wire going from the upper to the lower loop. As Ton said, only use one event structure for all your events.

/J

Link to comment
New unprotected version above... ;)

I was just too tired...

The reason you only get 10 updates per second in your file is that 10 samples takes 100ms to get (assuming 100Hz sample rate).

Then you only update the file once for all these 10 samples, i.e. 10 times per second.

Maybe you are treating all 10 samples as one, instread of 10? I don't know why you use the "Convert To Dynamic data" node, when you could act on the 2D array of doubles directly.

/J

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.