Hi all long time no speak,
I managed to get the above code working (mostly); this means I haven't touched LUA in months and have instead been getting to grips with MATLAB. Unfortunately I now have another requirement, I need to read in data from a continually updating external txt file with one line of data and six separated variables.
I've tried several different approaches but keep drawing a blank. I can obviously create the destination file but am getting stuck with importing the data as the file is left empty (and I can't see were the code is breaking as debugging won't seem to work); it's almost certain that I've got an error with the reading of the external file and the defining of it's variables but, as I'm still an absolute novice I can't see where, any thoughts?
' "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 doesnt exist' ' It will go into the Modules folder because Ive not included a full path (like "C:\....")' ' By using "assert" you get an error message if this fails' ' create the destination file' f = assert(io.open("FSrecord.txt","a+")) f:close() ' 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) gr = ipc.readUB(0x0BE8) '--working - Gear position' sw, os = ipc.readStruct(0x036C, "2UB") '--working - stall warning, overspeed' apM, apwl, apnl, aphl, aphv, apal, apav = ipc.readStruct(0x7BC, "1UB", "3SW", "1UD", "1UB", "1DD") '--working - Autopilot data' tp = ipc.readSW(0x088C)' --working - throttle position' '-- 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) tp = (tp - 96) / 16192 * 100 '--working' gr = gr / 255 '--working' '-- but only log this time IF we arent 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) -- retrieve head postion data -- open data source txt file' f = assert(io.open("C:\Program Files (x86)\NaturalPoint\OptiTrack Camera SDK\samples\VectorTracking\headtrackstream.txt","r")) '-- read data' local x, y, z, yw, p, r = io.read("*number", "*number", "*number", "*number", "*number", "*number") '-- close file' f:close() if (ipc.readUW(0x3364) == 0) and (ipc.readUW(0x0264) == 0) then '-- write a CSV line to the open file' '-- open destination file' f = assert(io.open("FSrecord.txt","a+")) 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,%f,%f,%f,%s,%f,%f,%f,%93.1f,%s,%.1f,%f,%f,%f,%f,%f,%f,%f\n", time,hour, min, sec, hourZ, minZ,lat,lon,alt,pitch,bank,hdgT,hdgM,vs,ias,tas,gs,mach,gr,sw,os,apM,apwl,apnl,aphl,aphv,apal,apav,tp,x,y,z,yw,p,r)) '-- close the destination file' f:write("\n") f:close() 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' [/CODE]