Added getCTRL and getPOSI support to python client.

This commit is contained in:
Jason Watkins
2015-05-19 15:18:24 -07:00
parent 4b6b1d57ce
commit b903bdd90d
2 changed files with 95 additions and 2 deletions

View File

@@ -144,6 +144,28 @@ class XPlaneConnect(object):
self.sendUDP(buffer)
# Position
def getPOSI(self, ac = 0):
'''Gets position information for the specified aircraft.
Args:
ac: The aircraft to set the control surfaces of. 0 is the main/player aircraft.
'''
# Send request
buffer = struct.pack("<4sxB", "GETP", ac)
self.sendUDP(buffer)
# Read response
resultBuf = self.readUDP()
if len(resultBuf) != 34:
raise ValueError("Unexpected response length.")
result = struct.unpack("<4sxBfffffff", resultBuf)
if result[0] != "POSI":
raise ValueError("Unexpected header: " + result[0])
# Drop the header & ac from the return value
return result[2:]
def sendPOSI(self, values, ac = 0):
'''Sets position information on the specified aircraft.
@@ -155,8 +177,8 @@ class XPlaneConnect(object):
* Latitude (deg)
* Longitude (deg)
* Altitude (m above MSL)
* Roll (deg)
* Pitch (deg)
* Roll (deg)
* True Heading (deg)
* Gear (0=up, 1=down)
ac: The aircraft to set the control surfaces of. 0 is the main/player aircraft.
@@ -179,6 +201,29 @@ class XPlaneConnect(object):
self.sendUDP(buffer)
# Controls
def getCTRL(self, ac = 0):
'''Gets the control surface information for the specified aircraft.
Args:
ac: The aircraft to set the control surfaces of. 0 is the main/player aircraft.
'''
# Send request
buffer = struct.pack("<4sxB", "GETC", ac)
self.sendUDP(buffer)
# Read response
resultBuf = self.readUDP()
if len(resultBuf) != 31:
raise ValueError("Unexpected response length.")
result = struct.unpack("<4sxffffbfBf", resultBuf)
if result[0] != "CTRL":
raise ValueError("Unexpected header: " + result[0])
# Drop the header from the return value
result =result[1:7] + result[8:]
return result
def sendCTRL(self, values, ac = 0):
'''Sets control surface information on the specified aircraft.

View File

@@ -202,6 +202,29 @@ class XPCTests(unittest.TestCase):
expected = 0.0
do_test()
def test_getCTRL(self):
values = None
ac = 0
expected = None
def do_test():
with xpc.XPlaneConnect() as client:
# Execute
client.sendCTRL(values, ac)
result = client.getCTRL(ac)
# Test
self.assertEqual(len(result), len(expected))
for a, e in zip(result, expected):
self.assertAlmostEqual(a, e, 4)
values = [0.0, 0.0, 0.0, 0.8, 1.0, 0.5, -1.5]
expected = values
ac = 0
do_test()
ac = 3
do_test()
def test_sendCTRL(self):
# Setup
@@ -270,6 +293,31 @@ class XPCTests(unittest.TestCase):
ctrl[6] = 0.0
do_test()
def test_getPOSI(self):
values = None
ac = 0
expected = None
def do_test():
with xpc.XPlaneConnect() as client:
# Execute
client.pauseSim(True)
client.sendPOSI(values, ac)
result = client.getPOSI(ac)
client.pauseSim(False)
# Test
self.assertEqual(len(result), len(expected))
for a, e in zip(result, expected):
self.assertAlmostEqual(a, e, 4)
values = [ 37.524, -122.06899, 2500, 45, -45, 15, 1 ]
expected = values
ac = 0
do_test()
ac = 3
do_test()
def test_sendPOSI(self):
# Setup
drefs = ["sim/flightmodel/position/latitude",\