ethzasl-msf - Modular Sensor Fusion
Time delay compensated single and multi sensor fusion framework based on an EKF
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
msf_macros.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2013 Simon Lynen, ASL, ETH Zurich, Switzerland
3  * You can contact the author at <slynen at ethz dot ch>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MSF_MACROS_H_
18 #define MSF_MACROS_H_
19 
20 #ifndef NUMERIC_PREC
21 #define NUMERIC_PREC 4 // Number of decimal places.
22 #endif
23 
24 #ifdef UNUSEDPARAM
25 #elif defined(__GNUC__)
26 # define UNUSEDPARAM(x) UNUSED_ ## x __attribute__((unused))
27 #elif defined(__LCLINT__)
28 # define UNUSEDPARAM(x) /*@unused@*/ x
29 #else
30 # define UNUSEDPARAM(x) x
31 #endif
32 
33 #ifndef UNUSED
34 # define UNUSED(x) (void)x;
35 #endif
36 
37 #ifndef STREAMQUAT
38 #define STREAMQUAT(q) "["<<std::setprecision(3)<<(q).w()<<", "<<(q).x()<<", "<< \
39  (q).y()<<", "<<(q).z()<<std::setprecision(NUMERIC_PREC)<<"]"
40 #endif
41 
42 #ifndef DEG2RAD
43 #define DEG2RAD (M_PI/180.0)
44 #endif
45 
46 #ifdef WIN32
47 #define MSF_LIKELY(x) (x)
48 #define MSF_UNLIKELY(x) (x)
49 #else
50 #define MSF_LIKELY(x) __builtin_expect((x),1)
51 #define MSF_UNLIKELY(x) __builtin_expect((x),0)
52 #endif
53 
54 #ifdef ROS_PACKAGE_NAME // Use ROS if it is available.
55 #include <ros/console.h>
56 #define MSF_INFO_STREAM(x) ROS_INFO_STREAM(x)
57 #define MSF_WARN_STREAM(x) ROS_WARN_STREAM(x)
58 #define MSF_ERROR_STREAM(x) ROS_ERROR_STREAM(x)
59 
60 #define MSF_INFO_STREAM_ONCE(x) ROS_INFO_STREAM_ONCE(x)
61 #define MSF_WARN_STREAM_ONCE(x) ROS_WARN_STREAM_ONCE(x)
62 #define MSF_ERROR_STREAM_ONCE(x) ROS_ERROR_STREAM_ONCE(x)
63 
64 #define MSF_LOG_STREAM_THROTTLE(rate, x) ROS_LOG_STREAM_THROTTLE(rate, x)
65 #define MSF_WARN_STREAM_THROTTLE(rate, x) ROS_WARN_STREAM_THROTTLE(rate, x)
66 #define MSF_ERROR_STREAM_THROTTLE(rate, x) ROS_ERROR_STREAM_THROTTLE(rate, x)
67 
68 #define MSF_INFO_STREAM_COND(cond, x) ROS_INFO_STREAM_COND(cond, x)
69 #define MSF_WARN_STREAM_COND(cond, x) ROS_WARN_STREAM_COND(cond, x)
70 #define MSF_ERROR_STREAM_COND(cond, x) ROS_ERROR_STREAM_COND(cond, x)
71 
72 #else
73 #include <chrono> // Using std::chrono instead of ros::Time.
74 // Adapted from rosconsole.
75 //Copyright (c) 2008, Willow Garage, Inc.
76 #ifndef MSF_INFO_STREAM
77 #define MSF_INFO_STREAM(x) std::cerr<<"\033[0;0m[INFO] "<<x<<"\033[0;0m"<<std::endl;
78 #endif
79 
80 #ifndef MSF_WARN_STREAM
81 #define MSF_WARN_STREAM(x) std::cerr<<"\033[0;33m[WARN] "<<x<<"\033[0;0m"<<std::endl;
82 #endif
83 
84 #ifndef MSF_ERROR_STREAM
85 #define MSF_ERROR_STREAM(x) std::cerr<<"\033[1;31m[ERROR] "<<x<<"\033[0;0m"<<std::endl;
86 #endif
87 
88 #define MSF_INFO_STREAM_ONCE(x) \
89  do \
90  { \
91  static bool __log_stream_once__hit__ = false; \
92  if (MSF_UNLIKELY(!__log_stream_once__hit__)) \
93  { \
94  __log_stream_once__hit__ = true; \
95  MSF_INFO_STREAM(x); \
96  } \
97  } while(0)
98 
99 #define MSF_WARN_STREAM_ONCE(x) \
100  do \
101  { \
102  static bool __log_stream_once__hit__ = false; \
103  if (MSF_UNLIKELY(!__log_stream_once__hit__)) \
104  { \
105  __log_stream_once__hit__ = true; \
106  MSF_WARN_STREAM(x); \
107  } \
108  } while(0)
109 
110 #define MSF_ERROR_STREAM_ONCE(x) \
111  do \
112  { \
113  static bool __log_stream_once__hit__ = false; \
114  if (MSF_UNLIKELY(!__log_stream_once__hit__)) \
115  { \
116  __log_stream_once__hit__ = true; \
117  MSF_ERROR_STREAM(x); \
118  } \
119  } while(0)
120 
121 #define MSF_LOG_STREAM_THROTTLE(rate, x) \
122  do \
123  { \
124  static double __log_stream_throttle__last_hit__ = 0.0; \
125  std::chrono::time_point<std::chrono::system_clock> __log_stream_throttle__now__ = \
126  std::chrono::system_clock::now(); \
127  if (MSF_UNLIKELY(__log_stream_throttle__last_hit__ + rate <= \
128  std::chrono::duration_cast<std::chrono::seconds>( \
129  __log_stream_throttle__now__.time_since_epoch()).count())) \
130  { \
131  __log_stream_throttle__last_hit__ = std::chrono::duration_cast< \
132  std::chrono::seconds>(__log_stream_throttle__now__.time_since_epoch()).count(); \
133  MSF_INFO_STREAM(x); \
134  } \
135  } while(0)
136 
137 #define MSF_WARN_STREAM_THROTTLE(rate, x) \
138  do \
139  { \
140  static double __log_stream_throttle__last_hit__ = 0.0; \
141  std::chrono::time_point<std::chrono::system_clock> __log_stream_throttle__now__ = \
142  std::chrono::system_clock::now(); \
143  if (MSF_UNLIKELY(__log_stream_throttle__last_hit__ + rate <= \
144  std::chrono::duration_cast<std::chrono::seconds>( \
145  __log_stream_throttle__now__.time_since_epoch()).count())) \
146  { \
147  __log_stream_throttle__last_hit__ = \
148  std::chrono::duration_cast<std::chrono::seconds>( \
149  __log_stream_throttle__now__.time_since_epoch()).count(); \
150  MSF_WARN_STREAM(x); \
151  } \
152  } while(0)
153 
154 #define MSF_ERROR_STREAM_THROTTLE(rate, x) \
155  do \
156  { \
157  static double __log_stream_throttle__last_hit__ = 0.0; \
158  std::chrono::time_point<std::chrono::system_clock> __log_stream_throttle__now__ = \
159  std::chrono::system_clock::now(); \
160  if (MSF_UNLIKELY(__log_stream_throttle__last_hit__ + rate <= \
161  std::chrono::duration_cast<std::chrono::seconds>(\
162  __log_stream_throttle__now__.time_since_epoch()).count())) \
163  { \
164  __log_stream_throttle__last_hit__ = \
165  std::chrono::duration_cast<std::chrono::seconds>\
166  (__log_stream_throttle__now__.time_since_epoch()).count(); \
167  MSF_ERROR_STREAM(x); \
168  } \
169  } while(0)
170 
171 #define MSF_INFO_STREAM_COND(cond, x) \
172  do \
173  { \
174  if (MSF_UNLIKELY(cond)) \
175  { \
176  MSF_INFO_STREAM(x); \
177  } \
178  } while(0)
179 
180 #define MSF_WARN_STREAM_COND(cond, x) \
181  do \
182  { \
183  if (MSF_UNLIKELY(cond)) \
184  { \
185  MSF_WARN_STREAM(x); \
186  } \
187  } while(0)
188 
189 #define MSF_ERROR_STREAM_COND(cond, x) \
190  do \
191  { \
192  if (MSF_UNLIKELY(cond)) \
193  { \
194  MSF_ERROR_STREAM(x); \
195  } \
196  } while(0)
197 
198 #endif
199 #endif // MSF_MACROS_H_