![]() | ![]() Main Index |
| MCA2 Programmer's Guide | ||||
|---|---|---|---|---|
| Prev | Fast Backward | Chapter 3. Building MCA parts | Fast Forward | Next |
A standard main program for MCA parts can be generated automatically using the newPart script. It needs a project directory and the name of the main module as parameters. Here is an example:
It creates the file project/test/part_ReplyValues.C, which contains all necessary functions. Of course you have to modify this file in most cases! The structure of a main program is therefore described in the following.
All MCA main programs need three functions: initPartDescription,
startup and shutdown.
In shutdown necessary cleanup routines may be
called. But in most cases this function rests empty, as all MCA
related cleanups (deletion of part and modules) are done automatically.
initPartDescriptioninitPartDescription is used to define version
string and command line options of the program.
Command line options can be easily defined using the tPartParameter struct. We also recommend to use enumerations as for all array definitions in MCA. The following values are examples taken from the puma tutorial.
Example 3-2. defining program command line options (1)
enum {
eOPT_NAME,
eOPT_MIN_ANGLE,
eOPT_MAX_ANGLE,
eOPT_ANGULAR_VELOCITY,
eOPT_DIMENSION
};
tPartParameter parameter[eOPT_DIMENSION+1]={
{"name:", "set the modules name"},
{"min:","set the minimum joint angle"},
{"max:","set the maximum joint angle"},
{"v:","set the angular velocity"},
{0,0}
}; |
The definitions can be done globally at the top of a main program.
They are used within the initPartDescription:
startupIn startup, the main module is created. Also
user defined parameters are evaluated and sometimes attributes of a
specified configuration file are read. The the main loop is executed.
The defined user command line parameters have to be read and the results have then be used to set parameters of modules (or used as construcotr parameter or used to decied weather simulation or real robot controle has to be done etc.).
The following example shows how to check and read command line
parameters within the startup function of our
main program:
Example 3-4. startup function
int startup(tPart* part)
{
float min=0;
float max=2*M_PI;
float v=0.1;
char *name=0;
if (part->ParamSet(eOPT_NAME))
name=part->ParamOpt(eOPT_NAME);
// Read other parameter values
if (part->ParamSet(eOPT_MIN_ANGLE))
min=atof(part->ParamOpt(eOPT_MIN_ANGLE));
if (part->ParamSet(eOPT_MAX_ANGLE))
max=atof(part->ParamOpt(eOPT_MAX_ANGLE));
if (part->ParamSet(eOPT_ANGULAR_VELOCITY))
v=atof(part->ParamOpt(eOPT_ANGULAR_VELOCITY));
// create main module (which is a group)
mJointSimulation *joint=new mJointSimulation(part,name);
// Set module parameter values
joint->SetParameters(mJointSimulation::ePAR_DIMENSION,
mJointSimulation::ePAR_MIN_ANGLE,min,
mJointSimulation::ePAR_MAX_ANGLE,max,
mJointSimulation::ePAR_ANGULAR_VELOCITY,v);
// Execute as usual
return part->Execute(tTime().FromMSec(30));
} |