1 /*
2 * Copyright (c) 2014, Texas Instruments Incorporated
3 * 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 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
33 #include "Utilities.h"
34 #include <math.h>
36 #ifndef M_PI
37 #define M_PI 3.14159265358979323846
38 #endif
40 #define NUM_ERRORVALS 10
42 Utilities::Utilities()
43 {
44 }
46 float Utilities::wrapAngle(float angle)
47 {
48 angle = fmod(angle + 180, 360);
49 if (angle < 0) {
50 angle += 360;
51 }
52 return (angle - 180);
53 }
55 float Utilities::wrapAngle360(float angle)
56 {
57 angle = wrapAngle(angle);
58 if (angle < 0) {
59 angle += 360;
60 }
61 return angle;
62 }
64 float Utilities::saturate(float value, float min, float max)
65 {
66 if (value < min) {
67 value = min;
68 }
69 else if (value > max) {
70 value = max;
71 }
72 return value;
73 }
75 bool Utilities::inRange(float value, float lowerBound, float upperBound)
76 {
77 return ((value > lowerBound) && (value < upperBound));
78 }
80 int Utilities::clip(int a)
81 {
82 if (a > 400) {
83 a = 400;
84 }
85 if (a < -400) {
86 a = -400;
87 }
88 return a;
89 }
91 float Utilities::clip(float a)
92 {
93 if (a > 400) {
94 a = 400;
95 }
96 if (a < -400) {
97 a = -400;
98 }
99 return a;
100 }
102 int Utilities::min(int a, int b)
103 {
104 if (a < b) {
105 return a;
106 }
107 else {
108 return b;
109 }
110 }
112 int Utilities::max(int a, int b)
113 {
114 if (a > b) {
115 return a;
116 }
117 else {
118 return b;
119 }
120 }
122 float Utilities::toDegrees(float angle)
123 {
124 return float ((angle * 180.0f) / M_PI);
125 }