mrpt_sensorlib.cpp
Go to the documentation of this file.
1 
3 
5 #if MRPT_VERSION >= 0x199
6 #include <mrpt/serialization/CArchive.h>
7 #include <mrpt/config/CConfigFile.h>
8 using mrpt::config::CConfigFile;
9 #else
10 #include <mrpt/utils/CConfigFile.h>
12 #endif
13 
14 using namespace mrpt::hwdrivers;
15 using namespace mrpt_sensors;
16 
17 GenericSensorNode::GenericSensorNode() {}
18 
19 GenericSensorNode::~GenericSensorNode() {}
20 
21 void GenericSensorNode::init(int argc, char** argv)
22 {
23  try
24  {
25  // Load parameters:
26  nhlocal_.getParam("config_file", cfgfilename_);
27  nhlocal_.getParam("config_section", cfg_section_);
28  CConfigFile iniFile(cfgfilename_);
29 
30  // Call sensor factory:
31  std::string driver_name =
32  iniFile.read_string(cfg_section_, "driver", "", true);
34  if (!sensor_)
35  {
37  "Sensor class name not recognized: " << driver_name);
38  return;
39  }
40 
41  // Load common & sensor specific parameters:
42  sensor_->loadConfig(iniFile, cfg_section_);
43 
44  // Initialize sensor:
45  sensor_->initialize();
46 
47  // Custom init:
48  this->init_sensor_specific();
49 
50  // Open rawlog file, if enabled:
51  if (!out_rawlog_prefix_.empty())
52  {
53  // Build full rawlog file name:
54 
55  std::string rawlog_postfix = "_";
56 
57  // rawlog_postfix += dateTimeToString( now() );
60  rawlog_postfix += mrpt::format(
61  "%04u-%02u-%02u_%02uh%02um%02us", (unsigned int)parts.year,
62  (unsigned int)parts.month, (unsigned int)parts.day,
63  (unsigned int)parts.hour, (unsigned int)parts.minute,
64  (unsigned int)parts.second);
65 
66  rawlog_postfix =
68 
69  const std::string fil = out_rawlog_prefix_ + rawlog_postfix;
70  // auto out_arch = archiveFrom(out_rawlog_);
71  ROS_INFO("Writing rawlog to file: `%s`", fil.c_str());
72 
73  out_rawlog_.open(fil, rawlog_GZ_compress_level_);
74 
75  if (!out_rawlog_.is_open())
76  ROS_ERROR("Error opening output rawlog for writing");
77  }
78  }
79  catch (std::exception& e)
80  {
82  "Exception in GenericSensorNode::init(): " << e.what());
83  return;
84  }
85 }
86 
88 void GenericSensorNode::run()
89 {
90  ROS_INFO("Starting run() at %.02f Hz", sensor_->getProcessRate());
91  if (!sensor_)
92  {
93  ROS_ERROR("Aborting: sensor object was not properly initialized.");
94  return;
95  }
96  ros::Rate loop_rate(sensor_->getProcessRate());
97  while (ros::ok())
98  {
99  sensor_->doProcess();
100 
101  // Get new observations
103  sensor_->getObservations(lstObjs);
104 
105  if (out_rawlog_.is_open())
106  {
107 #if MRPT_VERSION >= 0x199
108  auto out_arch = mrpt::serialization::archiveFrom(out_rawlog_);
109 #else
110  auto& out_arch = out_rawlog_;
111 #endif
112  for (const auto& o : lstObjs)
113  {
114  out_arch << *o.second;
115  }
116  }
117 
118  ros::spinOnce();
119  }
120 }
void BASE_IMPEXP timestampToParts(TTimeStamp t, TTimeParts &p, bool localTime=false)
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
mrpt::system::TTimeStamp now()
static CGenericSensorPtr createSensorPtr(const std::string &className)
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
#define ROS_INFO(...)
CConfigFile * iniFile
ROSCPP_DECL bool ok()
std::string BASE_IMPEXP fileNameStripInvalidChars(const std::string &filename, const char replacement_to_invalid_chars= '_')
#define ROS_ERROR_STREAM(args)
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
std::multimap< mrpt::system::TTimeStamp, mrpt::utils::CSerializablePtr > TListObservations


mrpt_sensorlib
Author(s): Jose-Luis Blanco-Claraco
autogenerated on Thu Jul 16 2020 03:15:20