Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
laeUtils.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Laelaps
4 //
5 // Library: liblaelaps
6 //
7 // File: leaUtils.h
8 //
9 //
10 /*! \file
11  *
12  * $LastChangedDate: 2016-01-21 16:50:25 -0700 (Thu, 21 Jan 2016) $
13  * $Rev: 4268 $
14  *
15  * \brief Laelaps common utilities.
16  *
17  * \author Robin Knight (robin.knight@roadnarrows.com)
18  *
19  * \par Copyright
20  * \h_copy 2015-2017. RoadNarrows LLC.\n
21  * http://www.roadnarrows.com\n
22  * All Rights Reserved
23  */
24 /*
25  * @EulaBegin@
26  *
27  * Unless otherwise stated explicitly, all materials contained are copyrighted
28  * and may not be used without RoadNarrows LLC's written consent,
29  * except as provided in these terms and conditions or in the copyright
30  * notice (documents and software) or other proprietary notice provided with
31  * the relevant materials.
32  *
33  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
34  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
35  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
36  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
37  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
38  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
41  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
42  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
43  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
44  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
45  *
46  * @EulaEnd@
47  */
48 ////////////////////////////////////////////////////////////////////////////////
49 
50 #ifndef _LAE_UTILS_H
51 #define _LAE_UTILS_H
52 
53 #include <sys/types.h>
54 #include <sys/time.h>
55 #include <time.h>
56 #include <math.h>
57 
58 #include <string>
59 #include <vector>
60 
61 #include "rnr/rnrconfig.h"
62 #include "rnr/log.h"
63 
64 #include "Laelaps/laelaps.h"
65 
66 #define M_TAU (2.0 * M_PI) ///< tau = 2 * pi
67 
68 namespace laelaps
69 {
70  const long MILLION = 1000000;
71  const long long BILLION = 1000000000;
72 
73 
74  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
75  // String functions.
76  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
77 
78  /*!
79  * \brief Convert version dotted string to integer equivalent.
80  *
81  * \param str Dotted version string "M[.m[.R]]".
82  *
83  * \return Version number.
84  */
85  extern uint_t strToVersion(const std::string &str);
86 
87  /*!
88  * \brief Get the error string describing the \h_laelaps error code.
89  *
90  * The absolute value of the error code is taken prior retrieving the string.
91  * An unknown or out-of-range error code will be mapped to
92  * \ref LAE_ECODE_BADEC.
93  *
94  * \param ecode Instance of \ref lae_ecodes.
95  *
96  * \return Returns the appropriate error code string.
97  */
98  extern const char *getStrError(const int ecode);
99 
100  /*!
101  * \brief Boolean to string.
102  *
103  * \param b Boolean value.
104  *
105  * \return Pointer to null-terminated constant character string.
106  */
107  inline const char *boolstr(bool b)
108  {
109  return b? "true": "false";
110  }
111 
112 
113  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
114  // Math functions.
115  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
116 
117  /*!
118  * \brief Convert degrees to radians
119  *
120  * \param d Degrees.
121  *
122  * \return Radians.
123  */
124  inline double degToRad(double d)
125  {
126  return d / 360.0 * M_TAU;
127  }
128 
129  /*!
130  * \brief Convert radians to degrees
131  *
132  * \param r Radians.
133  *
134  * \return Degrees.
135  */
136  inline double radToDeg(double r)
137  {
138  return r / M_TAU * 360.0;
139  }
140 
141  /*!
142  * \brief Integer absolute value.
143  *
144  * \param a Integer value.
145  *
146  * \return |a|
147  */
148  inline int iabs(int a)
149  {
150  return a>=0? a: -a;
151  }
152 
153  /*!
154  * \brief Cap value within limits [min, max].
155  *
156  * \param a Value.
157  * \param min Minimum.
158  * \param max Maximum.
159  *
160  * \return a: min \h_le a \h_le max
161  */
162  inline double fcap(double a, double min, double max)
163  {
164  return a<min? min: a>max? max: a;
165  }
166 
167  /*!
168  * \brief Cap value within limits [min, max].
169  *
170  * \param a Value.
171  * \param min Minimum.
172  * \param max Maximum.
173  *
174  * \return a: min \h_le a \h_le max
175  */
176  inline int cap(int a, int min, int max)
177  {
178  return a<min? min: a>max? max: a;
179  }
180 
181  /*!
182  * \brief Cap value within limits [min, max].
183  *
184  * \param a Value.
185  * \param min Minimum.
186  * \param max Maximum.
187  *
188  * \return a: min \h_le a \h_le max
189  */
190  inline uint_t cap(uint_t a, uint_t min, uint_t max)
191  {
192  return a<min? min: a>max? max: a;
193  }
194 
195  /*!
196  * \brief Sign of a.
197  *
198  * \return Return 1.0 or -1.0.
199  */
200  inline double signof(double a)
201  {
202  return a<0? -1.0: 1.0;
203  }
204 
205  /*!
206  * \brief Sign of a.
207  *
208  * \return Return 1 or -1.
209  */
210  inline int signof(int a)
211  {
212  return a<0? -1: 1;
213  }
214 
215 
216  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
217  // timeval functions.
218  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
219 
220  typedef struct timeval timeval_t; ///< typedef'ed timeval structure
221  typedef struct timespec timespec_t; ///< typedef'ed timespec structure
222 
223  /*!
224  * \brief Less than compare operator.
225  *
226  * Compare if the left hand side time is earlier than the right hand side
227  * time.
228  *
229  * lhs \h_lt rhs
230  *
231  * \param lhs Left hand side time.
232  * \param rhs Right hand side time.
233  *
234  * \return Returns true or false.
235  */
236  bool operator<(const struct timeval& lhs, const struct timeval& rhs);
237 
238  /*!
239  * \brief Equality compare operator.
240  *
241  * Compare if the two times are equal.
242  *
243  * lhs == rhs
244  *
245  * \param lhs Left hand side time.
246  * \param rhs Right hand side time.
247  *
248  * \return Returns true or false.
249  */
250  bool operator==(const struct timeval& lhs, const struct timeval& rhs);
251 
252  /*!
253  * \brief Greater than compare operator.
254  *
255  * Compare if the left hand side time is later than the right hand side
256  * time.
257  *
258  * lhs \h_gt rhs
259  *
260  * \param lhs Left hand side time.
261  * \param rhs Right hand side time.
262  *
263  * \return Returns true or false.
264  */
265  bool operator>(const struct timeval& lhs, const struct timeval& rhs);
266 
267  /*!
268  * \brief Less than or equal to compare operator.
269  *
270  * Compare if the left hand side time is equal or earlier than the right hand
271  * side time.
272  *
273  * lhs \h_le rhs
274  *
275  * \param lhs Left hand side time.
276  * \param rhs Right hand side time.
277  *
278  * \return Returns true or false.
279  */
280  bool operator<=(const struct timeval& lhs, const struct timeval& rhs);
281 
282  /*!
283  * \brief Greater than or equal to compare operator.
284  *
285  * Compare if the left hand side time is equal or later than the right hand
286  * side time.
287  *
288  * lhs \h_ge rhs
289  *
290  * \param lhs Left hand side time.
291  * \param rhs Right hand side time.
292  *
293  * \return Returns true or false.
294  */
295  bool operator>=(const struct timeval& lhs, const struct timeval& rhs);
296 
297  /*!
298  * \brief Addition operator.
299  *
300  * op1 + op2
301  *
302  * \param op1 Left hand side time.
303  * \param op2 Right hand side time.
304  *
305  * \return Sum of times.
306  */
307  struct timeval operator+(const struct timeval& op1,
308  const struct timeval& op2);
309 
310  /*!
311  * \brief Subtraction operator.
312  *
313  * op1 - op2, op1 \h_ge op2.
314  *
315  * \param op1 Left hand side time.
316  * \param op2 Right hand side time.
317  *
318  * \return Difference of times.
319  */
320  struct timeval operator-(const struct timeval& op1,
321  const struct timeval& op2);
322 
323  /*!
324  * \brief Calculate delta time in microseconds.
325  *
326  * t1 - t0, t1 \h_ge t0
327  *
328  * \param t1 Later time.
329  * \param t0 Earlier time.
330  *
331  * \return Returns dt in microseconds.
332  */
333  long dt_usec(struct timeval& t1, struct timeval& t0);
334 
335  /*!
336  * \brief Calculate delta time.
337  *
338  * t1 - t0
339  *
340  * \param t1 Time 1.
341  * \param t0 Time 0.
342  *
343  * \return Returns dt as a double. May be negative.
344  */
345  double dt(struct timeval& t1, struct timeval& t0);
346 
347 
348  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
349  // timespec functions.
350  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
351 
352  /*!
353  * \brief Less than compare operator.
354  *
355  * Compare if the left hand side time is earlier than the right hand side
356  * time.
357  *
358  *
359  * lhs \h_lt rhs
360  *
361  * \param lhs Left hand side time.
362  * \param rhs Right hand side time.
363  *
364  * \return Returns true or false.
365  */
366  bool operator<(const struct timespec& lhs, const struct timespec& rhs);
367 
368  /*!
369  * \brief Equality compare operator.
370  *
371  * Compare if the two times are equal.
372  *
373  * lhs == rhs
374  *
375  * \param lhs Left hand side time.
376  * \param rhs Right hand side time.
377  *
378  * \return Returns true or false.
379  */
380  bool operator==(const struct timespec& lhs, const struct timespec& rhs);
381 
382  /*!
383  * \brief Greater than compare operator.
384  *
385  * Compare if the left hand side time is later than the right hand side
386  * time.
387  *
388  * lhs \h_gt rhs
389  *
390  * \param lhs Left hand side time.
391  * \param rhs Right hand side time.
392  *
393  * \return Returns true or false.
394  */
395  bool operator>(const struct timespec& lhs, const struct timespec& rhs);
396 
397  /*!
398  * \brief Less than or equal to compare operator.
399  *
400  * Compare if the left hand side time is equal or earlier than the right hand
401  * side time.
402  *
403  * lhs \h_le rhs
404  *
405  * \param lhs Left hand side time.
406  * \param rhs Right hand side time.
407  *
408  * \return Returns true or false.
409  */
410  bool operator<=(const struct timespec& lhs, const struct timespec& rhs);
411 
412  /*!
413  * \brief Greater than or equal to compare operator.
414  *
415  * Compare if the left hand side time is equal or later than the right hand
416  * side time.
417  *
418  * lhs \h_ge rhs
419  *
420  * \param lhs Left hand side time.
421  * \param rhs Right hand side time.
422  *
423  * \return Returns true or false.
424  */
425  bool operator>=(const struct timespec& lhs, const struct timespec& rhs);
426 
427  /*!
428  * \brief Addition operator.
429  *
430  * op1 + op2
431  *
432  * \param op1 Left hand side time.
433  * \param op2 Right hand side time.
434  *
435  * \return Sum of times.
436  */
437  struct timespec operator+(const struct timespec& op1,
438  const struct timespec& op2);
439 
440  /*!
441  * \brief Subtraction operator.
442  *
443  * op1 - op2, op1 \h_ge op2.
444  *
445  * \param op1 Left hand side time.
446  * \param op2 Right hand side time.
447  *
448  * \return Difference of times.
449  */
450  struct timespec operator-(const struct timespec& op1,
451  const struct timespec& op2);
452 
453  /*!
454  * \brief Calculate delta time in nanoseconds.
455  *
456  * t1 - t0, t1 \h_ge t0
457  *
458  * \param t1 Later time.
459  * \param t0 Earlier time.
460  *
461  * \return Returns dt in nanoseconds.
462  */
463  long long dt_nsec(struct timespec& t1, struct timespec& t0);
464 
465  /*!
466  * \brief Calculate delta time.
467  *
468  * t1 - t0
469  *
470  * \param t1 Time 1.
471  * \param t0 Time 0.
472  *
473  * \return Returns dt as a double. May be negative.
474  */
475  double dt(struct timespec& t1, struct timespec& t0);
476 
477 
478  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
479  // Miscellanea.
480  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
481 
482  /*!
483  * \brief Get real device name.
484  *
485  * If the given device name is a symbolic link, then the real device the link
486  * references is returned. Otherwise the given device name is returned.
487  *
488  * \param strDevName Given device name.
489  *
490  * \return String.
491  */
492  std::string getRealDeviceName(const std::string &strDevName);
493 
494  /*!
495  * \brief Split string at the delimiter character.
496  *
497  * \param s String to split.
498  * \param delem Delimiter character.
499  * \param [out] elems Vector of split strings.
500  *
501  * \return Reference to vector of split strings.
502  */
503  std::vector<std::string> &split(const std::string &s,
504  char delim,
505  std::vector<std::string> &elems);
506 
507  /*!
508  * \brief Split string at the delimiter character.
509  *
510  * \param s String to split.
511  * \param delem Delimiter character.
512  *
513  * \return Vector of split strings.
514  */
515  std::vector<std::string> split(const std::string &s, char delim);
516 
517  // ---------------------------------------------------------------------------
518  // Class Dim
519  // ---------------------------------------------------------------------------
520 
521  /*!
522  * \brief Object width x height x length dimensions class.
523  */
524  class Dim
525  {
526  public:
527  double m_width; ///< object width (meters)
528  double m_height; ///< object height (meters)
529  double m_length; ///< object length (meters)
530 
531  /*!
532  * \brief Default constructor.
533  */
534  Dim()
535  {
536  clear();
537  }
538 
539  /*!
540  * \brief Initialization constructor.
541  *
542  * \param width Object width (meters)
543  * \param height Object height (meters)
544  * \param length Object length (meters)
545  */
546  Dim(double width, double height, double length)
547  {
548  m_width = width;
549  m_height = height;
550  m_length = length;
551  }
552 
553  /*!
554  * \brief Copy constructor.
555  *
556  * \param src Source object.
557  */
558  Dim(const Dim &src)
559  {
560  m_width = src.m_width;
561  m_height = src.m_height;
562  m_length = src.m_length;
563  }
564 
565  /*!
566  * \brief Destructor.
567  */
568  virtual ~Dim()
569  {
570  }
571 
572  /*!
573  * \brief Assignment operator.
574  *
575  * \param rhs Right hand side object.
576  *
577  * \return Returns copy of this.
578  */
579  Dim operator=(const Dim &rhs)
580  {
581  m_width = rhs.m_width;
582  m_height = rhs.m_height;
583  m_length = rhs.m_length;
584 
585  return *this;
586  }
587 
588  void clear()
589  {
590  m_width = 0.0;
591  m_length = 0.0;
592  m_height = 0.0;
593  }
594 
595  }; // class Dim
596 
597 
598 } // namespace laelaps
599 
600 
601 #endif // _LAE_UTILS_H
bool operator>=(const struct timeval &lhs, const struct timeval &rhs)
Greater than or equal to compare operator.
double dt(struct timeval &t1, struct timeval &t0)
Calculate delta time.
Dim()
Default constructor.
Definition: laeUtils.h:534
double degToRad(double d)
Convert degrees to radians.
Definition: laeUtils.h:124
double signof(double a)
Sign of a.
Definition: laeUtils.h:200
uint_t strToVersion(const std::string &str)
Convert version dotted string to integer equivalent.
virtual ~Dim()
Destructor.
Definition: laeUtils.h:568
bool operator>(const struct timeval &lhs, const struct timeval &rhs)
Greater than compare operator.
double fcap(double a, double min, double max)
Cap value within limits [min, max].
Definition: laeUtils.h:162
double m_length
object length (meters)
Definition: laeUtils.h:529
Object width x height x length dimensions class.
Definition: laeUtils.h:524
Dim(double width, double height, double length)
Initialization constructor.
Definition: laeUtils.h:546
const char * getStrError(const int ecode)
Get the error string describing the <b><i>Laelaps</i></b> error code.
double radToDeg(double r)
Convert radians to degrees.
Definition: laeUtils.h:136
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
Split string at the delimiter character.
int iabs(int a)
Integer absolute value.
Definition: laeUtils.h:148
struct timespec timespec_t
typedef&#39;ed timespec structure
Definition: laeUtils.h:221
bool operator<=(const struct timeval &lhs, const struct timeval &rhs)
Less than or equal to compare operator.
bool operator<(const struct timeval &lhs, const struct timeval &rhs)
Less than compare operator.
The <b><i>Laelaps</i></b> namespace encapsulates all <b><i>Laelaps</i></b> related constructs...
Definition: laeAlarms.h:64
Dim(const Dim &src)
Copy constructor.
Definition: laeUtils.h:558
Dim operator=(const Dim &rhs)
Assignment operator.
Definition: laeUtils.h:579
int cap(int a, int min, int max)
Cap value within limits [min, max].
Definition: laeUtils.h:176
double m_height
object height (meters)
Definition: laeUtils.h:528
long long dt_nsec(struct timespec &t1, struct timespec &t0)
Calculate delta time in nanoseconds.
bool operator==(const struct timeval &lhs, const struct timeval &rhs)
Equality compare operator.
struct timeval timeval_t
typedef&#39;ed timeval structure
Definition: laeUtils.h:220
#define M_TAU
tau = 2 * pi
Definition: laeUtils.h:66
double m_width
object width (meters)
Definition: laeUtils.h:527
const char * boolstr(bool b)
Boolean to string.
Definition: laeUtils.h:107
std::string getRealDeviceName(const std::string &strDevName)
Get real device name.
Top-level package include file.
long dt_usec(struct timeval &t1, struct timeval &t0)
Calculate delta time in microseconds.