Skip to content

Commit

Permalink
tokio async don't block executer
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander89 committed Sep 5, 2021
1 parent b0a5fd4 commit fe12816
Show file tree
Hide file tree
Showing 13 changed files with 1,177 additions and 195 deletions.
91 changes: 90 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[package]
name = "tello"
description = "SDK for intel DJI Tello drone using the native api"
version = "0.5.2"
version = "0.6.0"
authors = ["Alex Halemba <[email protected]>"]
edition = "2018"
repository = "https://github.com/Alexander89/rust-tello"
license-file = "./LICENCE"
license = "MIT"
readme = "./README.md"
keywords = ["Drone", "Tello", "Ryze", "DJI", "RyzeRobotics"]
documentation = "https://docs.rs/tello"
Expand All @@ -26,18 +27,28 @@ path = "examples/fly_gamepad/main.rs"
name = "command_mode"
path = "examples/command_mode/main.rs"

[[example]]
name = "command_mode_tokio"
path = "examples/command_mode_tokio/main.rs"
required-features = ["tokio_async"]

[[example]]
name = "command_mode_keyboard"
path = "examples/command_mode_keyboard/main.rs"

[dependencies]
byteorder = "1.4"
chrono = "0.4.19"
tokio = { version = "1.11.0", features = ["net", "rt", "sync", "macros", "rt-multi-thread"], optional = true }
tokio-stream = { version = "0.1.7", optional = true }

[dev-dependencies]
sdl2 = {version = "0.34.5", features = ["ttf"]}
# glib = "0.9"
# gstreamer = "0.15"
# gstreamer-video = "0.15"
gilrs = "0.7.2"
gilrs = "0.7.4"
futures = "0.3.16"

[features]
tokio_async = ["tokio", "tokio-stream"]
2 changes: 1 addition & 1 deletion LICENCE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Alexander Halemba
Copyright (c) 2021 Alexander Halemba

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
46 changes: 24 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,33 @@ use std::{string::String, thread::sleep, time::Duration};
use tello::Drone;

fn main() -> Result<(), String> {
let mut drone = Drone::new("192.168.10.1:8889").command_mode();
block_on(drone.enable());

match drone.state_receiver.recv_timeout(Duration::from_secs(5)) {
Ok(message) => println!(
"Battery {}% Height {}dm POS {:?}",
message.bat, message.h, drone.odometry
),
_ => println!("No state package received"),
}
block_on(async {
let mut drone = Drone::new("192.168.10.1:8889").command_mode();
let state = drone.state_receiver().unwrap();
drone.enable().await?;

match state.recv_timeout(Duration::from_secs(5)) {
Ok(message) => println!(
"Battery {}% Height {}dm POS {:?}",
message.bat, message.h, drone.odometry
),
_ => println!("No state package received"),
}

block_on(drone.take_off());
sleep(Duration::from_secs(7));
println!("take_off {:?}", drone.take_off().await);
sleep(Duration::from_secs(7));

for _ in 0..4 {
block_on(drone.forward(50));
sleep(Duration::from_secs(5));
block_on(drone.cw(90));
sleep(Duration::from_secs(4));
}

block_on(drone.land());
sleep(Duration::from_secs(3));
for _ in 0..6 {
println!("forward {:?}", drone.forward(30).await);
sleep(Duration::from_secs(5));
println!("cw {:?}", drone.cw(60).await);
sleep(Duration::from_secs(4));
}

Ok(())
println!("land {:?}", drone.land().await);
sleep(Duration::from_secs(3));
Ok(())
})
}
```

Expand Down
9 changes: 5 additions & 4 deletions examples/command_mode/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use tello::Drone;
fn main() -> Result<(), String> {
block_on(async {
let mut drone = Drone::new("192.168.10.1:8889").command_mode();
let _failed_sometimes_but_works = drone.enable().await;
let state = drone.state_receiver().unwrap();
drone.enable().await?;

match drone.state_receiver.recv_timeout(Duration::from_secs(5)) {
match state.recv_timeout(Duration::from_secs(5)) {
Ok(message) => println!(
"Battery {}% Height {}dm POS {:?}",
message.bat, message.h, drone.odometry
Expand All @@ -27,6 +28,6 @@ fn main() -> Result<(), String> {

println!("land {:?}", drone.land().await);
sleep(Duration::from_secs(3));
});
Ok(())
Ok(())
})
}
81 changes: 42 additions & 39 deletions examples/command_mode_keyboard/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,52 @@ use std::{io, thread};
use tello::Drone;

fn main() -> Result<(), String> {
let mut drone = Drone::new("192.168.10.1:8889").command_mode();
block_on(async {
let mut drone = Drone::new("192.168.10.1:8889").command_mode();
let state = drone.state_receiver().unwrap();

let stdin_channel = create_stdin_channel();
let _failed_sometimes_but_works = block_on(drone.enable());
'mainLoop: loop {
match stdin_channel.try_recv() {
Ok(input) => {
let commands: Vec<&str> = input.split(' ').collect();
let send = match commands[0] {
"exit" => break 'mainLoop,
"streamon" => block_on(drone.video_on()),
"streamoff" => block_on(drone.video_off()),
"enable" => block_on(drone.enable()),
"start" => block_on(drone.take_off()),
"land" => block_on(drone.land()),
"down" => block_on(drone.down(commands[1].parse().unwrap_or(0))),
"up" => block_on(drone.up(commands[1].parse().unwrap_or(0))),
"forward" => block_on(drone.forward(commands[1].parse().unwrap_or(0))),
"back" => block_on(drone.back(commands[1].parse().unwrap_or(0))),
"left" => block_on(drone.left(commands[1].parse().unwrap_or(0))),
"right" => block_on(drone.right(commands[1].parse().unwrap_or(0))),
"cw" => block_on(drone.cw(commands[1].parse().unwrap_or(0))),
"ccw" => block_on(drone.ccw(commands[1].parse().unwrap_or(0))),
//"home" => drone.go_home(0, 0, 0, 20),
_ => Ok(()),
};
if send.is_err() {
println!("{}", send.err().unwrap())
let stdin_channel = create_stdin_channel();
let _failed_sometimes_but_works = drone.enable().await;
'mainLoop: loop {
match stdin_channel.try_recv() {
Ok(input) => {
let commands: Vec<&str> = input.split(' ').collect();
let send = match commands[0] {
"exit" => break 'mainLoop,
"streamon" => drone.video_on().await,
"streamoff" => drone.video_off().await,
"enable" => drone.enable().await,
"start" => drone.take_off().await,
"land" => drone.land().await,
"down" => drone.down(commands[1].parse().unwrap_or(0)).await,
"up" => drone.up(commands[1].parse().unwrap_or(0)).await,
"forward" => drone.forward(commands[1].parse().unwrap_or(0)).await,
"back" => drone.back(commands[1].parse().unwrap_or(0)).await,
"left" => drone.left(commands[1].parse().unwrap_or(0)).await,
"right" => drone.right(commands[1].parse().unwrap_or(0)).await,
"cw" => drone.cw(commands[1].parse().unwrap_or(0)).await,
"ccw" => drone.ccw(commands[1].parse().unwrap_or(0)).await,
//"home" => drone.go_home(0, 0, 0, 20),
_ => Ok(()),
};
if send.is_err() {
println!("{}", send.err().unwrap())
}
}
Err(TryRecvError::Disconnected) => panic!("Channel disconnected"),
_ => (),
}
Err(TryRecvError::Disconnected) => panic!("Channel disconnected"),
_ => (),
}
match drone.state_receiver.try_recv() {
Ok(message) => println!(
"battery {}% height {}dm POS {:?}",
message.bat, message.h, drone.odometry
),
_ => (),
match state.try_recv() {
Ok(message) => println!(
"battery {}% height {}dm POS {:?}",
message.bat, message.h, drone.odometry
),
_ => (),
}
sleep(Duration::from_millis(100));
}
sleep(Duration::from_millis(100));
}
Ok(())
Ok(())
})
}

fn create_stdin_channel() -> Receiver<String> {
Expand Down
38 changes: 38 additions & 0 deletions examples/command_mode_tokio/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::time::Duration;
use tello::Drone;
use tokio::{select, time::sleep};

#[tokio::main]
async fn main() -> Result<(), String> {
let mut drone = Drone::new("192.168.10.1:8889").command_mode();
// let mut drone = Drone::new("127.0.0.1:8880").command_mode();
drone.enable().await?;

let mut state = drone.state_receiver().unwrap();

loop {
let path = async {
for _ in 0..6 {
println!("forward {:?}", drone.forward(30).await);
sleep(Duration::from_secs(5)).await;
println!("cw {:?}", drone.cw(60).await);
sleep(Duration::from_secs(4)).await;
}
println!("land {:?}", drone.land().await);
};

select! {
_ = state.changed() => {
if let Some(s) = state.borrow_and_update().clone() {
println!( "Battery {}% Height {}dm POS {:?}", s.bat, s.h, drone.odometry );
}
},
_ = path => {
println!("done");
break;
}
}
}
sleep(Duration::from_secs(3)).await;
Ok(())
}
Loading

0 comments on commit fe12816

Please sign in to comment.