Advertisement
Guest User

Dynamixel X PWM read write example

a guest
Feb 12th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.67 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. ################################################################################
  5. # Copyright 2017 ROBOTIS CO., LTD.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. #     http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. ################################################################################
  19.  
  20. # Author: Ryu Woon Jung (Leon), Andreas Gerken
  21.  
  22. #
  23. # *********     Read and Write Example for PWM control   *********
  24. #
  25. #
  26. # Available Dynamixel model on this example : All models using Protocol 2.0
  27. # This example is designed for using a Dynamixel X series actuator, and an USB2DYNAMIXEL.
  28. # Be sure that Dynamixel X properties are already set as %% ID : 1 / Baudnum : 1 (Baudrate : 57600)
  29. #
  30.  
  31. import os
  32.  
  33. if os.name == 'nt':
  34.     import msvcrt
  35.     def getch():
  36.         return msvcrt.getch().decode()
  37. else:
  38.     import sys, tty, termios
  39.     fd = sys.stdin.fileno()
  40.     old_settings = termios.tcgetattr(fd)
  41.     def getch():
  42.         try:
  43.             tty.setraw(sys.stdin.fileno())
  44.             ch = sys.stdin.read(1)
  45.         finally:
  46.             termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  47.         return ch
  48.  
  49. os.sys.path.append('../dynamixel_functions_py')             # Path setting
  50.  
  51. import dynamixel_functions as dynamixel                     # Uses Dynamixel SDK library
  52.  
  53. # Control table address
  54.  
  55. ADDR_X_OPERATING_MODE       = 11
  56. X_OPERATING_MODE_VELOCITY   = 1
  57. X_OPERATING_MODE_POSITION   = 3
  58. X_OPERATING_MODE_PWM        = 16
  59. ADDR_X_PWM_LIMIT            = 36
  60. X_PWM_LIMIT                 = 885                           # Initial value from datasheet
  61. ADDR_X_GOAL_PWM             = 100
  62.  
  63. ADDR_X_TORQUE_ENABLE        = 64
  64. ADDR_X_GOAL_POSITION        = 116
  65. ADDR_X_PRESENT_POSITION     = 132
  66.  
  67. # Protocol version
  68. PROTOCOL_VERSION            = 2                             # See which protocol version is used in the Dynamixel
  69.  
  70. # Default setting
  71. DXL_ID                      = 1                             # Dynamixel ID: 1
  72. BAUDRATE                    = 57600
  73. DEVICENAME                  = "/dev/ttyUSB0".encode('utf-8')# Check which port is being used on your controller
  74.                                                             # ex) Windows: "COM1"   Linux: "/dev/ttyUSB0" Mac: "/dev/tty.usbserial-*"
  75.  
  76. TORQUE_ENABLE               = 1                             # Value for enabling the torque
  77. TORQUE_DISABLE              = 0                             # Value for disabling the torque
  78.  
  79.  
  80. ESC_ASCII_VALUE             = 0x1b
  81.  
  82. COMM_SUCCESS                = 0                             # Communication Success result value
  83. COMM_TX_FAIL                = -1001                         # Communication Tx Failed
  84.  
  85. # Initialize PortHandler Structs
  86. # Set the port path
  87. # Get methods and members of PortHandlerLinux or PortHandlerWindows
  88. port_num = dynamixel.portHandler(DEVICENAME)
  89.  
  90. # Initialize PacketHandler Structs
  91. dynamixel.packetHandler()
  92.  
  93. index = 0
  94. dxl_goal_pwm = [-100,100]
  95. dxl_comm_result = COMM_TX_FAIL                              # Communication result
  96.  
  97. dxl_error = 0                                               # Dynamixel error
  98. dxl_present_position = 0                                    # Present position
  99.  
  100. # Open port
  101. if dynamixel.openPort(port_num):
  102.     print("Succeeded to open the port!")
  103. else:
  104.     print("Failed to open the port!")
  105.     print("Press any key to terminate...")
  106.     getch()
  107.     quit()
  108.  
  109. # Set port baudrate
  110. if dynamixel.setBaudRate(port_num, BAUDRATE):
  111.     print("Succeeded to change the baudrate!")
  112. else:
  113.     print("Failed to change the baudrate!")
  114.     print("Press any key to terminate...")
  115.     getch()
  116.     quit()
  117.  
  118. print("Setting operating mode to pwm")
  119. dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_X_OPERATING_MODE, X_OPERATING_MODE_PWM)
  120.  
  121. print("Setting pwm limit")
  122. dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_X_PWM_LIMIT, X_PWM_LIMIT)
  123.  
  124. # Enable Dynamixel Torque
  125. dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_X_TORQUE_ENABLE, TORQUE_ENABLE)
  126. dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  127. dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  128. if dxl_comm_result != COMM_SUCCESS:
  129.     print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  130. elif dxl_error != 0:
  131.     print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  132. else:
  133.     print("Dynamixel has been successfully connected")
  134.  
  135.  
  136. while 1:
  137.     print("Press any key to continue! (or press ESC to quit!)")
  138.     if getch() == chr(ESC_ASCII_VALUE):
  139.         break
  140.  
  141.     # Write goal pwm
  142.     dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_X_GOAL_PWM, dxl_goal_pwm[index])
  143.     dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  144.     dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  145.     if dxl_comm_result != COMM_SUCCESS:
  146.         print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  147.     elif dxl_error != 0:
  148.         print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  149.  
  150.     # Read present position
  151.     dxl_present_position = dynamixel.read4ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_X_PRESENT_POSITION)
  152.     dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  153.     dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  154.     if dxl_comm_result != COMM_SUCCESS:
  155.         print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  156.     elif dxl_error != 0:
  157.         print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  158.  
  159.     print("[ID:%03d] GoalPos:%03d  PresPos:%03d" % (DXL_ID, dxl_goal_pwm[index], dxl_present_position))
  160.  
  161.     # Change goal position
  162.     if index == 0:
  163.         index = 1
  164.     else:
  165.         index = 0
  166.  
  167. # Disable Dynamixel Torque
  168. dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_X_TORQUE_ENABLE, TORQUE_DISABLE)
  169. dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  170. dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  171. if dxl_comm_result != COMM_SUCCESS:
  172.     print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  173. elif dxl_error != 0:
  174.     print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  175.  
  176.  
  177. # Close port
  178. dynamixel.closePort(port_num)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement