From f1a629b4c48f31fe0abd2bab58f8da0544e0d063 Mon Sep 17 00:00:00 2001 From: Dion Dokter Date: Wed, 24 Apr 2024 13:17:05 +0200 Subject: [PATCH 1/3] Fix port being used for right protocol version --- fake-anchors/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fake-anchors/src/main.rs b/fake-anchors/src/main.rs index 0f14b02..faddaa8 100644 --- a/fake-anchors/src/main.rs +++ b/fake-anchors/src/main.rs @@ -16,7 +16,7 @@ const TIME_PER_SAMPLE: f64 = 1.0 / SAMPLE_SPEED; fn main() { let config = spaeter_core::config::Config::load(None).unwrap(); - let mut mqttoptions = MqttOptions::new("fake-anchors", "127.0.0.1", 1883); + let mut mqttoptions = MqttOptions::new("fake-anchors", "127.0.0.1", 1884); mqttoptions.set_keep_alive(Duration::from_secs(5)); let (mut client, mut connection) = Client::new(mqttoptions, 10); From 4bd83afe8e1614b69721b7eabed41c8eb109e612 Mon Sep 17 00:00:00 2001 From: Dion Dokter Date: Wed, 24 Apr 2024 13:22:37 +0200 Subject: [PATCH 2/3] Fix frontend port as well --- frontend/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/main.rs b/frontend/src/main.rs index c93df70..3c33b08 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -22,7 +22,7 @@ fn main() { // .add_plugins(FrameTimeDiagnosticsPlugin) .add_systems(Startup, setup) .insert_resource(MqttConnection::new( - "mqtt://127.0.0.1:1883?client_id=spaeter-frontend".into(), + "mqtt://127.0.0.1:1884?client_id=spaeter-frontend".into(), )) .register_type::() .insert_resource(LocationStats { From 11257264117cf8140aed2ba22659cb02a520f6f0 Mon Sep 17 00:00:00 2001 From: Dion Dokter Date: Wed, 24 Apr 2024 14:55:11 +0200 Subject: [PATCH 3/3] Improve visualizations --- fake-anchors/src/main.rs | 122 +++++++++++++++++++++------------------ frontend/src/main.rs | 10 ++-- 2 files changed, 72 insertions(+), 60 deletions(-) diff --git a/fake-anchors/src/main.rs b/fake-anchors/src/main.rs index faddaa8..73f28c1 100644 --- a/fake-anchors/src/main.rs +++ b/fake-anchors/src/main.rs @@ -21,83 +21,93 @@ fn main() { let (mut client, mut connection) = Client::new(mqttoptions, 10); + let mut fake_anchors = config + .anchors + .iter() + .map(|anchor| FakeAnchor { + id: anchor.id, + location: anchor.location, + signal_detector: SignalDetector::new(SAMPLE_SPEED as f32), + }) + .collect::>(); + + let max_position = fake_anchors + .iter() + .map(|a| a.location) + .reduce(Vec3::max) + .unwrap(); + let min_position = fake_anchors + .iter() + .map(|a| a.location) + .reduce(Vec3::min) + .unwrap(); + thread::spawn(move || { - let mut fake_anchors = config - .anchors - .iter() - .map(|anchor| FakeAnchor { - id: anchor.id, - location: anchor.location, - signal_detector: SignalDetector::new(SAMPLE_SPEED as f32), - }) - .collect::>(); - - let max_position = fake_anchors - .iter() - .map(|a| a.location) - .reduce(Vec3::max) - .unwrap(); - let min_position = fake_anchors - .iter() - .map(|a| a.location) - .reduce(Vec3::min) - .unwrap(); - - for _ in 0..10 { - let test_position = vec3( + loop { + let start_position = vec3( + rand::thread_rng().gen_range(min_position.x..max_position.x), + rand::thread_rng().gen_range(min_position.y..max_position.y), + rand::thread_rng().gen_range(min_position.z..max_position.z), + ); + + let end_position = vec3( rand::thread_rng().gen_range(min_position.x..max_position.x), rand::thread_rng().gen_range(min_position.y..max_position.y), rand::thread_rng().gen_range(min_position.z..max_position.z), ); - let mut test_samples = [0.0; 4410usize.next_multiple_of(64)]; + let freq = rand::thread_rng().gen_range(500.0..15000.0); + + const MAX: u32 = 10; + for i in 0..MAX { + let test_position = start_position + + (end_position - start_position) * (i as f32 / (MAX - 1) as f32); + + let mut test_samples = [0.0; 4410usize.next_multiple_of(64)]; - let freq1 = rand::thread_rng().gen_range(500.0..15000.0); - let freq2 = rand::thread_rng().gen_range(500.0..15000.0); - let freq3 = rand::thread_rng().gen_range(500.0..15000.0); + add_signal(&mut test_samples[700..1500], 700, freq, 1.0, 0.0); - // add_signal(&mut test_samples[100..200], 100, freq1, 1.0, 0.0); - add_signal(&mut test_samples[700..1500], 700, freq2, 1.0, 0.0); - // add_signal(&mut test_samples[3510..4000], 3510, freq3, 1.0, 0.0); + println!("Signal\n\tfreq: {freq:.1},\n\tlocation: {test_position}"); - println!("Signal\n\tfreq1: {freq1:.1},\n\tfreq2: {freq2:.1},\n\tfreq3: {freq3:.1},\n\tlocation: {test_position}"); + let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); - let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); + for anchor in fake_anchors.iter_mut() { + let id = anchor.id; - for anchor in fake_anchors.iter_mut() { - let id = anchor.id; + let mut test_samples = test_samples; + add_random_noise(&mut test_samples, 0.1); - let mut test_samples = test_samples; - add_random_noise(&mut test_samples, 0.1); + anchor.feed( + &test_samples, + test_position, + Timestamp::new(current_time.as_nanos() as u64, 0), + |payload| { + // println!("{payload:?}"); - anchor.feed( - &test_samples, - test_position, - Timestamp::new(current_time.as_nanos() as u64, 0), - |payload| { - // println!("{payload:?}"); + let mut buffer = [0; 1024]; - let mut buffer = [0; 1024]; + let len = payload.serialize(&mut buffer).unwrap(); - let len = payload.serialize(&mut buffer).unwrap(); + client + .publish( + signal_peak_topic(Some(id)).as_str(), + QoS::ExactlyOnce, + false, + &buffer[..len], + ) + .unwrap(); + }, + ); + } - client - .publish( - signal_peak_topic(Some(id)).as_str(), - QoS::ExactlyOnce, - false, - &buffer[..len], - ) - .unwrap(); - }, - ); + std::thread::sleep(Duration::from_secs_f32(0.2)); } - std::thread::sleep(Duration::from_secs_f32(0.5)); + std::thread::sleep(Duration::from_secs_f32(2.0)); } }); - while connection.recv_timeout(Duration::from_millis(600)).is_ok() {} + while connection.recv_timeout(Duration::from_millis(6000)).is_ok() {} } struct FakeAnchor { diff --git a/frontend/src/main.rs b/frontend/src/main.rs index 3c33b08..0624d88 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -16,7 +16,7 @@ mod mqtt; fn main() { App::new() .add_plugins(DefaultPlugins) - .add_plugins(WorldInspectorPlugin::new()) // Debug window + // .add_plugins(WorldInspectorPlugin::new()) // Debug window .add_plugins(NoCameraPlayerPlugin) // Fly cam .add_plugins(LogDiagnosticsPlugin::default()) // .add_plugins(FrameTimeDiagnosticsPlugin) @@ -148,12 +148,14 @@ impl LocationStats { fn high_confidence_value(&self) -> f32 { let average = self.confidences.iter().sum::() / self.confidences.len() as f32; - average + let standard_deviation = self.confidences.iter().map(|c| (c - average).abs()).sum::() / self.confidences.len() as f32; + + average + standard_deviation * 2.0 } } -const MAX_AGE_SECS: f32 = 10.0; -const MAX_AGE_SIZE: f32 = 2.0; +const MAX_AGE_SECS: f32 = 30.0; +const MAX_AGE_SIZE: f32 = 1.0; fn location_sphere_runner( mut commands: Commands,