Kuon  1.1.3
RoadNarrows Robotics Large Outdoor Mobile Robot Project
kuonUtils.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Kuon
4 //
5 // Library: libkuon
6 //
7 // File: kuonUtils.h
8 //
9 //
10 /*! \file
11  *
12  * $LastChangedDate: 2014-04-04 17:05:03 -0600 (Fri, 04 Apr 2014) $
13  * $Rev: 3629 $
14  *
15  * \brief Kuon common utilities.
16  *
17  * \author Robin Knight (robin.knight@roadnarrows.com)
18  *
19  * \copyright
20  * \h_copy 2014-2017. RoadNarrows LLC.\n
21  * http://www.roadnarrows.com\n
22  * All Rights Reserved
23  */
24 /*
25  * @EulaBegin@
26  *
27  * Permission is hereby granted, without written agreement and without
28  * license or royalty fees, to use, copy, modify, and distribute this
29  * software and its documentation for any purpose, provided that
30  * (1) The above copyright notice and the following two paragraphs
31  * appear in all copies of the source code and (2) redistributions
32  * including binaries reproduces these notices in the supporting
33  * documentation. Substantial modifications to this software may be
34  * copyrighted by their authors and need not follow the licensing terms
35  * described here, provided that the new terms are clearly indicated in
36  * all files where they apply.
37  *
38  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
39  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
40  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
41  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
42  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
43  * THE POSSIBILITY OF SUCH DAMAGE.
44  *
45  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
46  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
47  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
48  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
49  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
50  *
51  * @EulaEnd@
52  */
53 ////////////////////////////////////////////////////////////////////////////////
54 
55 #ifndef _KUON_UTILS_H
56 #define _KUON_UTILS_H
57 
58 #include <sys/types.h>
59 #include <sys/time.h>
60 #include <math.h>
61 
62 #include <string>
63 
64 #include "rnr/rnrconfig.h"
65 #include "rnr/log.h"
66 
67 #include "Kuon/kuon.h"
68 
69 #define M_TAU (2.0 * M_PI) ///< tau = 2 * pi
70 
71 namespace kuon
72 {
73  /*!
74  * \brief Convert version dotted string to integer equivalent.
75  *
76  * \param str Dotted version string "M[.m[.R]]".
77  *
78  * \return Version number.
79  */
80  extern uint_t strToVersion(const std::string &str);
81 
82  /*!
83  * \brief Get the error string describing the \h_hek error code.
84  *
85  * The absolute value of the error code is taken prior retrieving the string.
86  * An unknown or out-of-range error code will be mapped to
87  * \ref HEK_ECODE_BADEC.
88  *
89  * \param ecode Instance of \ref hek_ecodes.
90  *
91  * \return Returns the appropriate error code string.
92  */
93  extern const char *getStrError(const int ecode);
94 
95  /*!
96  * \brief Convert degrees to radians
97  *
98  * \param d Degrees.
99  *
100  * \return Radians.
101  */
102  inline double degToRad(double d)
103  {
104  return d / 360.0 * M_TAU;
105  }
106 
107  /*!
108  * \brief Convert radians to degrees
109  *
110  * \param r Radians.
111  *
112  * \return Degrees.
113  */
114  inline double radToDeg(double r)
115  {
116  return r / M_TAU * 360.0;
117  }
118 
119  /*!
120  * \brief Integer absolute value.
121  *
122  * \param a Integer value.
123  *
124  * \return |a|
125  */
126  inline int iabs(int a)
127  {
128  return a>=0? a: -a;
129  }
130 
131  /*!
132  * \brief Cap value within limits [min, max].
133  *
134  * \param a Value.
135  * \param min Minimum.
136  * \param max Maximum.
137  *
138  * \return a: min \h_le a \h_le max
139  */
140  inline double fcap(double a, double min, double max)
141  {
142  return a<min? min: a>max? max: a;
143  }
144 
145  /*!
146  * \brief Cap value within limits [min, max].
147  *
148  * \param a Value.
149  * \param min Minimum.
150  * \param max Maximum.
151  *
152  * \return a: min \h_le a \h_le max
153  */
154  inline int icap(int a, int min, int max)
155  {
156  return a<min? min: a>max? max: a;
157  }
158 
159  /*!
160  * \brief Boolean to string.
161  *
162  * \param b Boolean value.
163  *
164  * \return Pointer to null-terminated constant character string.
165  */
166  inline const char *boolstr(bool b)
167  {
168  return b? "true": "false";
169  }
170 
171  /*!
172  * \brief Compare operator to test if left hand side time is earlier than
173  * the right hand side time.
174  *
175  * \term lhs \h_lt rhs <==> lhs is an earlier time than rhs.
176  *
177  * \param lhs Left hand side time.
178  * \param rhs Right hand side time.
179  *
180  * \return Returns true or false.
181  */
182  inline bool operator<(const struct timeval& lhs, const struct timeval& rhs)
183  {
184  if( lhs.tv_sec < rhs.tv_sec )
185  {
186  return true;
187  }
188  else if( (lhs.tv_sec == rhs.tv_sec) && (lhs.tv_usec < rhs.tv_usec) )
189  {
190  return true;
191  }
192  else
193  {
194  return false;
195  }
196  }
197 
198  /*!
199  * \brief Compare operator to test if left hand side time equals
200  * the right hand side time.
201  *
202  * \term lhs == rhs <==> lhs time is the same time as the rhs.
203  *
204  * \param lhs Left hand side time.
205  * \param rhs Right hand side time.
206  *
207  * \return Returns true or false.
208  */
209  inline bool operator==(const struct timeval& lhs, const struct timeval& rhs)
210  {
211  return (lhs.tv_sec == rhs.tv_sec) && (lhs.tv_usec == rhs.tv_usec)?
212  true: false;
213  }
214 
215  /*!
216  * \brief Get real device name.
217  *
218  * If the given device name is a symbolic link, then the real device the link
219  * references is returned. Otherwise the given device name is returned.
220  *
221  * \param strDevName Given device name.
222  *
223  * \return String.
224  */
225  std::string getRealDeviceName(const std::string &strDevName);
226 
227 } // namespace kuon
228 
229 
230 #endif // _KUON_UTILS_H
uint_t strToVersion(const std::string &str)
Convert version dotted string to integer equivalent.
const char * boolstr(bool b)
Boolean to string.
Definition: kuonUtils.h:166
The <b><i>Kuon</i></b> namespace encapsulates all <b><i>Kuon</i></b> related constructs.
Definition: kuon.h:66
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...
Definition: kuonUtils.h:182
int iabs(int a)
Integer absolute value.
Definition: kuonUtils.h:126
int icap(int a, int min, int max)
Cap value within limits [min, max].
Definition: kuonUtils.h:154
RoadNarrows Kuon robot top-level header file.
std::string getRealDeviceName(const std::string &strDevName)
Get real device name.
double fcap(double a, double min, double max)
Cap value within limits [min, max].
Definition: kuonUtils.h:140
const char * getStrError(const int ecode)
Get the error string describing the error code.
Definition: kuonUtils.cxx:126
#define M_TAU
tau = 2 * pi
Definition: kuonUtils.h:69
double degToRad(double d)
Convert degrees to radians.
Definition: kuonUtils.h:102
double radToDeg(double r)
Convert radians to degrees.
Definition: kuonUtils.h:114
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.
Definition: kuonUtils.h:209