YARP
Yet Another Robot Platform
The RFModule Class

Table of Contents

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:

1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include <yarp/os/Network.h>
10 #include <yarp/os/RFModule.h>
11 
12 #include <iostream>
13 
14 class MyModule : public yarp::os::RFModule
15 {
16  yarp::os::Port handlerPort; // a port to handle messages
17  int count;
18 public:
19  double getPeriod()
20  {
21  // module periodicity (seconds), called implicitly by the module.
22  return 1.0;
23  }
24  // This is our main function. Will be called periodically every getPeriod() seconds
25  bool updateModule()
26  {
27  count++;
28  std::cout << "[" << count << "]" << " updateModule..." << '\n';
29  return true;
30  }
31  // Message handler. Just echo all received messages.
32  bool respond(const yarp::os::Bottle& command, yarp::os::Bottle& reply)
33  {
34  std::cout << "Got something, echo is on" << '\n';
35  if (command.get(0).asString() == "quit")
36  return false;
37  else
38  reply = command;
39  return true;
40  }
41  // Configure function. Receive a previously initialized
42  // resource finder object. Use it to configure your module.
43  // If you are migrating from the old module, this is the function
44  // equivalent to the "open" method.
46  {
47  count=0;
48  if (!handlerPort.open("/myModule"))
49  return false;
50 
51  // optional, attach a port to the module
52  // so that messages received from the port are redirected
53  // to the respond method
54  attach(handlerPort);
55 
56  return true;
57  }
58  // Interrupt function.
59  bool interruptModule()
60  {
61  std::cout << "Interrupting your module, for port cleanup" << '\n';
62  return true;
63  }
64  // Close function, to perform cleanup.
65  bool close()
66  {
67  // optional, close port explicitly
68  std::cout << "Calling close function\n";
69  handlerPort.close();
70  return true;
71  }
72 };
73 
74 int main(int argc, char * argv[])
75 {
76  // initialize yarp network
78 
79  // create your module
80  MyModule module;
81 
82  // prepare and configure the resource finder
84  rf.configure(argc, argv);
85 
86  std::cout << "Configuring and starting module.\n";
87  // This calls configure(rf) and, upon success, the module execution begins with a call to updateModule()
88  if (!module.runModule(rf)) {
89  std::cerr << "Error module did not start\n";
90  }
91 
92  std::cout << "Main returning..." << '\n';
93  return 0;
94 }

See code in: examples/rfmodule/main.cpp

yarp::os::Port::close
void close() override
Stop port activity.
Definition: Port.cpp:357
yarp::os::RFModule
A base-class for standard YARP modules that supports ResourceFinder.
Definition: RFModule.h:24
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
Network.h
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:261
yarp::os::Port::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:82
yarp::os::RFModule::interruptModule
virtual bool interruptModule()
Try to halt any ongoing operations by threads managed by the module.
Definition: RFModule.cpp:487
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
yarp::os::RFModule::updateModule
virtual bool updateModule()=0
Override this to do whatever your module needs to do.
yarp::os::ResourceFinder::configure
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
Definition: ResourceFinder.cpp:803
yarp::os::Bottle::get
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
yarp::os::RFModule::attach
virtual bool attach(yarp::os::Port &source)
Make any input from a Port object go to the respond() method.
Definition: RFModule.cpp:459
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
module
static RFModule * module
Definition: RFModule.cpp:234
RFModule.h
yarp::os::RFModule::respond
virtual bool respond(const Bottle &command, Bottle &reply)
Respond to a message.
Definition: RFModule.cpp:448
yarp::os::RFModule::configure
virtual bool configure(yarp::os::ResourceFinder &rf)
Configure the module, pass a ResourceFinder object to the module.
Definition: RFModule.cpp:441
yarp::os::Network
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition: Network.h:786
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::RFModule::getPeriod
virtual double getPeriod()
You can override this to control the approximate periodicity at which updateModule() is called by run...
Definition: RFModule.cpp:320
yarp::os::ResourceFinder
Helper class for finding config files and other external resources.
Definition: ResourceFinder.h:33
yarp::os::RFModule::close
virtual bool close()
Close function.
Definition: RFModule.cpp:493