Packet module

This module contains the general Packet class which implements the either “instruction packet” (the packets sent by the controller to the Dynamixel actuators to send commands) or “status packet” (the response packets from the Dynamixel units to the main controller after receiving an instruction packet).

class pyax12.packet.Packet(dynamixel_id, data)[source]

The general raw Packet class.

It implements the either “instruction packet” (the packets sent by the controller to the Dynamixel actuators to send commands) or “status packet” (the response packets from the Dynamixel units to the main controller after receiving an instruction packet).

The structure of a general Packet is as the following:

0xFF 0xFF ID LENGTH DATA... CHECK SUM

This class has been made for debugging purpose and is not intended to be used widely to create packets. Instead, it is recommanded to use InstructionPacket or StatusPacket classes to build Packet instances.

Parameters:
  • dynamixel_id (int) – the unique ID of a Dynamixel unit (from 0x00 to 0xFD), 0xFE is a broadcasting ID.
  • data (bytes) – a sequence of byte containing the packet’s data: the instruction to perform or the status of the Dynamixel actuator. This data argument contains the fifth to the penultimate byte of the full built packet.
checksum

The packet checksum, used to prevent packet transmission error.

This member is a read-only property.

data

A sequence of byte containing the packet’s data, i.e. from the fifth to the penultimate byte of the “full packet”.

It contains either:

  • the instruction to perform and its parameters if the packet is an “instruction packet”;
  • or the status of the Dynamixel actuator (the “error” and “parameters” fields) if the packet is a “status packet”.

This member is a read-only property.

dynamixel_id

The unique ID of a Dynamixel unit concerned with this packet.

This byte either:

  • has a value between 0x00 and 0xFD to affect the corresponding Dynamixel unit
  • or has the value 0xFE to affect all connected units (0xFE is the “broadcasting” ID).

This member is a read-only property.

header

The header of the packet.

This pair of byte should always be equals to b'\xff\xff'.

This member is a read-only property.

length

The so called “length” of the packet.

This is not the actual length of the full packet (self._bytes) but its number of bytes after its fourth byte, i.e.:

len(self._bytes[4:])

or in other words:

len(self._bytes) - 4

This value (so called “LENGTH”) defines the fourth byte of each packet.

This member is a read-only property.

parameters

A sequence of byte used if there is additional information needed to be read (other than the error itself).

This member is a read-only property.

to_byte_array()[source]

Return the packet as a bytearray (a mutable sequence of bytes).

This function returns something like:

bytearray(b'\xff\xff\xfe\x04\x03\x03\x01\xf6')
to_bytes()[source]

Return the packet as a bytes string (an immutable sequence of bytes).

This function returns something like:

b'\xff\xff\xfe\x04\x03\x03\x01\xf6'
to_integer_tuple()[source]

Return the packet as a tuple of integers.

This function returns something like:

(255, 255, 254, 4, 3, 3, 1, 246)
to_printable_string()[source]

Return the packet as a string of hexadecimal values.

This function returns something like:

ff ff fe 04 03 03 01 f6
pyax12.packet.compute_checksum(byte_seq)[source]

Compute and return the checksum of the byte_seq packet.

The checksum is the value of the last byte of each packet. It is used to prevent transmission errors of packets. Checksums are computed as follow:

Checksum = ~(dynamixel_id + length + data1 + ... + dataN)

where ~ represent the NOT logic operation.

If the computed value is larger than 255, then only its lower byte is defined as the checksum value.

Parameters:byte_seq (bytes) – a byte sequence containing the packet’s bytes involved in the computation of the checksum (i.e. from the third to the penultimate byte of the “full packet” considered).