ROBOTIS e-Manual v1.13.01
This is an example for Visual C#.
This example has been tested in Visual Studio 2005.
The environment must be set in C# to execute the examples.
1. In the Solution Browser, select Add -> Existing Category by clicking the right button of the mouse on the Project File name.

2. Add dynamixel.cs file. dynamixel.cs is in the import folder where DynamixelSDK is saved.
(For example, C:\DynamixelSDK\import\dynamixel.cs)

3. Check whether the dynamixel.cs is added or not.

4. Add using ROBOTIS, and then use Dynamixel API functions.
Please refer to API Reference on the usage of each function.

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

|
// Open device if( dynamixel.dxl_initialize(DEFAULT_PORTNUM, DEFAULT_BAUDNUM) == 0 ) { // Failed to open USB2Dynamixel } else { // Succeed to open USB2Dynamixel } |
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 the succeeded source goes to "Else" sentence.
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, and DEFAULT_BAUDNUM is set to 1Mbps.
|
// Close device dynamixel.dxl_terminate(); |
The source above is for termination.
The connection is terminated by calling dxl_teminate function from dynamixel API.
|
// Write goal position dynamixel.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 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 = dynamixel.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 = dynamixel.dxl_get_result(); if (CommStatus == dynamixel.COMM_RXSUCCESS) { PrintErrorCode(); } else { PrintCommStatus(CommStatus); } |
The source above saves the packet communication result in CommStatus variables.
dxl_get_result function is used to check whether the result is correct ro not.
If the result is successful, it checks the error status during the operation of Dynamixel by calling PrintErrorCode function.
If the result is failed, the error information returned from dxl_get_result function is transmitted to PrintCommStatus function and printed on the screen.
○ PrintErrorCode Function
|
static void PrintErrorCode() { if (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_VOLTAGE) == 1) Console.WriteLine("Input voltage error!"); if (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_ANGLE) == 1) Console.WriteLine("Angle limit error!"); if (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_OVERHEAT) == 1) Console.WriteLine("Overheat error!"); if (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_RANGE) == 1) Console.WriteLine("Out of range error!"); if (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_CHECKSUM) == 1) Console.WriteLine("Checksum error!"); if (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_OVERLOAD) == 1) Console.WriteLine("Overload error!"); if (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_INSTRUCTION) == 1) Console.WriteLine("Instruction code error!"); } |
PrintErrorCode function checks the status packet error by calling dxl_get_rxpacket_error function.
○ PrintCommStatus Function
|
static void PrintCommStatus(int CommStatus) { switch (CommStatus) { case dynamixel.COMM_TXFAIL: Console.WriteLine("COMM_TXFAIL: Failed transmit instruction packet!"); break; case dynamixel.COMM_TXERROR: Console.WriteLine("COMM_TXERROR: Incorrect instruction packet!"); break; case dynamixel.COMM_RXFAIL: Console.WriteLine("COMM_RXFAIL: Failed get status packet from device!"); break; case dynamixel.COMM_RXWAITING: Console.WriteLine("COMM_RXWAITING: Now recieving status packet!"); break; case dynamixel.COMM_RXTIMEOUT: Console.WriteLine("COMM_RXTIMEOUT: There is no status packet!"); break; case dynamixel.COMM_RXCORRUPT: Console.WriteLine("COMM_RXCORRUPT: Incorrect status packet!"); break; default: Console.WriteLine("This is unknown error code!"); break; } } |
PrintCommStatus prints the error information returned from dxl_get_result function on the screen.
|
// Check moving done Moving = dynamixel.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.
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( int i=0; i<NUM_ACTUATOR; i++ ) { id[i] = i+1; } |
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 Dynamixel ID value.
|
// Make syncwrite packet dynamixel.dxl_set_txpacket_id(dynamixel.BROADCAST_ID); dynamixel.dxl_set_txpacket_instruction(dynamixel.INST_SYNC_WRITE); dynamixel.dxl_set_txpacket_parameter(0, P_GOAL_POSITION_L); dynamixel.dxl_set_txpacket_parameter(1, 2);
for( int i=0; i<NUM_ACTUATOR; i++ ) { dynamixel.dxl_set_txpacket_parameter(2+3*i, id[i]); GoalPos = (int)((Math.Sin(theta+phase[i]) + 1.0) * (double)AmpPos); dynamixel.dxl_set_txpacket_parameter(2 + 3 * i + 1, dynamixel.dxl_get_lowbyte(GoalPos)); dynamixel.dxl_set_txpacket_parameter(2 + 3 * i + 2, dynamixel.dxl_get_highbyte(GoalPos)); }
dynamixel.dxl_set_txpacket_length((2 + 1) * NUM_ACTUATOR + 4); dynamixel.dxl_txrx_packet(); |
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.