moteus 0.5.0

Rust client library for moteus brushless motor controllers
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
// Copyright 2026 mjbots Robotic Systems, LLC.  info@mjbots.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Controller API for moteus devices.

use crate::command_types::Command;
use crate::device_address::DeviceAddress;
use moteus_protocol::command::{
    AuxPwmCommand, AuxPwmFormat, BrakeCommand, CurrentCommand, CurrentFormat, OutputExactCommand,
    OutputNearestCommand, PositionCommand, PositionFormat, RecapturePositionVelocityCommand,
    RequireReindexCommand, SetTrimCommand, StayWithinCommand, StayWithinFormat, StopCommand,
    VFOCCommand, VFOCFormat, ZeroVelocityCommand, ZeroVelocityFormat,
};
use moteus_protocol::query::{QueryFormat, QueryResult};
use moteus_protocol::{CanFdFrame, Register, Resolution, WriteCanData, WriteCombiner};

/// Controller for a single moteus device.
///
/// This struct provides methods for building commands (make_* methods)
/// that can be sent to a moteus controller. The actual transport is handled
/// separately, allowing the same Controller to be used with different backends.
///
/// # Example
///
/// ```rust
/// use moteus::Controller;
///
/// let controller = Controller::new(1);
///
/// // Build a stop command
/// let cmd = controller.make_stop(false);
/// let frame = cmd.into_frame(); // Convert to wire frame for transport
/// ```
pub struct Controller {
    /// Device address (CAN ID or UUID)
    pub address: DeviceAddress,
    /// CAN bus ID used for framing (resolved from address).
    /// For UUID-only addresses this is 0x7F (broadcast).
    pub id: u8,
    /// Source CAN ID for this client
    pub source_id: u8,
    /// 13-bit CAN prefix
    pub can_prefix: u16,
    /// Query resolution format
    pub query_format: QueryFormat,
    /// Position command format
    pub position_format: PositionFormat,
    /// VFOC command format
    pub vfoc_format: VFOCFormat,
    /// Current command format
    pub current_format: CurrentFormat,
    /// Stay-within command format
    pub stay_within_format: StayWithinFormat,
    /// Zero-velocity command format
    pub zero_velocity_format: ZeroVelocityFormat,
    /// AUX PWM command format
    pub aux_pwm_format: AuxPwmFormat,

    /// UUID prefix data prepended to every command frame when addressing
    /// by UUID. Empty when addressing by CAN ID.
    uuid_prefix_data: Vec<u8>,
}

impl Controller {
    /// Creates a new Controller for the given device address.
    ///
    /// # Arguments
    /// * `address` - Device address (CAN ID, UUID, or both). Integers are
    ///   automatically converted to CAN ID addresses. UUID-only
    ///   addresses use 0x7F (broadcast) as the CAN ID, with
    ///   UUID mask prefix data prepended to each command frame.
    ///
    /// # Example
    ///
    /// ```rust
    /// use moteus::{Controller, DeviceAddress};
    /// use moteus_protocol::query::QueryFormat;
    ///
    /// // Using integer CAN ID (most common)
    /// let controller = Controller::new(1);
    ///
    /// // Using explicit DeviceAddress
    /// let controller = Controller::new(DeviceAddress::can_id(1));
    ///
    /// // Using UUID address
    /// let uuid = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    ///             0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10];
    /// let controller = Controller::new(DeviceAddress::uuid(uuid));
    ///
    /// // With builder pattern
    /// let controller = Controller::new(1)
    ///     .source_id(0x10)
    ///     .query_format(QueryFormat::comprehensive());
    /// ```
    pub fn new(address: impl Into<DeviceAddress>) -> Self {
        let address = address.into();
        let id = address.as_can_id().unwrap_or(0x7F);

        let uuid_prefix_data = if address.can_id.is_none() {
            if let Some(uuid) = address.as_uuid() {
                crate::transport::make_uuid_prefix(uuid)
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };

        Controller {
            address,
            id,
            source_id: 0,
            can_prefix: 0x0000,
            query_format: QueryFormat::default(),
            position_format: PositionFormat::default(),
            vfoc_format: VFOCFormat::default(),
            current_format: CurrentFormat::default(),
            stay_within_format: StayWithinFormat::default(),
            zero_velocity_format: ZeroVelocityFormat::default(),
            aux_pwm_format: AuxPwmFormat::default(),
            uuid_prefix_data,
        }
    }

    /// Sets the source CAN ID (builder pattern).
    #[must_use]
    pub fn source_id(mut self, id: u8) -> Self {
        self.source_id = id;
        self
    }

    /// Sets the CAN prefix (builder pattern).
    #[must_use]
    pub fn can_prefix(mut self, prefix: u16) -> Self {
        self.can_prefix = prefix & 0x1fff;
        self
    }

    /// Sets the query format (builder pattern).
    #[must_use]
    pub fn query_format(mut self, format: QueryFormat) -> Self {
        self.query_format = format;
        self
    }

    /// Sets the position command format (builder pattern).
    #[must_use]
    pub fn position_format(mut self, format: PositionFormat) -> Self {
        self.position_format = format;
        self
    }

    /// Sets the VFOC command format (builder pattern).
    #[must_use]
    pub fn vfoc_format(mut self, format: VFOCFormat) -> Self {
        self.vfoc_format = format;
        self
    }

    /// Sets the current command format (builder pattern).
    #[must_use]
    pub fn current_format(mut self, format: CurrentFormat) -> Self {
        self.current_format = format;
        self
    }

    /// Sets the stay-within command format (builder pattern).
    #[must_use]
    pub fn stay_within_format(mut self, format: StayWithinFormat) -> Self {
        self.stay_within_format = format;
        self
    }

    /// Sets the zero-velocity command format (builder pattern).
    #[must_use]
    pub fn zero_velocity_format(mut self, format: ZeroVelocityFormat) -> Self {
        self.zero_velocity_format = format;
        self
    }

    /// Sets the AUX PWM command format (builder pattern).
    #[must_use]
    pub fn aux_pwm_format(mut self, format: AuxPwmFormat) -> Self {
        self.aux_pwm_format = format;
        self
    }

    /// Prepares a Command with common routing fields set.
    ///
    /// If the device address has a `transport_device` set, it is propagated
    /// to the command's `channel` field for direct transport routing.
    ///
    /// For UUID-addressed controllers, the UUID mask prefix data is
    /// pre-filled into the frame so that subsequent serialization appends
    /// after it.
    pub(crate) fn prepare_command(&self, query: bool) -> Command {
        let mut cmd = Command::new(self.id as i8, self.source_id as i8, self.can_prefix)
            .reply_required(query);
        cmd.channel = self.address.transport_device;
        cmd.address = Some(self.address.clone());

        if query {
            cmd.reply_filter = Some(Command::query_reply_filter());
        }

        if !self.uuid_prefix_data.is_empty() {
            let frame = cmd.frame_mut();
            let len = self.uuid_prefix_data.len();
            frame.data[..len].copy_from_slice(&self.uuid_prefix_data);
            frame.size = len as u8;
        }

        cmd
    }

    // === Query Methods ===

    /// Builds a query-only command.
    ///
    /// Returns a command that requests telemetry data from the controller
    /// without sending any motor commands.
    pub fn make_query(&self) -> Command {
        self.make_query_with_format(&self.query_format)
    }

    /// Builds a query command with a custom format.
    pub fn make_query_with_format(&self, format: &QueryFormat) -> Command {
        let mut cmd = self.prepare_command(true);
        cmd.expected_reply_size = format.serialize(cmd.frame_mut());
        cmd
    }

    /// Parses a query response from a received frame.
    pub fn parse_query(&self, frame: &CanFdFrame) -> QueryResult {
        QueryResult::parse(frame)
    }

    // === Stop/Brake Methods ===

    /// Builds a stop command.
    ///
    /// Stop mode disables the motor output, allowing it to coast.
    ///
    /// # Arguments
    /// * `query` - If true, also request telemetry in the response
    pub fn make_stop(&self, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        StopCommand::serialize(cmd.frame_mut());
        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    /// Builds a brake command.
    ///
    /// Brake mode shorts the motor windings for active braking.
    pub fn make_brake(&self, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        BrakeCommand::serialize(cmd.frame_mut());
        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    // === Position Mode Methods ===

    /// Builds a position mode command.
    ///
    /// This is the primary control mode for moteus.
    ///
    /// # Arguments
    /// * `cmd` - Position command built with the builder pattern
    /// * `query` - If true, also request telemetry in the response
    ///
    /// # Example
    ///
    /// ```rust
    /// use moteus::Controller;
    /// use moteus_protocol::command::PositionCommand;
    ///
    /// let controller = Controller::new(1);
    /// let cmd = controller.make_position_command(
    ///     &PositionCommand::new().position(0.5).velocity(1.0),
    ///     true,
    /// );
    /// ```
    pub fn make_position_command(&self, pos_cmd: &PositionCommand, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        pos_cmd.serialize(cmd.frame_mut(), &self.position_format);
        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    // === Current Mode Methods ===

    /// Builds a current (torque) mode command.
    ///
    /// # Arguments
    /// * `cur_cmd` - Current command built with the builder pattern
    /// * `query` - If true, also request telemetry
    pub fn make_current_command(&self, cur_cmd: &CurrentCommand, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        cur_cmd.serialize(cmd.frame_mut(), &self.current_format);
        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    // === VFOC Mode Methods ===

    /// Builds a voltage FOC mode command.
    ///
    /// # Arguments
    /// * `vfoc_cmd` - VFOC command built with the builder pattern
    /// * `query` - If true, also request telemetry
    pub fn make_vfoc_command(&self, vfoc_cmd: &VFOCCommand, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        vfoc_cmd.serialize(cmd.frame_mut(), &self.vfoc_format);
        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    // === Stay-Within Mode Methods ===

    /// Builds a stay-within mode command.
    ///
    /// # Arguments
    /// * `sw_cmd` - Stay-within command built with the builder pattern
    /// * `query` - If true, also request telemetry
    pub fn make_stay_within_command(&self, sw_cmd: &StayWithinCommand, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        sw_cmd.serialize(cmd.frame_mut(), &self.stay_within_format);
        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    // === Zero-Velocity Mode Methods ===

    /// Builds a zero-velocity mode command.
    ///
    /// # Arguments
    /// * `zv_cmd` - Zero-velocity command built with the builder pattern
    /// * `query` - If true, also request telemetry
    pub fn make_zero_velocity_command(&self, zv_cmd: &ZeroVelocityCommand, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        zv_cmd.serialize(cmd.frame_mut(), &self.zero_velocity_format);
        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    // === Output Position Methods ===

    /// Builds a set-output-nearest command.
    ///
    /// Sets the output position to the nearest value matching the given position.
    pub fn make_set_output_nearest(&self, position: f32, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        OutputNearestCommand::new(position).serialize(cmd.frame_mut());
        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    /// Builds a set-output-exact command.
    ///
    /// Sets the output position to exactly the given value.
    pub fn make_set_output_exact(&self, position: f32, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        OutputExactCommand::new(position).serialize(cmd.frame_mut());
        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    /// Builds a require-reindex command.
    pub fn make_require_reindex(&self, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        RequireReindexCommand::serialize(cmd.frame_mut());
        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    /// Builds a recapture-position-velocity command.
    pub fn make_recapture_position_velocity(&self, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        RecapturePositionVelocityCommand::serialize(cmd.frame_mut());
        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    // === GPIO Methods ===

    /// Builds a command to read GPIO digital inputs.
    ///
    /// The response will contain `aux1_gpio` and `aux2_gpio` values in the QueryResult.
    /// Each bit represents a pin's state, with bit 0 being pin 0.
    pub fn make_read_gpio(&self) -> Command {
        let mut cmd = self.prepare_command(true);
        let frame = cmd.frame_mut();
        let mut writer = WriteCanData::new(frame);

        // Read GPIO status registers (0x05e and 0x05f)
        let resolutions = [Resolution::Int8, Resolution::Int8];
        let mut combiner = WriteCombiner::new(
            0x10, // Read base
            Register::Aux1GpioStatus.address(),
            &resolutions,
        );

        for _ in 0..resolutions.len() {
            combiner.maybe_write(&mut writer);
        }
        cmd.expected_reply_size = combiner.reply_size();
        cmd
    }

    /// Builds a command to write GPIO digital outputs.
    ///
    /// # Arguments
    /// * `aux1` - Optional value to write to AUX1 GPIO outputs (bit 0 = pin 0)
    /// * `aux2` - Optional value to write to AUX2 GPIO outputs (bit 0 = pin 0)
    /// * `query` - If true, also request telemetry in the response
    ///
    /// # Example
    ///
    /// ```rust
    /// use moteus::Controller;
    ///
    /// let controller = Controller::new(1);
    /// // Set all GPIO outputs to high
    /// let cmd = controller.make_write_gpio(Some(0x7f), Some(0x7f), false);
    /// ```
    pub fn make_write_gpio(&self, aux1: Option<u8>, aux2: Option<u8>, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        {
            let frame = cmd.frame_mut();
            let mut writer = WriteCanData::new(frame);

            // Write to GPIO command registers (0x05c and 0x05d)
            let resolutions = [
                if aux1.is_some() {
                    Resolution::Int8
                } else {
                    Resolution::Ignore
                },
                if aux2.is_some() {
                    Resolution::Int8
                } else {
                    Resolution::Ignore
                },
            ];

            let mut combiner = WriteCombiner::new(
                0x00, // Write base
                Register::Aux1GpioCommand.address(),
                &resolutions,
            );

            if combiner.maybe_write(&mut writer) {
                writer.write_i8(aux1.unwrap_or(0) as i8);
            }
            if combiner.maybe_write(&mut writer) {
                writer.write_i8(aux2.unwrap_or(0) as i8);
            }
        }

        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    /// Result of reading GPIO values.
    ///
    /// Each value is a bitfield where bit N represents pin N's state.
    pub fn parse_gpio(&self, frame: &CanFdFrame) -> GpioResult {
        let result = QueryResult::parse(frame);
        GpioResult {
            aux1: result.aux1_gpio as u8,
            aux2: result.aux2_gpio as u8,
        }
    }

    // === Custom Query Methods ===

    /// Builds a command to query specific registers.
    ///
    /// This allows querying arbitrary registers by specifying them as
    /// (register_address, resolution) pairs.
    ///
    /// # Arguments
    /// * `registers` - Slice of (register address, resolution) tuples to query
    ///
    /// # Example
    ///
    /// ```rust
    /// use moteus::Controller;
    /// use moteus_protocol::{Register, Resolution};
    ///
    /// let controller = Controller::new(1);
    /// // Query position and velocity with specific resolutions
    /// let cmd = controller.make_custom_query(&[
    ///     (Register::Position.address(), Resolution::Float),
    ///     (Register::Velocity.address(), Resolution::Float),
    /// ]);
    /// ```
    pub fn make_custom_query(&self, registers: &[(u16, Resolution)]) -> Command {
        let mut cmd = self.prepare_command(true);

        if registers.is_empty() {
            return cmd;
        }

        let frame = cmd.frame_mut();
        let mut writer = WriteCanData::new(frame);

        // Find min and max register addresses
        let min_addr = registers.iter().map(|(a, _)| *a).min().unwrap();
        let max_addr = registers.iter().map(|(a, _)| *a).max().unwrap();

        // Build resolution array for the register range
        let range_len = (max_addr - min_addr + 1) as usize;
        let mut resolutions = vec![Resolution::Ignore; range_len];
        for (addr, res) in registers {
            let idx = (*addr - min_addr) as usize;
            resolutions[idx] = *res;
        }

        let mut combiner = WriteCombiner::new(
            0x10, // Read base
            min_addr,
            &resolutions,
        );

        for _ in 0..range_len {
            combiner.maybe_write(&mut writer);
        }
        cmd.expected_reply_size = combiner.reply_size();
        cmd
    }

    // === AUX PWM Methods ===

    /// Builds an AUX PWM command.
    ///
    /// Sets PWM duty cycles on the AUX1 and AUX2 ports.
    ///
    /// # Arguments
    /// * `pwm_cmd` - AUX PWM command built with the builder pattern
    /// * `query` - If true, also request telemetry in the response
    ///
    /// # Example
    ///
    /// ```rust
    /// use moteus::Controller;
    /// use moteus_protocol::command::AuxPwmCommand;
    ///
    /// let controller = Controller::new(1);
    /// let cmd = controller.make_aux_pwm(
    ///     &AuxPwmCommand::new().aux1_pwm1(0.5).aux1_pwm2(0.75),
    ///     false,
    /// );
    /// ```
    pub fn make_aux_pwm(&self, pwm_cmd: &AuxPwmCommand, query: bool) -> Command {
        let mut cmd = self.prepare_command(query);
        pwm_cmd.serialize(cmd.frame_mut(), &self.aux_pwm_format);
        if query {
            cmd.expected_reply_size = self.query_format.serialize(cmd.frame_mut());
        }
        cmd
    }

    // === Clock Trim Methods ===

    /// Builds a set-trim command.
    ///
    /// Sets the clock trim value used for timing correction.
    ///
    /// # Arguments
    /// * `trim` - Clock trim value (i32)
    pub fn make_set_trim(&self, trim: i32) -> Command {
        let mut cmd = self.prepare_command(false);
        SetTrimCommand::new(trim).serialize(cmd.frame_mut());
        cmd
    }
}

/// Result of reading GPIO digital inputs.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default)]
pub struct GpioResult {
    /// AUX1 GPIO input state (bit N = pin N)
    pub aux1: u8,
    /// AUX2 GPIO input state (bit N = pin N)
    pub aux2: u8,
}

impl std::fmt::Debug for Controller {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Controller")
            .field("address", &self.address)
            .field("id", &self.id)
            .field("source_id", &self.source_id)
            .field("can_prefix", &self.can_prefix)
            .finish()
    }
}

impl Default for Controller {
    fn default() -> Self {
        Self::new(1)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new_controller() {
        let controller = Controller::new(5);
        assert_eq!(controller.id, 5);
        assert_eq!(controller.address, DeviceAddress::can_id(5));
        assert_eq!(controller.source_id, 0);
    }

    #[test]
    fn test_new_controller_with_device_address() {
        let controller = Controller::new(DeviceAddress::can_id(7));
        assert_eq!(controller.id, 7);
        assert_eq!(controller.address, DeviceAddress::can_id(7));
    }

    #[test]
    fn test_make_stop() {
        let controller = Controller::new(1);
        let cmd = controller.make_stop(false);

        assert_eq!(cmd.destination, 1);
        assert!(!cmd.reply_required);
        assert!(cmd.data_size() >= 3);
    }

    #[test]
    fn test_make_stop_into_frame() {
        let controller = Controller::new(1);
        let frame = controller.make_stop(false).into_frame();

        // No reply: arb_id = 0x0001
        assert_eq!(frame.arbitration_id, 0x0001);
        assert!(frame.size >= 3);
    }

    #[test]
    fn test_make_position_command() {
        let controller = Controller::new(1);
        let pos_cmd = PositionCommand::new().position(0.5).velocity(1.0);
        let cmd = controller.make_position_command(&pos_cmd, true);

        assert_eq!(cmd.destination, 1);
        assert!(cmd.reply_required);
        assert!(cmd.data_size() > 3);
    }

    #[test]
    fn test_make_query() {
        let controller = Controller::new(1);
        let cmd = controller.make_query();

        assert!(cmd.reply_required);
        assert!(cmd.expected_reply_size > 0);
    }

    #[test]
    fn test_make_query_into_frame() {
        let controller = Controller::new(1);
        let frame = controller.make_query().into_frame();

        // Reply required: arb_id = 0x8001
        assert_eq!(frame.arbitration_id, 0x8001);
    }

    #[test]
    fn test_new_controller_uuid_only() {
        let uuid = [
            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
            0x0f, 0x10,
        ];
        let controller = Controller::new(DeviceAddress::uuid(uuid));
        assert_eq!(controller.id, 0x7F);
        assert!(!controller.uuid_prefix_data.is_empty());
    }

    #[test]
    fn test_uuid_controller_frame_has_prefix() {
        let uuid = [
            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
            0x0f, 0x10,
        ];
        let controller = Controller::new(DeviceAddress::uuid(uuid));

        let cmd = controller.make_stop(false);
        let frame = cmd.into_frame();

        // Destination is 0x7F (broadcast)
        assert_eq!(frame.arbitration_id & 0x7F, 0x7F);

        // Frame data should start with UUID prefix, followed by stop command
        let prefix_len = controller.uuid_prefix_data.len();
        assert!(frame.size as usize > prefix_len);
        assert_eq!(&frame.data[..prefix_len], &controller.uuid_prefix_data[..]);
    }

    #[test]
    fn test_uuid_controller_query_frame() {
        let uuid = [
            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
            0x0f, 0x10,
        ];
        let controller = Controller::new(DeviceAddress::uuid(uuid));

        let cmd = controller.make_query();
        let frame = cmd.into_frame();

        // Reply required with broadcast: arb_id = 0x807F
        assert_eq!(frame.arbitration_id, 0x807F);

        // Frame should have UUID prefix followed by query
        let prefix_len = controller.uuid_prefix_data.len();
        assert!(frame.size as usize > prefix_len);
    }

    #[test]
    fn test_can_id_controller_no_uuid_prefix() {
        let controller = Controller::new(1);
        assert!(controller.uuid_prefix_data.is_empty());

        let cmd = controller.make_stop(false);
        // Stop command starts at byte 0 (no prefix)
        assert!(cmd.data_size() >= 3);
    }

    #[test]
    fn test_uuid_controller_with_transport_device() {
        let addr = DeviceAddress::uuid([0x01, 0x02, 0x03, 0x04]).with_transport_device(2);
        let controller = Controller::new(addr);
        assert_eq!(controller.id, 0x7F);

        let cmd = controller.make_stop(false);
        let frame = cmd.into_frame();
        assert_eq!(frame.channel, Some(2));
    }
}