iCub-main
Getting accustomed with motor interfaces

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

Introduction

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).

Initialization

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:

/test/client/state:i
/test/client/command:o
/test/client/rpc:o

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

/icubSim/right_arm/state:o
/icubSim/right_arm/command:i
/icubSim/right_arm/rpc:i

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;

and:

robotDevice.view(pos);
robotDevice.view(vel);
robotDevice.view(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;
pos->getAxes(&jnts);

Let's create some vectors for later use:

Vector tmp;
Vector encoders;
Vector command_position;
Vector command_velocity;
tmp.resize(jnts);
encoders.resize(jnts);
...

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;
}
pos->setRefAccelerations(tmp.data());

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

enc->getEncoders(encoders.data());

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(encoders.data());
if (!ret)
{
fprintf(stderr, "Error reading encoders, check connectivity with the robot\n");
}
else
{
/* use encoders */
}

Position control

First do:

int i;
for (i = 0; i < jnts; i++) {
tmp[i] = 40.0;
}
pos->setRefSpeeds(tmp.data());

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(command_position.data());

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(command_velocity.data());

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:

robotDevice.close();

More interfaces

To read more about the interfaces that are available in YARP read the online documentation: http://eris.liralab.it/yarpdoc/namespaceyarp_1_1dev.html

Code

See code in: src/motorControlBasic/tutorial_arm.cpp