MRPT  1.9.9
CObservationGasSensors.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "obs-precomp.h" // Precompiled headers
11 
15 #include <mrpt/system/os.h>
16 #include <fstream>
17 #include <iostream>
18 
19 using namespace mrpt::obs;
20 using namespace mrpt::poses;
21 using namespace mrpt::math;
22 using namespace std;
23 
24 // This must be added to any CSerializable class implementation file.
26 
27 /** Constructor
28  */
30 uint8_t CObservationGasSensors::serializeGetVersion() const { return 5; }
33 {
34  uint32_t i, n = m_readings.size();
35  out << n;
36 
37  for (i = 0; i < n; i++)
38  {
39  out << CPose3D(m_readings[i].eNosePoseOnTheRobot);
40  out << m_readings[i].readingsVoltage;
41  out << m_readings[i].sensorTypes;
42  out << m_readings[i].hasTemperature;
43  if (m_readings[i].hasTemperature) out << m_readings[i].temperature;
44  }
45 
46  out << sensorLabel << timestamp;
47 }
48 
50  mrpt::serialization::CArchive& in, uint8_t version)
51 {
52  switch (version)
53  {
54  case 2:
55  case 3:
56  case 4:
57  case 5:
58  {
59  // A general set of e-nose descriptors:
60  uint32_t i, n;
61 
62  in >> n;
63  m_readings.resize(n);
64 
65  CPose3D aux;
66 
67  for (i = 0; i < n; i++)
68  {
69  in >> aux;
70  m_readings[i].eNosePoseOnTheRobot = aux.asTPose();
71  in >> m_readings[i].readingsVoltage;
72  in >> m_readings[i].sensorTypes;
73  if (version >= 3)
74  {
75  in >> m_readings[i].hasTemperature;
76  if (m_readings[i].hasTemperature)
77  in >> m_readings[i].temperature;
78  }
79  else
80  {
81  m_readings[i].hasTemperature = false;
82  m_readings[i].temperature = 0;
83  }
84  }
85 
86  if (version >= 4)
87  in >> sensorLabel;
88  else
89  sensorLabel = "";
90 
91  if (version >= 5)
92  in >> timestamp;
93  else
94  timestamp = INVALID_TIMESTAMP;
95  }
96  break;
97  case 0:
98  case 1:
99  {
100  TObservationENose eNose;
101 
102  m_readings.clear();
103 
104  // There was a single set of 16 values from "Sancho" (DEC-2006)
105  CVectorFloat readings;
106  in >> readings;
107 
108  ASSERT_(readings.size() == 16);
109 
110  // There was TWO e-noses:
111  // (1)
112  eNose.eNosePoseOnTheRobot =
113  TPose3D(0.20, -0.15, 0.10, 0, 0, 0); // (x,y,z) only
114  eNose.readingsVoltage.resize(4);
115  eNose.readingsVoltage[0] = readings[2];
116  eNose.readingsVoltage[1] = readings[4];
117  eNose.readingsVoltage[2] = readings[5];
118  eNose.readingsVoltage[3] = readings[6];
119 
120  eNose.sensorTypes.clear();
121  eNose.sensorTypes.resize(4, 0);
122  m_readings.push_back(eNose);
123 
124  // (2)
125  eNose.eNosePoseOnTheRobot =
126  TPose3D(0.20, 0.15, 0.10, .0, .0, .0); // (x,y,z) only
127  eNose.readingsVoltage.resize(4);
128  eNose.readingsVoltage[0] = readings[8];
129  eNose.readingsVoltage[1] = readings[10];
130  eNose.readingsVoltage[2] = readings[12];
131  eNose.readingsVoltage[3] = readings[14];
132 
133  eNose.sensorTypes.clear();
134  eNose.sensorTypes.resize(4, 0);
135  m_readings.push_back(eNose);
136  }
137  break;
138  default:
140  };
141 }
142 
143 /*---------------------------------------------------------------
144  getSensorPose
145  ---------------------------------------------------------------*/
147 {
148  if (!m_readings.empty())
149  out_sensorPose = CPose3D(m_readings[0].eNosePoseOnTheRobot);
150  else
151  out_sensorPose = CPose3D(0, 0, 0);
152 }
153 
155 {
156  for (auto& r : m_readings) r.eNosePoseOnTheRobot = newSensorPose.asTPose();
157 }
158 
160  float& reading, mrpt::system::TTimeStamp& timestamp)
161 {
162  try
163  {
164  // Noise filtering
165  noise_filtering(reading, timestamp);
166 
167  // Decimate
168  if (decimate_count != decimate_value)
169  {
170  decimate_count++;
171  return false;
172  }
173 
174  // Gas concentration estimation based on FIRST ORDER + NONLINEAR
175  // COMPENSATIONS DYNAMICS
176  inverse_MOSmodeling(
177  m_antiNoise_window[winNoise_size / 2].reading_filtered,
178  m_antiNoise_window[winNoise_size / 2].timestamp);
179  decimate_count = 1;
180 
181  // update (output)
182  reading = last_Obs.estimation;
183  timestamp = last_Obs.timestamp;
184 
185  // Save data map in log file for Matlab visualization
186  if (save_maplog)
187  save_log_map(
188  last_Obs.timestamp, last_Obs.reading, last_Obs.estimation,
189  last_Obs.tau);
190 
191  return true;
192  }
193  catch (...)
194  {
195  cout << "Error when decimating \n";
197  return false;
198  }
199 }
200 
201 /*---------------------------------------------------------------
202  noise_filtering (smooth)
203  ---------------------------------------------------------------*/
205  const float& reading, const mrpt::system::TTimeStamp& timestamp)
206 {
207  try
208  {
209  // Store values in the temporal Observation
210  temporal_Obs.reading = reading;
211  temporal_Obs.timestamp = timestamp;
212 
213  // If first reading from E-nose
214  if (m_antiNoise_window.empty())
215  {
216  // use default values
217  temporal_Obs.reading_filtered = reading;
218 
219  // Populate the noise window
220  m_antiNoise_window.assign(winNoise_size, temporal_Obs);
221  }
222  else
223  {
224  // Erase the first element (the oldest), and add the new one
225  m_antiNoise_window.erase(m_antiNoise_window.begin());
226  m_antiNoise_window.push_back(temporal_Obs);
227  }
228 
229  // Average data to reduce noise (Noise Filtering)
230  float partial_sum = 0;
231  for (auto& i : m_antiNoise_window) partial_sum += i.reading;
232 
233  m_antiNoise_window.at(winNoise_size / 2).reading_filtered =
234  partial_sum / winNoise_size;
235  }
236  catch (...)
237  {
238  cout << "Error when filtering noise from readings \n";
240  }
241 }
242 
243 /*---------------------------------------------------------------
244  inverse_MOSmodeling
245  ---------------------------------------------------------------*/
247  const float& reading, const mrpt::system::TTimeStamp& timestamp)
248 {
249  try
250  {
251  // Keep the minimum reading value as an approximation to the basline
252  // level
253  if (reading < min_reading) min_reading = reading;
254 
255  // Check if estimation posible (not possible in the first iteration)
256  if (!first_iteration)
257  {
258  // Assure the samples are provided at constant rate (important for
259  // the correct gas distribution estimation)
260  double incT =
261  mrpt::system::timeDifference(last_Obs.timestamp, timestamp);
262 
263  if ((incT > 0) & (!first_incT))
264  { // not the same sample due to initialization of buffers
265  if (fixed_incT == 0)
266  fixed_incT = incT;
267  else
268  // ASSERT_(fabs(incT - fixed_incT) < (double)(0.05));
269  if (fabs(incT - fixed_incT) > (double)(0.05))
270  cout << "IncT is not constant by HW." << endl;
271  }
272  else
273  {
274  if (incT > 0) first_incT = false;
275  }
276 
277  // slope<0 -->Decay
278  if (reading < last_Obs.reading)
279  {
280  last_Obs.tau = a_decay * abs(reading - min_reading) + b_decay;
281  }
282  else // slope>=0 -->rise
283  {
284  last_Obs.tau = a_rise * abs(reading - min_reading) + b_rise;
285  } // end-if
286 
287  // New estimation values -- Ziegler-Nichols model --
288  if (incT > 0)
289  // Initially there may come repetetive values till
290  // m_antiNoise_window is full populated.
291  last_Obs.estimation =
292  ((reading - last_Obs.reading) * last_Obs.tau / incT) +
293  reading;
294  else
295  last_Obs.estimation = reading;
296 
297  // Prepare the New observation
298  last_Obs.timestamp = timestamp;
299  last_Obs.reading = reading;
300  }
301  else
302  {
303  // First filtered reading (use default values)
304  last_Obs.tau = b_rise;
305  last_Obs.reading = reading;
306  last_Obs.timestamp = timestamp;
307  last_Obs.estimation =
308  reading; // No estimation possible at this step
309  first_iteration = false;
310  } // end-if estimation values
311  }
312  catch (const exception& e)
313  {
314  cerr << "**Exception in "
315  "CObservationGasSensors::CMOSmodel::inverse_MOSmodeling** "
316  << e.what() << endl;
317  }
318 }
319 
320 /*---------------------------------------------------------------
321  save_log_map
322  ---------------------------------------------------------------*/
324  const mrpt::system::TTimeStamp& timestamp, const float& reading,
325  const float& estimation, const float& tau)
326 {
327  // function to save in a log file the information of the generated gas
328  // distribution estimation
329 
330  double time = mrpt::system::timestampTotime_t(timestamp);
331  char buffer[50];
332  sprintf(buffer, "./log_MOSmodel_GasDistribution.txt");
333 
334  if (!m_debug_dump) m_debug_dump = new ofstream(buffer);
335 
336  if (m_debug_dump->is_open())
337  {
338  *m_debug_dump << format("%f \t", time);
339  *m_debug_dump << format("%f \t", reading);
340  *m_debug_dump << format("%f \t", estimation);
341  *m_debug_dump << format("%f \t", tau);
342  *m_debug_dump << "\n";
343  }
344  else
345  cout << "Unable to open file";
346 }
347 
349 {
350  using namespace std;
352 
353  for (size_t j = 0; j < m_readings.size(); j++)
354  {
355  o << format("e-nose #%u:\n", (unsigned)j);
356 
357  vector<float>::const_iterator it;
358  std::vector<int>::const_iterator itKind;
359 
360  ASSERT_(
361  m_readings[j].readingsVoltage.size() ==
362  m_readings[j].sensorTypes.size());
363 
364  for (it = m_readings[j].readingsVoltage.begin(),
365  itKind = m_readings[j].sensorTypes.begin();
366  it != m_readings[j].readingsVoltage.end(); it++, itKind++)
367  o << format("%04X: %.03f ", *itKind, *it);
368 
369  o << endl;
370 
371  o << format(
372  " Sensor pose on robot: (x,y,z)=(%.02f,%.02f,%.02f)\n",
373  m_readings[j].eNosePoseOnTheRobot.x,
374  m_readings[j].eNosePoseOnTheRobot.y,
375  m_readings[j].eNosePoseOnTheRobot.z);
376 
377  o << "Measured temperature: ";
378  if (m_readings[j].hasTemperature)
379  o << format("%.03f degC\n", m_readings[j].temperature);
380  else
381  o << "NOT AVAILABLE\n";
382  }
383 }
mrpt::math::TPose3D asTPose() const
Definition: CPose3D.cpp:775
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Template for column vectors of dynamic size, compatible with Eigen.
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
void noise_filtering(const float &reading, const mrpt::system::TTimeStamp &timestamp)
Reduce noise by averaging with a mobile window of specific size (winNoise_size)
GLuint buffer
Definition: glext.h:3928
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
GLenum GLsizei n
Definition: glext.h:5136
STL namespace.
void getSensorPose(mrpt::poses::CPose3D &out_sensorPose) const override
A general method to retrieve the sensor pose on the robot.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
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...
double timestampTotime_t(const mrpt::system::TTimeStamp t) noexcept
Transform from TTimeStamp to standard "time_t" (actually a double number, it can contain fractions of...
Definition: datetime.h:105
This namespace contains representation of robot actions and observations.
Declares a class derived from "CObservation" that represents a set of readings from gas sensors...
void setSensorPose(const mrpt::poses::CPose3D &newSensorPose) override
A general method to change the sensor pose on the robot.
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
void pause(const std::string &msg=std::string("Press any key to continue...")) noexcept
Shows the message "Press any key to continue" (or other custom message) to the current standard outpu...
Definition: os.cpp:432
void save_log_map(const mrpt::system::TTimeStamp &timestamp, const float &reading, const float &estimation, const float &tau)
Save the gas distribution estiamtion into a log file for offline representation.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::vision::TStereoCalibResults out
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:43
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: TPose3D.h:24
GLuint in
Definition: glext.h:7391
void inverse_MOSmodeling(const float &reading, const mrpt::system::TTimeStamp &timestamp)
Estimates the gas concentration based on readings and sensor model.
double timeDifference(const mrpt::system::TTimeStamp t_first, const mrpt::system::TTimeStamp t_later)
Returns the time difference from t1 to t2 (positive if t2 is posterior to t1), in seconds...
Definition: datetime.h:123
std::vector< float > readingsVoltage
The set of readings (in volts) from the array of sensors (size of "sensorTypes" is the same that the ...
void getDescriptionAsText(std::ostream &o) const override
Build a detailed, multi-line textual description of the observation contents and dump it to the outpu...
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
Definition: datetime.h:43
int sprintf(char *buf, size_t bufSize, const char *format,...) noexcept MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
Definition: os.cpp:191
virtual void getDescriptionAsText(std::ostream &o) const
Build a detailed, multi-line textual description of the observation contents and dump it to the outpu...
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: 9b18308f3 Mon Nov 18 23:39:25 2019 +0100 at lun nov 18 23:45:12 CET 2019