-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/ab#2198 add speed and smooth location #20
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0aaf5e7
Fix processing of duplicate GGA messages
flonix8 2c9fded
Add gps filter (and config); Add test track
flonix8 a10db41
Update vision-api to 3.6.0; Refactor source structure; Add gps_read_t…
flonix8 ad0439f
Improve settings template comment
flonix8 62da9f3
Remove support for serial reader (we're not using it); Add reader tests
flonix8 01ee43e
Refactor readers; Add config timing-related config options to readers…
flonix8 29d3de9
Add test
flonix8 94b3834
Update changelog
flonix8 6edfa8e
Add link to Wikipedia page for alpha beta filter
flonix8 ccea589
Fix link
flonix8 90bcd21
Extend comment on calculations
flonix8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Empty file.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import math | ||
| from typing import NamedTuple | ||
| from datetime import datetime | ||
| from .util import ll_to_xy, xy_to_ll | ||
|
|
||
|
|
||
| class GPSFilterState(NamedTuple): | ||
| timestamp: datetime | ||
| lat: float | ||
| lon: float | ||
| speed_kmh: float | ||
| heading_deg: float | ||
|
|
||
|
|
||
| class GPSFilter: | ||
| ''' | ||
| An implementation of an alpha-beta filter for GPS position smoothing with additional spike rejection. | ||
| See: https://en.wikipedia.org/wiki/Alpha_beta_filter | ||
| ''' | ||
| def __init__( | ||
| self, | ||
| alpha=0.75, | ||
| beta=0.05, | ||
| spike_radius_m=80.0, | ||
| ): | ||
| self.alpha = alpha | ||
| self.beta = beta | ||
|
|
||
| self.spike_radius_m = spike_radius_m | ||
|
|
||
| self._init_params() | ||
|
|
||
| def _init_params(self): | ||
| self.origin_lat = None | ||
| self.origin_lon = None | ||
|
|
||
| self.x = None | ||
| self.y = None | ||
| self.vx = 0.0 | ||
| self.vy = 0.0 | ||
|
|
||
| self.last_t = None | ||
| self.stop_timer = 0.0 | ||
|
|
||
| def reset(self): | ||
| self._init_params() | ||
|
|
||
| def update(self, lat: float, lon: float, timestamp_ms: float) -> GPSFilterState: | ||
| """ | ||
| lat/lon in decimal degrees | ||
| timestamp_ms = timestamp in milliseconds | ||
| """ | ||
|
|
||
| t = timestamp_ms / 1000.0 | ||
|
|
||
| # first point initializes system | ||
| if self.origin_lat is None: | ||
| self.origin_lat = lat | ||
| self.origin_lon = lon | ||
|
|
||
| self.x = 0.0 | ||
| self.y = 0.0 | ||
| self.last_t = t | ||
|
|
||
| return GPSFilterState(timestamp=datetime.fromtimestamp(t), lat=lat, lon=lon, speed_kmh=0.0, heading_deg=0.0) | ||
|
|
||
| # convert measurement to local coordinates | ||
| x_meas, y_meas = ll_to_xy(lat, lon, self.origin_lat, self.origin_lon) | ||
|
|
||
| dt = t - self.last_t | ||
| if dt <= 0: | ||
| dt = 1e-3 | ||
|
|
||
| # ---------------------------- | ||
| # Predict | ||
| # ---------------------------- | ||
| x_pred = self.x + self.vx * dt | ||
| y_pred = self.y + self.vy * dt | ||
|
|
||
| # residual | ||
| rx = x_meas - x_pred | ||
| ry = y_meas - y_pred | ||
|
|
||
| residual_dist = math.hypot(rx, ry) | ||
|
|
||
| # ---------------------------- | ||
| # Spike rejection | ||
| # ---------------------------- | ||
| if residual_dist > self.spike_radius_m: | ||
| # ignore impossible jump | ||
| rx = 0.0 | ||
| ry = 0.0 | ||
|
|
||
| # ---------------------------- | ||
| # Correct location and velocity | ||
| # ---------------------------- | ||
| self.x = x_pred + self.alpha * rx | ||
| self.y = y_pred + self.alpha * ry | ||
|
|
||
| self.vx = self.vx + self.beta * rx / dt | ||
| self.vy = self.vy + self.beta * ry / dt | ||
|
|
||
| self.last_t = t | ||
|
|
||
| # ---------------------------- | ||
| # Derived values | ||
| # ---------------------------- | ||
| speed = math.hypot(self.vx, self.vy) | ||
|
|
||
| heading = math.degrees(math.atan2(self.vx, self.vy)) | ||
| heading = (heading + 360) % 360 | ||
|
|
||
| out_lat, out_lon = xy_to_ll(self.x, self.y, self.origin_lat, self.origin_lon) | ||
|
|
||
| return GPSFilterState( | ||
| timestamp=datetime.fromtimestamp(self.last_t), | ||
| lat=out_lat, | ||
| lon=out_lon, | ||
| speed_kmh=speed * 3.6, | ||
| heading_deg=heading, | ||
| ) |
Empty file.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import math | ||
| from typing import Tuple | ||
|
|
||
| EARTH_RADIUS = 6378137.0 # meters | ||
|
|
||
| def distance_m(lat1: float, lon1: float, lat2: float, lon2: float) -> float: | ||
| # use ll_to_xy to convert to local meters and then compute distance from origin | ||
| dx, dy = ll_to_xy(lat1, lon1, lat2, lon2) | ||
| return math.hypot(dx, dy) | ||
|
|
||
| def ll_to_xy(lat: float, lon: float, origin_lat: float, origin_lon: float) -> Tuple[float, float]: | ||
| """ | ||
| Convert lat/lon to local meters (i.e. distance relative to origin) | ||
| """ | ||
| dlat = math.radians(lat - origin_lat) | ||
| dlon = math.radians(lon - origin_lon) | ||
|
|
||
| mean_lat = math.radians((lat + origin_lat) / 2) | ||
|
|
||
| x = EARTH_RADIUS * dlon * math.cos(mean_lat) | ||
| y = EARTH_RADIUS * dlat | ||
| return x, y | ||
|
|
||
| def xy_to_ll(x: float, y: float, origin_lat: float, origin_lon: float) -> Tuple[float, float]: | ||
| """ | ||
| Convert local meters (i.e. distance relative to origin) to lat/lon | ||
| """ | ||
| lat = origin_lat + math.degrees(y / EARTH_RADIUS) | ||
|
|
||
| mean_lat = math.radians((lat + origin_lat) / 2) | ||
|
|
||
| lon = origin_lon + math.degrees( | ||
| x / (EARTH_RADIUS * math.cos(mean_lat)) | ||
| ) | ||
|
|
||
| return lat, lon |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.