Main MRPT website > C++ reference for MRPT 1.9.9
CBoardSonars.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 
12 #include <mrpt/utils/utils_defs.h>
13 #include <mrpt/utils/CMessage.h>
14 #include <mrpt/system/os.h>
15 
17 
18 #include <thread>
19 
20 using namespace mrpt::utils;
21 using namespace mrpt::hwdrivers;
22 using namespace std;
23 
25 
26 /*-------------------------------------------------------------
27  CBoardSonars
28 -------------------------------------------------------------*/
30 {
32  m_usbSerialNumber = "SONAR001";
33 
34  m_sensorLabel = "SONAR1";
35 
36  m_gain = 6;
37  m_maxRange = 4.0f;
38 
39  MRPT_END
40 }
41 
42 /*-------------------------------------------------------------
43  loadConfig_sensorSpecific
44 -------------------------------------------------------------*/
45 void CBoardSonars::loadConfig_sensorSpecific(
46  const mrpt::utils::CConfigFileBase& configSource,
47  const std::string& iniSection)
48 {
50 
51  std::vector<double> aux; // Auxiliar vector
52 
53  // Some parameters ...
54  m_usbSerialNumber = configSource.read_string(
55  iniSection, "USB_serialNumber", m_usbSerialNumber, true);
56  m_gain = configSource.read_int(iniSection, "gain", m_gain, true);
57  m_maxRange =
58  configSource.read_float(iniSection, "maxRange", m_maxRange, true);
59  m_minTimeBetweenPings = configSource.read_float(
60  iniSection, "minTimeBetweenPings", m_minTimeBetweenPings, true);
61  // ----------------------------------------------------------------------------------------------------------------------
62  ASSERT_(m_maxRange > 0 && m_maxRange <= 11);
63  ASSERT_(m_gain <= 16);
64 
65  // Sonar firing order ...
66  configSource.read_vector(
67  iniSection, "firingOrder", m_firingOrder, m_firingOrder, true);
68 
69  // ----------------------------------------------------------------------------------------------------------------------
70 
71  // Individual sonar gains ...
72  configSource.read_vector(iniSection, "sonarGains", aux, aux, true);
73 
76  for (itSonar = m_firingOrder.begin(), itAux = aux.begin();
77  itSonar != m_firingOrder.end(); ++itSonar, ++itAux)
78  m_sonarGains[*itSonar] = *itAux;
79  // ----------------------------------------------------------------------------------------------------------------------
80  ASSERT_(aux.size() == m_firingOrder.size());
81 
82  // Individual sonar poses
83  aux.clear();
84  for (itSonar = m_firingOrder.begin(); itSonar != m_firingOrder.end();
85  ++itSonar)
86  {
87  configSource.read_vector(
88  iniSection, format("pose%i", *itSonar), aux, aux,
89  true); // Get the sonar poses
90  m_sonarPoses[*itSonar] = mrpt::math::TPose3D(
91  aux[0], aux[1], aux[2], DEG2RAD((float)aux[3]),
92  DEG2RAD((float)aux[4]), DEG2RAD((float)aux[5]));
93  }
94  // ----------------------------------------------------------------------------------------------------------------------
95  ASSERT_(m_sonarGains.size() == m_firingOrder.size());
96 
97  MRPT_END
98 }
99 
100 /*-------------------------------------------------------------
101  queryFirmwareVersion
102 -------------------------------------------------------------*/
103 bool CBoardSonars::queryFirmwareVersion(string& out_firmwareVersion)
104 {
105  try
106  {
107  utils::CMessage msg, msgRx;
108 
109  // Try to connect to the device:
110  if (!checkConnectionAndConnect()) return false;
111 
112  msg.type = 0x10;
113  sendMessage(msg);
114 
115  if (receiveMessage(msgRx))
116  {
117  msgRx.getContentAsString(out_firmwareVersion);
118  return true;
119  }
120  else
121  return false;
122  }
123  catch (...)
124  {
125  Close();
126  return false;
127  }
128 }
129 
130 /*-------------------------------------------------------------
131  checkConnectionAndConnect
132 -------------------------------------------------------------*/
133 bool CBoardSonars::sendConfigCommands()
134 {
135  try
136  {
137  if (!isOpen()) return false;
138  utils::CMessage msg, msgRx;
139  size_t i;
140 
141  // Send cmd for firing order:
142  // ----------------------------
143  msg.type = 0x12;
144  msg.content.resize(16);
145  for (i = 0; i < 16; i++)
146  {
147  if (i < m_firingOrder.size())
148  msg.content[i] = m_firingOrder[i];
149  else
150  msg.content[i] = 0xFF;
151  }
152  sendMessage(msg);
153  if (!receiveMessage(msgRx)) return false; // Error
154 
155  // Send cmd for gain:
156  // ----------------------------
157  // msg.type = 0x13;
158  // msg.content.resize(1);
159  // msg.content[0] = m_gain;
160  // sendMessage(msg);
161  // if (!receiveMessage(msgRx) ) return false; // Error
162 
163  // Send cmd for set of gains:
164  // ----------------------------
165  msg.type = 0x16;
166  msg.content.resize(16);
167  for (i = 0; i < 16; i++)
168  {
169  if (m_sonarGains.find(i) != m_sonarGains.end())
170  msg.content[i] = m_sonarGains[i];
171  else
172  msg.content[i] = 0xFF;
173  }
174  sendMessage(msg);
175  if (!receiveMessage(msgRx)) return false; // Error
176 
177  // Send cmd for max range:
178  // ----------------------------
179  msg.type = 0x14;
180  msg.content.resize(1);
181  msg.content[0] = (int)((m_maxRange / 0.043f) - 1);
182  sendMessage(msg);
183  if (!receiveMessage(msgRx)) return false; // Error
184 
185  // Send cmd for max range:
186  // ----------------------------
187  msg.type = 0x15;
188  msg.content.resize(2);
189  uint16_t T = (uint16_t)(m_minTimeBetweenPings * 1000.0f);
190  msg.content[0] = T >> 8;
191  msg.content[1] = T & 0x00FF;
192  sendMessage(msg);
193  if (!receiveMessage(msgRx)) return false; // Error
194 
195  return true;
196  }
197  catch (...)
198  {
199  // Error opening device:
200  Close();
201  return false;
202  }
203 }
204 
205 /*-------------------------------------------------------------
206  getObservation
207 -------------------------------------------------------------*/
209 {
210  try
211  {
212  obs.sensorLabel = m_sensorLabel;
214  obs.minSensorDistance = 0.04f;
215  obs.maxSensorDistance = m_maxRange;
216  obs.sensorConeApperture = DEG2RAD(30.0f);
217  obs.sensedData.clear();
219 
220  utils::CMessage msg, msgRx;
221 
222  // Try to connect to the device:
223  if (!checkConnectionAndConnect()) return false;
224 
225  msg.type = 0x11;
226  sendMessage(msg);
227 
228  if (receiveMessage(msgRx))
229  {
230  if (msgRx.content.empty()) return false;
231 
232  // For each sensor:
233  ASSERT_((msgRx.content.size() % 2) == 0);
234  vector<uint16_t> data(msgRx.content.size() / 2);
235  memcpy(&data[0], &msgRx.content[0], msgRx.content.size());
236 
237  for (size_t i = 0; i < data.size() / 2; i++)
238  {
239  uint16_t sonar_idx = data[2 * i + 0];
240  uint16_t sonar_range_cm = data[2 * i + 1];
241  if (sonar_range_cm != 0xFFFF && sonar_idx < 16)
242  {
243  obsRange.sensorID = sonar_idx;
244  obsRange.sensorPose =
245  m_sonarPoses[sonar_idx]; // mrpt::poses::CPose3D(); //
246  // sonar_idx
247  obsRange.sensedDistance = sonar_range_cm * 0.01f;
248  obs.sensedData.push_back(obsRange);
249  }
250  }
251  return true;
252  }
253  else
254  return false;
255  }
256  catch (...)
257  {
258  Close();
259  return false;
260  }
261 }
262 
263 /*-------------------------------------------------------------
264  programI2CAddress
265 -------------------------------------------------------------*/
266 bool CBoardSonars::programI2CAddress(uint8_t currentAddress, uint8_t newAddress)
267 {
268  try
269  {
270  utils::CMessage msg, msgRx;
271 
272  // Try to connect to the device:
273  if (!checkConnectionAndConnect()) return false;
274 
275  msg.type = 0x20;
276  msg.content.resize(2);
277  msg.content[0] = currentAddress;
278  msg.content[1] = newAddress;
279  sendMessage(msg);
280 
281  std::this_thread::sleep_for(10ms);
282 
283  return receiveMessage(msgRx);
284  }
285  catch (...)
286  {
287  Close();
288  return false;
289  }
290 }
291 
292 /*-------------------------------------------------------------
293  checkConnectionAndConnect
294 -------------------------------------------------------------*/
295 bool CBoardSonars::checkConnectionAndConnect()
296 {
297  if (isOpen()) return true;
298 
299  try
300  {
301  OpenBySerialNumber(m_usbSerialNumber);
302  std::this_thread::sleep_for(10ms);
303  Purge();
304  std::this_thread::sleep_for(10ms);
305  SetLatencyTimer(1);
306  SetTimeouts(300, 100);
307 
308  return sendConfigCommands();
309  }
310  catch (...)
311  {
312  // Error opening device:
313  Close();
314  return false;
315  }
316 }
317 
318 /*-------------------------------------------------------------
319  doProcess
320 -------------------------------------------------------------*/
321 void CBoardSonars::doProcess()
322 {
324  mrpt::make_aligned_shared<mrpt::obs::CObservationRange>();
325  if (getObservation(*obs)) appendObservation(obs);
326 }
#define IMPLEMENTS_GENERIC_SENSOR(class_name, NameSpace)
This must be inserted in all CGenericSensor classes implementation files:
This "software driver" implements the communication protocol for interfacing a Ultrasonic range finde...
Definition: CBoardSonars.h:57
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
Declares a class derived from "CObservation" that encapsules a single range measurement,...
std::shared_ptr< CObservationRange > Ptr
float sensorConeApperture
Cone aperture of each ultrasonic beam, in radians.
TMeasurementList sensedData
All the measurements.
float minSensorDistance
The data members.
This class allows loading and storing values and vectors of different types from a configuration text...
void read_vector(const std::string &section, const std::string &name, const VECTOR_TYPE &defaultValue, VECTOR_TYPE &outValues, bool failIfNotFound=false) const
Reads a configuration parameter of type vector, stored in the file as a string: "[v1 v2 v3 ....
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
float read_float(const std::string &section, const std::string &name, float defaultValue, bool failIfNotFound=false) const
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
Definition: CMessage.h:32
void getContentAsString(std::string &str)
Gets the contents of the message as a string.
std::vector< uint8_t > content
The contents of the message (memory is automatically handled by the std::vector object)
Definition: CMessage.h:39
uint32_t type
An identifier of the message type (only the least-sig byte is typically sent)
Definition: CMessage.h:36
Scalar * iterator
Definition: eigen_plugins.h:26
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3547
GLsizei const GLchar ** string
Definition: glext.h:4101
OBSERVATION_T::Ptr getObservation(mrpt::obs::CSensoryFrame::Ptr &observations, mrpt::obs::CObservation::Ptr &observation, bool priority_to_sf=true)
Given an mrpt::obs::CSensoryFrame and a mrpt::obs::CObservation pointer if a OBSERVATION_T type obser...
Definition: obs_utils.h:36
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
Definition: os.cpp:355
mrpt::system::TTimeStamp getCurrentTime()
Returns the current (UTC) system time.
Definition: datetime.cpp:73
#define MRPT_START
Definition: mrpt_macros.h:425
#define ASSERT_(f)
Definition: mrpt_macros.h:309
#define MRPT_END
Definition: mrpt_macros.h:429
Contains classes for various device interfaces.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values,...
double DEG2RAD(const double x)
Degrees to radians.
Definition: bits.h:96
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
unsigned __int16 uint16_t
Definition: rptypes.h:44
unsigned char uint8_t
Definition: rptypes.h:41
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
uint16_t sensorID
Some kind of sensor ID which identifies it on the bus (if applicable, 0 otherwise)
math::TPose3D sensorPose
The 6D position of the sensor on the robot.
float sensedDistance
The measured range, in meters (or a value of 0 if there was no detected echo).



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 63ea9d1f1 Thu Nov 23 00:06:53 2017 +0100 at mar 26 may 2026 12:19:29 CEST