minor stuff

pull/2234/head
frederic wagner 2022-07-22 07:31:23 +02:00
parent d2b3840518
commit f4ca3afe34
2 changed files with 498 additions and 434 deletions

View File

@ -1,30 +1,22 @@
let simulated = true; let simulated = true;
let file_version = 3; let file_version = 3;
let code_key = 47490; let code_key = 47490;
let interests_colors = [ let interests_colors = [
0x780F, // Bakery, purple 0x780f, // Bakery, purple
0x001F, // DrinkingWater, blue 0x001f, // DrinkingWater, blue
0x07FF, // Toilets, cyan 0x07ff, // Toilets, cyan
0x7BEF, // BikeShop, dark grey 0xfd20, // Artwork, orange
0xAFE5, // ChargingStation, green yellow
0x7800, // Bank, maroon
0xF800, // Supermarket, red
0xF81F, // Table, pink
0xFD20, // Artwork, orange
0x07E0, // Pharmacy, green
]; ];
function binary_search(array, x) { function binary_search(array, x) {
let start = 0, end = array.length - 1; let start = 0,
end = array.length - 1;
while (start <= end){ while (start <= end) {
let mid = Math.floor((start + end) / 2); let mid = Math.floor((start + end) / 2);
if (array[mid] < x) if (array[mid] < x) start = mid + 1;
start = mid + 1; else end = mid - 1;
else
end = mid - 1;
} }
return start; return start;
} }
@ -54,8 +46,12 @@ class Status {
this.remaining_distances = r; // how much distance remains at start of each segment this.remaining_distances = r; // how much distance remains at start of each segment
} }
update_position(new_position, direction) { update_position(new_position, direction) {
if (
if (Bangle.isLocked() && this.position !== null && new_position.lon == this.position.lon && new_position.lat == this.position.lat) { Bangle.isLocked() &&
this.position !== null &&
new_position.lon == this.position.lon &&
new_position.lat == this.position.lat
) {
return; return;
} }
@ -64,18 +60,31 @@ 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, this.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, this.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);
if (this.on_path == lost) { // if status changes if (this.on_path == lost) {
// if status changes
if (lost) { if (lost) {
Bangle.buzz(); // we lost path Bangle.buzz(); // we lost path
setTimeout(()=>Bangle.buzz(), 500); setTimeout(() => Bangle.buzz(), 500);
} }
this.on_path = !lost; this.on_path = !lost;
} }
@ -84,7 +93,9 @@ class Status {
// check if we are nearing the next point on our path and alert the user // check if we are nearing the next point on our path and alert the user
let next_point = this.current_segment + 1; let next_point = this.current_segment + 1;
this.distance_to_next_point = Math.ceil(this.position.distance(this.path.point(next_point))); 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) { if (this.reaching != next_point && this.distance_to_next_point <= 20) {
this.reaching = next_point; this.reaching = next_point;
let reaching_waypoint = this.path.is_waypoint(next_point); let reaching_waypoint = this.path.is_waypoint(next_point);
@ -101,12 +112,18 @@ class Status {
} }
} }
remaining_distance() { remaining_distance() {
return this.remaining_distances[this.current_segment+1] + this.position.distance(this.path.point(this.current_segment+1)); return (
this.remaining_distances[this.current_segment + 1] +
this.position.distance(this.path.point(this.current_segment + 1))
);
} }
is_lost(segment) { is_lost(segment) {
let distance_to_nearest = this.position.fake_distance_to_segment(this.path.point(segment), this.path.point(segment+1)); 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; let meters = 6371e3 * distance_to_nearest;
return (meters > 20); return meters > 20;
} }
display() { display() {
g.clear(); g.clear();
@ -118,17 +135,32 @@ class Status {
display_interest_points() { display_interest_points() {
// this is the algorithm in case we have a lot of interest points // this is the algorithm in case we have a lot of interest points
// let's draw all points for 5 segments centered on current one // let's draw all points for 5 segments centered on current one
let starting_group = Math.floor(Math.max(this.current_segment-2, 0) / 3); let starting_group = Math.floor(Math.max(this.current_segment - 2, 0) / 3);
let ending_group = Math.floor(Math.min(this.current_segment+2, this.path.len-2) / 3); let ending_group = Math.floor(
let starting_bucket = binary_search(this.path.interests_starts, starting_group); Math.min(this.current_segment + 2, this.path.len - 2) / 3
let ending_bucket = binary_search(this.path.interests_starts, ending_group+0.5); );
let starting_bucket = binary_search(
this.path.interests_starts,
starting_group
);
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
let end_index = Math.min(this.path.interests_types.length - 1, ending_bucket*5); let end_index = Math.min(
for (let i = starting_bucket*5 ; i <= end_index ; i++) { 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);
let c = interest_point.coordinates(this.position, this.cos_direction, this.sin_direction); let c = interest_point.coordinates(
this.position,
this.cos_direction,
this.sin_direction
);
g.setColor(color).fillCircle(c[0], c[1], 5); g.setColor(color).fillCircle(c[0], c[1], 5);
} }
} }
@ -138,12 +170,24 @@ class Status {
let now = new Date(); let now = new Date();
let minutes = now.getMinutes().toString(); let minutes = now.getMinutes().toString();
if (minutes.length < 2) { if (minutes.length < 2) {
minutes = '0' + minutes; minutes = "0" + minutes;
} }
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) + "/" + this.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
@ -163,9 +207,9 @@ class Status {
let half_height = g.getHeight() / 2; let half_height = g.getHeight() / 2;
let previous_x = null; let previous_x = null;
let previous_y = null; let previous_y = null;
for (let i = start ; i < end ; i++) { for (let i = start; i < end; i++) {
let tx = (points[2*i] - cx) * 40000.0; let tx = (points[2 * i] - cx) * 40000.0;
let ty = (points[2*i+1] - cy) * 40000.0; let ty = (points[2 * i + 1] - cy) * 40000.0;
let rotated_x = tx * cos - ty * sin; let rotated_x = tx * cos - ty * sin;
let rotated_y = tx * sin + ty * cos; let rotated_y = tx * sin + ty * cos;
let x = half_width - Math.round(rotated_x); // x is inverted let x = half_width - Math.round(rotated_x); // x is inverted
@ -178,7 +222,7 @@ class Status {
} }
g.drawLine(previous_x, previous_y, x, y); g.drawLine(previous_x, previous_y, x, y);
if (this.path.is_waypoint(i-1)) { if (this.path.is_waypoint(i - 1)) {
g.setColor(g.theme.fg); g.setColor(g.theme.fg);
g.fillCircle(previous_x, previous_y, 6); g.fillCircle(previous_x, previous_y, 6);
g.setColor(g.theme.bg); g.setColor(g.theme.bg);
@ -188,14 +232,13 @@ class Status {
g.fillCircle(previous_x, previous_y, 4); g.fillCircle(previous_x, previous_y, 4);
g.setColor(g.theme.bg); g.setColor(g.theme.bg);
g.fillCircle(previous_x, previous_y, 3); g.fillCircle(previous_x, previous_y, 3);
} }
previous_x = x; previous_x = x;
previous_y = y; previous_y = y;
} }
if (this.path.is_waypoint(end-1)) { if (this.path.is_waypoint(end - 1)) {
g.setColor(g.theme.fg); g.setColor(g.theme.fg);
g.fillCircle(previous_x, previous_y, 6); g.fillCircle(previous_x, previous_y, 6);
g.setColor(g.theme.bg); g.setColor(g.theme.bg);
@ -223,13 +266,13 @@ class Path {
let key = header[0]; let key = header[0];
let version = header[1]; let version = header[1];
let points_number = header[2]; let points_number = header[2];
if ((key != code_key)||(version>file_version)) { if (key != code_key || version > file_version) {
E.showMessage("Invalid gpc file"); E.showMessage("Invalid gpc file");
load(); load();
} }
// path points // path points
this.points = Float64Array(buffer, offset, points_number*2); this.points = Float64Array(buffer, offset, points_number * 2);
offset += 8 * points_number * 2; offset += 8 * points_number * 2;
// path waypoints // path waypoints
@ -239,14 +282,22 @@ class Path {
// interest points // interest points
let interests_number = header[3]; let interests_number = header[3];
this.interests_coordinates = Float64Array(buffer, offset, interests_number * 2); this.interests_coordinates = Float64Array(
buffer,
offset,
interests_number * 2
);
offset += 8 * interests_number * 2; offset += 8 * interests_number * 2;
this.interests_types = Uint8Array(buffer, offset, interests_number); this.interests_types = Uint8Array(buffer, offset, interests_number);
offset += interests_number; offset += interests_number;
// interests on path // interests on path
let interests_on_path_number = header[4]; let interests_on_path_number = header[4];
this.interests_on_path = Uint16Array(buffer, offset, interests_on_path_number); this.interests_on_path = Uint16Array(
buffer,
offset,
interests_on_path_number
);
offset += 2 * interests_on_path_number; offset += 2 * interests_on_path_number;
let starts_length = Math.ceil(interests_on_path_number / 5.0); let starts_length = Math.ceil(interests_on_path_number / 5.0);
this.interests_starts = Uint16Array(buffer, offset, starts_length); this.interests_starts = Uint16Array(buffer, offset, starts_length);
@ -254,7 +305,7 @@ class Path {
} }
is_waypoint(point_index) { is_waypoint(point_index) {
return (this.waypoints[Math.floor(point_index / 8)] & (point_index % 8)); return this.waypoints[Math.floor(point_index / 8)] & point_index % 8;
} }
// execute op on all segments. // execute op on all segments.
@ -296,17 +347,21 @@ class Path {
// we are going to compute two min distances, one for each direction. // we are going to compute two min distances, one for each direction.
let indices = [0, 0]; let indices = [0, 0];
let mins = [Number.MAX_VALUE, Number.MAX_VALUE]; let mins = [Number.MAX_VALUE, Number.MAX_VALUE];
this.on_segments(function(p1, p2, i) { this.on_segments(
function (p1, p2, i) {
// we use the dot product to figure out if oriented correctly // we use the dot product to figure out if oriented correctly
let distance = point.fake_distance_to_segment(p1, p2); let distance = point.fake_distance_to_segment(p1, p2);
let diff = p2.minus(p1); let diff = p2.minus(p1);
let dot = cos_direction * diff.lon + sin_direction * diff.lat; let dot = cos_direction * diff.lon + sin_direction * diff.lat;
let orientation = + (dot < 0); // index 0 is good orientation let orientation = +(dot < 0); // index 0 is good orientation
if (distance <= mins[orientation]) { if (distance <= mins[orientation]) {
mins[orientation] = distance; mins[orientation] = distance;
indices[orientation] = i - 1; indices[orientation] = i - 1;
} }
}, start, end); },
start,
end
);
// by default correct orientation (0) wins // by default correct orientation (0) wins
// but if other one is really closer, return other one // but if other one is really closer, return other one
if (mins[1] < mins[0] / 10.0) { if (mins[1] < mins[0] / 10.0) {
@ -327,11 +382,13 @@ class Point {
} }
coordinates(current_position, cos_direction, sin_direction) { coordinates(current_position, cos_direction, sin_direction) {
let translated = this.minus(current_position).times(40000.0); let translated = this.minus(current_position).times(40000.0);
let rotated_x = translated.lon * cos_direction - translated.lat * sin_direction; let rotated_x =
let rotated_y = translated.lon * sin_direction + translated.lat * cos_direction; translated.lon * cos_direction - translated.lat * sin_direction;
let rotated_y =
translated.lon * sin_direction + translated.lat * cos_direction;
return [ return [
g.getWidth() / 2 - Math.round(rotated_x), // x is inverted g.getWidth() / 2 - Math.round(rotated_x), // x is inverted
g.getHeight() / 2 + Math.round(rotated_y) g.getHeight() / 2 + Math.round(rotated_y),
]; ];
} }
minus(other_point) { minus(other_point) {
@ -344,7 +401,7 @@ class Point {
} }
length_squared(other_point) { length_squared(other_point) {
let d = this.minus(other_point); let d = this.minus(other_point);
return (d.lon * d.lon + d.lat * d.lat); return d.lon * d.lon + d.lat * d.lat;
} }
times(scalar) { times(scalar) {
return new Point(this.lon * scalar, this.lat * scalar); return new Point(this.lon * scalar, this.lat * scalar);
@ -355,14 +412,17 @@ class Point {
distance(other_point) { distance(other_point) {
//see https://www.movable-type.co.uk/scripts/latlong.html //see https://www.movable-type.co.uk/scripts/latlong.html
const R = 6371e3; // metres const R = 6371e3; // metres
const phi1 = this.lat * Math.PI / 180; const phi1 = (this.lat * Math.PI) / 180;
const phi2 = other_point.lat * Math.PI / 180; const phi2 = (other_point.lat * Math.PI) / 180;
const deltaphi = (other_point.lat - this.lat) * Math.PI / 180; const deltaphi = ((other_point.lat - this.lat) * Math.PI) / 180;
const deltalambda = (other_point.lon - this.lon) * Math.PI / 180; const deltalambda = ((other_point.lon - this.lon) * Math.PI) / 180;
const a = Math.sin(deltaphi / 2) * Math.sin(deltaphi / 2) + const a =
Math.cos(phi1) * Math.cos(phi2) * Math.sin(deltaphi / 2) * Math.sin(deltaphi / 2) +
Math.sin(deltalambda / 2) * Math.sin(deltalambda / 2); Math.cos(phi1) *
Math.cos(phi2) *
Math.sin(deltalambda / 2) *
Math.sin(deltalambda / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c; // in metres return R * c; // in metres
@ -381,20 +441,18 @@ class Point {
// We find projection of point p onto the line. // We find projection of point p onto the line.
// It falls where t = [(p-v) . (w-v)] / |w-v|^2 // It falls where t = [(p-v) . (w-v)] / |w-v|^2
// We clamp t from [0,1] to handle points outside the segment vw. // 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 let projection = v.plus(w.minus(v).times(t)); // Projection falls on the segment
return this.fake_distance(projection); return this.fake_distance(projection);
} }
} }
Bangle.loadWidgets(); Bangle.loadWidgets();
let fake_gps_point = 0.0; let fake_gps_point = 0.0;
function simulate_gps(status) { function simulate_gps(status) {
if (fake_gps_point > status.path.len -1) { if (fake_gps_point > status.path.len - 1) {
return; return;
} }
let point_index = Math.floor(fake_gps_point); let point_index = Math.floor(fake_gps_point);
@ -405,7 +463,7 @@ function simulate_gps(status) {
let p2 = status.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));
let old_pos = status.position; let old_pos = status.position;
fake_gps_point += 0.2; // advance simulation fake_gps_point += 0.2; // advance simulation
@ -415,18 +473,18 @@ function simulate_gps(status) {
function drawMenu() { function drawMenu() {
const menu = { const menu = {
'': { 'title': 'choose trace' } "": { title: "choose trace" },
}; };
var files = require("Storage").list(".gpc"); var files = require("Storage").list(".gpc");
for (var i=0; i<files.length; ++i) { for (var i = 0; i < files.length; ++i) {
menu[files[i]] = start.bind(null, files[i]); menu[files[i]] = start.bind(null, files[i]);
} }
menu['Exit'] = function() { load(); }; menu["Exit"] = function () {
load();
};
E.showMenu(menu); E.showMenu(menu);
} }
function start(fn) { function start(fn) {
console.log("loading", fn); console.log("loading", fn);
@ -447,18 +505,18 @@ function start(fn) {
let frame = 0; let frame = 0;
let old_points = []; // remember the at most 3 previous points let old_points = []; // remember the at most 3 previous points
let set_coordinates = function(data) { let set_coordinates = function (data) {
frame += 1; frame += 1;
let valid_coordinates = !isNaN(data.lat) && !isNaN(data.lon); let valid_coordinates = !isNaN(data.lat) && !isNaN(data.lon);
if (valid_coordinates) { if (valid_coordinates) {
// we try to figure out direction by looking at previous points // we try to figure out direction by looking at previous points
// instead of the gps course which is not very nice. // instead of the gps course which is not very nice.
let direction = data.course * Math.PI / 180.0; let direction = (data.course * Math.PI) / 180.0;
let position = new Point(data.lon, data.lat); let position = new Point(data.lon, data.lat);
if (old_points.length == 0) { if (old_points.length == 0) {
old_points.push(position); old_points.push(position);
} else { } else {
let last_point = old_points[old_points.length-1]; let last_point = old_points[old_points.length - 1];
if (last_point.x != position.x || last_point.y != position.y) { if (last_point.x != position.x || last_point.y != position.y) {
if (old_points.length == 4) { if (old_points.length == 4) {
old_points.shift(); old_points.shift();
@ -469,9 +527,9 @@ function start(fn) {
if (old_points.length == 4) { if (old_points.length == 4) {
// let's just take average angle of 3 previous segments // let's just take average angle of 3 previous segments
let angles_sum = 0; let angles_sum = 0;
for(let i = 0 ; i < 3 ; i++) { for (let i = 0; i < 3; i++) {
let p1 = old_points[i]; let p1 = old_points[i];
let p2 = old_points[i+1]; let p2 = old_points[i + 1];
let diff = p2.minus(p1); let diff = p2.minus(p1);
let angle = Math.atan2(diff.lat, diff.lon); let angle = Math.atan2(diff.lat, diff.lon);
angles_sum += angle; angles_sum += angle;
@ -482,16 +540,18 @@ function start(fn) {
} }
} }
let gps_status_color; let gps_status_color;
if ((frame % 2 == 0)||valid_coordinates) { if (frame % 2 == 0 || valid_coordinates) {
gps_status_color = g.theme.bg; gps_status_color = g.theme.bg;
} else { } else {
gps_status_color = g.theme.fg; gps_status_color = g.theme.fg;
} }
g.setColor(gps_status_color).setFont("6x8:2").drawString("gps", g.getWidth() - 40, 30); g.setColor(gps_status_color)
} .setFont("6x8:2")
.drawString("gps", g.getWidth() - 40, 30);
};
Bangle.setGPSPower(true, "gipy"); Bangle.setGPSPower(true, "gipy");
Bangle.on('GPS', set_coordinates); Bangle.on("GPS", set_coordinates);
} }
} }
@ -505,4 +565,3 @@ if (files.length <= 1) {
} else { } else {
drawMenu(); drawMenu();
} }

View File

@ -650,6 +650,7 @@ fn position_interests_along_path(
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let input_file = std::env::args().nth(1).unwrap_or("m.gpx".to_string()); let input_file = std::env::args().nth(1).unwrap_or("m.gpx".to_string());
let osm_file = std::env::args().nth(2);
eprintln!("input is {}", input_file); eprintln!("input is {}", input_file);
let mut segmented_points = points(&input_file); let mut segmented_points = points(&input_file);
let p = segmented_points let p = segmented_points
@ -675,7 +676,11 @@ async fn main() {
} }
rp.extend(segmented_points.last().and_then(|l| l.last()).copied()); rp.extend(segmented_points.last().and_then(|l| l.last()).copied());
let mut interests = parse_osm_data("ardeche.osm.pbf"); let mut interests = if let Some(osm) = osm_file {
parse_osm_data(osm)
} else {
Vec::new()
};
// let mut interests = parse_osm_data("isere.osm.pbf"); // let mut interests = parse_osm_data("isere.osm.pbf");
let buckets = position_interests_along_path(&mut interests, &rp, 0.001, 5, 3); let buckets = position_interests_along_path(&mut interests, &rp, 0.001, 5, 3);
// let i = get_openstreetmap_data(&rp).await; // let i = get_openstreetmap_data(&rp).await;