Matlab interface for Dynamixel motors and Bioloid robot

This is an old one, but is working quite nice and should be still useful for working with Dynamixel motor (AX-12+, AX-12A).
The goal was to have a nice toolbox used for direct control of Dynamixel motors using USB2Dynamixel or any other USB-Serial (half-duplex, ~1Mbps) adapter.

Source code and short description:

Steps to get it working:
clone the repository

 git clone

Copy the whole directory +hw to your current Matlab directory. Matlab recognises
all the directories that are written as +dir as structures with functions inside,
so you can use dir.function() at the Matlab console.

First choose serial port number – 0,1,2,… for linux /dev/ttyUSB0, 1, etc., for Windows
5,6,7,8 for COM5, COM6, … ans so on.

hw.start( 0 )

This initialises the serial COM port for the USB2Dynamixel, now if you have a motor (let’s say number 13)
you can move it to the zero position:

hw.set_position( 13, 0 )

function set_position works in radians, so the usual permitted range for standard AX-12 would
be -2.61…2.61 (the Murata SV-01 potentiometer has the range of about 300 degrees). To retrieve a current position:

hw.get_position( 13 )

and to switch off the internal PID control for a moment:

hw.set_off( 13 )

If you want to limit the speed of one of the motors you can use

hw.set_speed( 13, 0.1 )

which limits the max speed to 10% (parameter range 0=0%, 1=100%).

If you would like to switch between closed loop PID-controlled joint mode and open-loop (servo behaves like a wheel
then, but still can read the positions during movement! in other words: you control the PWM of the H-bridge on the DC motor controller)

hw.set_mode_joint( 13 )
hw.set_position( 13, 0 )
hw.set_mode_wheel( 13 )
hw.set_speed( 13, -0.25 )

set_speed() now controls the PWM on the motor, remember that now you can move forward and backwards (-1.0 … 1.0 )

get_pose() and set_pose() reads the current pose of a humanoid robot, including a little offset for hips so they don’t
collide if you accidentally push zeros to all joints.

Possible errors:

Error using loadlibrary (line 419)
Building libdxl_thunk_glnxa64 failed.  Compiler output is:
gcc -I"/usr/local/matlab2012a/extern/include"  -D_GNU_SOURCE  -fexceptions -fPIC -fno-omit-frame-pointer
-pthread -I"/home/anil/phd/ravia/iva" -I"/home/anil/phd/ravia/iva/+hw/GLNXA64" "libdxl_thunk_glnxa64.c" -o
"" -Wl,-E -shared
In file included from libdxl_thunk_glnxa64.c:18:0:
/usr/local/matlab2012a/extern/include/tmwtypes.h:819:9: error: unknown type name 'char16_t'
 typedef char16_t CHAR16_T;

Error in hw.start (line 11)
		loadlibrary(['+hw/' computer '/'],['+hw/' computer '/libdxl.h'])

that means your compiler options are a little bit off with the compiler itself.
I solved mine by adding to the CFLAGS:


for correct gcc options and to CXXFLAGS

to g++ options. Remember which architecture you are on: 32-bit - glnx86 or 64-bit - glnxa64.

The config file is located either in your Matlab directory (main one):

or if you already tried mex -setup you will have a local copy in

In general: mex -setup  will show you where the file is while doing the configuration of the compiler.


     CFLAGS='-ansi -D_GNU_SOURCE'
     CFLAGS='-ansi -D_GNU_SOURCE'


     CFLAGS='-ansi -D_GNU_SOURCE  -Dchar16_t=UINT16_T'
     CXXFLAGS='-ansi -D_GNU_SOURCE  -std=c++11'
     CFLAGS='-ansi -D_GNU_SOURCE  -Dchar16_t=UINT16_T'
     CXXFLAGS='-ansi -D_GNU_SOURCE  -std=c++11'