Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef HELPER_H_
00033 #define HELPER_H_
00034
00035 #include <asctec_hl_comm/mav_ctrl.h>
00036 #include <tf/transform_datatypes.h>
00037
00038 namespace helper
00039 {
00040
00042 const double ASCTEC_ACC_TO_SI = 9.81e-3;
00043
00045 const double ASCTEC_OMEGA_TO_SI = 0.015 * M_PI / 180.0;
00046
00048 const double ASCTEC_ATTITUDE_TO_SI = 0.01 * M_PI / 180.0;
00049
00051 template<typename T>
00052 inline double asctecAccToSI(const T & val)
00053 {
00054 return static_cast<double> (val) * ASCTEC_ACC_TO_SI;
00055 }
00056
00058 template<typename T>
00059 inline double asctecOmegaToSI(const T & val)
00060 {
00061 return static_cast<double> (val) * ASCTEC_OMEGA_TO_SI;
00062 }
00063
00065 template<typename T>
00066 inline double asctecAttitudeToSI(const T & val)
00067 {
00068 return static_cast<double> (val) * ASCTEC_ATTITUDE_TO_SI;
00069 }
00070
00072 inline uint16_t rateToPeriod(const double & rate)
00073 {
00074 if (rate > 0)
00075 return static_cast<uint16_t> (1000.0 / rate);
00076 else
00077 return 0;
00078 }
00079
00081 template<typename T>
00082 inline T clamp(const T & min, const T & max, const T & val)
00083 {
00084 if (val >= max)
00085 return max;
00086 else if (val <= min)
00087 return min;
00088 else
00089 return val;
00090 }
00091
00093 template<typename T>
00094 inline T clamp(const T & min, const T & max, const T & val, bool * clamped)
00095 {
00096 if (val >= max)
00097 {
00098 *clamped = true;
00099 return max;
00100 }
00101 else if (val <= min)
00102 {
00103 *clamped = true;
00104 return min;
00105 }
00106 else
00107 {
00108 *clamped = false;
00109 return val;
00110 }
00111 }
00112
00114
00117 inline uint32_t param2Fixpoint(const double & param)
00118 {
00119 return clamp<uint32_t> (0, 4294967296.0 - 1, param * 32768.0 + 2147483648.0 - 1);
00120
00121 }
00122
00124
00127 inline double param2Double(const uint32_t & param)
00128 {
00129 return ((double)param - 2147483648.0) / 32768.0;
00130 }
00131
00133
00136 inline double debug2Double(const int16_t & param)
00137 {
00138 return param * 0.001953125;
00139
00140 }
00141
00143 inline int yaw2asctec(const double & yaw)
00144 {
00145 return ((yaw < 0 ? yaw + 2 * M_PI : yaw) * 180.0 / M_PI) * 1000.0;
00146 }
00147
00148 #ifdef __APPLE__
00149
00150 #if (defined(__i386__) || defined(i386) || defined(__amd64) || defined(__x86_64))
00151 #if (!defined(__x87_inline_math__) && defined(__FAST_MATH__))
00152 #define __x87_inline_math__
00153 #endif
00154 #endif
00155
00156 #ifdef __x87_inline_math__
00157
00158
00159
00160
00161
00162 #define sincos(x,s,c) sincos_x87_inline(x,s,c)
00163 void sincos_x87_inline(double x,double *s,double *c);
00164 extern __inline__ void sincos_x87_inline(double x,double *s,double *c)
00165 {
00166 __asm__ ("fsincos;" : "=t" (*c), "=u" (*s) : "0" (x) : "st(7)");
00167 }
00168 #else
00169 #define sincos(th,x,y) { (*(x))=sin(th); (*(y))=cos(th); }
00170 #endif
00171 #endif
00172
00174 inline void angle2quaternion(const double &roll, const double &pitch, const double &yaw, double *w, double *x,
00175 double *y, double *z)
00176 {
00177 double sR2, cR2, sP2, cP2, sY2, cY2;
00178 sincos(roll * 0.5, &sR2, &cR2);
00179 sincos(pitch * 0.5, &sP2, &cP2);
00180 sincos(yaw * 0.5, &sY2, &cY2);
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 *w = cP2 * cR2 * cY2 + sP2 * sR2 * sY2;
00191 *x = cP2 * cY2 * sR2 - cR2 * sP2 * sY2;
00192 *y = cR2 * cY2 * sP2 + cP2 * sR2 * sY2;
00193 *z = cP2 * cR2 * sY2 - cY2 * sP2 * sR2;
00194 }
00195
00196 template<typename T>
00197 inline void setDiagonalCovariance(T & covariance_matrix, const double & covariance)
00198 {
00199 covariance_matrix[0] = covariance;
00200 covariance_matrix[4] = covariance;
00201 covariance_matrix[8] = covariance;
00202 }
00203
00204 }
00205
00206 #endif