Skip to content

Commit

Permalink
[no ci] refactor 2
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlettman committed Aug 13, 2024
1 parent fa6d761 commit e8d637b
Show file tree
Hide file tree
Showing 31 changed files with 1,046 additions and 334 deletions.
46 changes: 23 additions & 23 deletions src/metadata/config_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,21 @@ pub struct ConfigParams {
#[serde(default = "ConfigParams::default_signal_multiplier")]
signal_multiplier: f64,

/// The configuration of LIDAR packets.
///
/// For additional information, [`udp_profile_lidar`].
///
/// [`udp_profile_lidar`]: https://static.ouster.dev/sensor-docs/image_route1/image_route2/common_sections/API/sensor_configuration_description.html#udp-profile-lidar
#[serde(rename = "udp_profile_lidar", default = "ConfigParams::default_lidar_profile")]
lidar_profile: LidarProfile,

/// The configuration of IMU packets.
///
/// For additional information, [`udp_profile_imu`].
///
/// [`udp_profile_imu`]: https://static.ouster.dev/sensor-docs/image_route1/image_route2/common_sections/API/sensor_configuration_description.html#udp-profile-imu
#[serde(rename = "udp_profile_imu", default = "ConfigParams::default_imu_profile")]
imu_profile: ImuProfile,
// /// The configuration of LIDAR packets.
// ///
// /// For additional information, [`udp_profile_lidar`].
// ///
// /// [`udp_profile_lidar`]: https://static.ouster.dev/sensor-docs/image_route1/image_route2/common_sections/API/sensor_configuration_description.html#udp-profile-lidar
// #[serde(rename = "udp_profile_lidar", default = "ConfigParams::default_lidar_profile")]
// lidar_profile: LidarProfile,

// /// The configuration of IMU packets.
// ///
// /// For additional information, [`udp_profile_imu`].
// ///
// /// [`udp_profile_imu`]: https://static.ouster.dev/sensor-docs/image_route1/image_route2/common_sections/API/sensor_configuration_description.html#udp-profile-imu
// #[serde(rename = "udp_profile_imu", default = "ConfigParams::default_imu_profile")]
// imu_profile: ImuProfile,

/// Determines whether phase locking is enabled.
///
Expand Down Expand Up @@ -301,8 +301,8 @@ impl ConfigParams {
pub const DEFAULT_PHASE_LOCK_ENABLE: bool = false;
pub const DEFAULT_PHASE_LOCK_OFFSET: u32 = 0;

pub const DEFAULT_LIDAR_PROFILE: LidarProfile = LidarProfile::DEFAULT;
pub const DEFAULT_IMU_PROFILE: ImuProfile = ImuProfile::DEFAULT;
// pub const DEFAULT_LIDAR_PROFILE: LidarProfile = LidarProfile::DEFAULT;
// pub const DEFAULT_IMU_PROFILE: ImuProfile = ImuProfile::DEFAULT;

pub const DEFAULT_MIN_RANGE_THRESHOLD: usize = 50;

Expand Down Expand Up @@ -356,12 +356,12 @@ impl ConfigParams {
Self::DEFAULT_PHASE_LOCK_OFFSET
}

fn default_lidar_profile() -> LidarProfile {
Self::DEFAULT_LIDAR_PROFILE
}
fn default_imu_profile() -> ImuProfile {
Self::DEFAULT_IMU_PROFILE
}
// fn default_lidar_profile() -> LidarProfile {
// Self::DEFAULT_LIDAR_PROFILE
// }
// fn default_imu_profile() -> ImuProfile {
// Self::DEFAULT_IMU_PROFILE
// }
fn default_auto_start_flag() -> bool {
true
}
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/lidar_data_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl LidarDataFormat {

LidarDataFormat {
message: "".to_string(),
profile: LidarProfile::DEFAULT,
profile: LidarProfile::Legacy,
pixels_per_column,
column_window,
columns_per_frame,
Expand Down
102 changes: 93 additions & 9 deletions src/packet/imu/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ use binrw::prelude::*;
#[derive(Debug, BinRead, BinWrite, Serialize, Deserialize, PartialEq)]
pub struct Packet {
/// Timestamp of the monotonic system time since boot in ns.
diagnostic_system_time: u64,
pub diagnostic_system_time: u64,

/// Timestamp for the Accelerometer relative to
/// [TimestampMode](crate::types::TimestampMode) in ns.
accelerometer_time: u64,
pub accelerometer_time: u64,

/// Timestamp for the Gyroscope relative to
/// [TimestampMode](crate::types::TimestampMode) in ns.
gyroscope_time: u64,
pub gyroscope_time: u64,

/// Measured linear acceleration in g for the X axis.
linear_acceleration_x: f32,
pub linear_acceleration_x: f32,

/// Measured linear acceleration in g for the Y axis.
linear_acceleration_y: f32,
pub linear_acceleration_y: f32,

/// Measured linear acceleration in g for the Z axis.
linear_acceleration_z: f32,
pub linear_acceleration_z: f32,

/// Measured angular velocity in °/sec for the X axis.
angular_velocity_x: f32,
pub angular_velocity_x: f32,

/// Measured angular velocity in °/sec for the Y axis.
angular_velocity_y: f32,
pub angular_velocity_y: f32,

/// Measured angular velocity in °/sec for the Z axis.
angular_velocity_z: f32,
pub angular_velocity_z: f32,
}

impl Packet {
Expand All @@ -61,6 +61,24 @@ impl Packet {
}
}

impl Default for Packet {
fn default() -> Self {
Self {
diagnostic_system_time: 0,
accelerometer_time: 0,
gyroscope_time: 0,

linear_acceleration_x: 0.0,
linear_acceleration_y: 0.0,
linear_acceleration_z: 0.0,

angular_velocity_x: 0.0,
angular_velocity_y: 0.0,
angular_velocity_z: 0.0,
}
}
}

impl Display for Packet {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
Expand All @@ -76,3 +94,69 @@ impl Display for Packet {
)
}
}

#[cfg(test)]
mod tests {
use super::*;

use log::info;
use test_log::test;

#[test]
fn test_default() {
let want = Packet {
diagnostic_system_time: 0,
accelerometer_time: 0,
gyroscope_time: 0,
linear_acceleration_x: 0.0,
linear_acceleration_y: 0.0,
linear_acceleration_z: 0.0,
angular_velocity_x: 0.0,
angular_velocity_y: 0.0,
angular_velocity_z: 0.0,
};

let got = Packet::default();
assert_eq!(want, got);
}

#[test]
fn test_display() {
let cases = vec![
(
Packet {
diagnostic_system_time: 0,
accelerometer_time: 0,
gyroscope_time: 0,
linear_acceleration_x: 0.0,
linear_acceleration_y: 0.0,
linear_acceleration_z: 0.0,
angular_velocity_x: 0.0,
angular_velocity_y: 0.0,
angular_velocity_z: 0.0,
},
"(t+0, →a=[0.0000, 0.0000, 0.0000], ω=[0.0000, 0.0000, 0.0000])",
),
(
Packet {
diagnostic_system_time: 0,
accelerometer_time: 0,
gyroscope_time: 0,
linear_acceleration_x: 1.0,
linear_acceleration_y: 2.0,
linear_acceleration_z: 3.0,
angular_velocity_x: 4.0,
angular_velocity_y: 5.0,
angular_velocity_z: 6.0,
},
"(t+0, →a=[1.0000, 2.0000, 3.0000], ω=[4.0000, 5.0000, 6.0000])",
),
];

for (packet, want) in cases {
info!("Displaying {packet:?}, expecting {want:?}");
let got = format!("{packet}");
assert_eq!(want, got);
}
}
}
3 changes: 0 additions & 3 deletions src/packet/lidar/alert_flags.rs

This file was deleted.

132 changes: 132 additions & 0 deletions src/packet/lidar/column/block/dual_returns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use binrw::{BinRead, BinResult, BinWrite, Endian, Error};
use std::io::{Error as IOError, ErrorKind::InvalidData, Read, Seek, SeekFrom, Write};

const MASK_RANGE: u32 = 0b1111_1111_1111_1111_1110_0000_0000_0000;
const SHIFT_RANGE: usize = 13;
const MAX_RANGE: u32 = 0b111_1111_1111_1111_1111;
const ERR_RANGE_INVALID: &'static str = "Invalid range value";

const MASK_REFLECTIVITY: u32 = 0b0000_0000_0000_0000_0000_0000_1111_1111;

#[inline]
fn valid_range(range: u32) -> bool {
range <= MAX_RANGE
}

#[derive(Debug, Eq, PartialEq, Clone, derive_new::new)]
pub struct DualReturnBlock {
pub range: u32,
pub reflectivity: u8,

pub range2: u32,
pub reflectivity2: u8,

pub signal: u16,
pub signal2: u16,

pub near_ir: u16,
}

impl DualReturnBlock {
fn read_rr_block<R: Read + Seek>(reader: &mut R, endian: Endian) -> BinResult<(u32, u8)> {
let raw = u32::read_options(reader, endian, ())?;
let range = (raw & MASK_RANGE) >> SHIFT_RANGE;
let reflectivity = (raw & MASK_REFLECTIVITY) as u8;
Ok((range, reflectivity))
}

fn write_rr_block<W: Write + Seek>(
range: u32,
reflectivity: u8,
writer: &mut W,
endian: Endian,
) -> BinResult<()> {
if !valid_range(range) {
let pos = writer.stream_position()?;
return Err(Error::Custom {
pos,
err: Box::new(IOError::new(InvalidData, ERR_RANGE_INVALID)),
});
}

let raw = reflectivity as u32 | (range & MASK_RANGE) << SHIFT_RANGE;
raw.write_options(writer, endian, ())
}
}

impl Default for DualReturnBlock {
fn default() -> Self {
Self {
range: 0,
reflectivity: 0,
range2: 0,
reflectivity2: 0,
signal: 0,
signal2: 0,
near_ir: 0,
}
}
}

impl BinRead for DualReturnBlock {
type Args<'a> = ();

fn read_options<R: Read + Seek>(
reader: &mut R,
endian: Endian,
_: Self::Args<'_>,
) -> BinResult<Self> {
// obtain the first range-reflectivity block
let (range, reflectivity) = Self::read_rr_block(reader, endian)?;

// obtain the second range-reflectivity block
let (range2, reflectivity2) = Self::read_rr_block(reader, endian)?;

// read the first signal
let signal = u16::read_options(reader, endian, ())?;

// read the second signal
let signal2 = u16::read_options(reader, endian, ())?;

// read near-IR
let near_ir = u16::read_options(reader, endian, ())?;

// skip the reserved 16-bit chunk
let pos = reader.stream_position()?;
reader.seek(SeekFrom::Current(2)).map_err(|e| Error::Custom { pos, err: Box::new(e) })?;

Ok(Self { range, reflectivity, range2, reflectivity2, signal, signal2, near_ir })
}
}

impl BinWrite for DualReturnBlock {
type Args<'a> = ();

fn write_options<W: Write + Seek>(
&self,
writer: &mut W,
endian: Endian,
_: Self::Args<'_>,
) -> BinResult<()> {
// write the first range-reflectivity block
Self::write_rr_block(self.range, self.reflectivity, writer, endian)?;

// write the second range-reflectivity block
Self::write_rr_block(self.range2, self.reflectivity2, writer, endian)?;

// write the first signal
self.signal.write_options(writer, endian, ())?;

// write the second signal
self.signal2.write_options(writer, endian, ())?;

// write near-IR
self.near_ir.write_options(writer, endian, ())?;

// skip the reserved 16-bit chunk
let pos = writer.stream_position()?;
writer.seek(SeekFrom::Current(2)).map_err(|e| Error::Custom { pos, err: Box::new(e) })?;

Ok(())
}
}
Loading

0 comments on commit e8d637b

Please sign in to comment.