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

Anafi mgmt with an example 🚀 #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
650 changes: 464 additions & 186 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ members = [
"arsdk-rs",
"jumpingsumo-rs",
"bebop2",
# "anafi-rs",
"anafi-rs",
]
1 change: 1 addition & 0 deletions anafi-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
arsdk-rs = { path = "../arsdk-rs" }
scroll = "0.10.1"

[dev-dependencies]
# Used for examples
Expand Down
53 changes: 45 additions & 8 deletions anafi-rs/examples/liftoff.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,64 @@
use log::info;

Check warning on line 1 in anafi-rs/examples/liftoff.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `log::info`

Check warning on line 1 in anafi-rs/examples/liftoff.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `log::info`

Check warning on line 1 in anafi-rs/examples/liftoff.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `log::info`

Check warning on line 1 in anafi-rs/examples/liftoff.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `log::info`
use std::error::Error;

use anafi_rs::prelude::*;
use std::time::Duration;

// https://www.dema.ch/media/catalog/product/pdf/1976008063/pdf_file_3/en_US/white-paper-anafi-usa-v1.5.2_en.pdf
// https://github.com/RIAEvangelist/node-parrot-drone/blob/master/docs/ardrone3.md

fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();

let drone_ip: std::net::IpAddr = "192.168.42.1".parse()?;
let drone = Anafi::connect(drone_ip.into())?;

info!("Takeoff!");
drone.take_off()?;

for i in 0..50 {
drone.take_off()?;
}
std::thread::sleep(Duration::from_secs(2));
log::warn!("UP!");
drone.up()?;
std::thread::sleep(Duration::from_secs(2));

info!("Wait 5 seconds and fly UP");
std::thread::sleep(Duration::from_secs(5));
log::warn!("forward!");
drone.forward()?;
std::thread::sleep(Duration::from_secs(1));
drone.stop()?;

log::warn!("backward!");
drone.backward()?;
std::thread::sleep(Duration::from_secs(1));
drone.stop()?;

log::warn!("left!");
drone.strafe_left()?;
std::thread::sleep(Duration::from_secs(1));

log::warn!("right!");
drone.strafe_right()?;
std::thread::sleep(Duration::from_secs(1));

for i in 0..50 {
drone.landing()?;
log::warn!("turn left!");
for _ in 0..30 {
drone.turn_left()?;
std::thread::sleep(Duration::from_millis(300));
}

log::warn!("turn right!");
for _ in 0..30 {
drone.turn_right()?;
std::thread::sleep(Duration::from_millis(300));
}

log::warn!("DOWN!");
drone.down()?;
std::thread::sleep(Duration::from_secs(2));

std::thread::sleep(Duration::from_secs(2));
log::warn!("LAND!");
drone.landing()?;

std::thread::sleep(Duration::from_secs(5));

Ok(())
}
138 changes: 122 additions & 16 deletions anafi-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub mod prelude {
}

pub struct Anafi {
// roll: 0,
// pitch: forward / backward,
// yaw: strafe left / right,
// gaz: up / down
drone: Drone,
}

Expand All @@ -31,41 +35,148 @@ impl Anafi {
pub fn take_off(&self) -> Result<(), Error> {
let feature = Feature::ArDrone3(Some(ArDrone3::Piloting(Piloting::TakeOff)));

let frame = Frame::for_drone(
&self.drone,
Type::DataWithAck,
BufferID::CDAck,
Some(feature),
);
let frame = Frame::for_drone(&self.drone, Type::Data, BufferID::CDNonAck, Some(feature));

self.drone.send_frame(frame)
}

pub fn up(&self, sequence_id: u8) -> Result<(), Error> {
pub fn up(&self) -> Result<(), Error> {
let feature = Feature::ArDrone3(Some(ArDrone3::Piloting(Piloting::PCMD(PCMD {
flag: true,
roll: 0,
pitch: 0,
yaw: 0,
gaz: 100,
timestamp: Utc::now(),
sequence_id,
sequence_id: self.drone.piloting_id(),
}))));

let frame = Frame::for_drone(&self.drone, Type::Data, BufferID::CDNonAck, Some(feature));

self.drone.send_frame(frame)
}

pub fn down(&self, sequence_id: u8) -> Result<(), Error> {
pub fn down(&self) -> Result<(), Error> {
let feature = Feature::ArDrone3(Some(ArDrone3::Piloting(Piloting::PCMD(PCMD {
flag: true,
roll: 0,
pitch: 0,
yaw: 0,
gaz: -100,
timestamp: Utc::now(),
sequence_id,
sequence_id: self.drone.piloting_id(),
}))));

let frame = Frame::for_drone(&self.drone, Type::Data, BufferID::CDNonAck, Some(feature));

self.drone.send_frame(frame)
}

pub fn backward(&self) -> Result<(), Error> {
let feature = Feature::ArDrone3(Some(ArDrone3::Piloting(Piloting::PCMD(PCMD {
flag: true,
roll: 0,
pitch: -100,
yaw: 0,
gaz: 0,
timestamp: Utc::now(),
sequence_id: self.drone.piloting_id(),
}))));

let frame = Frame::for_drone(&self.drone, Type::Data, BufferID::CDNonAck, Some(feature));

self.drone.send_frame(frame)
}

pub fn forward(&self) -> Result<(), Error> {
let feature = Feature::ArDrone3(Some(ArDrone3::Piloting(Piloting::PCMD(PCMD {
flag: true,
roll: 0,
pitch: 100,
yaw: 0,
gaz: 0,
timestamp: Utc::now(),
sequence_id: self.drone.piloting_id(),
}))));

let frame = Frame::for_drone(&self.drone, Type::Data, BufferID::CDNonAck, Some(feature));

self.drone.send_frame(frame)
}

pub fn strafe_left(&self) -> Result<(), Error> {
let feature = Feature::ArDrone3(Some(ArDrone3::Piloting(Piloting::PCMD(PCMD {
flag: true,
roll: -100,
pitch: 0,
yaw: 0,
gaz: 0,
timestamp: Utc::now(),
sequence_id: self.drone.piloting_id(),
}))));

let frame = Frame::for_drone(&self.drone, Type::Data, BufferID::CDNonAck, Some(feature));

self.drone.send_frame(frame)
}

pub fn strafe_right(&self) -> Result<(), Error> {
let feature = Feature::ArDrone3(Some(ArDrone3::Piloting(Piloting::PCMD(PCMD {
flag: true,
roll: 100,
pitch: 0,
yaw: 0,
gaz: 0,
timestamp: Utc::now(),
sequence_id: self.drone.piloting_id(),
}))));

let frame = Frame::for_drone(&self.drone, Type::Data, BufferID::CDNonAck, Some(feature));

self.drone.send_frame(frame)
}

pub fn turn_left(&self) -> Result<(), Error> {
let feature = Feature::ArDrone3(Some(ArDrone3::Piloting(Piloting::PCMD(PCMD {
flag: false,
roll: 0,
pitch: 0,
yaw: -128,
gaz: 0,
timestamp: Utc::now(),
sequence_id: self.drone.piloting_id(),
}))));

let frame = Frame::for_drone(&self.drone, Type::Data, BufferID::CDNonAck, Some(feature));

self.drone.send_frame(frame)
}

pub fn turn_right(&self) -> Result<(), Error> {
let feature = Feature::ArDrone3(Some(ArDrone3::Piloting(Piloting::PCMD(PCMD {
flag: false,
roll: 0,
pitch: 0,
yaw: 127,
gaz: 0,
timestamp: Utc::now(),
sequence_id: self.drone.piloting_id(),
}))));

let frame = Frame::for_drone(&self.drone, Type::Data, BufferID::CDNonAck, Some(feature));

self.drone.send_frame(frame)
}

pub fn stop(&self) -> Result<(), Error> {
let feature = Feature::ArDrone3(Some(ArDrone3::Piloting(Piloting::PCMD(PCMD {
flag: true,
roll: 0,
pitch: 0,
yaw: 0,
gaz: 0,
timestamp: Utc::now(),
sequence_id: self.drone.piloting_id(),
}))));

let frame = Frame::for_drone(&self.drone, Type::Data, BufferID::CDNonAck, Some(feature));
Expand All @@ -76,12 +187,7 @@ impl Anafi {
pub fn landing(&self) -> Result<(), Error> {
let feature = Feature::ArDrone3(Some(ArDrone3::Piloting(Piloting::Landing)));

let frame = Frame::for_drone(
&self.drone,
Type::DataWithAck,
BufferID::CDAck,
Some(feature),
);
let frame = Frame::for_drone(&self.drone, Type::Data, BufferID::CDNonAck, Some(feature));

self.drone.send_frame(frame)
}
Expand Down
9 changes: 6 additions & 3 deletions arsdk-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
[package]
name = "arsdk-rs"
version = "0.0.5"
authors = ["o0Ignition0o <[email protected]>", "Lachezar Lechev <[email protected]>"]
authors = [
"o0Ignition0o <[email protected]>",
"Lachezar Lechev <[email protected]>",
]
edition = "2018"
description = "Parrot drones SDK in Rust (AeroRust)"
license = "MIT/Apache-2.0"
keywords = ["AeroRust", "drone", "parrot", "sdk"]

[dependencies]
thiserror = "1.0"
pnet = "0.25"
serde = {version = "1.0", features = ["derive"]}
pnet = "0.26"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_with = "1"
dashmap = "3.11"
Expand Down
2 changes: 1 addition & 1 deletion arsdk-rs/src/ardrone3/piloting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
StopPilotedPOI,
}

impl Into<u16> for &Piloting {

Check failure on line 48 in arsdk-rs/src/ardrone3/piloting.rs

View workflow job for this annotation

GitHub Actions / Clippy

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true

Check failure on line 48 in arsdk-rs/src/ardrone3/piloting.rs

View workflow job for this annotation

GitHub Actions / Clippy

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
fn into(self) -> u16 {
use Piloting::*;

Expand Down Expand Up @@ -107,7 +107,7 @@
}
}

impl<'a> ctx::TryIntoCtx<Endian> for Piloting {

Check failure on line 110 in arsdk-rs/src/ardrone3/piloting.rs

View workflow job for this annotation

GitHub Actions / Clippy

this lifetime isn't used in the impl

Check failure on line 110 in arsdk-rs/src/ardrone3/piloting.rs

View workflow job for this annotation

GitHub Actions / Clippy

this lifetime isn't used in the impl
type Error = Error;

fn try_into_ctx(self, this: &mut [u8], ctx: Endian) -> Result<usize, Self::Error> {
Expand All @@ -120,7 +120,7 @@
Piloting::PCMD(pcmd) => {
this.gwrite_with(pcmd, &mut offset, ctx)?;
}
// Piloting::Landing => {}
Piloting::Landing => {}
// Piloting::Emergency => {}
// Piloting::NavigateHome => {}
// Piloting::AutoTakeOffMode => {}
Expand Down
1 change: 0 additions & 1 deletion arsdk-rs/src/ardrone3/piloting/pcmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
pub yaw: i8,
pub gaz: i8,
pub timestamp: DateTime<Utc>,
// TODO: How should we handle the `sequence_id` in order not to show it to the user?
pub sequence_id: u8,
}

Expand Down Expand Up @@ -59,7 +58,7 @@
}
}

impl<'a> ctx::TryIntoCtx<Endian> for PCMD {

Check failure on line 61 in arsdk-rs/src/ardrone3/piloting/pcmd.rs

View workflow job for this annotation

GitHub Actions / Clippy

this lifetime isn't used in the impl

Check failure on line 61 in arsdk-rs/src/ardrone3/piloting/pcmd.rs

View workflow job for this annotation

GitHub Actions / Clippy

this lifetime isn't used in the impl
type Error = Error;

fn try_into_ctx(self, this: &mut [u8], ctx: Endian) -> Result<usize, Self::Error> {
Expand Down Expand Up @@ -100,7 +99,7 @@
0,
0,
]);
let timestamp = Utc.timestamp_millis(timestamp_i64);

Check failure on line 102 in arsdk-rs/src/ardrone3/piloting/pcmd.rs

View workflow job for this annotation

GitHub Actions / Clippy

use of deprecated method `chrono::TimeZone::timestamp_millis`: use `timestamp_millis_opt()` instead

Check warning on line 102 in arsdk-rs/src/ardrone3/piloting/pcmd.rs

View workflow job for this annotation

GitHub Actions / Check

use of deprecated method `chrono::TimeZone::timestamp_millis`: use `timestamp_millis_opt()` instead

Check warning on line 102 in arsdk-rs/src/ardrone3/piloting/pcmd.rs

View workflow job for this annotation

GitHub Actions / Check

use of deprecated method `chrono::TimeZone::timestamp_millis`: use `timestamp_millis_opt()` instead

Check failure on line 102 in arsdk-rs/src/ardrone3/piloting/pcmd.rs

View workflow job for this annotation

GitHub Actions / Clippy

use of deprecated method `chrono::TimeZone::timestamp_millis`: use `timestamp_millis_opt()` instead

Check warning on line 102 in arsdk-rs/src/ardrone3/piloting/pcmd.rs

View workflow job for this annotation

GitHub Actions / Test Suite

use of deprecated method `chrono::TimeZone::timestamp_millis`: use `timestamp_millis_opt()` instead

Check warning on line 102 in arsdk-rs/src/ardrone3/piloting/pcmd.rs

View workflow job for this annotation

GitHub Actions / Test Suite

use of deprecated method `chrono::TimeZone::timestamp_millis`: use `timestamp_millis_opt()` instead
// 8 bits
let sequence_id = timestamp_and_seq[3];

Expand All @@ -114,7 +113,7 @@
}
}

impl<'a> ctx::TryIntoCtx<Endian> for TimestampAndSeq {

Check failure on line 116 in arsdk-rs/src/ardrone3/piloting/pcmd.rs

View workflow job for this annotation

GitHub Actions / Clippy

this lifetime isn't used in the impl

Check failure on line 116 in arsdk-rs/src/ardrone3/piloting/pcmd.rs

View workflow job for this annotation

GitHub Actions / Clippy

this lifetime isn't used in the impl
type Error = Error;

fn try_into_ctx(self, this: &mut [u8], ctx: Endian) -> Result<usize, Self::Error> {
Expand Down
28 changes: 25 additions & 3 deletions arsdk-rs/src/ardrone3/piloting_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
SpeedChanged,
/// ARCOMMANDS_ID_ARDRONE3_PILOTINGSTATE_CMD_ATTITUDECHANGED = 6,
/// Frame { frame_type: Data, buffer_id: DCNavdata, sequence_id: 40, feature: Some(ArDrone3(Some(PilotingState { data: [6, 0, 44, 49, 49, 55, 153, 38, 7, 185, 107, 25, 201, 63] }))) }
AttitudeChanged,
AttitudeChanged(AttitudeChanged),
/// ARCOMMANDS_ID_ARDRONE3_PILOTINGSTATE_CMD_AUTOTAKEOFFMODECHANGED = 7,
AutoTakeOffModeChanged,
/// ARCOMMANDS_ID_ARDRONE3_PILOTINGSTATE_CMD_ALTITUDECHANGED = 8,
Expand Down Expand Up @@ -80,7 +80,7 @@
},
}

impl Into<u16> for &PilotingState {

Check failure on line 83 in arsdk-rs/src/ardrone3/piloting_state.rs

View workflow job for this annotation

GitHub Actions / Clippy

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true

Check failure on line 83 in arsdk-rs/src/ardrone3/piloting_state.rs

View workflow job for this annotation

GitHub Actions / Clippy

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
fn into(self) -> u16 {
use PilotingState::*;

Expand All @@ -91,7 +91,7 @@
NavigateHomeStateChanged => 3,
PositionChanged => 4,
SpeedChanged => 5,
AttitudeChanged => 6,
AttitudeChanged(_) => 6,
AutoTakeOffModeChanged => 7,
AltitudeChanged => 8,
GpsLocationChanged => 9,
Expand All @@ -106,6 +106,11 @@
}
}

// ------------------------------------------------------------

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct AttitudeChanged(Vec<u8>);

pub mod scroll_impl {
use super::*;
use crate::{frame::Error, parse::read_unknown};
Expand All @@ -118,8 +123,8 @@
fn try_from_ctx(src: &'a [u8], ctx: Endian) -> Result<(Self, usize), Self::Error> {
let mut offset = 0;

#[allow(clippy::match_single_binding)]
let piloting_state = match src.gread_with::<u16>(&mut offset, ctx)? {
6 => Self::AttitudeChanged(AttitudeChanged::try_from_ctx(&src[offset..], ctx)?.0),
unknown => Self::Unknown {
piloting_state: unknown,
data: read_unknown(src, &mut offset)?,
Expand All @@ -130,7 +135,7 @@
}
}

impl<'a> ctx::TryIntoCtx<Endian> for PilotingState {

Check failure on line 138 in arsdk-rs/src/ardrone3/piloting_state.rs

View workflow job for this annotation

GitHub Actions / Clippy

this lifetime isn't used in the impl

Check failure on line 138 in arsdk-rs/src/ardrone3/piloting_state.rs

View workflow job for this annotation

GitHub Actions / Clippy

this lifetime isn't used in the impl
type Error = Error;

fn try_into_ctx(self, this: &mut [u8], ctx: Endian) -> Result<usize, Self::Error> {
Expand All @@ -148,4 +153,21 @@
Ok(offset)
}
}

impl<'a> ctx::TryFromCtx<'a, Endian> for AttitudeChanged {
type Error = Error;

// and the lifetime annotation on `&'a [u8]` here
fn try_from_ctx(src: &'a [u8], ctx: Endian) -> Result<(Self, usize), Self::Error> {
let mut offset = 0;

let mut buffer = Vec::new();

while let Ok(b) = src.gread_with::<u8>(&mut offset, ctx) {
buffer.push(b);
}

Ok((Self(buffer), offset))
}
}
}
22 changes: 11 additions & 11 deletions arsdk-rs/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
/// 3. `ARCOMMANDS_Generic_DroneSettings_t` in `libARCommands/libARCommands/ARCOMMANDS_Types.h`
/// 4. `ARCOMMANDS_Generic_DroneSettingsChanged_t` in `libARCommands/libARCommands/ARCOMMANDS_Types.h`
Generic,
FollowMe, // ARCOMMANDS_ID_FEATURE_FOLLOW_ME = 134,
Wifi, // ARCOMMANDS_ID_FEATURE_WIFI = 135,
RC, // ARCOMMANDS_ID_FEATURE_RC = 136,
DroneManager, // ARCOMMANDS_ID_FEATURE_DRONE_MANAGER = 137,
Mapper, // ARCOMMANDS_ID_FEATURE_MAPPER = 138,
Debug, // ARCOMMANDS_ID_FEATURE_DEBUG = 139,
ControllerInfo, // ARCOMMANDS_ID_FEATURE_CONTROLLER_INFO = 140,
MapperMini, // ARCOMMANDS_ID_FEATURE_MAPPER_MINI = 141,
ThermalCam, // ARCOMMANDS_ID_FEATURE_THERMAL_CAM = 142,
Animation, // ARCOMMANDS_ID_FEATURE_ANIMATION = 144,
SequoiaCam, // ARCOMMANDS_ID_FEATURE_SEQUOIA_CAM = 147,
FollowMe, // ARCOMMANDS_ID_FEATURE_FOLLOW_ME = 134,
Wifi, // ARCOMMANDS_ID_FEATURE_WIFI = 135,
RC, // ARCOMMANDS_ID_FEATURE_RC = 136,
DroneManager, // ARCOMMANDS_ID_FEATURE_DRONE_MANAGER = 137,
Mapper, // ARCOMMANDS_ID_FEATURE_MAPPER = 138,
Debug, // ARCOMMANDS_ID_FEATURE_DEBUG = 139,
ControllerInfo, // ARCOMMANDS_ID_FEATURE_CONTROLLER_INFO = 140,
MapperMini, // ARCOMMANDS_ID_FEATURE_MAPPER_MINI = 141,
ThermalCam, // ARCOMMANDS_ID_FEATURE_THERMAL_CAM = 142,
Animation, // ARCOMMANDS_ID_FEATURE_ANIMATION = 144,
SequoiaCam, // ARCOMMANDS_ID_FEATURE_SEQUOIA_CAM = 147,
/// Unknown 149 from anafi4k
/// Frame { frame_type: Data, buffer_id: DCNavdata, sequence_id: 14, feature: Some(Unknown { feature: 149, data: [0, 3, 0, 91, 33] }) }
/// Unknown 148 from anafi4k
Expand All @@ -53,7 +53,7 @@

// --------------------- Conversion impls --------------------- //

impl Into<u8> for &Feature {

Check failure on line 56 in arsdk-rs/src/command.rs

View workflow job for this annotation

GitHub Actions / Clippy

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true

Check failure on line 56 in arsdk-rs/src/command.rs

View workflow job for this annotation

GitHub Actions / Clippy

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
fn into(self) -> u8 {
use Feature::*;

Expand Down
Loading
Loading