From 61649f14b3d0170de291b19832d7341bf47e49ae Mon Sep 17 00:00:00 2001 From: Raul Victor Trombin Date: Thu, 10 Oct 2024 18:34:59 -0300 Subject: [PATCH 1/4] src: examples: Share resources between examples --- examples/common/mod.rs | 70 ++++++++++++++++++++++++++++++++++++++ examples/ping_1d.rs | 77 ++---------------------------------------- 2 files changed, 73 insertions(+), 74 deletions(-) create mode 100644 examples/common/mod.rs diff --git a/examples/common/mod.rs b/examples/common/mod.rs new file mode 100644 index 000000000..faaaf74c1 --- /dev/null +++ b/examples/common/mod.rs @@ -0,0 +1,70 @@ +use clap::Parser; +use std::{ + net::{IpAddr, SocketAddr}, + path::PathBuf, + str::FromStr, +}; +use tokio_serial::{SerialPort, SerialPortBuilderExt}; +use udp_stream::UdpStream; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +pub struct Args { + #[arg(long, group = "source", conflicts_with_all = ["udp_address"])] + pub serial_port: Option, + #[arg(long, default_value_t = 115200)] + pub serial_baud_rate: u32, + #[arg(long, group = "source", conflicts_with_all = ["serial_port"])] + pub udp_address: Option, + #[arg(long, default_value_t = 8080)] + pub udp_port: u32, +} + +pub enum Port { + Serial(tokio_serial::SerialStream), + Udp(udp_stream::UdpStream), +} + +pub async fn create_port() -> Port { + let args = Args::parse(); + + match (args.serial_port, args.udp_address) { + (Some(serial_port), None) => { + println!("Using serial port: {:?}", serial_port); + let port = tokio_serial::new(serial_port.to_string_lossy(), args.serial_baud_rate) + .open_native_async() + .map_err(|e| { + eprintln!("Error opening serial port: {}", e); + e + }) + .unwrap(); + port.clear(tokio_serial::ClearBuffer::All).unwrap(); + Port::Serial(port) + } + (None, Some(udp_address)) => { + println!("Using UDP address: {}", udp_address); + let socket_addr = SocketAddr::from_str(&format!("{}:{}", udp_address, args.udp_port)) + .map_err(|e| { + eprintln!("Error parsing UDP address: {}", e); + e + }) + .unwrap(); + let port = UdpStream::connect(socket_addr) + .await + .map_err(|e| { + eprintln!("Error connecting to UDP socket: {}", e); + e + }) + .unwrap(); + Port::Udp(port) + } + (None, None) => { + eprintln!("Error: either serial_port_name or udp_address must be provided"); + std::process::exit(1); + } + (Some(_), Some(_)) => { + eprintln!("Error: serial_port_name and udp_address are mutually exclusive"); + std::process::exit(1); + } + } +} diff --git a/examples/ping_1d.rs b/examples/ping_1d.rs index b775b8068..cea8b2536 100644 --- a/examples/ping_1d.rs +++ b/examples/ping_1d.rs @@ -1,11 +1,6 @@ -use clap::Parser; -use std::{ - convert::TryFrom, - net::{IpAddr, SocketAddr}, - path::PathBuf, - str::FromStr, -}; -use udp_stream::UdpStream; +mod common; +use common::{create_port, Port}; +use std::convert::TryFrom; use bluerobotics_ping::{ device::{Ping1D, PingDevice}, @@ -14,7 +9,6 @@ use bluerobotics_ping::{ ping1d::{self, ProfileStruct}, Messages, }; -use tokio_serial::{SerialPort, SerialPortBuilderExt}; #[tokio::main] async fn main() -> Result<(), PingError> { @@ -137,68 +131,3 @@ async fn main() -> Result<(), PingError> { Ok(()) } - -#[derive(Parser, Debug)] -#[command(version, about, long_about = None)] -struct Args { - #[arg(long, group = "source", - conflicts_with_all = ["udp_address"])] - serial_port: Option, - #[arg(long, default_value_t = 115200)] - serial_baud_rate: u32, - #[arg(long, group = "source", - conflicts_with_all = ["serial_port"])] - udp_address: Option, - #[arg(long, default_value_t = 8080)] - udp_port: u32, -} - -enum Port { - Serial(tokio_serial::SerialStream), - Udp(udp_stream::UdpStream), -} - -async fn create_port() -> Port { - let args = Args::parse(); - - let port = match (args.serial_port, args.udp_address) { - (Some(serial_port), None) => { - println!("Using serial port: {:?}", serial_port); - let port = tokio_serial::new(serial_port.to_string_lossy(), args.serial_baud_rate) - .open_native_async() - .map_err(|e| { - eprintln!("Error opening serial port: {}", e); - e - }) - .unwrap(); - port.clear(tokio_serial::ClearBuffer::All).unwrap(); - Port::Serial(port) - } - (None, Some(udp_address)) => { - println!("Using UDP address: {}", udp_address); - let socket_addr = SocketAddr::from_str(&format!("{}:{}", udp_address, args.udp_port)) - .map_err(|e| { - eprintln!("Error parsing UDP address: {}", e); - e - }) - .unwrap(); - let port = UdpStream::connect(socket_addr) - .await - .map_err(|e| { - eprintln!("Error connecting to UDP socket: {}", e); - e - }) - .unwrap(); - Port::Udp(port) - } - (None, None) => { - eprintln!("Error: either serial_port_name or udp_address must be provided"); - std::process::exit(1); - } - (Some(_), Some(_)) => { - eprintln!("Error: serial_port_name and udp_address are mutually exclusive"); - std::process::exit(1); - } - }; - port -} From 026eabb9b48edf7333f6c3edf566d3db37385ebf Mon Sep 17 00:00:00 2001 From: Raul Victor Trombin Date: Thu, 10 Oct 2024 18:45:00 -0300 Subject: [PATCH 2/4] src: examples: Add ping360 example --- examples/ping_360.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 examples/ping_360.rs diff --git a/examples/ping_360.rs b/examples/ping_360.rs new file mode 100644 index 000000000..0a71c9cd4 --- /dev/null +++ b/examples/ping_360.rs @@ -0,0 +1,50 @@ +mod common; +use common::{create_port, Port}; + +use bluerobotics_ping::{ + device::{Ping360, PingDevice}, + error::PingError, +}; + +#[tokio::main] +async fn main() -> Result<(), PingError> { + println!("Parsing user provided values and creating port..."); + let port = create_port().await; + + println!("Creating your Ping 360 device"); + let ping360 = match port { + Port::Serial(port) => Ping360::new(port), + Port::Udp(port) => Ping360::new(port), + }; + + println!("Reading transducer data:"); + let p = ping360 + .transducer(1, 1, 0, 500, 20000, 700, 1000, 1, 0) + .await?; + println!("mode: {}", p.mode); + println!("gain_setting: {}", p.gain_setting); + println!("angle: {}", p.angle); + println!("transmit_duration: {}", p.transmit_duration); + println!("sample_period: {}", p.sample_period); + println!("transmit_frequency: {}", p.transmit_frequency); + println!("number_of_samples: {}", p.number_of_samples); + println!("data_length: {}", p.data_length); + println!("data: {:?}", p.data.len()); + + // Creating futures to read different device Properties + let (protocol_version_struct, device_information) = + tokio::try_join!(ping360.protocol_version(), ping360.device_information()) + .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 information: {device_information:?}"); + + Ok(()) +} From 5a888d249cb5e114e1abc36b6d8dbdb1d2991a55 Mon Sep 17 00:00:00 2001 From: Raul Victor Trombin Date: Thu, 10 Oct 2024 18:54:22 -0300 Subject: [PATCH 3/4] github: action: Add ping360 test to local runners --- .github/workflows/action.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index db4d1d323..60591ec71 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -75,7 +75,8 @@ jobs: run: | /etc/github_runner/run-tests.sh \ "Basic Ping1D Test w Ping1:ping_1d:--serial-port /dev/ttyUSB_Ping1:30" \ - "Basic Ping1D Test w Ping2:ping_1d:--serial-port /dev/ttyUSB_Ping2:30" + "Basic Ping1D Test w Ping2:ping_1d:--serial-port /dev/ttyUSB_Ping2:30" \ + "Basic Ping360 Test:ping_360:--serial-port /dev/ttyUSB_Ping360:30" # Test Name:Example Name:Additional Args:Timeout - name: Post CI management script if: always() From e042a56d3095f89568ef59ec907f4aaa92b5fa50 Mon Sep 17 00:00:00 2001 From: Raul Victor Trombin Date: Thu, 10 Oct 2024 18:58:32 -0300 Subject: [PATCH 4/4] github: action: Add ping360 udp test --- .github/workflows/action.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 60591ec71..89adb6371 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -76,7 +76,8 @@ jobs: /etc/github_runner/run-tests.sh \ "Basic Ping1D Test w Ping1:ping_1d:--serial-port /dev/ttyUSB_Ping1:30" \ "Basic Ping1D Test w Ping2:ping_1d:--serial-port /dev/ttyUSB_Ping2:30" \ - "Basic Ping360 Test:ping_360:--serial-port /dev/ttyUSB_Ping360:30" + "Basic Ping360 Test w Serial:ping_360:--serial-port /dev/ttyUSB_Ping360:30" \ + "Basic Ping360 Test w UDP:ping_360:--udp-address 192.168.1.197 --udp-port 12345:30" # Test Name:Example Name:Additional Args:Timeout - name: Post CI management script if: always()