Skip to content
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

Ready up for demo for server #14

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
124 changes: 67 additions & 57 deletions fake-anchors/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,88 +16,98 @@ 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);

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::<Vec<_>>();

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::<Vec<_>>();

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 {
Expand Down
12 changes: 7 additions & 5 deletions frontend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ 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)
.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::<LocationStats>()
.insert_resource(LocationStats {
Expand Down Expand Up @@ -148,12 +148,14 @@ impl LocationStats {

fn high_confidence_value(&self) -> f32 {
let average = self.confidences.iter().sum::<f32>() / self.confidences.len() as f32;
average
let standard_deviation = self.confidences.iter().map(|c| (c - average).abs()).sum::<f32>() / 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,
Expand Down