jimiweber Posted July 23, 2017 Report Share Posted July 23, 2017 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 Quote Link to comment
LogMAN Posted July 23, 2017 Report Share Posted July 23, 2017 Welcome to LAVA! 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: 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. 1 Quote Link to comment
jimiweber Posted July 23, 2017 Author Report Share Posted July 23, 2017 (edited) 1 hour ago, LogMAN said: Welcome to LAVA! Thank you 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 Volume Dispense.vi Edited July 23, 2017 by jimiweber Quote Link to comment
LogMAN Posted July 23, 2017 Report Share Posted July 23, 2017 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: Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.