Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added average/median gps smoothing #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions libs/Location.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def __init__(self, ip, port):
self.bearing = 0.0
self.running = True
self.all_zero = True
self.wait_time = 1
self.gps_refresh = 0.05
self.wait_time = 0.5
self.num_coords = 11

def config(self):
# read from a file, probably configure this to work with
Expand All @@ -31,7 +33,8 @@ def config(self):

# Returns distance in kilometers between given latitude and longitude
def distance_to(self, lat:float, lon:float):
earth_radius = 6371.301
# https://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html (earth radii numbers)
earth_radius = 6371.000 # NASA - Volumetric mean radius (km) (+- 10 km variability)
delta_lat = (lat - self.latitude) * (pi/180.0)
delta_lon = (lon - self.longitude) * (pi/180.0)

Expand Down Expand Up @@ -72,17 +75,37 @@ def update_fields_loop(self):
self.old_latitude = self.latitude
self.old_longitude = self.longitude

self.latitude = gps.get_latitude()
self.longitude = gps.get_longitude()
# smoothing patch fix number 1: find average of 11 (parity doesn't matter, should probably be around 10) gps coords
lat_sum = 0
long_sum = 0
for i in range(self.num_coords): # gets gps coords
lat_sum += gps.get_latitude() # adds lat and long to respective sums
long_sum += gps.get_longitude()
sleep(self.gps_refresh)
self.latitude = lat_sum / self.num_coords # sets lat and long as the average of the sums
self.longitude = long_sum / self.num_coords

# smoothing patch fix number 2: find median of 11 (other values such as 7 or 5 could work) gps coords
#lat_list = [] # lists to hold 11 lats and longs
#long_list = []
#for i in range (num_coords): # gets 11 gps coords
# lat_list.append(self.get_latitude()) # adds lat and longs to respective lists
# long_list.append(self.get_longitude())
# sleep(self.gps_refresh)
#lat_list.sort() # sort lists
#long_list.sort()
#self.latitude = lat_list[num_coords/2] # sets lat and long as medians from lat and long lists
#self.longitude = long_list[num_coords/2]

self.height = gps.get_height()
self.time = gps.get_time()
self.error = gps.get_error()
self.bearing = self.calc_bearing(self.old_latitude, self.old_longitude, self.latitude, self.longitude)
self.all_zero = False
else:
all_zero = True
self.all_zero = True
# maybe print info or sleep or something idk
sleep(self.wait_time)
sleep(self.wait_time) # currently have it at .5 seconds, 1 might be too slow (.1 might be a good option as well)
return

# Calculates bearing between two points.
Expand All @@ -95,7 +118,8 @@ def calc_bearing(self,lat1:float, lon1:float, lat2:float, lon2:float):
# Calculate latitutde and longitude given distance (in km) and bearing (in degrees)
def get_coordinates(self, distance:float, bearing:float):
# https://stackoverflow.com/questions/7222382/get-lat-long-given-current-point-distance-and-bearing
R = 6371.301
# https://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html (earth radii numbers)
R = 6371.000 # NASA - Volumetric mean radius (km) (+- 10 km variability)
brng = radians(bearing) # Assuming bearing is in degrees
d = distance

Expand Down
Loading