LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Running Sum

Solved!
Go to solution

How do you calculate a running sum, A  = A + B, in Labview?  I have seen exaples of how to do it with process variables, a while loop and shift registers.  A while loop won't work in my application because it calculates the sum every wait period.   Where as I want to calculate  the sum only when new X and Y commands are sent to a stepper motor controller.

 

The X and Y sums will be used to display how far a X and Y table has moved from a 'Home" position.

 

Howard

 

 

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

Create a subVI with a while loop.  Wire a constant to the stop condition so it always terminates after one loop iteration.  Add a shift register around the while loop.  DO NOT wire anything to the shift register from the left side, so that the shift register is left uninitialized.  Add a control for the value you want to add; you might also want to add a "reset" control.  Create an indicator for the shift register on the right side of the loop (outside the loop) - that's your sum.  Inside the while loop add the numeric control to the current shift register value, and if you want create a way to reset it when the reset input is true.  Call that subVI whenever you need to increase the running sum.

Message 2 of 7
(4,835 Views)

Thank you for your instructions.  The while loop is calculating a running sum now.  However, I am having problems with resetting the sum to zero.  My first try was to connect the constant 0 to the left side of the left shift register to reset and to leave the left side of  the left shift register open to sum.  But I can't find a way to do both.  As far as I can tell Labview doesn't have any SPDT switches.  A case structure didn't wok because Labview insisted I connect something to the output terminal for both cases. 

 

What did you have in mind when you suggesting a reset? 

 

Howard

0 Kudos
Message 3 of 7
(4,817 Views)
Solution
Accepted by topic author hrh1818

Either a case structure or a Select function will work.  The case structure is possibly easier.  Put the case structure around the addition, putting the add inside the false case.  In the true case, connect the value you were going to add directly to the output tunnel (no addition).  This way you can reset to any value you want.  The value to add will need to be outside the case structure, of course, and for optimal efficiency it should also be outside the while loop.  If this isn't clear, or you'd like someone to check your work, attach your VI.  Make sure you reset the first time you run the VI (or handle this inside the running sum VI by using the "First Call" function and OR the output with the reset input).

Message 4 of 7
(4,813 Views)

The attached VI meets my requirements. If it is convenient please check the VI to see if it is consistent with your instructions.

 

To calculate a running sum set "Reset Sum" slide switch to the "Sum = Sum + Input" position, enter "Input" value and run the VI.

 

To Reset the Sum to 0 set the "Reset Sum" slide switch to "Reset" position, enter 0 in "Input" and run the V.

 

Many Thanks for your help. 

 

Howard

0 Kudos
Message 5 of 7
(4,784 Views)

Looks fine.  Two small suggestions:

1) Wire the sum output to the shift register; no need for a separate loop tunnel.

2) Wire a constant to the stop condition of the while loop (right-click the stop condition and create a constant, then click to switch it to true); the 1 >= 1 looks a bit silly.

0 Kudos
Message 6 of 7
(4,774 Views)

Thank you for your comments.

 

Howard

0 Kudos
Message 7 of 7
(4,770 Views)