In this tutorial we show more details of how to write an iCub module using the module helper class.
Introduction
In a previous tutorial we showed how to use the ResourceFinder to organize modules parameters in $YARP_DATA_HOME/contexts.
The RFModule helper class simplify writing an iCub module that uses the ResourceFinder class.
This is how a module will look like:
#include <iostream>
#include <iomanip>
#include <yarp/os/Network.h>
#include <yarp/os/RFModule.h>
using namespace std;
using namespace yarp::os;
class MyModule:public RFModule
{
Port handlerPort;
int count;
public:
double getPeriod()
{
return 1.0;
}
bool updateModule()
{
count++;
cout<<"["<<count<<"]"<< " updateModule... "<<endl;
return true;
}
bool respond(const Bottle& command, Bottle& reply)
{
cout<<"Got something, echo is on"<<endl;
if (command.get(0).asString()=="quit")
return false;
else
reply=command;
return true;
}
bool configure(yarp::os::ResourceFinder &rf)
{
count=0;
handlerPort.open("/myModule");
attach(handlerPort);
return true;
}
bool interruptModule()
{
cout<<"Interrupting your module, for port cleanup"<<endl;
return true;
}
bool close()
{
cout<<"Calling close function\n";
handlerPort.close();
return true;
}
};
and this is the main function to instantiate it.
int main(
int argc,
char * argv[])
{
MyModule module;
ResourceFinder rf;
rf.configure(argc, argv);
rf.setVerbose(true);
cout << "Configuring and starting module. \n";
module.runModule(rf);
cout<<"Main returning..."<<endl;
return 0;
}
Copyright (C) 2008 RobotCub Consortium.
Code
See code in: src/module/tutorial_module.cpp