openrr_tracing/
de.rs

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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Deserialize tracing log in JSON format

use std::time::{Duration, SystemTime};

use arci::{nalgebra, BaseVelocity};
use serde::Deserialize;

pub type Timestamp = chrono::DateTime<chrono::Utc>;

pub fn from_str(lines: &str) -> Result<Vec<TracingLog>, arci::Error> {
    let mut res = vec![];
    for line in lines.lines() {
        let value: serde_json::Value =
            serde_json::from_str(line).map_err(|e| arci::Error::Other(e.into()))?;
        // ignore unrelated line and log for other target (library/binary/module).
        if !matches!(value.get("target"), Some(target) if target == "openrr_tracing") {
            continue;
        }
        let Some(fields) = value.get("fields") else {
            continue;
        };
        match fields.get("method") {
            Some(v) if v == "arci::Localization::current_pose" => {
                let log: CurrentPoseLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::CurrentPose(log));
            }
            Some(v) if v == "arci::MotorDriveEffort::set_motor_effort" => {
                let log: SetMotorEffortLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::SetMotorEffort(log));
            }
            Some(v) if v == "arci::MotorDriveEffort::get_motor_effort" => {
                let log: GetMotorEffortLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::GetMotorEffort(log));
            }
            Some(v) if v == "arci::MotorDrivePosition::set_motor_position" => {
                let log: SetMotorPositionLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::SetMotorPosition(log));
            }
            Some(v) if v == "arci::MotorDrivePosition::get_motor_position" => {
                let log: GetMotorPositionLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::GetMotorPosition(log));
            }
            Some(v) if v == "arci::MotorDriveVelocity::set_motor_velocity" => {
                let log: SetMotorVelocityLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::SetMotorVelocity(log));
            }
            Some(v) if v == "arci::MotorDriveVelocity::get_motor_velocity" => {
                let log: GetMotorVelocityLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::GetMotorVelocity(log));
            }
            Some(v) if v == "arci::MoveBase::send_velocity" => {
                let log: SendVelocityLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::SendVelocity(log));
            }
            Some(v) if v == "arci::MoveBase::current_velocity" => {
                let log: CurrentVelocityLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::CurrentVelocity(log));
            }
            Some(v) if v == "arci::Navigation::send_goal_pose" => {
                let log: SendGoalPoseLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::SendGoalPose(log));
            }
            Some(v) if v == "arci::Navigation::cancel" => {
                let log: NavigationCancelLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::NavigationCancel(log));
            }
            Some(v) if v == "arci::Speaker::speak" => {
                let log: SpeakLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::Speak(log));
            }
            Some(v) if v == "arci::TransformResolver::resolve_transformation" => {
                let log: ResolveTransformationLog =
                    serde_json::from_value(value).map_err(|e| arci::Error::Other(e.into()))?;
                res.push(TracingLog::ResolveTransformation(log));
            }
            _ => continue, // TODO
        }
    }
    Ok(res)
}

#[derive(Debug)]
#[non_exhaustive]
pub enum TracingLog {
    /// [`arci::Localization::current_pose`]
    CurrentPose(CurrentPoseLog),

    /// [`arci::MotorDriveEffort::set_motor_effort`]
    SetMotorEffort(SetMotorEffortLog),
    /// [`arci::MotorDriveEffort::get_motor_effort`]
    GetMotorEffort(GetMotorEffortLog),

    /// [`arci::MotorDrivePosition::set_motor_position`]
    SetMotorPosition(SetMotorPositionLog),
    /// [`arci::MotorDrivePosition::get_motor_position`]
    GetMotorPosition(GetMotorPositionLog),

    /// [`arci::MotorDriveVelocity::set_motor_velocity`]
    SetMotorVelocity(SetMotorVelocityLog),
    /// [`arci::MotorDriveVelocity::get_motor_velocity`]
    GetMotorVelocity(GetMotorVelocityLog),

    /// [`arci::MoveBase::send_velocity`]
    SendVelocity(SendVelocityLog),
    /// [`arci::MoveBase::current_velocity`]
    CurrentVelocity(CurrentVelocityLog),

    /// [`arci::Navigation::send_goal_pose`]
    SendGoalPose(SendGoalPoseLog),
    /// [`arci::Navigation::cancel`]
    NavigationCancel(NavigationCancelLog),

    /// [`arci::Speaker::speak`]
    Speak(SpeakLog),

    /// [`arci::TransformResolver::resolve_transformation`]
    ResolveTransformation(ResolveTransformationLog),
}

#[derive(Deserialize)]
struct RawTracingLog<Fields> {
    timestamp: Timestamp,
    fields: Fields,
}

// =============================================================================
// arci::Localization

#[derive(Debug)]
#[non_exhaustive]
pub struct CurrentPoseLog {
    pub timestamp: Timestamp,
    pub frame_id: String,
    pub pose: arci::Isometry2<f64>,
}

impl<'de> Deserialize<'de> for CurrentPoseLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct Fields {
            frame_id: String,
            pose_rotation_re: f64,
            pose_rotation_im: f64,
            pose_translation_x: f64,
            pose_translation_y: f64,
        }
        let v = RawTracingLog::<Fields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
            frame_id: v.fields.frame_id,
            pose: arci::Isometry2::from_parts(
                nalgebra::Translation2::new(
                    v.fields.pose_translation_x,
                    v.fields.pose_translation_y,
                ),
                nalgebra::UnitComplex::from_complex(nalgebra::Complex {
                    re: v.fields.pose_rotation_re,
                    im: v.fields.pose_rotation_im,
                }),
            ),
        })
    }
}

// =============================================================================
// arci::MotorDriveEffort

// Currently, both SetMotorEffortLog and GetMotorEffortLog are the same.
// However, we may want to add result field to SetMotorEffortLog.
#[derive(Debug)]
#[non_exhaustive]
pub struct SetMotorEffortLog {
    pub timestamp: Timestamp,
    pub effort: f64,
}
#[derive(Debug)]
#[non_exhaustive]
pub struct GetMotorEffortLog {
    pub timestamp: Timestamp,
    pub effort: f64,
}

#[derive(Deserialize)]
struct MotorEffortLogFields {
    effort: f64,
}
impl<'de> Deserialize<'de> for SetMotorEffortLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let v = RawTracingLog::<MotorEffortLogFields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
            effort: v.fields.effort,
        })
    }
}
impl<'de> Deserialize<'de> for GetMotorEffortLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let v = RawTracingLog::<MotorEffortLogFields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
            effort: v.fields.effort,
        })
    }
}

// =============================================================================
// arci::MotorDrivePosition

// Currently, both SetMotorPositionLog and GetMotorPositionLog are the same.
// However, we may want to add result field to SetMotorPositionLog.
#[derive(Debug)]
#[non_exhaustive]
pub struct SetMotorPositionLog {
    pub timestamp: Timestamp,
    pub position: f64,
}
#[derive(Debug)]
#[non_exhaustive]
pub struct GetMotorPositionLog {
    pub timestamp: Timestamp,
    pub position: f64,
}

#[derive(Deserialize)]
struct MotorPositionLogFields {
    position: f64,
}
impl<'de> Deserialize<'de> for SetMotorPositionLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let v = RawTracingLog::<MotorPositionLogFields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
            position: v.fields.position,
        })
    }
}
impl<'de> Deserialize<'de> for GetMotorPositionLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let v = RawTracingLog::<MotorPositionLogFields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
            position: v.fields.position,
        })
    }
}

// =============================================================================
// arci::MotorDriveVelocity

// Currently, both SetMotorVelocityLog and GetMotorVelocityLog are the same.
// However, we may want to add result field to SetMotorVelocityLog.
#[derive(Debug)]
#[non_exhaustive]
pub struct SetMotorVelocityLog {
    pub timestamp: Timestamp,
    pub velocity: f64,
}
#[derive(Debug)]
#[non_exhaustive]
pub struct GetMotorVelocityLog {
    pub timestamp: Timestamp,
    pub velocity: f64,
}

#[derive(Deserialize)]
struct MotorVelocityLogFields {
    velocity: f64,
}
impl<'de> Deserialize<'de> for SetMotorVelocityLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let v = RawTracingLog::<MotorVelocityLogFields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
            velocity: v.fields.velocity,
        })
    }
}
impl<'de> Deserialize<'de> for GetMotorVelocityLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let v = RawTracingLog::<MotorVelocityLogFields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
            velocity: v.fields.velocity,
        })
    }
}

// =============================================================================
// arci::MoveBase

// Currently, both SendVelocityLog and CurrentVelocityLog are the same.
// However, we may want to add result field to SendVelocityLog.
#[derive(Debug)]
#[non_exhaustive]
pub struct SendVelocityLog {
    pub timestamp: Timestamp,
    pub velocity: BaseVelocity,
}
#[derive(Debug)]
#[non_exhaustive]
pub struct CurrentVelocityLog {
    pub timestamp: Timestamp,
    pub velocity: BaseVelocity,
}

#[derive(Deserialize)]
struct VelocityLogFields {
    velocity_x: f64,
    velocity_y: f64,
    velocity_theta: f64,
}
impl<'de> Deserialize<'de> for SendVelocityLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let v = RawTracingLog::<VelocityLogFields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
            velocity: BaseVelocity {
                x: v.fields.velocity_x,
                y: v.fields.velocity_y,
                theta: v.fields.velocity_theta,
            },
        })
    }
}
impl<'de> Deserialize<'de> for CurrentVelocityLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let v = RawTracingLog::<VelocityLogFields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
            velocity: BaseVelocity {
                x: v.fields.velocity_x,
                y: v.fields.velocity_y,
                theta: v.fields.velocity_theta,
            },
        })
    }
}

// =============================================================================
// arci::Navigation

#[derive(Debug)]
#[non_exhaustive]
pub struct SendGoalPoseLog {
    pub timestamp: Timestamp,
    pub goal: arci::Isometry2<f64>,
    pub frame_id: String,
    pub timeout: Duration,
}
#[derive(Debug)]
#[non_exhaustive]
pub struct NavigationCancelLog {
    pub timestamp: Timestamp,
}

impl<'de> Deserialize<'de> for SendGoalPoseLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct Fields {
            goal_rotation_re: f64,
            goal_rotation_im: f64,
            goal_translation_x: f64,
            goal_translation_y: f64,
            frame_id: String,
            timeout_secs: u64,
            timeout_nanos: u32,
        }
        let v = RawTracingLog::<Fields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
            goal: arci::Isometry2::from_parts(
                nalgebra::Translation2::new(
                    v.fields.goal_translation_x,
                    v.fields.goal_translation_y,
                ),
                nalgebra::UnitComplex::from_complex(nalgebra::Complex {
                    re: v.fields.goal_rotation_re,
                    im: v.fields.goal_rotation_im,
                }),
            ),
            frame_id: v.fields.frame_id,
            timeout: Duration::new(v.fields.timeout_secs, v.fields.timeout_nanos),
        })
    }
}
impl<'de> Deserialize<'de> for NavigationCancelLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct Fields {}
        let v = RawTracingLog::<Fields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
        })
    }
}

// =============================================================================
// arci::Speaker

#[derive(Debug)]
#[non_exhaustive]
pub struct SpeakLog {
    pub timestamp: Timestamp,
    pub message: String,
}

impl<'de> Deserialize<'de> for SpeakLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct Fields {
            message: String,
        }
        let v = RawTracingLog::<Fields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
            message: v.fields.message,
        })
    }
}

// =============================================================================
// arci::TransformResolver

#[derive(Debug)]
#[non_exhaustive]
pub struct ResolveTransformationLog {
    pub timestamp: Timestamp,
    pub from: String,
    pub to: String,
    pub time: SystemTime,
}

impl<'de> Deserialize<'de> for ResolveTransformationLog {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct Fields {
            from: String,
            to: String,
            time_secs: u64,
            time_nanos: u32,
        }
        let v = RawTracingLog::<Fields>::deserialize(deserializer)?;
        Ok(Self {
            timestamp: v.timestamp,
            from: v.fields.from,
            to: v.fields.to,
            time: SystemTime::UNIX_EPOCH + Duration::new(v.fields.time_secs, v.fields.time_nanos),
        })
    }
}