Jump to content
The simFlight Network Forums

Loop Delay


Recommended Posts

In some Lua code I am writing I use a loop like the below:

...

while n > 0 do    
      ipc.control(65823, 77) 
      ipc.control(65823)
      ipc.sleep (80)
      n = n-1

end

...

I find to get the loop to execute reliably I need the delay of 80 ms. If I reduce the delay the loop sometimes seems to execute the wrong number of times (i.e., the control functions don't execute the correct number of times) . The initial value of n can be from 0 to over 40. The loop updates information in a G1000 display on an addon aircraft.

I use FSUIPC ver 4.936.

 

Does the need for this magnitude of delay seem reasonable? 

 

I am also trying to understand why the loop seems to execute the wrong number of times if the delay is to small.  When an ipc.control function call is executed, does the system automatically wait for the control function to be completed (and return) before the next loop instruction is executed, or not?

 

Thx,

Al

Link to comment
Share on other sites

In some Lua code I am writing I use a loop like the below:

...

while n > 0 do    

      ipc.control(65823, 77) 

      ipc.control(65823)

      ipc.sleep (80)

      n = n-1

end

...

I find to get the loop to execute reliably I need the delay of 80 ms. If I reduce the delay the loop sometimes seems to execute the wrong number of times (i.e., the control functions don't execute the correct number of times) . The initial value of n can be from 0 to over 40. The loop updates information in a G1000 display on an addon aircraft.

I use FSUIPC ver 4.936.

 

Does the need for this magnitude of delay seem reasonable? 

 

I am also trying to understand why the loop seems to execute the wrong number of times if the delay is to small. 

 

You'd be better off using the event.timer facility. Each of the lines in your loop may take 0, 1 or possibly 2 milliseconds. If FS gets really busy doing other things there could be bigger changes. FSUIPC cannot give a high priority to the Lua threads, it must yield all the time to avoid impacting FS performance.

 

If you use the timer event it won't matter about variations in the loop execution time. The timing is still not guaranteed (it never can be unless the threads are made very high priiority, which is not practical nor desirable), but it will be close and maintain the correct order of time over a longer period.

 

 When an ipc.control function call is executed, does the system automatically wait for the control function to be completed (and return) before the next loop instruction is executed, or not?

 

No, it cannot. It is just a message sent to the Windows message queue for FS to pick up and obey. FSUIPC has no idea what the control does, so obviously it cannot check for completion.

 

Pete

Link to comment
Share on other sites

No, it cannot. It is just a message sent to the Windows message queue for FS to pick up and obey. FSUIPC has no idea what the control does, so obviously it cannot check for completion.

 

 

OK, so that means if the control process takes FSX long enough to exeucute, the loop could "loop" around and issue another control call before the first one had completed.  I can see how this could lead to unreliable results.

 

I'll look up the event timer.

 

Thx much, Pete.

 

Al

Link to comment
Share on other sites

I looked at event.timer(msecs, “functionname") but I'm afraid I just don't see how to make good use of it in the loop below the purpose of which is to execute the two control functions n times, where the value of n is a function of the preceding code.

Thx,

Al

 

...

while n > 0 do    
      ipc.control(65823, 77) 
      ipc.control(65823)
      ipc.sleep (80)
      n = n-1

end

...

Link to comment
Share on other sites

I looked at event.timer(msecs, “functionname") but I'm afraid I just don't see how to make good use of it in the loop below the purpose of which is to execute the two control functions n times, where the value of n is a function of the preceding code.

Thx,

Al

 

...

while n > 0 do    

      ipc.control(65823, 77) 

      ipc.control(65823)

      ipc.sleep (80)

      n = n-1

end

...

 

The event system uses a function which is called when the event occurs. You don't use any loops. You simply cancel the event when your value of n reaches zero.

 

Pete

Link to comment
Share on other sites

I appoligize Pete, but I just don't follow what you are patiently trying to tell me. For my example above, how do I define the  "function"?  Is it something like

 

function  Alt_inc(k)

              ipc.control(65823, 77)

              ipc.control(65823)

              k=k-1

end

 

and then use

 

event.timer(80, “Alt_inc(n)")  where I have taken a guess at the msecs value?

                    

How do you cancel the event when n reaches 0?

 

If this is "way off" and only a very few lines of code are involved, can you show me what replaces my original loop above?

 

Thx,

Al

Link to comment
Share on other sites

I appoligize Pete, but I just don't follow what you are patiently trying to tell me. For my example above, how do I define the  "function"?  Is it something like

 

function  Alt_inc(k)

              ipc.control(65823, 77)

              ipc.control(65823)

              k=k-1

end

 

and then use

 

event.timer(80, “Alt_inc(n)")  where I have taken a guess at the msecs value?

                    

How do you cancel the event when n reaches 0?

 

If this is "way off" and only a very few lines of code are involved, can you show me what replaces my original loop above?

 

First off, you don't appear to be referring to the Lua library documentation at all. how can you possibly think “Alt_inc(n)" is the actual function name? The (n) part defines its parameters, it isn't part of the name! Surely you can see that "Alt_inc" is the name?

 

There are plenty of examples provided in the package and a load also posted in the User Contributions subforum. I shouldn't need to have to code things for everyone. Just this once, but please try to do things for yourself next time.

 

where I have taken a guess at the msecs value?

 

Why is "80" a guess? Isn't 80 what you wanted, as shown in your original?

 

Surely you stated that n was determined by earlier code in your program, right? So assuming n has been determined, all you need after that is:

function  Alt_inc()
       ipc.control(65823, 77)
       ipc.control(65823)
       n = n-1
       if n <= 0 then 
              event.cancel("Alt_inc")
       end
end

event.timer(80, "Alt_inc")

Pete

Link to comment
Share on other sites

Thank you.

As you can see, I'm not an expereinced programmer. I now understand my mistake. I thought the initial value of n had to be passed to the function, I see that it does not. The 80ms is my guess at about how long it will take for the function to execute. I'd like the value to be as small as possible while still getting reliabale results. I will determine that by experimentation.

Thanks again,

Al 

Link to comment
Share on other sites

I thought the initial value of n had to be passed to the function, I see that it does not.

 

There's actually no way to pass such a parameter with the event library -- the parameters passed are defined by the type of event, and in the case of the timer event it is the time in mSecs.

 

The value of n is available inside the function because it is a "global" -- it is declared and set outside of any function. If it was only declared within a function then it would be local to that function and not seen outside.

 

Pete

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.