-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmod.rs
More file actions
291 lines (264 loc) · 9.07 KB
/
mod.rs
File metadata and controls
291 lines (264 loc) · 9.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use super::super::RequestError::{MavErr, RpcErr};
use super::super::RequestResult;
use futures::stream::{Stream, StreamExt};
use futures::task::{Context, Poll};
use std::convert::From;
use std::pin::Pin;
mod pb {
include!("mavsdk.rpc.telemetry.rs");
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
pub enum MavFrame {
Undef = 0,
/// Setpoint in body NED frame. This makes sense if all position control is
/// externalized - e.g. useful to command 2 m/s^2 acceleration to the right.
BodyNed = 8,
/// Odometry local coordinate frame of data given by a vision estimation system,
/// Z-down (x: north, y: east, z: down).
VisionNed = 16,
/// Odometry local coordinate frame of data given by an estimator running
/// onboard the vehicle, Z-down (x: north, y: east, z: down).
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 {
/// Timestamp (0 to use Backend timestamp).
pub time_usec: u64,
/// Coordinate frame of reference for the pose data.
pub frame_id: MavFrame,
/// Coordinate frame of reference for the velocity in free space (twist) data.
pub child_frame_id: MavFrame,
/// Position.
pub position_body: PositionBody,
/// Quaternion components, w, x, y, z (1 0 0 0 is the null-rotation).
pub q: Quaternion,
/// Linear speed (m/s).
pub speed_body: SpeedBody,
/// Angular speed (rad/s).
pub angular_velocity_body: AngularVelocityBody,
/// Pose cross-covariance matrix.
pub pose_covariance: Covariance,
/// Velocity cross-covariance matrix.
pub velocity_covariance: Covariance,
}
/// Body position type
#[derive(Clone, PartialEq, Debug, Default)]
pub struct PositionBody {
/// X position in metres.
pub x_m: f32,
/// Y position in metres.
pub y_m: f32,
/// Z position in metres.
pub z_m: f32,
}
impl From<&pb::PositionBody> for PositionBody {
fn from(rpc_position_body: &pb::PositionBody) -> Self {
PositionBody {
x_m: rpc_position_body.x_m,
y_m: rpc_position_body.y_m,
z_m: rpc_position_body.z_m,
}
}
}
/// Speed type, represented in the Body (X Y Z) frame and in metres/second.
#[derive(Clone, PartialEq, Debug, Default)]
pub struct SpeedBody {
/// Velocity in X in metres/second.
pub velocity_x_m_s: f32,
/// Velocity in Y in metres/second.
pub velocity_y_m_s: f32,
/// Velocity in Z in metres/second.
pub velocity_z_m_s: f32,
}
impl From<&pb::SpeedBody> for SpeedBody {
fn from(rpc_speed_body: &pb::SpeedBody) -> Self {
SpeedBody {
velocity_x_m_s: rpc_speed_body.velocity_x_m_s,
velocity_y_m_s: rpc_speed_body.velocity_y_m_s,
velocity_z_m_s: rpc_speed_body.velocity_z_m_s,
}
}
}
impl Into<pb::SpeedBody> for SpeedBody {
fn into(self) -> pb::SpeedBody {
pb::SpeedBody {
velocity_x_m_s: self.velocity_x_m_s,
velocity_y_m_s: self.velocity_y_m_s,
velocity_z_m_s: self.velocity_z_m_s,
}
}
}
/// Angular velocity type
#[derive(Clone, PartialEq, Debug, Default)]
pub struct AngularVelocityBody {
/// Roll angular velocity in radians/second.
pub roll_rad_s: f32,
/// Pitch angular velocity in radians/second.
pub pitch_rad_s: f32,
/// Yaw angular velocity in radians/second.
pub yaw_rad_s: f32,
}
impl From<&pb::AngularVelocityBody> for AngularVelocityBody {
fn from(rpc_angular_velocity_body: &pb::AngularVelocityBody) -> Self {
AngularVelocityBody {
roll_rad_s: rpc_angular_velocity_body.roll_rad_s,
pitch_rad_s: rpc_angular_velocity_body.pitch_rad_s,
yaw_rad_s: rpc_angular_velocity_body.yaw_rad_s,
}
}
}
impl Into<pb::AngularVelocityBody> for AngularVelocityBody {
fn into(self) -> pb::AngularVelocityBody {
pb::AngularVelocityBody {
roll_rad_s: self.roll_rad_s,
pitch_rad_s: self.pitch_rad_s,
yaw_rad_s: self.yaw_rad_s,
}
}
}
/// Covariance type.
/// Row-major representation of a 6x6 cross-covariance matrix
/// upper right triangle.
/// Set first to NaN if unknown.
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Covariance {
pub covariance_matrix: ::std::vec::Vec<f32>,
}
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: 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.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(),
),
}
}
}
impl Into<pb::Covariance> for Covariance {
fn into(self) -> pb::Covariance {
pb::Covariance {
covariance_matrix: self.covariance_matrix,
}
}
}
///
/// Quaternion type.
///
/// All rotations and axis systems follow the right-hand rule.
/// The Hamilton quaternion product definition is used.
/// A zero-rotation quaternion is represented by (1,0,0,0).
/// The quaternion could also be written as w + xi + yj + zk.
///
/// For more info see: https://en.wikipedia.org/wiki/Quaternion
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Quaternion {
/// Quaternion entry 0, also denoted as a
pub w: f32,
/// Quaternion entry 1, also denoted as b
pub x: f32,
/// Quaternion entry 2, also denoted as c
pub y: f32,
/// Quaternion entry 3, also denoted as d
pub z: f32,
}
impl From<&pb::Quaternion> for Quaternion {
fn from(rpc_quaternion: &pb::Quaternion) -> Self {
Quaternion {
w: rpc_quaternion.w,
x: rpc_quaternion.x,
y: rpc_quaternion.y,
z: rpc_quaternion.z,
}
}
}
impl Into<pb::Quaternion> for Quaternion {
fn into(self) -> pb::Quaternion {
pb::Quaternion {
w: self.w,
x: self.x,
y: self.y,
z: self.z,
}
}
}
#[derive(Clone, Debug)]
pub enum TelemetryError {
/// Unknown error
Unknown(String),
/// No system is connected
NoSystem(String),
/// Connection error
ConnectionError(String),
/// Invalid request data
InvalidRequestData(String),
}
#[doc = ""]
#[doc = " Allow users to get vehicle telemetry and state information"]
#[doc = " (e.g. battery, GPS, RC connection, flight mode etc.) and set telemetry update rates."]
pub struct Telemetry {
service_client: pb::telemetry_service_client::TelemetryServiceClient<tonic::transport::Channel>,
}
impl Telemetry {
pub async fn subscribe_odometry(&mut self) -> Result<OdometryStream, tonic::Status> {
let request = pb::SubscribeOdometryRequest {};
let response = self.service_client.subscribe_odometry(request).await?;
Ok(OdometryStream {
streaming: response.into_inner(),
})
}
}
pub struct OdometryStream {
streaming: tonic::codec::Streaming<pb::OdometryResponse>,
}
pub type OdometryResult = RequestResult<Odometry, TelemetryError>;
impl Stream for OdometryStream {
type Item = OdometryResult;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.streaming.poll_next_unpin(cx) {
Poll::Pending => Poll::Pending,
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)))),
None => Poll::Ready(Some(Err(MavErr(TelemetryError::Unknown(
"Unexpected value".into(),
))))),
},
Err(err) => Poll::Ready(Some(Err(RpcErr(err)))),
},
}
}
}
#[tonic::async_trait]
impl super::super::Connect for Telemetry {
async fn connect(url: &String) -> std::result::Result<Telemetry, tonic::transport::Error> {
match pb::telemetry_service_client::TelemetryServiceClient::connect(url.clone()).await {
Ok(client) => Ok(Telemetry {
service_client: client,
}),
Err(err) => Err(err),
}
}
}