Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.76 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. ################################################################################
  5. # Copyright (c) 2016, ROBOTIS CO., LTD.
  6. # All rights reserved.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions are met:
  10. #
  11. # * Redistributions of source code must retain the above copyright notice, this
  12. #   list of conditions and the following disclaimer.
  13. #
  14. # * Redistributions in binary form must reproduce the above copyright notice,
  15. #   this list of conditions and the following disclaimer in the documentation
  16. #   and/or other materials provided with the distribution.
  17. #
  18. # * Neither the name of ROBOTIS nor the names of its
  19. #   contributors may be used to endorse or promote products derived from
  20. #   this software without specific prior written permission.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ################################################################################
  33.  
  34. # Author: Ryu Woon Jung (Leon)
  35.  
  36. #
  37. # *********     Sync Write Example      *********
  38. #
  39. #
  40. # Available Dynamixel model on this example : All models using Protocol 1.0
  41. # This example is designed for using two Dynamixel MX-28, and an USB2DYNAMIXEL.
  42. # To use another Dynamixel model, such as X series, see their details in E-Manual(support.robotis.com) and edit below variables yourself.
  43. # Be sure that Dynamixel MX properties are already set as %% ID : 1 / Baudnum : 34 (Baudrate : 57600)
  44. #
  45.  
  46. import os, ctypes
  47.  
  48. if os.name == 'nt':
  49.     import msvcrt
  50.     def getch():
  51.         return msvcrt.getch().decode()
  52. else:
  53.     import sys, tty, termios
  54.     fd = sys.stdin.fileno()
  55.     old_settings = termios.tcgetattr(fd)
  56.     def getch():
  57.         try:
  58.             tty.setraw(sys.stdin.fileno())
  59.             ch = sys.stdin.read(1)
  60.         finally:
  61.             termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  62.         return ch
  63.  
  64. os.sys.path.append('../dynamixel_functions_py')             # Path setting
  65.  
  66. import dynamixel_functions as dynamixel                     # Uses Dynamixel SDK library
  67.  
  68. # Control table address
  69. ADDR_MX_TORQUE_ENABLE       = 24                            # Control table address is different in Dynamixel model
  70. ADDR_MX_GOAL_POSITION       = 30
  71. ADDR_MX_PRESENT_POSITION    = 36
  72.  
  73. # Data Byte Length
  74. LEN_MX_GOAL_POSITION        = 2
  75. LEN_MX_PRESENT_POSITION     = 2
  76.  
  77. # Protocol version
  78. PROTOCOL_VERSION            = 1                             # See which protocol version is used in the Dynamixel
  79.  
  80. # Default setting
  81. DXL1_ID                     = 1                             # Dynamixel ID: 1
  82. DXL2_ID                     = 2                             # Dynamixel ID: 2
  83. DXL3_ID                     = 3
  84. BAUDRATE                    = 1000000
  85. #DEVICENAME                  = "/dev/ttyUSB1".encode('utf-8')# Check which port is being used on your controller
  86.                                                             # ex) Windows: "COM1"   Linux: "/dev/ttyUSB0" Mac: "/dev/tty.usbserial-*"
  87. DEVICENAME                  = "/dev/ttyACM0".encode('utf-8')# Check which port is being used on your controller
  88.  
  89. TORQUE_ENABLE               = 1                             # Value for enabling the torque
  90. TORQUE_DISABLE              = 0                             # Value for disabling the torque
  91. DXL_MINIMUM_POSITION_VALUE  = 100                           # Dynamixel will rotate between this value
  92. 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.)
  93. DXL_MOVING_STATUS_THRESHOLD = 10                            # Dynamixel moving status threshold
  94.  
  95. ESC_ASCII_VALUE             = 0x1b
  96.  
  97. COMM_SUCCESS                = 0                             # Communication Success result value
  98. COMM_TX_FAIL                = -1001                         # Communication Tx Failed
  99.  
  100. # Initialize PortHandler Structs
  101. # Set the port path
  102. # Get methods and members of PortHandlerLinux or PortHandlerWindows
  103. port_num = dynamixel.portHandler(DEVICENAME)
  104.  
  105. # Initialize PacketHandler Structs
  106. dynamixel.packetHandler()
  107.  
  108. # Initialize Groupsyncwrite instance
  109. group_num = dynamixel.groupSyncWrite(port_num, PROTOCOL_VERSION, ADDR_MX_GOAL_POSITION, LEN_MX_GOAL_POSITION)
  110.  
  111. index = 0
  112. dxl_comm_result = COMM_TX_FAIL                              # Communication result
  113. dxl_goal_position = [DXL_MINIMUM_POSITION_VALUE, DXL_MAXIMUM_POSITION_VALUE]         # Goal position
  114.  
  115. dxl_error = 0                                               # Dynamixel error
  116. dxl1_present_position = 0                                   # Present position
  117. dxl2_present_position = 0
  118.  
  119. # Open port
  120. if dynamixel.openPort(port_num):
  121.     print("Succeeded to open the port!")
  122. else:
  123.     print("Failed to open the port!")
  124.     print("Press any key to terminate...")
  125.     getch()
  126.     quit()
  127.  
  128. # Set port baudrate
  129. if dynamixel.setBaudRate(port_num, BAUDRATE):
  130.     print("Succeeded to change the baudrate!")
  131. else:
  132.     print("Failed to change the baudrate!")
  133.     print("Press any key to terminate...")
  134.     getch()
  135.     quit()
  136.  
  137.  
  138. # Enable Dynamixel#1 Torque
  139. dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL1_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE)
  140. dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  141. dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  142. if dxl_comm_result != COMM_SUCCESS:
  143.     print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  144. elif dxl_error != 0:
  145.     print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  146. else:
  147.     print("Dynamixel#1 has been successfully connected")
  148.  
  149. # Enable Dynamixel#2 Torque
  150. dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL2_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE)
  151. dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  152. dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  153. if dxl_comm_result != COMM_SUCCESS:
  154.     print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  155. elif dxl_error != 0:
  156.     print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  157. else:
  158.     print("Dynamixel#2 has been successfully connected")
  159.  
  160. # Enable Dynamixel#3 Torque
  161. dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL3_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE)
  162. dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  163. dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  164. if dxl_comm_result != COMM_SUCCESS:
  165.     print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  166. elif dxl_error != 0:
  167.     print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  168. else:
  169.     print("Dynamixel#3 has been successfully connected")
  170.  
  171.  
  172. while 1:
  173.     print("Press any key to continue! (or press ESC to quit!)")
  174.     if getch() == chr(ESC_ASCII_VALUE):
  175.         break
  176.  
  177.     dxl_addparam_result = 1
  178.     # Add Dynamixel#1 goal position value to the Syncwrite storage
  179.     dxl_addparam_result = ctypes.c_ubyte(dynamixel.groupSyncWriteAddParam(group_num, DXL1_ID, dxl_goal_position[index], LEN_MX_GOAL_POSITION)).value
  180.     if dxl_addparam_result != 1:
  181.         print("[ID:%03d] groupSyncWrite addparam failed" % (DXL1_ID))
  182.         quit()
  183.  
  184.     # Add Dynamixel#2 goal position value to the Syncwrite parameter storage
  185.     dxl_addparam_result = ctypes.c_ubyte(dynamixel.groupSyncWriteAddParam(group_num, DXL2_ID, dxl_goal_position[index], LEN_MX_GOAL_POSITION)).value
  186.     if dxl_addparam_result != 1:
  187.         print("[ID:%03d] groupSyncWrite addparam failed" % (DXL2_ID))
  188.  
  189.     # Add Dynamixel#3 goal position value to the Syncwrite parameter storage
  190.     dxl_addparam_result = ctypes.c_ubyte(dynamixel.groupSyncWriteAddParam(group_num, DXL3_ID, dxl_goal_position[index], LEN_MX_GOAL_POSITION)).value
  191.     if dxl_addparam_result != 1:
  192.         print("[ID:%03d] groupSyncWrite addparam failed" % (DXL3_ID))
  193.  
  194.     # Syncwrite goal position
  195.     dynamixel.groupSyncWriteTxPacket(group_num)
  196.     dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  197.     dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  198.     if dxl_comm_result != COMM_SUCCESS:
  199.         print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  200.     elif dxl_error != 0:
  201.             print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  202.  
  203.     # Clear syncwrite parameter storage
  204.     dynamixel.groupSyncWriteClearParam(group_num)
  205.     getch()
  206.  
  207.     while 1:
  208.         # Read Dynamixel#1 present position
  209.         dxl1_present_position = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL1_ID, ADDR_MX_PRESENT_POSITION)
  210.         dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  211.         dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  212.         if dxl_comm_result != COMM_SUCCESS:
  213.             print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  214.         elif dxl_error != 0:
  215.             print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  216.  
  217.         # Read Dynamixel#2 present position
  218.         dxl2_present_position = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL2_ID, ADDR_MX_PRESENT_POSITION)
  219.         dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  220.         dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  221.         if dxl_comm_result != COMM_SUCCESS:
  222.             print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  223.         elif dxl_error != 0:
  224.             print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  225.  
  226.         # Read Dynamixel#3 present position
  227.         dxl3_present_position = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL3_ID, ADDR_MX_PRESENT_POSITION)
  228.         dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  229.         dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  230.         if dxl_comm_result != COMM_SUCCESS:
  231.             print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  232.         elif dxl_error != 0:
  233.             print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  234.  
  235.         print("[ID:%03d] GoalPos:%03d  PresPos:%03d\t[ID:%03d] GoalPos:%03d  PresPos:%03d" % (DXL1_ID, dxl_goal_position[index], dxl1_present_position, DXL2_ID, dxl_goal_position[index], dxl2_present_position))
  236.  
  237.         if not ((abs(dxl_goal_position[index] - dxl1_present_position) > DXL_MOVING_STATUS_THRESHOLD) and (abs(dxl_goal_position[index] - dxl2_present_position) > DXL_MOVING_STATUS_THRESHOLD)):
  238.             break
  239.  
  240.     # Change goal position
  241.     if index == 0:
  242.         index = 1
  243.     else:
  244.         index = 0
  245.  
  246.  
  247. # Disable Dynamixel#1 Torque
  248. dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL1_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_DISABLE)
  249. dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  250. dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  251. if dxl_comm_result != COMM_SUCCESS:
  252.     print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  253. elif dxl_error != 0:
  254.     print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  255.  
  256. # Disable Dynamixel#2 Torque
  257. dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL2_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_DISABLE)
  258. dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  259. dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  260. if dxl_comm_result != COMM_SUCCESS:
  261.     print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  262. elif dxl_error != 0:
  263.     print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  264.  
  265. # Disable Dynamixel#3 Torque
  266. dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL3_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_DISABLE)
  267. dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
  268. dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
  269. if dxl_comm_result != COMM_SUCCESS:
  270.     print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
  271. elif dxl_error != 0:
  272.     print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
  273.  
  274. # Close port
  275. dynamixel.closePort(port_num)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement