Main MRPT website > C++ reference for MRPT 1.5.7
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 
11 
12 #include "hwdrivers-precomp.h" // Precompiled headers
13 #include <mrpt/hwdrivers/CRaePID.h>
14 #include <mrpt/system/threads.h>
15 #include <mrpt/system/datetime.h>
16 
17 #include <iostream>
18 #include <iterator>
19 #include <sstream>
20 
21 using namespace std;
22 using namespace mrpt::utils;
23 using namespace mrpt::hwdrivers;
24 
25 
27 
28 /* -----------------------------------------------------
29  Constructor
30  ----------------------------------------------------- */
32 {
33  m_sensorLabel = "RAE_PID";
34 }
35 
36 
37 /* -----------------------------------------------------
38  loadConfig_sensorSpecific
39  ----------------------------------------------------- */
40 void CRaePID::loadConfig_sensorSpecific(
41  const mrpt::utils::CConfigFileBase &configSource,
42  const std::string &iniSection )
43 {
44 #ifdef MRPT_OS_WINDOWS
45  com_port = configSource.read_string(iniSection, "COM_port_PID", "COM1", true ) ;
46 #else
47  com_port = configSource.read_string(iniSection, "COM_port_PID", "/dev/tty0", true );
48 #endif
49 
50  com_bauds = configSource.read_int( iniSection, "baudRate",9600, false );
51 
52  pose_x = configSource.read_float(iniSection,"pose_x",0,true);
53  pose_y = configSource.read_float(iniSection,"pose_y",0,true);
54  pose_z = configSource.read_float(iniSection,"pose_z",0,true);
55  pose_roll = configSource.read_float(iniSection,"pose_roll",0,true);
56  pose_pitch = configSource.read_float(iniSection,"pose_pitch",0,true);
57  pose_yaw = configSource.read_float(iniSection,"pose_yaw",0,true);
58 
59 }
60 
61 /* -----------------------------------------------------
62  tryToOpenTheCOM
63 ----------------------------------------------------- */
64 bool CRaePID::tryToOpenTheCOM()
65 {
66  if (COM.isOpen())
67  return true; // Already open
68 
69  if (m_verbose) cout << "[CRaePID] Opening " << com_port << " @ " <<com_bauds << endl;
70 
71  try
72  {
73  COM.open(com_port);
74  // Config:
75  COM.setConfig( com_bauds, 0, 8, 1 );
76  //COM.setTimeouts( 1, 0, 1, 1, 1 );
77  COM.setTimeouts(50,1,100, 1,20);
78  //mrpt::system::sleep(10);
79  COM.purgeBuffers();
80  //mrpt::system::sleep(10);
81 
82  return true; // All OK!
83  }
84  catch (std::exception &e)
85  {
86  std::cerr << "[CRaePID::tryToOpenTheCOM] Error opening or configuring the serial port:" << std::endl << e.what();
87  COM.close();
88  return false;
89  }
90  catch (...)
91  {
92  std::cerr << "[CRaePID::tryToOpenTheCOM] Error opening or configuring the serial port." << std::endl;
93  COM.close();
94  return false;
95  }
96 }
97 
98 /* -----------------------------------------------------
99  doProcess
100 ----------------------------------------------------- */
101 void CRaePID::doProcess()
102 {
103  // Is the COM open?
104  if (!tryToOpenTheCOM())
105  {
106  m_state = ssError;
107  THROW_EXCEPTION("Cannot open the serial port");
108  }
109 
110  bool have_reading = false;
111  std::string power_reading;
112  bool time_out = false;
113 
114  while (!have_reading)
115  {
116  // Send command to PID to request a measurement
117  COM.purgeBuffers();
118  COM.Write("R",1);
119 
120  // Read PID response
121  power_reading = COM.ReadString(500,&time_out);
122  if (time_out)
123  {
124  //cout << "[CRaePID] " << com_port << " @ " <<com_bauds << " - measurement Timed-Out" << endl;
126  }
127  else
128  have_reading = true;
129  }
130 
131  //cout << "[CRaePID] " << com_port << " @ " <<com_bauds << " - measurement -> " << 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(mrpt::obs::CObservationGasSensorsPtr(new mrpt::obs::CObservationGasSensors(obsG)));
148 
149 }
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)
164  s_read = "Time_out";
165  return s_read;
166 }
167 
168 std::string CRaePID::getModel()
169 {
170  // Send the command
171  COM.purgeBuffers();
172  COM.Write("M",1);
173 
174  // Read the returned text
175  return COM.ReadString();
176 }
177 
178 
179 std::string CRaePID::getSerialNumber()
180 {
181  // Send the command
182  COM.purgeBuffers();
183  COM.Write("S",1);
184 
185  // Read the returned text
186  return COM.ReadString();
187 }
188 
189 std::string CRaePID::getName()
190 {
191  // Send the command
192  COM.purgeBuffers();
193  COM.Write("N",1);
194 
195  // Read the returned text
196  return COM.ReadString();
197 
198 }
199 
200 bool CRaePID::switchPower()
201 {
202  // Send the command
203  COM.purgeBuffers();
204  COM.Write("P",1);
205 
206  // Read the returned text
207  std::string reading;
208  reading = COM.ReadString();
209 
210  if (strcmp(reading.c_str(),"Sleep...")==0)
211  return true;
212  else
213  return false;
214 }
215 
216 mrpt::obs::CObservationGasSensors CRaePID::getFullInfo()
217 {
218  // Send the command
219  COM.purgeBuffers();
220  COM.Write("C",1);
221 
222  // Read the returned text
223  std::string reading;
224  reading = COM.ReadString();
225 
226  // Iterate over each information component (tokenize first)
227  // construct a stream from the string (as seen in http://stackoverflow.com/a/53921)
228  std::stringstream readings_str(reading);
229 
230  // use stream iterators to copy the stream to the vector as whitespace separated strings
231  std::istream_iterator<std::string> it(readings_str);
232  std::istream_iterator<std::string> endit;
233  std::vector<std::string> measurements_text(it, endit);
234 
235  // Convert the text to a number (ppm)
238 
239  for (size_t k=0; k < measurements_text.size(); k++)
240  {
241  const float readnum = atof(measurements_text[k].c_str());
242  const float val_ppm = readnum/1000.f;
243 
244  // Fill the observation
245  obs.readingsVoltage.push_back(val_ppm);
246  obsG.m_readings.push_back(obs);
247  }
248 
249  obsG.sensorLabel = this->getSensorLabel();
250  obsG.timestamp = mrpt::system::now();
251 
252  return obsG;
253 
254 }
255 
256 bool CRaePID::errorStatus(std::string &errorString)
257 {
258  // Send the command
259  COM.purgeBuffers();
260  COM.Write("E",1);
261 
262  // Read the returned text
263  std::string reading;
264  reading = COM.ReadString();
265 
266  // Tokenize to separate the two components:
267  // construct a stream from the string (as seen in http://stackoverflow.com/a/53921)
268  std::stringstream readings_str(reading);
269 
270  // use stream iterators to copy the stream to the vector as whitespace 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) && (strcmp(errors_text[1].c_str(),"0")==0)) // no error
277  {
278  return false;
279  }
280  else
281  {
282  // By the moment, return the raw error; note that if necessary a detailed description of the error can be obtained analyzing the two error strings separately
283  errorString = reading;
284  return true;
285  }
286 }
287 
288 void CRaePID::getLimits(float &min, float &max)
289 {
290  // Send the command
291  COM.purgeBuffers();
292  COM.Write("L",1);
293 
294  // Read the returned text
295  std::string reading;
296  reading = COM.ReadString();
297 
298  // Tokenize to separate the two components:
299  // construct a stream from the string (as seen in http://stackoverflow.com/a/53921)
300  std::stringstream readings_str(reading);
301 
302  // use stream iterators to copy the stream to the vector as whitespace separated strings
303  std::istream_iterator<std::string> it(readings_str);
304  std::istream_iterator<std::string> endit;
305  std::vector<std::string> readings_text(it, endit);
306 
307  // read min and max
308  max = atof(readings_text[0].c_str());
309  min = atof(readings_text[1].c_str());
310 
311 
312 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
#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:29
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:70
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
void BASE_IMPEXP sleep(int time_ms) MRPT_NO_THROWS
An OS-independent method for sending the current thread to "sleep" for a given period of time...
Definition: threads.cpp:57
Declares a class derived from "CObservation" that represents a set of readings from gas sensors...
GLsizei const GLchar ** string
Definition: glext.h:3919
#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.
mrpt::system::TTimeStamp timestamp
The associated UTC time-stamp. Where available, this should contain the accurate satellite-based time...
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.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019