From ab5f4ed3de22e77a1ae33730d5d146bc0d39b34b Mon Sep 17 00:00:00 2001 From: frederic wagner Date: Tue, 19 Jul 2022 10:05:34 +0200 Subject: [PATCH] buzz --- apps/gipy/ChangeLog | 4 +++ apps/gipy/TODO | 18 +++++------- apps/gipy/app.js | 65 ++++++++++++++++++++++++++++++++--------- apps/gipy/metadata.json | 2 +- 4 files changed, 63 insertions(+), 26 deletions(-) create mode 100644 apps/gipy/ChangeLog diff --git a/apps/gipy/ChangeLog b/apps/gipy/ChangeLog new file mode 100644 index 000000000..f3c377e06 --- /dev/null +++ b/apps/gipy/ChangeLog @@ -0,0 +1,4 @@ +0.01: Initial code +0.05: We now buzz before reaching a waypoint. Display is only updated when not +locked. We detect leaving path and finding path again. We display remaining distance to +next point. diff --git a/apps/gipy/TODO b/apps/gipy/TODO index 360ae0da5..a5d534b63 100644 --- a/apps/gipy/TODO +++ b/apps/gipy/TODO @@ -1,17 +1,13 @@ +- nearest_segment : use highest_completed_segment to disambiguate +- gps direction is weak when speed is low +- add a version number to gpc files +- water points -- detect reached waypoints -- beep when reaching waypoint -- display distance to next waypoint -- display average speed +- store several tracks - turn off gps when moving to next waypoint -- beep when moving away from path + +- display average speed - dynamic map rescale - display scale (100m) -- store several tracks - -- water points - - compress path - -- add a version number to gpc files diff --git a/apps/gipy/app.js b/apps/gipy/app.js index ebbb37965..a4c005073 100644 --- a/apps/gipy/app.js +++ b/apps/gipy/app.js @@ -4,10 +4,14 @@ let simulated = false; class Status { constructor(path) { this.path = path; + this.on_path = false; // are we on the path or lost ? this.position = null; // where we are this.cos_direction = null; // cos of where we look at this.sin_direction = null; // sin of where we look at this.current_segment = null; // which segment is closest + this.highest_completed_segment = -1; // remember what we already acomplished to disambiguate nearest path when some segments are takend in both directions + this.reaching = null; // which waypoint are we reaching ? + this.distance_to_next_point = null; // how far are we from next point ? let r = [0]; // let's do a reversed prefix computations on all distances: @@ -23,26 +27,61 @@ class Status { this.remaining_distances = r; // how much distance remains at start of each segment } update_position(new_position, direction) { + if (Bangle.isLocked() && this.position !== null && new_position.lon == this.position.lon && new_position.lat == this.position.lat) { return; } + this.cos_direction = Math.cos(-direction - Math.PI / 2.0); this.sin_direction = Math.sin(-direction - Math.PI / 2.0); this.position = new_position; - if (this.is_lost()) { - this.current_segment = this.path.nearest_segment(this.position, 0, path.len - 1); - } else { - this.current_segment = this.path.nearest_segment(this.position, Math.max(0, current_segment-1), Math.min(current_segment+2, path.len - 1)); + + // detect segment we are on now + let next_segment = this.path.nearest_segment(this.position, Math.max(0, this.current_segment-1), Math.min(this.current_segment+2, path.len - 1)); + + if (this.is_lost(next_segment)) { + // it did not work, try anywhere + next_segment = this.path.nearest_segment(this.position, 0, path.len - 1); + } + // now check if we strayed away from path or back to it + let lost = this.is_lost(next_segment); + if (this.on_path == lost) { + if (lost) { + Bangle.buzz(); // we lost path + setTimeout(()=>Bangle.buzz(), 300); + } else { + Bangle.buzz(); // we found path back + } + this.on_path = !lost; + } + + if (this.current_segment != next_segment) { + if (this.current_segment == next_segment - 1) { + this.highest_completed_segment = this.current_segment; + } + } + this.current_segment = next_segment; + + // check if we are nearing the next point on our path and alert the user + let next_point = this.current_segment + 1; + this.distance_to_next_point = Math.ceil(this.position.distance(this.path.point(next_point))); + if (this.reaching != next_point && this.distance_to_next_point <= 20) { + this.reaching = next_point; + Bangle.buzz(); + } + + // re-display unless locked + if (!Bangle.isLocked()) { + this.display(); } - this.display(); } remaining_distance() { return this.remaining_distances[this.current_segment+1] + this.position.distance(this.path.point(this.current_segment+1)); } - is_lost() { - let distance_to_nearest = this.position.fake_distance_to_segment(this.path.point(this.current_segment), this.path.point(this.current_segment+1)); - // TODO: if more than something then we are lost - return true; + is_lost(segment) { + let distance_to_nearest = this.position.fake_distance_to_segment(this.path.point(segment), this.path.point(segment+1)); + let meters = 6371e3 * distance_to_nearest; + return (meters > 20); } display() { g.clear(); @@ -61,7 +100,7 @@ class Status { let hours = now.getHours().toString(); g.setFont("6x8:2").drawString(hours + ":" + minutes, 0, g.getHeight() - 49); g.drawString("d. " + rounded_distance + "/" + total, 0, g.getHeight() - 32); - g.drawString("seg." + (this.current_segment + 1) + "/" + path.len, 0, g.getHeight() - 15); + g.drawString("seg." + (this.current_segment + 1) + "/" + path.len + " " + this.distance_to_next_point + "m", 0, g.getHeight() - 15); } display_map() { // don't display all segments, only those neighbouring current segment @@ -207,8 +246,8 @@ class Point { // We find projection of point p onto the line. // It falls where t = [(p-v) . (w-v)] / |w-v|^2 // We clamp t from [0,1] to handle points outside the segment vw. - let t = - Math.max(0, Math.min(1, (this.minus(v)).dot(w.minus(v)) / l2)); + let t = Math.max(0, Math.min(1, (this.minus(v)).dot(w.minus(v)) / l2)); + let projection = v.plus((w.minus(v)).times(t)); // Projection falls on the segment return this.fake_distance(projection); } @@ -254,5 +293,3 @@ if (simulated) { Bangle.setGPSPower(true, "gipy"); Bangle.on('GPS', set_coordinates); } - - diff --git a/apps/gipy/metadata.json b/apps/gipy/metadata.json index 9e05dec6e..f5baf2eeb 100644 --- a/apps/gipy/metadata.json +++ b/apps/gipy/metadata.json @@ -2,7 +2,7 @@ "id": "gipy", "name": "Gipy", "shortName": "Gipy", - "version": "0.04", + "version": "0.05", "description": "Follow gpx files", "allow_emulator":false, "icon": "gipy.png",