Struct dynamixel2::Bus

source ·
pub struct Bus<ReadBuffer, WriteBuffer> { /* private fields */ }
Expand description

Dynamixel Protocol 2 communication bus.

Implementations§

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn action(&mut self, motor_id: u8) -> Result<Response<()>, TransferError>

Send an action command to trigger a previously registered instruction.

You may specify crate::instructions::packet_id::BROADCAST as motor ID. If you do, none of the devices will reply with a response, and this function will not wait for any.

If you want to broadcast this instruction, it may be more convenient to use Self::broadcast_action() instead.

source

pub fn broadcast_action(&mut self) -> Result<(), WriteError>

Broadcast an action command to all connected motors to trigger a previously registered instruction.

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn bulk_read_cb<Read, F>( &mut self, reads: &[Read], on_response: F ) -> Result<(), WriteError>

Synchronously read arbitrary data ranges from multiple motors in one command.

Unlike the sync read instruction, a bulk read can be used to read a different amount of data from a different address for each motor.

The data for multi-byte registers is received in little-endian format.

The on_response function is called for the reply from each motor. If the function fails to write the instruction, an error is returned and the function is not called.

§Panics

The protocol forbids specifying the same motor ID multiple times. This function panics if the same motor ID is used for more than one read.

source

pub fn bulk_read<Read>( &mut self, reads: &[Read] ) -> Result<Vec<Response<Vec<u8>>>, TransferError>
where Read: AsRef<BulkReadData>,

Synchronously read arbitrary data ranges from multiple motors in one command.

Unlike the sync read instruction, a bulk read can be used to read a different amount of data from a different address for each motor.

If this function fails to get the data from any of the motors, the entire function retrns an error. If you need access to the data from other motors, or if you want acces to the error for each motor, see Self::bulk_read_cb.

§Panics

The protocol forbids specifying the same motor ID multiple times. This function panics if the same motor ID is used for more than one read.

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn bulk_write<Write, T>( &mut self, writes: &[Write] ) -> Result<(), WriteError>
where Write: Borrow<BulkWriteData<T>>, T: AsRef<[u8]>,

Synchronously write arbitrary data ranges to multiple motors.

Each motor will perform the write as soon as it receives the command. This gives much shorter delays than executing a regular Self::write for each motor individually. Unlike the sync write instruction, a bulk write allows you to write a different amount of data to a different address for each motor.

The data for multi-byte registers should serialized as little-endian.

§Panics

The protocol forbids specifying the same motor ID multiple times. This function panics if the same motor ID is used for more than one write.

This function also panics if the data length for a motor exceeds the capacity of a u16.

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn clear_revolution_counter( &mut self, motor_id: u8 ) -> Result<Response<()>, TransferError>

Clear the multi-revolution counter of a motor.

This will reset the “present position” register to a value between 0 and a whole revolution. It is not possible to clear the revolution counter of a motor while it is moving. Doing so will cause the motor to return an error, and the revolution counter will not be reset.

You may specify crate::instructions::packet_id::BROADCAST as motor ID. If you do, none of the devices will reply with a response, and this function will not wait for any.

If you want to broadcast this instruction, it may be more convenient to use Self::broadcast_clear_revolution_counter() instead.

source

pub fn broadcast_clear_revolution_counter(&mut self) -> Result<(), WriteError>

Clear the revolution counter of all connected motors.

This will reset the “present position” register to a value between 0 and a whole revolution. It is not possible to clear the mutli-revolution counter of a motor while it is moving. Doing so will cause the motor to return an error, and the revolution counter will not be reset.

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn factory_reset( &mut self, motor_id: u8, kind: FactoryResetKind ) -> Result<Response<()>, TransferError>

Reset the settings of a motor to the factory defaults.

This will reset all registers to the factory default, including the EEPROM registers. The only exceptions are the ID and baud rate settings, which may be kept depending on the kind argument.

You may specify crate::instructions::packet_id::BROADCAST as motor ID. If you do, none of the devices will reply with a response, and this function will not wait for any.

If you want to broadcast this instruction, it may be more convenient to use Self::broadcast_factory_reset() instead.

Starting with version 42 of the firmware for the MX-series and X-series, motors ignore a broadcast reset command with FactoryResetKind::ResetAll. Motors with older firmware may still execute the command, which would cause multiple motors on the bus to have the same ID. At that point, communication with those motors is not possible anymore. The only way to restore communication is to physically disconnect all but one motor at a time and re-assign unique IDs. Or use the ID Inspection Tool in the Dynamixel Wizard 2.0

source

pub fn broadcast_factory_reset( &mut self, kind: FactoryResetKind ) -> Result<(), WriteError>

Reset the settings of all connected motors to the factory defaults.

This will reset all registers to the factory default, including the EEPROM registers. The only exceptions are the ID and baud rate settings, which may be kept depending on the kind argument.

Starting with version 42 of the firmware for the MX-series and X-series, motors ignore a broadcast reset command with FactoryResetKind::ResetAll. Motors with older firmware may still execute the command, which would cause multiple motors on the bus to have the same ID. At that point, communication with those motors is not possible anymore. The only way to restore communication is to physically disconnect all but one motor at a time and re-assign unique IDs.

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn ping(&mut self, motor_id: u8) -> Result<PingResponse, TransferError>

Ping a specific motor by ID.

This will not work correctly if the motor ID is packet_id::BROADCAST. Use Self::scan or Self::scan_cb instead.

source

pub fn scan( &mut self ) -> Result<Vec<Result<PingResponse, ReadError>>, WriteError>

Scan a bus for motors with a broadcast ping, returning the responses in a Vec.

Only timeouts are filtered out since they indicate a lack of response. All other responses (including errors) are collected.

source

pub fn scan_cb<F>(&mut self, on_response: F) -> Result<(), WriteError>

Scan a bus for motors with a broadcast ping, calling an FnMut for each response.

Only timeouts are filtered out since they indicate a lack of response. All other responses (including errors) are passed to the handler.

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn read( &mut self, motor_id: u8, address: u16, count: u16 ) -> Result<Response<Vec<u8>>, TransferError>

Read an arbitrary number of bytes from a specific motor.

This function will not work correctly if the motor ID is set to packet_id::BROADCAST. Use Self::sync_read to read from multiple motors with one command.

source

pub fn read_u8( &mut self, motor_id: u8, address: u16 ) -> Result<Response<u8>, TransferError>

Read an 8 bit register from a specific motor.

This function will not work correctly if the motor ID is set to packet_id::BROADCAST. Use Self::sync_read to read from multiple motors with one command.

source

pub fn read_u16( &mut self, motor_id: u8, address: u16 ) -> Result<Response<u16>, TransferError>

Read 16 bit register from a specific motor.

This function will not work correctly if the motor ID is set to packet_id::BROADCAST. Use Self::sync_read to read from multiple motors with one command.

source

pub fn read_u32( &mut self, motor_id: u8, address: u16 ) -> Result<Response<u32>, TransferError>

Read 32 bit register from a specific motor.

This function will not work correctly if the motor ID is set to packet_id::BROADCAST. Use Self::sync_read to read from multiple motors with one command.

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn reboot(&mut self, motor_id: u8) -> Result<Response<()>, TransferError>

Send a reboot command to a specific motor.

Certain error conditions can only be cleared by rebooting a motor. When a motor reboots, all volatile (non-EEPROM) registers are reset to their initial value. This also has the effect of disabling motor torque and resetting the multi-revolution information.

You may specify crate::instructions::packet_id::BROADCAST as motor ID. If you do, none of the devices will reply with a response, and this function will not wait for any.

If you want to broadcast this instruction, it may be more convenient to use Self::broadcast_reboot() instead.

source

pub fn broadcast_reboot(&mut self) -> Result<(), WriteError>

Broadcast an reboot command to all connected motors to trigger a previously registered instruction.

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn reg_write( &mut self, motor_id: u8, address: u16, data: &[u8] ) -> Result<Response<()>, TransferError>

Register a write of an arbitrary number of bytes, to be triggered later by an action command.

Only one write command can be registered per motor.

You can have all connected motors execute their registered write using Self::broadcast_action, or a single motor using Self::action.

You may specify crate::instructions::packet_id::BROADCAST as motor ID. If you do, none of the devices will reply with a response, and this function will not wait for any.

source

pub fn reg_write_u8( &mut self, motor_id: u8, address: u16, value: u8 ) -> Result<Response<()>, TransferError>

Register a write command for a 8 bit value to a specific motor.

Only one write command can be registered per motor.

You can have all connected motors execute their registered write using Self::broadcast_action, or a single motor using Self::action.

You may specify crate::instructions::packet_id::BROADCAST as motor ID. If you do, none of the devices will reply with a response, and this function will not wait for any.

source

pub fn reg_write_u16( &mut self, motor_id: u8, address: u16, value: u16 ) -> Result<Response<()>, TransferError>

Register a write command for a 16 bit value to a specific motor.

Only one write command can be registered per motor.

You can have all connected motors execute their registered write using Self::broadcast_action, or a single motor using Self::action.

You may specify crate::instructions::packet_id::BROADCAST as motor ID. If you do, none of the devices will reply with a response, and this function will not wait for any.

source

pub fn reg_write_u32( &mut self, motor_id: u8, address: u16, value: u32 ) -> Result<Response<()>, TransferError>

Register a write command for a 32 bit value to a specific motor.

Only one write command can be registered per motor.

You can have all connected motors execute their registered write using Self::broadcast_action, or a single motor using Self::action.

You may specify crate::instructions::packet_id::BROADCAST as motor ID. If you do, none of the devices will reply with a response, and this function will not wait for any.

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn sync_read_cb<'a, F>( &'a mut self, motor_ids: &'a [u8], address: u16, count: u16, on_response: F ) -> Result<(), WriteError>
where F: FnMut(Result<Response<&[u8]>, ReadError>),

Synchronously read an arbitrary number of bytes from multiple motors in one command.

The on_response function is called for the reply from each motor. If the function fails to write the instruction, an error is returned and the function is not called.

source

pub fn sync_read_u8_cb<'a, F>( &'a mut self, motor_ids: &'a [u8], address: u16, on_response: F ) -> Result<(), WriteError>

Synchronously read an 8 bit value from multiple motors in one command.

The on_response function is called for the reply from each motor. If the function fails to write the instruction, an error is returned and the function is not called.

source

pub fn sync_read_u16_cb<'a, F>( &'a mut self, motor_ids: &'a [u8], address: u16, on_response: F ) -> Result<(), WriteError>

Synchronously read a 16 bit value from multiple motors in one command.

The on_response function is called for the reply from each motor. If the function fails to write the instruction, an error is returned and the function is not called.

source

pub fn sync_read_u32_cb<'a, F>( &'a mut self, motor_ids: &'a [u8], address: u16, on_response: F ) -> Result<(), WriteError>

Synchronously read a 32 bit value from multiple motors in one command.

The on_response function is called for the reply from each motor. If the function fails to write the instruction, an error is returned and the function is not called.

source

pub fn sync_read<'a>( &'a mut self, motor_ids: &'a [u8], address: u16, count: u16 ) -> Result<Vec<Response<Vec<u8>>>, TransferError>

Synchronously read an arbitrary number of bytes from multiple motors in one command.

If this function fails to get the data from any of the motors, the entire function retrns an error. If you need access to the data from other motors, or if you want acces to the error for each motor, see Self::sync_read_cb.

source

pub fn sync_read_u8<'a>( &'a mut self, motor_ids: &'a [u8], address: u16 ) -> Result<Vec<Response<u8>>, TransferError>

Synchronously read an 8 bit value from multiple motors in one command.

If this function fails to get the data from any of the motors, the entire function retrns an error. If you need access to the data from other motors, or if you want acces to the error for each motor, see Self::sync_read_u8_cb.

source

pub fn sync_read_u16<'a>( &'a mut self, motor_ids: &'a [u8], address: u16 ) -> Result<Vec<Response<u16>>, TransferError>

Synchronously read a 16 bit value from multiple motors in one command.

If this function fails to get the data from any of the motors, the entire function retrns an error. If you need access to the data from other motors, or if you want acces to the error for each motor, see Self::sync_read_u16_cb.

source

pub fn sync_read_u32<'a>( &'a mut self, motor_ids: &'a [u8], address: u16 ) -> Result<Vec<Response<u32>>, TransferError>

Synchronously read a 32 bit value from multiple motors in one command.

If this function fails to get the data from any of the motors, the entire function retrns an error. If you need access to the data from other motors, or if you want acces to the error for each motor, see Self::sync_read_u32_cb.

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn sync_write<'a, Iter, Data>( &mut self, address: u16, count: u16, data: Iter ) -> Result<(), WriteError>
where Iter: IntoIterator<Item = Data>, Iter::IntoIter: ExactSizeIterator, Data: AsRef<SyncWriteData<&'a [u8]>>,

Synchronously write an arbitrary number of bytes to multiple motors.

Each motor will perform the write as soon as it receives the command. This gives much shorter delays than executing a regular Self::write for each motor individually.

§Panics

The amount of data to write for each motor must be exactly count bytes. This function panics if that is not the case.

source

pub fn sync_write_u8<Iter, Data>( &mut self, address: u16, data: Iter ) -> Result<(), WriteError>
where Iter: IntoIterator<Item = Data>, Iter::IntoIter: ExactSizeIterator, Data: AsRef<SyncWriteData<u8>>,

Synchronously write a 8 bit value to multiple motors.

Each motor will perform the write as soon as it receives the command. This gives much shorter delays than executing a regular Self::write for each motor individually.

source

pub fn sync_write_u16<Iter, Data>( &mut self, address: u16, data: Iter ) -> Result<(), WriteError>
where Iter: IntoIterator<Item = Data>, Iter::IntoIter: ExactSizeIterator, Data: AsRef<SyncWriteData<u16>>,

Synchronously write a 16 bit value to multiple motors.

Each motor will perform the write as soon as it receives the command. This gives much shorter delays than executing a regular Self::write for each motor individually.

source

pub fn sync_write_u32<Iter, Data>( &mut self, address: u16, data: Iter ) -> Result<(), WriteError>
where Iter: IntoIterator<Item = Data>, Iter::IntoIter: ExactSizeIterator, Data: AsRef<SyncWriteData<u32>>,

Synchronously write a 32 bit value to multiple motors.

Each motor will perform the write as soon as it receives the command. This gives much shorter delays than executing a regular Self::write for each motor individually.

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn write( &mut self, motor_id: u8, address: u16, data: &[u8] ) -> Result<Response<()>, TransferError>

Write an arbitrary number of bytes to a specific motor.

You may specify crate::instructions::packet_id::BROADCAST as motor ID. If you do, none of the devices will reply with a response, and this function will not wait for any.

source

pub fn write_u8( &mut self, motor_id: u8, address: u16, value: u8 ) -> Result<Response<()>, TransferError>

Write an 8 bit value to a specific motor.

You may specify crate::instructions::packet_id::BROADCAST as motor ID. If you do, none of the devices will reply with a response, and this function will not wait for any.

source

pub fn write_u16( &mut self, motor_id: u8, address: u16, value: u16 ) -> Result<Response<()>, TransferError>

Write an 16 bit value to a specific motor.

You may specify crate::instructions::packet_id::BROADCAST as motor ID. If you do, none of the devices will reply with a response, and this function will not wait for any.

source

pub fn write_u32( &mut self, motor_id: u8, address: u16, value: u32 ) -> Result<Response<()>, TransferError>

Write an 32 bit value to a specific motor.

You may specify crate::instructions::packet_id::BROADCAST as motor ID. If you do, none of the devices will reply with a response, and this function will not wait for any.

source§

impl Bus<Vec<u8>, Vec<u8>>

source

pub fn open( path: impl AsRef<Path>, baud_rate: u32, read_timeout: Duration ) -> Result<Self>

Open a serial port with the given baud rate.

This will allocate a new read and write buffer of 128 bytes each. Use Self::open_with_buffers() if you want to use a custom buffers.

source

pub fn new(stream: SerialPort, read_timeout: Duration) -> Self

Create a new bus for an open serial port.

This will allocate a new read and write buffer of 128 bytes each. Use Self::with_buffers() if you want to use a custom buffers.

source§

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,

source

pub fn open_with_buffers( path: impl AsRef<Path>, baud_rate: u32, read_timeout: Duration, read_buffer: ReadBuffer, write_buffer: WriteBuffer ) -> Result<Self>

Open a serial port with the given baud rate.

This will allocate a new read and write buffer of 128 bytes each.

source

pub fn with_buffers( serial_port: SerialPort, read_timeout: Duration, read_buffer: ReadBuffer, write_buffer: WriteBuffer ) -> Self

Create a new bus using pre-allocated buffers.

source

pub fn transfer_single<F>( &mut self, packet_id: u8, instruction_id: u8, parameter_count: usize, encode_parameters: F ) -> Result<StatusPacket<'_>, TransferError>
where F: FnOnce(&mut [u8]),

Write a raw instruction to a stream, and read a single raw response.

This function also checks that the packet ID of the status response matches the one from the instruction.

This is not suitable for broadcast instructions. For broadcast instructions, each motor sends an individual response or no response is send at all. Instead, use Self::write_instruction and Self::read_status_response.

source

pub fn write_instruction<F>( &mut self, packet_id: u8, instruction_id: u8, parameter_count: usize, encode_parameters: F ) -> Result<(), WriteError>
where F: FnOnce(&mut [u8]),

Write an instruction message to the bus.

source

pub fn read_status_response(&mut self) -> Result<StatusPacket<'_>, ReadError>

Read a raw status response from the bus.

Trait Implementations§

source§

impl<ReadBuffer, WriteBuffer> Debug for Bus<ReadBuffer, WriteBuffer>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<ReadBuffer, WriteBuffer> Freeze for Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: Freeze, WriteBuffer: Freeze,

§

impl<ReadBuffer, WriteBuffer> RefUnwindSafe for Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: RefUnwindSafe, WriteBuffer: RefUnwindSafe,

§

impl<ReadBuffer, WriteBuffer> Send for Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: Send, WriteBuffer: Send,

§

impl<ReadBuffer, WriteBuffer> Sync for Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: Sync, WriteBuffer: Sync,

§

impl<ReadBuffer, WriteBuffer> Unpin for Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: Unpin, WriteBuffer: Unpin,

§

impl<ReadBuffer, WriteBuffer> UnwindSafe for Bus<ReadBuffer, WriteBuffer>
where ReadBuffer: UnwindSafe, WriteBuffer: UnwindSafe,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.