MATLAB

This is an example for MATLAB.

This example has been tested in MATLAB 7.0.

 

Setting Environement

The environment must be set in MATLAB to execute the examples.

 

◆  Registering the Path

The path must be registered to use Dynamixel SDK's API in MATLAB.

 

1.  Click File -> Set Path in the menu.

 

 

2. Click "ADD Folder."

 

 

3. Register the locations of dynamixel.dll and dynamixel.h in MATLAB.  Dynamixel.dll is in the bin folder where DynamixelSDK is installed; dynamixel.h is in the import folder.

(For Example: C:\Program Files\ROBOTIS\USB2Dynamixel\bin\dynamixel.dll

                                        C:\Program Files\ROBOTIS\USB2Dynamixel\import\dynamixel.h )

 

4. Click "Save" and close the window after confirming the path is added.

      

 

◆  Using Dynamixel API

(STEP 1) Load Library.

loadlibrary(‘dynamixel’, ‘dynamixel.h’)

(STEP 2) Check the list of library functions.

libfunctions(‘dynamixel’)

(STEP 3) Call the functions. Please refer to API Reference on the usage of each function.

Returned value = calllib(‘dynamixel’, ‘function name’, function data1, function data2, … )

(For example, value = calllib(‘dynamixel’, ‘dxl_read_word’, 1, 36 ) )

(STEP 4) Unload Library when the program is ended.

unloadlibrary(‘dynamixel’)

loadlibrary('dynamixel', 'dynamixel.h');

libfunctions('dynamixel');

DEFAULT_PORTNUM = 3;  //COM3

DEFAULT_BAUDNUM = 1;  //1Mbps

 

calllib('dynamixel', 'dxl_initialize', DEFAULT_PORTNUM, DEFAULT_BAUDNUM);

 

unloadlibrary('dynamixel');

 

Read/Write Example

This is a Read/Write example of Dynamixel.  

[Preparation]

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 must 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.

 

[Source]

%open device

res = calllib('dynamixel','dxl_initialize',DEFAULT_PORTNUM,DEFAULT_BAUDNUM);

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

If the initialization is successful, 1 is saved; if it is failed, 0 is saved.

Using calllib function of MATLAB, dxl_initialize function is called from dynamixel API.

DEFAULT_PORTNUM means the number of a connected device.

DEFAULT_BAUDNUM means the set value of Baud Rate.

DEFAULT_PORTNUM and DEFAULT_BAUDNUM must be set pursuant to users' status.

PORTNUM is originally set to COM3, DEFAULT_BAUDNUM is set to 1Mbps.

 

%Close Device

calllib('dynamixel','dxl_terminate');

The source above is for termination.

The connection is terminated by calling dxl_teminate function from dynamixel API using calllib function of MATLAB.

 

%Write goal position

calllib('dynamixel','dxl_write_word',id,P_GOAL_POSITION,GoalPos(index));  

The source above is to move Dynamixel to goal position.

Using calllib function of MATLAB, dxl_write_word function is called from dynamixel API.

ID means the ID value of currently connected Dynamixel.

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

GoalPos(index) declares GoalPosition value by array, and 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 = int32(calllib('dynamixel','dxl_read_word',id,P_PRESENT_POSITION));

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

Using calllib function of MATLAB, dxl_read_word function is called from ynamixel API.

ID means the ID value of currently connected Dynamixel.

P_PRESENT_POSITION value declared, in advanec, the address value (36), which saves the present position of Dynamixel.

 

CommStatus = int32(calllib('dynamixel','dxl_get_result'));

if CommStatus == COMM_RXSUCCESS

PrintErrorCode();

else

             PrintCommStatus(CommStatus);

The source above saves the packet communication result in CommStatus variable.

dxl_get_result functions is used to check 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

function [] = PrintErrorCode()

global ERRBIT_VOLTAGE

ERRBIT_VOLTAGE     = 1;

global ERRBIT_ANGLE

ERRBIT_ANGLE       = 2;

global ERRBIT_OVERHEAT

ERRBIT_OVERHEAT    = 4;

global ERRBIT_RANGE

ERRBIT_RANGE       = 8;

global ERRBIT_CHECKSUM

ERRBIT_CHECKSUM    = 16;

global ERRBIT_OVERLOAD

ERRBIT_OVERLOAD    = 32;

global ERRBIT_INSTRUCTION

ERRBIT_INSTRUCTION = 64;

 if int32(calllib('dynamixel','dxl_get_rxpacket_error', ERRBIT_VOLTAGE))==1

     disp('Input Voltage Error!');

 elseif int32(calllib('dynamixel','dxl_get_rxpacket_error',ERRBIT_ANGLE))==1

     disp('Angle limit error!');

 elseif int32(calllib('dynamixel','dxl_get_rxpacket_error',ERRBIT_OVERHEAT))==1

     disp('Overheat error!');

 elseif int32(calllib('dynamixel','dxl_get_rxpacket_error',ERRBIT_RANGE))==1

     disp('Out of range error!');

 elseif int32(calllib('dynamixel','dxl_get_rxpacket_error',ERRBIT_CHECKSUM))==1

     disp('Checksum error!');

 elseif int32(calllib('dynamixel','dxl_get_rxpacket_error',ERRBIT_OVERLOAD))==1

     disp('Overload error!');

 elseif int32(calllib('dynamixel','dxl_get_rxpacket_error',ERRBIT_INSTRUCTION))==1

     disp('Instruction code error!');

 end

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

 

○  PrintCommStatus Function

 function [] = PrintCommStatus( CommStatus )

global COMM_TXSUCCESS

COMM_TXSUCCESS     = 0;

global COMM_RXSUCCESS

COMM_RXSUCCESS     = 1;

global COMM_TXFAIL

COMM_TXFAIL        = 2;

global COMM_RXFAIL

COMM_RXFAIL        = 3;

global COMM_TXERROR

COMM_TXERROR       = 4;

global COMM_RXWAITING

COMM_RXWAITING     = 5;

global COMM_RXTIMEOUT

COMM_RXTIMEOUT     = 6;

global COMM_RXCORRUPT

COMM_RXCORRUPT     = 7;

switch(CommStatus)

    case COMM_TXFAIL

        disp('COMM_TXFAIL : Failed transmit instruction packet!');

    case COMM_TXERROR

        disp('COMM_TXERROR: Incorrect instruction packet!');

    case COMM_RXFAIL

        disp('COMM_RXFAIL: Failed get status packet from device!');

    case COMM_RXWAITING

        disp('COMM_RXWAITING: Now recieving status packet!');

    case COMM_RXTIMEOUT

        disp('COMM_RXTIMEOUT: There is no status packet!');

    case COMM_RXCORRUPT

        disp('COMM_RXCORRUPT: Incorrect status packet!');

    otherwise

        disp('This is unknown error code!');

  

end

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

 

%Check moving done

Moving = int32(calllib('dynamixel','dxl_read_byte',id,P_Moving));

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

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

[Preparation]

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.

 

[Source]

for i = 1:1:NUM_ACTUATOR

    id(i) = i;

end

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 teh relevant Dynamixel ID value.

 % Make syncwrite packet

calllib('dynamixel','dxl_set_txpacket_id',BROADCAST_ID);

calllib('dynamixel','dxl_set_txpacket_instruction',INST_SYNC_WRITE);

calllib('dynamixel','dxl_set_txpacket_parameter',0,P_GOAL_POSITION);

calllib('dynamixel','dxl_set_txpacket_parameter',1,2);

for i = 0:1:NUM_ACTUATOR-1

 calllib('dynamixel','dxl_set_txpacket_parameter',2+3*i,id(i+1));

               GoalPos = ((sin(theta+phase(i+1)) + 1) * AmpPos);

               Position(i+1) = int32(GoalPos);

               low = calllib('dynamixel','dxl_get_lowbyte',GoalPos);

               calllib('dynamixel','dxl_set_txpacket_parameter',2+3*i+1,low);

               high = calllib('dynamixel','dxl_get_highbyte',GoalPos);

               calllib('dynamixel','dxl_set_txpacket_parameter',2+3*i+2,high);

end

calllib('dynamixel','dxl_set_txpacket_length',(2+1)*NUM_ACTUATOR+4);

calllib('dynamixel','dxl_txrx_packet');

The source above is to make the syncwriter packet.

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