Created
September 19, 2018 04:00
-
-
Save notthetup/3b72e23aa5e5f3e87d692a975bc406fb to your computer and use it in GitHub Desktop.
Calculate live distance between two GPS(NMEA) over TCP devices.
This file contains hidden or 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
#!/usr/bin/env python3 | |
# Calculates distance between two GPS (NMEA) devices sharing location over TCP. | |
# | |
# Dependencies : pynmea2, arlpy | |
# | |
# Usage dist.py <local-ip-address> <local-gps-port> <remote-ip-address> <remote-gps-port> | |
# Usage dist.py <local-ip-address> <remote-ip-address> | |
import sys | |
import socket | |
import time | |
import threading | |
import datetime | |
from arlpy import geo | |
import pynmea2 | |
local_gps_port = 7777 | |
remote_gps_port = 7777 | |
if len(sys.argv) < 3: | |
print("Need atleast 2 IP addresses as arguments") | |
exit(-1); | |
if len(sys.argv) == 4: | |
print("Please indicate both GPS ports") | |
exit(-1); | |
if len(sys.argv) == 5: | |
remote_gps_port = int(sys.argv[4]) | |
local_gps_port = int(sys.argv[2]) | |
remote_ip = sys.argv[3] | |
local_ip = sys.argv[1] | |
else : | |
remote_ip = sys.argv[2] | |
local_ip = sys.argv[1] | |
lat_local = 0 | |
lon_local = 0 | |
lat_remote = 0 | |
lon_remote = 0 | |
def print_dist(): | |
global lat_local, lat_remote, lon_local, lon_remote | |
if (lat_local > 0 and lat_remote > 0 and lon_local > 0 and lon_remote > 0): | |
time = datetime.datetime.now().strftime('%H:%M:%S') | |
dist = geo.distance(geo.pos([lat_local, lon_local]), geo.pos([lat_remote, lon_remote])) | |
print(str(time) + " , " + str(format(dist,'.2f'))) | |
def local_connected(s): | |
global lat_local, lat_remote, lon_local, lon_remote | |
print("Connected to local.") | |
for line in s.makefile('r'): | |
if (line.startswith('$GPGGA')): | |
try: | |
msg = pynmea2.parse(line) | |
except pynmea2.nmea.ParseError as e: | |
continue; | |
lat_local = msg.latitude | |
lon_local = msg.longitude | |
print_dist() | |
def local_recv_proc(): | |
while True: | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); | |
try: | |
s.connect((local_ip, local_gps_port)); | |
local_connected(s) | |
s.close() | |
except Exception as e: | |
pass | |
print("Disconnected to remote. Trying again.") | |
time.sleep(1) | |
def remote_connected(s): | |
global lat_local, lat_remote, lon_local, lon_remote | |
print("Connected to remote.") | |
for line in s.makefile('r'): | |
if (line.startswith('$GPGGA')): | |
try: | |
msg = pynmea2.parse(line) | |
except pynmea2.nmea.ParseError as e: | |
continue; | |
lat_remote = msg.latitude | |
lon_remote = msg.longitude | |
print_dist() | |
def remote_recv_proc(): | |
while True: | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); | |
try: | |
s.connect((remote_ip, remote_gps_port)); | |
remote_connected(s) | |
s.close() | |
except Exception as e: | |
pass | |
print("Disconnected to remote. Trying again.") | |
time.sleep(1) | |
local_recv_thread = threading.Thread(target=local_recv_proc) | |
local_recv_thread.start() | |
remote_recv_thread = threading.Thread(target=remote_recv_proc()) | |
remote_recv_thread.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment