Linux gcc is used for this example.


Setting Environment

Using Dynamixel API

STEP1) Designate header file

Add #include <dynamixel.h> to source file.

STEP2) Call a function

Call necessary functions for the source.  Please refer to API Reference on the usage of each function.



Read/Write Example

This is a Read/Write example of Dynamixel.  


The communication mode of USB2Dynamixe is selected for the appropriate environment.

The information regarding communication modes pursuant to the location of switch is as follows:

1 - TTL Mode

It can communicate with devices using 3-pin cable such as AX-12/12+, AX-S1, and AX-20.

2 - RS485 Mode

It can communicate with devices using 4-pin cable such as DX-Series, RX-Series, etc.

3 - RS232 Mode

It can communicate with devices using serial cable such as CM-5, CM-510, etc.


One Dynamixel msut be connected to USB2Dynamixel.

The ID of used Dynamixel must be 1, and the communication speed must be set to 1Mbps.  


In case of using Ex-series Dynamixel, the source for EX-series must be used.



///////// Open USB2Dynamixel ////////////

if( dxl_initialize(deviceIndex, baudnum) == 0 )


printf( "Failed to open USB2Dynamixel!\n" );

printf( "Press Enter key to terminate...\n" );


return 0;



printf( "Succeed to open USB2Dynamixel!\n"

The source above is to check whether the initialization is done properly or not.

Since the initialization is succeeded, 1 is returned; if it is failed, 0 is returned, the failed source is included in "If" sentence, and teh succeeded source goes to "Else" sentence.

dxl_initialize function is called from dynamixel API.

deviceIndex means the number of a connected device.

baudnum means the set value of Baud Rate.

deviceIndex and baudnum must be set pursuant to users' status.

deviceIndex is set to 0, baudnum is originally set to 1Mbps.


// Close device


The source above is for termination.

The connection is ternminated by calling dxl_teminate function from dynamixel API.


// Write goal position

dxl_write_word( DEFAULT_ID, P_GOAL_POSITION_L, GoalPos[index] );

The source above is to move Dynamixel to goal position.

dxl_write_word function is called from dynamixel API.

DEFAULT_ID means the ID value of currently connected Dynamixel.

 P_GOAL_POSITION_L value declared, in advance, the starting address value (30), which belongs to GOAL_POSITION of the Dynamixel's Control Table.

GoalPos(index) declares GoalPosition value which is desired to move by array; thus, the relevant value is transmitted whenever the index is changed.


※ Word-type and byte-type functions are used to read and write the value on Dynamixel.

If the values of Dynamixel in Control Table are byte-type, the functions such as dxl_write_byte and dxl_read_byte are used, and if the functions are word-type,  dxl_write_word and dxl_read_word functions are used.

Since position information is a word-type function, word-type functions are used for the following examples.


// Read present position

PresentPos = dxl_read_word( DEFAULT_ID, P_PRESENT_POSITION_L );

The source above reads the present position value of Dynamixel on PresentPos variables.

dxl_read_word function is called from dynamixel API.

DEFAULT_ID means the ID value of currently connected Dynamixel.

P_PRESENT_POSITION_L value declared, in advance, the starting address value (36), which saves the present position of Dynamixel.


CommStatus = dxl_get_result();

if( CommStatus == COMM_RXSUCCESS )








The source above saves the packet communication result in CommStatus variables

dxl_get_result function is used whether the result is correct or not.

If the result is successful, it checks the error status during the operation of Dynamixel by calling PrintErrorCode function.

If the communication result is failed, the error information returned from dxl_get_result function is transmitted to PrintCommStatus function and printed on the screen.


○ PrintErrorCode Function

PrintErrorCode function checks the status packet error by calling dxl_get_rxpacket_error function.

void PrintErrorCode()


// Method 2. using function

if(dxl_get_rxpacket_error(ERRBIT_VOLTAGE) == 1)

printf("Input voltage error!\n");

if(dxl_get_rxpacket_error(ERRBIT_ANGLE) == 1)

printf("Angle limit error!\n");

if(dxl_get_rxpacket_error(ERRBIT_OVERHEAT) == 1)

printf("Overheat error!\n");

if(dxl_get_rxpacket_error(ERRBIT_RANGE) == 1)

printf("Out of range error!\n");

if(dxl_get_rxpacket_error(ERRBIT_CHECKSUM) == 1)

printf("Checksum error!\n");

if(dxl_get_rxpacket_error(ERRBIT_OVERLOAD) == 1)

printf("Overload error!\n");

if(dxl_get_rxpacket_error(ERRBIT_INSTRUCTION) == 1)

printf("Instruction code error!\n");



○  PrintCommStatus Function

PrintCommStatus prints the error information returned from dxl_get_result function on the screen.

void PrintCommStatus(int CommStatus)





printf("COMM_TXFAIL: Failed transmit instruction packet!\n");



printf("COMM_TXERROR: Incorrect instruction packet!\n");



printf("COMM_RXFAIL: Failed get status packet from device!\n");



printf("COMM_RXWAITING: Now recieving status packet!\n");



printf("COMM_RXTIMEOUT: There is no status packet!\n");



printf("COMM_RXCORRUPT: Incorrect status packet!\n");



printf("This is unknown error code!\n");





// Check moving done

Moving = dxl_read_byte( DEFAULT_ID, P_MOVING );

The source above checks and saves whether Dynamixel is moving or not, using dxl_read_byte on Moving variable.

DEFAULT_ID means the ID value of relevant Dynamixel.

P_MOVING means moving part's address value (46) in Dynamixel's Control Table.

Moving value is checked using dxl_read_byte because Moving is byte-type data.


Sync Write Example


Three Dynamixels must be connected to USB2Dynamixel.

The IDs of each Dynamixel are 1, 2, and 3, and the communication speed must be set to 1Mbps.

In case of using EX-series Dynamixel, the source for Ex-series must be used.



for( i=0; i<NUM_ACTUATOR; i++ )


id[i] = i+1;

phase[i] = 2*PI * (float)i / (float)NUM_ACTUATOR;


The source above is to insert the ID value of Dynamixel on the ID array.

The Dynamixel with the IDs of 1, 2, and 3 are used in the examples.

If Dynamixel with different ID value is used, the Dynamixel's ID value must be changed, or the ID array value in the source must be inputted as the relevant Dynamixle ID value.

Phase means the initial phase of each Dynamixel.




dxl_set_txpacket_parameter(0, P_GOAL_POSITION_L);

dxl_set_txpacket_parameter(1, 2);

for( i=0; i<NUM_ACTUATOR; i++ )


dxl_set_txpacket_parameter(2+3*i, id[i]);

GoalPos = (int)((sin(theta+phase[i]) + 1.0) * (float)AmpPos);

printf( "%d  ", GoalPos );

dxl_set_txpacket_parameter(2+3*i+1, dxl_get_lowbyte(GoalPos));

dxl_set_txpacket_parameter(2+3*i+2, dxl_get_highbyte(GoalPos));




printf( "\n" );



CommStatus = dxl_get_result();

The source above is to make syncwrite packet.

Please refer to Dynamixel Packet Structure for the packet structure, and the packet must be made using necessary functions to make packet.