Added example for the Python client.
This commit is contained in:
72
Python/src/example.py
Normal file
72
Python/src/example.py
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
from time import sleep
|
||||||
|
import xpc
|
||||||
|
|
||||||
|
def ex():
|
||||||
|
print "X-Plane Connect example script"
|
||||||
|
print "Setting up simulation"
|
||||||
|
with xpc.XPlaneConnect() as client:
|
||||||
|
# Verify connection
|
||||||
|
try:
|
||||||
|
# If X-Plane does not respond to the request, a timeout error
|
||||||
|
# will be raised.
|
||||||
|
client.getDREF("sim/test/test_float")
|
||||||
|
except:
|
||||||
|
print "Error establishing connection to X-Plane."
|
||||||
|
print "Exiting..."
|
||||||
|
return
|
||||||
|
|
||||||
|
# Set position of the player aircraft
|
||||||
|
print "Setting position"
|
||||||
|
# Lat Lon Alt Pitch Roll Yaw Gear
|
||||||
|
posi = [37.524, -122.06899, 2500, 0, 0, 0, 1]
|
||||||
|
client.sendPOSI(posi)
|
||||||
|
|
||||||
|
# Set position of a non-player aircraft
|
||||||
|
print "Setting NPC position"
|
||||||
|
# Lat Lon Alt Pitch Roll Yaw Gear
|
||||||
|
posi = [37.52465, -122.06899, 2500, 0, 20, 0, 1]
|
||||||
|
client.sendPOSI(posi, 1)
|
||||||
|
|
||||||
|
# Set angle of attack, velocity, and orientation using the DATA command
|
||||||
|
print "Setting orientation"
|
||||||
|
data = [\
|
||||||
|
[18, 0, -998, 0, -998, -998, -998, -998, -998],\
|
||||||
|
[ 3, 130, 130, 130, 130, -998, -998, -998, -998],\
|
||||||
|
[16, 0, 0, 0, -998, -998, -998, -998, -998]\
|
||||||
|
]
|
||||||
|
client.sendDATA(data)
|
||||||
|
|
||||||
|
# Set control surfaces and throttle of the player aircraft using sendCTRL
|
||||||
|
print "Setting controls"
|
||||||
|
ctrl = [0.0, 0.0, 0.0, 0.8]
|
||||||
|
client.sendCTRL(ctrl)
|
||||||
|
|
||||||
|
# Pause the sim
|
||||||
|
print "Pausing"
|
||||||
|
client.pauseSim(True)
|
||||||
|
sleep(2)
|
||||||
|
|
||||||
|
# Toggle pause state to resume
|
||||||
|
print "Resuming"
|
||||||
|
client.pauseSim(False)
|
||||||
|
|
||||||
|
# Stow landing gear using a dataref
|
||||||
|
print "Stowing gear"
|
||||||
|
gear_dref = "sim/cockpit/switches/gear_handle_status"
|
||||||
|
client.sendDREF(gear_dref, 0)
|
||||||
|
|
||||||
|
# Let the sim run for a bit.
|
||||||
|
sleep(4)
|
||||||
|
|
||||||
|
# Make sure gear was stowed successfully
|
||||||
|
gear_status = client.getDREF(gear_dref)
|
||||||
|
if gear_status[0] == 0:
|
||||||
|
print "Gear stowed"
|
||||||
|
else:
|
||||||
|
print "Error stowing gear"
|
||||||
|
|
||||||
|
print "End of Python client example"
|
||||||
|
raw_input("Press any key to exit...")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
ex()
|
||||||
@@ -43,6 +43,13 @@ class XPlaneConnect(object):
|
|||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
|
# Define __enter__ and __exit__ to support the `with` construct.
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, type, value, traceback):
|
||||||
|
self.close()
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
'''Closes the specified connection and releases resources associated with it.'''
|
'''Closes the specified connection and releases resources associated with it.'''
|
||||||
if self.socket is not None:
|
if self.socket is not None:
|
||||||
|
|||||||
@@ -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\xpc.py</StartupFile>
|
<StartupFile>src\example.py</StartupFile>
|
||||||
<SearchPath>
|
<SearchPath>
|
||||||
</SearchPath>
|
</SearchPath>
|
||||||
<WorkingDirectory>.</WorkingDirectory>
|
<WorkingDirectory>.</WorkingDirectory>
|
||||||
|
|||||||
Reference in New Issue
Block a user