Jump to content
The simFlight Network Forums

Small timer intervals techniques.


Recommended Posts

Hi All!

As i 've been programming with fsuipc for only 4 days i want to discuss with the more experienced users here the tecqniques with which i can optimize my code for speed.

I want to be able to have intervals in timer controls around 50-100 ms and to be able to process info like latitude,longtitude,speed,height,v/s and in parallel to write a couple of lines in a text file in EVERY tick event .

I am using Visual Basic and Visual Studio 2008...

Am i asking too many things or there is a way?

As i am now it works in an acceptable way but i have some stuttering even in 200ms interval....

During this procedure i am disabling the functions that print on screen the variables but still.....

i am declaring around 25 offsets and i am not disconnecting any of them...I prefer not to if i have other options..

Any ideas?

Elias

Link to comment
Share on other sites

I want to be able to have intervals in timer controls around 50-100 ms and to be able to process info like latitude,longtitude,speed,height,v/s and in parallel to write a couple of lines in a text file in EVERY tick event .

Yes, you can do that. In fact there is a Lua plug-in supplied with FSUIPC which does just that, and with a lot more information too. It is one of the examples in the Lua Plugins package -- it actually creates a CSV file, loadable into Excel, for example, but it could easily be adapted any way you like.

I am using Visual Basic and Visual Studio 2008...

Okay. It is also easily possible with that. Even over a Network you should be able to attain 100 mSecs, often quicker, but more variable. If you want it really regular the Lua plug-in method can't be beaten as it is running inside FS's process and saves the unpredictable affects of process changes and message queues.

As i am now it works in an acceptable way but i have some stuttering even in 200ms interval....

Variable intervals are inevitable with separate processes -- it's the way Windows timesharing and message passing works. If you needed definite regular intervals you'd probably need to interpolate.

During this procedure i am disabling the functions that print on screen the variables but still.....

i am declaring around 25 offsets and i am not disconnecting any of them...I prefer not to if i have other options..

Sorry, I don't understand this part. "disconnecting"?

You aren't reading them all with separate Process calls to FSUIPC are you? You should build the total request per cycle, using FSUIPC_Read (which takes no time at all as it is only an internal data build function), the do the one FSUIPC_Process call per loop. Of course, only Open and Close the connection at the beginning and end of the program, respectively.

Regards

Pete

Link to comment
Share on other sites

Hi!

1) i have to read about lua plug ins since i havent..:)

2) i already create a file..it is a .kml for google earth not excel.

3) when i say disconnecting i mean that i use the .net dll version you provide..

And yes i am doing one fsuipc.process per timer tick and at any time i have the offsets that i want, enabled. For all the others i am disconnecting them using fs_var.disconnect....

So from what you are saying if my code is properly written then i should expect erratic behaviour except if i use the lua mystery?

am i correct?

Elias

Link to comment
Share on other sites

1) i have to read about lua plug ins since i havent..:)

You could just try the examples provided. With the Lua files in the FS Modules folder, you can assign them to button or keypresses, as "Lua "

2) i already create a file..it is a .kml for google earth not excel.

Okay. File creation in Lua is easy too -- take a look at the example. Here, I'll print it out for you (it's the "record to csv.lua" file):

-- "Record to CSV" example data logging LUA plug-in, by Pete Dowson, September 2008

-- Open an exisitng "FSrecord.csv" file to append to, or create it if it doesn't exist
-- It will go into the Modules folder because I've not included a full path (like "C:\....")
-- By using "assert" you get an error message if this fails

f = assert(io.open("FSrecord.csv","a+"))


-- write the CSV column headings
f:write("\r\nmsecs,timeL,timeZ,lat,lon,alt(ft),pitch,bank,hdgT,hdgM,vs,ias,tas,gs,mach\n")

--  note the elapsed mSecs count now so can provide relative mSec timing column
time0 = ipc.elapsedtime()

-- Loop until our Flag 0 is set (by assigned FSUIPC control)
while not ipc.testflag(0) do

-- Set the timestamp for this loop
time = ipc.elapsedtime() - time0

-- Read all the data we want from FSUIPC
hour, min, sec, hourZ, minZ  = ipc.readStruct(0x238, "5UB")
gs, tas, ias = ipc.readStruct(0x02B4, "3UD")
lat, lon, alt, pitch, bank, hdgT = ipc.readStruct(0x0560,"3DD", "2SD", "1UD")
mach = ipc.readUW(0x11C6)
vs = ipc.readSW(0x842)

-- now convert them all from FS units into those we normally recognise
gs = (gs * 3600) / (65536 * 1852)
tas = tas / 128
ias = ias / 128
mach = mach / 20480
vs = vs * -3.28084

lat = lat * 90 / (10001750 * 65536 * 65536)
lon = lon * 360 /  (65536 * 65536 * 65536 * 65536)
alt = alt * 3.28084 / (65536 * 65536)
pitch = pitch * 360 / (65536 * 65536)
bank = bank * 360 / (65536 * 65536)
hdgM = hdgT - (ipc.readSW(0x02A0) * 65536)
hdgM = hdgM * 360 / (65536 * 65536)
hdgT = hdgT * 360 / (65536 * 65536)

-- but only log this time IF we aren't in an FS menu, or loading scenery
-- (check the "ready-to-fly" flag word at 3364)
-- and provided we are not paused (flagged at 0264)


if (ipc.readUW(0x3364) == 0) and (ipc.readUW(0x0264) == 0) then

   -- write a CSV line to the open file
   f:write(string.format("%d,%02d:%02d:%02d,%02d%02dZ,%02.4f,%03.4f,%.1f,%.2f,%.2f,%03.1f,%93.1f,%d,%d,%d,%d,%.3f\n",
	time,hour, min, sec, hourZ, minZ,lat,lon,alt,pitch,bank,hdgT,hdgM,vs,ias,tas,gs,mach))
end

-- 20 times per second, roughly (allow 2 mSecs overhead)
ipc.sleep(48)

end

-- tidy up at end ...
f:write("\n")
f:close()

-- end of example program

Note that this runs forever or until you set its Flog 0 (using a key or button assigned to the "LuaSet record to csv" assignment with parameter 0 (from the Flag number).

3) when i say disconnecting i mean that i use the .net dll version you provide..

Ah, not so much I (I don't know .Net managed languages I'm afraid) as other kind users donating their work.

And yes i am doing one fsuipc.process per timer tick and at any time i have the offsets that i want, enabled. For all the others i am disconnecting them using fs_var.disconnect....

Yes, well, I don't understand that stuff. Sorry.

So from what you are saying if my code is properly written then i should expect erratic behaviour except if i use the lua mystery?

Not sure "erratic" is the correct word. All I'm saying is that you certainly should be able to read stuff at fairly close intervals, but that those intervals won't be all the same because of the many unpredictable timing changes which are inevitable. If you have a multicore PC running fast enough for FS to run with ease, then you might expect a more regular service, but seeing as there a process switches and message queues to contend with on every transaction, then apart from whatever might be loading FS down, you aren't going to get precise intervals. It would be easier to achieve in Lua because there are no process switches or message queues involved, but even that is subject to FS's performance needs as they vary.

In the end you have to accept what you get, then if you need precise intervals, compute those intermediately timed values by interpolation.

Have to tried using FSInterogate in a reading loop with the same set of values, to see how regular that appears? You can reduce its polling interval, I think, to quite a low number of mSecs. FSInterrogate will be more efficient, code-wise, as it it Native code, written and compiled in Delphi. (I never did like managed code. Too inefficient an idea for my tastes ;-) ).

Regards

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.