MCA Logo
MCA Online Reference Documentation
Main Index

3.4. Connecting parts

A part includes several functions in order to get informations about other already registered parts. With these functions the user is able to search for specific parts, check their ControllerInputs and SensorOutputs and is at last able to connect its ControllerOutput and SensorInput with these IOs.

The following example shows how to print a list of the names of all parts:

Example 3-5. Get informations about other already running parts (1)

int startup(tPart* part)
{
  char description[100];
  int number_of_parts=part->NumberOfParts();
  int part_index;
  for (part_index=0;part_index < number_of_parts;part_index++)
  {
      part->GetPartDescription(part_index,description);
      UINFO("part no %i is called %s and belongs to level %i.\n",part_index,description,part->PartLevel(i);
  }
}

If a special part is demanded the returned description have to be compared with a given name. The following example searches a part named guitest and prints all ControllerOutput and SensorInput descriptions.

Example 3-6. Get informations about other already running parts (2)

int startup(tPart* part)
{
  char description[100];
  int number_of_parts=part->NumberOfParts();
  int part_index,io_index;
  for (part_index=0;part_index < number_of_parts;part_index++)
  {
      part->GetPartDescription(part_index,description);
      if (!strcmp(description,"guitest"))
      {
         for (io_index=0;io_index < part->ControllerInputDimensionOfPart(part_index);io_index++)
         {
	     part->GetControllerInputDescriptionOfPart(part_index,io_index,description);
             UINFO("CI %i has description %s.\n",io_index,description);
	 }
         for (io_index=0;io_index < part->SensorOutputDimensionOfPart(part_index);io_index++)
         {
	     part->SensorOutputDescriptionOfPart(part_index,io_index,description);
             UINFO("SO %i has description %s.\n",io_index,description);
	 }
      }
  }
}

The above functions can be used in order to verify existence and IO-structure of needed parts. It's not necessary to check parts before connecting to them but it is recommended as especially IOs may have changed and then unpredictable behaviour may occur.

Connecting parts is done with socalled inter part edges. They are used in the same matter as edges within groups. Only free permutation of indices are not possible. An inter part edge copies a fragment of successive values of an IO vector of one part to a corresponding fragment of an IO vector of another part. A part can only connect its ControllerOutput to a ControllerInput of another part and/or an SensorOutput of another part to its SensorInput. Multiple connections to different parts are possible.

The following example shows how to connect to other parts. It is taken from the ctrltest program.

Example 3-7. Initialization of interpart edges

int startup(tPart* part)
{
  int controller_dimension=0,sensor_dimension=0;
  int highest_level=0,level,dim;

  int part_no=part->NumberOfParts();

  UINFO("ctrltest: found %i parts\n",part_no);
  // look for highest level
  int part_index, j;
  for (part_index=0;i< part_no;part_index++)
    {
      level=part->PartLevel(part_index);
      if (level>highest_level)
	highest_level=level;
    }
  // summarise all IOs of all parts of the highest level
  // (the CIs and SOs of the parts on the highest level are
  // not connected at all)
  for (part_index=0;part_index< part_no;part_index++)
       if (part->PartLevel(part_index)==highest_level)
	 {
	   controller_dimension+=part->ControllerInputDimensionOfPart(part_index);
	   sensor_dimension+=part->SensorOutputDimensionOfPart(part_index);
	 }
  // now we have got the dimension we need:
  tDescription* controller_description=new tDescription[controller_dimension+1];
  tDescription* sensor_description=new tDescription[sensor_dimension+1];
  // set end-marker
  controller_description[controller_dimension]=cDATA_VECTOR_END_MARKER;
  sensor_description[sensor_dimension]=cDATA_VECTOR_END_MARKER;

  // here we write the actual values of CI of the parts
  controller_startvalues=new tDataType[controller_dimension];

  // further only used as indices:
  controller_dimension=0;
  sensor_dimension=0;

  // now we read IO descriptions and CI values
  char buffer[1000];
  for (part_index=0;part_index< part_no;part_index++)
    if (part->PartLevel(part_index)==highest_level)
      {
	for (j=0;j< part->ControllerInputDimensionOfPart(part_index);j++)
	  {
	    part->GetControllerInputDescriptionOfPart(part_index,j,buffer);
	    controller_description[controller_dimension]=strdup(buffer);
	    controller_startvalues[controller_dimension]=part->ControllerInputValueOfPart(part_index, j);
	    controller_dimension++;
	  }
	for (j=0;j< part->SensorOutputDimensionOfPart(part_index);j++)
	  {
	    part->GetSensorOutputDescriptionOfPart(part_index,j,buffer);
	    sensor_description[sensor_dimension++]=strdup(buffer);
	  }
      }

  UINFO("ctrltest: IO-Dims:%i %i\n",controller_dimension,sensor_dimension);

  // create a module with the read dimensions and descriptions:
  new tPassValues(part,controller_dimension,sensor_dimension,controller_description,sensor_description);

  //################################################
  //#  Connect Parts
  //################################################

  // again only as indices!
  controller_dimension=0;
  sensor_dimension=0;

  // Now we connect the corresponding parts:
  for (part_index=0;part_index< part_no;part_index++)
    {
      if (part->PartLevel(part_index)==highest_level)
	{

	  dim=part->ControllerInputDimensionOfPart(part_index);
	  if (dim)
	    {
	      // connect this CO with other CI
	      part->AddEdgeDownTo(part_index,dim,controller_dimension,0);
	      controller_dimension+=dim;
	    }

	  dim=part->SensorOutputDimensionOfPart(part_index);
	  if (dim)
	    {
	      // connect other SO with this SI
	      part->AddEdgeUpFrom(part_index,dim,0,sensor_dimension);
	      sensor_dimension+=dim;
	    }
	}
    }
  UINFO("ctrltest: running...\n");
  return part->Execute(tTime().FromMSec(100), controller_startvalues);
}

More informations about parts, their functions and possible informations about other parts can be founds in th API of tPartBase.