Skip to content

Commit

Permalink
Use argparse to setup the 'dynamixel_id', 'baudrate', 'timeout' and '…
Browse files Browse the repository at this point in the history
…port' values.
  • Loading branch information
jeremiedecock committed Jun 10, 2015
1 parent 4e3fe11 commit 26d61e6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
27 changes: 20 additions & 7 deletions examples/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,42 @@
import pydynamixel.connection
import pydynamixel.instruction_packet as ip

import argparse
import time

def main():
serial_connection = pydynamixel.connection.Connection()

# Switch ON the LED
# PARSE OPTIONS

#instruction_packet = ip.InstructionPacket(_id=pk.BROADCAST_ID, _instruction=ip.WRITE_DATA, _parameters=(pk.LED, 1))
instruction_packet = ip.InstructionPacket(_id=0x02, _instruction=ip.WRITE_DATA, _parameters=(pk.LED, 1))
parser = argparse.ArgumentParser(description='A PyAX-12 demo.')

parser.add_argument("--dynamixel_id", "-i", help="The unique ID of a Dynamixel unit to work with (254 is a broadcasting ID)", metavar="INTEGER", type=int, default=pk.BROADCAST_ID)
parser.add_argument("--baudrate", "-b", help="The baudrate speed (e.g. 57600)", metavar="INTEGER", type=int, default=57600)
parser.add_argument("--timeout", "-t", help="The timeout value for the connection", metavar="FLOAT", type=float, default=0.1)
parser.add_argument("--port", "-p", help="The serial device to connect with (e.g. '/dev/ttyUSB0' for Unix users)", metavar="STRING", default="/dev/ttyUSB0")
args = parser.parse_args()

# CONNECT TO THE SERIAL PORT

serial_connection = pydynamixel.connection.Connection(_port=args.port, _baudrate=args.baudrate, _timeout=args.timeout)

# SWITCH ON THE LED

instruction_packet = ip.InstructionPacket(_id=args.dynamixel_id, _instruction=ip.WRITE_DATA, _parameters=(pk.LED, 1))
print('> ', instruction_packet.to_printable_string())

status_packet = serial_connection.send(instruction_packet)

if status_packet is not None:
print('< ', status_packet.to_printable_string())

# Wait 2 seconds
# WAIT 2 SECONDS

time.sleep(2)

# Switch OFF the LED
# SWITCH OFF THE LED

instruction_packet = ip.InstructionPacket(_id=pk.BROADCAST_ID, _instruction=ip.WRITE_DATA, _parameters=(pk.LED, 0))
instruction_packet = ip.InstructionPacket(_id=args.dynamixel_id, _instruction=ip.WRITE_DATA, _parameters=(pk.LED, 0))
print('> ', instruction_packet.to_printable_string())

status_packet = serial_connection.send(instruction_packet)
Expand Down
26 changes: 20 additions & 6 deletions examples/move.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,42 @@
import pydynamixel.connection
import pydynamixel.instruction_packet as ip

import argparse
import time

def main():
serial_connection = pydynamixel.connection.Connection()

# Goto to 180°
# PARSE OPTIONS

instruction_packet = ip.InstructionPacket(_id=pk.BROADCAST_ID, _instruction=ip.WRITE_DATA, _parameters=(pk.GOAL_POSITION, 0x00, 0x02, 0x00, 0x02))
parser = argparse.ArgumentParser(description='A PyAX-12 demo.')

parser.add_argument("--dynamixel_id", "-i", help="The unique ID of a Dynamixel unit to work with (254 is a broadcasting ID)", metavar="INTEGER", type=int, default=pk.BROADCAST_ID)
parser.add_argument("--baudrate", "-b", help="The baudrate speed (e.g. 57600)", metavar="INTEGER", type=int, default=57600)
parser.add_argument("--timeout", "-t", help="The timeout value for the connection", metavar="FLOAT", type=float, default=0.1)
parser.add_argument("--port", "-p", help="The serial device to connect with (e.g. '/dev/ttyUSB0' for Unix users)", metavar="STRING", default="/dev/ttyUSB0")
args = parser.parse_args()

# CONNECT TO THE SERIAL PORT

serial_connection = pydynamixel.connection.Connection(_port=args.port, _baudrate=args.baudrate, _timeout=args.timeout)

# GOTO TO 180°

instruction_packet = ip.InstructionPacket(_id=args.dynamixel_id, _instruction=ip.WRITE_DATA, _parameters=(pk.GOAL_POSITION, 0x00, 0x02, 0x00, 0x02))
print('> ', instruction_packet.to_printable_string())

status_packet = serial_connection.send(instruction_packet)

if status_packet is not None:
print('< ', status_packet.to_printable_string())

# Wait 2 seconds
# WAIT 2 SECONDS

time.sleep(1)

# Go back to 0
# GO BACK TO 0°

instruction_packet = ip.InstructionPacket(_id=pk.BROADCAST_ID, _instruction=ip.WRITE_DATA, _parameters=(pk.GOAL_POSITION, 0x00, 0x00, 0x00, 0x02))
instruction_packet = ip.InstructionPacket(_id=args.dynamixel_id, _instruction=ip.WRITE_DATA, _parameters=(pk.GOAL_POSITION, 0x00, 0x00, 0x00, 0x02))
print('> ', instruction_packet.to_printable_string())

status_packet = serial_connection.send(instruction_packet)
Expand Down

0 comments on commit 26d61e6

Please sign in to comment.