master
frederic wagner 2022-07-21 15:09:16 +02:00
parent 1bce4cf8e0
commit b1e3db6d81
4 changed files with 100 additions and 78 deletions

View File

@ -23,3 +23,4 @@
0.09: 0.09:
* We now display interest points. * We now display interest points.
* Menu to choose which file to load.

View File

@ -1,4 +1,5 @@
- it is becoming messy
- split on points with comments - split on points with comments
- turn off gps when moving to next waypoint - turn off gps when moving to next waypoint
@ -8,7 +9,6 @@
- buzzing does not work nicely - buzzing does not work nicely
-> is_lost seems fishy -> is_lost seems fishy
- store several tracks
- display average speed - display average speed
- dynamic map rescale - dynamic map rescale
- display scale (100m) - display scale (100m)

View File

@ -1,5 +1,5 @@
let simulated = true; let simulated = false;
let file_version = 2; let file_version = 2;
let code_key = 47490; let code_key = 47490;
@ -64,11 +64,11 @@ class Status {
this.position = new_position; this.position = new_position;
// detect segment we are on now // 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), this.cos_direction, this.sin_direction); let next_segment = this.path.nearest_segment(this.position, Math.max(0, this.current_segment-1), Math.min(this.current_segment+2, this.path.len - 1), this.cos_direction, this.sin_direction);
if (this.is_lost(next_segment)) { if (this.is_lost(next_segment)) {
// it did not work, try anywhere // it did not work, try anywhere
next_segment = this.path.nearest_segment(this.position, 0, path.len - 1, this.cos_direction, this.sin_direction); next_segment = this.path.nearest_segment(this.position, 0, this.path.len - 1, this.cos_direction, this.sin_direction);
} }
// now check if we strayed away from path or back to it // now check if we strayed away from path or back to it
let lost = this.is_lost(next_segment); let lost = this.is_lost(next_segment);
@ -123,7 +123,8 @@ class Status {
let starting_bucket = binary_search(this.path.interests_starts, starting_group); let starting_bucket = binary_search(this.path.interests_starts, starting_group);
let ending_bucket = binary_search(this.path.interests_starts, ending_group+0.5); let ending_bucket = binary_search(this.path.interests_starts, ending_group+0.5);
// we have 5 points per bucket // we have 5 points per bucket
for (let i = starting_bucket*5 ; i <= ending_bucket*5 ; i++) { let end_index = Math.min(this.path.interests_types.length - 1, ending_bucket*5);
for (let i = starting_bucket*5 ; i <= end_index ; i++) {
let index = this.path.interests_on_path[i]; let index = this.path.interests_on_path[i];
let interest_point = this.path.interest_point(index); let interest_point = this.path.interest_point(index);
let color = this.path.interest_color(i); let color = this.path.interest_color(i);
@ -142,7 +143,7 @@ class Status {
let hours = now.getHours().toString(); let hours = now.getHours().toString();
g.setFont("6x8:2").setColor(g.theme.fg).drawString(hours + ":" + minutes, 0, g.getHeight() - 49); g.setFont("6x8:2").setColor(g.theme.fg).drawString(hours + ":" + minutes, 0, g.getHeight() - 49);
g.drawString("d. " + rounded_distance + "/" + total, 0, g.getHeight() - 32); g.drawString("d. " + rounded_distance + "/" + total, 0, g.getHeight() - 32);
g.drawString("seg." + (this.current_segment + 1) + "/" + path.len + " " + this.distance_to_next_point + "m", 0, g.getHeight() - 15); g.drawString("seg." + (this.current_segment + 1) + "/" + this.path.len + " " + this.distance_to_next_point + "m", 0, g.getHeight() - 15);
} }
display_map() { display_map() {
// don't display all segments, only those neighbouring current segment // don't display all segments, only those neighbouring current segment
@ -376,53 +377,7 @@ class Point {
Bangle.loadWidgets(); Bangle.loadWidgets();
let path = new Path("test.gpc");
let status = new Status(path);
let frame = 0;
let old_points = []; // remember the at most 3 previous points
function set_coordinates(data) {
frame += 1;
let valid_coordinates = !isNaN(data.lat) && !isNaN(data.lon);
if (valid_coordinates) {
// we try to figure out direction by looking at previous points
// instead of the gps course which is not very nice.
let direction = data.course * Math.PI / 180.0;
let position = new Point(data.lon, data.lat);
if (old_points.length == 0) {
old_points.push(position);
} else {
let last_point = old_points[old_points.length-1];
if (last_point.x != position.x || last_point.y != position.y) {
if (old_points.length == 4) {
old_points.shift();
}
old_points.push(position);
}
}
if (old_points.length == 4) {
// let's just take average angle of 3 previous segments
let angles_sum = 0;
for(let i = 0 ; i < 3 ; i++) {
let p1 = old_points[i];
let p2 = old_points[i+1];
let diff = p2.minus(p1);
let angle = Math.atan2(diff.lat, diff.lon);
angles_sum += angle;
}
status.update_position(position, angles_sum / 3.0);
} else {
status.update_position(position, direction);
}
}
let gps_status_color;
if ((frame % 2 == 0)||valid_coordinates) {
gps_status_color = g.theme.bg;
} else {
gps_status_color = g.theme.fg;
}
g.setColor(gps_status_color).setFont("6x8:2").drawString("gps", g.getWidth() - 40, 30);
}
let fake_gps_point = 0.0; let fake_gps_point = 0.0;
function simulate_gps(status) { function simulate_gps(status) {
@ -430,11 +385,11 @@ function simulate_gps(status) {
return; return;
} }
let point_index = Math.floor(fake_gps_point); let point_index = Math.floor(fake_gps_point);
if (point_index >= path.len) { if (point_index >= status.path.len) {
return; return;
} }
let p1 = path.point(point_index); let p1 = status.path.point(point_index);
let p2 = path.point(point_index + 1); let p2 = status.path.point(point_index + 1);
let alpha = fake_gps_point - point_index; let alpha = fake_gps_point - point_index;
let pos = p1.times(1-alpha).plus(p2.times(alpha)); let pos = p1.times(1-alpha).plus(p2.times(alpha));
@ -445,22 +400,96 @@ function simulate_gps(status) {
status.update_position(pos, direction); status.update_position(pos, direction);
} }
function drawMenu() {
if (simulated) { const menu = {
status.position = new Point(status.path.point(0)); '': { 'title': 'choose trace' }
setInterval(simulate_gps, 500, status); };
} else { var files = require("Storage").list(".gpc");
// let's display start while waiting for gps signal for (var i=0; i<files.length; ++i) {
let p1 = status.path.point(0); menu[files[i]] = start.bind(null, files[i]);
let p2 = status.path.point(1); }
let diff = p2.minus(p1); menu['Exit'] = function() { load(); };
let direction = Math.atan2(diff.lat, diff.lon); E.showMenu(menu);
Bangle.setLocked(false);
status.update_position(p1, direction);
Bangle.setGPSPower(true, "gipy");
Bangle.on('GPS', set_coordinates);
} }
function start(fn) {
console.log("loading", fn);
let path = new Path(fn);
let status = new Status(path);
if (simulated) {
status.position = new Point(status.path.point(0));
setInterval(simulate_gps, 500, status);
} else {
// let's display start while waiting for gps signal
let p1 = status.path.point(0);
let p2 = status.path.point(1);
let diff = p2.minus(p1);
let direction = Math.atan2(diff.lat, diff.lon);
Bangle.setLocked(false);
status.update_position(p1, direction);
let frame = 0;
let old_points = []; // remember the at most 3 previous points
let set_coordinates = function(data) {
frame += 1;
let valid_coordinates = !isNaN(data.lat) && !isNaN(data.lon);
if (valid_coordinates) {
// we try to figure out direction by looking at previous points
// instead of the gps course which is not very nice.
let direction = data.course * Math.PI / 180.0;
let position = new Point(data.lon, data.lat);
if (old_points.length == 0) {
old_points.push(position);
} else {
let last_point = old_points[old_points.length-1];
if (last_point.x != position.x || last_point.y != position.y) {
if (old_points.length == 4) {
old_points.shift();
}
old_points.push(position);
}
}
if (old_points.length == 4) {
// let's just take average angle of 3 previous segments
let angles_sum = 0;
for(let i = 0 ; i < 3 ; i++) {
let p1 = old_points[i];
let p2 = old_points[i+1];
let diff = p2.minus(p1);
let angle = Math.atan2(diff.lat, diff.lon);
angles_sum += angle;
}
status.update_position(position, angles_sum / 3.0);
} else {
status.update_position(position, direction);
}
}
let gps_status_color;
if ((frame % 2 == 0)||valid_coordinates) {
gps_status_color = g.theme.bg;
} else {
gps_status_color = g.theme.fg;
}
g.setColor(gps_status_color).setFont("6x8:2").drawString("gps", g.getWidth() - 40, 30);
}
Bangle.setGPSPower(true, "gipy");
Bangle.on('GPS', set_coordinates);
}
}
let files = require("Storage").list(".gpc");
if (files.length <= 1) {
if (files.length == 0) {
load();
} else {
start(files[0]);
}
} else {
drawMenu();
}

View File

@ -379,7 +379,6 @@ fn save_gpc<P: AsRef<Path>>(path: P, points: &[Point], buckets: &[Bucket]) -> st
.try_for_each(|i| writer.write_all(&i.to_le_bytes()))?; .try_for_each(|i| writer.write_all(&i.to_le_bytes()))?;
let starts: Vec<_> = buckets.iter().map(|b| b.start).collect(); let starts: Vec<_> = buckets.iter().map(|b| b.start).collect();
eprintln!("starts {:?} of length {}", starts, starts.len());
Ok(()) Ok(())
} }
@ -592,13 +591,6 @@ fn position_interests_along_path(
.chunks(groups_size) .chunks(groups_size)
.map(|c| c.iter().flatten().unique().copied().collect::<Vec<_>>()) .map(|c| c.iter().flatten().unique().copied().collect::<Vec<_>>())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
eprintln!(
"group sizes are {:?}",
grouped_positions
.iter()
.map(|g| g.len())
.collect::<Vec<_>>()
);
// now, group the points in buckets // now, group the points in buckets
let chunks = grouped_positions let chunks = grouped_positions
.iter() .iter()