From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

For loop inside a while loop

Solved!
Go to solution
In the start state I kept the coils off because in this state the decision is made on which state to move to(clw or cclw) by looking at the voltage from the que(if greater than 1 move to clw otherwise cclw). In each state counterclockwise or clockwise) I attached a constant to the N of the for loop). the reason I want to create state machine is because without it, when the motor finishes up all the steps then any change in voltage afterwards would not do anything unless I rerun the program
0 Kudos
Message 21 of 28
(1,015 Views)

Your last two replies seem to make contradictory statements. The motor runs 150 steps in the clockwise case or 75 steps in the counterclockwise case and then stops unitl the next state starts.  The direction decision is made in each state (using the data present before that state starts). Changes in voltage after the motor has moved will eventually have an effect on the next direction, but you have a nasty timing problem which may mask the effect.

 

Let's look at the behavior of two loops: the producer loop with the DAQ Assistant and 2 Enqueue functions, and the state machine loop.

1. Assuming that the DAQ Assistant requires less than 250 ms to acquire each data set, then 4 values are enqueued each second. Let's call the elements d0, d1, d2, d3,... If d0 is available immediately when the loop starts, then d2 will be enqueued at about t = 500 ms.

2. The state machine Dequeues the first element (d0) and enters the Start state no later than 250 ms after the loops start and decides the next state based on the value of the first element dequeued.  Let us arbitrarily say that it is CCW.

3. On the second iteration of the state machine while loop the Dequeue waits until the next data (d1) is available on the queue.  This can be up to 250 ms. Then it enters the CCW state.  In this state it decides the next state based on the data just dequeued (d1).  It then write 75 steps to the Digital Output at 50 ms intervals which consumes 3.75 seconds. The motor stops. While this is happening the Producer has Enqueued data elements d2, d3, ... d76.

4. On the third iteration of the state machine loop element d2 is immediately Dequeued and the state determined by element d1 (which was Dequeued 3.75 seconds earlier) is entered.  It is likely that this will also be the CCW state because the motor had not yet moved or had only moved a few steps in the previous iteration. So you get another 75 steps over the next 3.75 seconds. The motor stops. The next state will be that determined by d2 (which represents the motor position at t = 500 ms or 7 seconds prior to the current time). The queue will now contain d3, d4, ... d151.

5. The process continues. The direction decision making gets farther and farther behind. At the end of each state the motor stops, although probably only for a few microseconds before it starts up again in the next state.

 

Try this: Put a Get Queue Status function inside the state machine while loop just before the Dequeue function. Wire an indicator to the # pending remove output. Also wire an indicator to the "i" terminal in the state machine while loop.  Run the program and watch those indicators.  I predict that the # pending remove will jump by about 75 or 150 (depending on direction) everytime the i indicator changes.

 

Lynn

Message 22 of 28
(1,008 Views)

Actually you want the # elements in queu not the # pending remove.

 

Here is a little example.  It uses the same structure as your program with all the DAQ stuff removed.

 

Lynn

0 Kudos
Message 23 of 28
(1,005 Views)

ok well what you are saying does make sense.. The main idea behind this program is that as long as the voltage is above 1 the motor should rotate once 150 steps and then stops and rotate again only if the voltage goes below 1....

 

so what iam trying to do is that dequeue the element from the data acquisition, look at this data(voltage)..if it is greater than one then motor should rotate 150 steps and stops and then once it has stopped rotating then look at this data again and rotate only if the voltage goes below 1.

 

Thanks

0 Kudos
Message 24 of 28
(1,001 Views)

Now you are reaching the point where you have code which performs each of the separate tasks (some better than othrers), but the overall program architecture does not allow it to perform the tasks in the order which meets your requirements.

 

From looking at the code and reading your comments it appears that you want the progrma to do something like this:

 

Read a voltage.

 

Compare voltage to a threshold (currently threshold = 1).

 

If voltage > threshold, then Move motor clockwise N steps

                                 else Move motor counterclockwise M steps.

 

Loop

   Read a voltage.

 

   Compare voltage to a threshold.

 

   If (voltage > threshold) AND (previous voltage > threshold), then do nothing.

   If (voltage < threshold) AND (previous voltage > threshold), then Move counterclockwise M steps.

   If (voltage > threshold) AND (previous voltage < threshold), then Move clockwise N steps.

   If (voltage < threshold) AND (previous voltage < threshold), then do nothing.

 

   Put something here to stop looping.

Repeat.

 

This may not be exactly what you want but you get the idea. 1. It is important and very useful to plan and design yor program before you begin to write code. 2. The plan helps you to decide on the appropriate architecture for your program. 3. The plan helps you make sure the program meets the real requirements.

 

Given how long it takes to move the motor and that you only wnat a voltage measurement when the motor is stopped, one loop may be all you need.  I would really need to see a good design or at least a requirements document to see what other factors are involved to be sure.  However, I believe it is very unlikely that you need the five loops you currently have.  One or two is what I would expect.

 

Lynn

0 Kudos
Message 25 of 28
(982 Views)

In order to do the following what changes do I need to make in the program. Are you saying to not include the sate machine. Also, in order to do this in one loop what will I need to do..... like how do I compare the current voltage with previous voltage using AND just like you said before. 

 

If (voltage > threshold) AND (previous voltage > threshold), then do nothing.
 
   If (voltage < threshold) AND (previous voltage > threshold), then Move counterclockwise M steps.
 
   If (voltage > threshold) AND (previous voltage < threshold), then Move clockwise N steps.
 
   If (voltage < threshold) AND (previous voltage < threshold), then do nothing.

 

Thanks

0 Kudos
Message 26 of 28
(966 Views)

now if the for loop is inside the while loop then the for loop will keep iterating forever unless i stop the while loop..

 

Before what I did was I would count the for loop and stop the while loop once the for loop reaches the number of steps, then I would have manually turn on the while loop.

 

in the previous message you said that I can do all of that in one loop and compare it with previous voltage then make decision based on that...then put some kind of stop condition...how do I do that

0 Kudos
Message 27 of 28
(959 Views)

I am not sure I would try to fix the program you now have.

 

I think I would put together a state machine with more states: Initialize Voltage Read, Initialize Motor, Read Voltage, Test Voltage, Move Motor, Shutdown Motor, Shutdown Read, Halt.  Keep the motor step boolean array, the index of the current step, the most recently read voltage, the motor direction, and maybe your times and angles in shift registers.  Since Stop seems to be the only user input while the program is running, You do not really need a producer/consumer architecture or an event structure.  Whe the Stop button is pressed, go through the two Shutdown states before entering the Halt state.  The Halt state will wire a True to the while loop stop terminal, and all other cases will produce default or False value on that wire.

 

Depending on what some of your other loops do, you may need extra states for those tasks.

 

Lynn

0 Kudos
Message 28 of 28
(958 Views)