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

Templates (in progress) #10

Open
wants to merge 1 commit into
base: develop
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
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ Version received: Version { flight_sw_major: 1, flight_sw_minor: 10, flight_sw_p
- `mocap`
```bash
$ cargo run --example mocap
Finished dev [unoptimized + debuginfo] target(s) in 0.31s
Running `target/debug/examples/mocap`
Finished dev [unoptimized + debuginfo] target(s) in 0.31s
Running `target/debug/examples/mocap`
All sended successfully!
```
- `telemetry`
```bash
$ cargo run --example telemery
Compiling libmavsdk v0.1.0 (/home/ildar/sw/mavsdk-rust)
Finished dev [unoptimized + debuginfo] target(s) in 4.92s
Running `target/debug/examples/telemetry`
Odometry: Odometry { time_usec: 0, frame_id: EstimNed, child_frame_id: Undef, position_body: PositionBody { x_m: 0.0, y_m: 0.0, z_m: -3.483048 }, q: Quaternion { w: 0.6384722, x: -0.004061609, y: 0.079110526, z: 0.76555747 }, speed_body: SpeedBody { velocity_x_m_s: 0.0042169667, velocity_y_m_s: -0.0015938352, velocity_z_m_s: -0.014632007 }, angular_velocity_body: AngularVelocityBody { roll_rad_s: 0.0005086092, pitch_rad_s: 0.00023366197, yaw_rad_s: -0.0002803828 }, pose_covariance: Covariance { covariance_matrix: [0.0079130605, 0.0, 0.0, 0.0, 0.0, 0.0, 0.007913225, 0.0, 0.0, 0.0, 0.0, 0.044821125, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }, velocity_covariance: Covariance { covariance_matrix: [0.0052988436, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0052990587, 0.0, 0.0, 0.0, 0.0, 0.0045366324, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] } }
Expand Down
46 changes: 28 additions & 18 deletions src/generated/telemetry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ pub enum MavFrame {
EstimNed = 18,
}

impl From<&i32> for MavFrame {
fn from(rpc_mav_frame: &i32) -> Self {
MavFrame::from_i32(*rpc_mav_frame).unwrap()
}
}

/// Odometry message type.
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Odometry {
Expand Down Expand Up @@ -138,26 +144,30 @@ pub struct Covariance {
pub covariance_matrix: ::std::vec::Vec<f32>,
}

impl From<pb::Odometry> for Odometry {
fn from(rpc_odometry: pb::Odometry) -> Odometry {
impl From<&pb::Covariance> for Covariance {
fn from(rpc_covariance: &pb::Covariance) -> Self {
Covariance {
covariance_matrix: rpc_covariance.covariance_matrix.clone(),
}
}
}

impl From<&pb::Odometry> for Odometry {
fn from(rpc_odometry: &pb::Odometry) -> Odometry {
Odometry {
time_usec: 0,
frame_id: MavFrame::from_i32(rpc_odometry.frame_id).unwrap(),
child_frame_id: MavFrame::from_i32(rpc_odometry.child_frame_id).unwrap(),
position_body: PositionBody::from(&rpc_odometry.position_body.unwrap()),
q: Quaternion::from(&rpc_odometry.q.unwrap()),
speed_body: SpeedBody::from(
&rpc_odometry.speed_body.unwrap_or(pb::SpeedBody::default()),
),
time_usec: rpc_odometry.time_usec,
frame_id: MavFrame::from(&rpc_odometry.frame_id),
child_frame_id: MavFrame::from(&rpc_odometry.child_frame_id),
position_body: PositionBody::from(rpc_odometry.position_body.as_ref().unwrap()),
q: Quaternion::from(rpc_odometry.q.as_ref().unwrap()),
speed_body: SpeedBody::from(rpc_odometry.speed_body.as_ref().unwrap()),
angular_velocity_body: AngularVelocityBody::from(
&rpc_odometry.angular_velocity_body.unwrap(),
rpc_odometry.angular_velocity_body.as_ref().unwrap(),
),
pose_covariance: Covariance::from(rpc_odometry.pose_covariance.as_ref().unwrap()),
velocity_covariance: Covariance::from(
rpc_odometry.velocity_covariance.as_ref().unwrap(),
),
pose_covariance: Covariance {
covariance_matrix: rpc_odometry.pose_covariance.unwrap().covariance_matrix,
},
velocity_covariance: Covariance {
covariance_matrix: rpc_odometry.velocity_covariance.unwrap().covariance_matrix,
},
}
}
}
Expand Down Expand Up @@ -257,7 +267,7 @@ impl Stream for OdometryStream {
Poll::Ready(None) => Poll::Ready(None),
Poll::Ready(Some(rpc_result)) => match rpc_result {
Ok(odometry_response) => match odometry_response.odometry {
Some(rpc_odometry) => Poll::Ready(Some(Ok(Odometry::from(rpc_odometry)))),
Some(rpc_odometry) => Poll::Ready(Some(Ok(Odometry::from(&rpc_odometry)))),
None => Poll::Ready(Some(Err(MavErr(TelemetryError::Unknown(
"Unexpected value".into(),
))))),
Expand Down
14 changes: 11 additions & 3 deletions templates/enum.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#[derive(PartialEq, Clone, Debug)]
pub enum Result {
// TODO
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
pub enum {{ name.upper_camel_case }} {
{% for value in values %}
/// {{ value.description }}
{{ value.name.upper_camel_case }} = {{ loop.index - 1 }}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to use actual Enum member value.

{%- endfor %}
}

impl From<&i32> for {{ nested_enum.name.upper_camel_case }} {
fn from(rpc_{{ nested_enum.name.lower_snake_case }}: &i32) -> Self {
{{ nested_enum.name.upper_camel_case }}::from_i32(rpc_{{ nested_enum.name.lower_snake_case }}).unwrap()
}
}
18 changes: 12 additions & 6 deletions templates/file.j2
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
mod {{ plugin_name.lower_snake_case }}_grpc;
mod {{ plugin_name.lower_snake_case }};
use super::super::FromRpcResponse;
use super::super::RequestError::{MavErr, RpcErr};
use super::super::RequestResult;
use super::super::TonicResult;
use futures::stream::{Stream, StreamExt};
use futures::task::{Context, Poll};
use std::convert::From;
use std::convert::Into;
use std::pin::Pin;

use {{ plugin_name.lower_snake_case }}::{{ plugin_name.upper_camel_case }}Result_Result;

impl {{ plugin_name.upper_camel_case }} {
mod pb {
include!("mavsdk.rpc.{{ plugin_name.lower_snake_case }}.rs");
}

{%- for enum in enums %}
{{ indent(enum, 1) }}
Expand All @@ -17,7 +24,6 @@ impl {{ plugin_name.upper_camel_case }} {
{{ indent(method, 1) }}
{%- endfor %}

}

impl super::super::FromChannel for {{ plugin_name.upper_camel_case}} {
fn new(channel: ::grpcio::Channel) -> Self {
Expand Down
34 changes: 32 additions & 2 deletions templates/struct.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
#[derive(PartialEq, Clone, Default, Debug)]
pub struct Version {
// TODO
pub struct {{ name.upper_camel_case }} {
{% for field in fields %}
/// {{ field.description }}
pub {{ field.name.lower_snake_case }}: {{ field.type_info.name }},
{%- endfor %}
}

{% for nested_enum in nested_enums %}

impl From<&i32> for {{ nested_enum.name.upper_camel_case }} {
fn from(rpc_{{ nested_enum.name.lower_snake_case }}: &i32) -> Self {
{{ nested_enum.name.upper_camel_case }}::from_i32(rpc_{{ nested_enum.name.lower_snake_case }}).unwrap()
}
}
{%- endfor %}

impl From<&pb::{{ name.upper_camel_case }}> for {{ name.upper_camel_case }} {
fn from(rpc_{{ lower_snake_case }}: &pb::{{ name.upper_camel_case }}) -> {{ name.upper_camel_case }} {
{{ name.upper_camel_case }} {
{% for field in fields -%}
{% if field.type_info.is_primitive %}
{{ field.name.lower_snake_case }}: {{ field.type_info.name }}::from(&rpc_{{ lower_snake_case }}.{{ field.name.lower_snake_case }}),
{% else %}
{% if field.type_info.is_repeated %}
{{ field.name.lower_snake_case }}: rpc_{{ lower_snake_case }}.{{ field.name.lower_snake_case }}.clone(),
{% else %}
{{ field.name.lower_snake_case }}: {{ field.type_info.name }}::from(&rpc_{{ lower_snake_case }}.{{ field.name.lower_snake_case }}.as_ref().unwrap()),
{% endif %}
{% endif %}
{% endfor -%}
}
}
}
18 changes: 9 additions & 9 deletions templates/type_conversions
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"double": "Double",
"float": "Float",
"int64_t": "Int64",
"uint64_t": "UInt64",
"int32_t": "Int32",
"bool": "Bool",
"double": "f64",
"float": "f32",
"int64_t": "i64",
"uint64_t": "u64",
"int32_t": "i32",
"bool": "bool",
"std::string": "String",
"std::byte": "Data",
"uint32_t": "UInt32",
"repeated": { "prefix": "[", "suffix": "]" }
"std::byte": "u8",
"uint32_t": "u32",
"repeated": { "prefix": "::std::vec::Vec<", "suffix": ">" }
}