PX4 Firmware
PX4 Autopilot Software http://px4.io
test.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file test.cpp
36  *
37  * Test library code
38  */
39 
40 #include <stdio.h>
41 #include <math.h>
42 #include <stdlib.h>
43 
44 #include <px4_platform_common/defines.h>
45 
46 #include "test.hpp"
47 
48 bool __EXPORT equal(float a, float b, float epsilon)
49 {
50  float diff = fabsf(a - b);
51 
52  if (diff > epsilon) {
53  printf("not equal ->\n\ta: %12.8f\n\tb: %12.8f\n", double(a), double(b));
54  return false;
55 
56  } else { return true; }
57 }
58 
59 bool __EXPORT greater_than(float a, float b)
60 {
61  if (a > b) {
62  return true;
63 
64  } else {
65  printf("not a > b ->\n\ta: %12.8f\n\tb: %12.8f\n", double(a), double(b));
66  return false;
67  }
68 }
69 
70 bool __EXPORT less_than(float a, float b)
71 {
72  if (a < b) {
73  return true;
74 
75  } else {
76  printf("not a < b ->\n\ta: %12.8f\n\tb: %12.8f\n", double(a), double(b));
77  return false;
78  }
79 }
80 
81 bool __EXPORT greater_than_or_equal(float a, float b)
82 {
83  if (a >= b) {
84  return true;
85 
86  } else {
87  printf("not a >= b ->\n\ta: %12.8f\n\tb: %12.8f\n", double(a), double(b));
88  return false;
89  }
90 }
91 
92 bool __EXPORT less_than_or_equal(float a, float b)
93 {
94  if (a <= b) {
95  return true;
96 
97  } else {
98  printf("not a <= b ->\n\ta: %12.8f\n\tb: %12.8f\n", double(a), double(b));
99  return false;
100  }
101 }
102 
104  const float &num,
105  float &sig,
106  int &exp)
107 {
108 // FIXME - This code makes no sense when exp is an int
109 // FIXME - isnan and isinf not defined for QuRT
110 #ifndef __PX4_QURT
111  if (!PX4_ISFINITE(num)) {
112  sig = 0.0f;
113  exp = -99;
114  return;
115  }
116 
117  if (fabsf(num) < 1.0e-38f) {
118  sig = 0;
119  exp = 0;
120  return;
121  }
122 
123  exp = log10f(fabsf(num));
124 
125  if (exp > 0) {
126  exp = ceil(exp);
127 
128  } else {
129  exp = floor(exp);
130  }
131 
132 #endif
133 
134  sig = num;
135 
136  // cheap power since it is integer
137  if (exp > 0) {
138  for (int i = 0; i < abs(exp); i++) { sig /= 10; }
139 
140  } else {
141  for (int i = 0; i < abs(exp); i++) { sig *= 10; }
142  }
143 }
144 
145 
void __EXPORT float2SigExp(const float &num, float &sig, int &exp)
Definition: test.cpp:103
Definition: I2C.hpp:51
Controller library code.
Dual< Scalar, N > ceil(const Dual< Scalar, N > &a)
Definition: Dual.hpp:203
bool __EXPORT less_than_or_equal(float a, float b)
Definition: test.cpp:92
bool __EXPORT equal(float a, float b, float epsilon)
Definition: test.cpp:48
bool __EXPORT less_than(float a, float b)
Definition: test.cpp:70
bool __EXPORT greater_than(float a, float b)
Definition: test.cpp:59
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
bool __EXPORT greater_than_or_equal(float a, float b)
Definition: test.cpp:81
Dual< Scalar, N > abs(const Dual< Scalar, N > &a)
Definition: Dual.hpp:196
Dual< Scalar, N > floor(const Dual< Scalar, N > &a)
Definition: Dual.hpp:210