Example #1
0
def set_baudrate(id, new_baudrate, baudrate):
    """Sets a servo baudrate.
    
    :param int id: Servo ``ìd``
    :param baudrate: Baudrate of the servo to be configured
    :param int new_baudrate: ``new_baudrate`` to be configured
    :return: 
    --``PORT_ERROR`` case it fails to open the port.
    
    --``BAUDRATE_ERROR`` case it fails to change baudrate.
    
    --``COMM_ERROR`` case there is a communication error.
    
    --``HARDWARE_COMM_ERROR`` case there is a hardware communication error.
    
    --``NONE`` case the operation succeeds."""
    # Get methods and members of PortHandlerLinux or PortHandlerWindows
    port_num = dynamixel.portHandler(DEVICENAME)
    # Initialize PacketHandler Structs
    dynamixel.packetHandler()
    # Open port
    if dynamixel.openPort(port_num):
        print("Succeeded to open the port!")
    else:
        print("Failed to open the port!")
        return PORT_ERROR
    # Set port baudrate
    if dynamixel.setBaudRate(port_num, baudrate):
        print("Succeeded to change the baudrate!")
    else:
        print("Failed to change the baudrate!")
        return BAUDRATE_ERROR
    baudnum = {
        1000000: 1,
        500000: 3,
        400000: 4,
        250000: 7,
        200000: 9,
        115200: 16,
        57600: 34,
        19200: 103,
        9600: 207
    }
    value = baudnum[new_baudrate]

    # Set baudrate
    dynamixel.write1ByteTxRx(port_num, PROTOCOL_1, id, ADDR_BAUDRATE, value)
    dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_1)
    dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_1)
    if dxl_comm_result != COMM_SUCCESS:
        print(dynamixel.getTxRxResult(PROTOCOL_1, dxl_comm_result))
        return COMM_ERROR
    elif dxl_error != 0:
        print(dynamixel.getRxPacketError(PROTOCOL_1, dxl_error))
        return HARDWARE_COMM_ERROR
    else:
        print("Baudrate changed to : %s" % baudrate)
        time.sleep(0.2)

    dynamixel.closePort(port_num)
Example #2
0
    def release(self):
        ''' This method should be called for
        the class to explicitly close the
        open socket
        '''

        dxl.closePort(self.socket)
    def Disable_Torque_Close_Port(self):
        ADDR_PRO_TORQUE_ENABLE = 64
        TORQUE_DISABLE = 0
        dxl_comm_result = COMM_TX_FAIL
        # Enable Dynamixel#1 Torque
        dynamixel.write1ByteTxRx(self.port_num, self.proto_ver, self.m1id,
                                 ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE)
        dxl_comm_result = dynamixel.getLastTxRxResult(self.port_num,
                                                      self.proto_ver)
        dxl_error = dynamixel.getLastRxPacketError(self.port_num,
                                                   self.proto_ver)
        if dxl_comm_result != COMM_SUCCESS:
            print(dynamixel.getTxRxResult(self.proto_ver, dxl_comm_result))
        elif dxl_error != 0:
            print(dynamixel.getRxPacketError(self.proto_ver, dxl_error))
        else:
            print("Dynamixel#1 has been successfully freed")
        # Enable Dynamixel#2 Torque
        dynamixel.write1ByteTxRx(self.port_num, self.proto_ver, self.m2id,
                                 ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE)
        dxl_comm_result = dynamixel.getLastTxRxResult(self.port_num,
                                                      self.proto_ver)
        dxl_error = dynamixel.getLastRxPacketError(self.port_num,
                                                   self.proto_ver)
        if dxl_comm_result != COMM_SUCCESS:
            print(dynamixel.getTxRxResult(self.proto_ver, dxl_comm_result))
        elif dxl_error != 0:
            print(dynamixel.getRxPacketError(self.proto_ver, dxl_error))
        else:
            print("Dynamixel#2 has been successfully freed")
        # Enable Dynamixel#3 Torque
        dynamixel.write1ByteTxRx(self.port_num, self.proto_ver, self.m3id,
                                 ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE)
        dxl_comm_result = dynamixel.getLastTxRxResult(self.port_num,
                                                      self.proto_ver)
        dxl_error = dynamixel.getLastRxPacketError(self.port_num,
                                                   self.proto_ver)
        if dxl_comm_result != COMM_SUCCESS:
            print(dynamixel.getTxRxResult(self.proto_ver, dxl_comm_result))
        elif dxl_error != 0:
            print(dynamixel.getRxPacketError(self.proto_ver, dxl_error))
        else:
            print("Dynamixel#3 has been successfully freed")
        # Enable Dynamixel#4 Torque
        dynamixel.write1ByteTxRx(self.port_num, self.proto_ver, self.m4id,
                                 ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE)
        dxl_comm_result = dynamixel.getLastTxRxResult(self.port_num,
                                                      self.proto_ver)
        dxl_error = dynamixel.getLastRxPacketError(self.port_num,
                                                   self.proto_ver)
        if dxl_comm_result != COMM_SUCCESS:
            print(dynamixel.getTxRxResult(self.proto_ver, dxl_comm_result))
        elif dxl_error != 0:
            print(dynamixel.getRxPacketError(self.proto_ver, dxl_error))
        else:
            print("Dynamixel#4 has been successfully freed")

        #close port
        dynamixel.closePort(self.port_num)
Example #4
0
    def release(self):

        ''' This method should be called for
        the class to explicitly close the
        open socket
        '''

        dxl.closePort(self.socket)
Example #5
0
def set_torque_max(id, percentage, baudrate):
    """Sets a servo max torque.
    
    :param int id: Servo ``ìd``
    :param baudrate: Baudrate of the servo to be configured
    :param int percentage: Torque percentage
    :return: 
    --``PORT_ERROR`` case it fails to open the port.
    
    --``BAUDRATE_ERROR`` case it fails to change baudrate.
    
    --``COMM_ERROR`` case there is a communication error.
    
    --``HARDWARE_COMM_ERROR`` case there is a hardware communication error.
    
    --``NONE`` case the operation succeeds."""
    # Get methods and members of PortHandlerLinux or PortHandlerWindows
    port_num = dynamixel.portHandler(DEVICENAME)
    # Initialize PacketHandler Structs
    dynamixel.packetHandler()
    # Open port
    if dynamixel.openPort(port_num):
        print("Succeeded to open the port!")
    else:
        print("Failed to open the port!")
        return PORT_ERROR
    # Set port baudrate
    if dynamixel.setBaudRate(port_num, baudrate):
        print("Succeeded to change the baudrate!")
    else:
        print("Failed to change the baudrate!")
        return BAUDRATE_ERROR
    #Converting percentage to bit value (check dynamixel e-manual for info)
    if percentage == 100:
        value = 1023
    else:
        value = int(percentage / 0.0977517107)

    # Write goal position
    dynamixel.write2ByteTxRx(port_num, PROTOCOL_1, id, ADDR_MAX_TORQUE, value)
    dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_1)
    dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_1)
    if dxl_comm_result != COMM_SUCCESS:
        print(dynamixel.getTxRxResult(PROTOCOL_1, dxl_comm_result))
        return COMM_ERROR
    elif dxl_error != 0:
        print(dynamixel.getRxPacketError(PROTOCOL_1, dxl_error))
        return HARDWARE_COMM_ERROR
    else:
        print("Torque set to %s " % percentage)
        time.sleep(0.2)

    dynamixel.closePort(port_num)
Example #6
0
def deinit_motors():
    # disable dynamixel torque

    for DXL_ID in IDS:
        dynamixel.write1ByteTxRx(PORT_NUM, PROTOCOL_VERSION, DXL_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_DISABLE)
        dxl_comm_result = dynamixel.getTxRxResult(PORT_NUM, PROTOCOL_VERSION)
        dxl_error = dynamixel.getLastRxPacketError(PORT_NUM, PROTOCOL_VERSION)
        if dxl_comm_result != COMM_SUCCESS:
            print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
            return -1
        elif dxl_error != 0:
            print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
            return -1

    # close port
    dynamixel.closePort(PORT_NUM)
Example #7
0
def set_id(id, new_id, baudrate):
    """Sets a servo ID.
    
    :param int id: Servo ``ìd``
    :param baudrate: Baudrate of the servo to be configured
    :param int new_id: ``new_id`` to be configured
    :return: 
    --``PORT_ERROR`` case it fails to open the port.
    
    --``BAUDRATE_ERROR`` case it fails to change baudrate.
    
    --``COMM_ERROR`` case there is a communication error.
    
    --``HARDWARE_COMM_ERROR`` case there is a hardware communication error.
    
    --``NONE`` case the operation succeeds."""
    # Get methods and members of PortHandlerLinux or PortHandlerWindows
    port_num = dynamixel.portHandler(DEVICENAME)
    # Initialize PacketHandler Structs
    dynamixel.packetHandler()
    # Open port
    if dynamixel.openPort(port_num):
        print("Succeeded to open the port!")
    else:
        print("Failed to open the port!")
        return PORT_ERROR
    # Set port baudrate
    if dynamixel.setBaudRate(port_num, baudrate):
        print("Succeeded to change the baudrate!")
    else:
        print("Failed to change the baudrate!")
        return BAUDRATE_ERROR
    #Writes the new ID onto the register
    dynamixel.write1ByteTxRx(port_num, PROTOCOL_1, id, ADDR_ID, new_id)
    dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_1)
    dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_1)
    if dxl_comm_result != COMM_SUCCESS:
        print(dynamixel.getTxRxResult(PROTOCOL_1, dxl_comm_result))
        return COMM_ERROR
    elif dxl_error != 0:
        print(dynamixel.getRxPacketError(PROTOCOL_1, dxl_error))
        return HARDWARE_COMM_ERROR
    else:
        print("ID changed to: %s" % new_id)
        time.sleep(0.2)
    dynamixel.closePort(port_num)
Example #8
0
 def Quit(self):
     #self.DisableTorque()
     # Close port
     dynamixel.closePort(self.port_num)
     self.port_num = None
Example #9
0
 def disconnect(self):
     dxl.closePort(self.port)
        #     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
        
        rate.sleep()

    # Disable Dynamixel Torque
    dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_PRO_ID, ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE)
    dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_106_ID, ADDR_106_TORQUE_ENABLE, TORQUE_DISABLE)




# Close port
dynamixel.closePort(port_num)

if __name__ == '__main__':
    try:
        init()
        initDxl106()
        initDxlPro()
        control()
    except rospy.ROSInterruptException:
        # Disable Dynamixel Torque
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_PRO_ID, ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE)
Example #11
0
def set_angle_limit(id, cw_angle_limit, ccw_angle_limit, baudrate):
    """Configure the angle limits of a servo.
    
    :param int id: Servo ``ìd``
    :param int cw_angle_limit: Clockwise angle limit to be configured
    :param int ccw_angle_limit: Counter-clockwise angle limit to be configured
    :param baudrate: Baudrate of the servo to be configured
    :return: 
    --``PORT_ERROR`` case it fails to open the port.
    
    --``BAUDRATE_ERROR`` case it fails to change baudrate.
    
    --``COMM_ERROR`` case there is a communication error.
    
    --``HARDWARE_COMM_ERROR`` case there is a hardware communication error.
    
    --``NONE`` case the operation succeeds."""
    # Get methods and members of PortHandlerLinux or PortHandlerWindows
    port_num = dynamixel.portHandler(DEVICENAME)
    # Initialize PacketHandler Structs
    dynamixel.packetHandler()
    # Open port
    if dynamixel.openPort(port_num):
        print("Succeeded to open the port!")
    else:
        print("Failed to open the port!")
        return PORT_ERROR

    # Set port baudrate
    if dynamixel.setBaudRate(port_num, baudrate):
        print("Succeeded to change the baudrate!")
    else:
        print("Failed to change the baudrate!")
        return BAUDRATE_ERROR

    # Write goal position
    dynamixel.write2ByteTxRx(port_num, PROTOCOL_1, id, ADDR_CW_ANGLE_LIMIT,
                             cw_angle_limit)
    dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_1)
    dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_1)
    if dxl_comm_result != COMM_SUCCESS:
        print(dynamixel.getTxRxResult(PROTOCOL_1, dxl_comm_result))
        return COMM_ERROR
    elif dxl_error != 0:
        print(dynamixel.getRxPacketError(PROTOCOL_1, dxl_error))
        return HARDWARE_COMM_ERROR
    else:
        print("Cw angle changed to: %s" % cw_angle_limit)

    # Write goal position
    dynamixel.write2ByteTxRx(port_num, PROTOCOL_1, id, ADDR_CCW_ANGLE_LIMIT,
                             ccw_angle_limit)
    dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_1)
    dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_1)
    if dxl_comm_result != COMM_SUCCESS:
        print(dynamixel.getTxRxResult(PROTOCOL_1, dxl_comm_result))
        return COMM_ERROR
    elif dxl_error != 0:
        print(dynamixel.getRxPacketError(PROTOCOL_1, dxl_error))
        return HARDWARE_COMM_ERROR
    else:
        print("CCW angle changed to: %s" % cw_angle_limit)

    dynamixel.closePort(port_num)
Example #12
0
    # Change goal position
    if index == 0:
        index = 1
    else:
        index = 0

# Disable Dynamixel#1 Torque
dynamixel.write1ByteTxRx(port_num1, PROTOCOL_VERSION, DXL1_ID,
                         ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE)
dxl_comm_result = dynamixel.getLastTxRxResult(port_num1, PROTOCOL_VERSION)
dxl_error = dynamixel.getLastRxPacketError(port_num1, PROTOCOL_VERSION)
if dxl_comm_result != COMM_SUCCESS:
    print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
elif dxl_error != 0:
    print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))

# Disable Dynamixel#2 Torque
dynamixel.write1ByteTxRx(port_num2, PROTOCOL_VERSION, DXL2_ID,
                         ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE)
dxl_comm_result = dynamixel.getLastTxRxResult(port_num2, PROTOCOL_VERSION)
dxl_error = dynamixel.getLastRxPacketError(port_num2, PROTOCOL_VERSION)
if dxl_comm_result != COMM_SUCCESS:
    print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
elif dxl_error != 0:
    print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))

# Close port
dynamixel.closePort(port_num1)

dynamixel.closePort(port_num2)
Example #13
0
    # Change goal position
    if index == 0:
        index = 1
    else:
        index = 0


# Disable Dynamixel#1 Torque
dynamixel.write1ByteTxRx(port_num1, PROTOCOL_VERSION, DXL1_ID, ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE)
dxl_comm_result = dynamixel.getLastTxRxResult(port_num1, PROTOCOL_VERSION)
dxl_error = dynamixel.getLastRxPacketError(port_num1, PROTOCOL_VERSION)
if dxl_comm_result != COMM_SUCCESS:
    print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
elif dxl_error != 0:
    print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))

# Disable Dynamixel#2 Torque
dynamixel.write1ByteTxRx(port_num2, PROTOCOL_VERSION, DXL2_ID, ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE)
dxl_comm_result = dynamixel.getLastTxRxResult(port_num2, PROTOCOL_VERSION)
dxl_error = dynamixel.getLastRxPacketError(port_num2, PROTOCOL_VERSION)
if dxl_comm_result != COMM_SUCCESS:
    print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
elif dxl_error != 0:
    print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))

# Close port
dynamixel.closePort(port_num1)

dynamixel.closePort(port_num2)
Example #14
0
 def close(self):
     dynamixel.closePort(self.__port_num)
Example #15
0
 def Close_port(self):
     dynamixel.closePort(port_num)
     return None
Example #16
0
def set_pid_gain(id, d_gain, i_gain, p_gain, baudrate):
    """Sets the PID Gains.
    
    :param int id: Servo ``ìd``
    :param baudrate: Baudrate of the servo to be configured
    :param int d_gain: D Gain
    :param int i_gain: I Gain
    :param int p_gain: P Gain
    :return: 
    --``PORT_ERROR`` case it fails to open the port.
    
    --``BAUDRATE_ERROR`` case it fails to change baudrate.
    
    --``COMM_ERROR`` case there is a communication error.
    
    --``HARDWARE_COMM_ERROR`` case there is a hardware communication error.
    
    --``NONE`` case the operation succeeds."""
    # Get methods and members of PortHandlerLinux or PortHandlerWindows
    port_num = dynamixel.portHandler(DEVICENAME)
    # Initialize PacketHandler Structs
    dynamixel.packetHandler()
    # Open port
    if dynamixel.openPort(port_num):
        print("Succeeded to open the port!")
    else:
        print("Failed to open the port!")
        return PORT_ERROR
    # Set port baudrate
    if dynamixel.setBaudRate(port_num, baudrate):
        print("Succeeded to change the baudrate!")
    else:
        print("Failed to change the baudrate!")
        return BAUDRATE_ERROR
    # D gain config
    dynamixel.write1ByteTxRx(port_num, PROTOCOL_1, id, ADDR_D_GAIN, d_gain)
    dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_1)
    dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_1)
    if dxl_comm_result != COMM_SUCCESS:
        print(dynamixel.getTxRxResult(PROTOCOL_1, dxl_comm_result))
        return COMM_ERROR
    elif dxl_error != 0:
        print(dynamixel.getRxPacketError(PROTOCOL_1, dxl_error))
        return HARDWARE_COMM_ERROR
    else:
        print("D gain set")
        time.sleep(0.2)

    # I gain config
    dynamixel.write1ByteTxRx(port_num, PROTOCOL_1, id, ADDR_I_GAIN, i_gain)
    dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_1)
    dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_1)
    if dxl_comm_result != COMM_SUCCESS:
        print(dynamixel.getTxRxResult(PROTOCOL_1, dxl_comm_result))
        return COMM_ERROR
    elif dxl_error != 0:
        print(dynamixel.getRxPacketError(PROTOCOL_1, dxl_error))
        return HARDWARE_COMM_ERROR
    else:
        print("I gain set")
        time.sleep(0.2)

    # P gain config
    dynamixel.write1ByteTxRx(port_num, PROTOCOL_1, id, ADDR_P_GAIN, p_gain)
    dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_1)
    dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_1)
    if dxl_comm_result != COMM_SUCCESS:
        print(dynamixel.getTxRxResult(PROTOCOL_1, dxl_comm_result))
        return COMM_ERROR
    elif dxl_error != 0:
        print(dynamixel.getRxPacketError(PROTOCOL_1, dxl_error))
        return HARDWARE_COMM_ERROR
    else:
        print("P gain set")
        time.sleep(0.2)

    dynamixel.closePort(port_num)
Example #17
0
 def close_port(self):
     dynamixel.closePort(servo.port_num)
 def Close(self):
     dynamixel.closePort(self.port_num)
Example #19
0
 def Close(self):
     dynamixel.closePort(self.port_num)
Example #20
0
    def motor_operate(self, theta1_vp, theta2_vp, e, tx, ty, div_pt_x_vp, div_pt_y_vp):
        # ta1_vp and theta2_vp from list to float type

        theta1_vp = theta1_vp.tolist()
        theta2_vp = theta2_vp.tolist()

        for i in range(len(theta1_vp)):
            theta1_vp[i] = float(theta1_vp[i][0])
            theta2_vp[i] = float(theta2_vp[i][0])


        # Read a keypress and return the resulting character as a byte string.

        if os.name == 'nt':
            import msvcrt
            def getch():
                return msvcrt.getch().decode()
        else:
            import sys, tty, termios
            fd = sys.stdin.fileno()
            old_settings = termios.tcgetattr(fd)

            def getch():
                try:
                    tty.setraw(sys.stdin.fileno())
                    ch = sys.stdin.read(1)
                finally:
                    termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
                return ch

        # Control table address
        ADDR_MX_TORQUE_ENABLE = 24  # 1 byte     RW                         # Control table address is different in Dynamixel model
        ADDR_MX_GOAL_POSITION = 30  # 2 bytes    RW
        ADDR_MX_PRESENT_POSITION = 36  # 2 bytes    R
        ADDR_MX_MOVING_SPEED = 32  # 2 bytes    RW
        ADDR_MX_PRESENT_SPEED = 38  # 2 bytes    R
        ADDR_MX_PRESENT_LOAD = 40  # 2 bytes    R
        ADDR_MX_PRESENT_VOLTAGE = 42  # 1 byte     R
        ADDR_MX_GOAL_ACCELERATION = 73  # 1 byte     RW
        ADDR_MX_MAX_TORQUE = 14 # 2 bytes RW

        # PID Controller Address
        ADDR_MX_DERIVATIVE_GAIN = 26  # 1 byte     RW
        ADDR_MX_INTEGRAL_GAIN = 27  # 1 byte     RW
        ADDR_MX_PROPORTIONAL_GAIN = 28  # 1 byte     RW

        # Protocol version
        PROTOCOL_VERSION = 1  # See which protocol version is used in the Dynamixel

        # Default setting
        DXL_ID = [1, 2, 3, 4]

        BAUDRATE = 1000000
        DEVICENAME = "COM7".encode('utf-8')  # Check which port is being used on your controller
        # ex) Windows: "COM1"   Linux: "/dev/ttyUSB0" Mac: "/dev/tty.usbserial-*"
        # Resolution is 0.088 degree / Position 0~4095 /
        TORQUE_ENABLE = 1  # Value for enabling the torque
        TORQUE_DISABLE = 0  # Value for disabling the torque
        DXL_MOVING_STATUS_THRESHOLD = 5 # Dynamixel moving status threshold

        ESC_ASCII_VALUE = 0x1b  # for ESC key to escape out from the operation

        COMM_SUCCESS = 0  # Communication Success result value
        COMM_TX_FAIL = -1001  # Communication Tx Failed


        # 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_comm_result = COMM_TX_FAIL  # Communication result

        dxl_error = 0  # Dynamixel error
        dxl_present_position = 0  # Present position

        theta1 = []


        for i in range(len(theta1_vp)):
            theta1.append(int((180 * theta1_vp[i]) / (0.088 * np.pi)) + theta1_offset - 2048 + dynamixel_offset_90)
        print('=====================theta1 =======================')
        print(theta1)


        theta2 = []

        for i in range(len(theta2_vp)):
            theta2.append(int((180 * theta2_vp[i]) / (0.088 * np.pi)) + theta2_offset - 2048 + 2048)

        print('=====================theta2=======================')
        print(theta2)

        theta3 = [theta3_offset] * len(theta2_vp)
        #theta4 = [2048]

        # 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...")
            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...")
            getch()
            quit()

        #set position P gain, I gain, D gain - all 1 byte - P only 254 for unit communication
        # Motor 1
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0], ADDR_MX_DERIVATIVE_GAIN, 254)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0], ADDR_MX_INTEGRAL_GAIN, 30)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0], ADDR_MX_PROPORTIONAL_GAIN, 80)

        # Motor 2
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1], ADDR_MX_DERIVATIVE_GAIN, 254)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1], ADDR_MX_INTEGRAL_GAIN, 50)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1], ADDR_MX_PROPORTIONAL_GAIN, 120)

        # Motor 3
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2], ADDR_MX_DERIVATIVE_GAIN, 254)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2], ADDR_MX_INTEGRAL_GAIN, 50)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2], ADDR_MX_PROPORTIONAL_GAIN, 120)

        # Motor 4
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3], ADDR_MX_DERIVATIVE_GAIN, 254)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3], ADDR_MX_INTEGRAL_GAIN, 50)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3], ADDR_MX_PROPORTIONAL_GAIN, 120)

        for id in DXL_ID:
            # Enable Dynamixel Torque - 1byte
            dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, id, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE)
            dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, id, ADDR_MX_MAX_TORQUE, 1024)
            dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
            dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)

            # Initialize Goal Position 2048
            dxl_goal_position_init = 2048
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3], ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE)
        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3], ADDR_MX_MAX_TORQUE, 1024)

        # Estimate time
        start_time = time.time()
        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0], ADDR_MX_GOAL_POSITION, 2200)
        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1], ADDR_MX_GOAL_POSITION, 2200)
        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2], ADDR_MX_GOAL_POSITION, 2048)
        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3], ADDR_MX_GOAL_POSITION, 2500)
        time.sleep(2)

        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0], ADDR_MX_GOAL_POSITION, theta1[0])
        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1], ADDR_MX_GOAL_POSITION, theta2[0])
        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2], ADDR_MX_GOAL_POSITION, theta3[0])
        time.sleep(5)
        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3], ADDR_MX_GOAL_POSITION, 2038)
        time.sleep(4)

        while not (len(theta1) - index) == 0:
            # Write goal position

            dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0], ADDR_MX_GOAL_POSITION, theta1[index])
            dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1], ADDR_MX_GOAL_POSITION, theta2[index])
            dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2], ADDR_MX_GOAL_POSITION, theta3[index])
            dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3], ADDR_MX_GOAL_POSITION, 2038)


            dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
            dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
            if dxl_comm_result != COMM_SUCCESS:
                print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
            elif dxl_error != 0:
                print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))



            # write moving speed
            for id in DXL_ID:
                dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, id, ADDR_MX_MOVING_SPEED, 50)
            dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3], ADDR_MX_MOVING_SPEED, 20)

            while 1:
                # Read present position
                dxl_present_position_theta1 = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0],
                                                               ADDR_MX_PRESENT_POSITION)
                dxl_present_position_theta2 = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1],
                                                               ADDR_MX_PRESENT_POSITION)
                dxl_present_position_theta3 = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2],
                                                               ADDR_MX_PRESENT_POSITION)
                dxl_present_position_theta4 = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3],
                                                                      ADDR_MX_PRESENT_POSITION)


                dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
                dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)

                if dxl_comm_result != COMM_SUCCESS:
                    print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
                elif dxl_error != 0:
                    print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))

                # read voltage
                dxl_present_voltage = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0],
                                                             ADDR_MX_PRESENT_VOLTAGE)

                # read load
                dxl_present_load = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0], ADDR_MX_PRESENT_LOAD)

                # read present speed
                dxl_present_vel = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0], ADDR_MX_PRESENT_SPEED)

                #print("[ID:%03d] GoalPos:%03d  PresPos:%03d  PresVolt:%03d  PresLoad:%03d PresVel:%03d" % (
                #DXL_ID[0], theta2[index], dxl_present_position_theta2,
                #dxl_present_voltage, dxl_present_load, dxl_present_vel))
                #print('e', e.is_set())

                if e.is_set() == 0:
                    pass

                elif e.is_set() != 0:
                    dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3], ADDR_MX_GOAL_POSITION, 2500)
                    time.sleep(5)
                    dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0], ADDR_MX_GOAL_POSITION, 2200)
                    dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1], ADDR_MX_GOAL_POSITION, 2200)
                    dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2], ADDR_MX_GOAL_POSITION, 2048)

                    #contour
                    cam_paper = cv2.VideoCapture(0)
                    ret_paper, frame_paper = cam_paper.read()
                    frame_paper = cv2.flip(frame_paper, -1)
                    cv2.imshow('frame', frame_paper)
                    cv2.waitKey(1)
                    cont_paper = contour_paper.ContourPaper()
                    tx, ty = cont_paper.cont_paper(frame_paper)

                    print('translated tx and ty', tx, ty)


                    #paper move


                    #new space for current theta1 and theta2 to return IKINE
                    new_theta1 = np.zeros([len(theta1) - index, 1])
                    new_theta2 = np.zeros([len(theta2) - index, 1])

                    #new space of current theta1 and theta2 in degree
                    theta1_deg = np.zeros([])
                    theta2_deg = np.zeros([])
                    theta1_deg = np.delete(theta1_deg, 0)
                    theta2_deg = np.delete(theta2_deg, 0)

                    #current theta1 and theta2, remove past theta value
                    current_theta1 = theta1[index: ]
                    current_theta2 = theta2[index: ]
                    div_pt_x_vp = div_pt_x_vp[index: ]
                    div_pt_y_vp = div_pt_y_vp[index: ]

                    #print('x position, ', div_pt_x_vp)
                    #print('imhere1')
                    for i in range(len(current_theta1)):
                        theta1_deg = np.append(theta1_deg, ((current_theta1[i] - theta1_offset + 2048 - dynamixel_offset_90) * np.pi * 0.088 / 180))
                        theta2_deg = np.append(theta2_deg, ((current_theta2[i] - theta2_offset) * np.pi * 0.088 / 180))




                    IK = ikine.InverseKinematics()
                    for i in range(len(current_theta2)):
                        #print('theta1_deg', theta1_deg[i])
                        new_theta1[i], new_theta2[i] = IK.ikine(np.int(div_pt_x_vp[i]), np.int(div_pt_y_vp[i]), tx, ty)

                    #print('new_theta1, ', new_theta1)
                    new_theta1 = new_theta1.tolist()
                    new_theta2 = new_theta2.tolist()


                    for i in range(len(new_theta1)):
                        new_theta1[i] = float(new_theta1[i][0])
                        new_theta2[i] = float(new_theta2[i][0])

                    theta1 = []

                    for i in range(len(new_theta1)):
                        theta1.append(int((180 * new_theta1[i]) / (0.088 * np.pi)) + theta1_offset - 2048 + dynamixel_offset_90)
                    print('=====================theta1 =======================')
                    print(theta1)

                    theta2 = []

                    for i in range(len(new_theta2)):
                        theta2.append(int((180 * new_theta2[i]) / (0.088 * np.pi)) + theta2_offset - 2048 + 2048)

                    print('=====================theta2=======================')
                    print(theta2)

                    theta3 = [theta3_offset] * len(new_theta2)

                    index = 0

                    time.sleep(5)
                    break
                
                
                else:
                    pass


                if not ((abs(theta1[index] - dxl_present_position_theta1) > DXL_MOVING_STATUS_THRESHOLD) | ((abs(theta2[index] - dxl_present_position_theta2)) > DXL_MOVING_STATUS_THRESHOLD) | ((abs(theta3[index] - dxl_present_position_theta3)) > DXL_MOVING_STATUS_THRESHOLD)) :
                    break

                # check event
            #ABOUT TX and TY -> IKINE -> New theta1 and theta2
            init_tx = tx
            init_ty = ty
            if e.is_set() != 0:
                print('error')
                dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0], ADDR_MX_GOAL_POSITION, theta1[0])
                dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1], ADDR_MX_GOAL_POSITION, theta2[0])
                dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2], ADDR_MX_GOAL_POSITION, theta3[0])
                time.sleep(5)
                dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3], ADDR_MX_GOAL_POSITION, 2038)
                time.sleep(3)
                e.clear()

            else:
                pass


            index += 1

        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3], ADDR_MX_GOAL_POSITION, 2500)
        time.sleep(5)







        # Disable Dynamixel Torque
        for id in DXL_ID:
            dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, id, ADDR_MX_TORQUE_ENABLE, TORQUE_DISABLE)

        dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
        dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
        if dxl_comm_result != COMM_SUCCESS:
            print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
        elif dxl_error != 0:
            print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))


        # Close port
        dynamixel.closePort(port_num)

        return tx, ty
Example #21
0
def reverse_slave(id, reverse_mode_enable, slave_mode_enable, baudrate):
    """Sets the drive mode.
    
    :param int id: Servo ``ìd``
    :param baudrate: Baudrate of the servo to be configured
    :param int reverse_mode_enable: Reverse mode checkbox state
    :param int slave_mode_enable: Slave mode checkbox state
    :return: 
    --``PORT_ERROR`` case it fails to open the port.
    
    --``BAUDRATE_ERROR`` case it fails to change baudrate.
    
    --``COMM_ERROR`` case there is a communication error.
    
    --``HARDWARE_COMM_ERROR`` case there is a hardware communication error.
    
    --``NONE`` case the operation succeeds."""
    # Get methods and members of PortHandlerLinux or PortHandlerWindows
    port_num = dynamixel.portHandler(DEVICENAME)
    # Initialize PacketHandler Structs
    dynamixel.packetHandler()
    # Open port
    if dynamixel.openPort(port_num):
        print("Succeeded to open the port!")
    else:
        print("Failed to open the port!")
        return PORT_ERROR
    # Set port baudrate
    if dynamixel.setBaudRate(port_num, baudrate):
        print("Succeeded to change the baudrate!")
    else:
        print("Failed to change the baudrate!")
        return BAUDRATE_ERROR

    slave_binary = 0x02
    reverse_binary = 0x01
    drive_mode_byte = 0x00

    if reverse_mode_enable == 2:
        drive_mode_byte = reverse_binary + drive_mode_byte
    else:
        drive_mode_byte = drive_mode_byte

    if slave_mode_enable == 2:
        drive_mode_byte = slave_binary + drive_mode_byte
    else:
        drive_mode_byte = drive_mode_byte

    # Set drive mode
    dynamixel.write1ByteTxRx(port_num, PROTOCOL_1, id, ADDR_DRIVE_MODE,
                             drive_mode_byte)
    dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_1)
    dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_1)
    if dxl_comm_result != COMM_SUCCESS:
        print(dynamixel.getTxRxResult(PROTOCOL_1, dxl_comm_result))
        return COMM_ERROR
    elif dxl_error != 0:
        print(dynamixel.getRxPacketError(PROTOCOL_1, dxl_error))
        return HARDWARE_COMM_ERROR
    else:
        print("Drive mode changed")
        time.sleep(0.2)

    dynamixel.closePort(port_num)
Example #22
0
    def motor_init(self):
        # Read a keypress and return the resulting character as a byte string.

        if os.name == 'nt':
            import msvcrt

            def getch():
                return msvcrt.getch().decode()
        else:
            import sys, tty, termios
            fd = sys.stdin.fileno()
            old_settings = termios.tcgetattr(fd)

            def getch():
                try:
                    tty.setraw(sys.stdin.fileno())
                    ch = sys.stdin.read(1)
                finally:
                    termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
                return ch

        # Control table address
        ADDR_MX_TORQUE_ENABLE = 24  # 1 byte     RW                         # Control table address is different in Dynamixel model
        ADDR_MX_GOAL_POSITION = 30  # 2 bytes    RW
        ADDR_MX_PRESENT_POSITION = 36  # 2 bytes    R
        ADDR_MX_MOVING_SPEED = 32  # 2 bytes    RW
        ADDR_MX_PRESENT_SPEED = 38  # 2 bytes    R
        ADDR_MX_PRESENT_LOAD = 40  # 2 bytes    R
        ADDR_MX_PRESENT_VOLTAGE = 42  # 1 byte     R
        ADDR_MX_GOAL_ACCELERATION = 73  # 1 byte     RW
        ADDR_MX_MAX_TORQUE = 14  # 2 bytes RW

        # PID Controller Address
        ADDR_MX_DERIVATIVE_GAIN = 26  # 1 byte     RW
        ADDR_MX_INTEGRAL_GAIN = 27  # 1 byte     RW
        ADDR_MX_PROPORTIONAL_GAIN = 28  # 1 byte     RW

        # Protocol version
        PROTOCOL_VERSION = 1  # See which protocol version is used in the Dynamixel

        # Default setting
        DXL_ID = [1, 2, 3, 4]

        BAUDRATE = 1000000
        DEVICENAME = "COM7".encode(
            'utf-8')  # Check which port is being used on your controller
        # ex) Windows: "COM1"   Linux: "/dev/ttyUSB0" Mac: "/dev/tty.usbserial-*"
        # Resolution is 0.088 degree / Position 0~4095 /
        TORQUE_ENABLE = 1  # Value for enabling the torque
        TORQUE_DISABLE = 0  # Value for disabling the torque
        DXL_MOVING_STATUS_THRESHOLD = 5  # Dynamixel moving status threshold

        ESC_ASCII_VALUE = 0x1b  # for ESC key to escape out from the operation

        COMM_SUCCESS = 0  # Communication Success result value
        COMM_TX_FAIL = -1001  # Communication Tx Failed

        # 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_comm_result = COMM_TX_FAIL  # Communication result

        dxl_error = 0  # Dynamixel error
        dxl_present_position = 0  # Present position

        #set position P gain, I gain, D gain - all 1 byte - P only 254 for unit communication
        # Motor 1
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0],
                                 ADDR_MX_DERIVATIVE_GAIN, 254)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0],
                                 ADDR_MX_INTEGRAL_GAIN, 30)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0],
                                 ADDR_MX_PROPORTIONAL_GAIN, 65)

        # Motor 2
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1],
                                 ADDR_MX_DERIVATIVE_GAIN, 254)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1],
                                 ADDR_MX_INTEGRAL_GAIN, 50)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1],
                                 ADDR_MX_PROPORTIONAL_GAIN, 120)

        # Motor 3
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2],
                                 ADDR_MX_DERIVATIVE_GAIN, 254)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2],
                                 ADDR_MX_INTEGRAL_GAIN, 50)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2],
                                 ADDR_MX_PROPORTIONAL_GAIN, 120)

        # Motor 4
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3],
                                 ADDR_MX_DERIVATIVE_GAIN, 254)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3],
                                 ADDR_MX_INTEGRAL_GAIN, 50)
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3],
                                 ADDR_MX_PROPORTIONAL_GAIN, 120)

        for id in DXL_ID:
            # Enable Dynamixel Torque - 1byte
            dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, id,
                                     ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE)
            dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, id,
                                     ADDR_MX_MAX_TORQUE, 1024)
            dxl_comm_result = dynamixel.getLastTxRxResult(
                port_num, PROTOCOL_VERSION)
            dxl_error = dynamixel.getLastRxPacketError(port_num,
                                                       PROTOCOL_VERSION)

            # Initialize Goal Position 2048
            dxl_goal_position_init = 2048
        dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3],
                                 ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE)
        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3],
                                 ADDR_MX_MAX_TORQUE, 1024)

        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[3],
                                 ADDR_MX_GOAL_POSITION, 2500)
        time.sleep(5)

        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[0],
                                 ADDR_MX_GOAL_POSITION, 2048)
        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[1],
                                 ADDR_MX_GOAL_POSITION, 2048)
        dynamixel.write2ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID[2],
                                 ADDR_MX_GOAL_POSITION, 2048)

        time.sleep(3)

        # Disable Dynamixel Torque
        for id in DXL_ID:
            dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, id,
                                     ADDR_MX_TORQUE_ENABLE, TORQUE_DISABLE)

        dxl_comm_result = dynamixel.getLastTxRxResult(port_num,
                                                      PROTOCOL_VERSION)
        dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
        if dxl_comm_result != COMM_SUCCESS:
            print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
        elif dxl_error != 0:
            print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))

        # Close port
        dynamixel.closePort(port_num)
Example #23
0
def search(id_search_min, id_search_max, baudrates_search_list):
    """Search for servos in range of ``id_search_min`` and ``id_search_max`` for all baudrates in ``baudrates_search_list``.

    :param int id_search_min: ID to start searching.
    :param int id_search_max: ID to stop pinging.
    :param list baudrates_search_list: List containing the baudrates that the user want to search.
    :return: ``found_servos`` list containing the servos found.
    :rtype: List containing the ``Dynamixel`` object servos"""
    #SEARCHING IN THE NETWORK
    # Get methods and members of PortHandlerLinux or PortHandlerWindows
    port_num = dynamixel.portHandler(DEVICENAME)
    # Initialize PacketHandler Structs
    dynamixel.packetHandler()
    # Open port
    if dynamixel.openPort(port_num):
        print("Succeeded to open the port!")
    else:
        print("Failed to open the port!")
        return PORT_ERROR

    #Declaring the limits of the search
    init = id_search_min
    end = id_search_max
    #List containing the found servos in the network
    found_servos = []
    #Tries to ping in protocols 1 and 2
    for protocol in PROTOCOL_VERSIONS:
        print("Using protocol %s" % str(protocol))
        #Loop through all baudrates
        for baudrate in baudrates_search_list:
            actual_id = init
            # Set port baudrate
            if dynamixel.setBaudRate(port_num, baudrate):
                print("Succeeded to change the baudrate!")
            else:
                print("Failed to change the baudrate!")
                return BAUDRATE_ERROR

            time.sleep(0.2)

            while actual_id <= end:
                print("Pinging in ID: %s " % actual_id)
                # Try to ping the Dynamixel
                # Get Dynamixel model number
                dxl_model_number = dynamixel.pingGetModelNum(
                    port_num, protocol, actual_id)
                dxl_comm_result = dynamixel.getLastTxRxResult(
                    port_num, protocol)
                dxl_error = dynamixel.getLastRxPacketError(port_num, protocol)
                if dxl_comm_result != COMM_SUCCESS:
                    print(dynamixel.getTxRxResult(protocol, dxl_comm_result))
                elif dxl_error != 0:
                    print(dynamixel.getRxPacketError(protocol, dxl_error))
                else:
                    print(
                        "[ID:%03d] ping Succeeded. Dynamixel model number : %d"
                        % (actual_id, dxl_model_number))
                    servo = Dynamixel()
                    servo.id = actual_id
                    servo.baudrate = baudrate
                    servo.protocol = protocol
                    servo.model = dxl_model_number
                    found_servos.append(servo)
                actual_id = actual_id + 1

    # Close port
    dynamixel.closePort(port_num)
    return found_servos
Example #24
0
 def closePort(self):
     for port in self.port_num:
         dynamixel.closePort(port)
Example #25
0
    print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
elif dxl_error != 0:
    print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
else:
  print("[ID:%03d] Set Dynamixel baudnum to : %d" % (DXL_ID, NEW_BAUDNUM))

# Set port baudrate to BAUDRATE
if dynamixel.setBaudRate(port_num, BAUDRATE):
    print("Succeed to change the controller baudrate to : %d" % (BAUDRATE))
else:
    print("Failed to change the controller baudrate")
    getch()
    quit()

sleep(0.2)

# Read Dynamixel baudnum
dxl_baudnum_read = dynamixel.read1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_MX_BAUDRATE)
dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)
dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
if dxl_comm_result != COMM_SUCCESS:
    print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
elif dxl_error != 0:
    print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))
else:
  print("[ID:%03d] Dynamixel baudnum is now : %d" % (DXL_ID, dxl_baudnum_read))


# Close port
dynamixel.closePort(port_num)
Example #26
0
    def motor_disable(self, theta1_vp, theta2_vp):
        if os.name == 'nt':
            import msvcrt

            def getch():
                return msvcrt.getch().decode()
        else:
            import sys, tty, termios
            fd = sys.stdin.fileno()
            old_settings = termios.tcgetattr(fd)

            def getch():
                try:
                    tty.setraw(sys.stdin.fileno())
                    ch = sys.stdin.read(1)
                finally:
                    termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
                return ch

        # Control table address
        ADDR_MX_TORQUE_ENABLE = 24  # 1 byte     RW                         # Control table address is different in Dynamixel model

        # Protocol version
        PROTOCOL_VERSION = 1  # See which protocol version is used in the Dynamixel

        # Default setting
        DXL_ID = [1, 2, 3, 4]

        BAUDRATE = 1000000
        DEVICENAME = "COM7".encode(
            'utf-8')  # Check which port is being used on your controller
        # ex) Windows: "COM1"   Linux: "/dev/ttyUSB0" Mac: "/dev/tty.usbserial-*"
        # Resolution is 0.088 degree / Position 0~4095 /
        TORQUE_ENABLE = 1  # Value for enabling the torque
        TORQUE_DISABLE = 0  # Value for disabling the torque
        DXL_MOVING_STATUS_THRESHOLD = 3  # Dynamixel moving status threshold

        ESC_ASCII_VALUE = 0x1b  # for ESC key to escape out from the operation

        COMM_SUCCESS = 0  # Communication Success result value
        COMM_TX_FAIL = -1001  # Communication Tx Failed

        # 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_comm_result = COMM_TX_FAIL  # Communication result

        dxl_error = 0  # Dynamixel error
        dxl_present_position = 0  # Present position

        # Disable Dynamixel Torque
        for id in DXL_ID:
            dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, id,
                                     ADDR_MX_TORQUE_ENABLE, TORQUE_DISABLE)

        dxl_comm_result = dynamixel.getLastTxRxResult(port_num,
                                                      PROTOCOL_VERSION)
        dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)
        if dxl_comm_result != COMM_SUCCESS:
            print(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result))
        elif dxl_error != 0:
            print(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error))

        # Close port
        dynamixel.closePort(port_num)