Main MRPT website > C++ reference for MRPT 1.9.9
CRaePID.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-2017, 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 
10 #include "hwdrivers-precomp.h" // Precompiled headers
11 #include <mrpt/hwdrivers/CRaePID.h>
12 #include <mrpt/system/datetime.h>
13 
14 #include <iostream>
15 #include <iterator>
16 #include <sstream>
17 #include <thread>
18 
19 using namespace std;
20 using namespace mrpt::utils;
21 using namespace mrpt::hwdrivers;
22 
24 
25 /* -----------------------------------------------------
26  Constructor
27  ----------------------------------------------------- */
28 CRaePID::CRaePID() { m_sensorLabel = "RAE_PID"; }
29 /* -----------------------------------------------------
30  loadConfig_sensorSpecific
31  ----------------------------------------------------- */
32 void CRaePID::loadConfig_sensorSpecific(
33  const mrpt::utils::CConfigFileBase& configSource,
34  const std::string& iniSection)
35 {
36 #ifdef MRPT_OS_WINDOWS
37  com_port =
38  configSource.read_string(iniSection, "COM_port_PID", "COM1", true);
39 #else
40  com_port =
41  configSource.read_string(iniSection, "COM_port_PID", "/dev/tty0", true);
42 #endif
43 
44  com_bauds = configSource.read_int(iniSection, "baudRate", 9600, false);
45 
46  pose_x = configSource.read_float(iniSection, "pose_x", 0, true);
47  pose_y = configSource.read_float(iniSection, "pose_y", 0, true);
48  pose_z = configSource.read_float(iniSection, "pose_z", 0, true);
49  pose_roll = configSource.read_float(iniSection, "pose_roll", 0, true);
50  pose_pitch = configSource.read_float(iniSection, "pose_pitch", 0, true);
51  pose_yaw = configSource.read_float(iniSection, "pose_yaw", 0, true);
52 }
53 
54 /* -----------------------------------------------------
55  tryToOpenTheCOM
56 ----------------------------------------------------- */
57 bool CRaePID::tryToOpenTheCOM()
58 {
59  if (COM.isOpen()) return true; // Already open
60 
61  if (m_verbose)
62  cout << "[CRaePID] Opening " << com_port << " @ " << com_bauds << endl;
63 
64  try
65  {
66  COM.open(com_port);
67  // Config:
68  COM.setConfig(com_bauds, 0, 8, 1);
69  // COM.setTimeouts( 1, 0, 1, 1, 1 );
70  COM.setTimeouts(50, 1, 100, 1, 20);
71  // std::this_thread::sleep_for(10ms);
72  COM.purgeBuffers();
73  // std::this_thread::sleep_for(10ms);
74 
75  return true; // All OK!
76  }
77  catch (std::exception& e)
78  {
79  std::cerr << "[CRaePID::tryToOpenTheCOM] Error opening or configuring "
80  "the serial port:"
81  << std::endl
82  << e.what();
83  COM.close();
84  return false;
85  }
86  catch (...)
87  {
88  std::cerr << "[CRaePID::tryToOpenTheCOM] Error opening or configuring "
89  "the serial port."
90  << std::endl;
91  COM.close();
92  return false;
93  }
94 }
95 
96 /* -----------------------------------------------------
97  doProcess
98 ----------------------------------------------------- */
99 void CRaePID::doProcess()
100 {
101  // Is the COM open?
102  if (!tryToOpenTheCOM())
103  {
104  m_state = ssError;
105  THROW_EXCEPTION("Cannot open the serial port");
106  }
107 
108  bool have_reading = false;
109  std::string power_reading;
110  bool time_out = false;
111 
112  while (!have_reading)
113  {
114  // Send command to PID to request a measurement
115  COM.purgeBuffers();
116  COM.Write("R", 1);
117 
118  // Read PID response
119  power_reading = COM.ReadString(500, &time_out);
120  if (time_out)
121  {
122  // cout << "[CRaePID] " << com_port << " @ " <<com_bauds << " -
123  // measurement Timed-Out" << endl;
124  std::this_thread::sleep_for(10ms);
125  }
126  else
127  have_reading = true;
128  }
129 
130  // cout << "[CRaePID] " << com_port << " @ " <<com_bauds << " - measurement
131  // -> " << power_reading << endl;
132 
133  // Convert the text to a number (ppm)
134  const float readnum = atof(power_reading.c_str());
135  const float val_ppm = readnum / 1000;
136 
137  // Fill the observation
139  obs.readingsVoltage.push_back(val_ppm);
140  obs.sensorTypes.push_back(0x0001);
141 
143  obsG.sensorLabel = this->getSensorLabel();
144  obsG.m_readings.push_back(obs);
145  obsG.timestamp = mrpt::system::now();
146 
147  appendObservation(
150 }
151 
152 std::string CRaePID::getFirmware()
153 {
154  // Send the command
155  cout << "Firmware version: " << endl;
156  COM.purgeBuffers();
157  size_t B_written = COM.Write("F", 1);
158  if (!B_written) return std::string("COMMS.ERROR");
159 
160  // Read the returned text
161  bool time_out = false;
162  std::string s_read = COM.ReadString(2000, &time_out);
163  if (time_out) s_read = "Time_out";
164  return s_read;
165 }
166 
167 std::string CRaePID::getModel()
168 {
169  // Send the command
170  COM.purgeBuffers();
171  COM.Write("M", 1);
172 
173  // Read the returned text
174  return COM.ReadString();
175 }
176 
177 std::string CRaePID::getSerialNumber()
178 {
179  // Send the command
180  COM.purgeBuffers();
181  COM.Write("S", 1);
182 
183  // Read the returned text
184  return COM.ReadString();
185 }
186 
187 std::string CRaePID::getName()
188 {
189  // Send the command
190  COM.purgeBuffers();
191  COM.Write("N", 1);
192 
193  // Read the returned text
194  return COM.ReadString();
195 }
196 
197 bool CRaePID::switchPower()
198 {
199  // Send the command
200  COM.purgeBuffers();
201  COM.Write("P", 1);
202 
203  // Read the returned text
204  std::string reading;
205  reading = COM.ReadString();
206 
207  if (strcmp(reading.c_str(), "Sleep...") == 0)
208  return true;
209  else
210  return false;
211 }
212 
213 mrpt::obs::CObservationGasSensors CRaePID::getFullInfo()
214 {
215  // Send the command
216  COM.purgeBuffers();
217  COM.Write("C", 1);
218 
219  // Read the returned text
220  std::string reading;
221  reading = COM.ReadString();
222 
223  // Iterate over each information component (tokenize first)
224  // construct a stream from the string (as seen in
225  // http://stackoverflow.com/a/53921)
226  std::stringstream readings_str(reading);
227 
228  // use stream iterators to copy the stream to the vector as whitespace
229  // separated strings
230  std::istream_iterator<std::string> it(readings_str);
231  std::istream_iterator<std::string> endit;
232  std::vector<std::string> measurements_text(it, endit);
233 
234  // Convert the text to a number (ppm)
237 
238  for (size_t k = 0; k < measurements_text.size(); k++)
239  {
240  const float readnum = atof(measurements_text[k].c_str());
241  const float val_ppm = readnum / 1000.f;
242 
243  // Fill the observation
244  obs.readingsVoltage.push_back(val_ppm);
245  obsG.m_readings.push_back(obs);
246  }
247 
248  obsG.sensorLabel = this->getSensorLabel();
249  obsG.timestamp = mrpt::system::now();
250 
251  return obsG;
252 }
253 
254 bool CRaePID::errorStatus(std::string& errorString)
255 {
256  // Send the command
257  COM.purgeBuffers();
258  COM.Write("E", 1);
259 
260  // Read the returned text
261  std::string reading;
262  reading = COM.ReadString();
263 
264  // Tokenize to separate the two components:
265  // construct a stream from the string (as seen in
266  // http://stackoverflow.com/a/53921)
267  std::stringstream readings_str(reading);
268 
269  // use stream iterators to copy the stream to the vector as whitespace
270  // separated strings
271  std::istream_iterator<std::string> it(readings_str);
272  std::istream_iterator<std::string> endit;
273  std::vector<std::string> errors_text(it, endit);
274 
275  // Take the first part and check the possible error condition
276  if ((strcmp(errors_text[0].c_str(), "0") == 0) &&
277  (strcmp(errors_text[1].c_str(), "0") == 0)) // no error
278  {
279  return false;
280  }
281  else
282  {
283  // By the moment, return the raw error; note that if necessary a
284  // detailed description of the error can be obtained analyzing the two
285  // error strings separately
286  errorString = reading;
287  return true;
288  }
289 }
290 
291 void CRaePID::getLimits(float& min, float& max)
292 {
293  // Send the command
294  COM.purgeBuffers();
295  COM.Write("L", 1);
296 
297  // Read the returned text
298  std::string reading;
299  reading = COM.ReadString();
300 
301  // Tokenize to separate the two components:
302  // construct a stream from the string (as seen in
303  // http://stackoverflow.com/a/53921)
304  std::stringstream readings_str(reading);
305 
306  // use stream iterators to copy the stream to the vector as whitespace
307  // separated strings
308  std::istream_iterator<std::string> it(readings_str);
309  std::istream_iterator<std::string> endit;
310  std::vector<std::string> readings_text(it, endit);
311 
312  // read min and max
313  max = atof(readings_text[0].c_str());
314  min = atof(readings_text[1].c_str());
315 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
#define min(a, b)
float read_float(const std::string &section, const std::string &name, float defaultValue, bool failIfNotFound=false) const
#define THROW_EXCEPTION(msg)
This class implements a driver for the RAE Systems gas PhotoIonization Detector (PID) (Tested on a Mi...
Definition: CRaePID.h:31
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:76
Contains classes for various device interfaces.
vector_int sensorTypes
The kind of sensors in the array (size of "sensorTypes" is the same that the size of "readingsVoltage...
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
STL namespace.
This class allows loading and storing values and vectors of different types from a configuration text...
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
std::shared_ptr< CObservationGasSensors > Ptr
Declares a class derived from "CObservation" that represents a set of readings from gas sensors...
GLsizei const GLchar ** string
Definition: glext.h:4101
#define IMPLEMENTS_GENERIC_SENSOR(class_name, NameSpace)
This must be inserted in all CGenericSensor classes implementation files:
std::string sensorLabel
An arbitrary label that can be used to identify the sensor.
Definition: CObservation.h:60
mrpt::system::TTimeStamp timestamp
The associated UTC time-stamp.
Definition: CObservation.h:58
std::vector< TObservationENose > m_readings
One entry per e-nose on the robot.
std::vector< float > readingsVoltage
The set of readings (in volts) from the array of sensors (size of "sensorTypes" is the same that the ...



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019