Difference between revisions of "The protoComponent Example"

From David Vernon's Wiki
Jump to: navigation, search
(protoComponentConfiguration.cpp)
(protoComponentConfiguration.cpp)
Line 399: Line 399:
 
       return false;  // unable to open; let RFModule know so that it won't run
 
       return false;  // unable to open; let RFModule know so that it won't run
 
     }
 
     }
 
+
 
     if (!imageOut.open(outputPortName.c_str())) {
 
     if (!imageOut.open(outputPortName.c_str())) {
 
       cout << getName() << ": unable to open port " << outputPortName << endl;
 
       cout << getName() << ": unable to open port " << outputPortName << endl;

Revision as of 14:36, 31 August 2014

protoComponent.h

/** @file protoComponent.h  Interface file for the example DREAM component
 *
 * Version 1.0
 * 
 * 20/8/2014
 *
 *
 * \defgroup DREAM_protoComponent protoComponent
 * 
 * This application demonstates the use of the protoComponent module. 
 * This is a simple example to illustrate a component that is compliant with
 * mandatory DREAM software standards for
 * 
 * 1. File organization
 * 2. Internal source code documentation
 * 3. Component functionality
 * 
 * as well as recommended DREAM software standards for 
 * 
 * 4. Programming style
 * 5. Programming practice 
 * 
 * These standards are documented in Appendices A-E of Deliverable 3.2 
 * 
 * Overall, the standards address the 4 Cs of component-based software engineering:
 * 
 * - configuration
 * - coordination
 * - computation
 * - computation
 *
 * Functionally, the module just reads an input image from an input port, 
 * converts it to a binary image based on the supplied threshold,
 * and writes it to an output port
 *
 * The module also reads a configuration file containing the intrinsic
 * parameters of the robot's two cameras as an example of more advanced parsing 
 * techniques.
 *
 * A complete tutorial for this example is available on the DREAM wiki 
 *
 * 
 * \section lib_sec Libraries
 *
 * YARP.
 *
 * \section parameters_sec Parameters
 * 
 * Command-line Parameters  
 * 
 * The following key-value pairs can be specified as command-line parameters 
 * by prefixing \c -- to the key e.g. \c --from file.ini. 
 * The value part can be changed to suit your needs; the default values are 
 * shown below. 
 *
 * - \c from \c protoComponent.ini \n 
 *   specifies the configuration file
 *
 * - \c context \c protoComponent/configuration \n
 *   specifies the sub-path from \c $DREAM_ROOT/protoComponent/configuration 
 *   to the configuration file
 *
 * - \c name \c protoComponent \n   
 *   specifies the name of the module (used to form the stem of module port names)  
 *
 *
 * Configuration File Parameters 
 *
 * The following key-value pairs can be specified as parameters in the configuration file 
 * (they can also be specified as command-line parameters if you so wish). 
 * The value part can be changed to suit your needs; the default values are shown below. 
 *   
 * - \c myInputPort \c /image:i \n    
 *   specifies the input port name (this string will be prefixed by \c /protoComponent 
 *   or whatever else is specifed by the name parameter
 *
 * - \c myOutputPort \c /image:o \n  
 *   specifies the output port name (this string will be prefixed by \c /protoComponent 
 *   or whatever else is specifed by the name parameter
 *
 * - \c cameraConfig \c cameras.ini \n
 *   specifies the camera configuration file containing the intrinsic parameters of
 *   the left and right cameras
 *
 * - \c threshold \c 7 \n           
 *   specifies the threshold value
 *
 * 
 * \section portsa_sec Ports Accessed
 * 
 * - None
 *                      
 * \section portsc_sec Ports Created
 *
 *  Input ports
 *
 *  - \c /protoComponent \n
 *    This port is used to change the parameters of the module at run time or stop the module. \n
 *    The following commands are available
 * 
 *    \c help \n
 *    \c quit \n
 *    \c set \c thr \c <n>   ... set the threshold for binary segmentation of the input RGB image 
 *    (where \c <n> is an integer number)
 *
 *    Note that the name of this port mirrors whatever is provided by the \c --name parameter value
 *    The port is attached to the terminal so that you can type in commands and receive replies.
 *    The port can be used by other modules but also interactively by a user through the yarp rpc directive, viz.: \c yarp \c rpc \c /protoComponent
 *    This opens a connection from a terminal to the port and allows the user to then type in commands and receive replies.
 *       
 *  - \c /protoComponent/image:i \n
 *
 * Output ports
 *
 *  - \c /protoComponent \n
 *    see above
 *
 *  - \c /protoComponent/image:o \n
 *
 * Port types 
 *
 * The functional specification only names the ports to be used to communicate with the module 
 * but doesn't say anything about the data transmitted on the ports. This is defined by the following code. 
 *
 * \c BufferedPort<ImageOf<PixelRgb> >   \c myInputPort; \n 
 * \c BufferedPort<ImageOf<PixelRgb> >   \c myOutputPort;       
 *
 * \section in_files_sec Input Data Files
 *
 * None
 *
 * \section out_data_sec Output Data Files
 *
 * None
 *
 * \section conf_file_sec Configuration Files
 *
 * \c protoComponent.ini   
 * \c cameras.ini  
 *
 * \section example_sec Example Instantiation of the Module
 * 
 * protoComponent --name protoComponent --context components/protoComponent/config --from protoComponent.ini 
 *
 * \author 
 * 
 * <name of author>, <author institute>  
 * 
 * Copyright (C) 2014 DREAM Consortium
 * 
 */

/* 
 * Copyright (C) 2014 DREAM Consortium
 * FP7 Project 611391 co-funded by the European Commission
 *
 * Author:  <name of author>, <author institute> 
 * Email:   <preferred email address> 
 * Website: www.dream20202.eu 
 *
 * This program comes with ABSOLUTELY NO WARRANTY 
 */
 
  
/*
 * Audit Trail
 * -----------
 * 20/08/14  First version validated (David Vernon)
 */ 
 
#include <iostream>
#include <string>

#include <yarp/sig/all.h>
#include <yarp/os/all.h>
#include <yarp/os/RFModule.h>
#include <yarp/os/Network.h>
#include <yarp/os/Thread.h>
 
using namespace std;
using namespace yarp::os; 
using namespace yarp::sig;
  
class ProtoComponentThread : public Thread
{
private:

  /* class variables */

  int      x, y;
  PixelRgb rgbPixel;
  ImageOf<PixelRgb> *image;
  	    
  /* thread parameters: they are pointers so that they refer to the original variables in protoComponent */

  BufferedPort<ImageOf<PixelRgb> > *imagePortIn;
  BufferedPort<ImageOf<PixelRgb> > *imagePortOut;   
  int *thresholdValue;     

public:

  /* class methods */

  ProtoComponentThread(BufferedPort<ImageOf<PixelRgb> > *imageIn,  BufferedPort<ImageOf<PixelRgb> > *imageOut, int *threshold );
  bool threadInit();     
  void threadRelease();
  void run(); 
};


class ProtoComponent:public RFModule
{
  /* module parameters */

  string moduleName;
  string inputPortName;
  string outputPortName;  
  string handlerPortName;
  string cameraConfigFilename;
  float  fxLeft,  fyLeft;          // focal length
  float  fxRight, fyRight;         // focal length
  float  cxLeft,  cyLeft;          // coordinates of the principal point
  float  cxRight, cyRight;         // coordinates of the principal point
  int    thresholdValue;

  /* class variables */
 
  BufferedPort<ImageOf<PixelRgb> > imageIn;      //example input port
  BufferedPort<ImageOf<PixelRgb> > imageOut;     //example output port
  Port handlerPort;      //a port to handle messages 

  /* pointer to a new thread to be created and started in configure() and stopped in close() */

  ProtoComponentThread *protoComponentThread;
 

public:
    
  bool configure(yarp::os::ResourceFinder &rf); // configure all the module parameters and return true if successful
  bool interruptModule();                       // interrupt, e.g., the ports 
  bool close();                                 // close and shut down the module
  bool respond(const Bottle& command, Bottle& reply);
  double getPeriod(); 
  bool updateModule();
};

protoComponentMain.cpp

/* 
 * Copyright (C) 2014 DREAM Consortium
 * FP7 Project 611391 co-funded by the European Commission
 *
 * Author:  <name of author>, <author institute> 
 * Email:   <preferred email address> 
 * Website: www.dream20202.eu 
 *
 * This program comes with ABSOLUTELY NO WARRANTY 
 */
  

/*
 * Audit Trail
 * -----------
 * 20/08/14  First version validated (David Vernon)
 */ 
 
#include "protoComponent.h" 
 
int main(int argc, char * argv[])
{
  /* initialize yarp network */ 
  
  Network yarp;
 
  /* create your module */
 
  ProtoComponent protoComponent; 
 
  /* prepare and configure the resource finder */
 
  ResourceFinder rf;
  rf.setVerbose(true);
  rf.setDefaultConfigFile("protoComponent.ini");          // can be overridden by --from parameter
  rf.setDefaultContext("protoComponent/configuration");   // can be overridden by --context parameter
  rf.configure("DREAM_ROOT", argc, argv);                 // environment variable with root of configuration path
  
  /* run the module: runModule() calls configure first and, if successful, it then runs */
 
  protoComponent.runModule(rf);
 
  return 0;
}


protoComponentConfiguration.cpp

/* 
 * Copyright (C) 2014 DREAM Consortium
 * FP7 Project 611391 co-funded by the European Commission
 *
 * Author:  <name of author>, <author institute> 
 * Email:   <preferred email address> 
 * Website: www.dream20202.eu 
 *
 * This program comes with ABSOLUTELY NO WARRANTY 
 */
 

/*
 * Audit Trail
 * -----------
 * 20/08/14  First version validated (David Vernon)
 */ 

#include "protoComponent.h"

 
/* 
 * Configure method ... use it to do component coordination, 
 * i.e. to configure your component at runtime
 */

bool ProtoComponent::configure(yarp::os::ResourceFinder &rf)
{    
   /* Process all parameters from both command-line and .ini file */

   /* get the module name which will form the stem of all module port names */

   moduleName            = rf.check("name", 
                           Value("protoComponent"), 
                           "module name (string)").asString();

   /*
    * before continuing, set the module name before getting any other parameters, 
    * specifically the port names which are dependent on the module name
    */
   
   setName(moduleName.c_str());
 
   /* now, get the rest of the parameters */

   /* get the name of the input and output ports, automatically prefixing the module name by using getName() */

   inputPortName         = "/";
   inputPortName        += getName(
                           rf.check("protoInputPort", 
                           Value("/image:i"),
                           "Input image port (string)").asString()
                           );
   
   outputPortName        = "/";
   outputPortName       += getName(
                           rf.check("protoOutputPort", 
                           Value("/image:o"),
                           "Output image port (string)").asString()
                           );


   /* get the threshold value */

   thresholdValue        = rf.check("threshold",
                           Value(8),
                           "Key value (int)").asInt();

   
   /* 
    * get the cameraConfig file and read the required parameter values cx, cy 
    * in both the groups [CAMERA_CALIBRATION_LEFT] and [CAMERA_CALIBRATION_RIGHT]
    */

   cameraConfigFilename  = rf.check("cameraConfig", 
                           Value("cameras.ini"), 
                           "camera configuration filename (string)").asString();

   cameraConfigFilename = (rf.findFile(cameraConfigFilename.c_str())).c_str();

   Property cameraProperties;

   if (cameraProperties.fromConfigFile(cameraConfigFilename.c_str()) == false) {
      cout << "protoComponent: unable to read camera configuration file" << cameraConfigFilename << endl;
      return 0;
   }
   else {
      cxLeft  = (float) cameraProperties.findGroup("CAMERA_CALIBRATION_LEFT").check("cx", Value(160.0), "cx left").asDouble();
      cyLeft  = (float) cameraProperties.findGroup("CAMERA_CALIBRATION_LEFT").check("cy", Value(120.0), "cy left").asDouble();
      cxRight = (float) cameraProperties.findGroup("CAMERA_CALIBRATION_RIGHT").check("cx", Value(160.0), "cx right").asDouble();
      cyRight = (float) cameraProperties.findGroup("CAMERA_CALIBRATION_RIGHT").check("cy", Value(120.0), "cy right").asDouble();
   }


   /* do all initialization here */
     
   /* open ports  */ 
       
   if (!imageIn.open(inputPortName.c_str())) {
      cout << getName() << ": unable to open port " << inputPortName << endl;
      return false;  // unable to open; let RFModule know so that it won't run
   }

   if (!imageOut.open(outputPortName.c_str())) {
      cout << getName() << ": unable to open port " << outputPortName << endl;
      return false;  // unable to open; let RFModule know so that it won't run
   }

   /*
    * attach a port of the same name as the module (prefixed with a /) to the module
    * so that messages received from the port are redirected to the respond method
    */
 
   handlerPortName =  "/";
   handlerPortName += getName();         // use getName() rather than a literal 
 
   if (!handlerPort.open(handlerPortName.c_str())) {           
      cout << getName() << ": Unable to open port " << handlerPortName << endl;  
      return false;
   }

   attach(handlerPort);                  // attach to port
 
   /* create the thread and pass pointers to the module parameters */

   protoComponentThread = new ProtoComponentThread(&imageIn, &imageOut, &thresholdValue);
   /* now start the thread to do the work */

   protoComponentThread->start(); // this calls threadInit() and it if returns true, it then calls run()

   return true ;      // let the RFModule know everything went well
                      // so that it will then run the module
}


bool ProtoComponent::interruptModule()
{
   imageIn.interrupt();
   imageOut.interrupt();
   handlerPort.interrupt();
 
   return true;
}


bool ProtoComponent::close()
{
   /* stop the thread */

   imageIn.close();
   imageOut.close();
   handlerPort.close();

   protoComponentThread->stop();

   return true;
}


bool ProtoComponent::respond(const Bottle& command, Bottle& reply) 
{
  string helpMessage =  string(getName().c_str()) + 
                        " commands are: \n" +  
                        "help \n" + 
                        "quit \n" + 
                        "set thr <n> ... set the threshold \n" + 
                        "(where <n> is an integer number) \n";

  reply.clear(); 

  if (command.get(0).asString()=="quit") {
       reply.addString("quitting");
       return false;     
   }
   else if (command.get(0).asString()=="help") {
      cout << helpMessage;
	  reply.addString("command is: set thr <n>");
   }
   else if (command.get(0).asString()=="set") {
      if (command.get(1).asString()=="thr") {
         thresholdValue = command.get(2).asInt(); // set parameter value
         reply.addString("ok");
       }
   }
   return true;
}


/* Called periodically every getPeriod() seconds */
 
bool ProtoComponent::updateModule()
{
   return true;
}



double ProtoComponent::getPeriod()
{
   /* module periodicity (seconds), called implicitly by protoComponent */
   
   return 0.1;
}

protoComponentComputation.cpp