Python-Based Dobot Magician Control on Linux


#1

First off, this post is not a question but a documentation to overcome various undocumented and unfixed issues. I run into the following two issues which are related:
Issue 1
Issue 2

Objective: Writing a python application to control the Dobot Magician robot on Linux 64bit

Issues: Dobot DLL causes seg faults for all Set*() function calls

### Prerequisites ###
Ubuntu 22.04 LTS
Dobot Magician
Python3 and Python2 tested

### Compiling Dobot DLL from Source ###
Obtain the source files from the Dobot Download Centre. Once logged in, please download the Dobot Demo v2.3 file. Untar it and copy the magiciandll-master.zip to your Linux machine under /usr/local/src/.

Next, install various packages you will require to compile from source:

apt install -y build-essential \
libxcomposite-dev \
libxcb-glx0-dev \
libx11-xcb-dev \
libxrender-dev \
libxkbcommon-x11-dev \
libfontconfig-dev \
libwayland-cursor0 \
libgl-dev \
unzip

Next, install QT. Note, the QT installer will pop up a window where you go through a couple of steps. Either connect a display to your Linux machine or use x-forwarding when SSHing into the machine (ssh -x):

cd /usr/local/src/
wget https://download.qt.io/archive/qt/5.12/5.12.11/qt-opensource-linux-x64-5.12.11.run
chmod +x qt-opensource-linux-x64-5.12.11.run
./qt-opensource-linux-x64-5.12.11.run

Once QT is installed, compile and install the DLL

/usr/local/src/# unzip magiciandll-master.zip
/usr/local/src/magiciandll-master# /opt/Qt5.12.11/5.12.11/gcc_64/bin/qmake
/usr/local/src/magiciandll-master# make
/usr/local/src/magiciandll-master# make install

Now update your system to include the newly compiled libraries. I chose to add the following to my ~/.bashrc

export DOBOT_LIB_PATH=/usr/DobotDllOutput
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$DOBOT_LIB_PATH

### Patch Python DLL Wrapper ###
The Dobot Demo 2.3 RAR file includes an example for Python. Unzip the demo-magician-python-64-master.zip and find the DobotDllType.py file which is imported in your python code via

import DobotDllType as dType

The DobotDllType.py requires a patch which I uploaded to pastebin.com. Download the patch, save it as dobot.patch in the same folder as the DobotDllType.py and apply it via:

patch -u DobotDllType.py -i dobot.patch

Enjoy controlling your Dobot Magician on Linux!

PS: If the Dobot support team reads this post, please update your documentation on how to compile it from source on Linux and please patch your python ctypes wrapper. Note, under python2, the Chinese characters in the .py cannot be parsed by the python compiler, as it expects ASCI-compliant characters. Thus, I had to remove them.


#2

@seronline Thanks for this post. I have followed your instructions. I have some issues linking the so library in the DobotDllType.py, but finally it was done, I changed line 595 in the patched file for using just DLL. After patch the DobotDllType.py, and my modification and running DobotControl.py, I can see a Segmentation Fault when I run the DobotControl.py with sudo, if I don’t use sudo it shows me that the port is occupied, and I have been aware that the port is not opened when I run the script.

Any ideas of how I need to modify the DobotDllType.py. I can see that it is a problem how we are calling the api, because the robot is connected and working on Ubuntu.

Appreciate your help on this.


#3

happy that my post is helpful :slight_smile: Can you please provide the change you made in DobotDllType.py for others to follow suit? I checked the patched file and Line 595 is a “continue” prompt within a while loop.

As for your USB port issue, this orthogonal to the scope of this topic and deserves a dedicated thread. But in a nutshell, if the library says the USB port is occupied, then another process is using it. Make sure you have the correct port configured. If you plug-in the Dobot, check with dmesg or journalctl which /dev/ttyUSB port it was assigned by the OS. Depending on the USB devices you have connected to your machine, /dev/ttyUSB0 might be already taken.


#4

@seronline So, I have followed the process you have outlined. I can see that Dobot is connected to ttyUSB1 and SearchDobot, GetPose, etc. results with the information i think is right. But, whenever, I try to invoke ConnectDobot, it results into segmentation fault. I tried debugging a bit, and I see that ConnectDobot returns a device ID to something unrecognizable - it is consistent, some number like 1919243124, but it should be Magician or, in other words 2 - which is completely weird.

Do you have any suggestions? Is there a different firmware that I might need to use? I know it is a long shot, but it is very hard to get any help on this machine, so trying my luck.


#5

Hi @amul1umt

I haven’t used the robots in a while and cannot remember what I got as the device ID. But a number sounds about right. Just wondering whether you have ensured the robot is actually connected by checking the connection state, e.g.

state = dtype.ConnectDobot(dtype.load(), "/dev/ttyUSB1", 115200)[0]
if state == dtype.DobotConnect.DobotConnect_NoError: 
   print("Connected")

I also remember to be able to see logs from the Dobot library via dtype.SetDebugEnable() allowing you to debug a bit what’s going on under the hood.

Hope that helps


#6

Hi, I wonder if you have solved your problem? I encountered the same problem after following the original post.


#7

Note: this post works for me if I specified the port in Python 3.7.16
I have tried the very same code on 3.5, 3.8, and 3.11, and I get segmentation fault on those.


#8

also, api = dType.load() does not work for me, but api = CDLL("libDobotDll.so") works. (add from ctypes import CDLL at the start of the code`)