-
Notifications
You must be signed in to change notification settings - Fork 25
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
#10 (stage 1) implement complementary filter to fuse acc/gyro #11
Open
tribbloid
wants to merge
1
commit into
badicsalex:master
Choose a base branch
from
hpvdt:complementary-filter/dev1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+355
−23
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (C) 2023, Alex Badics | ||
// This file is part of ar-drivers-rs | ||
// Licensed under the MIT license. See LICENSE file in the project root for details. | ||
|
||
use ar_drivers::any_fusion; | ||
|
||
fn main() { | ||
let mut fusion = any_fusion().unwrap(); // Declare conn as mutable | ||
|
||
let serial = fusion.glasses().serial().unwrap(); | ||
println!("Got glasses, serial={}", serial); | ||
|
||
println!(""); | ||
loop { | ||
fusion.update(); | ||
let quaternion = fusion.attitude_quaternion(); | ||
let frd = fusion.attitude_frd_deg(); | ||
let inconsistency = fusion.inconsistency(); | ||
|
||
println!("quaternion:\t{:10.7}", quaternion); | ||
println!("euler:\t{:10.7}", frd.transpose()); | ||
|
||
println!("inconsistency:\t{:10.7}", inconsistency); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
format_code_in_doc_comments = true | ||
format_macro_matchers = true | ||
group_imports = "StdExternalCrate" | ||
hex_literal_case = "Lower" | ||
imports_granularity = "Module" | ||
newline_style = "Unix" | ||
normalize_comments = true | ||
normalize_doc_attributes = true | ||
use_field_init_shorthand = true | ||
use_try_shorthand = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,10 +36,13 @@ | |
|
||
use nalgebra::{Isometry3, Matrix3, UnitQuaternion, Vector2, Vector3}; | ||
|
||
use crate::naive_cf::NaiveCF; | ||
|
||
#[cfg(feature = "grawoow")] | ||
pub mod grawoow; | ||
#[cfg(feature = "mad_gaze")] | ||
pub mod mad_gaze; | ||
mod naive_cf; | ||
#[cfg(feature = "nreal")] | ||
pub mod nreal_air; | ||
#[cfg(feature = "nreal")] | ||
|
@@ -77,6 +80,58 @@ pub enum Error { | |
|
||
type Result<T> = std::result::Result<T, Error>; | ||
|
||
/* | ||
high level interface of glasses & state estimation, with the following built-in fusion pipeline: | ||
|
||
(the first version should only use complementary filter for simplicity and sanity test) | ||
|
||
- roll/pitch <= acc + gyro (complementary filter) | ||
- assuming that acc vector always pointed up, spacecraft moving in that direction can create 1G artificial gravity | ||
- TODO: this obviously assumes no steadily accelerating frame, at which point up d_acc has to be used for correction | ||
- TODO: use ESKF (error-state/multiplicatory KF, https://arxiv.org/abs/1711.02508) | ||
- gyro-yaw <= gyro (integrate over time) | ||
- mag-yaw <= mag + roll/pitch (arctan) | ||
- TODO: mag calibration? | ||
(continuous ellipsoid fitting, assuming homogeneous E-M environment & hardpoint-mounted E-M interference) | ||
- yaw <= mag-yaw + gyro-gyro (complementary filter) | ||
- TODO: use EKF | ||
|
||
CAUTION: unlike [[GlassesEvent]], all states & outputs should use FRD reference frame | ||
(forward, right, down, corresponding to roll, pitch, yaw in Euler angles-represented rotation) | ||
|
||
FRD is the standard frame for aerospace, and is also the default frame for NALgebra | ||
*/ | ||
pub trait Fusion: Send { | ||
fn glasses(&mut self) -> &mut Box<dyn ARGlasses>; | ||
// TODO: only declared mutable as many API of ARGlasses are also mutable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could have two APIs: |
||
|
||
/// primary estimation output | ||
/// can be used to convert to Euler angles of different conventions | ||
fn attitude_quaternion(&self) -> UnitQuaternion<f32>; | ||
|
||
/// use FRD frame as error in Quaternion is multiplicative & is over-defined | ||
fn inconsistency(&self) -> f32; | ||
|
||
fn update(&mut self) -> (); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should most probably be |
||
} | ||
|
||
impl dyn Fusion { | ||
pub fn attitude_frd_rad(&self) -> Vector3<f32> { | ||
let (roll, pitch, yaw) = (self.attitude_quaternion()).euler_angles(); | ||
Vector3::new(roll, pitch, yaw) | ||
} | ||
|
||
pub fn attitude_frd_deg(&self) -> Vector3<f32> { | ||
self.attitude_frd_rad().map(|x| x.to_degrees()) | ||
} | ||
} | ||
|
||
pub fn any_fusion() -> Result<Box<dyn Fusion>> { | ||
// let glasses = any_glasses()?; | ||
let glasses = any_glasses()?; | ||
Ok(Box::new(NaiveCF::new(glasses)?)) | ||
} | ||
|
||
impl std::error::Error for Error { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
match self { | ||
|
@@ -243,30 +298,43 @@ pub struct DisplayMatrices { | |
pub isometry: Isometry3<f64>, | ||
} | ||
|
||
fn upcast<G: ARGlasses + 'static>(result: Result<G>) -> Result<Box<dyn ARGlasses>> { | ||
result.map(|glasses| Box::new(glasses) as Box<dyn ARGlasses>) | ||
} | ||
|
||
/// Convenience function to detect and connect to any of the supported glasses | ||
#[cfg(not(target_os = "android"))] | ||
pub fn any_glasses() -> Result<Box<dyn ARGlasses>> { | ||
#[cfg(feature = "rokid")] | ||
if let Ok(glasses) = rokid::RokidAir::new() { | ||
return Ok(Box::new(glasses)); | ||
}; | ||
#[cfg(feature = "nreal")] | ||
if let Ok(glasses) = nreal_air::NrealAir::new() { | ||
return Ok(Box::new(glasses)); | ||
}; | ||
#[cfg(feature = "nreal")] | ||
if let Ok(glasses) = nreal_light::NrealLight::new() { | ||
return Ok(Box::new(glasses)); | ||
}; | ||
#[cfg(feature = "grawoow")] | ||
if let Ok(glasses) = grawoow::GrawoowG530::new() { | ||
return Ok(Box::new(glasses)); | ||
}; | ||
#[cfg(feature = "mad_gaze")] | ||
if let Ok(glasses) = mad_gaze::MadGazeGlow::new() { | ||
return Ok(Box::new(glasses)); | ||
}; | ||
Err(Error::NotFound) | ||
let glasses_factories: Vec<(&str, fn() -> Result<Box<dyn ARGlasses>>)> = vec![ | ||
#[cfg(feature = "rokid")] | ||
("RokidAir", || upcast(rokid::RokidAir::new())), | ||
#[cfg(feature = "nreal")] | ||
("NrealAir", || upcast(nreal_air::NrealAir::new())), | ||
#[cfg(feature = "nreal")] | ||
("NrealLight", || upcast(nreal_light::NrealLight::new())), | ||
#[cfg(feature = "grawoow")] | ||
("GrawoowG530", || upcast(grawoow::GrawoowG530::new())), | ||
#[cfg(feature = "mad_gaze")] | ||
("MadGazeGlow", || upcast(mad_gaze::MadGazeGlow::new())), | ||
]; | ||
|
||
glasses_factories | ||
.into_iter() | ||
.find_map(|(glasses_type, factory)| { | ||
let factory: fn() -> Result<Box<dyn ARGlasses>> = factory; | ||
|
||
factory() | ||
.map_err(|e| { | ||
// | ||
println!("can't find {}: {}", glasses_type, e) | ||
}) | ||
.ok() | ||
.map(|v| { | ||
println!("found {}", glasses_type); | ||
v | ||
}) | ||
}) | ||
.ok_or(Error::NotFound) | ||
} | ||
|
||
impl From<std::io::Error> for Error { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to use rustfmt at factory setting always, so please don't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will remove