LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to send value from inside while loop to the outside

Solved!
Go to solution

Hello,

 

My project objective is to control the height of a ball in a tube (there is a fan down the bottom to lift the ball up and down) and an ultrasonic sensor up the top to read the distance to the ball.

I am currently using a PWM signal to control the speed of the fans (which works fine) and I am using the VISA Read/Write blocks to communicate to my sensors (which also works fine - I can obtain the distance to the ball - when I move the ball up and down by changing the duty cycle).

 

My problem:

Now, to create a control problem, I need to read the distance to the ball, have some sort of controller, and then feed this output as my new duty cycle for the fans... However I need the loops I have (running the fans and sensors to keep running (I guess? unless there is a way around it..) so the fans keep working and sensors keep reading. I need to get the distance measurement from the sensor loop (after its been read) passed out of the loop and to keep updating of course - So then calculations can be done on this and then can be used as my duty.

 

Does anyone a way I can do this?? 🙂 Its all attached for your reference, labview version 8.6.

 

Thanks heaps!

 

Nick.

 

Download All
0 Kudos
Message 1 of 16
(15,514 Views)

Hi Nick,

 

For passing values from inside of a running loop to the outside, you can use a Local Variable or Value Property nodes. To create a Local variable just right click your " DISTANCE FROM SENSOR" indicator and select create>>Local Variable. Now place this local variable where you want the value and connect it. 

 

As i can see in your attached image, if you directly wire the local variable to the duty cycle, it may not take the exact value as this part of the code runs parallel to the Write/Read sensor while loop. So you can use a while loop for the above portion of your code along with an event structure. In the value change event of the "Distance From Sensor" indicator, you can place the code and take out the value of the indicator and pass it to the duty cycle.

 

Make sure that you don't let the local variables create any race conditions. 


Regards,
NitzZ

(Give Kudos to Good Answers, Mark it as a Solution if your problem is Solved;)) 

Message 2 of 16
(15,501 Views)

Thanks for replying so fast.

 

I'm not 100% sure what you mean though.. I've created the local variable, and put it in a while loop (as in first attachment), so now it is updating and I can read the distance up in that loop. However still a problem being able to eventually passing it to the duty cycle (after calculations are done). Or should I have it set up similar to the second layout?? However, if this is done, the value doesn't update, because it never goes outside of that inner loop where the counter/PWM is being written.

 

Cheers,

Nick.

Download All
0 Kudos
Message 3 of 16
(15,490 Views)

Dear Nick,

 

You made some mistakes in your code. If you check your VI (project.vi) in your upper loop you are using Duty cycle control and this control you compare with the same local variable, but your are not going to update your values on real time either in duty cycle control or local variable. So if you put probe on your not equal to output this will always false.

 

As you share project layout 2.png image this way of coding will not work. In your main loop you have another loop. Until unless you finish your inner loop you will never come out to your main loop. So don't go with this method. In your project layout you are going to update distance from sensor 2 control from distance from sensor local variable. This method will work but your code performance will low and this will take the more memory. This is not the right way to do coding. I am unable to understand what you are trying to do in your code. This distance from sensor local variable you can directly use anywhere in your code no need to update second control and again use that second control local variable.

 

I feel you can go with local variable, notifier or queue method to get the value in your second loop. Whatever the method you want to use its upto you. If you feel more comfortable with local variable you can use that.

Please refer the attached VI

Thanks and Regards
Himanshu Goyal | LabVIEW Engineer- Power System Automation
Values that steer us ahead: Passion | Innovation | Ambition | Diligence | Teamwork
It Only gets BETTER!!!
0 Kudos
Message 4 of 16
(15,470 Views)

Hello,

 

I understand what you are saying with the comparing of duty cycle to its local variable. However, I got most of that from the example finder - Frequency counter output example. The first duty cycle on the left is a set value on start up, and then when duty cycle is changed (currently through a numeric control) only the local variable inside the loop is updated. So its true whenever you change the value of duty cycle i guess.. However, this counter update works perfectly.

 

In "Project layout", that top left loop, I am only using "Distance from sensor 2" to check whether it wasn updating the local variable in real time as I moved the sensor (which it was). I want to somehow use that local variable of "distance from sensor" (after some calculations/scaling) to be my new duty cycle of the top loop. And of course, as time goes on that local variable will change and so should the duty cycle.

 

Thanks,

 

Nick.

0 Kudos
Message 5 of 16
(15,454 Views)

Also, sorry, I'm using Labview 8.6, that file you attached was version 10 and was unable to open.

 

Cheers,

Nick.

0 Kudos
Message 6 of 16
(15,448 Views)

Hi Nick,

 

Your are perfectly right. I just want to know in your code on real time where you are updating this duty cycle control value. You have to update that control and compare the updated value with the previous value then it will work but in your code your control and local variable value always same no update. In both case same value is passing to shift register so it will not work you will get the same value again and again. I hope whatever the examples you are referring check the case structure design and the control value update techniques.

 

Find the attached VI in 8.6

Thanks and Regards
Himanshu Goyal | LabVIEW Engineer- Power System Automation
Values that steer us ahead: Passion | Innovation | Ambition | Diligence | Teamwork
It Only gets BETTER!!!
0 Kudos
Message 7 of 16
(15,444 Views)

Nick,

 

Generally, both local variables and property nodes are NOT the preferred means of transferring data between parallel loops.  LVOOP classes, queues, or functional globals (sometimes also called Action Engines) are more versatile, may be faster, and tend to be less likely to have race conditions.

 

As Himanshu Goyal pointed out, neither your images nor his code ever changes Duty Cycle.

 

You use default values from the false case where the sensor Read occurs.  How will your system respond to a zero distance if one ever occurs?  In your control system VI, you probably do not want the Write and Read buttons or the corresponding case structures at all. What happens if the Bytes at Port is zero or one? The numeric output you get may not be meaningful data.  Put the Enqueue inside the Read case.

 

If the delays in the two loops are different,  You will either accumulate data in the queue indefinitely or the PWM loop will wait for data.  You need to look at the timing.

 

Lynn

0 Kudos
Message 8 of 16
(15,419 Views)

Hi guys,

 

I think where the confusion is coming in (unless I don't really understand what you guys are trying to say... which is possible.. i'm very new with this.. :P) is that I don't have any type of control scheme implemented yet.. Ie. The duty cycle isn't being updated through any kind of control method; I only change the duty cycle when I want to using a numeric control on the block diagram.

What I want to do is use the sensor distance to somehow configure control the duty cycle to keep my ball at a certain height. Which means the top "layout" (PWM while loop etc) probably needs to be changed a fair bit to do this... I'm not sure.

 

I'm assuming that first "duty cycle" input (to the configuration block) doesn't need to change - So I guess I can set that to a particular constant so on startup the duty cycle is (for example) 50%?? And then I guess that local variable inside the loop wouldn't need to be there, because that value would be replaced by the value obtained after doing calculations with the "distance from sensor" measurement? Would that be right? If so, how do I go about doing this?

 

So this is what I have now (attached is both a picture and the VI file) - I tried using the local variable of distance from sensor (which is a distance in cm between 15 - 240cm (To answer your question Lynn:) Sensor can't read less than 15cm), and then scaled it as a percentage of 140cm, which is the height of the tube, maximum height the sensor should be reading. And I'm then trying to use this as a duty cycle (to see if it will work), so therefore at a distance of 70cm, the duty cycle should be 70/140 = 0.5 (or 50%).

HOWEVER, I am getting an error (which is shown in the picture).

 

Thanks again for replying and helping guys. Any more advice about where to go from here??

 

Nick.

Download All
0 Kudos
Message 9 of 16
(15,378 Views)

Hey Nick,

 

You may want to try waiting before you write to the duty cycle, since it seems to be not liking the fact that you are immediately updating the properties after you start the task (similar to the picture shown below). What card are you using to control the PWM? gen_pulses_revised.JPG

 

I would also try probing the wires going into your write to make sure your not sending a value less than 0 or greater than 1 to the duty cycle. I hope this helps!

 

-Nathan H

Software Developer
National Instruments
0 Kudos
Message 10 of 16
(15,342 Views)