SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
foc_utils.h
Go to the documentation of this file.
1#ifndef FOCUTILS_LIB_H
2#define FOCUTILS_LIB_H
3
4#include "Arduino.h"
5
6// sign function
7#define _sign(a) ( ( (a) < 0 ) ? -1 : ( (a) > 0 ) )
8#ifndef _round
9#define _round(x) ((x)>=0?(long)((x)+0.5f):(long)((x)-0.5f))
10#endif
11#define _constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
12#define _sqrt(a) (_sqrtApprox(a))
13#define _isset(a) ( (a) != (NOT_SET) )
14#define _UNUSED(v) (void) (v)
15#define _powtwo(x) (1 << (x))
16
17#define _swap(a, b) { auto temp = a; a = b; b = temp; }
18
19// utility defines
20#define _2_SQRT3 1.15470053838f
21#define _SQRT3 1.73205080757f
22#define _1_SQRT3 0.57735026919f
23#define _SQRT3_2 0.86602540378f
24#define _SQRT2 1.41421356237f
25#define _120_D2R 2.09439510239f
26#define _PI 3.14159265359f
27#define _PI_2 1.57079632679f
28#define _PI_3 1.0471975512f
29#define _2PI 6.28318530718f
30#define _3PI_2 4.71238898038f
31#define _PI_6 0.52359877559f
32#define _RPM_TO_RADS 0.10471975512f
33
34#define NOT_SET -12345.0f
35#define _HIGH_IMPEDANCE 0
36#define _HIGH_Z _HIGH_IMPEDANCE
37#define _ACTIVE 1
38#define _NC ((int) NOT_SET)
39
40#define MIN_ANGLE_DETECT_MOVEMENT (_2PI/101.0f)
41
42// dq variables
43struct DQ_s
44{
45 float d;
46 float q;
47};
48
49// dq voltage structs
51// dq current structs
53
54// alpha-beta variables
55struct AB_s
56{
57 float alpha;
58 float beta;
59};
60typedef AB_s ABVoltage_s; // NOT USED
62
63// phase structs
64struct Phase_s
65{
66 float a;
67 float b;
68 float c;
69};
70typedef Phase_s PhaseVoltage_s; // NOT USED
72
73
74/**
75 * Function approximating the sine calculation by using fixed size array
76 * - execution time ~40us (Arduino UNO)
77 *
78 * @param a angle in between 0 and 2PI
79 */
80float _sin(float a);
81/**
82 * Function approximating cosine calculation by using fixed size array
83 * - execution time ~50us (Arduino UNO)
84 *
85 * @param a angle in between 0 and 2PI
86 */
87float _cos(float a);
88/**
89 * Function returning both sine and cosine of the angle in one call.
90 * Internally it uses the _sin and _cos functions, but you may wish to
91 * provide your own implementation which is more optimized.
92 */
93void _sincos(float a, float* s, float* c);
94
95/**
96 * Function approximating atan2
97 *
98 */
99float _atan2(float y, float x);
100
101/**
102 * normalizing radian angle to [0,2PI]
103 * @param angle - angle to be normalized
104 */
106
107
108/**
109 * Electrical angle calculation
110 *
111 * @param shaft_angle - shaft angle of the motor
112 * @param pole_pairs - number of pole pairs
113 */
114float _electricalAngle(float shaft_angle, int pole_pairs);
115
116/**
117 * Function approximating square root function
118 * - using fast inverse square root
119 *
120 * @param value - number
121 */
122float _sqrtApprox(float value);
123
124#endif
@ angle
Position/angle motion control.
Definition FOCMotor.h:52
float a
Definition foc_utils.cpp:59
float * s
Definition foc_utils.cpp:43
float float * c
Definition foc_utils.cpp:43
float x
Definition foc_utils.cpp:54
Phase_s PhaseCurrent_s
Definition foc_utils.h:71
float _sqrtApprox(float value)
float _atan2(float y, float x)
AB_s ABCurrent_s
Definition foc_utils.h:61
float _sin(float a)
DQ_s DQVoltage_s
Definition foc_utils.h:50
DQ_s DQCurrent_s
Definition foc_utils.h:52
float _electricalAngle(float shaft_angle, int pole_pairs)
Definition foc_utils.cpp:83
Phase_s PhaseVoltage_s
Definition foc_utils.h:70
float _normalizeAngle(float angle)
float _cos(float a)
void _sincos(float a, float *s, float *c)
AB_s ABVoltage_s
Definition foc_utils.h:60
float alpha
Definition foc_utils.h:57
float beta
Definition foc_utils.h:58
float q
Definition foc_utils.h:46
float d
Definition foc_utils.h:45
float c
Definition foc_utils.h:68
float a
Definition foc_utils.h:66
float b
Definition foc_utils.h:67