From 85160a1e382e0d424437c581b45b38c76dc1b497 Mon Sep 17 00:00:00 2001 From: axkralj990 Date: Sun, 20 Nov 2022 12:13:41 +0100 Subject: [PATCH 1/3] Added get terrain elevation function for Python3 --- Python3/src/xpc.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Python3/src/xpc.py b/Python3/src/xpc.py index 56671017..ed0547cc 100644 --- a/Python3/src/xpc.py +++ b/Python3/src/xpc.py @@ -445,6 +445,46 @@ def sendCOMM(self, comm): # Send self.sendUDP(buffer) + # Terrain + + def getTERR(self, values, ac=0): + """Gets terrain information for the specified aircraft. + + Args: + values: The position values of the desired terrain location. `values` is a array + containing 2 elements. The elements in `values` correspond to the + following: + * Latitude (deg) + * Longitude (deg) + ac: The aircraft to set the position of. 0 is the main/player aircraft. + """ + # Pack message + buffer = struct.pack(b"<4sxB", b"GETT", ac) + for i in range(3): + val = -998 + if i < len(values): + val = values[i] + if i < 3: + buffer += struct.pack(b" Date: Sun, 20 Nov 2022 13:00:20 +0100 Subject: [PATCH 2/3] Added sendPOST (set position, get terrain info) for Python3 --- Python3/src/xpc.py | 51 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/Python3/src/xpc.py b/Python3/src/xpc.py index ed0547cc..83d613e8 100644 --- a/Python3/src/xpc.py +++ b/Python3/src/xpc.py @@ -446,7 +446,6 @@ def sendCOMM(self, comm): self.sendUDP(buffer) # Terrain - def getTERR(self, values, ac=0): """Gets terrain information for the specified aircraft. @@ -485,6 +484,56 @@ def getTERR(self, values, ac=0): # Drop the header & ac from the return value return result[2:] + def sendPOST(self, values, ac=0): + """Sets position information on the specified aircraft and returns terrain information. + + Args: + values: The position values to set. `values` is a array containing up to + 7 elements. If less than 7 elements are specified or any elment is set to `-998`, + those values will not be changed. The elements in `values` corespond to the + following: + * Latitude (deg) + * Longitude (deg) + * Altitude (m above MSL) + * Pitch (deg) + * Roll (deg) + * True Heading (deg) + * Gear (0=up, 1=down) + ac: The aircraft to set the position of. 0 is the main/player aircraft. + """ + # Preconditions + if len(values) < 1 or len(values) > 7: + raise ValueError("Must have between 0 and 7 items in values.") + if ac < 0 or ac > 20: + raise ValueError("Aircraft number must be between 0 and 20.") + + # Pack message + buffer = struct.pack(b"<4sxB", b"POST", ac) + for i in range(7): + val = -998 + if i < len(values): + val = values[i] + if i < 3: + buffer += struct.pack(b" Date: Tue, 22 Nov 2022 19:19:32 +0100 Subject: [PATCH 3/3] Fixed the UDP terrain response unpacking (now it gets the entire response) --- Python3/src/xpc.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Python3/src/xpc.py b/Python3/src/xpc.py index 83d613e8..e06d9e56 100644 --- a/Python3/src/xpc.py +++ b/Python3/src/xpc.py @@ -456,6 +456,13 @@ def getTERR(self, values, ac=0): * Latitude (deg) * Longitude (deg) ac: The aircraft to set the position of. 0 is the main/player aircraft. + + Returns: A 12-element array containing terrain information. The format of the output is + [Lat, Lon, Alt, Nx, Ny, Nz, Vx, Vy, Vz, wet result]. The first three are for output of + the Lat and Lon of the aircraft with the terrain height directly below. The next three + represent the terrain normal. The next three represent the velocity of the terrain. + The wet variable is 0.0 if the terrain is dry and 1.0 if wet. + The last element is the terrain probe result parameter. """ # Pack message buffer = struct.pack(b"<4sxB", b"GETT", ac) @@ -474,7 +481,7 @@ def getTERR(self, values, ac=0): # Read response resultBuf = self.readUDP() if len(resultBuf) == 62: - result = struct.unpack(b"<4sxBdddfffff",resultBuf[0:50]) + result = struct.unpack(b"<4sxBdddffffffff",resultBuf[0:62]) else: raise ValueError("Unexpected response length.") @@ -489,8 +496,8 @@ def sendPOST(self, values, ac=0): Args: values: The position values to set. `values` is a array containing up to - 7 elements. If less than 7 elements are specified or any elment is set to `-998`, - those values will not be changed. The elements in `values` corespond to the + 7 elements. If less than 7 elements are specified or any element is set to `-998`, + those values will not be changed. The elements in `values` correspond to the following: * Latitude (deg) * Longitude (deg) @@ -500,6 +507,8 @@ def sendPOST(self, values, ac=0): * True Heading (deg) * Gear (0=up, 1=down) ac: The aircraft to set the position of. 0 is the main/player aircraft. + + Returns: A 12-element array containing terrain information (described in getTERR). """ # Preconditions if len(values) < 1 or len(values) > 7: @@ -524,7 +533,7 @@ def sendPOST(self, values, ac=0): # Read response resultBuf = self.readUDP() if len(resultBuf) == 62: - result = struct.unpack(b"<4sxBdddfffff",resultBuf[0:50]) + result = struct.unpack(b"<4sxBdddffffffff",resultBuf[0:62]) else: raise ValueError("Unexpected response length.")