Skip to content

Commit

Permalink
WIP: Prepare MQTT and serialization structure
Browse files Browse the repository at this point in the history
  • Loading branch information
matteocarnelos committed May 13, 2024
1 parent a113154 commit dc31c82
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 183 deletions.
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ edition = "2021"

[dependencies]
rumqttc = "0.24"
eskf = "0.2.0"
nalgebra = "=0.25.4"
quaternion-core = "0.5.0"
tokio = { version = "1.17.0", features = ["full"] }
eskf = "0.2"
nalgebra = "0.25"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.116"
serde_json = "1.0"
92 changes: 0 additions & 92 deletions src/bin/eskf.rs

This file was deleted.

86 changes: 0 additions & 86 deletions src/bin/rumqtt.rs

This file was deleted.

93 changes: 93 additions & 0 deletions src/bin/sailtrack-aggregator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use eskf::ESKF;
use std::thread;
use std::time::Duration;

use rumqttc::{Client, Event, Incoming, MqttOptions, QoS};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Euler {
x: f32,
y: f32,
z: f32,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Imu {
euler: Euler,
// TODO: Add fields
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Gps {
fix_type: i32,
// TODO: Add fields
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Boat {
sog: f32,
// TODO: Add fields
}

fn on_message_imu(filter: ESKF, message: Imu) {
println!("Received IMU message: {message:?}");
// TODO: Update filter
}

fn on_message_gps(filter: ESKF, message: Gps) {
println!("Received GPS message: {message:?}");
// TODO: Update filter
}

fn main() {
// TODO: Add username and password authentication
let mqqt_opts = MqttOptions::new("sailtrack-aggregator", "localhost", 1883);

let mut filter = eskf::Builder::new()
.acceleration_variance(0.01) // FIXME
.rotation_variance(0.01) // FIXME
.build();

let (client, mut connection) = Client::new(mqqt_opts, 10);
client.subscribe("sensor/gps0", QoS::AtMostOnce).unwrap();
client.subscribe("sensor/imu0", QoS::AtMostOnce).unwrap();

thread::spawn(move || {
loop {
let message = Boat {
sog: 10.0, // FIXME
};
client
.publish(
"boat",
QoS::AtLeastOnce,
false,
serde_json::to_vec(&message).unwrap(),
)
.unwrap();
println!("Published boat measurement: {message:?}");
thread::sleep(Duration::from_millis(200));
}
});

for notification in connection.iter().flatten() {
if let Event::Incoming(Incoming::Publish(packet)) = notification {
match packet.topic.as_str() {
"sensor/imu0" => on_message_imu(
filter,
serde_json::from_slice(packet.payload.as_ref()).unwrap(),
),
"sensor/gps0" => on_message_gps(
filter,
serde_json::from_slice(packet.payload.as_ref()).unwrap(),
),
_ => (),
}
}
}
}

0 comments on commit dc31c82

Please sign in to comment.