|
May
20
2010
Developing Well-Designed Packages for Robot Operating System (ROS), Part IIIPart III: Developing and Testing the Domain Layer [Author's note, July 28, 2010: Fixed minor bug in LaserScanReader.cpp wherein it couldn't be restarted after being stopped; had to reset _stopRequested to false.] In Part II of this series, we created the humble beginnings of the package and added folders to accommodate all of the layers of our end product, a well-designed ROS package that reports (fake) laser scan reports. In this post, the domain layer of the package will be fleshed out along with unit tests to verify the model and functionality, accordingly. The entire focus will be on implementing just one of the requirements initially described in Part I: The package will read laser reports coming from a laser range-finder. If you’d like to download the resulting source for this article, click here. That certainly sounds easy enough. Disregarding the previous discussions concerning architecture, the gut reaction might be to start adding code to main(), simply taking the results from the range-finder, turning them directly into a ROS message, and publishing the messages to the appropriate ROS topic. This myopic “get ‘er done” approach quickly gets out of hand as main() turns into a tangled mess of code managing a variety of responsibilities. Object oriented principles aside, having all of these separate concerns mashed into main turns the little package into a maintenance nightmare with little ability to reuse code. As mentioned, the first concern that we’ll want to tackle is the ability to read laser range-finder reports. We’ll tackle this requirement by encapsulating the range-finder integration code within a class called LaserScanReader.cpp. By doing so, all of the communications to the range-finder are properly encapsulated within one or more classes, making the integration code easier to reuse and maintain. To keep our focus on the overall architecture, and to avoid the need to have a physical range-finder handy, we’ll simulate range-finder communications within LaserScanReader.cpp. Certainly an added benefit of this approach, if we were doing this for a real-world package, is that one group could work on the “rest” of the package while another group works on the actually range-finder communications; so when ready, LaserScanReader.cpp could be switched out with the “real” range-finder integration code. Prerequisites:
Before proceeding, recall that, as described in Part I, the domain layer of the package should not have knowledge concerning how to communicate with the messaging middleware directly (e.g., ROS). This implies that the domain layer should have no direct dependencies on the messaging middleware. This allows the domain layer to be more easily reused with another messaging middleware solution. Additionally, keeping this clean separation of concerns facilitates the testing of the domain layer independently from its interactions with the messaging middleware. Accordingly, the simple domain layer developed in this post will adhere to this guidance along with full testing for verification of capabilities as well. Our LaserScanReader class will expose two methods, beginReading() and stopReading(), along with an observer hook to provide a call-back to be invoked whenever a new reading is available. For now, we won’t worry about what exactly will be called back in the completed package, as that’ll be a concern of the application services layer; but we’ll need to prepare for it by including an interface for the laser scan observer. Target Class Diagram The following diagram shows what the package will look like after completing the steps in this post. While the individual elements will be discussed in more detail; the class diagram should serve as a good bird’s eye view of the current objectives.
1. Setup the Package Skeleton If not done already, follow the steps in Part II to create the beginnings of the package. 2. Create the ILaserScanListener Observer Header Whenever a laser scan is read, it’ll need to be given to whomever is interested in it. It should not be the responsibility of the laser scan reader to predict who will want the laser scans. Accordingly, an observer interface should be introduced which the laser scan reader will communicate through to raise laser scan events to an arbitrary number of listeners. Add a new interface header file to /ladar_reporter/src/core called ILaserScanListener.hpp containing the following code: // ILaserScanListener.hpp #ifndef GUARD_ILaserScanListener #define GUARD_ILaserScanListener #include "sensor_msgs/LaserScan.h" namespace ladar_reporter_core { class ILaserScanListener { public: // Virtual destructor to pass pointer ownership without exposing base class [Meyers, 2005, Item 7] virtual ~ILaserScanListener() {} virtual void onLaserScanAvailableEvent(const sensor_msgs::LaserScan& laserScan) const = 0; }; } #endif /* GUARD_ILaserScanListener */ As you can see, this C++ interface (or as close as you can get to an interface in C++) simply exposes a single function to handle laser scan events. 3. Create the LaserScanReader Header Add a new class header file to /ladar_reporter/src/core called LaserScanReader.hpp containing the following code, which we’ll discuss in detail below. // LaserScanReader.hpp #ifndef GUARD_LaserScanReader #define GUARD_LaserScanReader #include <pthread.h> #include <vector> #include "sensor_msgs/LaserScan.h" #include "ILaserScanListener.hpp" namespace ladar_reporter_core { class LaserScanReader { public: LaserScanReader(); void beginReading(); void stopReading(); // Provides a call-back mechanism for objects interested in receiving scans void attach(ILaserScanListener& laserScanListener); private: void readLaserScans(); void notifyLaserScanListeners(const sensor_msgs::LaserScan& laserScan); std::vector<ILaserScanListener*> _laserScanListeners; // Basic threading support as suggested by Jeremy Friesner at // http://stackoverflow.com/questions/1151582/pthread-function-from-a-class volatile bool _stopRequested; volatile bool _running; pthread_t _thread; static void * readLaserScansFunction(void * This) { ((LaserScanReader *)This)->readLaserScans(); return 0; } }; } #endif /* GUARD_LaserScanReader */ Let’s now review the more interesting parts of the header class:
4. Create the LaserScanReader Class Implementation Add a new class file to /ladar_reporter/src/core called LaserScanReader.cpp containing the following code, which we’ll discuss in detail below. // LaserScanReader.cpp #include "LaserScanReader.hpp" namespace ladar_reporter_core { LaserScanReader::LaserScanReader() : _stopRequested(false), _running(false) { _laserScanListeners.reserve(1); } void LaserScanReader::attach(ILaserScanListener& laserScanListener) { _laserScanListeners.push_back(&laserScanListener); } void LaserScanReader::beginReading() { if (! _running) { _running = true; _stopRequested = false; // Spawn async thread for reading laser scans pthread_create(&_thread, 0, readLaserScansFunction, this); } } void LaserScanReader::stopReading() { if (_running) { _running = false; _stopRequested = true; // Wait to return until _thread has completed pthread_join(_thread, 0); } } void LaserScanReader::readLaserScans() { int i = 0; while (! _stopRequested) { sensor_msgs::LaserScan laserScan; // Just set the angle_min to include some data laserScan.angle_min = ++i; notifyLaserScanListeners(laserScan); sleep(1); } } void LaserScanReader::notifyLaserScanListeners(const sensor_msgs::LaserScan& laserScan) { for (int i= 0; i < _laserScanListeners.size(); i++) { _laserScanListeners[i]->onLaserScanAvailableEvent(laserScan); } } } Let’s now review the more interesting parts of the class implementation:
5. Add a ROS sensor_msgs Dependency to manifest.xml Since the code above refers to sensor_msgs::LaserScan, a dependency needs to be added for the package to use this class, accordingly:
6. Configure CMake to Include both the Header and Implementation With the header and implementation classes completed, we need to make a couple of minor modifications to CMake for their inclusion in the build. First, open /ladar_reporter/CMakeLists.txt and make the following modifications:
In doing so, the LaserScanReader header has been included for consumption by other classes and application layers. This inclusion has been done in the root CMakeLists.txt as this will make the headers available to the unit tests as well. For more complex packages, this approach of including the headers in the root CMakeLists.txt, to make them globally accessible, may become a bit messy; it may be more appropriate to put the header inclusions in only the CMakeLists.txt which actually require the respective headers if the package is larger. At this point, a new CMake file is needed under /ladar_report/src:
You’ll quickly notice that this CMake file has merely passed the buck of defining class libraries further down the chain. Accordingly, a CMakeLists.txt will be setup for each of the package layers including application_services, core, message_endpoints, and ui. All of these layers will be compiled into separate class libraries and, finally, an executable. Arguably, all of these layers could be combined into a single executable with a single CMakeLists.txt file. But keeping them in separate class libraries keeps a clean separation of concerns in their respective responsibilities and makes each aspect of our package more easily testable in isolation from the other layers. Next, in order to create the core class library, a new CMake file is needed under /ladar_reporter/src/core:
We’re now ready to compile the class library for the “core” layer of the package… 7. Build the core Class Library In a terminal window, cd to /ladar_reporter and run Woohoo! Done, right? Well, not yet…time to test our new functionality. 8. Unit Test the LadarScanReader Functionality So far, you’ve had to simply assume that a successful build means everything is working as expected. Obviously, when developing a ROS package, we’ll want a bit more reassurance than a successful build to be confident that the developed capabilities are working as expected. Accordingly, unit tests should be developed to test the functionality; in the case at hand, a unit test will be developed to initialize, begin and stop the laser reading cycle to ensure that it is raising laser scan events as designed. The standard ROS testing tool is gtest; this is a great choice as gtest is very easy to setup and provides informative output during unit test execution. To setup and run unit tests for the package, only two elements are needed: a “test runner” to act as the unit tests’ main and to execute all of the package unit tests, and the unit tests themselves which may be spread out among a variety of folders and classes. When unit testing, I typically write one unit testing class (a test fixture) per class being tested. Furthermore, I include one unit test for each public function or behavior of the class being tested. As for a couple of other unit testing best practices, be sure to keep the unit tests independent from each other – they should be able to be run in isolation without being dependent on the running of other unit tests. Additionally, each unit test is organized as three stages of the test: “establish context” wherein the testing context is setup, “act” wherein the desired behavior is invoked, and “assert” wherein the results of the behavior are verified. Finally, no testing code should be added to the “production” source code itself; all tests should be maintained in a separate executable to keep a clean separation of concerns between application code and tests. We’ll see an example of this as we proceed.
When the test runs, you should see a few laser scan angle_min values get printed to the terminal along with the final report that the test successfully passed. Now that we’ve done all of the above to complete the core domain layer of the package, let’s review what have was accomplished:
In Part IV, we’ll be setting our sights on developing and testing the application services layer of the application with a test double standing in for the ROS messaging system. Enjoy! |
© 2011-2012 Codai, Inc. All Rights Reserved