diff --git a/examples/h61-forward-march.rs b/examples/h61-forward-march.rs index cde1109..e28d2dd 100644 --- a/examples/h61-forward-march.rs +++ b/examples/h61-forward-march.rs @@ -1,14 +1,12 @@ - /// Import std stuff use std::error::Error; -use std::time::Duration; use std::thread; +use std::time::Duration; // Import traits use ufo_rs::traits::control::*; use ufo_rs::traits::drone::*; - fn main() -> Result<(), Box> { // Import controller use ufo_rs::drones::jjrc::h61; @@ -49,7 +47,7 @@ fn main() -> Result<(), Box> { println!("Landing..."); driver.land()?; - + println!("Sent!"); // Ta-dah! diff --git a/examples/h61-hover.rs b/examples/h61-hover.rs index 3b047fc..0a279b8 100644 --- a/examples/h61-hover.rs +++ b/examples/h61-hover.rs @@ -1,14 +1,12 @@ - /// Import std stuff use std::error::Error; -use std::time::Duration; use std::thread; +use std::time::Duration; // Import traits use ufo_rs::traits::control::*; use ufo_rs::traits::drone::*; - fn main() -> Result<(), Box> { // Import controller use ufo_rs::drones::jjrc::h61; @@ -35,19 +33,16 @@ fn main() -> Result<(), Box> { println!("Sent!"); - driver.hover()?; // Wait for a second thread::sleep(delay); - - // Land println!("Landing..."); driver.land()?; - + println!("Sent!"); // Ta-dah! diff --git a/examples/h61-rotate90.rs b/examples/h61-rotate90.rs index 88f37f4..4b34508 100644 --- a/examples/h61-rotate90.rs +++ b/examples/h61-rotate90.rs @@ -1,13 +1,12 @@ /// Import std stuff use std::error::Error; -use std::time::Duration; use std::thread; +use std::time::Duration; // Import traits use ufo_rs::traits::control::*; use ufo_rs::traits::drone::*; - fn main() -> Result<(), Box> { // Import controller use ufo_rs::drones::jjrc::h61; @@ -46,7 +45,7 @@ fn main() -> Result<(), Box> { println!("Landing..."); driver.land()?; - + println!("Sent!"); // Ta-dah! diff --git a/examples/h61-takeoff-landing.rs b/examples/h61-takeoff-landing.rs index ba91717..667f96a 100644 --- a/examples/h61-takeoff-landing.rs +++ b/examples/h61-takeoff-landing.rs @@ -1,13 +1,12 @@ /// Import std stuff use std::error::Error; -use std::time::Duration; use std::thread; +use std::time::Duration; // Import traits use ufo_rs::traits::control::*; use ufo_rs::traits::drone::*; - fn main() -> Result<(), Box> { // Import controller use ufo_rs::drones::jjrc::h61; @@ -41,7 +40,7 @@ fn main() -> Result<(), Box> { println!("Landing..."); driver.land()?; - + println!("Sent!"); // Ta-dah! diff --git a/src/drones/jjrc/h61.rs b/src/drones/jjrc/h61.rs index 3ec467d..508d37f 100644 --- a/src/drones/jjrc/h61.rs +++ b/src/drones/jjrc/h61.rs @@ -20,7 +20,7 @@ pub const CONN_TCP_PORT: &str = "8888"; pub enum TcpHex { Photo, - Video + Video, } impl TcpHex { @@ -29,14 +29,11 @@ impl TcpHex { // Define hex codes match self { TcpHex::Photo => "000102030405060708092525".to_string(), - TcpHex::Video => "000102030405060708092828".to_string() + TcpHex::Video => "000102030405060708092828".to_string(), } - } } - - // Hex Codes for UDP socket /// Hex codes for different commands pub enum UdpHex { @@ -66,9 +63,6 @@ pub enum UdpHex { RotLeft, /// Hex code for rotating right RotRight, - - - } impl UdpHex { @@ -91,16 +85,13 @@ impl UdpHex { UdpHex::RotLeft => "ff087e00403f9012120046".to_string(), UdpHex::RotRight => "ff087e7e403f90121200c8".to_string(), } - } } - // Code for drone programmer-facing API /// UFO controls of the JJRC H61 foldable drone pub struct Driver { - /// UDP Drone controller, handles connections etc. connection: crate::DroneUdpConnection, camera: crate::DroneTcpConnection, @@ -116,8 +107,18 @@ impl Driver { pub fn new() -> Self { // create new connection Driver { - connection: crate::DroneUdpConnection::new(BIND_IP.to_string(), BIND_PORT.to_string(), CONN_IP.to_string(), CONN_UDP_PORT.to_string()), - camera: crate::DroneTcpConnection::new(BIND_IP.to_string(), BIND_PORT.to_string(), CONN_IP.to_string(), CONN_TCP_PORT.to_string()), + connection: crate::DroneUdpConnection::new( + BIND_IP.to_string(), + BIND_PORT.to_string(), + CONN_IP.to_string(), + CONN_UDP_PORT.to_string(), + ), + camera: crate::DroneTcpConnection::new( + BIND_IP.to_string(), + BIND_PORT.to_string(), + CONN_IP.to_string(), + CONN_TCP_PORT.to_string(), + ), } } @@ -133,7 +134,6 @@ impl Driver { self.camera.read() } - /// Connect to drone pub fn connect(&mut self) -> Result<(), Box> { self.connection.connect() @@ -154,7 +154,6 @@ impl drone::Hover for Driver { } } - // Implement FlightControl for H61 Driver impl control::FlightControl for Driver { fn take_off(&mut self) -> Result<(), Box> { @@ -174,7 +173,6 @@ impl drone::Stop for Driver { // Stop propellers self.connection.send_command(UdpHex::Stop.value()) } - } // Add movement controls @@ -189,7 +187,6 @@ impl control::Movement for Driver { fn up(&mut self, time: usize) -> Result<(), Box> { self.connection.send_command(UdpHex::Up.value()) - } fn down(&mut self, time: usize) -> Result<(), Box> { self.connection.send_command(UdpHex::Down.value()) @@ -199,18 +196,15 @@ impl control::Movement for Driver { fn forwards(&mut self, time: usize) -> Result<(), Box> { self.connection.send_command(UdpHex::Forwards.value()) - } fn backwards(&mut self, time: usize) -> Result<(), Box> { self.connection.send_command(UdpHex::Backwards.value()) - } // TODO Determine length of time parameter (milliseconds or seconds) fn rot_left(&mut self, time: usize) -> Result<(), Box> { self.connection.send_command(UdpHex::RotLeft.value()) - } fn rot_right(&mut self, time: usize) -> Result<(), Box> { self.connection.send_command(UdpHex::RotRight.value()) diff --git a/src/lib.rs b/src/lib.rs index 522b8cd..827984d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,8 @@ -use std::net::UdpSocket; -use std::net::TcpStream; +use hex; use std::error::Error; use std::io::prelude::*; -use hex; +use std::net::TcpStream; +use std::net::UdpSocket; pub mod drones; pub mod traits; @@ -17,7 +17,7 @@ mod tests { pub mod errors { use std::fmt; - + /// Error for issues with socket connection pub struct ConnectionError; @@ -35,14 +35,16 @@ pub mod errors { } } - /// Error for issues with drone executing commands pub struct CommandError; // Implement std::fmt::Display for CommandError impl fmt::Display for CommandError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "An Command Error Occurred, Please confirm the hex values for your drone are valid") // user-facing output + write!( + f, + "An Command Error Occurred, Please confirm the hex values for your drone are valid" + ) // user-facing output } } @@ -67,13 +69,17 @@ pub struct DroneUdpConnection { connect_port: String, /// Socket of drone - sock: UdpSocket + sock: UdpSocket, } impl DroneUdpConnection { - /// Create a new drone connection takes in `bind_ip`, `bind_port`, `connect_ip`, and `connect_port` all as `Strings` - pub fn new(bind_ip: String, bind_port: String, connect_ip: String, connect_port: String) -> Self { + pub fn new( + bind_ip: String, + bind_port: String, + connect_ip: String, + connect_port: String, + ) -> Self { let mut bind_url = String::new(); bind_url.push_str(&bind_ip); @@ -86,7 +92,7 @@ impl DroneUdpConnection { connect_ip, connect_port, // Not yet connected - sock: UdpSocket::bind(bind_url).unwrap() + sock: UdpSocket::bind(bind_url).unwrap(), } } @@ -99,7 +105,6 @@ impl DroneUdpConnection { // bind_url.push_str(":"); // bind_url.push_str(&self.bind_port); - // // Bind to port // self.sock = UdpSocket::bind(bind_url)?; @@ -113,7 +118,6 @@ impl DroneUdpConnection { self.sock.connect(conn_url)?; Ok(()) - } /// Send a static command to the drone @@ -130,15 +134,14 @@ impl DroneUdpConnection { // Return we're okay Ok(()) } - } -/// Status of a drone -pub struct DroneStatus{ +/// Status of a drone +pub struct DroneStatus { /// Battery charge of the drone bat_charge: usize, /// Capacity of drone battery - bat_cap: usize + bat_cap: usize, } pub struct DroneTcpConnection { @@ -153,13 +156,17 @@ pub struct DroneTcpConnection { connect_port: String, /// Socket of drone - sock: TcpStream + sock: TcpStream, } impl DroneTcpConnection { - /// Create a new drone connection takes in `bind_ip`, `bind_port`, `connect_ip`, and `connect_port` all as `Strings` - pub fn new(bind_ip: String, bind_port: String, connect_ip: String, connect_port: String) -> Self { + pub fn new( + bind_ip: String, + bind_port: String, + connect_ip: String, + connect_port: String, + ) -> Self { let mut connect_url = String::new(); connect_url.push_str(&connect_ip); @@ -172,7 +179,7 @@ impl DroneTcpConnection { connect_ip, connect_port, // Not yet connected - sock: TcpStream::connect(connect_url).unwrap() + sock: TcpStream::connect(connect_url).unwrap(), } } @@ -209,11 +216,10 @@ impl DroneTcpConnection { } pub fn read(&mut self) -> Result, Box> { - let mut buffer: &mut[u8] = &mut [0; 128]; + let mut buffer: &mut [u8] = &mut [0; 128]; self.sock.read(&mut buffer)?; Ok(buffer.to_owned()) } - } diff --git a/src/traits.rs b/src/traits.rs index 0bf1d29..6ae37c4 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -1,7 +1,7 @@ /// Mod for traits regarding onboard functions. (i.e. Camera stream, LEDs, barometer, battery status, etc) pub mod drone { use std::error::Error; - + // TODO Calibration trait /// Trait for drone calibration pub trait Calibrate { @@ -11,7 +11,6 @@ pub mod drone { pub trait Stop { fn stop(&mut self) -> Result<(), Box>; - } /// Trait for drones that can hover @@ -20,7 +19,6 @@ pub mod drone { fn hover(&mut self) -> Result<(), Box>; } - // TODO Emergency trait // TODO VideoStream trait // TODO PhotoStream trait @@ -64,7 +62,6 @@ pub mod control { /// Movement pub trait (i.e. controls for Up, Down, Forward, Backwards, Left and Right); pub trait Movement { - // TODO Determine length of time parameter (milliseconds or seconds) // X Axis @@ -97,7 +94,6 @@ pub mod control { fn rot_right(&mut self, time: usize) -> Result<(), Box>; } - /// pub trait for Takeoff and Landing pub trait FlightControl { /// Performs a takeoff