In this tutorial we show more details of how to write a YARP module using the module helper class.
Introduction
The RFModule helper class simplify writing a generic module. It provides support for:
- Perform periodic activities
- Handle signals, catch termination and perform smooth shutdown
- Parses messages from a port to monitor module activity and set/get parameters
- Use the ResourceFinder class to handle parameters
You do not need to understand the details of yarp::os::ResourceFinder to proceed with this tutorial. However it may be useful: ResourceFinder Tutorials and Specification.
Code
This is how a module will look like:
28 std::cout <<
"[" << count <<
"]" <<
" updateModule..." <<
'\n';
34 std::cout <<
"Got something, echo is on" <<
'\n';
48 if (!handlerPort.
open(
"/myModule"))
61 std::cout <<
"Interrupting your module, for port cleanup" <<
'\n';
68 std::cout <<
"Calling close function\n";
74 int main(
int argc,
char * argv[])
86 std::cout <<
"Configuring and starting module.\n";
88 if (!
module.runModule(rf)) {
89 std::cerr <<
"Error module did not start\n";
92 std::cout <<
"Main returning..." <<
'\n';
A simple collection of objects that can be described and transmitted in a portable way.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Utilities for manipulating the YARP network, including initialization and shutdown.
A mini-server for network communication.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
A base-class for standard YARP modules that supports ResourceFinder.
virtual bool updateModule()=0
Override this to do whatever your module needs to do.
virtual bool close()
Close function.
virtual bool respond(const Bottle &command, Bottle &reply)
Respond to a message.
virtual bool attach(yarp::os::Port &source)
Make any input from a Port object go to the respond() method.
virtual bool configure(yarp::os::ResourceFinder &rf)
Configure the module, pass a ResourceFinder object to the module.
virtual bool interruptModule()
Try to halt any ongoing operations by threads managed by the module.
virtual double getPeriod()
You can override this to control the approximate periodicity at which updateModule() is called by run...
Helper class for finding config files and other external resources.
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
virtual std::string asString() const
Get string value.
The main, catch-all namespace for YARP.
int main(int argc, char *argv[])
See code in: examples/rfmodule/main.cpp