LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Conditionally progressing through an array after a certain value is met

Solved!
Go to solution

Hello everyone, I will do my absolute best to explain my issue. First, I will explain what my program does:

 

I have made a system that increments from zero up to a maximum value, defined by the user, then decrements back to zero. The user also sets the time in which they want the system to reach the maximum value. Once the program is ran, a step size for the counter is calculated and the program starts stepping up, every 50ms, until it reaches the max value. The program will not count up if the calculated step size is higher than 0.02.

 

Now, onto my issue:

My program currently has only two user inputs - one for the max value and one for the time in which the value must be reached.

 

I need make it so that instead of a single value, I can define a range of values that the user can input. The idea is that once the program reaches the value of the first element in the range, it goes to the second, third, etc... and it essentially recalculates the step size, based on the input value. I tried using an array in the place of the "Stall Torque" input, but was not able to make it work. Please give me ideas on how to add this functionality to my system. Thank you.

 

0 Kudos
Message 1 of 8
(1,528 Views)

I'm not sure if the screenshot relates to the description.

 

I recommend that you check out the ramp generator function: https://www.ni.com/docs/en-US/bundle/labview/page/lvanls/ramp_pattern.html

 

You can then solve the problem like this:

 

You have a start value, a target value and time to go from the start to the target.

You choose a frequency/temporal resolution for the ramp. Multiply the resolution with the ramp time to get the number of samples in the ramp.

Enter the number of samples, the start and target value into the ramp pattern SubVI to receive an array of step values.

 

You repeat this for all target values and ramp times in your list, so that the next start value is the target value of the last step.

0 Kudos
Message 2 of 8
(1,507 Views)

I strongly recommend to start with the basic LabVIEW tutorials before jumping into the deep end. You need to learn how to swim first!

 

  • Why would you have a greedy loop that may never stop under certain unusual inputs?
  • Sequences structures and unneeded local variables are hallmarks of poor coding practices.
  • Why do you keep values in shift registers if they are never modified (all controls for the first loop)?
  • You still have your super-convoluted boolean logic even though we showed you much simpler ways....

 


@InfamousHobo wrote:

I need make it so that instead of a single value, I can define a range of values that the user can input. The idea is that once the program reaches the value of the first element in the range, it goes to the second, third, etc... and it essentially recalculates the step size, based on the input value. I tried using an array in the place of the "Stall Torque" input, but was not able to make it work. Please give me ideas on how to add this functionality to my system. Thank you.

 


Well, a "state machine" comes to mind. The rest should fall into place.

 

So do you have an array of max values and one shared time, or an array of max values and an array of times? Calculating the step size is basic math. No need for any loops.

 

Please define the exact requirements:

So we have a range of values. With one value, it will go from zero up to that value then back to zero. Right?

If we have a range of values, what should happen once the first value is reached? (Go back to zero, then ramp to the next value? Ramp to the second value directly?)

0 Kudos
Message 3 of 8
(1,473 Views)

Hi altenbach, I saw the recommendation in your other reply, but decided to stick with my own simply because I understood it better, having done it myself. I do understand that it is more efficient than mine, but please bear with me, I've had about 2 months of on-off Labview experience, not 20 years. I will try to answer your questions here:

 

  • Why do you keep values in shift registers if they are never modified (all controls for the first loop)? - ultimately I want to modify them after each iteration completes. I want to add intermediate values between the zero and the max value. Let's say I tell the program to go from 0 to 5 in 30 seconds, stay at 5 for 10 seconds, then go from 5 to 0 in 15 seconds. I would definitely appreciate suggestions on how to do exactly that.
  • So do you have an array of max values and one shared time, or an array of max values and an array of times? - that's also a good point. An array of max values and an array of times would actually be ideal for my case, although I do not know how I would implement them.
  • If we have a range of values, what should happen once the first value is reached? (Go back to zero, then ramp to the next value? Ramp to the second value directly?) - if we have a range of values, say 0, 5, 10, 15, I will need to ramp to the second value, then the third, etc. until I reach the end, when they will start ramping down sequentially. 
  • Why would you have a greedy loop that may never stop under certain unusual inputs? - If the user enters several values for time, intermediate values and max value, the program will need to recalculate step size every time.

I hope that clears everything up. I would highly appreciate a tip on how to implement multiple times and intermediate values via arrays or similar methods, I just don't know how to go about it myself.

0 Kudos
Message 4 of 8
(1,445 Views)

So you would just have a 2D array where each row is a torque and a time. Or you could do a 1D array of clusters containing pairs of well-named scalars for more self-documenting code. Later you would use "unbundle by name" to avoid confusion.

 

Your first loop is not only a greedy loop, but as attached, it will run forever for certain inputs. Try torque=2 and time=1, (for example). and place an indicator for the iteration counter, then run the VI. Are you sure you want "continue if true" and "not stop if true" for the loop condition? Calculating a step size based on inputs is basic math. No loop needed. Since the controls are before the loop and the loop never modifies any values, the result will be identical for all iterations and it will either stop immediately or never.

 

You can limit the valid input range for each control and also define default values. Having both values at zero by default does not give us an indication of typical inputs. For example we don't know if 1e-9, 1, or 1e9 is most reasonable.

 

 


@InfamousHobo wrote:
  • If we have a range of values, what should happen once the first value is reached? (Go back to zero, then ramp to the next value? Ramp to the second value directly?) - if we have a range of values, say 0, 5, 10, 15, I will need to ramp to the second value, then the third, etc. until I reach the end, when they will start ramping down sequentially. 

 

Can you explain the "ramping down sequentially" part? So would it go 0(t0),5(t1),10(t2),15(t3),10(t2),5(t1),0(t0) in your example for the full experiment? 

 

 

As said, your use of local variables is not only wrong, but probably dangerous! Since you continue to read from the local variables during the run of the second loop, things could quickly go haywire if the user would start playing with the control while the process is running. Do you really want to allow that?

0 Kudos
Message 5 of 8
(1,429 Views)

The sequence you typed 0(t0),5(t1),10(t2),15(t3),10(t2),5(t1),0(t0) is exactly what I meant by "going up and down sequentially". I also very much like the idea of using a 2D array, but how would I go about doing that? Could you please explain how you would use it in a system like mine? Purely technically, I don't what block to put where, to extract the data from the 2D array and use it for my calculations in the first loop. My idea is that step size will be recalculated every time the array progresses through a row of values. For example, I have a 2D array with 5 rows and 2 columns. The program uses the values from row 1 of column 1 and 2 to calculate step size. That is then used to calculate "Current Torque" in the next loop. That is also the reason why I thought it would be necessary to use a loop for the step size, as I don't want it to be calculated only once. Thanks for keeping up with me btw, I really appreciate it.

0 Kudos
Message 6 of 8
(1,418 Views)
Solution
Accepted by topic author InfamousHobo

You also need to be very careful with the division, for example if one of the steps is set to a duration of 0 seconds (you never know what your users will enter!), you'll get a division by zero and an infinite step size! You should also account for a situation where the torque should not change for a certain amount of time. Once the step size is calculated, the experiment should proceed to the next step purely based on time.

 

Here's a very quick draft that might give you some ideas. I am sure it would need to be tuned. I think it is better to define the entire up-down ramp, so each step can have it's own time, independent of direction.

 

If you don't even know how to get an element (or row) from a 2D array (index array!), you really need to start over with the basic tutorials or you won't get anywhere.

 

Calculating the step size based on duration and previous and current torque is a trivial scalar operation and can be done in the main loop. No need to pre-calculate,

 

This will cycle through all steps forever. You can easily modify to go to an idle state once all steps have completed, the start over on demand (hint: state machine, not shown in my example).

 

altenbach_0-1654015613574.png

 

 

Message 7 of 8
(1,411 Views)

Wow thanks a ton! That is tremendously helpful of you.. I will examine your system and try to understand the logic behind it. Thanks again for the tips, I will revisit the tutorials on arrays tomorrow and try to work my problem out, should be doable now.

0 Kudos
Message 8 of 8
(1,402 Views)