-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hiwonder imu, refactor and return data as struct
- Loading branch information
1 parent
473aa51
commit 9f13b24
Showing
3 changed files
with
75 additions
and
27 deletions.
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 |
---|---|---|
@@ -1,32 +1,55 @@ | ||
use hiwonder::IMU; | ||
use std::time::Duration; | ||
use std::io::{self, Read}; | ||
use std::io; | ||
|
||
//* run by `cargo run --bin log` */ | ||
|
||
fn main() -> io::Result<()> { | ||
let port_name = "/dev/ttyUSB0"; | ||
let baud_rate = 9600; | ||
|
||
let mut port = serialport::new(port_name, baud_rate) | ||
.timeout(Duration::from_millis(500)) // 0.5 second timeout | ||
.open() | ||
.expect("Failed to open serial port"); | ||
#[derive(Debug)] | ||
struct IMUData { | ||
acc_x: f32, | ||
acc_y: f32, | ||
acc_z: f32, | ||
gyro_x: f32, | ||
gyro_y: f32, | ||
gyro_z: f32, | ||
angle_x: f32, | ||
angle_y: f32, | ||
angle_z: f32, | ||
} | ||
|
||
impl From<([f32; 3], [f32; 3], [f32; 3])> for IMUData { | ||
fn from((acc, gyro, angle): ([f32; 3], [f32; 3], [f32; 3])) -> Self { | ||
IMUData { | ||
acc_x: acc[0], | ||
acc_y: acc[1], | ||
acc_z: acc[2], | ||
gyro_x: gyro[0], | ||
gyro_y: gyro[1], | ||
gyro_z: gyro[2], | ||
angle_x: angle[0], | ||
angle_y: angle[1], | ||
angle_z: angle[2], | ||
} | ||
} | ||
} | ||
|
||
let mut imu = IMU::new(); | ||
let mut buffer: Vec<u8> = vec![0; 1024]; | ||
fn main() -> io::Result<()> { | ||
let mut imu = IMU::new("/dev/ttyUSB0", 9600)?; | ||
|
||
loop { | ||
match port.read(&mut buffer) { | ||
Ok(bytes_read) => { | ||
if bytes_read > 0 { | ||
imu.process_data(&buffer[..bytes_read]); | ||
} | ||
} | ||
Err(e) => { | ||
eprintln!("Error reading from serial port: {}", e); | ||
match imu.read_data() { | ||
Ok(Some(data)) => { | ||
let data = IMUData::from(data); | ||
println!( | ||
"acc: x: {: >10.3} y: {: >10.3} z: {: >10.3}\n\ | ||
gyro: x: {: >10.3} y: {: >10.3} z: {: >10.3}\n\ | ||
angle: x: {: >10.3} y: {: >10.3} z: {: >10.3}", | ||
data.acc_x, data.acc_y, data.acc_z, | ||
data.gyro_x, data.gyro_y, data.gyro_z, | ||
data.angle_x, data.angle_y, data.angle_z | ||
); | ||
} | ||
Ok(None) => (), // No complete data available yet | ||
Err(e) => eprintln!("Error reading from serial port: {}", e), | ||
} | ||
} | ||
} |
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