UMRT Arm Firmware Library
Functions | Variables
CommunicationMaster.py File Reference

Python MWE for controlling stepper motors over the communication link. More...

Functions

def CommunicationMaster.pack_32 (integer)
 Pack a 32-bit integer into a little-endian bytearray. More...
 
def CommunicationMaster.pack_16 (integer)
 Pack a 16-bit integer into a little-endian bytearray. More...
 
def CommunicationMaster.firmatify (pack)
 Encode a packed bytearray to the 7-bit packets Firmata uses. More...
 
def CommunicationMaster.defirmatify (data)
 Decode Firmata 7-bit packets into a bytearray of 8-bit packets. More...
 
def CommunicationMaster.decode_32 (data, offset=0, signed=True)
 Decode a portion of a packed bytearray to a 32-bit integer. More...
 
def CommunicationMaster.decode_16 (data, offset=0, signed=True)
 Decode a portion of a packed bytearray to a 16-bit integer. More...
 
def CommunicationMaster.decode_8 (data, offset=0, signed=True)
 Decode a portion of a packed bytearray to an 8-bit integer. More...
 
def CommunicationMaster.on_echo_text (*data)
 Print text data to the console. More...
 
def CommunicationMaster.on_echo_int32 (*data)
 Print a 32-bit integer to the console. More...
 
def CommunicationMaster.on_echo_int16 (*data)
 Print a 16-bit integer to the console. More...
 
def CommunicationMaster.on_echo_raw (*data)
 Print a raw Firmata message to the console. More...
 
def CommunicationMaster.on_set_speed (*data)
 Extract and print the info returned by a SET_SPEED command. More...
 
def CommunicationMaster.on_get_speed (*data)
 Extract and print the info returned by a GET_SPEED command. More...
 
def CommunicationMaster.on_send_step (*data)
 Extract and print the info returned by a SEND_STEP command. More...
 
def CommunicationMaster.on_seek_position (*data)
 Extract and print the info returned by a SEEK_POSITION command. More...
 
def CommunicationMaster.on_get_position (*data)
 Extract and print the info returned by a GET_POSITION command. More...
 

Variables

string CommunicationMaster.COM_PORT = '/dev/umrt-arm'
 
list CommunicationMaster.MOTOR_IDS = [2]
 
int CommunicationMaster.SYSEX_COMMAND_ECHO = 0x00
 
int CommunicationMaster.SYSEX_COMMAND_SET_SPEED = 0x01
 
int CommunicationMaster.SYSEX_COMMAND_GET_SPEED = 0x02
 
int CommunicationMaster.SYSEX_COMMAND_SEND_STEP = 0x03
 
int CommunicationMaster.SYSEX_COMMAND_SEEK_POS = 0x04
 
int CommunicationMaster.SYSEX_COMMAND_GET_POS = 0x05
 
int CommunicationMaster.SYSEX_COMMAND_SET_GRIPPER = 0x06
 
 CommunicationMaster.b = Arduino(COM_PORT)
 
 CommunicationMaster.it = util.Iterator(b)
 

Detailed Description

Python MWE for controlling stepper motors over the communication link.

When run, the testing sequence described at Arduino Communication Test Script is executed.

# Serial device corresponding to the controller
COM_PORT = '/dev/umrt-arm'
# IDs of the motors to test
MOTOR_IDS = [2]
# Copy of enum from SYSEX_COMMANDS.h since we can't easily load it in here
SYSEX_COMMAND_ECHO = 0x00
SYSEX_COMMAND_SET_SPEED = 0x01
SYSEX_COMMAND_GET_SPEED = 0x02
SYSEX_COMMAND_SEND_STEP = 0x03
SYSEX_COMMAND_SEEK_POS = 0x04
SYSEX_COMMAND_GET_POS = 0x05
SYSEX_COMMAND_SET_GRIPPER = 0x06
# Setup Firmata
b = Arduino(COM_PORT)
b.add_cmd_handler(0x71, on_echo_text)
it = util.Iterator(b)
it.start()
# Setup handlers
b.add_cmd_handler(SYSEX_COMMAND_SET_SPEED, on_set_speed)
b.add_cmd_handler(SYSEX_COMMAND_GET_SPEED, on_get_speed)
b.add_cmd_handler(SYSEX_COMMAND_SEND_STEP, on_send_step)
b.add_cmd_handler(SYSEX_COMMAND_SEEK_POS, on_seek_position)
b.add_cmd_handler(SYSEX_COMMAND_GET_POS, on_get_position)
# Send text echo
b.add_cmd_handler(SYSEX_COMMAND_ECHO, on_echo_text)
b.send_sysex(SYSEX_COMMAND_ECHO, util.str_to_two_byte_iter("hello world"))
time.sleep(1)
# Send some numerical echos
b.add_cmd_handler(SYSEX_COMMAND_ECHO, on_echo_int32)
b.send_sysex(SYSEX_COMMAND_ECHO, firmatify(pack_32(0xDEAD_BEEF)))
b.send_sysex(SYSEX_COMMAND_ECHO, firmatify(pack_32(1000)))
b.send_sysex(SYSEX_COMMAND_ECHO, firmatify(pack_32(32767)))
time.sleep(1)
# Send the numerical echos raw
b.add_cmd_handler(SYSEX_COMMAND_ECHO, on_echo_raw)
b.send_sysex(SYSEX_COMMAND_ECHO, firmatify(pack_32(0xDEAD_BEEF)))
b.send_sysex(SYSEX_COMMAND_ECHO, firmatify(pack_32(1000)))
b.send_sysex(SYSEX_COMMAND_ECHO, firmatify(pack_32(32767)))
time.sleep(1)
for motor in MOTOR_IDS:
# Send speed of 2 RPM for 5 seconds, then 1 RPM in other direction for 5 seconds, then stop
b.send_sysex(SYSEX_COMMAND_GET_POS, firmatify(bytearray([motor])))
b.send_sysex(SYSEX_COMMAND_SET_SPEED, firmatify(bytearray([motor]) + pack_16(20)))
b.send_sysex(SYSEX_COMMAND_GET_SPEED, firmatify(bytearray([motor])))
time.sleep(5)
b.send_sysex(SYSEX_COMMAND_SET_SPEED, firmatify(bytearray([motor]) + pack_16(-10)))
b.send_sysex(SYSEX_COMMAND_GET_SPEED, firmatify(bytearray([motor])))
time.sleep(5)
b.send_sysex(SYSEX_COMMAND_SET_SPEED, firmatify(bytearray([motor]) + pack_16(0)))
b.send_sysex(SYSEX_COMMAND_GET_SPEED, firmatify(bytearray([motor])))
# Step forward 20 steps at 10 RPM, then back 10 steps at 5 RPM
b.send_sysex(SYSEX_COMMAND_GET_POS, firmatify(bytearray([motor])))
b.send_sysex(SYSEX_COMMAND_SEND_STEP, firmatify(bytearray([motor]) + pack_16(20) + pack_16(100)))
time.sleep(1)
b.send_sysex(SYSEX_COMMAND_GET_POS, firmatify(bytearray([motor])))
b.send_sysex(SYSEX_COMMAND_SEND_STEP, firmatify(bytearray([motor]) + pack_16(10) + pack_16(-50)))
time.sleep(1)
b.send_sysex(SYSEX_COMMAND_GET_POS, firmatify(bytearray([motor])))
time.sleep(1)
# Seek back to position 0 from wherever we ended up at 10 RPM
b.send_sysex(SYSEX_COMMAND_SEEK_POS, firmatify(bytearray([motor]) + pack_32(0) + pack_16(100)))
time.sleep(1)
b.send_sysex(SYSEX_COMMAND_GET_POS, firmatify(bytearray([motor])))
time.sleep(1)
std::vector< uint8_t > pack_32(const uint32_t integer)
Packs a 32-bit integer into a vector of 8-bit integers, in little-endian format.
Definition: utils.hpp:26
std::vector< uint8_t > pack_16(const uint16_t integer)
Packs a 16-bit integer into a vector of 8-bit integers, in little-endian format.
Definition: utils.hpp:91

Function Documentation

◆ decode_16()

def CommunicationMaster.decode_16 (   data,
  offset = 0,
  signed = True 
)

Decode a portion of a packed bytearray to a 16-bit integer.

:param data: a bytearray to unpack from :param offset: the index in data which the integer begins at :param signed: true if the integer is signed :return: the integer represented by data[offset:offset+2]

◆ decode_32()

def CommunicationMaster.decode_32 (   data,
  offset = 0,
  signed = True 
)

Decode a portion of a packed bytearray to a 32-bit integer.

:param data: a bytearray to unpack from :param offset: the index in data which the integer begins at :param signed: true if the integer is signed :return: the integer represented by data[offset:offset+4]

◆ decode_8()

def CommunicationMaster.decode_8 (   data,
  offset = 0,
  signed = True 
)

Decode a portion of a packed bytearray to an 8-bit integer.

:param data: a bytearray to unpack from :param offset: the index in data which the integer begins at :param signed: true if the integer is signed :return: the integer represented by data[offset:offset+1]

◆ defirmatify()

def CommunicationMaster.defirmatify (   data)

Decode Firmata 7-bit packets into a bytearray of 8-bit packets.

See firmatify for an explanation of what Firmata does to packets.

:param data: a list of packets to decode, must have even length :return: the reconstructed bytearray

◆ firmatify()

def CommunicationMaster.firmatify (   pack)

Encode a packed bytearray to the 7-bit packets Firmata uses.

Must be called on the pack provided to send_sysex. Useful for checking a == decode_32(firmatify(pack_32(a))).

E.g. for [0xEF, 0xBE, 0xAD, 0xDE]:

# Bit form: [1101 1110, 1010 1101, 1011 1110, 1110 1111]
# firmatafied = [ 0101 1110, 0000 0001, 0010 1101, 0000 0001, 0011 1110, 0000 0001, 0110 1111, 0000 0001 ]
# = [ 0x5E, 0x01, 0x2D, 0x01, 0x3E, 0x01, 0x6F, 0x01]

:param pack: a bytearray :return: the bytearray split into 7-bit segments

◆ on_echo_int16()

def CommunicationMaster.on_echo_int16 ( data)

Print a 16-bit integer to the console.

:param data: a Firmata-encoded 16-bit integer

◆ on_echo_int32()

def CommunicationMaster.on_echo_int32 ( data)

Print a 32-bit integer to the console.

:param data: a Firmata-encoded 32-bit integer

◆ on_echo_raw()

def CommunicationMaster.on_echo_raw ( data)

Print a raw Firmata message to the console.

:param data: some Firmata-encoded data

◆ on_echo_text()

def CommunicationMaster.on_echo_text ( data)

Print text data to the console.

:param data: a Firmata text packet

◆ on_get_position()

def CommunicationMaster.on_get_position ( data)

Extract and print the info returned by a GET_POSITION command.

:param data: a Firmata message from a GET_POSITION command response

◆ on_get_speed()

def CommunicationMaster.on_get_speed ( data)

Extract and print the info returned by a GET_SPEED command.

:param data: a Firmata message from a GET_SPEED command response

◆ on_seek_position()

def CommunicationMaster.on_seek_position ( data)

Extract and print the info returned by a SEEK_POSITION command.

:param data: a Firmata message from a SEEK_POSITION command response

◆ on_send_step()

def CommunicationMaster.on_send_step ( data)

Extract and print the info returned by a SEND_STEP command.

:param data: a Firmata message from a SEND_STEP command response

◆ on_set_speed()

def CommunicationMaster.on_set_speed ( data)

Extract and print the info returned by a SET_SPEED command.

:param data: a Firmata message from a SET_SPEED command response

◆ pack_16()

def CommunicationMaster.pack_16 (   integer)

Pack a 16-bit integer into a little-endian bytearray.

E.g. for 0xBEEF:

# Bits: 1011 1110 1110 1111
# Index: 1111 1111 0000 0000
>>>pack_16(0xBEEF)
[ 0xEF, 0xBE ]

:param integer: a number to pack :return: the packed representation

◆ pack_32()

def CommunicationMaster.pack_32 (   integer)

Pack a 32-bit integer into a little-endian bytearray.

E.g. for 0xDEAD_BEEF:

# Bits: 1101 1110 1010 1101 1011 1110 1110 1111
# Index: 3333 3333 2222 2222 1111 1111 0000 0000'
>>> pack_32(0xDEADBEEF)
[ 0xEF, 0xBE, 0xAD, 0xDE ]

:param integer: a number to pack :return: the packed representation