diff --git a/applications/autoware/Cargo.toml b/applications/autoware/Cargo.toml index 96d8a9c7f..31b7485c2 100644 --- a/applications/autoware/Cargo.toml +++ b/applications/autoware/Cargo.toml @@ -16,3 +16,5 @@ awkernel_lib = { path = "../../awkernel_lib", default-features = false } imu_driver = { path = "./imu_driver", default-features = false } imu_corrector = { path = "./imu_corrector", default-features = false } vehicle_velocity_converter = { path = "./vehicle_velocity_converter", default-features = false } +gyro_odometer = { path = "./gyro_odometer", default-features = false} + diff --git a/applications/autoware/common_types/Cargo.toml b/applications/autoware/common_types/Cargo.toml new file mode 100644 index 000000000..c54b7769b --- /dev/null +++ b/applications/autoware/common_types/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "common_types" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/applications/autoware/common_types/src/lib.rs b/applications/autoware/common_types/src/lib.rs new file mode 100644 index 000000000..2ca030990 --- /dev/null +++ b/applications/autoware/common_types/src/lib.rs @@ -0,0 +1,21 @@ +#![no_std] +extern crate alloc; + +#[derive(Debug, Clone)] +pub struct Header { + pub frame_id: &'static str, + pub timestamp: u64, +} + +#[derive(Debug, Clone)] +pub struct Vector3 { + pub x: f64, + pub y: f64, + pub z: f64, +} + +impl Vector3 { + pub fn new(x: f64, y: f64, z: f64) -> Self { + Self { x, y, z } + } +} diff --git a/applications/autoware/gyro_odometer/Cargo.toml b/applications/autoware/gyro_odometer/Cargo.toml new file mode 100644 index 000000000..332559fa2 --- /dev/null +++ b/applications/autoware/gyro_odometer/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "gyro_odometer" +version = "0.1.0" +edition = "2021" + +[dependencies] +imu_driver = { path = "../imu_driver", default-features = false } +imu_corrector = { path = "../imu_corrector", default-features = false } +vehicle_velocity_converter = { path = "../vehicle_velocity_converter", default-features = false} diff --git a/applications/autoware/gyro_odometer/src/lib.rs b/applications/autoware/gyro_odometer/src/lib.rs new file mode 100644 index 000000000..373e19b27 --- /dev/null +++ b/applications/autoware/gyro_odometer/src/lib.rs @@ -0,0 +1,462 @@ +// Copyright 2015-2019 Autoware Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Ported from the following versions of the original C++ code: +// core/autoware_core: +// type: git +// url: https://github.com/autowarefoundation/autoware_core.git +// original file path: localization/autoware_gyro_odometer/src/gyro_odometer_core.cpp +// test code: localization/autoware_gyro_odometer/test/test_gyro_odometer_helper.cpp +// version: 1.8.0 + +#![no_std] +extern crate alloc; + +use alloc::{collections::VecDeque, string::String}; +use core::ptr::null_mut; +use core::sync::atomic::{AtomicPtr, Ordering as AtomicOrdering}; +use core::time::Duration; + +pub use imu_corrector::{transform_covariance, ImuWithCovariance, Transform}; +pub use imu_driver::{Header, ImuMsg, Vector3}; +pub use vehicle_velocity_converter::{Twist, TwistWithCovariance, TwistWithCovarianceStamped}; + +static GYRO_ODOMETER_INSTANCE: AtomicPtr = AtomicPtr::new(null_mut()); + +const COV_IDX_X_X: usize = 0; +const COV_IDX_Y_Y: usize = 4; +const COV_IDX_Z_Z: usize = 8; +const COV_IDX_XYZRPY_X_X: usize = 0; +const COV_IDX_XYZRPY_Y_Y: usize = 7; +const COV_IDX_XYZRPY_Z_Z: usize = 14; +const COV_IDX_XYZRPY_ROLL_ROLL: usize = 21; +const COV_IDX_XYZRPY_PITCH_PITCH: usize = 28; +const COV_IDX_XYZRPY_YAW_YAW: usize = 35; + +pub struct GyroOdometerCore { + pub output_frame: String, + pub message_timeout_sec: f64, + pub vehicle_twist_arrived: bool, + pub imu_arrived: bool, + pub vehicle_twist_queue: VecDeque, + pub gyro_queue: VecDeque, + pub config: GyroOdometerConfig, +} + +impl GyroOdometerCore { + pub fn new(config: GyroOdometerConfig) -> Result { + let queue_size = config.queue_size; + let output_frame = config.output_frame.clone(); + let message_timeout_sec = config.message_timeout_sec; + + Ok(Self { + output_frame, + message_timeout_sec, + vehicle_twist_arrived: false, + imu_arrived: false, + vehicle_twist_queue: VecDeque::with_capacity(queue_size), + gyro_queue: VecDeque::with_capacity(queue_size), + config, + }) + } + + pub fn concat_gyro_and_odometer( + &mut self, + current_time: u64, + ) -> Result> { + if !self.vehicle_twist_arrived { + self.vehicle_twist_queue.clear(); + self.gyro_queue.clear(); + return Ok(None); + } + if !self.imu_arrived { + self.vehicle_twist_queue.clear(); + self.gyro_queue.clear(); + return Ok(None); + } + if !self.vehicle_twist_queue.is_empty() && !self.gyro_queue.is_empty() { + let latest_vehicle_twist_stamp = + self.vehicle_twist_queue.back().unwrap().header.timestamp; + let latest_imu_stamp = self.gyro_queue.back().unwrap().header.timestamp; + + if Self::check_timeout( + current_time, + latest_vehicle_twist_stamp, + self.message_timeout_sec, + ) { + self.vehicle_twist_queue.clear(); + self.gyro_queue.clear(); + return Err(GyroOdometerError::TimeoutError(String::from( + "Vehicle twist message timeout", + ))); + } + + if Self::check_timeout(current_time, latest_imu_stamp, self.message_timeout_sec) { + self.vehicle_twist_queue.clear(); + self.gyro_queue.clear(); + return Err(GyroOdometerError::TimeoutError(String::from( + "IMU message timeout", + ))); + } + } + + if self.vehicle_twist_queue.is_empty() || self.gyro_queue.is_empty() { + return Ok(None); + } + + let tf = self.get_transform( + &self.gyro_queue.front().unwrap().header.frame_id, + &self.output_frame, + )?; + + for gyro in &mut self.gyro_queue { + let transformed_angular_velocity = tf.apply_to_vector(gyro.angular_velocity.clone()); + gyro.angular_velocity = transformed_angular_velocity; + } + + let mut vx_mean = 0.0; + let mut gyro_mean = Vector3::new(0.0, 0.0, 0.0); + let mut vx_covariance_original = 0.0; + let mut gyro_covariance_original = Vector3::new(0.0, 0.0, 0.0); + + for vehicle_twist in &self.vehicle_twist_queue { + vx_mean += vehicle_twist.twist.twist.linear.x; + vx_covariance_original += vehicle_twist.twist.covariance[0 * 6 + 0]; + } + vx_mean /= self.vehicle_twist_queue.len() as f64; + vx_covariance_original /= self.vehicle_twist_queue.len() as f64; + + for gyro in &self.gyro_queue { + gyro_mean.x += gyro.angular_velocity.x; + gyro_mean.y += gyro.angular_velocity.y; + gyro_mean.z += gyro.angular_velocity.z; + gyro_covariance_original.x += gyro.angular_velocity_covariance[COV_IDX_X_X]; + gyro_covariance_original.y += gyro.angular_velocity_covariance[COV_IDX_Y_Y]; + gyro_covariance_original.z += gyro.angular_velocity_covariance[COV_IDX_Z_Z]; + } + gyro_mean.x /= self.gyro_queue.len() as f64; + gyro_mean.y /= self.gyro_queue.len() as f64; + gyro_mean.z /= self.gyro_queue.len() as f64; + gyro_covariance_original.x /= self.gyro_queue.len() as f64; + gyro_covariance_original.y /= self.gyro_queue.len() as f64; + gyro_covariance_original.z /= self.gyro_queue.len() as f64; + + let latest_vehicle_twist_stamp = self.vehicle_twist_queue.back().unwrap().header.timestamp; + let latest_imu_stamp = self.gyro_queue.back().unwrap().header.timestamp; + + let result_timestamp = if latest_vehicle_twist_stamp < latest_imu_stamp { + latest_imu_stamp + } else { + latest_vehicle_twist_stamp + }; + + let mut result = TwistWithCovarianceStamped { + header: Header { + frame_id: self.gyro_queue.front().unwrap().header.frame_id, + timestamp: result_timestamp, + }, + twist: TwistWithCovariance { + twist: Twist { + linear: Vector3::new(vx_mean, 0.0, 0.0), + angular: gyro_mean, + }, + covariance: [0.0; 36], + }, + }; + + result.twist.covariance[COV_IDX_XYZRPY_X_X] = + vx_covariance_original / self.vehicle_twist_queue.len() as f64; + result.twist.covariance[COV_IDX_XYZRPY_Y_Y] = 100000.0; + result.twist.covariance[COV_IDX_XYZRPY_Z_Z] = 100000.0; + result.twist.covariance[COV_IDX_XYZRPY_ROLL_ROLL] = + gyro_covariance_original.x / self.gyro_queue.len() as f64; + result.twist.covariance[COV_IDX_XYZRPY_PITCH_PITCH] = + gyro_covariance_original.y / self.gyro_queue.len() as f64; + result.twist.covariance[COV_IDX_XYZRPY_YAW_YAW] = + gyro_covariance_original.z / self.gyro_queue.len() as f64; + + self.vehicle_twist_queue.clear(); + self.gyro_queue.clear(); + + Ok(Some(result)) + } + + pub fn check_timeout(current_timestamp: u64, last_timestamp: u64, timeout_sec: f64) -> bool { + let dt = (current_timestamp as f64 - last_timestamp as f64) / 1_000_000_000.0; + dt.abs() > timeout_sec + } + pub fn get_transform(&self, from_frame: &str, to_frame: &str) -> Result { + if from_frame == to_frame || from_frame == "" || to_frame == "" { + Ok(Transform::identity()) + } else { + Ok(Transform::identity()) + } + } + + pub fn process_result( + &self, + twist_with_cov_raw: TwistWithCovarianceStamped, + ) -> TwistWithCovarianceStamped { + if twist_with_cov_raw.twist.twist.angular.z.abs() < 0.01 + && twist_with_cov_raw.twist.twist.linear.x.abs() < 0.01 + { + let mut twist = twist_with_cov_raw; + twist.twist.twist.angular.x = 0.0; + twist.twist.twist.angular.y = 0.0; + twist.twist.twist.angular.z = 0.0; + twist + } else { + twist_with_cov_raw + } + } + + pub fn add_vehicle_twist(&mut self, twist: TwistWithCovarianceStamped) { + self.vehicle_twist_arrived = true; + self.vehicle_twist_queue.push_back(twist); + } + + pub fn add_imu(&mut self, imu: ImuWithCovariance) { + self.imu_arrived = true; + self.gyro_queue.push_back(imu); + } + + pub fn process_imu_with_covariance(&mut self, imu: ImuWithCovariance) -> Result<()> { + self.add_imu(imu); + Ok(()) + } + + pub fn process_and_get_result( + &mut self, + current_time: u64, + ) -> Option { + match self.concat_gyro_and_odometer(current_time) { + Ok(result) => result, + Err(_) => None, + } + } + + pub fn get_queue_sizes(&self) -> (usize, usize) { + (self.vehicle_twist_queue.len(), self.gyro_queue.len()) + } + + pub fn clear_queues(&mut self) { + self.vehicle_twist_queue.clear(); + self.gyro_queue.clear(); + } + + pub fn reset_arrival_flags(&mut self) { + self.vehicle_twist_arrived = false; + self.imu_arrived = false; + } +} + +#[derive(Debug)] +pub enum GyroOdometerError { + TransformError(String), + TimeoutError(String), + QueueError(String), + ParameterError(String), +} + +impl core::fmt::Display for GyroOdometerError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + GyroOdometerError::TransformError(msg) => write!(f, "Transform error: {}", msg), + GyroOdometerError::TimeoutError(msg) => write!(f, "Timeout error: {}", msg), + GyroOdometerError::QueueError(msg) => write!(f, "Queue error: {}", msg), + GyroOdometerError::ParameterError(msg) => write!(f, "Invalid parameter: {}", msg), + } + } +} + +impl core::error::Error for GyroOdometerError {} + +type Result = core::result::Result; + +#[derive(Debug, Clone)] +pub struct GyroOdometerConfig { + pub output_frame: String, + pub message_timeout_sec: f64, + pub queue_size: usize, + pub transform_timeout: Duration, + pub min_velocity_threshold: f64, + pub covariance_scale: f64, +} + +impl Default for GyroOdometerConfig { + fn default() -> Self { + Self { + output_frame: String::from("base_link"), + message_timeout_sec: 1.0, + queue_size: 100, + transform_timeout: Duration::from_secs(1), + min_velocity_threshold: 0.01, + covariance_scale: 100000.0, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + // Equivalent helpers to Autoware C++ test helper. + fn generate_sample_imu() -> ImuMsg { + ImuMsg { + header: Header { + frame_id: "base_link", + timestamp: 123456789, + }, + angular_velocity: Vector3::new(0.1, 0.2, 0.3), + linear_acceleration: Vector3::new(9.8, 0.0, 0.0), + } + } + + fn generate_sample_velocity() -> TwistWithCovarianceStamped { + TwistWithCovarianceStamped { + header: Header { + frame_id: "base_link", + timestamp: 123456789, + }, + twist: TwistWithCovariance { + twist: Twist { + linear: Vector3::new(1.0, 0.0, 0.0), + angular: Vector3::new(0.0, 0.0, 0.0), + }, + covariance: [0.0; 36], + }, + } + } + + fn get_config_with_default_params() -> GyroOdometerConfig { + GyroOdometerConfig { + output_frame: String::from("base_link"), + message_timeout_sec: 1e12, + ..GyroOdometerConfig::default() + } + } + + #[test] + fn test_gyro_odometer_core_creation() { + let config = get_config_with_default_params(); + let core = GyroOdometerCore::new(config); + assert!(core.is_ok()); + } + + #[test] + fn test_imu_with_covariance_conversion() { + let imu_msg = generate_sample_imu(); + + let imu_with_cov = ImuWithCovariance::from_imu_msg(&imu_msg); + let converted_back = imu_with_cov.to_imu_msg(); + + assert_eq!(imu_msg.header.frame_id, converted_back.header.frame_id); + assert_eq!(imu_msg.header.timestamp, converted_back.header.timestamp); + assert_eq!( + imu_msg.angular_velocity.x, + converted_back.angular_velocity.x + ); + assert_eq!( + imu_msg.angular_velocity.y, + converted_back.angular_velocity.y + ); + assert_eq!( + imu_msg.angular_velocity.z, + converted_back.angular_velocity.z + ); + } + + #[test] + fn test_add_vehicle_twist() { + let config = get_config_with_default_params(); + let mut core = GyroOdometerCore::new(config).unwrap(); + + let sample_twist = generate_sample_velocity(); + assert_eq!(sample_twist.header.frame_id, "base_link"); + assert_eq!(sample_twist.twist.twist.linear.x, 1.0); + + core.add_vehicle_twist(sample_twist.clone()); + + assert_eq!(core.get_queue_sizes(), (1, 0)); + assert!(core.vehicle_twist_arrived); + let queued_twist = core.vehicle_twist_queue.front().unwrap(); + assert_eq!(queued_twist.header.frame_id, sample_twist.header.frame_id); + assert_eq!(queued_twist.header.timestamp, sample_twist.header.timestamp); + assert_eq!(queued_twist.twist.twist.linear.x, 1.0); + assert_eq!(queued_twist.twist.twist.linear.y, 0.0); + assert_eq!(queued_twist.twist.twist.linear.z, 0.0); + } + + #[test] + fn test_imu_corrector_integration() { + let config = get_config_with_default_params(); + let mut core = GyroOdometerCore::new(config).unwrap(); + + let imu_msg = generate_sample_imu(); + let mut imu_with_cov = ImuWithCovariance::from_imu_msg(&imu_msg); + imu_with_cov.angular_velocity_covariance = + [0.0009, 0.0, 0.0, 0.0, 0.0009, 0.0, 0.0, 0.0, 0.0009]; + imu_with_cov.linear_acceleration_covariance = [ + 100000000.0, + 0.0, + 0.0, + 0.0, + 100000000.0, + 0.0, + 0.0, + 0.0, + 100000000.0, + ]; + + let result = core.process_imu_with_covariance(imu_with_cov); + assert!(result.is_ok()); + } + + #[test] + fn test_transform_covariance_from_imu_corrector() { + let input = [1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0]; + let output = transform_covariance(&input); + assert_eq!(output[COV_IDX_X_X], 3.0); + assert_eq!(output[COV_IDX_Y_Y], 3.0); + assert_eq!(output[COV_IDX_Z_Z], 3.0); + } +} + +pub fn get_or_initialize() -> Result<&'static mut GyroOdometerCore> { + let ptr = GYRO_ODOMETER_INSTANCE.load(AtomicOrdering::Acquire); + + if !ptr.is_null() { + return Ok(unsafe { &mut *ptr }); + } + + let config = GyroOdometerConfig::default(); + let core = GyroOdometerCore::new(config)?; + let boxed_core = alloc::boxed::Box::new(core); + let new_ptr = alloc::boxed::Box::into_raw(boxed_core); + + match GYRO_ODOMETER_INSTANCE.compare_exchange( + null_mut(), + new_ptr, + AtomicOrdering::Acquire, + AtomicOrdering::Relaxed, + ) { + Ok(_) => Ok(unsafe { &mut *new_ptr }), + Err(existing_ptr) => { + unsafe { + let _ = alloc::boxed::Box::from_raw(new_ptr); + } + Ok(unsafe { &mut *existing_ptr }) + } + } +} diff --git a/applications/autoware/imu_driver/Cargo.toml b/applications/autoware/imu_driver/Cargo.toml index da816a55f..16fd6f500 100644 --- a/applications/autoware/imu_driver/Cargo.toml +++ b/applications/autoware/imu_driver/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +common_types = { path = "../common_types", default-features = false } diff --git a/applications/autoware/imu_driver/src/lib.rs b/applications/autoware/imu_driver/src/lib.rs index 857886eac..c04edb768 100644 --- a/applications/autoware/imu_driver/src/lib.rs +++ b/applications/autoware/imu_driver/src/lib.rs @@ -60,6 +60,8 @@ extern crate alloc; use alloc::{format, string::String, vec, vec::Vec}; use core::f64::consts::PI; +pub use common_types::{Header, Vector3}; + #[derive(Clone, Debug)] pub struct ImuMsg { pub header: Header, @@ -74,12 +76,6 @@ pub struct ImuCsvRow { pub linear_acceleration: Vector3, } -#[derive(Clone, Debug)] -pub struct Header { - pub frame_id: &'static str, - pub timestamp: u64, -} - #[derive(Debug, Clone)] pub struct Quaternion { pub x: f64, @@ -88,19 +84,6 @@ pub struct Quaternion { pub w: f64, } -#[derive(Clone, Debug)] -pub struct Vector3 { - pub x: f64, - pub y: f64, - pub z: f64, -} - -impl Vector3 { - pub fn new(x: f64, y: f64, z: f64) -> Self { - Self { x, y, z } - } -} - impl Default for ImuMsg { fn default() -> Self { Self { diff --git a/applications/autoware/vehicle_velocity_converter/Cargo.toml b/applications/autoware/vehicle_velocity_converter/Cargo.toml index 45fb9b156..067d89ddd 100644 --- a/applications/autoware/vehicle_velocity_converter/Cargo.toml +++ b/applications/autoware/vehicle_velocity_converter/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +common_types = { path = "../common_types", default-features = false } log = { version = "0.4", default-features = false } diff --git a/applications/autoware/vehicle_velocity_converter/src/lib.rs b/applications/autoware/vehicle_velocity_converter/src/lib.rs index ddb9607cc..19f9a36de 100644 --- a/applications/autoware/vehicle_velocity_converter/src/lib.rs +++ b/applications/autoware/vehicle_velocity_converter/src/lib.rs @@ -22,18 +22,7 @@ #![no_std] -#[derive(Debug, Clone)] -pub struct Header { - pub frame_id: &'static str, - pub timestamp: u64, -} - -#[derive(Debug, Clone)] -pub struct Vector3 { - pub x: f64, - pub y: f64, - pub z: f64, -} +pub use common_types::{Header, Vector3}; #[derive(Debug, Clone)] pub struct VelocityReport {