Merge pull request #192 from barf/master

Python3 demos using Python2 syntax were run through 2to3, fixes #191
This commit is contained in:
Jason Watkins
2020-04-19 08:44:26 -07:00
committed by GitHub
3 changed files with 41 additions and 41 deletions

View File

@@ -2,8 +2,8 @@ from time import sleep
import xpc import xpc
def ex(): def ex():
print "X-Plane Connect example script" print("X-Plane Connect example script")
print "Setting up simulation" print("Setting up simulation")
with xpc.XPlaneConnect() as client: with xpc.XPlaneConnect() as client:
# Verify connection # Verify connection
try: try:
@@ -11,24 +11,24 @@ def ex():
# will be raised. # will be raised.
client.getDREF("sim/test/test_float") client.getDREF("sim/test/test_float")
except: except:
print "Error establishing connection to X-Plane." print("Error establishing connection to X-Plane.")
print "Exiting..." print("Exiting...")
return return
# Set position of the player aircraft # Set position of the player aircraft
print "Setting position" print("Setting position")
# Lat Lon Alt Pitch Roll Yaw Gear # Lat Lon Alt Pitch Roll Yaw Gear
posi = [37.524, -122.06899, 2500, 0, 0, 0, 1] posi = [37.524, -122.06899, 2500, 0, 0, 0, 1]
client.sendPOSI(posi) client.sendPOSI(posi)
# Set position of a non-player aircraft # Set position of a non-player aircraft
print "Setting NPC position" print("Setting NPC position")
# Lat Lon Alt Pitch Roll Yaw Gear # Lat Lon Alt Pitch Roll Yaw Gear
posi = [37.52465, -122.06899, 2500, 0, 20, 0, 1] posi = [37.52465, -122.06899, 2500, 0, 20, 0, 1]
client.sendPOSI(posi, 1) client.sendPOSI(posi, 1)
# Set angle of attack, velocity, and orientation using the DATA command # Set angle of attack, velocity, and orientation using the DATA command
print "Setting orientation" print("Setting orientation")
data = [\ data = [\
[18, 0, -998, 0, -998, -998, -998, -998, -998],\ [18, 0, -998, 0, -998, -998, -998, -998, -998],\
[ 3, 130, 130, 130, 130, -998, -998, -998, -998],\ [ 3, 130, 130, 130, 130, -998, -998, -998, -998],\
@@ -37,21 +37,21 @@ def ex():
client.sendDATA(data) client.sendDATA(data)
# Set control surfaces and throttle of the player aircraft using sendCTRL # Set control surfaces and throttle of the player aircraft using sendCTRL
print "Setting controls" print("Setting controls")
ctrl = [0.0, 0.0, 0.0, 0.8] ctrl = [0.0, 0.0, 0.0, 0.8]
client.sendCTRL(ctrl) client.sendCTRL(ctrl)
# Pause the sim # Pause the sim
print "Pausing" print("Pausing")
client.pauseSim(True) client.pauseSim(True)
sleep(2) sleep(2)
# Toggle pause state to resume # Toggle pause state to resume
print "Resuming" print("Resuming")
client.pauseSim(False) client.pauseSim(False)
# Stow landing gear using a dataref # Stow landing gear using a dataref
print "Stowing gear" print("Stowing gear")
gear_dref = "sim/cockpit/switches/gear_handle_status" gear_dref = "sim/cockpit/switches/gear_handle_status"
client.sendDREF(gear_dref, 0) client.sendDREF(gear_dref, 0)
@@ -61,12 +61,12 @@ def ex():
# Make sure gear was stowed successfully # Make sure gear was stowed successfully
gear_status = client.getDREF(gear_dref) gear_status = client.getDREF(gear_dref)
if gear_status[0] == 0: if gear_status[0] == 0:
print "Gear stowed" print("Gear stowed")
else: else:
print "Error stowing gear" print("Error stowing gear")
print "End of Python client example" print("End of Python client example")
raw_input("Press any key to exit...") input("Press any key to exit...")
if __name__ == "__main__": if __name__ == "__main__":
ex() ex()

View File

@@ -8,8 +8,8 @@ def monitor():
posi = client.getPOSI(); posi = client.getPOSI();
ctrl = client.getCTRL(); ctrl = client.getCTRL();
print "Loc: (%4f, %4f, %4f) Aileron:%2f Elevator:%2f Rudder:%2f\n"\ print("Loc: (%4f, %4f, %4f) Aileron:%2f Elevator:%2f Rudder:%2f\n"\
% (posi[0], posi[1], posi[2], ctrl[1], ctrl[0], ctrl[2]) % (posi[0], posi[1], posi[2], ctrl[1], ctrl[0], ctrl[2]))
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -5,78 +5,78 @@ def record(path, interval = 0.1, duration = 60):
try: try:
fd = open(path, "w") fd = open(path, "w")
except: except:
print "Unable to open file." print("Unable to open file.")
return return
count = int(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("localhost", 49009, 0, 1000) 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}\n".format(*posi)) fd.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}\n".format(*posi))
except: except:
print "Error reading position" print("Error reading position")
continue continue
sleep(interval); sleep(interval);
print "Recording Complete" print("Recording Complete")
fd.close() fd.close()
def playback(path, interval): def playback(path, interval):
try: try:
fd = open(path, "r") fd = open(path, "r")
except: except:
print "Unable to open file." print("Unable to open file.")
return return
with xpc.XPlaneConnect("localhost", 49009, 0, 1000) 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" 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+---------------------------------------------- +" print("\n+---------------------------------------------- +")
print "| {0:42} |\n".format(title) print("| {0:42} |\n".format(title))
print "+---------------------------------------------- +" print("+---------------------------------------------- +")
for i in range(0,len(opts)): for i in range(0,len(opts)):
print "| {0:2}. {1:40} |".format(i + 1, opts[i]) print("| {0:2}. {1:40} |".format(i + 1, opts[i]))
print "+---------------------------------------------- +" print("+---------------------------------------------- +")
return int(raw_input("Please select and option: ")) return int(input("Please select and option: "))
def ex(): def ex():
print "X-Plane Connect Playback Example [Version 1.2.0]" print("X-Plane Connect Playback Example [Version 1.2.0]")
print "(c) 2013-2015 United States Government as represented by the Administrator" print("(c) 2013-2015 United States Government as represented by the Administrator")
print "of the National Aeronautics and Space Administration. All Rights Reserved." 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" ]
while True: while True:
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 = input("Enter save file path: ")
interval = float(raw_input("Enter interval between frames (seconds): ")) interval = float(input("Enter interval between frames (seconds): "))
duration = float(raw_input("Enter duration to record for (seconds): ")) duration = float(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 = input("Enter save file path: ")
interval = float(raw_input("Enter interval between frames (seconds): ")) interval = float(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__": if __name__ == "__main__":
ex() ex()