Jump to content
Sign in to follow this  
Zach Decou

Timer: Practical Example?

Recommended Posts

My plugin has multiple automated control surface deflection functions that are called when certain conditions are met. For instance- my auto-flap function has separate deploy and retract schedules that get called when the ASI trend reaches a certain value. This is the scripts way of reading the pilots mind; "I see you are accelerating at this rate, so I will retract flaps as though you are climbing out". Problem is, during approach, it may be necessary to accelerate briefly (to avoid mountain goats... and mountains). I would like to set up a timer that starts when a dataref value is reached, and who's own value can be used to call a function. 

if ASItrend == a then

timer start

if timer value > b then

autoflapRetract()

[...]

 

...I'm not exactly sure how the "if's" would look, but the main thing is getting the timer to work. I found the timer on the wiki, but as I am still very new to the lingo, am not able to turn it into something useful. 

Any tips? Thanks!

Share this post


Link to post
Share on other sites

Hi,

I setup my own timer in order to trigger something, if a certain time condition is met. I did it the follwoing way:


dr_time = xp.getDataref("sim/time/total_running_time_sec")

time_current = 0
time_last = 0
delta_time = 0

function set_something()
    local time_current = xp.getFloat( dr_time )
    local delta_time = time_current - time_last

    if (delta_time > 200) then
         set_control()
    end

    local time_last = time_current
end

You might use a different time, but this is an example how I used it so far.

Hoe that helps.

-Pete

Share this post


Link to post
Share on other sites

More recent documentation here:

http://benrussell.github.com/Gizmo/dox/html/files.html

Timers:

http://benrussell.github.com/Gizmo/dox/html/api__timer_8cpp.html



function main()

 --declare our variable.
 --Filling it with nil will make Lua garbage collect it if we don't do something useful with it by the end of this code.

 tmr_foo = nil

 if ASI_trend > a then
   --create a new timer due to go off in one second
   tmr_foo = timer.newTimer( "do_stuff_delayed", 1 )
 else ASI_trend < b then
   timer.stop( tmr_foo )
 end

end --main()



function do_stuff_delayed()
--this will execute 1 second after the ASI trend is detected, unless the ASI trend met our 'cancel criteria
--and timer.stop was called.

 xp.setFloat( dr_whatever, 1.234 )

end

Something like that?

Share this post


Link to post
Share on other sites

or this

....somewhere in the middle, is the solution.



function main()

 if ASI_trend > a then
    if( tmr_foo == nil )then --the timer is NOT runnning.
     --create a new timer due to go off in one second
      -- a "one shot" timer will call its function once, if the timer interval is reached, and then destroy itself.
 tmr_foo = timer.newOneShot( "do_stuff_delayed", 1 )
    end
 else ASI_trend < b then

    --check to see we have a timer to actually bother stopping...
    if( tmr_foo ~= nil )then
     timer.stop( tmr_foo )
    end
 end

end --main()



function do_stuff_delayed()
--this will execute 1 second after the ASI trend is detected, unless the ASI trend met our 'cancel criteria
--and timer.stop was called.

 xp.setFloat( dr_whatever, 1.234 )

end

...some means to check your timer handle is actually valid would be nice. Not sure what happens if you pass "blank" ones around. (Nil, etc)

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×