Edit on GitHub
Step 2: Read Data from DYNAMIXEL
- This tutorial shows you how to read data from a Dynamixel using the Dynamixel Easy SDK.
NOTE: It assumes that you have already installed and built the SDK.
Make cpp file
- Create a new C++ source file Open it with your editor.
$ touch tutorial_step2.cpp
Check the Port Names
- Before running the code, check the port name of the connected Dynamixel.
- For General
- OpenRB-150:
ttyACM0: USB ACM device - U2D2:
FTDI USB Serial Device converter now attached to ttyUSB0$ sudo dmesg | grep tty
- OpenRB-150:
Source Code Description
#include "dynamixel_easy_sdk/dynamixel_easy_sdk.hpp"
int main(){
dynamixel::Connector connector("/dev/ttyACM0", 57600);
std::unique_ptr<dynamixel::Motor> motor1 = connector.createMotor(1);
auto result_int32_t = motor1->getPresentPosition();
int present_position = result_int32_t.value();
std::cout << "Present Position: " << present_position << std::endl;
}
Add Header Files
- Add
dynamixel_easy_sdk/dynamixel_easy_sdk.hppto the top of your CPP file. This class is included in the Dynamixel SDK package.#include "dynamixel_easy_sdk/dynamixel_easy_sdk.hpp"
Create Connector and Motor Object
- Create a
Connectorobject with port name, baud rate, and protocol version to manage the communication.(Only protocol 2.0 is supported)int main(){ dynamixel::Connector connector("/dev/ttyACM0", 57600); - Create a
Motorobject for each Dynamixel servo you want to control, using thecreateMotormethod of theConnectorclass. - This method takes the motor ID as an argument and returns a unique pointer to a
Motorinstance. (shared_ptr is also available)std::unique_ptr<dynamixel::Motor> motor1 = connector.createMotor(1); - This process throws a
DxlRuntimeErrorif the object creation fails.
Get Present Position
- You can read the present position of the motor using the
getPresentPositionmethod. - Return type of this method is
Result<int32_t, Error>. You can get the actual position value using thevalue()method of theResultobject.auto result_int32_t = motor1->getPresentPosition(); int present_position = result_int32_t.value(); std::cout << "Present Position: " << present_position << std::endl;
Error Handling
- To ensure your code is robust, every method that sends a command to the motor returns a Result object that encapsulates values and errors.
- This object lets you safely check for any communication or device errors before proceeding.
- You can check for communication errors and device(dynamixel) errors using the Result object.
-
If you use
value()when error occurred without checking for errors, it may throw an exception.Example
auto result_int32_t = motor1->getPresentPosition(); if (!result_int32_t.isSuccess()) { std::cerr << dynamixel::getErrorMessage(result_int32_t.error()) << std::endl; return 1; } int present_position = result_int32_t.value();
Compile and Run
- You can compile and run the code using the following commands
$ g++ tutorial_step2.cpp -o tutorial_step2 -l dxl_x64_cpp $ ./tutorial_step2
Full Source Code With Error Handling
#include "dynamixel_easy_sdk/dynamixel_easy_sdk.hpp"
int main(){
dynamixel::Connector connector("/dev/ttyUSB0", 2.0, 57600);
std::unique_ptr<dynamixel::Motor> motor1 = connector.createMotor(1);
auto result_int32_t = motor1->getPresentPosition();
if (!result_int32_t.isSuccess()) {
std::cerr << dynamixel::getErrorMessage(result_int32_t.error()) << std::endl;
return 1;
}
int present_position = result_int32_t.value();
std::cout << "Present Position: " << present_position << std::endl;
}
}

