nt_client 0.5.1

A blazingly fast NetworkTables 4.1 client
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
//! Various `wpimath` data structures.

#[cfg(feature = "protobuf")]
use crate::protobuf::{ProtobufData, controller::*, geometry2d::*, geometry3d::*, kinematics::*, plant::*, spline::*, system::*, trajectory::*, wpimath::*};
#[cfg(feature = "protobuf")]
use nt_client_macros::ProtobufData;

#[cfg(feature = "struct")]
use crate::{schema::PublishSchemaError, r#struct::{StructData, StructSchema, byte::{ByteBuffer, ByteReader}}};
#[cfg(feature = "struct")]
use nt_client_macros::StructData;

/// Feedforward constants that model a simple arm.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double ks;double kg;double kv;double ka;double dt"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufArmFeedforward))
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ArmFeedforward {
    /// The static gain in volts.
    #[cfg_attr(feature = "protobuf", protobuf(field(ks)))]
    pub k_s: f64,
    /// The gravity gain in volts.
    #[cfg_attr(feature = "protobuf", protobuf(field(kg)))]
    pub k_g: f64,
    /// The velocity gain in V/rad/s.
    #[cfg_attr(feature = "protobuf", protobuf(field(kv)))]
    pub k_v: f64,
    /// The acceleration gain in V/rad/s².
    #[cfg_attr(feature = "protobuf", protobuf(field(ka)))]
    pub k_a: f64,
    /// The period in seconds.
    #[cfg_attr(feature = "protobuf", protobuf(field(dt)))]
    pub d_t: f64,
}

/// Robot chassis speeds.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double vx;double vy;double omega"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufChassisSpeeds)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ChassisSpeeds {
    /// The x velocity in m/s.
    #[cfg_attr(feature = "protobuf", protobuf(field(vx)))]
    pub velocity_x: f64,
    /// The y velocity in m/s.
    #[cfg_attr(feature = "protobuf", protobuf(field(vy)))]
    pub velocity_y: f64,
    /// The angular velocity in rad/s.
    #[cfg_attr(feature = "protobuf", protobuf(field(omega)))]
    pub omega: f64,
}

/// Represents a hermite spline of degree 3.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double xInitial[2];double xFinal[2];double yInitial[2];double yFinal[2]"),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CubicHermiteSpline {
    /// The control vector for the initial point in the x dimension.
    pub x_initial_control_vector: [f64; 2],
    /// The control vector for the final point in the x dimension.
    pub x_final_control_vector: [f64; 2],
    /// The control vector for the initial point in the y dimension.
    pub y_initial_control_vector: [f64; 2],
    /// The control vector for the final point in the y dimension.
    pub y_final_control_vector: [f64; 2],
}

#[cfg(feature = "protobuf")]
impl ProtobufData for CubicHermiteSpline {
    type Proto = ProtobufCubicHermiteSpline;

    fn from_proto(proto: Self::Proto) -> Option<Self> {
        Some(Self {
            x_initial_control_vector: proto.x_initial.try_into().ok()?,
            x_final_control_vector: proto.x_final.try_into().ok()?,
            y_initial_control_vector: proto.y_initial.try_into().ok()?,
            y_final_control_vector: proto.y_final.try_into().ok()?,
        })
    }

    fn into_proto(self) -> Self::Proto {
        Self::Proto {
            x_initial: self.x_initial_control_vector.into(),
            x_final: self.x_final_control_vector.into(),
            y_initial: self.y_initial_control_vector.into(),
            y_final: self.y_final_control_vector.into(),
            ..Default::default()
        }
    }
}

/// Constants for a DC motor.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double ks;double kv;double ka;double dt"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufDCMotor)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DCMotor {
    /// Voltage at which the motor constants were measured in volts.
    pub nominal_voltage: f64,
    /// Torque when stalled in newton meters.
    pub stall_torque: f64,
    /// Current draw when stalled in amps.
    pub stall_current: f64,
    /// Current draw under no load in amps.
    pub free_current: f64,
    /// Angular velocity under no load in rad/s.
    pub free_speed: f64,
}

/// Feedforward constants that model a differential drive drivetrain.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double kVLinear;double kALinear;double kVAngular;double kAAngular"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufDifferentialDriveFeedforward)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DifferentialDriveFeedforward {
    /// The linear velocity gain in V/(m/s).
    #[cfg_attr(feature = "protobuf", protobuf(field(kv_linear)))]
    pub velocity_linear: f64,
    /// The linear acceleration gain in V/(m/s²).
    #[cfg_attr(feature = "protobuf", protobuf(field(ka_linear)))]
    pub acceleration_linear: f64,
    /// The angular velocity gain in V/(rad/s).
    #[cfg_attr(feature = "protobuf", protobuf(field(kv_angular)))]
    pub velocity_angular: f64,
    /// The angular acceleration gain in V/(rad/s²).
    #[cfg_attr(feature = "protobuf", protobuf(field(ka_angular)))]
    pub acceleration_angular: f64,
}

/// Kinematics for a differential drive.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double track_width"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufDifferentialDriveKinematics)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DifferentialDriveKinematics {
    /// The differential drive track width in meters.
    pub track_width: f64,
}

/// Represents wheel positions for a differential drive drivetrain.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double left;double right")
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufDifferentialDriveWheelPositions)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DifferentialDriveWheelPositions {
    /// Distance measured by the left side.
    pub left: f64,
    /// Distance measured by the right side.
    pub right: f64,
}

/// Represents the wheel speeds for a differential drive drivetrain.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double left;double right"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufDifferentialDriveWheelSpeeds)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DifferentialDriveWheelSpeeds {
    /// Speed of the left side of the robot in m/s.
    pub left: f64,
    /// Speed of the right side of the robot in m/s.
    pub right: f64,
}

/// Represents the motor voltages for a differential drive drivetrain.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double left;double right"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufDifferentialDriveWheelVoltages)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DifferentialDriveWheelVoltages {
    /// Left wheel voltage.
    pub left: f64,
    /// Right wheel voltage.
    pub right: f64,
}

/// Feedforward constants that model a simple elevator.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double ks;double kg;double kv;double ka;double dt"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufElevatorFeedforward)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ElevatorFeedforward {
    /// The static gain in volts.
    #[cfg_attr(feature = "protobuf", protobuf(field(ks)))]
    pub k_s: f64,
    /// The gravity gain in volts.
    #[cfg_attr(feature = "protobuf", protobuf(field(kg)))]
    pub k_g: f64,
    /// The velocity gain in V/(m/s).
    #[cfg_attr(feature = "protobuf", protobuf(field(kv)))]
    pub k_v: f64,
    /// The acceleration gain in V/(m/s²).
    #[cfg_attr(feature = "protobuf", protobuf(field(ka)))]
    pub k_a: f64,
    /// The period in seconds.
    #[cfg_attr(feature = "protobuf", protobuf(field(dt)))]
    pub d_t: f64,
}

/// Represents a 2D ellipse space containing translational, rotational, and scaling components.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "Pose2d center;double xSemiAxis;double ySemiAxis"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufEllipse2d)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Ellipse2d {
    /// The center of the ellipse.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub center: Pose2d,
    /// The x semi-axis.
    #[cfg_attr(feature = "protobuf", protobuf(field(xSemiAxis)))]
    pub x_semi_axis: f64,
    /// The y semi-axis.
    #[cfg_attr(feature = "protobuf", protobuf(field(ySemiAxis)))]
    pub y_semi_axis: f64,
}

/// Exponential profile state.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double position;double velocity"),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ExponentialProfileState {
    /// The position at this state
    pub position: f64,
    /// The velocity at this state
    pub velocity: f64,
}

/// Represents a plant defined using state-space notation.
///
/// A plant is a mathematical model of a system's dynamics.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LinearSystem<const S: usize, const I: usize, const O: usize> {
    /// The system matrix A.
    pub a: Matrix<S, S>,
    /// The system matrix B.
    pub b: Matrix<S, I>,
    /// The system matrix C.
    pub c: Matrix<O, S>,
    /// The system matrix D.
    pub d: Matrix<O, I>,
}

#[cfg(feature = "struct")]
impl<const S: usize, const I: usize, const O: usize> StructData for LinearSystem<S, I, O,> {
    fn schema() -> StructSchema {
        StructSchema(format!(
            "{} a;{} b;{} c;{} d",
            <Matrix<S, S>>::struct_type_name(),
            <Matrix<S, I>>::struct_type_name(),
            <Matrix<O, S>>::struct_type_name(),
            <Matrix<O, I>>::struct_type_name(),
        ))
    }

    fn struct_type_name() -> String {
        format!("LinearSystem__{}_{}_{}", S, I, O)
    }

    async fn publish_dependencies(manager: &mut crate::schema::SchemaManager) -> Result<(), PublishSchemaError> {
        manager.publish_struct::<Matrix<S, S>>().await?;
        manager.publish_struct::<Matrix<S, I>>().await?;
        manager.publish_struct::<Matrix<O, S>>().await?;
        manager.publish_struct::<Matrix<O, I>>().await
    }

    fn pack(self, buf: &mut ByteBuffer) {
        self.a.pack(buf);
        self.b.pack(buf);
        self.c.pack(buf);
        self.d.pack(buf);
    }

    fn unpack(read: &mut ByteReader) -> Option<Self> where Self: Sized {
        let a = Matrix::unpack(read)?;
        let b = Matrix::unpack(read)?;
        let c = Matrix::unpack(read)?;
        let d = Matrix::unpack(read)?;

        Some(Self { a, b, c, d })
    }
}

#[cfg(feature = "protobuf")]
impl<const S: usize, const I: usize, const O: usize> ProtobufData for LinearSystem<S, I, O> {
    type Proto = ProtobufLinearSystem;

    fn from_proto(proto: Self::Proto) -> Option<Self> {
        Some(Self {
            a: Matrix::from_proto(proto.a.into_option().unwrap_or_default())?,
            b: Matrix::from_proto(proto.b.into_option().unwrap_or_default())?,
            c: Matrix::from_proto(proto.c.into_option().unwrap_or_default())?,
            d: Matrix::from_proto(proto.d.into_option().unwrap_or_default())?,
        })
    }

    fn into_proto(self) -> Self::Proto {
        Self::Proto {
            num_states: S as u32,
            num_inputs: I as u32,
            num_outputs: O as u32,
            a: protobuf::MessageField::some(self.a.into_proto()),
            b: protobuf::MessageField::some(self.a.into_proto()),
            c: protobuf::MessageField::some(self.a.into_proto()),
            d: protobuf::MessageField::some(self.a.into_proto()),
            ..Default::default()
        }
    }
}

/// Represents a RxC matrix.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Matrix<const R: usize, const C: usize> {
    /// The data of the matrix, indexed as `data[row][col]`.
    pub data: [[f64; C]; R],
}

#[cfg(feature = "struct")]
impl<const R: usize, const C: usize> StructData for Matrix<R, C> {
    fn schema() -> StructSchema {
        StructSchema(format!("double data[{}]", R * C))
    }

    fn struct_type_name() -> String {
        format!("Matrix__{}_{}", R, C)
    }

    async fn publish_dependencies(_: &mut crate::schema::SchemaManager) -> Result<(), PublishSchemaError> {
        Ok(())
    }

    fn pack(self, buf: &mut ByteBuffer) {
        self.data.into_iter().flatten().for_each(|value| {
            buf.write_f64(value);
        });
    }

    fn unpack(read: &mut ByteReader) -> Option<Self> {
        let mut data = [[0.0; C]; R];
        for value in data.iter_mut().flatten() {
            *value = read.read_f64()?;
        }
        Some(Self { data })
    }
}

#[cfg(feature = "protobuf")]
impl<const R: usize, const C: usize> ProtobufData for Matrix<R, C> {
    type Proto = ProtobufMatrix;

    fn from_proto(proto: Self::Proto) -> Option<Self> {
        let mut data = [[0.0; C]; R];
        if proto.data.len() != C * R { return None; };
        proto.data.into_iter()
            .enumerate()
            .for_each(|(i, num)| {
                data[i / C][i % C] = num;
            });
        Some(Self {
            data,
        })
    }

    fn into_proto(self) -> Self::Proto {
        Self::Proto {
            num_rows: R as u32,
            num_cols: C as u32,
            data: self.data.into_iter().flatten().collect(),
            ..Default::default()
        }
    }
}

/// Kinematics for a mecanum drive.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "Translation2d front_left;Translation2d front_right;Translation2d rear_left;Translation2d rear_right"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufMecanumDriveKinematics))
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MecanumDriveKinematics {
    /// The front-left wheel translation.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub front_left: Translation2d,
    /// The front-right wheel translation.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub front_right: Translation2d,
    /// The rear-right wheel translation.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub rear_left: Translation2d,
    /// The rear-left wheel translation.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub rear_right: Translation2d,
}

/// Represents the wheel positions for a mecanum drive drivetrain.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double front_left;double front_right;double rear_left;double rear_right"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufMecanumDriveWheelPositions)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MecanumDriveWheelPositions {
    /// Distance measured by the front-left wheel in meters.
    pub front_left: f64,
    /// Distance measured by the front-right wheel in meters.
    pub front_right: f64,
    /// Distance measured by the rear-left wheel in meters.
    pub rear_left: f64,
    /// Distance measured by the rear-right wheel in meters.
    pub rear_right: f64,
}

/// Represents the wheel speeds for a mecanum drive drivetrain.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double front_left;double front_right;double rear_left;double rear_right"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufMecanumDriveWheelSpeeds)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MecanumDriveWheelSpeeds {
    /// Speed of the front-left wheel in m/s.
    pub front_left: f64,
    /// Speed of the front-right wheel in m/s.
    pub front_right: f64,
    /// Speed of the rear-left wheel in m/s.
    pub rear_left: f64,
    /// Speed of the rear-right wheel in m/s.
    pub rear_right: f64,
}

/// Represents a 2D pose containing translational and rotational elements.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "Translation2d translation;Rotation2d rotation"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufPose2d)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Pose2d {
    /// The translation component of the transformation.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub translation: Translation2d,
    /// The rotational component of the transformation.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub rotation: Rotation2d,
}

/// Represents a 3D pose containing translational and rotational elements.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "Translation3d translation;Rotation3d rotation"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufPose3d)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Pose3d {
    /// The translation component of the transformation.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub translation: Translation3d,
    /// The rotational component of the transformation.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub rotation: Rotation3d,
}

/// Represents a quaternion.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double w;double x;double y;double z"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufQuaternion)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Quaternion {
    /// The w component of the quaternion.
    pub w: f64,
    /// The x component of the quaternion.
    pub x: f64,
    /// The y component of the quaternion.
    pub y: f64,
    /// The z component of the quaternion.
    pub z: f64,
}

/// Represents a hermite spline of degree 5.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double xInitial[3];double xFinal[3];double yInitial[3];double yFinal[3]"),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct QuinticHermiteSpline {
    /// The control vector for the initial point in the x dimension.
    pub x_initial: [f64; 3],
    /// The control vector for the final point in the x dimension.
    pub x_final: [f64; 3],
    /// The control vector for the initial point in the y dimension.
    pub y_initial: [f64; 3],
    /// The control vector for the final point in the y dimension.
    pub y_final: [f64; 3],
}

#[cfg(feature = "protobuf")]
impl ProtobufData for QuinticHermiteSpline {
    type Proto = ProtobufQuinticHermiteSpline;

    fn from_proto(proto: Self::Proto) -> Option<Self> {
        Some(Self {
            x_initial: proto.x_initial.try_into().ok()?,
            x_final: proto.x_final.try_into().ok()?,
            y_initial: proto.y_initial.try_into().ok()?,
            y_final: proto.y_final.try_into().ok()?,
        })
    }

    fn into_proto(self) -> Self::Proto {
        Self::Proto {
            x_initial: self.x_initial.into(),
            x_final: self.x_final.into(),
            y_initial: self.y_initial.into(),
            y_final: self.y_final.into(),
            ..Default::default()
        }
    }
}

/// Represents a 2D rectangular space containing translational, rotational, and scaling components.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "Pose2d center;double xWidth;double yWidth"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufRectangle2d)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rectangle2d {
    /// The center of the rectangle.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub center: Pose2d,
    /// The x size component of the rectangle.
    #[cfg_attr(feature = "protobuf", protobuf(field(xWidth)))]
    pub x_width: f64,
    /// The y size component of the rectangle.
    #[cfg_attr(feature = "protobuf", protobuf(field(yWidth)))]
    pub y_width: f64,
}

/// Represents a rotation in a 2D coordinate frame representd by a point on the unit circle.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double value"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufRotation2d)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rotation2d {
    /// The rotation in radians.
    pub value: f64,
}

/// Represents a rotation in a 2D coordinate frame representd by a point on the unit circle.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(type_name = "Rotation3d", schema = "Quaternion q"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufRotation3d)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rotation3d {
    /// The quaternion representation of the rotation.
    #[cfg_attr(feature = "protobuf", protobuf(field(q), nested))]
    #[cfg_attr(feature = "struct", structdata(nested))]
    pub quaternion: Quaternion,
}

/// Feedforward constants that model a simple permanent-magnet DC motor.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double ks;double kv;double ka;double dt"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufSimpleMotorFeedforward)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SimpleMotorFeedforward {
    /// The static gain in volts.
    #[cfg_attr(feature = "protobuf", protobuf(field(ks)))]
    pub k_s: f64,
    /// The velocity gain in V/(units/s).
    #[cfg_attr(feature = "protobuf", protobuf(field(kv)))]
    pub k_v: f64,
    /// The acceleration gain in V/(units/s²).
    #[cfg_attr(feature = "protobuf", protobuf(field(ka)))]
    pub k_a: f64,
    /// The period in seconds.
    #[cfg_attr(feature = "protobuf", protobuf(field(dt)))]
    pub d_t: f64,
}

/// Kinematics for a swerve drive with N modules.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SwerveDriveKinematics<const N: usize> {
    /// The swerve module locations.
    pub modules: [Translation2d; N],
}

#[cfg(feature = "struct")]
impl<const N: usize> StructData for SwerveDriveKinematics<N> {
    fn schema() -> StructSchema {
        StructSchema(format!("Translation2d modules[{N}]"))
    }

    fn struct_type_name() -> String {
        format!("SwerveDriveKinematics__{}", N)
    }

    async fn publish_dependencies(manager: &mut crate::schema::SchemaManager) -> Result<(), PublishSchemaError> {
        manager.publish_struct::<Translation2d>().await
    }

    fn pack(self, buf: &mut ByteBuffer) {
        Translation2d::pack_iter(self.modules, buf);
    }

    fn unpack(read: &mut ByteReader) -> Option<Self> {
        let modules = Translation2d::unpack_array(read)?;
        Some(Self { modules })
    }
}

#[cfg(feature = "protobuf")]
impl<const N: usize> ProtobufData for SwerveDriveKinematics<N> {
    type Proto = ProtobufSwerveDriveKinematics;

    fn from_proto(proto: Self::Proto) -> Option<Self> {
        Some(Self {
            modules: proto.modules.into_iter()
                .map(Translation2d::from_proto)
                .collect::<Option<Vec<_>>>()?
                .try_into()
                .ok()?,
        })
    }
    
    fn into_proto(self) -> Self::Proto {
        Self::Proto {
            modules: self.modules.into_iter()
                .map(|module| module.into_proto())
                .collect(),
            ..Default::default()
        }
    }
}

/// Represents the state of one swerve module.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double distance;Rotation2d angle"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufSwerveModulePosition)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SwerveModulePosition {
    /// Distance measured by the wheel of the module in meters.
    pub distance: f64,
    /// Angle of the module.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub angle: Rotation2d,
}

/// Represents the state of one swerve module.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double speed;Rotation2d angle"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufSwerveModuleState)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SwerveModuleState {
    /// The speed of the wheel of the module in m/s.
    pub speed: f64,
    /// The angle of the module.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub angle: Rotation2d,
}

/// Represents a time-parameterized trajectory. The trajectory contains of various States that represent the pose, curvature, time elapsed, velocity, and acceleration at that point.
#[derive(Debug, Clone, PartialEq)]
pub struct Trajectory {
    /// The states of the trajectory.
    pub states: Vec<TrajectoryState>,
}

#[cfg(feature = "protobuf")]
impl ProtobufData for Trajectory {
    type Proto = ProtobufTrajectory;

    fn from_proto(proto: Self::Proto) -> Option<Self> {
        Some(Self {
            states: proto.states.into_iter()
                .map(TrajectoryState::from_proto)
                .collect::<Option<_>>()?
        })
    }

    fn into_proto(self) -> Self::Proto {
        Self::Proto {
            states: self.states.into_iter()
                .map(|state| state.into_proto())
                .collect(),
            ..Default::default()
        }
    }
}

/// Represents a state at a point in a trajectory.
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufTrajectoryState)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TrajectoryState {
    /// The time elapsed since the beginning of the trajectory in seconds.
    pub time: f64,
    /// The speed at that point of the trajector in m/s.
    pub velocity: f64,
    /// The acceleration at that point of the trajectory in m/s².
    pub acceleration: f64,
    /// The pose at that point of the trajectory in meters.
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub pose: Pose2d,
    /// The curvature at that point of the trajectory in rad/m.
    pub curvature: f64,
}

/// Represents a transformation for a Pose2d in the pose's frame.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "Translation2d translation;Rotation2d rotation"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufTransform2d)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Transform2d {
    /// The translation component of the transformation.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub translation: Translation2d,
    /// The rotational component of the transformation.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub rotation: Rotation2d,
}

/// Represents a transformation for a Pose3d in the pose's frame.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "Translation3d translation;Rotation3d rotation"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufTransform3d)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Transform3d {
    /// The translation component of the transformation.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub translation: Translation3d,
    /// The rotational component of the transformation.
    #[cfg_attr(feature = "struct", structdata(nested))]
    #[cfg_attr(feature = "protobuf", protobuf(nested))]
    pub rotation: Rotation3d,
}

/// Represents a translation in 2D space.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double x;double y"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufTranslation2d)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Translation2d {
    /// The x component of the translation.
    pub x: f64,
    /// The y component of the translation.
    pub y: f64,
}

/// Represents a translation in 2D space.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double x;double y;double z"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufTranslation3d)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Translation3d {
    /// The x component of the translation.
    pub x: f64,
    /// The y component of the translation.
    pub y: f64,
    /// The z component of the translation.
    pub z: f64,
}

/// Trapezoid profile state.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double position;double velocity"),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TrapezoidProfileState {
    /// The position at this state.
    pub position: f64,
    /// The velocity at this state.
    pub velocity: f64,
}

/// Represents a change in distance along a 2D arc.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double dx;double dy;double dtheta"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufTwist2d)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Twist2d {
    /// The linear "dx" component.
    pub dx: f64,
    /// The linear "dy" component.
    pub dy: f64,
    /// The linear "dtheta" component in radians.
    pub dtheta: f64,
}

/// Represents a change in distance along a 3D arc.
#[cfg_attr(feature = "struct",
    derive(StructData),
    structdata(schema = "double dx;double dy;double dz;double rx;double ry;double rz"),
)]
#[cfg_attr(feature = "protobuf",
    derive(ProtobufData),
    protobuf(from(ProtobufTwist3d)),
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Twist3d {
    /// The linear "dx" component.
    pub dx: f64,
    /// The linear "dy" component.
    pub dy: f64,
    /// The linear "dz" component.
    pub dz: f64,
    /// The rotation vector x component in radians.
    pub rx: f64,
    /// The rotation vector y component in radians.
    pub ry: f64,
    /// The rotation vector z component in radians.
    pub rz: f64,
}

/// Represents an N-dimensional Vector.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Vector<const N: usize> {
    /// The rows of the vector.
    pub rows: [f64; N],
}

#[cfg(feature = "struct")]
impl<const N: usize> StructData for Vector<N> {
    fn schema() -> StructSchema {
        StructSchema(format!("double data[{N}]"))
    }

    fn struct_type_name() -> String {
        format!("Vector__{}", N)
    }

    async fn publish_dependencies(_: &mut crate::schema::SchemaManager) -> Result<(), PublishSchemaError> {
        Ok(())
    }

    fn pack(self, buf: &mut ByteBuffer) {
        for value in self.rows {
            buf.write_f64(value);
        }
    }

    fn unpack(read: &mut ByteReader) -> Option<Self> {
        let mut rows = [0.0; N];
        for value in rows.iter_mut() {
            *value = read.read_f64()?;
        }
        Some(Self { rows })
    }
}

#[cfg(feature = "protobuf")]
impl<const N: usize> ProtobufData for Vector<N> {
    type Proto = ProtobufVector;

    fn from_proto(proto: Self::Proto) -> Option<Self> {
        Some(Self {
            rows: proto.rows.try_into().ok()?,
        })
    }

    fn into_proto(self) -> Self::Proto {
        Self::Proto {
            rows: self.rows.into(),
            ..Default::default()
        }
    }
}