diff --git a/applications/autoware/Cargo.toml b/applications/autoware/Cargo.toml index 96d8a9c7f..ff87530c7 100644 --- a/applications/autoware/Cargo.toml +++ b/applications/autoware/Cargo.toml @@ -13,6 +13,9 @@ libm = "0.2" csv-core = "0.1" awkernel_async_lib = { path = "../../awkernel_async_lib", default-features = false } awkernel_lib = { path = "../../awkernel_lib", default-features = false } +common_types = { path = "./common_types", 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} +ekf_localizer = { path = "./ekf_localizer", default-features = false} diff --git a/applications/autoware/build.rs b/applications/autoware/build.rs new file mode 100644 index 000000000..7269fdc9f --- /dev/null +++ b/applications/autoware/build.rs @@ -0,0 +1,31 @@ +use std::{env, fs, path::PathBuf}; + +fn main() { + let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR is not set")); + let csv_data_path = out_dir.join("csv_data.rs"); + + let imu_csv = read_csv_from_env("IMU_CSV_PATH"); + let velocity_csv = read_csv_from_env("VELOCITY_CSV_PATH"); + + let generated = format!( + "pub const IMU_CSV_DATA_STR: &str = {imu:?};\npub const VELOCITY_CSV_DATA_STR: &str = {velocity:?};\n", + imu = imu_csv, + velocity = velocity_csv, + ); + + fs::write(&csv_data_path, generated).expect("failed to write generated csv constants"); +} + +fn read_csv_from_env(var_name: &str) -> String { + let Ok(path) = env::var(var_name) else { + return String::new(); + }; + + match fs::read_to_string(&path) { + Ok(contents) => contents, + Err(err) => { + println!("cargo:warning=failed to read {var_name} from {path}: {err}"); + String::new() + } + } +} 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/ekf_localizer/Cargo.toml b/applications/autoware/ekf_localizer/Cargo.toml new file mode 100644 index 000000000..e2b8866b6 --- /dev/null +++ b/applications/autoware/ekf_localizer/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "ekf_localizer" +version = "0.1.0" +edition = "2021" + +[dependencies] +libm = "0.2" +nalgebra = { version = "0.32", default-features = false} +approx = "0.5" +common_types = { path = "../common_types", default-features = false } +vehicle_velocity_converter = { path = "../vehicle_velocity_converter", default-features = false} \ No newline at end of file diff --git a/applications/autoware/ekf_localizer/src/lib.rs b/applications/autoware/ekf_localizer/src/lib.rs new file mode 100644 index 000000000..0bc9dab63 --- /dev/null +++ b/applications/autoware/ekf_localizer/src/lib.rs @@ -0,0 +1,652 @@ +// Ported from the following versions of the original C++ code: +// core/autoware_core: +// type: git +// url: https://github.com/autowarefoundation/autoware_core.git +// version: 1.8.0 + +#![no_std] +#![allow(non_snake_case)] + +extern crate alloc; + +use alloc::{vec, vec::Vec}; +pub use common_types::Header; +use core::ptr::null_mut; +use core::sync::atomic::{AtomicPtr, Ordering as AtomicOrdering}; +use libm::{atan2, cos, sin}; +use nalgebra::{Matrix6, Vector3, Vector6}; +pub use vehicle_velocity_converter::TwistWithCovariance; + +static EKF_MODULE_INSTANCE: AtomicPtr = AtomicPtr::new(null_mut()); + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum StateIndex { + X = 0, + Y = 1, + Yaw = 2, + YawBias = 3, + Vx = 4, + Wz = 5, +} + +pub type StateVector = Vector6; +pub type StateCovariance = Matrix6; + +#[derive(Debug, Clone, Copy)] +pub struct Point3D { + pub x: f64, + pub y: f64, + pub z: f64, +} + +#[derive(Debug, Clone, Copy)] +pub struct Quaternion { + pub x: f64, + pub y: f64, + pub z: f64, + pub w: f64, +} + +#[derive(Debug, Clone, Copy)] +pub struct Pose { + pub position: Point3D, + pub orientation: Quaternion, +} + +#[derive(Debug, Clone, Copy)] +pub struct Twist { + pub linear: Vector3, + pub angular: Vector3, +} + +#[derive(Debug, Clone, Copy)] +pub struct PoseWithCovariance { + pub pose: Pose, + pub covariance: [f64; 36], +} + +#[derive(Debug, Clone)] +pub struct EKFOdometry { + pub header: common_types::Header, + pub child_frame_id: &'static str, + pub pose: PoseWithCovariance, + pub twist: TwistWithCovariance, +} + +#[derive(Debug, Clone)] +pub struct EKFParameters { + pub enable_yaw_bias_estimation: bool, + pub extend_state_step: usize, + pub proc_stddev_vx_c: f64, + pub proc_stddev_wz_c: f64, + pub proc_stddev_yaw_c: f64, + pub z_filter_proc_dev: f64, + pub roll_filter_proc_dev: f64, + pub pitch_filter_proc_dev: f64, +} + +impl Default for EKFParameters { + fn default() -> Self { + Self { + enable_yaw_bias_estimation: true, + extend_state_step: 50, + proc_stddev_vx_c: 2.0, + proc_stddev_wz_c: 1.0, + proc_stddev_yaw_c: 0.005, + z_filter_proc_dev: 1.0, + roll_filter_proc_dev: 0.1, + pitch_filter_proc_dev: 0.1, + } + } +} + +#[derive(Debug, Clone)] +pub struct Simple1DFilter { + initialized: bool, + x: f64, + var: f64, + proc_var_x_c: f64, +} + +impl Default for Simple1DFilter { + fn default() -> Self { + Self::new() + } +} + +impl Simple1DFilter { + pub fn new() -> Self { + Self { + initialized: false, + x: 0.0, + var: 1e9, + proc_var_x_c: 0.0, + } + } + + pub fn init(&mut self, init_obs: f64, obs_var: f64) { + self.x = init_obs; + self.var = obs_var; + self.initialized = true; + } + + pub fn update(&mut self, obs: f64, obs_var: f64, dt: f64) { + if !self.initialized { + self.init(obs, obs_var); + return; + } + + let proc_var_x_d = self.proc_var_x_c * dt * dt; + self.var += proc_var_x_d; + + let kalman_gain = self.var / (self.var + obs_var); + self.x += kalman_gain * (obs - self.x); + self.var *= 1.0 - kalman_gain; + } + + pub fn set_proc_var(&mut self, proc_var: f64) { + self.proc_var_x_c = proc_var; + } + + pub fn get_x(&self) -> f64 { + self.x + } + + pub fn get_var(&self) -> f64 { + self.var + } +} + +#[derive(Debug, Clone)] +pub struct EKFModule { + params: EKFParameters, + state: StateVector, + covariance: StateCovariance, + z_filter: Simple1DFilter, + roll_filter: Simple1DFilter, + pitch_filter: Simple1DFilter, + accumulated_delay_times: Vec, + // When true, only prediction is performed (no measurement updates) + is_mrm_mode: bool, +} + +impl EKFModule { + pub fn new(params: EKFParameters) -> Self { + let state = StateVector::zeros(); + let mut covariance = StateCovariance::identity() * 1e15; + + covariance[(StateIndex::Yaw as usize, StateIndex::Yaw as usize)] = 50.0; + if params.enable_yaw_bias_estimation { + covariance[(StateIndex::YawBias as usize, StateIndex::YawBias as usize)] = 50.0; + } + covariance[(StateIndex::Vx as usize, StateIndex::Vx as usize)] = 1000.0; + covariance[(StateIndex::Wz as usize, StateIndex::Wz as usize)] = 50.0; + + let mut z_filter = Simple1DFilter::new(); + let mut roll_filter = Simple1DFilter::new(); + let mut pitch_filter = Simple1DFilter::new(); + + z_filter.set_proc_var(params.z_filter_proc_dev * params.z_filter_proc_dev); + roll_filter.set_proc_var(params.roll_filter_proc_dev * params.roll_filter_proc_dev); + pitch_filter.set_proc_var(params.pitch_filter_proc_dev * params.pitch_filter_proc_dev); + + let accumulated_delay_times = vec![1e15; params.extend_state_step]; + + Self { + params, + state, + covariance, + z_filter, + roll_filter, + pitch_filter, + accumulated_delay_times, + is_mrm_mode: false, + } + } + + pub fn initialize(&mut self, initial_pose: Pose) { + self.state[StateIndex::X as usize] = initial_pose.position.x; + self.state[StateIndex::Y as usize] = initial_pose.position.y; + self.state[StateIndex::Yaw as usize] = self.quaternion_to_yaw(initial_pose.orientation); + self.state[StateIndex::YawBias as usize] = 0.0; + self.state[StateIndex::Vx as usize] = 0.0; + self.state[StateIndex::Wz as usize] = 0.0; + + self.covariance = StateCovariance::identity() * 0.01; + if self.params.enable_yaw_bias_estimation { + self.covariance[(StateIndex::YawBias as usize, StateIndex::YawBias as usize)] = 0.0001; + } + + self.z_filter.init(initial_pose.position.z, 0.01); + self.roll_filter.init(0.0, 0.01); + self.pitch_filter.init(0.0, 0.01); + } + + fn predict_next_state(&self, dt: f64) -> StateVector { + let mut x_next = self.state; + let x = self.state[StateIndex::X as usize]; + let y = self.state[StateIndex::Y as usize]; + let yaw = self.state[StateIndex::Yaw as usize]; + let yaw_bias = self.state[StateIndex::YawBias as usize]; + let vx = self.state[StateIndex::Vx as usize]; + let wz = self.state[StateIndex::Wz as usize]; + + x_next[StateIndex::X as usize] = x + vx * cos(yaw + yaw_bias) * dt; + x_next[StateIndex::Y as usize] = y + vx * sin(yaw + yaw_bias) * dt; + let yaw_next = yaw + wz * dt; + x_next[StateIndex::Yaw as usize] = atan2(sin(yaw_next), cos(yaw_next)); + x_next[StateIndex::YawBias as usize] = yaw_bias; + x_next[StateIndex::Vx as usize] = vx; + x_next[StateIndex::Wz as usize] = wz; + + x_next + } + + fn create_state_transition_matrix(&self, dt: f64) -> Matrix6 { + let mut F = Matrix6::identity(); + let yaw = self.state[StateIndex::Yaw as usize]; + let yaw_bias = self.state[StateIndex::YawBias as usize]; + let vx = self.state[StateIndex::Vx as usize]; + + F[(StateIndex::X as usize, StateIndex::Yaw as usize)] = -vx * sin(yaw + yaw_bias) * dt; + F[(StateIndex::X as usize, StateIndex::YawBias as usize)] = -vx * sin(yaw + yaw_bias) * dt; + F[(StateIndex::X as usize, StateIndex::Vx as usize)] = cos(yaw + yaw_bias) * dt; + + F[(StateIndex::Y as usize, StateIndex::Yaw as usize)] = vx * cos(yaw + yaw_bias) * dt; + F[(StateIndex::Y as usize, StateIndex::YawBias as usize)] = vx * cos(yaw + yaw_bias) * dt; + F[(StateIndex::Y as usize, StateIndex::Vx as usize)] = sin(yaw + yaw_bias) * dt; + + F[(StateIndex::Yaw as usize, StateIndex::Wz as usize)] = dt; + + F + } + + fn process_noise_covariance(&self, dt: f64) -> Matrix6 { + let mut Q = Matrix6::zeros(); + + Q[(StateIndex::Vx as usize, StateIndex::Vx as usize)] = + self.params.proc_stddev_vx_c * self.params.proc_stddev_vx_c * dt * dt; + Q[(StateIndex::Wz as usize, StateIndex::Wz as usize)] = + self.params.proc_stddev_wz_c * self.params.proc_stddev_wz_c * dt * dt; + Q[(StateIndex::Yaw as usize, StateIndex::Yaw as usize)] = + self.params.proc_stddev_yaw_c * self.params.proc_stddev_yaw_c * dt * dt; + + Q[(StateIndex::X as usize, StateIndex::X as usize)] = 0.0; + Q[(StateIndex::Y as usize, StateIndex::Y as usize)] = 0.0; + Q[(StateIndex::YawBias as usize, StateIndex::YawBias as usize)] = 0.0; + + Q + } + + pub fn predict(&mut self, dt: f64) { + self.state = self.predict_next_state(dt); + let F = self.create_state_transition_matrix(dt); + let Q = self.process_noise_covariance(dt); + self.covariance = F * self.covariance * F.transpose() + Q; + self.accumulate_delay_time(dt); + } + + pub fn predict_with_delay(&mut self, dt: f64) { + self.predict(dt); + } + + pub fn predict_only(&mut self, dt: f64) { + self.predict(dt); + } + + pub fn get_current_pose(&self, get_biased_yaw: bool) -> Pose { + let z = self.z_filter.get_x(); + let roll = self.roll_filter.get_x(); + let pitch = self.pitch_filter.get_x(); + + let x = self.state[StateIndex::X as usize]; + let y = self.state[StateIndex::Y as usize]; + let biased_yaw = self.state[StateIndex::Yaw as usize]; + let yaw_bias = self.state[StateIndex::YawBias as usize]; + + let yaw = if get_biased_yaw { + biased_yaw + } else { + biased_yaw + yaw_bias + }; + + Pose { + position: Point3D { x, y, z }, + orientation: self.rpy_to_quaternion(roll, pitch, yaw), + } + } + + pub fn get_current_twist(&self) -> Twist { + let vx = self.state[StateIndex::Vx as usize]; + let wz = self.state[StateIndex::Wz as usize]; + + Twist { + linear: Vector3::new(vx, 0.0, 0.0), + angular: Vector3::new(0.0, 0.0, wz), + } + } + + pub fn get_yaw_bias(&self) -> f64 { + self.state[StateIndex::YawBias as usize] + } + + pub fn get_current_pose_with_covariance(&self) -> PoseWithCovariance { + let pose = self.get_current_pose(false); + let pose_covariance = self.get_current_pose_covariance(); + PoseWithCovariance { + pose, + covariance: pose_covariance, + } + } + + pub fn get_current_pose_covariance(&self) -> [f64; 36] { + let mut cov = [0.0; 36]; + + for i in 0..6 { + for j in 0..6 { + cov[i * 6 + j] = self.covariance[(i, j)]; + } + } + + cov[14] = self.z_filter.get_var(); + cov[21] = self.roll_filter.get_var(); + cov[28] = self.pitch_filter.get_var(); + + cov + } + + pub fn get_current_twist_covariance(&self) -> [f64; 36] { + let mut cov = [0.0; 36]; + + cov[0] = self.covariance[(StateIndex::Vx as usize, StateIndex::Vx as usize)]; + cov[35] = self.covariance[(StateIndex::Wz as usize, StateIndex::Wz as usize)]; + + cov + } + + pub fn update_velocity(&mut self, vx_measurement: f64, wz_measurement: f64) { + if self.is_mrm_mode { + return; + } + + let vx_obs_var = 1.0; + let wz_obs_var = 0.1; + + let vx_var = self.covariance[(StateIndex::Vx as usize, StateIndex::Vx as usize)]; + let vx_gain = vx_var / (vx_var + vx_obs_var); + self.state[StateIndex::Vx as usize] = self.state[StateIndex::Vx as usize] + + vx_gain * (vx_measurement - self.state[StateIndex::Vx as usize]); + self.covariance[(StateIndex::Vx as usize, StateIndex::Vx as usize)] = + (1.0 - vx_gain) * vx_var; + + let wz_var = self.covariance[(StateIndex::Wz as usize, StateIndex::Wz as usize)]; + let wz_gain = wz_var / (wz_var + wz_obs_var); + self.state[StateIndex::Wz as usize] = self.state[StateIndex::Wz as usize] + + wz_gain * (wz_measurement - self.state[StateIndex::Wz as usize]); + self.covariance[(StateIndex::Wz as usize, StateIndex::Wz as usize)] = + (1.0 - wz_gain) * wz_var; + } + + pub fn set_mrm_mode(&mut self, is_mrm: bool) { + self.is_mrm_mode = is_mrm; + } + + pub fn is_mrm(&self) -> bool { + self.is_mrm_mode + } + + pub fn accumulate_delay_time(&mut self, dt: f64) { + let len = self.accumulated_delay_times.len(); + if len == 0 { + return; + } + + let last_time = self.accumulated_delay_times[len - 1]; + let new_time = last_time + dt; + + for i in 0..len - 1 { + self.accumulated_delay_times[i] = self.accumulated_delay_times[i + 1]; + } + self.accumulated_delay_times[len - 1] = new_time; + } + + pub fn find_closest_delay_time_index(&self, target_value: f64) -> usize { + let len = self.accumulated_delay_times.len(); + if len == 0 { + return 0; + } + + if target_value > self.accumulated_delay_times[len - 1] { + return len; + } + + let mut closest_index = 0; + let mut min_diff = f64::MAX; + + for i in 0..len { + let time = self.accumulated_delay_times[i]; + let diff = (target_value - time).abs(); + if diff < min_diff { + min_diff = diff; + closest_index = i; + } + } + + closest_index + } + + fn quaternion_to_yaw(&self, q: Quaternion) -> f64 { + atan2( + 2.0 * (q.w * q.z + q.x * q.y), + 1.0 - 2.0 * (q.y * q.y + q.z * q.z), + ) + } + + fn rpy_to_quaternion(&self, roll: f64, pitch: f64, yaw: f64) -> Quaternion { + let cy = cos(yaw * 0.5); + let sy = sin(yaw * 0.5); + let cp = cos(pitch * 0.5); + let sp = sin(pitch * 0.5); + let cr = cos(roll * 0.5); + let sr = sin(roll * 0.5); + + Quaternion { + w: cr * cp * cy + sr * sp * sy, + x: sr * cp * cy - cr * sp * sy, + y: cr * sp * cy + sr * cp * sy, + z: cr * cp * sy - sr * sp * cy, + } + } +} + +pub fn get_or_initialize_default_module() -> &'static mut EKFModule { + let existing = EKF_MODULE_INSTANCE.load(AtomicOrdering::Acquire); + if !existing.is_null() { + return unsafe { &mut *existing }; + } + + let boxed = alloc::boxed::Box::new(EKFModule::new(EKFParameters::default())); + let ptr = alloc::boxed::Box::into_raw(boxed); + + match EKF_MODULE_INSTANCE.compare_exchange( + null_mut(), + ptr, + AtomicOrdering::AcqRel, + AtomicOrdering::Acquire, + ) { + Ok(_) => unsafe { &mut *ptr }, + Err(existing_ptr) => unsafe { + let _ = alloc::boxed::Box::from_raw(ptr); + &mut *existing_ptr + }, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use core::f64::consts::PI; + use nalgebra::{Matrix6, Vector6}; + + #[test] + fn predict_next_state_matches_formula() { + let params = EKFParameters::default(); + let mut ekf = EKFModule::new(params); + + let x_curr = Vector6::new(2.0, 3.0, PI / 2.0, PI / 4.0, 10.0, 2.0 * PI / 3.0); + + ekf.state = x_curr.clone(); + + let dt = 0.5; + let x_next = ekf.predict_next_state(dt); + + let tol = 1e-10; + assert!((x_next[0] - (2.0 + 10.0 * (PI / 2.0 + PI / 4.0).cos() * dt)).abs() < tol); + assert!((x_next[1] - (3.0 + 10.0 * (PI / 2.0 + PI / 4.0).sin() * dt)).abs() < tol); + let yaw_next = PI / 2.0 + (2.0 * PI / 3.0) * dt; + let expected_yaw = yaw_next.sin().atan2(yaw_next.cos()); + assert!((x_next[2] - expected_yaw).abs() < 1e-6); + assert!((x_next[3] - x_curr[3]).abs() < tol); + assert!((x_next[4] - x_curr[4]).abs() < tol); + assert!((x_next[5] - x_curr[5]).abs() < tol); + } + + #[test] + fn create_state_transition_matrix_numeric_approximation() { + let params = EKFParameters::default(); + let mut ekf = EKFModule::new(params); + + // check around zero + let dt = 0.1; + let dx = Vector6::from_element(0.1); + let x = Vector6::zeros(); + + ekf.state = x.clone(); + let a = ekf.create_state_transition_matrix(dt); + + ekf.state = x.clone() + dx.clone(); + let x1 = ekf.predict_next_state(dt); + ekf.state = x.clone(); + let x0 = ekf.predict_next_state(dt); + let df = x1 - x0; + + { + let mut s = 0.0; + let v = df - a * dx; + for i in 0..6 { + let val = v[i]; + s += val * val; + } + assert!(s.sqrt() < 2e-3); + } + + // check around a non-zero state + let dx = Vector6::from_element(0.1); + let x = Vector6::new(0.1, 0.2, 0.1, 0.4, 0.1, 0.3); + + ekf.state = x.clone(); + let a = ekf.create_state_transition_matrix(dt); + + ekf.state = x.clone() + dx.clone(); + let x1 = ekf.predict_next_state(dt); + ekf.state = x.clone(); + let x0 = ekf.predict_next_state(dt); + let df = x1 - x0; + + { + let mut s = 0.0; + let v = df - a * dx; + for i in 0..6 { + let val = v[i]; + s += val * val; + } + assert!(s.sqrt() < 5e-3); + } + } + + #[test] + fn process_noise_covariance_values() { + let mut params = EKFParameters::default(); + params.proc_stddev_yaw_c = 1.0; + params.proc_stddev_vx_c = 2.0; + params.proc_stddev_wz_c = 3.0; + + let ekf = EKFModule::new(params); + + let q = ekf.process_noise_covariance(1.0); + + // indices: yaw = 2, vx = 4, wz = 5 + assert!((q[(2, 2)] - 1.0_f64.powi(2)).abs() < 1e-12); + assert!((q[(4, 4)] - 2.0_f64.powi(2)).abs() < 1e-12); + assert!((q[(5, 5)] - 3.0_f64.powi(2)).abs() < 1e-12); + + // zero case + let mut params = EKFParameters::default(); + params.proc_stddev_yaw_c = 0.0; + params.proc_stddev_vx_c = 0.0; + params.proc_stddev_wz_c = 0.0; + let ekf2 = EKFModule::new(params); + let q2 = ekf2.process_noise_covariance(1.0); + { + let mut s = 0.0; + for i in 0..6 { + for j in 0..6 { + let val = q2[(i, j)]; + s += val * val; + } + } + assert!(s == 0.0); + } + } + + #[test] + fn pose_and_twist_covariance_mapping() { + let params = EKFParameters::default(); + let mut ekf = EKFModule::new(params); + + // prepare a covariance matrix with top-left 3x3 = 1..9 + let mut p = Matrix6::::zeros(); + p[(0, 0)] = 1.0; + p[(0, 1)] = 2.0; + p[(0, 2)] = 3.0; + p[(1, 0)] = 4.0; + p[(1, 1)] = 5.0; + p[(1, 2)] = 6.0; + p[(2, 0)] = 7.0; + p[(2, 1)] = 8.0; + p[(2, 2)] = 9.0; + + ekf.covariance = p; + + // override the filter variances so those indices are replaced + ekf.z_filter.var = 100.0; + ekf.roll_filter.var = 200.0; + ekf.pitch_filter.var = 300.0; + + let cov = ekf.get_current_pose_covariance(); + + // check a few mapped entries according to get_current_pose_covariance implementation + assert_eq!(cov[0], 1.0); + assert_eq!(cov[1], 2.0); + assert_eq!(cov[2], 3.0); + assert_eq!(cov[6], 4.0); + assert_eq!(cov[7], 5.0); + assert_eq!(cov[8], 6.0); + assert_eq!(cov[12], 7.0); + assert_eq!(cov[13], 8.0); + // index 14 is overwritten by z_filter.var + assert_eq!(cov[14], 100.0); + + // twist covariance mapping (Vx -> index 0, Wz -> index 35) + let mut p2 = Matrix6::::zeros(); + p2[(4, 4)] = 1.0; + p2[(4, 5)] = 2.0; + p2[(5, 4)] = 3.0; + p2[(5, 5)] = 4.0; + ekf.covariance = p2; + + let tcov = ekf.get_current_twist_covariance(); + assert_eq!(tcov[0], 1.0); + assert_eq!(tcov[35], 4.0); + } +} diff --git a/applications/autoware/gyro_odometer/Cargo.toml b/applications/autoware/gyro_odometer/Cargo.toml new file mode 100644 index 000000000..be11aaefb --- /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} \ No newline at end of file diff --git a/applications/autoware/gyro_odometer/src/lib.rs b/applications/autoware/gyro_odometer/src/lib.rs new file mode 100644 index 000000000..64649d37e --- /dev/null +++ b/applications/autoware/gyro_odometer/src/lib.rs @@ -0,0 +1,439 @@ +// Ported from the following versions of the original C++ code: +// core/autoware_core: +// type: git +// url: https://github.com/autowarefoundation/autoware_core.git +// 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, Quaternion, 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]; + } + 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 { + let _ = (from_frame, to_frame); + 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 { + self.concat_gyro_and_odometer(current_time) + .unwrap_or_default() + } + + 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, + }, + orientation: Quaternion { + x: 0.0, + y: 0.0, + z: 0.0, + w: 1.0, + }, + 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_vehicle_velocity_conversion() { + 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); + + // Verify the sample twist structure directly + assert_eq!(sample_twist.header.timestamp, 123456789); + assert_eq!(sample_twist.twist.twist.linear.x, 1.0); + assert_eq!(sample_twist.twist.twist.linear.y, 0.0); + assert_eq!(sample_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_corrector/src/lib.rs b/applications/autoware/imu_corrector/src/lib.rs index 7e573e213..2953d1cea 100644 --- a/applications/autoware/imu_corrector/src/lib.rs +++ b/applications/autoware/imu_corrector/src/lib.rs @@ -274,7 +274,7 @@ impl ImuCorrector { pub fn correct_imu_with_dynamic_tf(&self, imu_msg: &ImuMsg) -> Option { let transform = self .transform_listener - .get_latest_transform(&imu_msg.header.frame_id, self.config.output_frame)?; + .get_latest_transform(imu_msg.header.frame_id, self.config.output_frame)?; let corrected_with_cov = self.correct_imu_with_covariance(imu_msg, Some(&transform)); Some(corrected_with_cov.to_imu_msg()) @@ -286,7 +286,7 @@ impl ImuCorrector { ) -> Option { let transform = self .transform_listener - .get_latest_transform(&imu_msg.header.frame_id, self.config.output_frame)?; + .get_latest_transform(imu_msg.header.frame_id, self.config.output_frame)?; Some(self.correct_imu_with_covariance(imu_msg, Some(&transform))) } 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/src/lib.rs b/applications/autoware/src/lib.rs index 438b2d083..38928916e 100644 --- a/applications/autoware/src/lib.rs +++ b/applications/autoware/src/lib.rs @@ -1,4 +1,660 @@ #![no_std] +#![allow(static_mut_refs)] extern crate alloc; -pub async fn run() {} +use alloc::{borrow::Cow, format, string::String, vec, vec::Vec}; +use awkernel_async_lib::dag::{create_dag, finish_create_dags}; +use awkernel_async_lib::net::IpAddr; +use awkernel_async_lib::scheduler::SchedulerType; +use awkernel_lib::delay::wait_microsec; +use awkernel_lib::sync::mutex::{MCSNode, Mutex}; +use core::net::Ipv4Addr; +use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use core::time::Duration; +use csv_core::{ReadRecordResult, Reader}; + +pub use common_types::Header; +use core::slice; +use ekf_localizer::{ + get_or_initialize_default_module, EKFOdometry, Point3D, Pose, PoseWithCovariance, Quaternion, +}; +use imu_corrector::{ImuCorrector, ImuWithCovariance}; +use imu_driver::{build_imu_msg_from_csv_row, ImuCsvRow, ImuMsg, TamagawaImuParser}; +use vehicle_velocity_converter::{ + build_velocity_report_from_csv_row, reactor_helpers, Twist, TwistWithCovariance, + TwistWithCovarianceStamped, VehicleVelocityConverter, VelocityCsvRow, +}; + +const LOG_ENABLE: bool = false; + +const INTERFACE_ID: u64 = 0; +const INTERFACE_ADDR: Ipv4Addr = Ipv4Addr::new(10, 0, 2, 64); +const UDP_TCP_DST_ADDR: Ipv4Addr = Ipv4Addr::new(10, 0, 2, 2); +const UDP_DST_PORT: u16 = 26099; + +static mut LATEST_JSON_DATA: Option = None; +static JSON_DATA_READY: AtomicBool = AtomicBool::new(false); +static JSON_DATA_LENGTH: AtomicUsize = AtomicUsize::new(0); + +include!(concat!(env!("OUT_DIR"), "/csv_data.rs")); + +static mut IMU_CSV_DATA: Option> = None; +static mut VELOCITY_CSV_DATA: Option> = None; +static IMU_CSV_COUNT: Mutex = Mutex::new(0); +static VELOCITY_CSV_COUNT: Mutex = Mutex::new(0); + +pub async fn run() { + wait_microsec(1000000); + + if let Err(e) = initialize_csv_data() { + log::warn!("Failed to initialize CSV data: {}", e); + } + + log::info!("Starting Autoware test application with simplified TCP networking"); + + let dag = create_dag(); + let _dag_id = dag.get_id(); + + dag.register_periodic_reactor::<_, (i32, i32, i32)>( + "start_dummy_data".into(), + move || -> (i32, i32, i32) { (1, 2, 3) }, + vec![ + Cow::from("start_imu"), + Cow::from("start_vel"), + Cow::from("start_pose"), + ], + SchedulerType::GEDF(5), + Duration::from_millis(50), + ) + .await; + + dag.register_reactor::<_, (i32,), (ImuMsg,)>( + "imu_driver".into(), + move |(_start_msg,): (i32,)| -> (ImuMsg,) { + let mut node = MCSNode::new(); + let mut count_guard = IMU_CSV_COUNT.lock(&mut node); + let count = *count_guard; + let data = unsafe { IMU_CSV_DATA.as_ref() }; + let awkernel_timestamp = get_awkernel_uptime_timestamp(); + + let imu_msg = if let Some(csv_data) = data { + if csv_data.is_empty() { + // Fallback: generate dummy IMU data + let mut parser = TamagawaImuParser::new("imu_link"); + let static_dummy_data = parser.generate_static_dummy_data(awkernel_timestamp); + parser + .parse_binary_data(&static_dummy_data, awkernel_timestamp) + .unwrap_or_default() + } else { + let idx = count % csv_data.len(); + let row = &csv_data[idx]; + build_imu_msg_from_csv_row(row, "imu_link", awkernel_timestamp) + } + } else { + // Fallback: generate dummy IMU data if data is not initialized + let mut parser = TamagawaImuParser::new("imu_link"); + let static_dummy_data = parser.generate_static_dummy_data(awkernel_timestamp); + parser + .parse_binary_data(&static_dummy_data, awkernel_timestamp) + .unwrap_or_default() + }; + + *count_guard += 1; + if *count_guard >= 5700 { + *count_guard = 0; + log::info!("rust_e2e_app: finish csv for IMU"); + loop { + wait_microsec(1000); + } + } + + if LOG_ENABLE { + log::debug!( + "IMU data in imu_driver_node,num={}, timestamp={}", + count, + imu_msg.header.timestamp + ); + } + + (imu_msg,) + }, + vec![Cow::from("start_imu")], + vec![Cow::from("imu_data")], + SchedulerType::GEDF(5), + ) + .await; + + dag.register_reactor::<_, (i32,), (TwistWithCovarianceStamped,)>( + "vehicle_velocity_converter".into(), + move |(_start_msg,): (i32,)| -> (TwistWithCovarianceStamped,) { + let converter = VehicleVelocityConverter::default(); + + let mut node = MCSNode::new(); + let mut count_guard = VELOCITY_CSV_COUNT.lock(&mut node); + let count = *count_guard; + let data = unsafe { VELOCITY_CSV_DATA.as_ref() }; + let awkernel_timestamp = get_awkernel_uptime_timestamp(); + + let twist_msg = if let Some(csv_data) = data { + if csv_data.is_empty() { + // Fallback: generate dummy velocity report + let dummy_report = vehicle_velocity_converter::VelocityReport { + header: common_types::Header { + frame_id: "base_link", + timestamp: awkernel_timestamp, + }, + longitudinal_velocity: 1.0, + lateral_velocity: 0.0, + heading_rate: 0.0, + }; + converter.convert_velocity_report(&dummy_report) + } else { + let idx = count % csv_data.len(); + let row = &csv_data[idx]; + let velocity_report = + build_velocity_report_from_csv_row(row, "base_link", awkernel_timestamp); + converter.convert_velocity_report(&velocity_report) + } + } else { + // Fallback: generate dummy velocity report if data is not initialized + let dummy_report = vehicle_velocity_converter::VelocityReport { + header: common_types::Header { + frame_id: "base_link", + timestamp: awkernel_timestamp, + }, + longitudinal_velocity: 1.0, + lateral_velocity: 0.0, + heading_rate: 0.0, + }; + converter.convert_velocity_report(&dummy_report) + }; + + *count_guard += 1; + if *count_guard >= 5700 { + *count_guard = 0; + log::info!("rust_e2e_app: finish csv for Velocity"); + loop { + wait_microsec(1000); + } + } + + if LOG_ENABLE { + log::debug!("Vehicle velocity converter: Converted velocity report to twist - linear.x={:.3}, angular.z={:.3}, awkernel_timestamp={}", + twist_msg.twist.twist.linear.x, + twist_msg.twist.twist.angular.z, + twist_msg.header.timestamp + ); + } + + (twist_msg,) + }, + vec![Cow::from("start_vel")], + vec![Cow::from("velocity_twist")], + SchedulerType::GEDF(5), + ) + .await; + + dag.register_reactor::<_, (ImuMsg,), (ImuWithCovariance,)>( + "imu_corrector".into(), + |(imu_msg,): (ImuMsg,)| -> (ImuWithCovariance,) { + let corrector = ImuCorrector::new(); + let corrected = corrector.correct_imu_with_covariance(&imu_msg, None); + (corrected,) + }, + vec![Cow::from("imu_data")], + vec![Cow::from("corrected_imu_data")], + SchedulerType::GEDF(5), + ) + .await; + + dag.register_reactor::<_, ( + ImuWithCovariance, + TwistWithCovarianceStamped, + ), (TwistWithCovarianceStamped,)>( + "gyro_odometer".into(), + |(imu_with_cov, vehicle_twist): ( + ImuWithCovariance, + TwistWithCovarianceStamped, + )| + -> (TwistWithCovarianceStamped,) { + let current_timestamp = imu_with_cov.header.timestamp; + let current_time = get_awkernel_uptime_timestamp(); + + let gyro_odometer = match gyro_odometer::get_or_initialize() { + Ok(core) => core, + Err(_) => { + return (reactor_helpers::create_empty_twist(current_timestamp),); + } + }; + + gyro_odometer.add_vehicle_twist(vehicle_twist); + gyro_odometer.add_imu(imu_with_cov); + + match gyro_odometer.process_and_get_result(current_time) { + Some(result) => (gyro_odometer.process_result(result),), + None => (reactor_helpers::create_empty_twist(current_timestamp),), + } + }, + vec![Cow::from("corrected_imu_data"), Cow::from("velocity_twist")], + vec![Cow::from("twist_with_covariance")], + SchedulerType::GEDF(5), + ) + .await; + + dag.register_reactor::<_, (i32,), (Pose,)>( + "pose_dummy_generator".into(), + move |(_start_msg,): (i32,)| -> (Pose,) { + let x = 0.0; + let y = 0.0; + let z = 0.0; + + let pose = Pose { + position: Point3D { x, y, z }, + orientation: Quaternion { + x: 0.0, + y: 0.0, + z: 0.0, + w: 1.0, + }, + }; + + (pose,) + }, + vec![Cow::from("start_pose")], + vec![Cow::from("dummy_pose")], + SchedulerType::GEDF(5), + ) + .await; + + dag.register_reactor::<_, (Pose, TwistWithCovarianceStamped), (Pose, EKFOdometry)>( + "ekf_localizer".into(), + |(pose, twist): (Pose, TwistWithCovarianceStamped)| -> (Pose, EKFOdometry) { + let ekf = get_or_initialize_default_module(); + + static mut INITIALIZED: bool = false; + unsafe { + if !INITIALIZED { + ekf.initialize(pose); + INITIALIZED = true; + } + } + + // Use a fixed 50ms timestep to match the Autoware publisher cadence. + const FIXED_DT: f64 = 0.05; + let dt = FIXED_DT; + + if dt > 0.0 { + ekf.predict(dt); + } + + let vx = twist.twist.twist.linear.x; + let wz = twist.twist.twist.angular.z; + ekf.update_velocity(vx, wz); + + let ekf_pose = ekf.get_current_pose(false); + + let pose_covariance = ekf.get_current_pose_covariance(); + let twist_covariance = ekf.get_current_twist_covariance(); + + let ekf_twist = ekf.get_current_twist(); + + let odometry = EKFOdometry { + header: common_types::Header { + frame_id: "map", + timestamp: twist.header.timestamp, + }, + child_frame_id: "base_link", + pose: PoseWithCovariance { + pose: ekf_pose, + covariance: pose_covariance, + }, + twist: TwistWithCovariance { + twist: Twist { + linear: common_types::Vector3::new( + ekf_twist.linear.x, + ekf_twist.linear.y, + ekf_twist.linear.z, + ), + angular: common_types::Vector3::new( + ekf_twist.angular.x, + ekf_twist.angular.y, + ekf_twist.angular.z, + ), + }, + covariance: twist_covariance, + }, + }; + + (ekf_pose, odometry) + }, + vec![Cow::from("dummy_pose"), Cow::from("twist_with_covariance")], + vec![Cow::from("estimated_pose"), Cow::from("ekf_odometry")], + SchedulerType::GEDF(5), + ) + .await; + + dag.register_sink_reactor::<_, (Pose, EKFOdometry)>( + "ekf_sink".into(), + move |(_pose, ekf_odom): (Pose, EKFOdometry)| { + + let json_data = format!( + r#"{{"header":{{"frame_id":"{}","timestamp":{}}},"child_frame_id":"{}","pose":{{"pose":{{"position":{{"x":{:.6},"y":{:.6},"z":{:.6}}},"orientation":{{"x":{:.6},"y":{:.6},"z":{:.6},"w":{:.6}}}}},"covariance":[{}]}},"twist":{{"twist":{{"linear":{{"x":{:.6},"y":{:.6},"z":{:.6}}},"angular":{{"x":{:.6},"y":{:.6},"z":{:.6}}}}},"covariance":[{}]}}}}"#, + ekf_odom.header.frame_id, + ekf_odom.header.timestamp, + ekf_odom.child_frame_id, + ekf_odom.pose.pose.position.x, + ekf_odom.pose.pose.position.y, + ekf_odom.pose.pose.position.z, + ekf_odom.pose.pose.orientation.x, + ekf_odom.pose.pose.orientation.y, + ekf_odom.pose.pose.orientation.z, + ekf_odom.pose.pose.orientation.w, + ekf_odom.pose.covariance.iter().map(|&x| format!("{:.6}", x)).collect::>().join(","), + ekf_odom.twist.twist.linear.x, + ekf_odom.twist.twist.linear.y, + ekf_odom.twist.twist.linear.z, + ekf_odom.twist.twist.angular.x, + ekf_odom.twist.twist.angular.y, + ekf_odom.twist.twist.angular.z, + ekf_odom.twist.covariance.iter().map(|&x| format!("{:.6}", x)).collect::>().join(",") + ); + + save_json_data_to_global(json_data); + }, + vec![Cow::from("estimated_pose"), Cow::from("ekf_odometry")], + SchedulerType::GEDF(5), + Duration::from_millis(50), + ) + .await; + + let result = finish_create_dags(slice::from_ref(&dag)).await; + + match result { + Ok(_) => { + log::info!("Autoware test application DAGs created successfully"); + } + Err(errors) => { + log::error!("Failed to create Autoware test application DAGs"); + for error in errors { + log::error!("- {error}"); + } + } + } + + log::info!("Autoware test application DAG completed"); + + log::info!("=== Network test start ==="); + log::info!("Interface ID: {}", INTERFACE_ID); + log::info!("Interface IP: {}", INTERFACE_ADDR); + log::info!("Destination IP: {}", UDP_TCP_DST_ADDR); + awkernel_lib::net::add_ipv4_addr(INTERFACE_ID, INTERFACE_ADDR, 24); + log::info!( + "Configured IPv4 address {} on interface {}", + INTERFACE_ADDR, + INTERFACE_ID + ); + + log::info!("Waiting for network stack initialization..."); + awkernel_async_lib::sleep(Duration::from_secs(2)).await; + + log::info!("Starting periodic UDP sender task"); + start_periodic_udp_sender().await; + + log::info!("Waiting for JSON data to become ready..."); + let mut wait_count = 0; + const MAX_WAIT_COUNT: u32 = 3; + + while !JSON_DATA_READY.load(Ordering::Relaxed) && wait_count < MAX_WAIT_COUNT { + awkernel_async_lib::sleep(Duration::from_secs(1)).await; + wait_count += 1; + } + + if JSON_DATA_READY.load(Ordering::Relaxed) { + } else { + log::warn!("JSON data was not ready. Periodic UDP sender task remains waiting"); + } + + log::info!("Autoware test application completed"); +} + +fn initialize_csv_data() -> Result<(), &'static str> { + unsafe { + if IMU_CSV_DATA.is_none() { + let imu_data = parse_imu_csv(IMU_CSV_DATA_STR)?; + log::info!("Loaded IMU CSV data: {} rows", imu_data.len()); + IMU_CSV_DATA = Some(imu_data); + } + + if VELOCITY_CSV_DATA.is_none() { + let velocity_data = parse_velocity_csv(VELOCITY_CSV_DATA_STR)?; + log::info!("Loaded velocity CSV data: {} rows", velocity_data.len()); + VELOCITY_CSV_DATA = Some(velocity_data); + } + } + + Ok(()) +} + +fn parse_imu_csv(csv: &str) -> Result, &'static str> { + let mut rows = Vec::new(); + + parse_csv_records(csv, |fields| { + if fields.len() < 12 { + return Err("IMU CSV has insufficient columns"); + } + + let timestamp = parse_timestamp(fields[0], fields[1])?; + let angular_velocity = common_types::Vector3::new( + parse_f64(fields[6])?, + parse_f64(fields[7])?, + parse_f64(fields[8])?, + ); + let linear_acceleration = common_types::Vector3::new( + parse_f64(fields[9])?, + parse_f64(fields[10])?, + parse_f64(fields[11])?, + ); + + rows.push(ImuCsvRow { + timestamp, + angular_velocity, + linear_acceleration, + }); + Ok(()) + })?; + + Ok(rows) +} + +fn parse_velocity_csv(csv: &str) -> Result, &'static str> { + let mut rows = Vec::new(); + + parse_csv_records(csv, |fields| { + if fields.len() < 5 { + return Err("Velocity CSV has insufficient columns"); + } + + let timestamp = parse_timestamp(fields[0], fields[1])?; + let longitudinal_velocity = parse_f64(fields[2])?; + let lateral_velocity = parse_f64(fields[3])?; + let heading_rate = parse_f64(fields[4])?; + + rows.push(VelocityCsvRow { + timestamp, + longitudinal_velocity, + lateral_velocity, + heading_rate, + }); + Ok(()) + })?; + + Ok(rows) +} + +fn parse_csv_records(csv: &str, mut on_record: F) -> Result<(), &'static str> +where + F: FnMut(&[&str]) -> Result<(), &'static str>, +{ + let mut reader = Reader::new(); + let mut input = csv.as_bytes(); + let mut output = vec![0u8; 4096]; + let mut ends = vec![0usize; 32]; + let mut header_skipped = false; + + loop { + let (result, in_read, _out_written, num_fields) = + reader.read_record(input, &mut output, &mut ends); + input = &input[in_read..]; + + if matches!(result, ReadRecordResult::OutputFull) { + return Err("CSV output buffer is too small"); + } + + if num_fields == 0 { + if matches!(result, ReadRecordResult::InputEmpty | ReadRecordResult::End) { + break; + } + continue; + } + + let mut fields: Vec<&str> = Vec::with_capacity(num_fields); + let mut start = 0usize; + for &end in ends.iter().take(num_fields) { + let slice = &output[start..end]; + let field = core::str::from_utf8(slice).map_err(|_| "Failed to decode CSV UTF-8")?; + fields.push(field); + start = end; + } + + if !header_skipped { + header_skipped = true; + } else { + on_record(&fields)?; + } + + if matches!(result, ReadRecordResult::End) { + break; + } + } + + Ok(()) +} + +fn parse_timestamp(sec: &str, nsec: &str) -> Result { + let sec_val = parse_u64(sec)?; + let nsec_val = parse_u64(nsec)?; + let ts = sec_val + .checked_mul(1_000_000_000) + .and_then(|v| v.checked_add(nsec_val)) + .ok_or("Timestamp calculation overflowed")?; + Ok(ts) +} + +fn parse_u64(field: &str) -> Result { + let trimmed = field.trim(); + if trimmed.is_empty() { + return Ok(0); + } + trimmed.parse::().map_err(|_| "Failed to parse u64") +} + +fn parse_f64(field: &str) -> Result { + let trimmed = field.trim(); + if trimmed.is_empty() { + return Ok(0.0); + } + trimmed.parse::().map_err(|_| "Failed to parse f64") +} + +fn get_awkernel_uptime_timestamp() -> u64 { + let uptime_nanos = awkernel_lib::delay::uptime_nano(); + if uptime_nanos > u64::MAX as u128 { + u64::MAX + } else { + uptime_nanos as u64 + } +} + +pub async fn start_periodic_udp_sender() { + awkernel_async_lib::spawn( + "periodic_udp_sender".into(), + periodic_udp_sender_task(), + awkernel_async_lib::scheduler::SchedulerType::GEDF(5), + ) + .await; +} + +async fn periodic_udp_sender_task() { + let socket_result = awkernel_async_lib::net::udp::UdpSocket::bind_on_interface( + INTERFACE_ID, + &Default::default(), + ); + + let mut socket = match socket_result { + Ok(socket) => socket, + Err(e) => { + log::error!( + "Periodic UDP sender task: failed to create UDP socket: {:?}", + e + ); + return; + } + }; + + let dst_addr = IpAddr::new_v4(UDP_TCP_DST_ADDR); + let mut counter = 0; + + loop { + if !JSON_DATA_READY.load(Ordering::Relaxed) { + awkernel_async_lib::sleep(Duration::from_secs(1)).await; + continue; + } + + let json_data = unsafe { LATEST_JSON_DATA.clone() }; + + if let Some(data) = json_data { + match socket.send(data.as_bytes(), &dst_addr, UDP_DST_PORT).await { + Ok(_) => { + counter += 1; + log::info!( + "Periodic UDP sender task: send success #{} ({} bytes)", + counter, + data.len() + ); + + let mut buf = [0u8; 1024]; + if let Some(Ok((n, src_addr, src_port))) = awkernel_async_lib::timeout( + Duration::from_millis(100), + socket.recv(&mut buf), + ) + .await + { + if let Ok(response) = core::str::from_utf8(&buf[..n]) { + log::debug!( + "Periodic UDP sender task: response received: {}:{} - {}", + src_addr.get_addr(), + src_port, + response + ); + } + } + } + Err(e) => { + log::warn!("Periodic UDP sender task: send failed: {:?}", e); + } + } + } else { + log::warn!("Periodic UDP sender task: failed to get JSON data"); + } + + awkernel_async_lib::sleep(Duration::from_millis(5)).await; + } +} + +fn save_json_data_to_global(json_data: String) { + unsafe { + LATEST_JSON_DATA = Some(json_data.clone()); + } + JSON_DATA_READY.store(true, Ordering::Relaxed); + JSON_DATA_LENGTH.store(json_data.len(), Ordering::Relaxed); +} 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..d3d65556b 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 { @@ -150,12 +139,12 @@ impl VehicleVelocityConverter { fn create_covariance_matrix(&self) -> [f64; 36] { let mut covariance = [0.0; 36]; - covariance[0 + 0 * 6] = self.stddev_vx * self.stddev_vx; - covariance[1 + 1 * 6] = 10000.0; - covariance[2 + 2 * 6] = 10000.0; - covariance[3 + 3 * 6] = 10000.0; - covariance[4 + 4 * 6] = 10000.0; - covariance[5 + 5 * 6] = self.stddev_wz * self.stddev_wz; + covariance[0] = self.stddev_vx * self.stddev_vx; + covariance[7] = 10000.0; + covariance[14] = 10000.0; + covariance[21] = 10000.0; + covariance[28] = 10000.0; + covariance[35] = self.stddev_wz * self.stddev_wz; covariance } diff --git a/userland/Cargo.toml b/userland/Cargo.toml index 6e5b64012..b622ead12 100644 --- a/userland/Cargo.toml +++ b/userland/Cargo.toml @@ -83,7 +83,7 @@ path = "../applications/tests/test_partitioned_edf" optional = true [features] -default = [] +default = ["autoware"] perf = ["awkernel_services/perf"] # Evaluation applications