SensorBase.cpp
Go to the documentation of this file.
00001 /*+-------------------------------------------------------------------------+
00002   |                       MultiVehicle simulator (libmvsim)                 |
00003   |                                                                         |
00004   | Copyright (C) 2014  Jose Luis Blanco Claraco (University of Almeria)    |
00005   | Copyright (C) 2017  Borys Tymchenko (Odessa Polytechnic University)     |
00006   | Distributed under GNU General Public License version 3                  |
00007   |   See <http://www.gnu.org/licenses/>                                    |
00008   +-------------------------------------------------------------------------+ */
00009 
00010 #include <mvsim/VehicleBase.h>
00011 #include <mvsim/Sensors/LaserScanner.h>
00012 
00013 #include <rapidxml.hpp>
00014 #include <rapidxml_utils.hpp>
00015 #include <rapidxml_print.hpp>
00016 #if MRPT_VERSION>=0x199
00017 #include <mrpt/core/format.h>
00018 #else
00019 #include <mrpt/utils/utils_defs.h>  // mrpt::format()
00020 #endif
00021 
00022 #include <sstream>  // std::stringstream
00023 #include <map>
00024 #include <string>
00025 
00026 using namespace mvsim;
00027 
00028 TClassFactory_sensors mvsim::classFactory_sensors;
00029 
00030 // Explicit registration calls seem to be one (the unique?) way to assure
00031 // registration takes place:
00032 void register_all_sensors()
00033 {
00034         static bool done = false;
00035         if (done)
00036                 return;
00037         else
00038                 done = true;
00039 
00040         REGISTER_SENSOR("laser", LaserScanner)
00041 }
00042 
00043 SensorBase::SensorBase(VehicleBase& vehicle)
00044         : VisualObject(vehicle.getWorldObject()),
00045           m_sensor_period(0.1),
00046           m_vehicle(vehicle),
00047           m_sensor_last_timestamp(0)
00048 {
00049 }
00050 
00051 SensorBase::~SensorBase() {}
00052 SensorBase* SensorBase::factory(
00053         VehicleBase& parent, const rapidxml::xml_node<char>* root)
00054 {
00055         register_all_sensors();
00056 
00057         using namespace std;
00058         using namespace rapidxml;
00059 
00060         if (!root) throw runtime_error("[SensorBase::factory] XML node is NULL");
00061         if (0 != strcmp(root->name(), "sensor"))
00062                 throw runtime_error(
00063                         mrpt::format(
00064                                 "[SensorBase::factory] XML root element is '%s' ('sensor' "
00065                                 "expected)",
00066                                 root->name()));
00067 
00068         // Get "class" attrib:
00069         const xml_attribute<>* sensor_class = root->first_attribute("class");
00070         if (!sensor_class || !sensor_class->value())
00071                 throw runtime_error(
00072                         "[VehicleBase::factory] Missing mandatory attribute 'class' in "
00073                         "node <sensor>");
00074 
00075         const string sName = string(sensor_class->value());
00076 
00077         // Class factory:
00078         SensorBase* we = classFactory_sensors.create(sName, parent, root);
00079 
00080         if (!we)
00081                 throw runtime_error(
00082                         mrpt::format(
00083                                 "[SensorBase::factory] Unknown sensor type '%s'",
00084                                 root->name()));
00085 
00086         return we;
00087 }


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 22:08:35