Visual Basic

This is an example for Visual Basic.

This example has been tested in Visual Studio 2005.

Setting Environment

The environment must be set in Visual Basic to execute the examples.

◆ Using Dynamixel API

1. In the Solution Browser, select Add -> Existing Category by clicking the right botton of the mouse on the Project File name.

 

vb_config.png

 

2. Add dynamixel.vb file.  dynamixel.vb is in the import folder where DynamixelSDK is saved.

(For example, C:\DynamixelSDK\import\dynamixel.vb)

vb_config1.png

 

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

vb_config2.png

 

4. Use Dynamixel API functions.

Please refer to API Reference on the usage of each function.

 

vb_config3.png

 

Read/Write Example

This is a Read / Write example of Dynamixel.  

[Preparation]

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

u2d_select.png

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.

vb_rw.png

 

[Source]

 'Open device

        If (dxl_initialize(DEFAULT_PORTNUM, DEFAULT_BAUDNUM) = 0) Then

            Console.WriteLine("Failed to open USB2Dynamixel!")

             Exit Sub

        Else

            Console.WriteLine("Succeed to open USB2Dynamixel!")

        End If

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

dxl_terminate()

The source above is for termination.

The connection is terminated 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) Then

                PrintErrorCode()

   Else

   PrintCommStatus(CommStatus)

   End If

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

dxl_get_result function 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

 'Print error bit of status packet

 Sub PrintErrorCode()

        If (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_VOLTAGE) = 1) Then

            Console.WriteLine("Input voltage error!")

        End If

        If (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_ANGLE) = 1) Then

            Console.WriteLine("Angle limit error!")

        End If

        If (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_OVERHEAT) = 1) Then

            Console.WriteLine("Overheat error!")

        End If

        If (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_RANGE) = 1) Then

            Console.WriteLine("Out of range error!")

        End If

        If (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_CHECKSUM) = 1) Then

            Console.WriteLine("Checksum error!")

        End If

        If (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_OVERLOAD) = 1) Then

            Console.WriteLine("Overload error!")

        End If

        If (dynamixel.dxl_get_rxpacket_error(dynamixel.ERRBIT_INSTRUCTION) = 1) Then

            Console.WriteLine("Instruction code error!")

        End If

End Sub

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

 

○  PrintCommStatus Function

Sub PrintCommStatus(ByVal CommStatus As Integer)

        Select Case (CommStatus)

            Case COMM_TXFAIL

                Console.WriteLine("COMM_TXFAIL: Failed transmit instruction packet!")

            Case COMM_TXERROR

                Console.WriteLine("COMM_TXERROR: Incorrect instruction packet!")

            Case COMM_RXFAIL

                Console.WriteLine("COMM_RXFAIL: Failed get status packet from device!")

            Case COMM_RXWAITING

                Console.WriteLine("COMM_RXWAITING: Now recieving status packet!")

            Case COMM_RXTIMEOUT

                Console.WriteLine("COMM_RXTIMEOUT: There is no status packet!")

            Case COMM_RXCORRUPT

                Console.WriteLine("COMM_RXCORRUPT: Incorrect status packet!")

            Case Else

                Console.WriteLine("This is unknown error code!")

        End Select

 End Sub

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

 

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

DEFAULT_ID means the ID value of relevant Dynamixel.

P_MOVING means Moving parts' 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 are1, 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 As Integer = 0 To (NUM_ACTUATOR - 1)

            id(i) = i + 1       

Next i

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

dxl_set_txpacket_id(dynamixel.BROADCAST_ID)

dxl_set_txpacket_instruction(dynamixel.INST_SYNC_WRITE)

dxl_set_txpacket_parameter(0, P_GOAL_POSITION_L)

dxl_set_txpacket_parameter(1, 2)

 

For i As Integer = 0 To (NUM_ACTUATOR - 1)

dxl_set_txpacket_parameter(2 + 3 * i, id(i))

GoalPos = (Math.Sin(theta + phase(i)) + 1) * AmpPos

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

dxl_set_txpacket_parameter(2 + 3 * i + 2, dynamixel.dxl_get_highbyte(GoalPos))

Next i

 

dxl_set_txpacket_length((2 + 1) * NUM_ACTUATOR + 4)

Console.WriteLine("")

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.