ISF  2.2 rev 5
Intelligent Sensing Framework for Kinetis with Processor Expert
approximations.c
Go to the documentation of this file.
1 // Copyright (c) 2014, 2015, Freescale Semiconductor, Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of Freescale Semiconductor, Inc. nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL FREESCALE SEMICONDUCTOR, INC. BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 // This file provides replacement trig functions which operate at reduced power,
27 // while still maintaining sufficient accuracy for sensor fusion.
28 #include "math.h"
29 #include "basic_types.h"
30 #include "approximations.h"
31 
32 // function returns an approximation to angle(deg)=asin(x) for x in the range -1 <= x <= 1
33 // and returns -90 <= angle <= 90 deg
34 // maximum error is 10.29E-6 deg
35 float fasin_deg(float x)
36 {
37  // for robustness, check for invalid argument
38  if (x >= 1.0F) return 90.0F;
39  if (x <= -1.0F) return -90.0F;
40 
41  // call the atan which will return an angle in the correct range -90 to 90 deg
42  // this line cannot fail from division by zero or negative square root since |x| < 1
43  return (fatan_deg(x / sqrtf(1.0F - x * x)));
44 }
45 
46 // function returns an approximation to angle(deg)=acos(x) for x in the range -1 <= x <= 1
47 // and returns 0 <= angle <= 180 deg
48 // maximum error is 14.67E-6 deg
49 float facos_deg(float x)
50 {
51  // for robustness, check for invalid arguments
52  if (x >= 1.0F) return 0.0F;
53  if (x <= -1.0F) return 180.0F;
54 
55  // call the atan which will return an angle in the incorrect range -90 to 90 deg
56  // these lines cannot fail from division by zero or negative square root
57  if (x == 0.0F) return 90.0F;
58  if (x > 0.0F) return fatan_deg((sqrtf(1.0F - x * x) / x));
59  return 180.0F + fatan_deg((sqrtf(1.0F - x * x) / x));
60 }
61 
62 // function returns angle in range -90 to 90 deg
63 // maximum error is 9.84E-6 deg
64 float fatan_deg(float x)
65 {
66  float fangledeg; // compute computed (deg)
67  int8 ixisnegative; // argument x is negative
68  int8 ixexceeds1; // argument x is greater than 1.0
69  int8 ixmapped; // argument in range tan(15 deg) to tan(45 deg)=1.0
70 
71 #define TAN15DEG 0.26794919243F // tan(15 deg) = 2 - sqrt(3)
72 #define TAN30DEG 0.57735026919F // tan(30 deg) = 1/sqrt(3)
73 
74  // reset all flags
75  ixisnegative = ixexceeds1 = ixmapped = 0;
76 
77  // test for negative argument to allow use of tan(-x)=-tan(x)
78  if (x < 0.0F)
79  {
80  x = -x;
81  ixisnegative = 1;
82  }
83 
84  // test for argument above 1 to allow use of atan(x)=pi/2-atan(1/x)
85  if (x > 1.0F)
86  {
87  x = 1.0F / x;
88  ixexceeds1 = 1;
89  }
90 
91  // at this point, x is in the range 0 to 1 inclusive
92  // map argument onto range -tan(15 deg) to tan(15 deg)
93  // using tan(angle-30deg) = (tan(angle)-tan(30deg)) / (1 + tan(angle)tan(30deg))
94  // tan(15deg) maps to tan(-15 deg) = -tan(15 deg)
95  // 1. maps to (sqrt(3) - 1) / (sqrt(3) + 1) = 2 - sqrt(3) = tan(15 deg)
96  if (x > TAN15DEG)
97  {
98  x = (x - TAN30DEG)/(1.0F + TAN30DEG * x);
99  ixmapped = 1;
100  }
101 
102  // call the atan estimator to obtain -15 deg <= angle <= 15 deg
103  fangledeg = fatan_15deg(x);
104 
105  // undo the distortions applied earlier to obtain -90 deg <= angle <= 90 deg
106  if (ixmapped) fangledeg += 30.0F;
107  if (ixexceeds1) fangledeg = 90.0F - fangledeg;
108  if (ixisnegative) fangledeg = -fangledeg;
109 
110  return (fangledeg);
111 }
112 
113 // function returns approximate atan2 angle in range -180 to 180 deg
114 // maximum error is 14.58E-6 deg
115 float fatan2_deg(float y, float x)
116 {
117  // check for zero x to avoid division by zero
118  if (x == 0.0F)
119  {
120  // return 90 deg for positive y
121  if (y > 0.0F) return 90.0F;
122  // return -90 deg for negative y
123  if (y < 0.0F) return -90.0F;
124  // otherwise y= 0.0 and return 0 deg (invalid arguments)
125  return 0.0F;
126  }
127 
128  // from here onwards, x is guaranteed to be non-zero
129  // compute atan2 for quadrant 1 (0 to 90 deg) and quadrant 4 (-90 to 0 deg)
130  if (x > 0.0F) return (fatan_deg(y / x));
131  // compute atan2 for quadrant 2 (90 to 180 deg)
132  if ((x < 0.0F) && (y > 0.0F)) return (180.0F + fatan_deg(y / x));
133  // compute atan2 for quadrant 3 (-180 to -90 deg)
134  return (-180.0F + fatan_deg(y / x));
135 
136 }
137 
138 // approximation to inverse tan function (deg) for x in range
139 // -tan(15 deg) to tan(15 deg) giving an output -15 deg <= angle <= 15 deg
140 // using modified Pade[3/2] approximation
141 float fatan_15deg(float x)
142 {
143  float x2; // x^2
144 
145 #define PADE_A 96.644395816F // theoretical Pade[3/2] value is 5/3*180/PI=95.49296
146 #define PADE_B 25.086941612F // theoretical Pade[3/2] value is 4/9*180/PI=25.46479
147 #define PADE_C 1.6867633134F // theoretical Pade[3/2] value is 5/3=1.66667
148 
149  // compute the approximation to the inverse tangent
150  // the function is anti-symmetric as required for positive and negative arguments
151  x2 = x * x;
152  return (x * (PADE_A + x2 * PADE_B) / (PADE_C + x2));
153 }
float fatan_deg(float x)
float fasin_deg(float x)
#define TAN30DEG
float fatan_15deg(float x)
#define PADE_C
#define TAN15DEG
#define PADE_A
#define PADE_B
float facos_deg(float x)
float fatan2_deg(float y, float x)
signed char int8
Definition: isf_types.h:72