LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Making Arrays of local variables

Solved!
Go to solution

So my code has multiple very similar loops which must run in sequence as given in the snippet. The only difference between them is the reference each of therm get. First loops get a reference to move the Y axis motor while second loop gets the reference to move the Z axis.Now since each of these references are just local variables is there a way I can put them in an array and do only one loop instead of multiple such while loops.( I want to add loops for the other axes motors a swell, like X , Yaw, Pitch etc.,) 

SNIPPET1.png

0 Kudos
Message 1 of 7
(1,817 Views)

Your picture is just way to small for us to be able to look at in order to try to decode what you are trying to say in your message.

 

Do you really need local references?

 

Yes you can build references into an array using Build Array.

0 Kudos
Message 2 of 7
(1,777 Views)

As RavensFan said, you can use Build Array and a For loop with the references you want to use.

You might run into difficulties if the references have different types (because the array wire will be of the closest shared parent type) in which case you might have better luck with an Object-Oriented / class-based approach (see below).

 

Edit: It looks like at least the example you gave uses two axes that are both "Modular Stepper Motor"s. To clarify, if all of your references have the same type (or are close enough that a shared parent type exposes the methods you need) I would stick with the raw references for simplicity in this case (although your case might become more complex, etc... up to you to determine if classes might be better, feel free to post more details).

 

That could look something like:

  • build a class for each type (but not necessarily each direction, etc)
  • provide it with methods to write the reference (data accessor, you probably need to call these before you start doing anything, and possibly before you build an array, so a 'constructor' type method might be appropriate
  • provide it with some actions that you need to carry out ("Set Position"?). In these (dynamic dispatch, public) VIs, carry out the appropriate action. I also can't see your image well enough to determine what that might be...

GCentral
0 Kudos
Message 3 of 7
(1,755 Views)

Snippet-2.png

 Hope this is much better 

0 Kudos
Message 4 of 7
(1,734 Views)

**As RavensFan said, you can use Build Array and a For loop with the references you want to use.

You might run into difficulties if the references have different types (because the array wire will be of the closest shared parent type) in which case you might have better luck with an Object-Oriented / class-based approach (see below).**

 

Will try using for loop with build array. Don't know much about classes, will have to read on that.

 

**Edit: It looks like at least the example you gave uses two axes that are both "Modular Stepper Motor"s. To clarify, if all of your references have the same type (or are close enough that a shared parent type exposes the methods you need) I would stick with the raw references for simplicity in this case (although your case might become more complex, etc... up to you to determine if classes might be better, feel free to post more details).

 

That could look something like:

  • build a class for each type (but not necessarily each direction, etc)**

I can't bild a type for each type of motor( stepper, piezo etc) Each motor will have to have its own .NET reference. This is a limitation of the references provided by the manufacturer of the motor stage. 

**

  • provide it with methods to write the reference (data accessor, you probably need to call these before you start doing anything, and possibly before you build an array, so a 'constructor' type method might be appropriate
  • provide it with some actions that you need to carry out ("Set Position"?). In these (dynamic dispatch, public) VIs, carry out the appropriate action. I also can't see your image well enough to determine what that might be...**

I have given a much clearer snippet now as a reply to RavensFan, do have a look.

 

Edit: So a build array work fine. I attached the array to an auto indexed while loop, something like this 

Snippet_3.png

 Just one issue, I would want the array to wraparound itsef. i.e: after 4th iteration go back to the first array element to continue running the while loop until the stop condition is met. How should I do this??

 

 

Thanks a lot 

bajajvishal11

0 Kudos
Message 5 of 7
(1,730 Views)
Solution
Accepted by topic author bajajvishal11

@bajajvishal11 wrote:

 Just one issue, I would want the array to wraparound itsef. i.e: after 4th iteration go back to the first array element to continue running the while loop until the stop condition is met. How should I do this??

To go with the quickest point first, use a While loop (because you want to continue indefinitely, from the point of view of number of iterations) and then use Quotient and Remainder with the divisor equal to the Array Size, and Index Array with the remainder as the index.

 

Something like this: 

cbutcher_0-1619352243079.png

Here of course is just an example, I used Doubles but you'd want to use your reference array.

 


@bajajvishal11 wrote:

That could look something like:

  • build a class for each type (but not necessarily each direction, etc)**

I can't bild a type for each type of motor( stepper, piezo etc) Each motor will have to have its own .NET reference. This is a limitation of the references provided by the manufacturer of the motor stage. 

**

  • provide it with methods to write the reference (data accessor, you probably need to call these before you start doing anything, and possibly before you build an array, so a 'constructor' type method might be appropriate
  • provide it with some actions that you need to carry out ("Set Position"?). In these (dynamic dispatch, public) VIs, carry out the appropriate action. I also can't see your image well enough to determine what that might be...**

Hmm, I think you didn't understand what I meant (or maybe I don't understand what you meant...) but you could do this. You'd create a class for Piezo, a class for Stepper etc. Then build an Object (instance) of that class for each axis you wanted to control, and give the object that reference as an input. 

But if you can use the .NET references in an array and have the operations you need (e.g. the movements) then you can just do that instead, probably easier and less stuff needed.


GCentral
Message 6 of 7
(1,721 Views)

The code worked fine exactly. 

 

I didn't know how classes worked in Labview. Read on it. What you said made complete sense, but I guess I'll stick to this approach since its simpler and I only have 6 motors to control. Creating a Class seems overkill. 

 

Thanks a lot.

 

bajajvishal11 

0 Kudos
Message 7 of 7
(1,690 Views)