Fixed several bugs in Python playbackExample

This commit is contained in:
Jason Watkins
2015-08-17 09:17:45 -07:00
parent 67881bf03b
commit c520132ba6
2 changed files with 21 additions and 16 deletions

View File

@@ -8,18 +8,19 @@ def record(path, interval = 0.1, duration = 60):
print "Unable to open file." print "Unable to open file."
return return
count = duration / interval count = int(duration / interval)
if count < 1: if count < 1:
print "duration is less than a single frame." print "duration is less than a single frame."
return return
with xpc.XPlaneConnect() as client: with xpc.XPlaneConnect("localhost", 49009, 0, 1000) as client:
print "Recording..." print "Recording..."
for i in range(0, count): for i in range(0, count):
try: try:
posi = client.getPOSI() posi = client.getPOSI()
fd.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}\n".format(posi)) fd.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}\n".format(posi))
except: except:
print "Error reading position"
continue continue
sleep(interval); sleep(interval);
print "Recording Complete" print "Recording Complete"
@@ -32,31 +33,32 @@ def playback(path, interval):
print "Unable to open file." print "Unable to open file."
return return
with xpc.XPlaneConnect() as client: with xpc.XPlaneConnect("localhost", 49009, 0, 1000) as client:
print "Starting Playback..." print "Starting Playback..."
for line in fd: for line in fd:
try: try:
posi = [ float(x) for x in line.split(',') ] posi = [ float(x) for x in line.split(',') ]
posi = client.sendPOSI(posi) posi = client.sendPOSI(posi)
except: except:
print "Error sending position"
continue continue
sleep(interval); sleep(interval);
print "Playback Complete" print "Playback Complete"
fd.close() fd.close()
def printMenu(title, opts): def printMenu(title, opts):
print "\n+---------------------------------------------- +\n" print "\n+---------------------------------------------- +"
print "| {0:-42} |\n".format(title) print "| {0:42} |\n".format(title)
print "+---------------------------------------------- +\n" print "+---------------------------------------------- +"
for i in range(0,len(opts)): for i in range(0,len(opts)):
print "| {0:2}. {1:-40} |\n".format(i + 1, opts[i]) print "| {0:2}. {1:40} |".format(i + 1, opts[i])
print "+---------------------------------------------- +\n" print "+---------------------------------------------- +"
return int(raw_input("Please select and option: ")) return int(raw_input("Please select and option: "))
def ex(): def ex():
print "X-Plane Connect Playback Example [Version 1.2.0]\n" print "X-Plane Connect Playback Example [Version 1.2.0]"
print "(c) 2013-2015 United States Government as represented by the Administrator\n" print "(c) 2013-2015 United States Government as represented by the Administrator"
print "of the National Aeronautics and Space Administration. All Rights Reserved.\n" print "of the National Aeronautics and Space Administration. All Rights Reserved."
mainOpts = [ "Record X-Plane", "Playback File", "Exit" ] mainOpts = [ "Record X-Plane", "Playback File", "Exit" ]
@@ -64,14 +66,17 @@ def ex():
opt = printMenu("What would you like to do?", mainOpts) opt = printMenu("What would you like to do?", mainOpts)
if opt == 1: if opt == 1:
path = raw_input("Enter save file path: ") path = raw_input("Enter save file path: ")
interval = int(raw_input("Enter interval between frames (milliseconds): ")) interval = float(raw_input("Enter interval between frames (seconds): "))
duration = int(raw_input("Enter duration to record for (seconds): ")) duration = float(raw_input("Enter duration to record for (seconds): "))
record(path, interval, duration) record(path, interval, duration)
elif opt == 2: elif opt == 2:
path = raw_input("Enter save file path: ") path = raw_input("Enter save file path: ")
interval = int(raw_input("Enter interval between frames (milliseconds): ")) interval = float(raw_input("Enter interval between frames (seconds): "))
playback(path, interval) playback(path, interval)
elif opt == 3: elif opt == 3:
return; return;
else: else:
print "Unrecognized option." print "Unrecognized option."
if __name__ == "__main__":
ex()

View File

@@ -5,7 +5,7 @@
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>3c7a940d-17c8-4e91-882f-9bc8b1d2f54b</ProjectGuid> <ProjectGuid>3c7a940d-17c8-4e91-882f-9bc8b1d2f54b</ProjectGuid>
<ProjectHome>.</ProjectHome> <ProjectHome>.</ProjectHome>
<StartupFile>src\basicExample.py</StartupFile> <StartupFile>src\playbackExample.py</StartupFile>
<SearchPath> <SearchPath>
</SearchPath> </SearchPath>
<WorkingDirectory>.</WorkingDirectory> <WorkingDirectory>.</WorkingDirectory>