Jump to content

Adding a constant number in loop


Recommended Posts

Hi everyone,

I couldn't phrase my question with proper search keywords to make sure whether this has been asked before or not, sorry if this is something which was discussed before.

What I try to do is add a constant number to a value, exactly every 1 milliseconds. The target is simple. It should be a dispense counter, that calculates how much is dispensed in loop iterations. So, for example, if the dispensed rate is 0.001 mL for every 1 ms, it should take 1 second to dispense 1mL.

I tried using a while loop with "wait until next ms multiple" delay to do this. Two different approaches I tried are 1. simple method, two variables, one for rate and one for dispensed. They are accumulated in every while loop iteration, and 2. using a shift register to avoid using numeric constants and local variables.

The elapsed time for both ways are calculated using flat sequence and "tick count". Both files are attached.

However, none of the two are working as I expect. They both take more than 1 second to do the job. Plus, they are dependent on computer processing power, which I don't want. What is a better way to do this? I am fairly new to labview programming, and maybe some insight might help a lot.

My laptop has Core-i5, 8gbs of ram, Windows 10 64bits, and Labview 2014.

Thanks

Volume Dispense-SR.vi

Volume Dispense.vi

Link to comment

Welcome to LAVA! :beer_mug:

2 hours ago, jimiweber said:

What I try to do is add a constant number to a value, exactly every 1 milliseconds.

[...]

However, none of the two are working as I expect. They both take more than 1 second to do the job. Plus, they are dependent on computer processing power, which I don't want. What is a better way to do this? I am fairly new to labview programming, and maybe some insight might help a lot.

The short answer to this is: You cannot do time constrained operations (within the millisecond area) using a regular PC. Other processes will "steal" CPU time which can put your program on halt for several milliseconds (up to several hundreds), so the timing is not predictable within your constraint. However do you really need to calculate a value every millisecond?

2 hours ago, jimiweber said:

The target is simple. It should be a dispense counter, that calculates how much is dispensed in loop iterations. So, for example, if the dispensed rate is 0.001 mL for every 1 ms, it should take 1 second to dispense 1mL.

According to your description the dispensed volume is a linear function over time. This means as long as you know how many milliseconds elapsed since a specific moment in time, you can calculate the total volume dispensed. The following VI is an example on how to do that:

Dispensed Volume.png

This will give you a new value after 1 second, however keep in mind that there is still a chance it could take more time depending on how busy your computer is and which process gets priority.

One recommendation after reading your VIs: Please make use of controls / indicators before using local variables. It makes your code much more readable and removes unnecessary complexity.

  • Like 1
Link to comment
1 hour ago, LogMAN said:

Welcome to LAVA! :beer_mug:

Thank you :thumbup1:

1 hour ago, LogMAN said:

The short answer to this is: You cannot do time constrained operations (within the millisecond area) using a regular PC. Other processes will "steal" CPU time which can put your program on halt for several milliseconds (up to several hundreds), so the timing is not predictable within your constraint. However do you really need to calculate a value every millisecond?

Yes, as a matter of fact, it is important to add numbers exactly every one seconds.

Your solution actually worked. It's consuming exactly the amount of time that is expected. I attached the edited file for future references.

1 hour ago, LogMAN said:

One recommendation after reading your VIs: Please make use of controls / indicators before using local variables. It makes your code much more readable and removes unnecessary complexity.

These files were just temporary files to test the idea, but thanks again for noticing :D

Volume Dispense.vi

Edited by jimiweber
Link to comment
3 hours ago, jimiweber said:

Your solution actually worked. It's consuming exactly the amount of time that is expected. I attached the edited file for future references.

While your VI works it takes a lot of CPU time by running the while loop each millisecond. In case you want it to wait for a desired amount of time, the snippet I posted earlier can be changed to handle any desired duration by adjusting the delay accordingly. You could also just calculate how much time you need in order to reach the specified limit:

Dispensed Volume Reverse.png

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.