Hekateros  3.4.3
RoadNarrows Robotics Robot Arm Project
hekUtils.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Hekateros
4 //
5 // Library: libhekateros
6 //
7 // File: hekUtils.h
8 //
9 //
10 /*! \file
11  *
12  * $LastChangedDate: 2015-04-17 15:31:34 -0600 (Fri, 17 Apr 2015) $
13  * $Rev: 3942 $
14  *
15  * \brief Hekateros common utilities.
16  *
17  * \author Daniel Packard (daniel@roadnarrows.com)
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  *
20  * \copyright
21  * \h_copy 2012-2017. RoadNarrows LLC.\n
22  * http://www.roadnarrows.com\n
23  * All Rights Reserved
24  */
25 /*
26  * @EulaBegin@
27  *
28  * Unless otherwise stated explicitly, all materials contained are copyrighted
29  * and may not be used without RoadNarrows LLC's written consent,
30  * except as provided in these terms and conditions or in the copyright
31  * notice (documents and software) or other proprietary notice provided with
32  * the relevant materials.
33  *
34  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
35  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
36  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
37  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
38  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
39  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
42  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
43  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
44  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
45  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
46  *
47  * @EulaEnd@
48  */
49 ////////////////////////////////////////////////////////////////////////////////
50 
51 #ifndef _HEK_UTILS_H
52 #define _HEK_UTILS_H
53 
54 #include <sys/types.h>
55 #include <sys/time.h>
56 #include <time.h>
57 #include <math.h>
58 
59 #include <string>
60 #include <vector>
61 
62 #include "rnr/rnrconfig.h"
63 #include "rnr/log.h"
64 
65 #include "Hekateros/hekateros.h"
66 
67 #define M_TAU (2.0 * M_PI) ///< tau = 2 * pi
68 
69 namespace hekateros
70 {
71  const long MILLION = 1000000;
72  const long long BILLION = 1000000000;
73 
74 
75  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
76  // String functions.
77  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
78 
79  /*!
80  * \brief Convert version dotted string to integer equivalent.
81  *
82  * \param str Dotted version string "M[.m[.R]]".
83  *
84  * \return Version number.
85  */
86  extern uint_t strToVersion(const std::string &str);
87 
88  /*!
89  * \brief Get the error string describing the \h_hek error code.
90  *
91  * The absolute value of the error code is taken prior retrieving the string.
92  * An unknown or out-of-range error code will be mapped to
93  * \ref HEK_ECODE_BADEC.
94  *
95  * \param ecode Instance of \ref hek_ecodes.
96  *
97  * \return Returns the appropriate error code string.
98  */
99  extern const char *getStrError(const int ecode);
100 
101  /*!
102  * \brief Boolean to string.
103  *
104  * \param b Boolean value.
105  *
106  * \return Pointer to null-terminated constant character string.
107  */
108  inline const char *boolstr(bool b)
109  {
110  return b? "true": "false";
111  }
112 
113 
114  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
115  // Math functions.
116  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
117 
118  /*!
119  * \brief Convert degrees to radians
120  *
121  * \param d Degrees.
122  *
123  * \return Radians.
124  */
125  inline double degToRad(double d)
126  {
127  return d / 360.0 * M_TAU;
128  }
129 
130  /*!
131  * \brief Convert radians to degrees
132  *
133  * \param r Radians.
134  *
135  * \return Degrees.
136  */
137  inline double radToDeg(double r)
138  {
139  return r / M_TAU * 360.0;
140  }
141 
142  /*!
143  * \brief Integer absolute value.
144  *
145  * \param a Integer value.
146  *
147  * \return |a|
148  */
149  inline int iabs(int a)
150  {
151  return a>=0? a: -a;
152  }
153 
154  /*!
155  * \brief Cap value within limits [min, max].
156  *
157  * \param a Value.
158  * \param min Minimum.
159  * \param max Maximum.
160  *
161  * \return a: min \h_le a \h_le max
162  */
163  inline double fcap(double a, double min, double max)
164  {
165  return a<min? min: a>max? max: a;
166  }
167 
168  /*!
169  * \brief Cap value within limits [min, max].
170  *
171  * \param a Value.
172  * \param min Minimum.
173  * \param max Maximum.
174  *
175  * \return a: min \h_le a \h_le max
176  */
177  inline int cap(int a, int min, int max)
178  {
179  return a<min? min: a>max? max: a;
180  }
181 
182  /*!
183  * \brief Sign of a.
184  *
185  * \return Return 1.0 or -1.0.
186  */
187  inline double signof(double a)
188  {
189  return a<0? -1.0: 1.0;
190  }
191 
192  /*!
193  * \brief Sign of a.
194  *
195  * \return Return 1 or -1.
196  */
197  inline int signof(int a)
198  {
199  return a<0? -1: 1;
200  }
201 
202 
203  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
204  // timeval functions.
205  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
206 
207  /*!
208  * \brief Compare operator to test if left hand side time is earlier than
209  * the right hand side time.
210  *
211  * lhs \h_lt rhs
212  *
213  * \param lhs Left hand side time.
214  * \param rhs Right hand side time.
215  *
216  * \return Returns true or false.
217  */
218  bool operator<(const struct timeval& lhs, const struct timeval& rhs);
219 
220  /*!
221  * \brief Compare operator to test if left hand side time equals
222  * the right hand side time.
223  *
224  * lhs == rhs
225  *
226  * \param lhs Left hand side time.
227  * \param rhs Right hand side time.
228  *
229  * \return Returns true or false.
230  */
231  bool operator==(const struct timeval& lhs, const struct timeval& rhs);
232 
233  /*!
234  * \brief Compare operator to test if left hand side time is later than
235  * the right hand side time.
236  *
237  * lhs \h_gt rhs
238  *
239  * \param lhs Left hand side time.
240  * \param rhs Right hand side time.
241  *
242  * \return Returns true or false.
243  */
244  bool operator>(const struct timeval& lhs, const struct timeval& rhs);
245 
246  /*!
247  * \brief Addition operator.
248  *
249  * op1 + op2
250  *
251  * \param op1 Left hand side time.
252  * \param op2 Right hand side time.
253  *
254  * \return Sum of times.
255  */
256  struct timeval operator+(const struct timeval& op1,
257  const struct timeval& op2);
258 
259  /*!
260  * \brief Subtraction operator.
261  *
262  * op1 - op2, op1 \h_ge op2.
263  *
264  * \param op1 Left hand side time.
265  * \param op2 Right hand side time.
266  *
267  * \return Difference of times.
268  */
269  struct timeval operator-(const struct timeval& op1,
270  const struct timeval& op2);
271 
272  /*!
273  * \brief Calculate delta time in microseconds.
274  *
275  * t1 - t0, t1 \h_ge t0
276  *
277  * \param t1 Later time.
278  * \param t0 Earlier time.
279  *
280  * \return Returns dt in microseconds.
281  */
282  long dt_usec(struct timeval& t1, struct timeval& t0);
283 
284  /*!
285  * \brief Calculate delta time.
286  *
287  * t1 - t0
288  *
289  * \param t1 Time 1.
290  * \param t0 Time 0.
291  *
292  * \return Returns dt as a double. May be negative.
293  */
294  double dt(struct timeval& t1, struct timeval& t0);
295 
296 
297  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
298  // timespec functions.
299  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
300 
301  /*!
302  * \brief Compare operator to test if left hand side time is earlier than
303  * the right hand side time.
304  *
305  * lhs \h_lt rhs
306  *
307  * \param lhs Left hand side time.
308  * \param rhs Right hand side time.
309  *
310  * \return Returns true or false.
311  */
312  bool operator<(const struct timespec& lhs, const struct timespec& rhs);
313 
314  /*!
315  * \brief Compare operator to test if left hand side time equals
316  * the right hand side time.
317  *
318  * lhs == rhs
319  *
320  * \param lhs Left hand side time.
321  * \param rhs Right hand side time.
322  *
323  * \return Returns true or false.
324  */
325  bool operator==(const struct timespec& lhs, const struct timespec& rhs);
326 
327  /*!
328  * \brief Compare operator to test if left hand side time is later than
329  * the right hand side time.
330  *
331  * lhs \h_gt rhs
332  *
333  * \param lhs Left hand side time.
334  * \param rhs Right hand side time.
335  *
336  * \return Returns true or false.
337  */
338  bool operator>(const struct timespec& lhs, const struct timespec& rhs);
339 
340  /*!
341  * \brief Addition operator.
342  *
343  * op1 + op2
344  *
345  * \param op1 Left hand side time.
346  * \param op2 Right hand side time.
347  *
348  * \return Sum of times.
349  */
350  struct timespec operator+(const struct timespec& op1,
351  const struct timespec& op2);
352 
353  /*!
354  * \brief Subtraction operator.
355  *
356  * op1 - op2, op1 \h_ge op2.
357  *
358  * \param op1 Left hand side time.
359  * \param op2 Right hand side time.
360  *
361  * \return Difference of times.
362  */
363  struct timespec operator-(const struct timespec& op1,
364  const struct timespec& op2);
365 
366  /*!
367  * \brief Calculate delta time in nanoseconds.
368  *
369  * t1 - t0, t1 \h_ge t0
370  *
371  * \param t1 Later time.
372  * \param t0 Earlier time.
373  *
374  * \return Returns dt in nanoseconds.
375  */
376  long long dt_nsec(struct timespec& t1, struct timespec& t0);
377 
378  /*!
379  * \brief Calculate delta time.
380  *
381  * t1 - t0
382  *
383  * \param t1 Time 1.
384  * \param t0 Time 0.
385  *
386  * \return Returns dt as a double. May be negative.
387  */
388  double dt(struct timespec& t1, struct timespec& t0);
389 
390 
391  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
392  // Miscellanea.
393  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
394 
395  /*!
396  * \brief Get real device name.
397  *
398  * If the given device name is a symbolic link, then the real device the link
399  * references is returned. Otherwise the given device name is returned.
400  *
401  * \param strDevName Given device name.
402  *
403  * \return String.
404  */
405  std::string getRealDeviceName(const std::string &strDevName);
406 
407  /*!
408  * \brief Split string at the delimiter character.
409  *
410  * \param s String to split.
411  * \param delem Delimiter character.
412  * \param [out] elems Vector of split strings.
413  *
414  * \return Reference to vector of split strings.
415  */
416  std::vector<std::string> &split(const std::string &s,
417  char delim,
418  std::vector<std::string> &elems);
419 
420  /*!
421  * \brief Split string at the delimiter character.
422  *
423  * \param s String to split.
424  * \param delem Delimiter character.
425  *
426  * \return Vector of split strings.
427  */
428  std::vector<std::string> split(const std::string &s, char delim);
429 
430 } // namespace hekateros
431 
432 
433 #endif // _HEK_UTILS_H
const char * boolstr(bool b)
Boolean to string.
Definition: hekUtils.h:108
bool operator>(const struct timeval &lhs, const struct timeval &rhs)
Compare operator to test if left hand side time is later than the right hand side time...
bool operator<(const struct timeval &lhs, const struct timeval &rhs)
Compare operator to test if left hand side time is earlier than the right hand side time...
double signof(double a)
Sign of a.
Definition: hekUtils.h:187
double dt(struct timeval &t1, struct timeval &t0)
Calculate delta time.
#define M_TAU
tau = 2 * pi
Definition: hekUtils.h:67
double fcap(double a, double min, double max)
Cap value within limits [min, max].
Definition: hekUtils.h:163
double radToDeg(double r)
Convert radians to degrees.
Definition: hekUtils.h:137
const char * getStrError(const int ecode)
Get the error string describing the <b><i>Hekateros</i></b> error code.
int cap(int a, int min, int max)
Cap value within limits [min, max].
Definition: hekUtils.h:177
bool operator==(const struct timeval &lhs, const struct timeval &rhs)
Compare operator to test if left hand side time equals the right hand side time.
int iabs(int a)
Integer absolute value.
Definition: hekUtils.h:149
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
Split string at the delimiter character.
Top-level package include file.
double degToRad(double d)
Convert degrees to radians.
Definition: hekUtils.h:125
uint_t strToVersion(const std::string &str)
Convert version dotted string to integer equivalent.
std::string getRealDeviceName(const std::string &strDevName)
Get real device name.
long long dt_nsec(struct timespec &t1, struct timespec &t0)
Calculate delta time in nanoseconds.
The <b><i>Hekateros</i></b> namespace encapsulates all <b><i>Hekateros</i></b> related constructs...
Definition: hekateros.h:56
long dt_usec(struct timeval &t1, struct timeval &t0)
Calculate delta time in microseconds.