LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Incrementing Array pointers

Hello,

 

I am recording position data on particular events. I want to store these position values in an array.

 

I am trying to store these values in an array one after another. I want the pointer to the array to automatically increment after the previous position value starting from 1 has been written to the array.

 

How do you automatically increment the pointer?

0 Kudos
Message 1 of 13
(3,895 Views)
This depends on how you are building your pointer array.  Are you using the build array function, the replace array subset function or are you using autoindexing from a loop?  If you post a snippet of code we can help you out better.
Message 2 of 13
(3,890 Views)

Let's assume you are doing this in a FOR loop.

 

If you only need the array after all points have been recorded, you can create the array by autoindexing at the right loop boundary. You'll get one element per iteration in the order they are generated.

 

If you need to have access to the array data while the loop is still executing, you can built it in a shift register initialized with an empty array.

 

If the array is large, you should initialize the shift register with the correctly sized array and then replace values as they are generated using "replace array subset" with the index wired to the iteration terminal. This will avoid memory allocation overhead.

 

Once we see your code, we can give more specific advice. 🙂

Message Edited by altenbach on 11-18-2008 09:47 AM
Message 3 of 13
(3,879 Views)

I have attached a jpeg file with the code I am trying to do.

 

Position Feedback is the data which I want to record in an array everytime the Detection Trigger is true for the case structure

 

I have initialized the array outside the case structure and want to increment the array pointer everytime the detection trigger is true so that position data is saved sequentially in the array.

 

 

 

0 Kudos
Message 4 of 13
(3,855 Views)
  • Start with an empty array and use built array.
  • Built in in a shift register or feedback node.
  • No need to keep track of indices! 
  • Why in the world would you go from an array to a cluster and then unbundle the elements???? Use index array resized to the desired number of elements (no need to wire indices, no need to set cluster size, etc.)
  • Why you use so many indicators for the elements?
  • Just us an array indicator and resize for the desired number of visible elements. Less code!
  • Search 1D array for zero seems incorrect. What if a real data value is exactly zero?
  • Why do you read from local variables? Where's the terminal. 😄

 

See image for some ideas.

Message Edited by altenbach on 11-19-2008 01:56 AM
0 Kudos
Message 5 of 13
(3,838 Views)

The problem is that the feedback node is only availiable for  for /while loops.

 

I am using a case statement in which I want to record the position feedback only when the trigger is activated.

 

So do I use this code which u gave inside a for loop which is inside a case statement?

 

i.e for loop in a case statement

 

0 Kudos
Message 6 of 13
(3,830 Views)

SInce LabVIEW 8.5 (?), a feedback node can be standalone and globally initialized. If you have a FOR loop you can place the shift register or feedback node in the loop. Built the array in one case and wire it across the other case of the case structure so nothing happens.

 

Why don't you attach a simplified version of your code so we can see what you're doing. It is difficult to guess from words alone. 🙂

0 Kudos
Message 7 of 13
(3,812 Views)

Hello altenbach,

 

Your previous advice was crucial. But I now have a new problem of initializing the feedback node everytime so that previous values in feedback node are not used in the VI when VI  is stopped and runs again.

 

I am attaching my code VI here. It would be useful if you could have a look at the while loop at the bottom.

 I am also attaching a picture which shows that part of the code.

 

It will be useful if you could also inform me about displaying pictures in the messages that we post here.

 

Regards.

 

Download All
0 Kudos
Message 8 of 13
(3,785 Views)

OK, you have a lot of work to do on this, because there are glaring mistakes. First of all, your inner FOR loop has no purpose, delete it. Use a shift register on the while loop and initiaize it with an empty array. Built the array in one case and wire it across the other case This should take care of the problem.

 

Now, let's talk race conditions!

 

Your biggest mistake is overuse of local variables, in this particular case you get the "Position feedback" into an indicator and read from a local variable of it in two different places. Most likely, the reading from the local variables in the midlde loop happens before the new value is written to the terminal, causing unpredictable code. Since the terminal is right there, you should use a wire. This ensures that the data is consistent.

Similarly, and probably even more important, the same applies to the "detection trigger". Use a wire!!!

 

The attached image show the middle while loop with some of the ideas implemented. Let me know if anything is not clear.

 

 

 

Some additional points (there are many more) you might want to look into: 

 

  • What determines the loop rate of the two while loops? They both seem to be real CPU burners.
  • Using "unbundle by name" is simpler and better self-documenting. Wiring out of a huge unbundle node can lead to wiring mistakes.
  • Use correct representation for indicators (detection trigger count is an integer (blue).
  • If you would properly dataflow (e.g. using error clusters) you wold not need the outer sequence structure.
  • I don't understand the upper code with the FOR loop and event structure at all, but is seems overly complicated and makes very little sense.

 

 

 

Message Edited by altenbach on 11-20-2008 01:28 AM
0 Kudos
Message 9 of 13
(3,776 Views)

Hello altenbach,

 

Thank you for your prompt reply. It would be worthwhile to mention that I am using Labview 8.2.

 

I tried out your code (in the picture) and removed the feedback node from build array. I also removed the for loop.

But the problem in this case seems to be that the positions, which I want to acquire at every true case (trigger), are overwritten at the same first index of the array built.

 

I want to acquire all the true case positions and store them in the array. Whenever the case turns true, the previous position value in the array is overwritten by the new value.

 

It would also be important for you to know about the top for loop with the event structure, which seemed complicated.

 

This for loop is used to send motor commands and the middle while loop (the one in the picture which you showed) is used to continuously gather data packets from the controller for monitoring. The event structure is used to disconnect motor in case of emergency.

 

So whenever the middle while loop runs to continuously gather data packets, it seems that the build array is initialized once again to 0. Hence the old value of position is erased and new one just overwrites it.

0 Kudos
Message 10 of 13
(3,759 Views)