LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Creating Exclusive Numbers

I come again defeated to the NI Discussion board!
 
Let's say I want to use LabVIEW to do 10 different calculations. These calculations are running together at the same time in their own little loops. The catch is the calculations must be performed with ONLY the numbers 1-8 and at any given time a number cannot be used in more than one calculation. If it can't exclusively grab one of the 8 numbers, it waits for the number to finish in another calculation.
 
How would I achieve this? I've tried using semaphores, thinking I could use some kind of shared variable and use the error in and outs to control access to each of the 8 numbers. I can get it work by using 8 different semaphores, but the problem is in my application there are 40 numbers that can only be used 1 at a time and not 8 so using 40 semaphores just seems overkill.
 
Thanks guys! And have a great weekend folks!
 
-- Jason

Message Edited by jmpugh on 03-10-2007 01:33 PM

0 Kudos
Message 1 of 11
(3,823 Views)
And just so nobody gets too caught up in my example, the application I am working on requires the movement of 4 objects around a track that has 40 positions. These 4 objects cannot be in the same position at  the same time so I wish to somehow "lockout" the position whenever an object moves to it.
 
After re-reading my initial post I became concerned possible help would get caught up with the calculations metaphor, which after reading, I think is weak. Sorry! But I still would love for anybody who knows their stuff to give me a wire hand! Thanks again!
 
-- Jason

Message Edited by jmpugh on 04-20-2007 01:46 PM

0 Kudos
Message 2 of 11
(3,818 Views)
Done in the LabVIEW 1.2 days.

We created an exclusive random number generator. It took a range of numbers, such as your 1-40, and randomized the order of the numbers, using each number just one time.

I am not sure how you would use this for your lockout but it may give you some ideas.

This is a LV5.1 version which can be opened in any newer version. (Well, the Forum made me add a .vi extension, so I won't guarantee it will work with all the old versions).

Lynn
Download All
Message 3 of 11
(3,809 Views)
Thanks for posting the code! I guess the difference for me is the "calculations" aren't just random. Referring to the second post, the 4 players move around a board with 40 spaces. The players aren't randomly being moved, they knew before they start what spaces they will stop along their way and they must stop there.
 
I was looking for a way to "lockout" a space so if Player 1 is on Space 5 and Player 2 needs to go from Space 3 to Space 5, Player 2 will be forced to wait until Player 1 has moved on. I was trying to simply things by doing the "calculations" analogy, but I think I just confused things.
 
To sum it up, I know how to use semaphores to lock out a section of code. But how would I lockout an entity of an array or control type enum?
 
 
-- Jason
0 Kudos
Message 4 of 11
(3,799 Views)

And if Lynn's suggestion does not do it for you here is an outline of an Action Engine that will do the job.

Init

Create an array of 40 numbers along with a similar  sized array  of booleans. THe numbers are your numbers and the booleans track wich values are in use.

Get Number

Searches the boolean array for the first false (indicating not in use) and then use the returned index to find the number to return. Set the boolean falg true so others do not use it. If the returned index is less than 0 then no numbers are available. Return a flag indicating such.

Return Number

Uses the value passed by the caller to serach the array of number and use the index of that element to clear the boolean "in use" flag for that number. If the boolean was already false, there was a program erro in the caller.

Have fun!

Ben

 

 

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 5 of 11
(3,801 Views)
After reading Johnsold's post I think (HOPE) I was struck by an epiphany that Ben's response further cemented.
 
Instead of using semaphores to lock out a "number" I can use them to lock out a flat sequence structure that contains an array of booleans and changes the corresponding boolean value to the required number to false.
 
Thanks guys! I'm going to try and code this idea before it fades! And feel free to correct me if I'm approaching this the wrong way. I was trying to use a semaphore to do the actual locking, where I really should use a semaphore to ensure that the section of code that does the locking is only used once at a time.
 
-- Jason
0 Kudos
Message 6 of 11
(3,795 Views)

The  Action Engine link expalins thtat the "semaphores behaviour" is built into the AE.

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 7 of 11
(3,787 Views)
I read your problem and immediately thought of an array of booleans. It looks like this has been suggested to you already. Before you get into semaphores, take a look at Ben's action engine idea. You won't need any semaphores if you go this route because the action engine VI will only be able to be run once at a time, it has built in race condition protection.
 
I put together a simple example that should get you started.
Message 8 of 11
(3,782 Views)

Thank you Marc! I get it now, since the vi won't be re-entrant it can't run twice at the same time. Heh. Very clever!

 It's also always educational AND entertaining to see how much neater and less "going around your butt to get to your elbow" a more experienced LabVIEWer's code is. The one I was attempting to create made use of several more array functions.

The reason I like the semaphore idea is I will have to check first to see if that space (in 1-40) is availible before moving to it. If it is already taken, I'll simply need that "player" to wait for it to open. I see how the action engine ensures only one number at a time is claimed, but I would still have to use a semaphore to keep other "players" out of that space until the occupying player has moved on. Correct?

Thanks to all!

-- Jason

0 Kudos
Message 9 of 11
(3,774 Views)

Not necessarily. Like I said, my example was just to get you started. You could add actions for 'enter' and 'leave'. In the enter action, if that array value is already true, you don't do anything. You could even return a boolean to represent an error trying to enter a space. When a player calls the action engine with 'enter', he sees there is already another player there so he has to wait.

You could have a queue of players that want to move. Each element could be the player and the space he wants to go to. Enqueue a player when he wants to move, then try the move, if that space is occupied, move the player to the back of the queue. There are a lot of possibilities for this. It sounds like a very good exercise to learn some very good programming techniques.

A semaphore might be helpful because it can wait as long as you want and enter immediately when it becomes available, but I think it will be more trouble than it's worth.

Message 10 of 11
(3,759 Views)