math.h
Go to the documentation of this file.
1 //#############################################################################
2 //
3 // FILE: math.h
4 //
5 // TITLE: C28x math library (floating point)
6 //
7 //#############################################################################
8 // $TI Release: MotorControl SDK v1.00.00.00 $
9 // $Release Date: Mon Mar 11 18:37:40 CDT 2019 $
10 // $Copyright:
11 // Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
12 //
13 // Redistribution and use in source and binary forms, with or without
14 // modification, are permitted provided that the following conditions
15 // are met:
16 //
17 // Redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // Redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the
23 // distribution.
24 //
25 // Neither the name of Texas Instruments Incorporated nor the names of
26 // its contributors may be used to endorse or promote products derived
27 // from this software without specific prior written permission.
28 //
29 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 // $
41 //#############################################################################
42 
43 #ifndef TI_MATH_H
44 #define TI_MATH_H
45 
46 //*****************************************************************************
47 //
48 // If building with a C++ compiler, make all of the definitions in this header
49 // have a C binding.
50 //
51 //*****************************************************************************
52 #ifdef __cplusplus
53 extern "C"
54 {
55 #endif
56 
57 //*****************************************************************************
58 //
61 //
62 //*****************************************************************************
63 
64 #ifndef __TMS320C28XX_CLA__
65 #include <math.h>
66 #endif
67 
69 
70 //*****************************************************************************
71 //
73 //
74 //*****************************************************************************
75 #define MATH_Nm_TO_lbin_SF ((float32_t)(8.8507457913f))
76 
77 //*****************************************************************************
78 //
80 //
81 //*****************************************************************************
82 #define MATH_TWO_OVER_THREE ((float32_t)(0.6666666666666666666666666667f))
83 
84 //*****************************************************************************
85 //
87 //
88 //*****************************************************************************
89 #define MATH_ONE_OVER_THREE ((float32_t)(0.3333333333333333333333333333f))
90 
91 //*****************************************************************************
92 //
94 //
95 //*****************************************************************************
96 #define MATH_ONE_OVER_PI ((float32_t)(0.318309886183791f))
97 
98 
99 //*****************************************************************************
100 //
102 //
103 //*****************************************************************************
104 #define MATH_SQRT_TWO ((float32_t)(1.414213562373095f))
105 
106 
107 //*****************************************************************************
108 //
110 //
111 //*****************************************************************************
112 #define MATH_SQRT_THREE ((float32_t)(1.73205080756887f))
113 
114 //*****************************************************************************
115 //
117 //
118 //*****************************************************************************
119 #define MATH_ONE_OVER_SQRT_THREE ((float32_t)(0.5773502691896257645091487805f))
120 
121 //*****************************************************************************
122 //
124 //
125 //*****************************************************************************
126 #define MATH_ONE_OVER_FOUR_PI ((float32_t)(0.07957747154594767f))
127 
128 //*****************************************************************************
129 //
131 //
132 //*****************************************************************************
133 #define MATH_ONE_OVER_TWO_PI ((float32_t) (0.1591549430918954f))
134 
135 //*****************************************************************************
136 //
138 //
139 //*****************************************************************************
140 #define MATH_PI ((float32_t)(3.1415926535897932384626433832f))
141 
142 //*****************************************************************************
143 //
145 //
146 //*****************************************************************************
147 #define MATH_PI_PU ((float32_t)(0.5f))
148 
149 //*****************************************************************************
150 //
152 //
153 //*****************************************************************************
154 #define MATH_TWO_PI ((float32_t)(6.283185307179586f))
155 
156 //*****************************************************************************
157 //
159 //
160 //*****************************************************************************
161 #define MATH_TWO_PI_PU ((float32_t)(1.0f))
162 
163 //*****************************************************************************
164 //
166 //
167 //*****************************************************************************
168 #define MATH_FOUR_PI ((float32_t)(12.56637061435917f))
169 
170 //*****************************************************************************
171 //
173 //
174 //*****************************************************************************
175 #define MATH_FOUR_PI_PU ((float32_t)(2.0f))
176 
177 //*****************************************************************************
178 //
180 //
181 //*****************************************************************************
182 #define MATH_PI_OVER_TWO ((float32_t)(1.570796326794897f))
183 
184 //*****************************************************************************
185 //
187 //
188 //*****************************************************************************
189 #define MATH_PI_OVER_TWO_PU ((float32_t)(0.25f))
190 
191 
192 //*****************************************************************************
193 //
195 //
196 //*****************************************************************************
197 #define MATH_PI_OVER_THREE ((float32_t)(1.047197551196598f))
198 
199 //*****************************************************************************
200 //
202 //
203 //*****************************************************************************
204 #define MATH_PI_OVER_FOUR ((float32_t)(0.785398163397448f))
205 
206 //*****************************************************************************
207 //
209 //
210 //*****************************************************************************
211 #define MATH_PI_OVER_FOUR_PU ((float32_t)(0.125f))
212 
213 //*****************************************************************************
214 //
216 //
217 //*****************************************************************************
218 typedef struct _MATH_Vec2_
219 {
221 } MATH_Vec2;
222 
224 
225 //*****************************************************************************
226 //
228 //
229 //*****************************************************************************
230 typedef struct _MATH_Vec3_
231 {
233 } MATH_Vec3;
234 
236 
237 //*****************************************************************************
238 //
244 //
245 //*****************************************************************************
246 static inline
248 {
249  float32_t out = in;
250 
251  if(in < (float32_t)0.0f)
252  {
253  out = -in;
254  }
255 
256  return(out);
257 } // end of MATH_abs() function
258 
259 
260 //*****************************************************************************
261 //
267 //
268 //*****************************************************************************
269 static inline
270 float32_t MATH_max(const float32_t in1, const float32_t in2)
271 {
272  float32_t out = in1;
273 
274 
275  if(in1 < in2)
276  {
277  out = in2;
278  }
279 
280  return(out);
281 } // end of MATH_max() function
282 
283 
284 //*****************************************************************************
285 //
291 //
292 //*****************************************************************************
293 static inline
294 float32_t MATH_min(const float32_t in1, const float32_t in2)
295 {
296  float32_t out = in1;
297 
298 
299  if(in1 > in2)
300  {
301  out = in2;
302  }
303 
304  return(out);
305 } // end of MATH_min() function
306 
307 //*****************************************************************************
308 //
316 //
317 //*****************************************************************************
318 #ifdef __TMS320C28XX_CLA__
319 #pragma FUNC_ALWAYS_INLINE(MATH_incrAngle)
320 #endif
321 
322 static inline float32_t
323 MATH_incrAngle(const float32_t angle_rad, const float32_t angleDelta_rad)
324 {
325  float32_t angleNew_rad;
326 
327  //
328  // Increment the angle
329  //
330  angleNew_rad = angle_rad + angleDelta_rad;
331 
332  //
333  // Check for limits
334  //
335 // if(angleNew_rad > MATH_PI)
336 // {
337 // angleNew_rad -= MATH_TWO_PI;
338 // }
339 // else if(angleNew_rad < (-MATH_PI))
340 // {
341 // angleNew_rad += MATH_TWO_PI;
342 // }
343 
344  angleNew_rad = (angleNew_rad > MATH_PI) ? angleNew_rad - MATH_TWO_PI : angleNew_rad;
345  angleNew_rad = (angleNew_rad < -MATH_PI) ? angleNew_rad + MATH_TWO_PI : angleNew_rad;
346 
347  return(angleNew_rad);
348 } // end of MATH_incrAngle() function
349 
350 //*****************************************************************************
351 //
361 //
362 //*****************************************************************************
363 static inline float32_t
364 MATH_sat(const float32_t in, const float32_t max, const float32_t min)
365 {
366  float32_t out = in;
367 
368 // if(in < min)
369 // {
370 // out = min;
371 // }
372 // else if(in > max)
373 // {
374 // out = max;
375 // }
376 
377  out = (out > max) ? max : out;
378  out = (out < min) ? min : out;
379 
380  return(out);
381 } // end of MATH_sat() function
382 
383 //----------------------------------------------------------------------------
384 // For Motor Fault Diagnostic
385 //-----------------------------------------------------------------------------
386 //*****************************************************************************
387 //
389 //
390 //*****************************************************************************
391 typedef struct _MATH_cplx_vec2_
392 {
393 
395 
397 
398 //*****************************************************************************
399 //
401 //
402 //*****************************************************************************
403 typedef struct _MATH_cplx_vec3_
404 {
405 
407 
409 
410 //*****************************************************************************
411 //
413 //
414 //*****************************************************************************
415 #define MATH_SIX_PI ((float32_t)(18.8495559215f))
416 
417 //*****************************************************************************
418 //
420 //
421 //*****************************************************************************
422 #define MATH_SQRTTHREE_OVER_TWO ((float32_t)(0.8660254038f))
423 
424 
425 //*****************************************************************************
426 //
432 //
433 //*****************************************************************************
434 #ifdef __TMS320C28XX_CLA__
435 static inline
436 float32_t cAbsSq(const cplx_float_t* x1)
437 {
438  float32_t y;
439 
440  y = 0.0f;
441 
442  return y;
443 }
444 #else
445 static inline
447 {
448  float32_t y;
449 
450  #ifdef __TMS320C28XX_TMU__
451  y = pow(x1->real,2) + pow(x1->imag,2);
452  #else
453  y = (float32_t)(pow((double_t)(x1->real),(double_t)(2.0)) +
454  pow((double_t)(x1->imag),(double_t)(2.0)));
455  #endif // __TMS320C28XX_TMU__
456 
457  return y;
458 }
459 #endif // __TMS320C28XX_CLA__
460 
461 
462 static inline
464 {
465  float32_t out = 1.0f;
466 
467 
468  if(in < 0.0f)
469  {
470  out = -1.0f;
471  }
472 
473  return(out);
474 } // end of MATH_sign() function
475 
476 static inline void
478 {
479  y->real = x1->real * x2->real - x1->imag * x2->imag;
480  y->imag = x1->real * x2->imag + x1->imag * x2->real;
481  return;
482 }
483 
484 static inline void
486 {
487  y->real = x1->real + x2->real;
488  y->imag = x1->imag + x2->imag;
489  return;
490 }
491 
492 static inline void
494 {
495  y->real = x1->real - x2->real;
496  y->imag = x1->imag - x2->imag;
497  return;
498 }
499 
500 static inline void
502 {
503  y->real = x1 * x2->real;
504  y->imag = x1 * x2->imag;
505  return;
506 }
507 
508 static inline void
510 {
511  y->real = x1 + x2->real;
512  y->imag = x2->imag;
513  return;
514 }
515 
516 static inline void
518 {
519  y->real = x1 - x2->real;
520  y->imag = - x2->imag;
521  return;
522 }
523 
524 static inline void
526 {
527  y->real = x1->real - x2;
528  y->imag = x1->imag;
529 
530  return;
531 }
532 
533 
534 
535 //*****************************************************************************
536 //
537 // Close the Doxygen group.
539 //
540 //*****************************************************************************
541 
542 //*****************************************************************************
543 //
544 // Mark the end of the C bindings section for C++ compilers.
545 //
546 //*****************************************************************************
547 #ifdef __cplusplus
548 }
549 #endif
550 
551 #endif // TI_MATH_H
_MATH_cplx_vec2_::value
cplx_float_t value[2]
Definition: math.h:394
_MATH_Vec2_
Defines a two element vector.
Definition: math.h:218
float32_t
float float32_t
Definition: sfra_f32.h:42
MATH_TWO_PI
#define MATH_TWO_PI
Defines 2*pi.
Definition: math.h:154
types.h
_MATH_cplx_vec3_
Defines a three element vector.
Definition: math.h:403
MATH_add_cc
static void MATH_add_cc(cplx_float_t *x1, cplx_float_t *x2, cplx_float_t *y)
Definition: math.h:485
_MATH_cplx_vec2_
Defines a two element vector.
Definition: math.h:391
MATH_incrAngle
static float32_t MATH_incrAngle(const float32_t angle_rad, const float32_t angleDelta_rad)
Increments an angle value and handles wrap-around.
Definition: math.h:323
MATH_PI
#define MATH_PI
Defines pi.
Definition: math.h:140
_MATH_cplx_vec3_::value
cplx_float_t value[3]
Definition: math.h:406
MATH_Vec3
struct _MATH_Vec3_ MATH_Vec3
Defines a three element vector.
MATH_vec2
MATH_Vec2 MATH_vec2
Definition: math.h:223
MATH_mult_rc
static void MATH_mult_rc(float32_t x1, cplx_float_t *x2, cplx_float_t *y)
Definition: math.h:501
MATH_sub_cr
static void MATH_sub_cr(cplx_float_t *x1, float32_t x2, cplx_float_t *y)
Definition: math.h:525
math.h
double_t
double double_t
Defines the portable data type for 64 bit, signed floating-point data.
Definition: types.h:162
MATH_sub_cc
static void MATH_sub_cc(cplx_float_t *x1, cplx_float_t *x2, cplx_float_t *y)
Definition: math.h:493
MATH_abs
static float32_t MATH_abs(const float32_t in)
Finds the absolute value.
Definition: math.h:247
_MATH_Vec3_::value
float32_t value[3]
Definition: math.h:232
MATH_max
static float32_t MATH_max(const float32_t in1, const float32_t in2)
Finds the maximum value between the twp input values.
Definition: math.h:270
MATH_sign
static float32_t MATH_sign(const float32_t in)
Definition: math.h:463
MATH_Vec2
struct _MATH_Vec2_ MATH_Vec2
Defines a two element vector.
MATH_add_rc
static void MATH_add_rc(float32_t x1, cplx_float_t *x2, cplx_float_t *y)
Definition: math.h:509
_cplx_float_t::imag
float_t imag
Definition: types.h:308
MATH_sat
static float32_t MATH_sat(const float32_t in, const float32_t max, const float32_t min)
Saturates the input value between the minimum and maximum values.
Definition: math.h:364
MATH_mult_cc
static void MATH_mult_cc(cplx_float_t *x1, cplx_float_t *x2, cplx_float_t *y)
Definition: math.h:477
MATH_cplx_vec2
struct _MATH_cplx_vec2_ MATH_cplx_vec2
Defines a two element vector.
_cplx_float_t
Define the complex data type for float real and imaginary components.
Definition: types.h:305
MATH_vec3
MATH_Vec3 MATH_vec3
Definition: math.h:235
MATH_sub_rc
static void MATH_sub_rc(float32_t x1, cplx_float_t *x2, cplx_float_t *y)
Definition: math.h:517
cAbsSq
static float32_t cAbsSq(const cplx_float_t *x1)
Finds the absolute value.
Definition: math.h:446
_cplx_float_t::real
float_t real
Definition: types.h:307
MATH_cplx_vec3
struct _MATH_cplx_vec3_ MATH_cplx_vec3
Defines a three element vector.
MATH_min
static float32_t MATH_min(const float32_t in1, const float32_t in2)
Finds the minimum value between the twp input values.
Definition: math.h:294
_MATH_Vec2_::value
float32_t value[2]
Definition: math.h:220
_MATH_Vec3_
Defines a three element vector.
Definition: math.h:230

Copyright 2023, Texas Instruments Incorporated