Skip to content

Commit

Permalink
Staging (#2)
Browse files Browse the repository at this point in the history
* named calibration profiles

* release v0.3.4
  • Loading branch information
galister authored Nov 7, 2024
1 parent 7e426a5 commit 35ef00e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "motoc"
description = "Monado Tracking Origin Calibrator"
version = "0.3.3"
version = "0.3.4"
edition = "2021"

[dependencies]
Expand Down
19 changes: 17 additions & 2 deletions src/calibrator/sampled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,25 @@ pub struct SampledMethod {
maintain: bool,
num_samples: usize,
progress: Option<ProgressBar>,
profile: String,
}

impl SampledMethod {
pub fn new(src_dev: usize, dst_dev: usize, maintain: bool, samples: u32) -> Self {
pub fn new(
src_dev: usize,
dst_dev: usize,
maintain: bool,
samples: u32,
profile: String,
) -> Self {
Self {
src_dev,
dst_dev,
samples: Vec::with_capacity(1000),
maintain,
num_samples: samples as _,
progress: None,
profile,
}
}

Expand Down Expand Up @@ -296,7 +304,13 @@ impl Calibrator for SampledMethod {
if self.maintain {
let offset = self.avg_b_to_a_offset(&offset);

match data.save_calibration(self.src_dev, self.dst_dev, offset, OffsetType::Device) {
match data.save_calibration(
&self.profile,
self.src_dev,
self.dst_dev,
offset,
OffsetType::Device,
) {
Ok(_) => log::info!(
"Saved calibration. Use `motoc continue` on next startup to use this."
),
Expand All @@ -312,6 +326,7 @@ impl Calibrator for SampledMethod {
} else {
let src_origin = data.get_device_origin(self.src_dev)?;
match data.save_calibration(
&self.profile,
src_origin.id as _,
dst_origin.id as _,
dst_root,
Expand Down
13 changes: 10 additions & 3 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl<'a> CalibratorData<'a> {

pub fn save_calibration(
&self,
profile: &str,
src: usize,
dst: usize,
offset: TransformD,
Expand All @@ -95,7 +96,7 @@ impl<'a> CalibratorData<'a> {
if !path.exists() {
std::fs::create_dir_all(&path)?;
}
path.push("last.json");
path.push(format!("{}.json", profile));

let (src_name, dst_name) = match offset_type {
OffsetType::TrackingOrigin => (
Expand All @@ -120,10 +121,16 @@ impl<'a> CalibratorData<'a> {
Ok(())
}

pub fn load_calibration(&self) -> anyhow::Result<SavedCalibration> {
pub fn load_calibration(&self, profile: &str) -> anyhow::Result<SavedCalibration> {
let xdg_dirs = xdg::BaseDirectories::new()?;
let mut path = xdg_dirs.get_config_home();
path.push("motoc/last.json");
path.push("motoc");
path.push(format!("{}.json", profile));

log::debug!(
"Will load calibration data from: {}",
path.to_string_lossy()
);

let f = File::open(path)?;
let data: SavedCalibration = serde_json::from_reader(f)?;
Expand Down
28 changes: 22 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ fn xr_loop(args: Args, monado: mnd::Monado, mut status: MultiProgress) -> anyhow
roll.unwrap_or(0.0),
),
vec3(x.unwrap_or(0.0), y.unwrap_or(0.0), z.unwrap_or(0.0)),
lerp.unwrap_or(0.05),
lerp,
);
c.init(&mut data, &mut status)?;
c
Expand All @@ -313,6 +313,7 @@ fn xr_loop(args: Args, monado: mnd::Monado, mut status: MultiProgress) -> anyhow
ref dst,
r#continue: maintain,
samples,
ref profile,
} => {
let Some(src_dev) = data.find_device(src) else {
log::error!("src: no such device: {}", &src);
Expand All @@ -336,13 +337,20 @@ fn xr_loop(args: Args, monado: mnd::Monado, mut status: MultiProgress) -> anyhow
dst_dev,
maintain,
samples.unwrap_or(500),
profile.clone(),
);
c.init(&mut data, &mut status)?;
c
}));
}
Subcommands::Continue => {
let last = data.load_calibration()?;
Subcommands::Continue { ref profile } => {
let Ok(last) = data.load_calibration(profile.as_str()) else {
log::error!(
"Could not load calibration for profile '{}'. Did you mean to calibrate first?",
profile
);
break 'main_loop;
};

match last.offset_type {
OffsetType::TrackingOrigin => {
Expand Down Expand Up @@ -553,8 +561,8 @@ enum Subcommands {
z: Option<f64>,

/// interpolation factor, lower is smoother. range (0, 1]
#[arg(long, value_name = "FACTOR")]
lerp: Option<f64>,
#[arg(long, value_name = "FACTOR", default_value = "0.05")]
lerp: f64,
},
/// Calibrate by sampling two devices that move together over time
Calibrate {
Expand All @@ -573,6 +581,10 @@ enum Subcommands {
/// number of samples to use for initial calibration. default: 500
#[arg(long)]
samples: Option<u32>,

/// save the calubration with this profile name
#[arg(long, value_name = "NAME", default_value = "last")]
profile: String,
},
/// Manually adjust the offset of the given tracking origin
Adjust {
Expand Down Expand Up @@ -607,7 +619,11 @@ enum Subcommands {
id: String,
},
/// Load a previous calibration. If last calibration was not continous; apply once and exit.
Continue,
Continue {
/// load the calubration from this profile
#[arg(long, value_name = "NAME", default_value = "last")]
profile: String,
},
/// Check if Monado is reachable, then exit.
Check,
}

0 comments on commit 35ef00e

Please sign in to comment.