Dobot

Tutorials: Dobot Magician Desktop Robot

Example Codes:


# Import necessary modules for serial port communication

from serial.tools import list_ports


# pydobot library for controlling Dobot robotic arm

import pydobot

##############################


# Libraries for socket communication, JSON parsing, datetime and multithreading

import socket

import json

from datetime import datetime

import time

from threading import Thread

import threading


# Declare global variables for storing positional coordinates

global X;global Y;global Z


# List all available serial ports and print them

available_ports = list_ports.comports()

print(f'available ports: {[x.device for x in available_ports]}')


# Select the first available port

port = available_ports[0].device


# Initialize the Dobot device on the selected port

device = pydobot.Dobot(port=port, verbose=False)


# Command to move the Dobot to a specified position 

device.move_to(225, 0, 25, 0, wait=True) 

# Get the current pose of the Dobot

(x, y, z, r, j1, j2, j3, j4) = device.pose()

# Initialize global positional coordinates to 0

X = 0;Y = 0;Z = 0


# Function for a thread to continuously receive data from a socket

def Recv_data_thread(client_socket,message):

    while True:

        # Send a message to the server

        client_socket.send(message.encode('utf-8'))  

        

        # Receive a response from the server

        data = client_socket.recv(1024).decode()  

        

        # Parse the received data as JSON

        parsed_data = json.loads(data)

        

        # Update global positional variables based on received data

        if ('X' in parsed_data.keys()):

            global X

            X = float(parsed_data['X'])

        elif('Y' in parsed_data.keys()):

            global Y

            Y = float(parsed_data['Y'])

        else:

            global Z

            Z = float(parsed_data['Z'])


# Client setup for socket communication


# Define host and port details for socket connection

host = '127.0.0.1'

port = 9000  


# Create a socket and connect to the server

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  

client_socket.connect((host, port))


# Define a message to send to the server

origin = {"id": "dobot", "action": "location change"}

message = json.dumps(origin)


# Start the data receiving thread

t1 = Thread(target=Recv_data_thread, args=(client_socket, message))

t1.start()


# Continuously print and adjust Dobot position based on received data 

# until the message is "bye"

while message.lower().strip() != 'bye':

    print("x= {}".format(x+X*2))

    print("y= {}".format(y+Y*2))

    print("z= {}".format(z+Z*2))

    

    # Move the Dobot based on the updated coordinates

    device.move_to(x+X*2, y+Y*2, z+Z*2, r, wait=False) 

    time.sleep(0.05)


# Close the Dobot device

device.close()