Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Device module and example for Ping1D #23

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ readme = "README.md"
bytes = "1.3.0"
tokio-util = { version = "0.6.3", features = ["codec"] }
tracing = "0.1.40"

[dev-dependencies]
tokio-serial = "5.4.4"
tokio = { version = "1.37.0", features = ["full"] }
tokio-serial = "5.4.4"
futures = "0.3.30"
clap = { version = "4.5.4", features = ["derive"] }

[dev-dependencies]
tracing-test = "0.2.4"

[build-dependencies]
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
# ping-rs
https://docs.bluerobotics.com/ping-rs/bluerobotics_ping/

## Using example:

To run examples use:

```shell
cargo run --example ping_1d -- --port-name /dev/ttyUSB0
```

Should output:
```shell
Parsing user provided values...
Creating your Ping 1D device
Testing set/get device id: 9
Testing set/get device id: 8
Testing set/get device id: 7
Testing set/get device id: 6
Testing set/get device id: 5
Testing set/get device id: 4
Testing set/get device id: 3
Testing set/get device id: 2
Testing set/get device id: 1
Set gain to auto: true
Protocol version is: 1.0.0
Device id is: 1
Waiting for 30 profiles...
Received 30 profiles
Turning-off the continuous messages stream from Ping1D
```
106 changes: 106 additions & 0 deletions examples/ping_1d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use clap::Parser;
use std::{convert::TryFrom, path::PathBuf};

use bluerobotics_ping::{
device::{Ping1D, PingDevice},
error::PingError,
message::MessageInfo,
ping1d::{self, ProfileStruct},
Messages,
};
use tokio_serial::{SerialPort, SerialPortBuilderExt};

#[tokio::main]
async fn main() -> Result<(), PingError> {
println!("Parsing user provided values...");
let args = Args::parse();

let port =
tokio_serial::new(args.port_name.to_string_lossy(), args.baud_rate).open_native_async()?;
port.clear(tokio_serial::ClearBuffer::All)?;

println!("Creating your Ping 1D device");
let ping1d = Ping1D::new(port);

// Creating a subscription channel which will receive 30 Profile measurements, we'll check this after the next methods!
let mut subscribed = ping1d.subscribe();
let (tx, rx) = tokio::sync::oneshot::channel::<Vec<ProfileStruct>>();
ping1d
.continuous_start(bluerobotics_ping::ping1d::ProfileStruct::id())
.await?;

tokio::spawn(async move {
let mut profile_struct_vector: Vec<ProfileStruct> = Vec::new();
loop {
let received = subscribed.recv().await;
match received {
Ok(msg) => {
if msg.message_id == bluerobotics_ping::ping1d::ProfileStruct::id() {
match Messages::try_from(&msg) {
Ok(Messages::Ping1D(ping1d::Messages::Profile(answer))) => {
profile_struct_vector.push(answer)
}
_ => continue,
}
}
}
Err(_e) => break,
}
if profile_struct_vector.len() >= 30 {
tx.send(profile_struct_vector).unwrap();
break;
};
}
});

for n in (1..10).rev() {
println!("Testing set/get device id: {n}");
ping1d.set_device_id(n).await?;
assert_eq!(n, ping1d.get_device_id().await.unwrap().device_id);
}

// Testing set command, all set commands check for their Ack message, Error and NAck error are possible
println!(
"Set gain to auto: {:?}",
ping1d.set_mode_auto(1).await.is_ok()
);

// Creating two futures to read Protocol Version and Device ID
let res1 = async { ping1d.get_protocol_version().await };
let res2 = async { ping1d.get_device_id().await };
let (protocol_version_struct, device_id_struct) =
tokio::try_join!(res1, res2).expect("Failed to join results");

let version = format!(
"{}.{}.{}",
protocol_version_struct.version_major,
protocol_version_struct.version_minor,
protocol_version_struct.version_patch
);

println!("Protocol version is: {version}");
println!("Device id is: {:?}", device_id_struct.device_id);

// Read the 30 packages we are waiting since the start of this example, all above tasks have success, we did it!
println!("Waiting for 30 profiles...");
match rx.await {
Ok(v) => println!("Received {} profiles", v.len()),
Err(_) => println!("The oneshot sender dropped"),
}

println!("Turning-off the continuous messages stream from Ping1D");
ping1d
.continuous_stop(bluerobotics_ping::ping1d::ProfileStruct::id())
.await?;

Ok(())
}

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(short, long)]
port_name: PathBuf,
#[arg(short, long, default_value_t = 115200)]
baud_rate: u32,
}
Loading
Loading