Getting accustomed with motor interfaces

This tutorial shows how to get interfaces to control the arm.


Motor control in YARP is done through a motor control device.

The code in this tutorial is a good example to start playing with motor control. This example uses a remoted device driver which exports exactly the same interfaces of the actual device driver (only initialization is different). The main difference between the two is in terms of efficiency, if you're developing a low-level control loop you might want to be local, on the other hand a simple sporadic position control can be effected through the remote device driver. Let's assume we have started the server side already (e.g. the simulator).


Let us assume the simulator is up and running.

In practice, we start by preparing a set of configuration parameters:

Property options;
options.put("device", "remote_controlboard");
options.put("local", "/test/client"); //local port names
options.put("remote", "/icubSim/right_arm"); //where we connect to

where the ''local'' and ''remote'' parameters are used to set the port names for the connection to the server side. In our example this means that the process will create the following (local) ports:


And will automatically connect them to the server, on the following (remote) ports (we assume here that the server created those ports correctly):


To create the driver we use the PolyDriver:

PolyDriver robotDevice(options);
if (!robotDevice.isValid()) {
printf("Device not available. Here are the known devices:\n");
printf("%s", Drivers::factory().toString().c_str());
return 1;

which instantiates the driver. If the parameters are correct and the server side has been prepared the local driver also connects to the remote one. The next step is to get a set of interfaces (pointers to) to work with, for example:

IPositionControl *pos;
IVelocityControl *vel;
IEncoders *enc;



if everything went ok the pointers are valid pointers (check it!). For example:

if (pos==0) {
printf("Error getting IPositionControl interface.\n");
return 1;

We can thus start with checking how many axes we can control by doing:

int jnts = 0;

Let's create some vectors for later use:

Vector tmp;
Vector encoders;
Vector command_position;
Vector command_velocity;

Let's now initialize the controllers and start up the amplifiers:

/* we need to set reference accelerations used to generate the velocity */
/* profile, here 50 degrees/sec^2 */
int i;
for (i = 0; i < jnts; i++) {
tmp[i] = 50.0;

if needed we can check the position of our axes by doing:


which reads the values of the motor encoders into a vector of doubles of size ''jnts''.

Important: check the return value of this function to ensure that communication with the robot is sane and that the function can return updated values.

bool ret=enc->getEncoders(;
if (!ret)
fprintf(stderr, "Error reading encoders, check connectivity with the robot\n");
/* use encoders */

Position control

First do:

int i;
for (i = 0; i < jnts; i++) {
tmp[i] = 40.0;

which sets the reference speed for all axes to 40 degrees per second. The reference speed is used to interpolate the trajectory between the current and the desired position.

Now you can do:

bool ok = pos->positionMove(;

where ''command_position'' is the desired position. This starts a movement of all axes toward the desired position.

Velocity control

To send velocity commands do:

bool ok = vel->velocityMove(;

which accelerates all axes to the velocity described by the vector ''command_velocity'' (in degrees per second).

Closing the device

When you are done with controlling the robot don't forget to close the device:


More interfaces

To read more about the interfaces that are available in YARP read the online documentation:


See code in: src/motorControlBasic/tutorial_arm.cpp