-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from o0Ignition0o/bebop2
- Loading branch information
Showing
14 changed files
with
399 additions
and
165 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
members = [ | ||
"arsdk-rs", | ||
"jumpingsumo-rs", | ||
] | ||
"bebop2", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.