Create workspace from previous implementation files
This commit is contained in:
23
simulation-tools/Cargo.toml
Normal file
23
simulation-tools/Cargo.toml
Normal file
@@ -0,0 +1,23 @@
|
||||
[package]
|
||||
name = "simulation-tools"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[[bin]]
|
||||
name = "nav_dev"
|
||||
path = "src/nav_dev/main.rs"
|
||||
|
||||
|
||||
[dependencies]
|
||||
paho-mqtt = { workspace = true }
|
||||
influxdb = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
dotenv = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
kairo-common = {path = "../kairo-common" }
|
||||
rand = "0.8.5"
|
||||
rand_distr = "0.4.3"
|
||||
59
simulation-tools/src/nav_dev/error_report.rs
Normal file
59
simulation-tools/src/nav_dev/error_report.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use influxdb::InfluxDbWriteable;
|
||||
use kairo_common::helper::for_async::get_influx_cli;
|
||||
use serde::Serialize;
|
||||
use tokio::time;
|
||||
|
||||
use kairo_common::{influxdb_models::KnownPosition, Point};
|
||||
|
||||
use crate::Config;
|
||||
|
||||
#[derive(Debug, Serialize, InfluxDbWriteable)]
|
||||
pub struct Error {
|
||||
error: f64,
|
||||
speed: f64,
|
||||
time: DateTime<Utc>,
|
||||
}
|
||||
|
||||
pub async fn thread(config: Config) {
|
||||
let period = time::Duration::from_millis(500);
|
||||
|
||||
let mut position = Point::new(config.radius, 0.0);
|
||||
let mut speed = position;
|
||||
position.rotate_by(f64::to_radians(config.angle_step));
|
||||
speed -= position;
|
||||
|
||||
let speed = speed.module();
|
||||
|
||||
loop {
|
||||
let start = time::Instant::now();
|
||||
|
||||
let real = KnownPosition::get_last_for("real", 1).await;
|
||||
let calc = KnownPosition::get_last_for(config.id.as_str(), 1).await;
|
||||
if real.is_ok() && calc.is_ok() {
|
||||
let real = real.unwrap();
|
||||
let calc = calc.unwrap();
|
||||
|
||||
if real.is_some() && calc.is_some() {
|
||||
let real = real.unwrap();
|
||||
let calc = calc.unwrap();
|
||||
#[allow(non_snake_case)]
|
||||
let Δx = real.x - calc.x;
|
||||
#[allow(non_snake_case)]
|
||||
let Δy = real.y - calc.y;
|
||||
let error = Error {
|
||||
speed,
|
||||
error: f64::sqrt(Δx.powi(2) + Δy.powi(2)),
|
||||
time: chrono::Utc::now(),
|
||||
};
|
||||
|
||||
let table_name = format!("error_{}", config.id.as_str());
|
||||
get_influx_cli()
|
||||
.query(error.into_query(table_name.as_str()))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
time::sleep(period - start.elapsed()).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
106
simulation-tools/src/nav_dev/main.rs
Normal file
106
simulation-tools/src/nav_dev/main.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
use rand_distr::{Distribution, Normal};
|
||||
use std::{thread, time};
|
||||
|
||||
mod error_report;
|
||||
|
||||
use kairo_common::helper::for_sync::{get_mqtt_cli, mqtt_pub};
|
||||
use kairo_common::{
|
||||
influxdb_models::{BeaconMeasure, KnownPosition},
|
||||
Antenna, DeviceReport, Point,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Config {
|
||||
period_ms: u64,
|
||||
radius: f64,
|
||||
noise_level: f64,
|
||||
angle_step: f64,
|
||||
id: String,
|
||||
real: bool,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let config = parse_cli();
|
||||
let period = time::Duration::from_millis(config.period_ms);
|
||||
let noise_gen = Normal::new(0.0, config.noise_level).unwrap();
|
||||
|
||||
if config.real {
|
||||
let config = config.clone();
|
||||
tokio::spawn(async move {
|
||||
error_report::thread(config).await;
|
||||
});
|
||||
}
|
||||
|
||||
let client = get_mqtt_cli();
|
||||
|
||||
let mut position = Point::new(config.radius, 0.0);
|
||||
|
||||
let antenna = vec![
|
||||
Antenna::new("e6:ad:0b:2e:d7:11", 30.0, Point::new(15.0, 15.0)),
|
||||
Antenna::new("c2:b5:f5:cc:e6:88", 30.0, Point::new(15.0, -15.0)),
|
||||
Antenna::new("e6:2e:e6:88:f5:cc", 30.0, Point::new(-15.0, 15.0)),
|
||||
Antenna::new("c2:ad:0b:b5:11:d7", 30.0, Point::new(-15.0, -15.0)),
|
||||
];
|
||||
|
||||
let topic = format!("device/{}/report", config.id);
|
||||
loop {
|
||||
let start = time::Instant::now();
|
||||
|
||||
let mut report = DeviceReport { data: vec![] };
|
||||
|
||||
for ant in (antenna).iter() {
|
||||
let d = ant.coord.distance_to(&position);
|
||||
let rssi = ant.get_rssi(d);
|
||||
|
||||
let noise: f64 = noise_gen.sample(&mut rand::thread_rng());
|
||||
|
||||
report.data.push(BeaconMeasure::new(&ant.id, rssi + noise));
|
||||
}
|
||||
let payload = serde_json::to_string(&report).unwrap_or_else(|_| "".to_string());
|
||||
mqtt_pub(&client, topic.as_str(), payload.as_str()).expect("Pub error");
|
||||
|
||||
if config.real {
|
||||
let _r = KnownPosition::new(position).write_for("real").await;
|
||||
}
|
||||
|
||||
position.rotate_by(f64::to_radians(config.angle_step));
|
||||
thread::sleep(period - start.elapsed());
|
||||
}
|
||||
}
|
||||
fn parse_cli() -> Config {
|
||||
use std::env;
|
||||
let mut config = Config {
|
||||
period_ms: 1000,
|
||||
radius: 12.0,
|
||||
noise_level: 0.0,
|
||||
angle_step: 3.6,
|
||||
id: "60:f2:62:01:a9:28".to_string(),
|
||||
real: true,
|
||||
};
|
||||
|
||||
let args = env::args().collect::<Vec<String>>();
|
||||
|
||||
for (i, arg) in args.iter().enumerate() {
|
||||
match arg.as_str() {
|
||||
"--noise" | "--noise-level" | "-n" => {
|
||||
config.noise_level = args[i + 1].parse::<f64>().unwrap();
|
||||
}
|
||||
"--rad" | "--radious" | "-r" => {
|
||||
config.radius = args[i + 1].parse::<f64>().unwrap();
|
||||
}
|
||||
"--period" | "-p" => {
|
||||
config.period_ms = args[i + 1].parse::<u64>().unwrap();
|
||||
}
|
||||
"--angle" | "--step" => {
|
||||
config.angle_step = args[i + 1].parse::<f64>().unwrap();
|
||||
}
|
||||
"--id" => {
|
||||
config.id = args[i + 1].clone();
|
||||
config.real = false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
config
|
||||
}
|
||||
Reference in New Issue
Block a user