Skip to content

Commit

Permalink
Merge pull request #5 from o0Ignition0o/bebop2
Browse files Browse the repository at this point in the history
  • Loading branch information
o0Ignition0o authored May 1, 2020
2 parents c2f77d4 + 381e0bb commit 00ec6cf
Show file tree
Hide file tree
Showing 14 changed files with 399 additions and 165 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
members = [
"arsdk-rs",
"jumpingsumo-rs",
]
"bebop2",
]
104 changes: 74 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,77 @@
# Parrot sdk

<a href="CODE_OF_CONDUCT.md">
<img src="https://img.shields.io/badge/Contributor%20Covenant-v1.4%20adopted-ff69b4.svg" alt="Contributor Covenant">
</a>
<a href="LICENSE-APACHE">
<img
src="https://img.shields.io/badge/license-apache2-green.svg" alt="MPL 2.0 License">
</a>
<a href="LICENSE-MIT">
<img
src="https://img.shields.io/badge/license-mit-blue.svg" alt="MIT License">
</a>

## arsdk-rs
<a href="https://crates.io/crates/arsdk-rs">
<img src="https://img.shields.io/badge/crates.io-v0.0.4-orange.svg" alt="crates.io">
</a>
<a href="https://docs.rs/arsdk-rs">
<img src="https://docs.rs/arsdk-rs/badge.svg" alt="arsdk documentation">
</a>

## jumpingsumo-rs

<a href="https://crates.io/crates/jumpingsumo-rs">
<img src="https://img.shields.io/badge/crates.io-v0.0.1-orange.svg" alt="crates.io">
</a>
<a href="https://docs.rs/jumpingsumo-rs">
<img src="https://docs.rs/jumpingsumo-rs/badge.svg" alt="arsdk documentation">
</a>
# arsdk-rs   [![crates.io](https://img.shields.io/crates/v/arsdk-rs.svg)](https://crates.io/crates/arsdk-rs) [![Documentation](https://docs.rs/arsdk-rs/badge.svg)](https://docs.rs/arsdk-rs) [![MPL 2.0 License](https://img.shields.io/badge/license-apache2-green.svg)](LICENSE-APACHE) [![MIT License](https://img.shields.io/badge/license-mit-blue.svg)](LICENSE-MIT)
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg)](CODE_OF_CONDUCT.md)



## jumpingsumo-rs   [![crates.io](https://img.shields.io/crates/v/jumpingsumo-rs.svg)](https://crates.io/crates/jumpingsumo-rs) [![Documentation](https://docs.rs/jumpingsumo-rs/badge.svg)](https://docs.rs/jumpingsumo-rs)

## bebop2

### Not released yet

## Sphinx simulator

https://developer.parrot.com/docs/sphinx/

1. Follow the installation guide

2. Simulate drone

```bash
sphinx /opt/parrot-sphinx/usr/share/sphinx/drones/{DRONE}.drone::stolen_interface={YOUR_INTERFACE}:eth0:192.168.42.1/24
```

**NOTE:** Since we don't have video streaming handling from the drone, you should disable the front camera for `Bebeop2` (`::with_front_cam=0`).
Otherwise you won't be able to connect (performa a handshake) to it:

```bash
sphinx /opt/parrot-sphinx/usr/share/sphinx/drones/{DRONE}.drone::stolen_interface={YOUR_INTERFACE}:eth0:192.168.42.1/24::with_front_cam=0
```


* *You can find your interface with:*
```bash
iwconfig
```

* Available drones:

```bash
ls -1a /opt/parrot-sphinx/usr/share/sphinx/drones/
```

* airborne.drone
* anafi4k.drone
* bebop2.drone
* bebop.drone
* bluegrass.drone
* disco.drone
* mambo.drone
* swing.drone


3. Run examples with IP:

```rust
use arsdk-rs::PARROT_SPHINX_IP;
use std::net::{IpAddr, Ipv4Addr};
fn main() {
let expected = IpAddr::V4(Ipv4Addr::new(10, 202, 0, 1))
assert_eq!(expected, PARROT_SPHINX_IP);
}
```

### Telemetery

Documetation:
* https://developer.parrot.com/docs/sphinx/visualization.html#tlm-data-logger


```bash
tlm-data-logger inet:127.0.0.1:9060
```


## Code of Conduct

Expand Down
45 changes: 45 additions & 0 deletions arsdk-rs/src/ardrone3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::frame::Data;

/// eARCOMMANDS_ID_ARDRONE3_PILOTING_CMD
#[repr(u8)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ArDrone3 {
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_FLATTRIM = 0
FlatTrim = 0,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_TAKEOFF = 1
TakeOff = 1,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_PCMD = 2
PCMD = 2,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_LANDING = 3
Landing = 3,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_EMERGENCY = 4
Emergency = 4,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_NAVIGATEHOME = 5
NavigateHome = 5,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_AUTOTAKEOFFMODE = 6
AutoTakeOffMode = 6,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_MOVEBY = 7
MoveBy = 7,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_USERTAKEOFF = 8
UserTakeOff = 8,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_CIRCLE = 9
Circle = 9,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_MOVETO = 10
MoveTo = 10,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_CANCELMOVETO = 11
CancelMoveTo = 11,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_STARTPILOTEDPOI = 12
StartPilotdPOI = 12,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_STOPPILOTEDPOI = 13
StopPilotedPOI = 13,
}

impl Data for ArDrone3 {
fn serialize(&self) -> Vec<u8> {
// todo: Fix this hardcoded value

let take_off: u16 = 1;

take_off.to_le_bytes().to_vec()
}
}
10 changes: 7 additions & 3 deletions arsdk-rs/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::ardrone3::ArDrone3;
use crate::common;
use crate::frame::Data;
use crate::jumping_sumo;
#[derive(Debug, PartialEq, Clone, Copy)]

pub enum Feature {
Common(common::Class), // ARCOMMANDS_ID_FEATURE_COMMON = 0,
Ardrone3, // ARCOMMANDS_ID_FEATURE_ARDRONE3 = 1,
ArDrone3(ArDrone3), // ARCOMMANDS_ID_FEATURE_ARDRONE3 = 1,
Minidrone, // ARCOMMANDS_ID_FEATURE_MINIDRONE = 2,
JumpingSumo(jumping_sumo::Class), // ARCOMMANDS_ID_FEATURE_JUMPINGSUMO = 3,
SkyController, // ARCOMMANDS_ID_FEATURE_SKYCONTROLLER = 4,
Expand All @@ -30,7 +31,7 @@ impl Into<u8> for Feature {
fn into(self) -> u8 {
match self {
Self::Common(_) => 0,
Self::Ardrone3 => 1,
Self::ArDrone3(_) => 1,
Self::Minidrone => 2,
Self::JumpingSumo(_) => 3,
Self::SkyController => 4,
Expand Down Expand Up @@ -62,6 +63,9 @@ impl Data for Feature {
Feature::Common(common) => {
buf.extend(common.serialize());
}
Feature::ArDrone3(drone) => {
buf.extend(drone.serialize());
}
_ => {}
}
buf
Expand All @@ -79,7 +83,7 @@ mod command_tests {
Feature::Common(common::Class::Common(common::Common::AllStates)),
0,
);
assert_feature(Feature::Ardrone3, 1);
assert_feature(Feature::ArDrone3(ArDrone3::TakeOff), 1);
assert_feature(Feature::Minidrone, 2);
assert_feature(
Feature::JumpingSumo(jumping_sumo::Class::Piloting(
Expand Down
83 changes: 83 additions & 0 deletions arsdk-rs/src/handshake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::{
io::{Read, Write},
net::{SocketAddr, TcpStream},
};

#[derive(Serialize)]
pub(crate) struct Request {
controller_name: String,
controller_type: String,
d2c_port: u16,
#[serde(skip_serializing_if = "Option::is_some")]
arstream2_client_stream_port: Option<u16>,
#[serde(skip_serializing_if = "Option::is_some")]
arstream2_client_control_port: Option<u16>,
}

#[derive(Deserialize, Debug)]
/// Request: "{\"controller_name\":\"arsdk-rs\",\"controller_type\":\"computer\",\"d2c_port\":43210}"
/// Response: "{ \"status\": 0, \"c2d_port\": 54321, \"c2d_update_port\": 51, \"c2d_user_port\": 21, \"qos_mode\": 0, \"arstream2_server_stream_port\": 5004, \"arstream2_server_control_port\": 5005 }\u{0}"
/// `\u{0}` causes issues, but for now we `trim_end_matches`
/// Error: trailing characters at line 1 column 171
pub(crate) struct Response {
#[serde(default)]
pub arstream_fragment_maximum_number: Option<u8>,
#[serde(default)]
pub arstream_fragment_size: Option<u16>,
#[serde(default)]
pub arstream_max_ack_interval: Option<i8>,
pub arstream2_server_stream_port: u16,
pub arstream2_server_control_port: u16,
pub c2d_port: u16,
pub c2d_update_port: u16,
pub c2d_user_port: u16,
pub status: i8,
// @TODO: qos_mode: bool maybe?!
}

pub(crate) fn perform_handshake(target: SocketAddr, d2c_port: u16) -> Result<Response> {
let request = Request {
controller_name: "arsdk-rs".to_string(),
controller_type: "computer".to_string(),
d2c_port,
arstream2_client_stream_port: Some(44445),
arstream2_client_control_port: Some(44446),
};

println!(
"connecting controller {}",
request.controller_name,
);

let mut handshake_stream =
retry(10, target).ok_or_else(|| anyhow!("Couldn't connect for handshake {}", target))?;

let request_string = serde_json::to_string(&request)?;

handshake_stream.write_all(&request_string.as_bytes())?;

let mut response_string = String::new();
handshake_stream.read_to_string(&mut response_string)?;
let response_string = response_string.trim_end_matches("\u{0}");

let response: Response = serde_json::from_str(&response_string)?;

if response.status != 0 {
anyhow!("connection refused - {:?}", response);
}
Ok(response)
}

fn retry(times: usize, target: SocketAddr) -> Option<TcpStream> {
let timeout = std::time::Duration::from_secs(2);
for retry_time in 0..times {
match TcpStream::connect_timeout(&target, timeout) {
Ok(stream) => return Some(stream),
Err(err) => eprintln!("Error connecting to Tcp ({}): {}", retry_time, err),
};
}

None
}
Loading

0 comments on commit 00ec6cf

Please sign in to comment.