MCA Logo
MCA Online Reference Documentation
Main Index

3.2. Write own main

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:

Example 3-1. generating part source file from a template

newPart project/test ReplyValues

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.

3.2.1. initPartDescription

initPartDescription 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:

Example 3-3. defining program command line options (2)

void initPartDescription()
{
  SetProgramDescription("\n This Program is a short description of what our program does.\n");
  SetProgramParameter(parameter);
}

3.2.2. startup

In 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));
}