Programming CM-510 with C: reading values from terminal and moving a Dynamixel AX-12

This entry is part 4 of 4 in the series Programming CM-5/CM-510

Programming CM-510 with C: reading values from terminal and moving a Dynamixel AX-12

In this post we are going to ask the ID AX-12+ that we want to move and the goal position.

The explanations are in the code as comments, I hope that there are enough comments to understand it, let me know if you can’t understand it.

The main loop is that easy:

int main(void)

while(true) // we'll repeat this looop forever
int id=getId(); // get the ID of the AX-12 that we want to move
int position=getPosition(); // get the goal position
dxl_write_word( id, P_GOAL_POSITION_L, position); // sent the command to the Dynamixel

return 0;

A brief explanation of printf: printf function is much more powerful than it seems at a first sight, it admits many parameters that allow us to display a large amount of data types and formats. In In the following example %i means that in that position the message will include an integer that will be passed as a parameter after the string “AX12 ID:”. The control character “n” means a line break.

Each character in the string is stored in a memory location [A][X][1][2][ ][I][D] strings are a special case of array.

getID and getPosition are also very easy, isn’t?

The next functions asks for the ID of the AX-12 to move, checking that the ID is a value between 1 and 18

int getId()
We define an array enough large, 256 bytes (characters). Probably it's enough with 4 bytes, but if we type more than the defined size we will get an error*/
char buffer[256];

And we define another integer variable, it's very advisable to asign a value in the definition, in this case we assign the minimun value, 1*/
int ax12Id=1;

// puts is very similar to printf, it shows the string that it receives as parameter
puts ("nnMoving a Dynamixel");

{ // starting the loop
puts ("Enter the ID of the AX-12 that you wwant to move, between 1 y 18, ");
ax12Id=readInteger(buffer); // this function will read from what we type in the keyboard
//// exclamation (!) is the NOT logical operator. It will repeat the loop while the value is not valid
}while(!isValid(ax12Id, 1, 18));

// Showing the typed value
printf("AX12 ID: %in", ax12Id);

return ax12Id;

// Now we will repeat almost the same code that above, it should be pretty easy to write a reusable function, isn't?

int getPosition()
char buffer[256];
int position=0;

puts ("Enter a value between 0 and 1023 as the goal position");
}while(!isValid(position, 0, 1023));

printf("nPosition: %in", position);

return position;

The functions used to read the text from the Terminal are quite interesting, showing the use of a string. It also shows how to use the .h file (declaration) and the .c (definitions, the content of the functions):

// Body (content) of the functions that read data

Recibimos una variable que tiene reservado espacio en memoria para almacenar
la cadena introducida

void readString(char bufferParameter[])
int i=0; // We'll use this variable as index of the buffer where we will store data
bufferParameter[i]=getchar(); // it store the read character in the i position of bufferParameter
putchar(bufferParameter[i]); // showing it
if (bufferParameter[i]=='b') // if Backspace was pressed
i--; // it goes to the previous position, rewritting the previoulsy typed character
else //
i++; // it will write in the next position
}while(bufferParameter[i-1]!='n'); // while the last values is not INTRO. The symmbol ! represents the logical operator NOT

bufferParameter[i]=0; // A NULL (0, zero) is necessary in the last position of any string

It read an string, it's converted to integer and returned
int readInteger(char bufferParameter[])
return atoi(bufferParameter);

You can download the sourcecode here

The language C, also C++, has some very interesting features such as the inclusion of code depending on certain conditions. This lets you use the same code for different processors by simply changing one or more parameters.

As an example, this ZIP contains a project for Dev-CPP with a version of the source files for the PC and the CM-510; simply commenting or uncommenting a line in the file “myCM510.h” (for AVR Studio you should create the appropriate project and include the sources files).

Instead of moving AX-12 it displays the text “dxl_write_word” with the parameters received. We can practice C and perform different tests on the PC without connecting any servo.

Series Navigation<< Bioloid CM-510 programming tutorial: First steps with C

2 comments on “Programming CM-510 with C: reading values from terminal and moving a Dynamixel AX-12

  1. Manuel says:

    Hola Jose, podrias subir de nuevo el codigo, la direccion para descargarlo al parecer ya no existe.


Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/softwa03/public_html/softwaresouls/wp-content/plugins/most-shared-posts/msp-fetch.php on line 179

Warning: file_get_contents( failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/softwa03/public_html/softwaresouls/wp-content/plugins/most-shared-posts/msp-fetch.php on line 179
%d bloggers like this: