実際のところ
導入
pip install dynamixel_sdk
スクリプト
COM5からデフォのXL330で右左に動かすには
#!/usr/bin/env python # -*- coding: utf-8 -*- from dynamixel_sdk import * # Uses Dynamixel SDK library import msvcrt # for getch import time def getch(): return msvcrt.getch().decode() #********* DYNAMIXEL Model definition ********* MY_DXL = 'X_SERIES' # X330 (5.0 V recommended), X430, X540, 2X430 ADDR_TORQUE_ENABLE = 64 ADDR_GOAL_POSITION = 116 ADDR_PRESENT_POSITION = 132 DXL_MINIMUM_POSITION_VALUE = 0 # Refer to the Minimum Position Limit of product eManual DXL_MAXIMUM_POSITION_VALUE = 4095 # Refer to the Maximum Position Limit of product eManual BAUDRATE = 57600 # DYNAMIXEL Protocol Version (1.0 / 2.0) # https://emanual.robotis.com/docs/en/dxl/protocol2/ PROTOCOL_VERSION = 2.0 # Factory default ID of all DYNAMIXEL is 1 DXL_ID = 1 # Use the actual port assigned to the U2D2. # ex) Windows: "COM*", Linux: "/dev/ttyUSB*", Mac: "/dev/tty.usbserial-*" DEVICENAME = 'COM5' TORQUE_ENABLE = 1 # Value for enabling the torque TORQUE_DISABLE = 0 # Value for disabling the torque DXL_MOVING_STATUS_THRESHOLD = 20 # Dynamixel moving status threshold index = 0 dxl_goal_position = [DXL_MINIMUM_POSITION_VALUE, DXL_MAXIMUM_POSITION_VALUE] # Goal position # Initialize PortHandler instance # Set the port path # Get methods and members of PortHandlerLinux or PortHandlerWindows portHandler = PortHandler(DEVICENAME) # Initialize PacketHandler instance # Set the protocol version # Get methods and members of Protocol1PacketHandler or Protocol2PacketHandler packetHandler = PacketHandler(PROTOCOL_VERSION) # Open port if portHandler.openPort(): print("Succeeded to open the port") else: print("Failed to open the port") print("Press any key to terminate...") getch() quit() # Set port baudrate if portHandler.setBaudRate(BAUDRATE): print("Succeeded to change the baudrate") else: print("Failed to change the baudrate") print("Press any key to terminate...") getch() quit() # Enable Dynamixel Torque dxl_comm_result, dxl_error = packetHandler.write1ByteTxRx(portHandler, DXL_ID, ADDR_TORQUE_ENABLE, TORQUE_ENABLE) if dxl_comm_result != COMM_SUCCESS: print("%s" % packetHandler.getTxRxResult(dxl_comm_result)) elif dxl_error != 0: print("%s" % packetHandler.getRxPacketError(dxl_error)) else: print("Dynamixel has been successfully connected") while 1: print("Press any key to continue! (or press ESC to quit!)") if getch() == chr(0x1b): break # Write goal position dxl_comm_result, dxl_error = packetHandler.write4ByteTxRx(portHandler, DXL_ID, ADDR_GOAL_POSITION, dxl_goal_position[index]) if dxl_comm_result != COMM_SUCCESS: print("%s" % packetHandler.getTxRxResult(dxl_comm_result)) elif dxl_error != 0: print("%s" % packetHandler.getRxPacketError(dxl_error)) while 1: # Read present position dxl_present_position, dxl_comm_result, dxl_error = packetHandler.read4ByteTxRx(portHandler, DXL_ID, ADDR_PRESENT_POSITION) dxl_comm_result = packetHandler.write4ByteTxOnly( portHandler, DXL_ID, ADDR_GOAL_POSITION , dxl_goal_position[index]) if dxl_comm_result != COMM_SUCCESS: print("%s" % packetHandler.getTxRxResult(dxl_comm_result)) elif dxl_error != 0: print("%s" % packetHandler.getRxPacketError(dxl_error)) print("[ID:%03d] GoalPos:%03d PresPos:%03d" % (DXL_ID, dxl_goal_position[index], dxl_present_position)) if not abs(dxl_goal_position[index] - dxl_present_position) > DXL_MOVING_STATUS_THRESHOLD: break # Change goal position if index == 0: index = 1 else: index = 0 # Disable Dynamixel Torque dxl_comm_result, dxl_error = packetHandler.write1ByteTxRx(portHandler, DXL_ID, ADDR_TORQUE_ENABLE, TORQUE_DISABLE) if dxl_comm_result != COMM_SUCCESS: print("%s" % packetHandler.getTxRxResult(dxl_comm_result)) elif dxl_error != 0: print("%s" % packetHandler.getRxPacketError(dxl_error)) # Close port portHandler.closePort()