rrtk 0.6.1

Rust Robotics ToolKit
Documentation
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright 2024 UxuginPython
//!Streams that convert from one type to another. Some of these also do keep the same type and are
//!for convenience in certain situations, for example when you do not want to handle a [`None`]
//!variant yourself.
use crate::streams::*;
///A stream converting all `Ok(None)` values from its input to `Err(_)` variants.
pub struct NoneToError<T: Clone, G: Getter<T, E> + ?Sized, E: Copy + Debug> {
    input: Reference<G>,
    phantom_t: PhantomData<T>,
    phantom_e: PhantomData<E>,
}
impl<T: Clone, G: Getter<T, E> + ?Sized, E: Copy + Debug> NoneToError<T, G, E> {
    ///Constructor for [`NoneToError`].
    pub const fn new(input: Reference<G>) -> Self {
        Self {
            input: input,
            phantom_t: PhantomData,
            phantom_e: PhantomData,
        }
    }
}
impl<T: Clone, G: Getter<T, E> + ?Sized, E: Copy + Debug> Getter<T, E> for NoneToError<T, G, E> {
    fn get(&self) -> Output<T, E> {
        let output = self.input.borrow().get()?;
        match output {
            Some(_) => {
                return Ok(output);
            }
            None => {
                return Err(Error::FromNone);
            }
        }
    }
}
impl<T: Clone, G: Getter<T, E> + ?Sized, E: Copy + Debug> Updatable<E> for NoneToError<T, G, E> {
    ///This does not need to be called.
    fn update(&mut self) -> NothingOrError<E> {
        Ok(())
    }
}
///A stream converting all `Ok(None)` values from its input to a default `Ok(Some(_))` value.
pub struct NoneToValue<
    T: Clone,
    G: Getter<T, E> + ?Sized,
    TG: TimeGetter<E> + ?Sized,
    E: Copy + Debug,
> {
    input: Reference<G>,
    time_getter: Reference<TG>,
    none_value: T,
    phantom_e: PhantomData<E>,
}
impl<T: Clone, G: Getter<T, E> + ?Sized, TG: TimeGetter<E> + ?Sized, E: Copy + Debug>
    NoneToValue<T, G, TG, E>
{
    ///Constructor for [`NoneToValue`].
    pub const fn new(input: Reference<G>, time_getter: Reference<TG>, none_value: T) -> Self {
        Self {
            input: input,
            time_getter: time_getter,
            none_value: none_value,
            phantom_e: PhantomData,
        }
    }
}
impl<T: Clone, G: Getter<T, E> + ?Sized, TG: TimeGetter<E> + ?Sized, E: Copy + Debug> Getter<T, E>
    for NoneToValue<T, G, TG, E>
{
    fn get(&self) -> Output<T, E> {
        let output = self.input.borrow().get()?;
        match output {
            Some(_) => {
                return Ok(output);
            }
            None => {
                return Ok(Some(Datum::new(
                    self.time_getter.borrow().get()?,
                    self.none_value.clone(),
                )))
            }
        }
    }
}
impl<T: Clone, G: Getter<T, E> + ?Sized, TG: TimeGetter<E> + ?Sized, E: Copy + Debug> Updatable<E>
    for NoneToValue<T, G, TG, E>
{
    fn update(&mut self) -> NothingOrError<E> {
        Ok(())
    }
}
pub use acceleration_to_state::AccelerationToState;
mod acceleration_to_state {
    use super::*;
    struct Update0 {
        last_update_time: Time,
        acc: Quantity,
        update_1: Option<Update1>,
    }
    struct Update1 {
        vel: Quantity,
        update_2: Option<Quantity>, //position
    }
    ///A stream that integrates an acceleration getter to construct a full state. Mostly useful for
    ///encoders.
    pub struct AccelerationToState<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> {
        acc: Reference<G>,
        update: Option<Update0>,
        phantom_e: PhantomData<E>,
    }
    impl<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> AccelerationToState<G, E> {
        ///Constructor for [`AccelerationToState`].
        pub const fn new(acc: Reference<G>) -> Self {
            Self {
                acc: acc,
                update: None,
                phantom_e: PhantomData,
            }
        }
    }
    impl<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> Getter<State, E>
        for AccelerationToState<G, E>
    {
        fn get(&self) -> Output<State, E> {
            match &self.update {
                Some(update_0) => match &update_0.update_1 {
                    Some(update_1) => match update_1.update_2 {
                        Some(position) => Ok(Some(Datum::new(
                            update_0.last_update_time,
                            State::new(position, update_1.vel, update_0.acc),
                        ))),
                        None => Ok(None),
                    },
                    None => Ok(None),
                },
                None => Ok(None),
            }
        }
    }
    impl<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> Updatable<E> for AccelerationToState<G, E> {
        fn update(&mut self) -> NothingOrError<E> {
            match self.acc.borrow().get() {
                Ok(gotten) => match gotten {
                    Some(new_acc_datum) => {
                        let new_time = new_acc_datum.time;
                        let new_acc = new_acc_datum.value;
                        new_acc
                            .unit
                            .assert_eq_assume_ok(&MILLIMETER_PER_SECOND_SQUARED);
                        match &self.update {
                            Some(update_0) => {
                                let old_time = update_0.last_update_time;
                                let old_acc = update_0.acc;
                                let delta_time = Quantity::from(new_time - old_time);
                                let vel_addend =
                                    (old_acc + new_acc) / Quantity::dimensionless(2.0) * delta_time;
                                match &update_0.update_1 {
                                    Some(update_1) => {
                                        let old_vel = update_1.vel;
                                        let new_vel = old_vel + vel_addend;
                                        let pos_addend = (old_vel + new_vel)
                                            / Quantity::dimensionless(2.0)
                                            * delta_time;
                                        match &update_1.update_2 {
                                            Some(old_pos) => {
                                                self.update = Some(Update0 {
                                                    last_update_time: new_time,
                                                    acc: new_acc,
                                                    update_1: Some(Update1 {
                                                        vel: new_vel,
                                                        update_2: Some(*old_pos + pos_addend),
                                                    }),
                                                })
                                            }
                                            None => {
                                                self.update = Some(Update0 {
                                                    last_update_time: new_time,
                                                    acc: new_acc,
                                                    update_1: Some(Update1 {
                                                        vel: new_vel,
                                                        update_2: Some(pos_addend),
                                                    }),
                                                })
                                            }
                                        }
                                    }
                                    None => {
                                        self.update = Some(Update0 {
                                            last_update_time: new_time,
                                            acc: new_acc,
                                            update_1: Some(Update1 {
                                                vel: vel_addend,
                                                update_2: None,
                                            }),
                                        })
                                    }
                                }
                            }
                            None => {
                                self.update = Some(Update0 {
                                    last_update_time: new_time,
                                    acc: new_acc,
                                    update_1: None,
                                });
                            }
                        }
                    }
                    None => (), //This just does nothing if the input gives a None. It does not reset
                                //it or anything.
                },
                Err(error) => {
                    self.update = None;
                    return Err(error);
                }
            }
            Ok(())
        }
    }
}
pub use velocity_to_state::VelocityToState;
mod velocity_to_state {
    use super::*;
    struct Update0 {
        last_update_time: Time,
        vel: Quantity,
        update_1: Option<Update1>,
    }
    struct Update1 {
        acc: Quantity,
        pos: Quantity,
    }
    ///A stream that integrates and derivates a velocity getter to construct a full state. Mostly
    ///useful for encoders.
    pub struct VelocityToState<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> {
        vel: Reference<G>,
        update: Option<Update0>,
        phantom_e: PhantomData<E>,
    }
    impl<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> VelocityToState<G, E> {
        ///Constructor for [`VelocityToState`].
        pub const fn new(vel: Reference<G>) -> Self {
            Self {
                vel: vel,
                update: None,
                phantom_e: PhantomData,
            }
        }
    }
    impl<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> Getter<State, E> for VelocityToState<G, E> {
        fn get(&self) -> Output<State, E> {
            match &self.update {
                Some(update_0) => match &update_0.update_1 {
                    Some(update_1) => Ok(Some(Datum::new(
                        update_0.last_update_time,
                        State::new(
                            update_1.pos.into(),
                            update_0.vel.into(),
                            update_1.acc.into(),
                        ),
                    ))),
                    None => Ok(None),
                },
                None => Ok(None),
            }
        }
    }
    impl<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> Updatable<E> for VelocityToState<G, E> {
        fn update(&mut self) -> NothingOrError<E> {
            match self.vel.borrow().get() {
                Ok(gotten) => match gotten {
                    Some(new_vel_datum) => {
                        let new_time = new_vel_datum.time;
                        let new_vel = new_vel_datum.value;
                        new_vel.unit.assert_eq_assume_ok(&MILLIMETER_PER_SECOND);
                        match &self.update {
                            Some(update_0) => {
                                let old_time = update_0.last_update_time;
                                let delta_time = Quantity::from(new_time - old_time);
                                let old_vel = update_0.vel;
                                let new_acc = (new_vel - old_vel) / delta_time;
                                let pos_addend =
                                    (old_vel + new_vel) / Quantity::dimensionless(2.0) * delta_time;
                                match &update_0.update_1 {
                                    Some(update_1) => {
                                        self.update = Some(Update0 {
                                            last_update_time: new_time,
                                            vel: new_vel,
                                            update_1: Some(Update1 {
                                                acc: new_acc,
                                                pos: update_1.pos + pos_addend,
                                            }),
                                        });
                                    }
                                    None => {
                                        self.update = Some(Update0 {
                                            last_update_time: new_time,
                                            vel: new_vel,
                                            update_1: Some(Update1 {
                                                acc: new_acc,
                                                pos: pos_addend,
                                            }),
                                        });
                                    }
                                }
                            }
                            None => {
                                self.update = Some(Update0 {
                                    last_update_time: new_time,
                                    vel: new_vel,
                                    update_1: None,
                                });
                            }
                        }
                    }
                    None => (),
                },
                Err(error) => {
                    self.update = None;
                    return Err(error);
                }
            }
            Ok(())
        }
    }
}
pub use position_to_state::PositionToState;
mod position_to_state {
    use super::*;
    struct Update0 {
        last_update_time: Time,
        pos: Quantity,
        update_1: Option<Update1>,
    }
    struct Update1 {
        vel: Quantity,
        update_2: Option<Quantity>, //acceleration
    }
    ///A stream that derivates a position getter to construct a full state. Mostly useful for encoders.
    pub struct PositionToState<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> {
        pos: Reference<G>,
        update: Option<Update0>,
        phantom_e: PhantomData<E>,
    }
    impl<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> PositionToState<G, E> {
        ///Constructor for [`PositionToState`].
        pub const fn new(pos: Reference<G>) -> Self {
            Self {
                pos: pos,
                update: None,
                phantom_e: PhantomData,
            }
        }
    }
    impl<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> Getter<State, E> for PositionToState<G, E> {
        fn get(&self) -> Output<State, E> {
            match &self.update {
                Some(update_0) => match &update_0.update_1 {
                    Some(update_1) => match update_1.update_2 {
                        Some(acc) => Ok(Some(Datum::new(
                            update_0.last_update_time,
                            State::new(update_0.pos.into(), update_1.vel.into(), acc.into()),
                        ))),
                        None => Ok(None),
                    },
                    None => Ok(None),
                },
                None => Ok(None),
            }
        }
    }
    impl<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> Updatable<E> for PositionToState<G, E> {
        fn update(&mut self) -> NothingOrError<E> {
            match self.pos.borrow().get() {
                Ok(gotten) => match gotten {
                    Some(new_pos_datum) => {
                        let new_time = new_pos_datum.time;
                        let new_pos = new_pos_datum.value;
                        new_pos.unit.assert_eq_assume_ok(&MILLIMETER);
                        match &self.update {
                            Some(update_0) => {
                                let old_time = update_0.last_update_time;
                                let delta_time = Quantity::from(new_time - old_time);
                                let old_pos = update_0.pos;
                                let new_vel = (new_pos - old_pos) / delta_time;
                                match &update_0.update_1 {
                                    Some(update_1) => {
                                        let old_vel = update_1.vel;
                                        let new_acc = (new_vel - old_vel) / delta_time;
                                        self.update = Some(Update0 {
                                            last_update_time: new_time,
                                            pos: new_pos,
                                            update_1: Some(Update1 {
                                                vel: new_vel,
                                                update_2: Some(new_acc),
                                            }),
                                        });
                                    }
                                    None => {
                                        self.update = Some(Update0 {
                                            last_update_time: new_time,
                                            pos: new_pos,
                                            update_1: Some(Update1 {
                                                vel: new_vel,
                                                update_2: None,
                                            }),
                                        });
                                    }
                                }
                            }
                            None => {
                                self.update = Some(Update0 {
                                    last_update_time: new_time,
                                    pos: new_pos,
                                    update_1: None,
                                });
                            }
                        }
                    }
                    None => (),
                },
                Err(error) => {
                    self.update = None;
                    return Err(error);
                }
            }
            Ok(())
        }
    }
}
///Stream to convert an [`f32`] to a [`Quantity`] with a given [`Unit`].
pub struct FloatToQuantity<G: Getter<f32, E> + ?Sized, E: Copy + Debug> {
    unit: Unit,
    input: Reference<G>,
    value: Output<f32, E>,
}
impl<G: Getter<f32, E>, E: Copy + Debug> FloatToQuantity<G, E> {
    ///Constructor for [`FloatToQuantity`].
    pub fn new(unit: Unit, input: Reference<G>) -> Self {
        Self {
            unit: unit,
            input: input,
            value: Ok(None),
        }
    }
}
impl<G: Getter<f32, E>, E: Copy + Debug> Updatable<E> for FloatToQuantity<G, E> {
    fn update(&mut self) -> NothingOrError<E> {
        self.value = self.input.borrow().get();
        Ok(())
    }
}
impl<G: Getter<f32, E>, E: Copy + Debug> Getter<Quantity, E> for FloatToQuantity<G, E> {
    fn get(&self) -> Output<Quantity, E> {
        match self.value {
            Err(err) => Err(err),
            Ok(None) => Ok(None),
            Ok(Some(datum)) => Ok(Some(Datum::new(
                datum.time,
                Quantity::new(datum.value, self.unit),
            ))),
        }
    }
}
///Stream to convert a [`Quantity`] to a raw [`f32`].
pub struct QuantityToFloat<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> {
    input: Reference<G>,
    value: Output<f32, E>,
}
impl<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> QuantityToFloat<G, E> {
    ///Constructor for [`QuantityToFloat`].
    pub fn new(input: Reference<G>) -> Self {
        Self {
            input: input,
            value: Ok(None),
        }
    }
}
impl<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> Getter<f32, E> for QuantityToFloat<G, E> {
    fn get(&self) -> Output<f32, E> {
        self.value
    }
}
impl<G: Getter<Quantity, E> + ?Sized, E: Copy + Debug> Updatable<E> for QuantityToFloat<G, E> {
    fn update(&mut self) -> NothingOrError<E> {
        let gotten = self.input.borrow().get();
        self.value = match gotten {
            Err(error) => Err(error),
            Ok(None) => Ok(None),
            Ok(Some(datum)) => Ok(Some(Datum::new(datum.time, datum.value.value))),
        };
        Ok(())
    }
}