Programming Servo Motions with DynamixelSDK on OpenCM 9.04 Microcontroller
This guide will show you how to set up the Arduino Integrated Development Environment (IDE), install the DynamixelSDK library for controlling servos, and set up a sketch to make a servo move.
To install the Arduino IDE
Go to https://www.arduino.cc/en/software and install the legacy version of the IDE, version 1.8.X.
Download the version of Arduino IDE that is appropriate for your operating system.
To install the OpenCM9.04 Board
Open the Arduino IDE.
Click File>Preferences.
Change the “additional boards manager url” to https://raw.githubusercontent.com/ROBOTIS-GIT/OpenCM9.04/master/arduino/opencm_release/package_opencm9.04_index.json.
Click OK.
Click Tools>Board>Board Manager.
Search for “opencm” and install the OpenCM9.04 board package. This may take 10 or 20 minutes, so please be patient. This will also install the DynamixelSDK library.
Now you should be able to use the DynamixelSDK packages and run example sketches. To enable these example sketches, Tools>Board>OpenCM>OpenCM9.04 Board. When selected, the bottom right corner of the IDE should now read “OpenCM9.04 Board, OpenCM Bootloader”.
With this board selected, you can now click File>Examples>OpenCM9.04>07_DynamixelSDK>protocol1.0 to view example sketches that show how to use the DynamixelSDK to control Dynamixels with Arduino.
See an example of how to use the DynamixelSDK
Let's open one of the example sketches and look at how it is written. This will help us write more sophisticated programs in the future.
Before we get started, let's turn on line numbers in the editor so we can refer to specific parts of the file. Click File>Preferences, then click the checkbox next to "Display line numbers".
Let's open an example sketch. Click File>Examples>OpenCM9.04>07DynamixelSDK>protocol1.0>read_write_ax. Because this example ships with the DynamixelSDK library, it is read-only. If you want to make edits, you can "save as" it somewhere else.
Note that there is a nice description on lines 1-10,
You would be wise to start all your programs with a similar description, to help other people (or yourself in the future) use it. You can see that you'll need an AX (or MX) series servo, an OpenCM9.04 board, and a 12V power supply to run this program.
The first line of code is line 12, which tells the compiler to include the DynamixelSDK library:
If you write your own libraries, you can include them just like this after saving them in Documents\Arduino\libraries. For more information on installing libraries, see: http://www.arduino.cc/en/Guide/Libraries.
Next, the sketch defines macros that contain the memory addresses in the Dynamixels (e.g. 24, 30) that correspond to various control and feedback variables:
These numerical values must be set to match your particular servo model. You can look them up in that servo's e-manual. For example, here is a link to the MX-28 e-manual.
Next, the sketch sets the control protocol used. There are protocols 1 and 2. I always use protocol 1, but you could read about protocol 2 and convince me that it is better.
This example sketch is for controlling just one servo. Thus, they save macros that store the ID of their single servo, its baudrate, and which serial port on the OpenCM9.04 Board it broadcasts commands over:
Note that they use the devicename "3", which is Serial3 on the OpenCM. However, if we look at the OpenCM9.04 Board e-manual, we see that we want to broadcast over Serial1, which includes our 3-pin TTL pins. Their comment also points this out.
On lines 28-34 they define some other macros that will make their code easier to read and write. This is a great programming tip. Always define numbers at the top of the program; never have "naked" number values throughout your program. When you need to change numbers later, it is easier to change one line at the top of your program rather than hunting for all the "3"s throughout the program. When you are reading the program later, you will be able to understand the meaning of the code because of descriptive macro and variable names, rather than trying to remember what "3" is the value of.
Line 38 defines the setup() method. Each Arduino sketch has a setup() method, which runs once, and a loop() method, which runs immediately after and repeats indefinitely.
Next, the program waits until a serial port is opened. This is accomplished by opening the serial monitor in the Arduino IDE after the program is downloaded to the board.
In the following lines, the program initializes instances of the PortHandler and PacketHandler structures. They are part of the dynamixel class. These lines of code can basically be copied into every Arduino sketch you write. Note that since these are initialized within the setup() method, they are limited in scope to setup(). There is another example sketch, read write ax in loop.ino, that shows how to initialize these structures with global scope.
To read and write data to the servos, we must open the portHandler and set the baudrate of the portHandler, which again are lines you can copy into any program:
One can see that the methods on lines 2 and 14 are type boolean, such that they return a value of false if something goes wrong and prevents them from executing. Checks like this are valuable for debugging as you write programs. If your robot malfunctions later, but you get the success messages from lines 4 and 16 in the serial monitor, then you can rule out basic communication errors.
On lines 88-101 we see the first attempt to send actual data to the servo:
There's a lot to unpack here. Look at line 2 above. You can see that the main method of interest is:
This will send a 1 byte command (that's the Tx; Tx = transmit) and listen for the reply (that's the Rx; Rx = receive). To send the 1 byte command, we must provide the portHandler, the ID of the servo where the message is going (DXL_ID), which memory address to write the byte to (ADDR AX TORQUE ENABLE), the value of the byte to write (TORQUE ENABLE), and a pointer to a variable where write1ByteTxRx can store an error message (&dxl error). See what I mean by not having "naked" numerical values? We can now read this message in human language and it makes sense: "Write one byte using the portHandler to servo DXL ID. Put the value into address ADDR AX TORQUE ENABLE. The value is TORQUE ENABLE."
There are other methods that we will find helpful, which all follow the same pattern:
These all work basically the same. The main difference is that the variable type of the transmitted data must match the method name: write1Byte sends uint8t (8 bit unsigned integer); write2Byte sends uint16t (16 bit unsigned integer); write4Byte sends uint32t (unsigned 32 bit integer). Each time you wish to write a new value to a servo's memory, you must use the write method that will send the number of bytes utilized by that memory address. For example, looking at the MX-28 e-manual again, we see that address 24 is 1 byte and signals whether Torque is Enabled, while address 30 is 2 bytes and signals the Commanded Position.
So, back to the actual program:
This code tells the servo to engage its motor (enable torque). It then checks to see if the command was sent correctly. If there was an error, it retrieves it and will print it. Then it checks to see if the servo itself encountered an error (e.g. it is overheated, it is overloaded). If so, it retrieves it and prints it. Else it prints that the connection to the servo was successful.
Next, the program moves the motor and reads the position as it moves. In older versions of the dynamixelSDK, the portHandler and packetHandler could not be initialized as global variables, meaning that one had to initialize them within the setup() method and then write a loop that wrote and read servo positions within setup(). The current version lets the programmer initialize the portHandler and packetHandler as global variables, and thus use the loop() method. Therefore, the following sections are out of date but still functional.
Here they begin an infinite loop (while 1 == 1). At the top of each loop, they print to the serial window, asking the user to input a letter. In line 6, they call Serial.available() repeatedly, which returns false unless the user has entered a character via the serial window. Once something is available in the Serial port, the program proceeds from line 6, reading one character from the Serial stream and saving it to the integer variable ch. (This is kind of wonky, but characters are ultimately just unsigned integers, so this is ok). If the character is 'q', then it breaks the loop. I don't know why they did it this way; in my opinion, this is sloppy. It would be better to initialize a boolean outside the while loop; condition the while loop while(!boolean); and set it to false when ch == 'q'. But whatever, it's a free example program.
On lines 117 - 126 of the original program, they send a position command, which looks essentially identical to the torque enable earlier, except that goal position is a 2 byte value:
On lines 128 - 147 of the original program, they read the position of the servo while the servo is moving toward the commanded value:
Here you can see the method:
Data is read from the servo by initializing a variable with the appropriate size (for ADDR AX PRESENT POSITION, this is the unsigned 16 bit integer dxl present position), sending a pointer to that variable to the servo when calling read2ByteTxRx, then reading the value of the variable after the message is sent.
This example program prints the servo's ID, goal position, and present position while the absolute value between the goal position and present position is greater than DXL MOVING STATUS THRESHOLD. Once this threshold is crossed, the program changes the value of index, so that the servo moves back to the starting point in the next loop:
This was a very close look at an example program with which you can learn to use the basic dynamixelSDK commands. Once you understand this, feel free to look through the other example programs or write your own.
Last updated