Dynamixel Example Code
Path, import
DynamixelSDK/python/tests
๋ด์ Python read write protocol 1.0.py
๋ฅผ ๋จ๊ณ๋ณ๋ก ์ดํด๋ณด๊ฒ ์ต๋๋ค.
os.sys.path.append('../dynamixel_functions_py') # Path setting
import dynamixel_functions as dynamixel # Uses DYNAMIXEL SDK library
dynamixel_functions_py
ํจํค์ง๋ด์dynamixel_functions
๋ฅผ import ํด์ค๋๋ค.
Address / Protocol
# Control table address
ADDR_MX_TORQUE_ENABLE = 24 # Control table address is different in Dynamixel model
ADDR_MX_GOAL_POSITION = 30
ADDR_MX_PRESENT_POSITION = 36
# Protocol version
PROTOCOL_VERSION = 1 # See which protocol version is used in the Dynamixel
- ์์ ์ ๋ชจํฐ์ ์๋ง๋ protocol ๋ฒ์ ๊ณผ address๋ฅผ ๊ธฐ์ ํฉ๋๋ค.
- ์์ธํ ์ ๋ณด๋ Dynamixel ํํ์ด์ง ๋ด ๋ชจํฐ manual์ ๊ธฐ์ ๋์ด ์์ต๋๋ค.⇒ ์๋ฅผ ๋ค์ด, AX-12 / AX-12A๋ ๋ค์ ์์น์ ์กด์ฌํฉ๋๋ค.
Motor Parameter
# Default setting
DXL_ID = 1 # Dynamixel ID: 1
BAUDRATE = 1000000
DEVICENAME = "/dev/ttyUSB0".encode('utf-8')# Check which port is being used on your controller
# ex) Windows: "COM1" Linux: "/dev/ttyUSB0"
TORQUE_ENABLE = 1 # Value for enabling the torque
TORQUE_DISABLE = 0 # Value for disabling the torque
DXL_MINIMUM_POSITION_VALUE = 100 # Dynamixel will rotate between this value
DXL_MAXIMUM_POSITION_VALUE = 4000 # and this value (note that the Dynamixel would not move when the position value is out of movable range. Check e-manual about the range of the Dynamixel you use.)
DXL_MOVING_STATUS_THRESHOLD = 10 # Dynamixel moving status threshold
DXL_ID
- ๊ฐ ๋ชจํฐ๊ฐ ๊ฐ์ง ๊ณ ์ ํ ID์ด๋ฉฐ, ์ฌ๋ฐ๋ฅธ ์ค์ ์ด ๋์ด ์์ด์ผ ํต์ ์ด ๊ฐ๋ฅํฉ๋๋ค.
- ๋ชจํฐ์ ID ๋ ์์ ์ํํธ์จ์ด(Dynamixel Wizard)๋ฅผ ๊น๊ณ ์ปดํจํฐ์ ์ฐ๊ฒฐํ๋ฉด ํ์ธํ ์ ์์ต๋๋ค.
Dynamixel Wizard
์ ์ค์น๊ณผ์ ๊ณผ ์์ธํ ์ค๋ช ์ ํ๋จ ๋งํฌ๋ฅผ ์ฐธ์กฐํ์ธ์
DEVICENAME
- ํธ์คํธ PC์์ ์ธ์ํ๋ Dynamixel ์ ์ด๋ฆ์ด๋ฉฐ, ์ด์์ฒด์ ์ ๋ฐ๋ผ ์ฐจ์ด๊ฐ ์์ต๋๋ค.
linux
$ dmesg | grep tty
๋ฅผ ํตํด/dev/ttyUSB0
์ด ์ฐ๊ฒฐ๋์๋์ง ํ์ธํฉ๋๋ค.
- ๋ณดํต ํ๋์ ์ฐ๊ฒฐ๋ง ์๋ค๋ฉด
/dev/ttyUSB0
์ ์ฐ์ ์ฐ๊ฒฐ๋๋ฉฐ, 2๊ฐ ์ด์์ธ ๊ฒฝ์ฐ/dev/ttyUSB1
๋ฑ์ผ๋ก ์ด๋ฆ์ด ๋ณ๊ฒฝ๋ฉ๋๋ค.
window
- ์ ์ดํ - ์ฅ์น๊ด๋ฆฌ์ - ํฌํธํญ์์ ํ์ธํ ์ ์์ต๋๋ค.
- ๋ชจํฐ๋ ๊ฐ๋๊ฐ ์๋ position value๋ฅผ ์ด์ฉํ์ฌ ๋ชจํฐ๋ฅผ ์ ์ดํ๋ค. position value๊ฐ ์ด๋ค ๊ฐ๋์ mapping ๋์ด ์๋์ง ํ์ธ ํ์
port number, packetHandler
# Initialize PortHandler Structs
# Set the port path
# Get methods and members of PortHandlerLinux or PortHandlerWindows
port_num = dynamixel.portHandler(DEVICENAME)
# Initialize PacketHandler Structs
dynamixel.packetHandler()
index = 0
dxl_goal_position = [DXL_MINIMUM_POSITION_VALUE, DXL_MAXIMUM_POSITION_VALUE] # Goal position
dxl_present_position = 0 # Present position
- ์ด ๋ถ๋ถ์์๋ ํน๋ณํ ์ค์ ํ ๊ฒ์ ์์ต๋๋ค.
- ์์์ ์ค์ ํ DEVICENAME์ ๋ฐ๋ผ์ port num์ด ์ค์ ๋๊ณ ํจํท ๊ตฌ์ฑ ๋ฐ ํจํท ์ ์ฅ์ ์ฌ์ฉ๋๋ ํจํท๊ตฌ์ฑ์ ์ฌ์ฉ๋๋ paramerter๋ฅผ ์ด๊ธฐํํฉ๋๋ค.
Setup
# Open port
if dynamixel.openPort(port_num):
print("Succeeded to open the port!")
else:
print("Failed to open the port!")
print("Press any key to terminate...")
msvcrt.getch()
quit()
# Set port baudrate
if dynamixel.setBaudRate(port_num, BAUDRATE):
print("Succeeded to change the baudrate!")
else:
print("Failed to change the baudrate!")
print("Press any key to terminate...")
msvcrt.getch()
quit()
# Enable Dynamixel Torque
dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE)
if dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION) != COMM_SUCCESS:
dynamixel.printTxRxResult(PROTOCOL_VERSION, dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION))
elif dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION) != 0:
dynamixel.printRxPacketError(PROTOCOL_VERSION, dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION))
else:
print("Dynamixel has been successfully connected")
- Open port : ์ปจํธ๋กค๋ฌ๊ฐ ๋ค์ด๋๋ฏน์ ๊ณผ ํต์ ํ๊ธฐ ์ํด port๋ฅผ ์ฝ๋๋ค.
- Set port baudrate : ํต์ baudrate๋ฅผ ์ค์ ํฉ๋๋ค.
- Enable Dynamixel Torque
write1ByteTxRx()
: DXL_ID์ id๋ฅผ ๊ฐ์ง ๋ชจํฐ์๊ฒ TORQUE_ENABLE value ์ธ 1byte๋ฅผ ADDR_MX_TORQUE_ENABLE address ๋ก ์ ๋ฌํฉ๋๋ค.
Read and Write Dynamixel motor
while 1:
print("Press any key to continue! (or press ESC to quit!)")
if msvcrt.getch().decode() == chr(ESC_ASCII_VALUE):
break
# Write goal position
dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_MX_GOAL_POSITION, dxl_goal_position[index])
if dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION) != COMM_SUCCESS:
dynamixel.printTxRxResult(PROTOCOL_VERSION, dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION))
elif dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION) != 0:
dynamixel.printRxPacketError(PROTOCOL_VERSION, dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION))
while 1:
# Read present position
dxl_present_position = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_MX_PRESENT_POSITION)
if dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION) != COMM_SUCCESS:
dynamixel.printTxRxResult(PROTOCOL_VERSION, dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION))
elif dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION) != 0:
dynamixel.printRxPacketError(PROTOCOL_VERSION, dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION))
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
- Enable Dynamixel Torque์ ๊ฐ์ ๋ฐฉ์์ด๋ฉฐ ๊ฐ ํจ์๋ ๋ค์๊ณผ ๊ฐ์ ์ญํ ์ ์ํํฉ๋๋ค.
write2ByteTxRx()
: dxl_goal_position[index] ์ธ ๋ชฉํ position value๋ฅผ ์ ๋ฌ read2ByteTxRx()
: ํ์ฌ position value๋ฅผ ์ฝ์ด์ค๊ธฐ
Exit
# Disable Dynamixel Torque
dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_DISABLE)
if dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION) != COMM_SUCCESS:
dynamixel.printTxRxResult(PROTOCOL_VERSION, dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION))
elif dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION) != 0:
dynamixel.printRxPacketError(PROTOCOL_VERSION, dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION))
# Close port
dynamixel.closePort(port_num)
- Dynamixel Torque๋ฅผ Disable ์ํ๋ก ๋ง๋ค๊ณ , port๋ฅผ ๋ซ์ต๋๋ค.
์ฝ๋ ์คํ์ ์ค๋ฅ๊ฐ ๋ ๊ฒฝ์ฐ
permission ๊ด๋ จ ์ค๋ฅ๊ฐ ๋ ๊ฒฝ์ฐ ์๋ ์ฝ๋๋ฅผ ํ๋ฒ ์คํ์ํค๊ณ ์งํํ๋ฉด ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋ฉ๋๋ค.
sudo chmod -R 777 /dev/ttyUSB0