### DXL Monitor Example Setup Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/sample_code/c_dxl_monitor This C code sets up the DXL Monitor example, including necessary includes, defines, and platform-specific functions for character input. ```c #ifdef __linux__ #include #include #include #include #elif defined(_WIN32) || defined(_WIN64) #include #endif #include #include #include #include "dynamixel_sdk.h" // Uses DYNAMIXEL SDK library // Protocol version #define PROTOCOL_VERSION1 1.0 // See which protocol version is used in the Dynamixel #define PROTOCOL_VERSION2 2.0 // Default setting #define DEVICENAME "/dev/ttyUSB0" // Check which port is being used on your controller // ex) Windows: "COM1" Linux: "/dev/ttyUSB0" int getch() { #ifdef __linux__ struct termios oldt, newt; int ch; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); return ch; #elif defined(_WIN32) || defined(_WIN64) return _getch(); #endif } int kbhit(void) { #ifdef __linux__ struct termios oldt, newt; int ch; int oldf; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); oldf = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); if (ch != EOF) { ungetc(ch, stdin); return 1; } return 0; #elif defined(_WIN32) || defined(_WIN64) return _kbhit(); #endif } void usage(char *progname) { printf("-----------------------------------------------------------------------\\n"); printf("usage: %s\n" \ " [-h | --help]........: display this help\\n" \ " [-d | --device]......: port to open \\n" \ , progname); printf("-----------------------------------------------------------------------\\n"); } ``` -------------------------------- ### DXL Monitor Example Setup Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/sample_code/cpp_dxl_monitor Includes necessary headers for Linux/Windows and DYNAMIXEL SDK. Defines protocol versions and default device name. Provides platform-specific getch and kbhit functions for keyboard input. ```cpp #ifdef __linux__ #include #include #include #include #elif defined(_WIN32) || defined(_WIN64) #include #endif #include #include #include #include #include "dynamixel_sdk.h" // Uses DYNAMIXEL SDK library // Protocol version #define PROTOCOL_VERSION1 1.0 // See which protocol version is used in the Dynamixel #define PROTOCOL_VERSION2 2.0 // Default setting #define DEVICENAME "/dev/ttyUSB0" // Check which port is being used on your controller // ex) Windows: "COM1" Linux: "/dev/ttyUSB0" int getch() { #ifdef __linux__ struct termios oldt, newt; int ch; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); return ch; #elif defined(_WIN32) || defined(_WIN64) return _getch(); #endif } int kbhit(void) { #ifdef __linux__ struct termios oldt, newt; int ch; int oldf; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); oldf = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); if (ch != EOF) { ungetc(ch, stdin); return 1; } return 0; #elif defined(_WIN32) || defined(_WIN64) return _kbhit(); #endif } void Usage(char *progname) { printf("-----------------------------------------------------------------------\\n"); printf("Usage: %s\\n"); printf(" [-h | --help]........: display this help\\n"); printf("[-d | --device]......: port to open\\n", progname); printf("-----------------------------------------------------------------------\\n"); } ``` -------------------------------- ### Dynamixel SDK Initialization and Ping Example Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/sample_code/csharp_ping_protocol_2_0 Initializes the Dynamixel SDK, opens a port, sets the baud rate, pings a Dynamixel device to get its model number, and prints the results. Handles potential communication errors. ```csharp static void Main(string[] args) { // Initialize PortHandler Structs // Set the port path // Get methods and members of PortHandlerLinux or PortHandlerWindows int port_num = dynamixel.portHandler(DEVICENAME); // Initialize PacketHandler Structs dynamixel.packetHandler(); int dxl_comm_result = COMM_TX_FAIL; // Communication result byte dxl_error = 0; // Dynamixel error UInt16 dxl_model_number; // Dynamixel model number // Open port if (dynamixel.openPort(port_num)) { Console.WriteLine("Succeeded to open the port!"); } else { Console.WriteLine("Failed to open the port!"); Console.WriteLine("Press any key to terminate..."); Console.ReadKey(); return; } // Set port baudrate if (dynamixel.setBaudRate(port_num, BAUDRATE)) { Console.WriteLine("Succeeded to change the baudrate!"); } else { Console.WriteLine("Failed to change the baudrate!"); Console.WriteLine("Press any key to terminate..."); Console.ReadKey(); return; } // Try to ping the Dynamixel // Get Dynamixel model number dxl_model_number = dynamixel.pingGetModelNum(port_num, PROTOCOL_VERSION, DXL_ID); if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS) { dynamixel.printTxRxResult(PROTOCOL_VERSION, dxl_comm_result); } else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0) { dynamixel.printRxPacketError(PROTOCOL_VERSION, dxl_error); } Console.WriteLine("[ID: {0}] ping Succeeded. Dynamixel model number : {1}", DXL_ID, dxl_model_number); // Close port dynamixel.closePort(port_num); return; } ``` -------------------------------- ### Import Dynamixel SDK Functions and Setup Input Handling Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/sample_code/python_bulk_read_write_protocol_2_0 This code sets up the necessary environment for running Dynamixel SDK examples in Python. It includes path configuration and platform-specific input handling for controlling example execution. ```python import os, sys, ctypes if os.name == 'nt': import msvcrt def getch(): return msvcrt.getch().decode() else: import tty, termios fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) tty.setraw(sys.stdin.fileno()) def getch(): return sys.stdin.read(1) os.sys.path.append('../dynamixel_functions_py') # Path setting import dynamixel_functions as dynamixel # Uses DYNAMIXEL SDK library ``` -------------------------------- ### Bulk Read and Write Example Setup Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/sample_code/c_bulk_read_write_protocol_2_0 This C code sets up the necessary components for bulk read and write operations with Dynamixel servos using Protocol 2.0. It includes definitions for control table addresses, data lengths, protocol version, and default settings for Dynamixel IDs, baudrate, and device name. It also provides utility functions for keyboard input detection. ```c #ifdef __linux__ #include #include #include #elif defined(_WIN32) || defined(_WIN64) #include #endif #include #include #include "dynamixel_sdk.h" // Uses DYNAMIXEL SDK library // Control table address #define ADDR_PRO_TORQUE_ENABLE 562 // Control table address is different in Dynamixel model #define ADDR_PRO_LED_RED 563 #define ADDR_PRO_GOAL_POSITION 596 #define ADDR_PRO_PRESENT_POSITION 611 // Data Byte Length #define LEN_PRO_LED_RED 1 #define LEN_PRO_GOAL_POSITION 4 #define LEN_PRO_PRESENT_POSITION 4 // Protocol version #define PROTOCOL_VERSION 2.0 // See which protocol version is used in the Dynamixel // Default setting #define DXL1_ID 1 // Dynamixel#1 ID: 1 #define DXL2_ID 2 // Dynamixel#2 ID: 2 #define BAUDRATE 1000000 #define DEVICENAME "/dev/ttyUSB0" // Check which port is being used on your controller // ex) Windows: "COM1" Linux: "/dev/ttyUSB0" #define TORQUE_ENABLE 1 // Value for enabling the torque #define TORQUE_DISABLE 0 // Value for disabling the torque #define DXL_MINIMUM_POSITION_VALUE -150000 // Dynamixel will rotate between this value #define DXL_MAXIMUM_POSITION_VALUE 150000 // and this value (note that the Dynamixel would not move when the position value is out of movable range. Check e-manual about the range of the Dynamixel you use.) #define DXL_MOVING_STATUS_THRESHOLD 20 // Dynamixel moving status threshold #define ESC_ASCII_VALUE 0x1b int getch() { #ifdef __linux__ struct termios oldt, newt; int ch; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); return ch; #elif defined(_WIN32) || defined(_WIN64) return _getch(); #endif } int kbhit(void) { #ifdef __linux__ struct termios oldt, newt; int ch; int oldf; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); oldf = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); if (ch != EOF) { ungetc(ch, stdin); return 1; } return 0; #elif defined(_WIN32) || defined(_WIN64) return _kbhit(); #endif } int main() { // Initialize PortHandler Structs // Set the port path // Get methods and members of PortHandlerLinux or PortHandlerWindows int port_num = portHandler(DEVICENAME); // Initialize PacketHandler Structs packetHandler(); // Initialize groupBulkWrite Struct int groupwrite_num = groupBulkWrite(port_num, PROTOCOL_VERSION); //groupBulkWrite groupBulkWrite(portHandler, packetHandler); // Initialize Groupbulkread Struct int groupread_num = groupBulkRead(port_num, PROTOCOL_VERSION); int index = 0; int dxl_comm_result = COMM_TX_FAIL; // Communication result uint8_t dxl_addparam_result = False; // AddParam result uint8_t dxl_getdata_result = False; // GetParam result int dxl_goal_position[2] = { DXL_MINIMUM_POSITION_VALUE, DXL_MAXIMUM_POSITION_VALUE }; // Goal position uint8_t dxl_error = 0; // Dynamixel error uint8_t dxl_led_value[2] = { 0x00, 0xFF }; // Dynamixel LED value for write int32_t dxl1_present_position = 0; // Present position uint8_t dxl2_led_value_read; // Dynamixel LED value for read // Open port if (openPort(port_num)) { printf("Succeeded to open the port!\n"); } else { printf("Failed to open the port!\n"); printf("Press any key to terminate...\n"); getch(); return 0; } ``` -------------------------------- ### Dynamixel Ping Example (Protocol 1.0) Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/sample_code/cpp_ping_protocol_1_0 This C++ example demonstrates how to initialize the Dynamixel SDK, open a serial port, set the baud rate, ping a Dynamixel motor to get its model number, and handle communication results. It's designed for Protocol 1.0. ```c++ /* * ping.cpp * * Created on: 2016. 2. 21. * Author: leon */ // // ********* ping Example ********* // // // Available Dynamixel model on this example : All models using Protocol 1.0 // This example is designed for using a Dynamixel MX-28, and an USB2DYNAMIXEL. // To use another Dynamixel model, such as X series, see their details in E-Manual(support.robotis.com) and edit below "#define"d variables yourself. // Be sure that Dynamixel MX properties are already set as %% ID : 1 / Baudnum : 1 (Baudrate : 1000000 [1M]) // #ifdef __linux__ #include #include #include #elif defined(_WIN32) || defined(_WIN64) #include #endif #include #include "dynamixel_sdk.h" // Uses DYNAMIXEL SDK library // Protocol version #define PROTOCOL_VERSION 1.0 // See which protocol version is used in the Dynamixel // Default setting #define DXL_ID 1 // Dynamixel ID: 1 #define BAUDRATE 1000000 #define DEVICENAME "/dev/ttyUSB0" // Check which port is being used on your controller // ex) Windows: "COM1" Linux: "/dev/ttyUSB0" int getch() { #ifdef __linux__ struct termios oldt, newt; int ch; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); return ch; #elif defined(_WIN32) || defined(_WIN64) return _getch(); #endif } int kbhit(void) { #ifdef __linux__ struct termios oldt, newt; int ch; int oldf; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); oldf = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); if (ch != EOF) { ungetc(ch, stdin); return 1; } return 0; #elif defined(_WIN32) || defined(_WIN64) return _kbhit(); #endif } int main() { // Initialize PortHandler instance // Set the port path // Get methods and members of PortHandlerLinux or PortHandlerWindows dynamixel::PortHandler *portHandler = dynamixel::PortHandler::getPortHandler(DEVICENAME); // Initialize PacketHandler instance // Set the protocol version // Get methods and members of Protocol1PacketHandler or Protocol2PacketHandler dynamixel::PacketHandler *packetHandler = dynamixel::PacketHandler::getPacketHandler(PROTOCOL_VERSION); int dxl_comm_result = COMM_TX_FAIL; // Communication result uint8_t dxl_error = 0; // Dynamixel error uint16_t dxl_model_number; // Dynamixel model number // Open port if (portHandler->openPort()) { printf("Succeeded to open the port!\n"); } else { printf("Failed to open the port!\n"); printf("Press any key to terminate...\n"); getch(); return 0; } // Set port baudrate if (portHandler->setBaudRate(BAUDRATE)) { printf("Succeeded to change the baudrate!\n"); } else { printf("Failed to change the baudrate!\n"); printf("Press any key to terminate...\n"); getch(); return 0; } // Try to ping the Dynamixel // Get Dynamixel model number dxl_comm_result = packetHandler->ping(portHandler, DXL_ID, &dxl_model_number, &dxl_error); if (dxl_comm_result != COMM_SUCCESS) { packetHandler->printTxRxResult(dxl_comm_result); } else if (dxl_error != 0) { packetHandler->printRxPacketError(dxl_error); } printf("[ID:%03d] ping Succeeded. Dynamixel model number : %d\n", DXL_ID, dxl_model_number); // Close port portHandler->closePort(); return 0; } ``` -------------------------------- ### Run Protocol 1.0 Example Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/library_setup/c Execute the compiled read_write example program for Protocol 1.0. ```bash $ ./read_write ``` -------------------------------- ### C Protocol Combined Example Setup Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/sample_code/c_protocol_combined This snippet includes necessary headers, defines control table addresses for MX and PRO models, sets protocol versions, and defines default parameters like ID, baudrate, and device name. It also includes platform-specific functions for character input. ```c #ifdef __linux__ #include #include #include #elif defined(_WIN32) || defined(_WIN64) #include #endif #include #include #include "dynamixel_sdk.h" // Uses DYNAMIXEL SDK library // Control table address for Dynamixel MX #define ADDR_MX_TORQUE_ENABLE 24 // Control table address is different in Dynamixel model #define ADDR_MX_GOAL_POSITION 30 #define ADDR_MX_PRESENT_POSITION 36 // Control table address for Dynamixel PRO #define ADDR_PRO_TORQUE_ENABLE 562 #define ADDR_PRO_GOAL_POSITION 596 #define ADDR_PRO_PRESENT_POSITION 611 // Protocol version #define PROTOCOL_VERSION1 1.0 // See which protocol version is used in the Dynamixel #define PROTOCOL_VERSION2 2.0 // Default setting #define DXL1_ID 1 // Dynamixel#1 ID: 1 #define DXL2_ID 2 // Dynamixel#2 ID: 2 #define BAUDRATE 1000000 #define DEVICENAME "/dev/ttyUSB0" // Check which port is being used on your controller // ex) Windows: "COM1" Linux: "/dev/ttyUSB0" #define TORQUE_ENABLE 1 // Value for enabling the torque #define TORQUE_DISABLE 0 // Value for disabling the torque #define DXL1_MINIMUM_POSITION_VALUE 100 // Dynamixel will rotate between this value #define DXL1_MAXIMUM_POSITION_VALUE 4000 // and this value (note that the Dynamixel would not move when the position value is out of movable range. Check e-manual about the range of the Dynamixel you use.) #define DXL2_MINIMUM_POSITION_VALUE -150000 #define DXL2_MAXIMUM_POSITION_VALUE 150000 #define DXL1_MOVING_STATUS_THRESHOLD 10 // Dynamixel MX moving status threshold #define DXL2_MOVING_STATUS_THRESHOLD 20 // Dynamixel PRO moving status threshold #define ESC_ASCII_VALUE 0x1b int getch() { #ifdef __linux__ struct termios oldt, newt; int ch; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); return ch; #elif defined(_WIN32) || defined(_WIN64) return _getch(); #endif } int kbhit(void) { #ifdef __linux__ struct termios oldt, newt; int ch; int oldf; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); oldf = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); if (ch != EOF) { ungetc(ch, stdin); return 1; } return 0; #elif defined(_WIN32) || defined(_WIN64) return _kbhit(); #endif } int main() { // Initialize PortHandler Structs // Set the port path // Get methods and members of PortHandlerLinux or PortHandlerWindows int port_num = portHandler(DEVICENAME); // Initialize PacketHandler Structs packetHandler(); int index = 0; int dxl_comm_result = COMM_TX_FAIL; // Communication result int dxl1_goal_position[2] = {DXL1_MINIMUM_POSITION_VALUE, DXL1_MAXIMUM_POSITION_VALUE}; // Goal position of Dynamixel MX int dxl2_goal_position[2] = {DXL2_MINIMUM_POSITION_VALUE, DXL2_MAXIMUM_POSITION_VALUE}; // Goal position of Dynamixel PRO ``` -------------------------------- ### Build and Install DYNAMIXEL SDK Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/library_setup/c Navigate to the appropriate build directory based on your system architecture and use 'make install' to compile and install the DYNAMIXEL SDK library. ```bash $ cd DynamixelSDK/c/build/linux64 # for 64-bit systems $ sudo make install ``` -------------------------------- ### Dynamixel Ping Example (Protocol 1.0) Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/sample_code/cpp_ping_protocol_1_0 Demonstrates how to initialize the PortHandler and PacketHandler for Protocol 1.0, open a serial port, set the baudrate, and ping a Dynamixel to get its model number. This is a fundamental step for device communication. ```c++ int main() { // Initialize PortHandler instance // Set the port path // Get methods and members of PortHandlerLinux or PortHandlerWindows dynamixel::PortHandler *portHandler = dynamixel::PortHandler::getPortHandler(DEVICENAME); // Initialize PacketHandler instance // Set the protocol version // Get methods and members of Protocol1PacketHandler or Protocol2PacketHandler dynamixel::PacketHandler *packetHandler = dynamixel::PacketHandler::getPacketHandler(PROTOCOL_VERSION); int dxl_comm_result = COMM_TX_FAIL; // Communication result uint8_t dxl_error = 0; // Dynamixel error uint16_t dxl_model_number; // Dynamixel model number // Open port if (portHandler->openPort()) { printf("Succeeded to open the port!\n"); } else { printf("Failed to open the port!\n"); printf("Press any key to terminate...\n"); getch(); return 0; } // Set port baudrate if (portHandler->setBaudRate(BAUDRATE)) { printf("Succeeded to change the baudrate!\n"); } else { printf("Failed to change the baudrate!\n"); printf("Press any key to terminate...\n"); getch(); return 0; } // Try to ping the Dynamixel // Get Dynamixel model number dxl_comm_result = packetHandler->ping(portHandler, DXL_ID, &dxl_model_number, &dxl_error); if (dxl_comm_result != COMM_SUCCESS) { packetHandler->printTxRxResult(dxl_comm_result); } else if (dxl_error != 0) { packetHandler->printRxPacketError(dxl_error); } printf("[ID:%03d] ping Succeeded. Dynamixel model number : %d\n", DXL_ID, dxl_model_number); // Close port portHandler->closePort(); return 0; } ``` -------------------------------- ### Dynamixel Factory Reset and Baud Rate Change Example (C) Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/sample_code/c_reset_protocol_2_0 This comprehensive example demonstrates the full process of resetting a Dynamixel motor to factory defaults and then changing its baud rate. It covers port initialization, baud rate setting, factory reset execution, reading and writing the baud rate register, and port closure. Ensure the DEVICENAME, BAUDRATE, DXL_ID, OPERATION_MODE, PROTOCOL_VERSION, FACTORYRST_DEFAULTBAUDRATE, and NEW_BAUDNUM are correctly defined for your setup. ```c int main() { // Initialize PortHandler Structs // Set the port path // Get methods and members of PortHandlerLinux or PortHandlerWindows int port_num = portHandler(DEVICENAME); // Initialize PacketHandler Structs packetHandler(); int dxl_comm_result = COMM_TX_FAIL; // Communication result uint8_t dxl_error = 0; // Dynamixel error uint8_t dxl_baudnum_read; // Read baudnum // Open port if (openPort(port_num)) { printf("Succeeded to open the port!\n"); } else { printf("Failed to open the port!\n"); printf("Press any key to terminate...\n"); getch(); return 0; } // Set port baudrate if (setBaudRate(port_num, BAUDRATE)) { printf("Succeeded to change the baudrate!\n"); } else { printf("Failed to change the baudrate!\n"); printf("Press any key to terminate...\n"); getch(); return 0; } // Read present baudrate of the controller printf("Now the controller baudrate is : %d\n", getBaudRate(port_num)); // Try factoryreset printf("[ID:%03d] Try factoryreset : ", DXL_ID); factoryReset(port_num, PROTOCOL_VERSION, DXL_ID, OPERATION_MODE); if ((dxl_comm_result = getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS) { printf("Aborted\n"); printTxRxResult(PROTOCOL_VERSION, dxl_comm_result); return 0; } else if ((dxl_error = getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0) { printRxPacketError(PROTOCOL_VERSION, dxl_error); } // Wait for reset printf("Wait for reset...\n"); msecSleep(2000); printf("[ID:%03d] factoryReset Success!\n", DXL_ID); // Set controller baudrate to Dynamixel default baudrate if (setBaudRate(port_num, FACTORYRST_DEFAULTBAUDRATE)) { printf("Succeed to change the controller baudrate to : %d\n", FACTORYRST_DEFAULTBAUDRATE); } else { printf("Failed to change the controller baudrate\n"); printf("Press any key to terminate...\n"); getch(); return 0; } // Read Dynamixel baudnum dxl_baudnum_read = read1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_PRO_BAUDRATE); if ((dxl_comm_result = getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS) { printTxRxResult(PROTOCOL_VERSION, dxl_comm_result); } else if ((dxl_error = getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0) { printRxPacketError(PROTOCOL_VERSION, dxl_error); } else { printf("[ID:%03d] DXL baudnum is now : %d\n", DXL_ID, dxl_baudnum_read); } // Write new baudnum write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_PRO_BAUDRATE, NEW_BAUDNUM); if ((dxl_comm_result = getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS) { printTxRxResult(PROTOCOL_VERSION, dxl_comm_result); } else if ((dxl_error = getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0) { printRxPacketError(PROTOCOL_VERSION, dxl_error); } else { printf("[ID:%03d] Set Dynamixel baudnum to : %d\n", DXL_ID, NEW_BAUDNUM); } // Set port baudrate to BAUDRATE if (setBaudRate(port_num, BAUDRATE)) { printf("Succeed to change the controller baudrate to : %d\n", BAUDRATE); } else { printf("Failed to change the controller baudrate\n"); printf("Press any key to terminate...\n"); getch(); return 0; } msecSleep(200); // Read Dynamixel baudnum dxl_baudnum_read = read1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_PRO_BAUDRATE); if ((dxl_comm_result = getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS) { printTxRxResult(PROTOCOL_VERSION, dxl_comm_result); } else if ((dxl_error = getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0) { printRxPacketError(PROTOCOL_VERSION, dxl_error); } else { printf("[ID:%03d] Dynamixel Baudnum is now : %d\n", DXL_ID, dxl_baudnum_read); } // Close port closePort(port_num); return 0; } ``` -------------------------------- ### Java Broadcast Ping Example Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/sample_code/java_broadcast_ping_protocol_2_0 This example demonstrates how to initialize the Dynamixel SDK, open a serial port, set the baud rate, and perform a broadcast ping to discover connected Dynamixel devices. It then iterates through possible IDs to report detected devices. Ensure the correct device name and baud rate are configured for your setup. ```java /* * BroadcastPing.java * * Created on: 2016. 6. 23. * Author: Ryu Woon Jung (Leon) */ // // ********* BroadcastPing Example ********* // // // Available Dynamixel model on this example : All models using Protocol 2.0 // This example is designed for using a Dynamixel PRO 54-200, and an USB2DYNAMIXEL. // To use another Dynamixel model, such as X series, see their details in E-Manual(support.robotis.com) and edit below variables yourself. // Be sure that Dynamixel PRO properties are already set as %% ID : 1 / Baudnum : 3 (Baudrate : 1000000 [1M]) // import java.util.Scanner; public class BroadcastPing { public static void main(String[] args) { // Protocol version int PROTOCOL_VERSION = 2; // See which protocol version is used in the Dynamixel // Default setting int BAUDRATE = 1000000; String DEVICENAME = "/dev/ttyUSB0"; // Check which port is being used on your controller // ex) "COM1" Linux: "/dev/ttyUSB0" int MAX_ID = 252; // Maximum ID value int COMM_SUCCESS = 0; // Communication Success result value int COMM_TX_FAIL = -1001; // Communication Tx Failed // Instead of getch Scanner scanner = new Scanner(System.in); // Initialize Dynamixel class for java Dynamixel dynamixel = new Dynamixel(); // Initialize PortHandler Structs // Set the port path // Get methods and members of PortHandlerLinux or PortHandlerWindows int port_num = dynamixel.portHandler(DEVICENAME); // Initialize PacketHandler Structs dynamixel.packetHandler(); int id; int dxl_comm_result = COMM_TX_FAIL; // Communication result // Open port if (dynamixel.openPort(port_num)) { System.out.println("Succeeded to open the port!"); } else { System.out.println("Failed to open the port!"); System.out.println("Press any key to terminate..."); scanner.nextLine(); return; } // Set port baudrate if (dynamixel.setBaudRate(port_num, BAUDRATE)) { System.out.println("Succeeded to change the baudrate!"); } else { System.out.println("Failed to change the baudrate!"); System.out.println("Press any key to terminate..."); scanner.nextLine(); return; } // Try to broadcast ping the Dynamixel dynamixel.broadcastPing(port_num, PROTOCOL_VERSION); if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS) dynamixel.printTxRxResult(PROTOCOL_VERSION, dxl_comm_result); System.out.println("Detected Dynamixel הראש:"); for (id = 0; id < MAX_ID; id++) { if (dynamixel.getBroadcastPingResult(port_num, PROTOCOL_VERSION, id)) System.out.printf("[ID: %d]\n", id); } // Close port dynamixel.closePort(port_num); return; } } ``` -------------------------------- ### Build Example Code with CMake (Linux) Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/library_setup/cpp Builds the example code for the Dynamixel SDK using CMake. The executable files will be generated in the 'build' folder within the example directory. ```bash $ cd DynamixelSDK/c++/example/protocol2.0 $ sudo cmake -B build $ sudo cmake --build build $ cd build $ ./broadcast_ping ``` -------------------------------- ### MATLAB Ping Protocol 1.0 Example Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/sample_code/matlab_ping_protocol_1_0 This script pings a Dynamixel to get its model number using Protocol 1.0. It loads the necessary libraries, configures the serial port, and checks for communication success. Ensure the Dynamixel SDK is installed and the correct port name is used. ```matlab % % ping.m % % Created on: 2016. 6. 7. % Author: Ryu Woon Jung (Leon) % % % ********* Ping Example ********* % % % Available Dynamixel model on this example : All models using Protocol 1.0 % This example is designed for using a Dynamixel MX-28, and an USB2DYNAMIXEL. % To use another Dynamixel model, such as X series, see their details in E-Manual(support.robotis.com) and edit below variables yourself. % Be sure that Dynamixel MX properties are already set as %% ID : 1 / Baudnum : 1 (Baudrate : 1000000 [1M]) % clc; clear all; % Load Libraries if ~libisloaded('dxl_x86_c'); [notfound, warnings] = loadlibrary('dxl_x86_c', 'dynamixel_sdk.h', 'addheader', 'port_handler.h', 'addheader', 'packet_handler.h'); end % Protocol version PROTOCOL_VERSION = 1.0; % See which protocol version is used in the Dynamixel % Default setting DXL_ID = 1; % Dynamixel ID: 1 BAUDRATE = 1000000; DEVICENAME = 'COM1'; % Check which port is being used on your controller % ex) Windows: "COM1" Linux: "/dev/ttyUSB0" COMM_SUCCESS = 0; % Communication Success result value COMM_TX_FAIL = -1001; % Communication Tx Failed % Initialize PortHandler Structs % Set the port path % Get methods and members of PortHandlerLinux or PortHandlerWindows port_num = portHandler(DEVICENAME); % Initialize PacketHandler Structs packetHandler(); dxl_comm_result = COMM_TX_FAIL; % Communication result % Open port if (openPort(port_num)) fprintf('Succeeded to open the port!\n'); else unloadlibrary('dxl_x86_c'); fprintf('Failed to open the port!\n'); input('Press any key to terminate...\n'); return; end % Set port baudrate if (setBaudRate(port_num, BAUDRATE)) fprintf('Succeeded to change the baudrate!\n'); else unloadlibrary('dxl_x86_c'); fprintf('Failed to change the baudrate!\n'); input('Press any key to terminate...\n'); return; end % Try to ping the Dynamixel % Get Dynamixel model number dxl_model_number = pingGetModelNum(port_num, PROTOCOL_VERSION, DXL_ID); if getLastTxRxResult(port_num, PROTOCOL_VERSION) ~= COMM_SUCCESS printTxRxResult(PROTOCOL_VERSION, getLastTxRxResult(port_num, PROTOCOL_VERSION)); elseif getLastRxPacketError(port_num, PROTOCOL_VERSION) ~= 0 printRxPacketError(PROTOCOL_VERSION, getLastRxPacketError(port_num, PROTOCOL_VERSION)); end fprintf('[ID:%03d] ping Succeeded. Dynamixel model number : %d\n', DXL_ID, dxl_model_number); % Close port closePort(port_num); % Unload Library unloadlibrary('dxl_x86_c'); close all; clear all; ``` -------------------------------- ### Get Time Since Start (Mac) Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/api_reference/c/c_porthandler Retrieves the elapsed time since the start of an operation for a given port. ```c double getTimeSinceStartMac(int port_num) ``` -------------------------------- ### Navigate to Protocol 1.0 Example Directory (Linux) Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/library_setup/c Change directory to the location of the Protocol 1.0 read_write example for Linux. ```bash $ cd [DynamixelSDK folder]/c/example/protocol1.0/read_write/linux32 ``` ```bash $ cd [DynamixelSDK folder]/c/example/protocol1.0/read_write/linux64 ``` -------------------------------- ### Get Time Since Start (Linux) Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/api_reference/c/c_porthandler Retrieves the time elapsed since the program started on a Linux system. Requires the port number as input. ```c double getTimeSinceStartLinux(int port_num) ``` -------------------------------- ### Get Time Since Start on Windows Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/api_reference/c/c_porthandler Retrieves the time elapsed since the start for a given port on Windows. Requires the port number as input. ```c double getTimeSinceStartWindows(int port_num) ``` -------------------------------- ### getTimeSinceStartMac Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/api_reference/c/c_porthandler Gets the elapsed time since the start for a given port. ```APIDOC ## getTimeSinceStartMac ### Description Gets the elapsed time since the start for a given port. ### Syntax ```c double getTimeSinceStartMac(int port_num) ``` ### Parameters #### Path Parameters - **port_num** (int) - Required - Port number ### Returns - double - The time elapsed since the start. ``` -------------------------------- ### getTimeSinceStartWindows Source: https://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/api_reference/c/c_porthandler Gets the elapsed time since the system started on Windows. ```APIDOC ## getTimeSinceStartWindows ### Description Gets the time elapsed since the system started. ### Method (Not specified, likely a function call) ### Endpoint (Not applicable for C API) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters None ### Request Example ```c // Usage might involve passing a time structure to be filled // Example is illustrative as exact usage is not detailed. // getTimeSinceStartWindows(&time_struct); ``` ### Response #### Success Response (void) This function does not return a value directly but likely fills a provided time structure. #### Response Example (No direct return value, depends on implementation details not provided) ```