Created
January 29, 2025 11:49
-
-
Save patrickelectric/fde7a1bdbf2cbe84f64e790cb86b2187 to your computer and use it in GitHub Desktop.
Simple Rust example for Ping1D
This file contains hidden or 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
use bluerobotics_ping::{device::Ping1D, error::PingError}; | |
use tokio_serial::{SerialPort, SerialPortBuilderExt}; | |
#[tokio::main] | |
async fn main() -> Result<(), PingError> { | |
let port = tokio_serial::new("/dev/ttyUSB0", 115200) | |
.open_native_async() | |
.map_err(|e| { | |
eprintln!("Error opening serial port: {}", e); | |
e | |
}) | |
.unwrap(); | |
port.clear(tokio_serial::ClearBuffer::All).unwrap(); | |
println!("Creating your Ping 1D device"); | |
let ping1d = Ping1D::new(port); | |
use std::time::Instant; | |
let start = Instant::now(); | |
let mut count = 0; | |
loop { | |
count += 1; | |
let distance = ping1d.distance().await?; | |
println!("Distance: {:?}", distance.distance); | |
if count % 10 == 0 { | |
let elapsed = start.elapsed(); | |
println!("Loop frequency: {} Hz", count as f32 / elapsed.as_secs_f32()); | |
} | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment