Skip to main content

QueryFormat

Struct QueryFormat 

Source
#[non_exhaustive]
pub struct QueryFormat {
Show 21 fields pub mode: Resolution, pub position: Resolution, pub velocity: Resolution, pub torque: Resolution, pub q_current: Resolution, pub d_current: Resolution, pub abs_position: Resolution, pub power: Resolution, pub motor_temperature: Resolution, pub trajectory_complete: Resolution, pub home_state: Resolution, pub voltage: Resolution, pub temperature: Resolution, pub fault: Resolution, pub aux1_gpio: Resolution, pub aux2_gpio: Resolution, pub aux1_pwm_input_period_us: Resolution, pub aux1_pwm_input_duty_cycle: Resolution, pub aux2_pwm_input_period_us: Resolution, pub aux2_pwm_input_duty_cycle: Resolution, pub extra: [(u16, Resolution); 16],
}
Expand description

Format configuration for queries.

Specifies which registers to query and at what resolution. Setting a field to Resolution::Ignore means that register will not be queried.

§Examples

use moteus_protocol::Resolution;
use moteus_protocol::query::QueryFormat;

// Default format queries position, velocity, torque, and q_current
let default = QueryFormat::default();

// Comprehensive format includes all standard registers
let full = QueryFormat::comprehensive();

// Or customize individual registers
let mut custom = QueryFormat::default();
custom.abs_position = Resolution::Float;
custom.voltage = Resolution::Int16;

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§mode: Resolution§position: Resolution§velocity: Resolution§torque: Resolution§q_current: Resolution§d_current: Resolution§abs_position: Resolution§power: Resolution§motor_temperature: Resolution§trajectory_complete: Resolution§home_state: Resolution§voltage: Resolution§temperature: Resolution§fault: Resolution§aux1_gpio: Resolution§aux2_gpio: Resolution§aux1_pwm_input_period_us: Resolution§aux1_pwm_input_duty_cycle: Resolution§aux2_pwm_input_period_us: Resolution§aux2_pwm_input_duty_cycle: Resolution§extra: [(u16, Resolution); 16]

Extra registers to query, as (register_number, resolution) pairs. Entries with Resolution::Ignore are unused. Active entries must be sorted by register number (maintained by add_extra).

Implementations§

Source§

impl QueryFormat

Source

pub fn minimal() -> QueryFormat

Creates a minimal query format (mode, position, velocity, torque only).

Source

pub fn comprehensive() -> QueryFormat

Creates a comprehensive query format with all common fields.

Source

pub fn extra_count(&self) -> usize

Returns the number of active extra register entries.

Source

pub fn add_extra(&mut self, register: u16, resolution: Resolution)

Adds an extra register to query.

Source

pub fn serialize(&self, frame: &mut CanFdFrame) -> u8

Serializes the query to a CAN frame and returns expected reply size.

Examples found in repository?
examples/protocol_only.rs (line 64)
29fn main() {
30    println!("=== moteus-protocol Example ===\n");
31
32    // Example 1: Create a stop command
33    println!("1. Creating a stop command for servo ID 1:");
34    let mut stop_frame = CanFdFrame::new();
35    stop_frame.arbitration_id = calculate_arbitration_id(0, 1, 0, true);
36    StopCommand::serialize(&mut stop_frame);
37    print_frame(&stop_frame);
38
39    // Example 2: Create a position command using builder pattern
40    println!("\n2. Creating a position command:");
41    let mut pos_frame = CanFdFrame::new();
42    pos_frame.arbitration_id = calculate_arbitration_id(0, 1, 0, true);
43
44    let pos_cmd = PositionCommand::new()
45        .position(0.5) // 0.5 revolutions
46        .velocity(1.0) // 1.0 rev/s
47        .kp_scale(1.0)
48        .kd_scale(1.0);
49    let pos_format = PositionFormat::default();
50
51    pos_cmd.serialize(&mut pos_frame, &pos_format);
52    print_frame(&pos_frame);
53
54    // Example 3: Create a query-only command
55    println!("\n3. Creating a query-only command:");
56    let mut query_frame = CanFdFrame::new();
57    query_frame.arbitration_id = calculate_arbitration_id(0, 1, 0, true);
58
59    let mut custom_query = QueryFormat::default();
60    custom_query.position = Resolution::Float; // Request full precision
61    custom_query.velocity = Resolution::Float;
62    custom_query.torque = Resolution::Float;
63
64    let reply_size = custom_query.serialize(&mut query_frame);
65    print_frame(&query_frame);
66    println!("  Expected reply size: {} bytes", reply_size);
67
68    // Example 4: Parse a simulated response
69    println!("\n4. Parsing a simulated response:");
70
71    // Construct a fake response frame as if from servo ID 1
72    let mut response = CanFdFrame::new();
73    response.arbitration_id = calculate_arbitration_id(1, 0, 0, false);
74
75    // Manually construct response data:
76    // This would normally come from the CAN bus
77    // Format: [reply header] [register] [values...]
78    response.data[0] = 0x21; // Reply Int8, count=1
79    response.data[1] = 0x00; // Register 0 (Mode)
80    response.data[2] = 0x0A; // Mode = Position (10)
81    response.data[3] = 0x2D; // Reply Float (0x2c), count=1 (0x01)
82    response.data[4] = 0x01; // Register 1 (Position)
83    response.data[5] = 0x00; // Position as f32 (0.5)
84    response.data[6] = 0x00;
85    response.data[7] = 0x00;
86    response.data[8] = 0x3F;
87    response.size = 9;
88
89    let result = QueryResult::parse(&response);
90    println!("  Parsed QueryResult:");
91    println!("    Mode: {:?}", result.mode);
92    println!("    Position: {:.4} rev", result.position);
93    println!("    Velocity: {:.4} rev/s", result.velocity);
94    println!("    Torque: {:.4} Nm", result.torque);
95
96    // Example 5: Create a current (torque) command using builder pattern
97    println!("\n5. Creating a current command:");
98    let mut current_frame = CanFdFrame::new();
99    current_frame.arbitration_id = calculate_arbitration_id(0, 1, 0, true);
100
101    let current_cmd = CurrentCommand::new().d_current(0.0).q_current(0.5);
102    current_cmd.serialize(&mut current_frame, &CurrentFormat::default());
103    print_frame(&current_frame);
104
105    // Example 6: Create a brake command
106    println!("\n6. Creating a brake command:");
107    let mut brake_frame = CanFdFrame::new();
108    brake_frame.arbitration_id = calculate_arbitration_id(0, 1, 0, true);
109
110    BrakeCommand::serialize(&mut brake_frame);
111    print_frame(&brake_frame);
112    println!("  Mode byte: {} (Brake=15)", brake_frame.data[2]);
113
114    println!("\n=== Done ===");
115}

Trait Implementations§

Source§

impl Clone for QueryFormat

Source§

fn clone(&self) -> QueryFormat

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for QueryFormat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for QueryFormat

Source§

fn default() -> QueryFormat

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> CommandExt for T

Source§

fn with_query(self, format: QueryFormat) -> WithQuery<Self>

Attaches a query format override to this command.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.