MRPT  1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
15 #include <mrpt/system/filesystem.h>
17 #include <iostream>
18 
19 using namespace mrpt;
20 using namespace mrpt::system;
21 using namespace mrpt::obs;
22 using namespace std;
23 using namespace mrpt::config;
24 using namespace mrpt::io;
25 using namespace mrpt::math;
26 using namespace mrpt::serialization;
27 
28 int main(int argc, char** argv)
29 {
30  // Variables
31  string rawlog_file, sensorLabel;
32  int enoseID, sensorType, indexMonitoredSensor,
33  delay_value; // decimate_value, winNoise_size
35  bool have_estimation, apply_delay;
36 
37  // Load configuration:
38  if (mrpt::system::fileExists("./CONFIG_MOXmodel.ini"))
39  {
40  cout << "Using configuration from './CONFIG_MOXmodel.ini'" << endl;
41  CConfigFile conf("./CONFIG_MOXmodel.ini");
42 
43  rawlog_file = conf.read_string("", "rawlog_file", "", true);
44  sensorLabel = conf.read_string("", "sensorLabel", "Full_MCEnose", true);
45  enoseID = conf.read_int("", "enoseID", 0, true);
46  std::string sensorType_str =
47  conf.read_string("", "sensorType", "-1", true);
48  stringstream convert(sensorType_str);
49  convert >> std::hex >> sensorType;
50 
51  // Delays
52  apply_delay = conf.read_bool("", "apply_delay", false, true);
53  delay_value = conf.read_int("", "delay_value", 0, true);
54 
55  // MOX model parameters
56  MOSmodel.a_rise = conf.read_float("", "a_rise", 0.0, true);
57  MOSmodel.b_rise = conf.read_float("", "b_rise", 0.0, true);
58  MOSmodel.a_decay = conf.read_float("", "a_decay", 0.0, true);
59  MOSmodel.b_decay = conf.read_float("", "b_decay", 0.0, true);
60  MOSmodel.winNoise_size = conf.read_int("", "winNoise_size", 0, true);
61  MOSmodel.decimate_value = conf.read_int("", "decimate_value", 0, true);
62  // save_maplog = conf.read_bool("","save_maplog",false,true);
63 
64  indexMonitoredSensor = -1;
65  }
66  else
67  {
68  cout << "Configuration file (ini) cannot be found\n" << endl;
69  // If you are in VisualStudio, assure that the working directory in the
70  // project properties is correctly set
71  return -1;
72  }
73 
74  // Open Rawlogs
75  cout << "Processing Rawlog " << rawlog_file << endl;
76  cout << "Obtaining MOXmodel from " << sensorLabel << "(" << enoseID
77  << ") - sensor " << sensorType << endl;
78  CFileGZInputStream file_input;
79  CFileGZOutputStream file_output;
80 
81  file_input.open(rawlog_file);
82  file_output.open("MOX_model_output.rawlog");
83 
84  if (!file_input.fileOpenCorrectly() || !file_output.fileOpenCorrectly())
85  cout << "Error opening rawlog file" << endl;
86 
87  // Process rawlog
88  bool read = true;
89  while (read)
90  {
91  try
92  {
94  archiveFrom(file_input) >> o;
95 
96  if (o) // ASSERT_(o);
97  {
99  {
101  std::dynamic_pointer_cast<CObservationGasSensors>(o);
102 
103  // Correct delay on gas readings
104  if (apply_delay)
105  obs->timestamp = obs->timestamp -
106  std::chrono::milliseconds(delay_value);
107 
108  if (obs->sensorLabel == sensorLabel)
109  {
110  //------------------------------------------------------
111  // Get reading from CH_i for gas distribution estimation
112  //------------------------------------------------------
113  float raw_reading;
114 
115  if (sensorType == 0)
116  { // compute the mean
117  raw_reading = math::mean(
118  obs->m_readings[enoseID].readingsVoltage);
119  }
120  else
121  {
122  // Get the reading of the specified sensorID
123  if (indexMonitoredSensor == -1)
124  {
125  // First reading, get the index according to
126  // sensorID
127  for (indexMonitoredSensor = 0;
128  indexMonitoredSensor <
129  (int)obs->m_readings[enoseID]
130  .sensorTypes.size();
131  indexMonitoredSensor++)
132  {
133  if (obs->m_readings[enoseID].sensorTypes.at(
134  indexMonitoredSensor) ==
135  std::vector<int>::value_type(
136  sensorType))
137  break;
138  }
139  }
140 
141  if (indexMonitoredSensor <
142  (int)obs->m_readings[enoseID]
143  .sensorTypes.size())
144  {
145  raw_reading =
146  obs->m_readings[enoseID].readingsVoltage.at(
147  indexMonitoredSensor);
148  }
149  else // Sensor especified not found, compute
150  // default mean value
151  {
152  cout << "sensorType not found. Computing the "
153  "mean value"
154  << endl;
155  raw_reading = math::mean(
156  obs->m_readings[enoseID].readingsVoltage);
157  }
158  }
159 
160  // Obtain MOX model output
161  TPose3D MOXmodel_pose =
162  obs->m_readings[enoseID].eNosePoseOnTheRobot;
163  float MOXmodel_estimation = raw_reading;
164  mrpt::system::TTimeStamp MOXmodel_timestamp =
165  obs->timestamp;
166 
167  have_estimation =
169  MOXmodel_estimation, MOXmodel_timestamp);
170 
171  if (have_estimation)
172  {
173  // Save as new obs
175  gd_est;
176  gd_est.hasTemperature = false;
177  gd_est.temperature = 0.0;
178  gd_est.isActive = false;
179  gd_est.sensorTypes.push_back(
180  0x0001); // indicates that is a MOXmodel output
181  gd_est.readingsVoltage.push_back(
182  MOXmodel_estimation);
183  gd_est.eNosePoseOnTheRobot = MOXmodel_pose;
184 
188  obs_GDM->sensorLabel = "GDM";
189  // modify timestamp to deal with the delay of the
190  // model
191  obs_GDM->timestamp = MOXmodel_timestamp;
192  obs_GDM->m_readings.push_back(gd_est);
193 
194  archiveFrom(file_output) << obs_GDM;
195  }
196  }
197  }
198 
199  // Save current sensor obs to the new Rawlog file
200  auto arch = archiveFrom(file_output);
201  arch << o;
202  }
203  }
204  catch (exception& e)
205  {
206  cout << "Exception: " << e.what() << endl;
207  file_input.close();
208  file_output.close();
209  read = false;
210  }
211  }
212 
213  return 0;
214 }
float b_rise
tau = a*AMPLITUDE +b (linear relationship)
float temperature
Sensed temperature in Celcius (valid if hasTemperature=true only)
This class allows loading and storing values and vectors of different types from ".ini" files easily.
This file implements several operations that operate element-wise on individual or pairs of container...
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:127
STL namespace.
size_t winNoise_size
The size of the mobile average window used to reduce noise on sensor reagings.
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT&#39;s CStream, std::istream, std::ostream, std::stringstream
Definition: CArchive.h:555
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:40
This base provides a set of functions for maths stuff.
math::TPose3D eNosePoseOnTheRobot
The pose of the sensors on the robot.
std::vector< int > sensorTypes
The kind of sensors in the array (size of "sensorTypes" is the same that the size of "readingsVoltage...
bool hasTemperature
Must be true for "temperature" to contain a valid measurement.
This namespace contains representation of robot actions and observations.
Declares a class within "CObservationGasSensors" that represents a set of gas concentration readings ...
Declares a class derived from "CObservation" that represents a set of readings from gas sensors...
GLsizei const GLchar ** string
Definition: glext.h:4101
float a_decay
tau = a*AMPLITUDE +b (linear relationship)
bool open(const std::string &fileName)
Opens the file for read.
bool isActive
True if the input to this chamber/enose is poluted air, False if clean air.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::rtti::CObject) is of the give...
Definition: CObject.h:102
int decimate_value
[useMOSmodel] The decimate frecuency applied after noise filtering
void close()
Closes the file.
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
double mean(const CONTAINER &v)
Computes the mean value of a vector.
Transparently opens a compressed "gz" file and reads uncompressed data from it.
std::shared_ptr< T > make_aligned_shared(Args &&... args)
Creates a shared_ptr with aligned memory via aligned_allocator_cpp11<>.
bool open(const std::string &fileName, int compress_level=1)
Open a file for write, choosing the compression level.
std::vector< float > readingsVoltage
The set of readings (in volts) from the array of sensors (size of "sensorTypes" is the same that the ...
Saves data to a file and transparently compress the data using the given compression level...
float b_decay
tau = a*AMPLITUDE +b (linear relationship)
float a_rise
tau = a*AMPLITUDE +b (linear relationship)
bool get_GasDistribution_estimation(float &reading, mrpt::system::TTimeStamp &timestamp)
Obtain an estimation of the gas distribution based on raw sensor readings.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020