Programming with YARP#
Programming in C++ with YARP#
- We usually derive our base classes from yarp::os::RFModule, thus inheriting a configure(yarp::os::ResourceFinder& rf) method that receives a map (rf) passed from
main(), a close() that gets called by CRTL+C, and a updateModule() which is invoked with a periodicity measured in seconds given by getPeriod(). In case you need a function that gets called more often, you may inherit the run() method from yarp::os::PeriodicThread and obtain a periodicity given in seconds wih double precision, to be specified in the constructor. - Implement your device as a class, and ideally as a YARP device (tutorial (Spanish)).
Regarding close()#
If there exists a close method that needs to release unmanaged resources (dynamically allocated memory) or terminate stuff in an ordered manner (if using PolyDriver class members, e.g. close device A before device B), always define a class destructor that calls close, be it a DeviceDriver or an RFModule derived class. Also, make sure nothing bad happens if this close method is called several times (i.e. set dangling pointers to nullptr). Why is that:
PolyDriver::openmay fail to initialize a subdevice, but it does not call the subdevice'sclosemethod; instead, it is immediately destructed via delete (ref).- If
RFModule::configurereturns false, as explained in the above comments,closewill never be called, hence we also want to use a destructor here. PolyDriver::closewill never close a wrapped device twice, but callers ofRFModulecan do that inadvertently because of the previous point: once after a successfulRFModule::configureand aCTRL+Csignal (it just stops execution flow and calls close before leavingrunModule), and one more time on class destruction.- Despite those two mechanisms being relatively different, this policy ensures we treat both
RFModuleandDeviceDriverconstructs in a similar manner:close()on destruction, avoid dangling pointers.
Similar and Related#
If you have any doubts or comments#
Please read the Asking Questions section, and once you've succeded with its self-evaluation follow the recommendations by commenting publicly HERE if required.