Created
February 11, 2020 09:58
-
-
Save moser/a2c9999230bd185562aadf386c2c1a19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Reads GPS data from gpsfeed+ (https://sourceforge.net/projects/gpsfeed/), adds | |
some fake FLARM and OpenVario data and sends this on to XCSoar. | |
""" | |
import socket | |
import functools | |
import time | |
import datetime as dt | |
XCSOAR_PORT = 4353 | |
GPSFEED_PORT = 2222 | |
def calculate_nmea_checksum(raw_sentence): | |
return ( | |
"%0.2X" % | |
functools.reduce(lambda a, b: a ^ b, (ord(char) for char in raw_sentence)) | |
) | |
def flarm_stat(**kw): | |
yield "PFLAU,1,1,1,1,2,-30,2,-32,300" | |
def flarm_target(**kw): | |
lines = """ | |
PFLAA,0,-315,338,235,2,DDD335,201,,33,0.6,1 | |
PFLAA,0,-253,442,398,2,DDC194,195,,38,1.2,1 | |
PFLAA,0,-392,-6,326,2,DDAECF,294,,32,0.4,1 | |
PFLAA,0,-237,-70,315,2,DD8403,337,,24,2.3,1 | |
PFLAA,0,-435,238,229,2,DDAF22,228,,31,-1.8,1 | |
PFLAA,0,-1533,451,39,2,DDDC55,325,,27,-2.2,1 | |
PFLAA,0,-51,-68,328,2,DD94C2,21,,25,2.5,1 | |
PFLAA,0,-120,386,405,2,DDD8E9,142,,33,0.2,1 | |
PFLAU,6,1,2,1,0,144,0,235,446 | |
""" | |
for line in lines.splitlines(): | |
line = line.strip() | |
if line: | |
yield line | |
def ov_vario(**kw): | |
yield "POV,E,+1.00" | |
def ov_alt_ias(**kw): | |
yield "POV,P,1000.00,Q,40.00" | |
def format_nmea_msg(raw_sentence): | |
return "${}*{}".format(raw_sentence, calculate_nmea_checksum(raw_sentence)) | |
def main(): | |
xconn = None | |
feedconn = None | |
buffers = "" | |
while True: | |
if not xconn: | |
try: | |
xconn = socket.create_connection(('127.0.0.1', XCSOAR_PORT)) | |
except Exception as exc: | |
print("failed to connect to XCSoar", exc) | |
if not feedconn: | |
try: | |
# run gpsfeed+ somewhere (you can also run it on your PC if your OV device has network access) | |
feedconn = socket.create_connection(('127.0.0.1', GPSFEED_PORT)) | |
except Exception as exc: | |
print("failed to connect to gpsfeed", exc) | |
try: | |
if feedconn: | |
buff = feedconn.recv(4096) | |
if buffers: | |
buff = buffers + buff | |
buffers = "" | |
buff_lines = buff.splitlines() | |
for line in buff.splitlines(True): | |
if line.endswith("\r\n"): | |
buff_lines.append(line.strip()) | |
else: # only the last "line" | |
buffers = buffers + line | |
if xconn: | |
for line in buff_lines: | |
xconn.send((line + "\r\n").encode()) | |
#for msg_factory in [flarm_stat, flarm_target, ov_vario, ov_alt_ias]: | |
for msg_factory in [flarm_target]: | |
for msg in msg_factory(): | |
xconn.send((format_nmea_msg(msg) + "\r\n").encode()) | |
time.sleep(0.1) | |
except Exception as exc: | |
print("send failed", exc) | |
xconn = None | |
feedconn = None | |
buffers = "" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment