-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdigitalControl.h
195 lines (167 loc) · 6.18 KB
/
digitalControl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//------------------------------------------------------------------------------
#ifndef __CONTROLLERS_H
#define __CONTROLLERS_H
//------------------------------------------------------------------------------
// LIBRARIES
//------------------------------------------------------------------------------
#include <stdbool.h>
#include <stdint.h>
//------------------------------------------------------------------------------
// DEFINITIONS
//------------------------------------------------------------------------------
#define EPSILON 0.01
//------------------------------------------------------------------------------
// CUSTOM TYPES
//------------------------------------------------------------------------------
typedef struct
{
float max;
float min;
} Saturator;
typedef struct
{
float upTime;
uint32_t upCounter;
float finalValue;
float initialValue;
float previousOutput;
float deltaTime;
// Constants
float samplingTime;
} RampGenerator;
// Integrator (Tustin's method)
typedef struct
{
float previousInput;
float previousOutput;
// Constants
float samplingTime;
} Integrator;
// Differentiator (Backward method)
typedef struct
{
float previousInput;
// Constants
float samplingTime;
} Differentiator;
typedef struct
{
float previousInput;
float previousOutput;
// Constants
float samplingTime;
float coef[3];
} FirstOrderFilter;
/* This is meant to be the minimalistic implementation
* of a first order filter, there is only one pole and
* it uses the simpler forward discretization method
*/
typedef struct
{
float previousOutput;
// Constants
float samplingTime;
float coef[2];
} SimplifiedFirstOrderFilter;
typedef struct
{
float controlSignal;
float previousError;
bool useSaturator;
Saturator sat;
// Constants
float samplingTime;
float Kc;
float zc;
} PI;
typedef struct
{
float feedBack[3];
float error[3];
float controlSignal[3];
bool useSaturator;
Saturator sat;
bool usePreFilter;
float spFiltered;
FirstOrderFilter preFilter;
// Constants
float samplingTime;
float coef[8];
} PI_D;
// 1st order observer
typedef struct
{
float previousInput;
float previousOutput;
// Constants
float samplingTime;
float Kd;
float zd;
} FirstOrderObserver;
typedef struct
{
PI piIqs;
PI piIds;
PI piIdm;
FirstOrderObserver IdmObserver;
bool useFeedforward;
float theta;
// Constants
float samplingTime;
float delta;
float Lsigmas;
float eta;
float gamma;
float Km;
} IFOC;
//------------------------------------------------------------------------------
// FUNCTION PROTOTYPES
//------------------------------------------------------------------------------
void saturatorSet(Saturator *this, const float max, const float min);
float applySaturator(Saturator *this, const float in);
void rampInit(RampGenerator *this, const float samplingTime);
void rampReset(RampGenerator *this);
void rampSet(RampGenerator *this, const float upTime, const float finalValue);
float calcRamp(RampGenerator *this);
void integratorInit(Integrator *this, const float samplingTime);
void integratorReset(Integrator *this);
float integrate(Integrator *this, const float input);
void diffInit(Differentiator *this, const float samplingTime);
void diffReset(Differentiator *this);
float differentiate(Differentiator *this, const float input);
void filterInit(FirstOrderFilter *this, const float zero, const float pole, const float samplingTime);
void filterReset(FirstOrderFilter *this);
void filterDiscreet(FirstOrderFilter *this, const float zero, const float pole);
float filterProcess(FirstOrderFilter *this, const float input);
/* This is meant to be the minimalistic implementation
* of a first order filter. There is only one pole and
* it uses the simpler forward discretization method
*/
void sFilterInit(SimplifiedFirstOrderFilter *this, const float pole, const float samplingTime);
void sFilterReset(SimplifiedFirstOrderFilter *this);
void sFilterDiscreet(SimplifiedFirstOrderFilter *this, const float pole);
float sFilterProcess(SimplifiedFirstOrderFilter *this, const float input);
void piInit(PI *this, const float samplingTime, const bool useSaturator);
void piReset(PI *this);
void piDiscreet(PI *this, const float Kp, const float Ti);
void piPoleZeroCancelationProject(PI *this, const float wcl, const float Kol, const float wol);
void piClosedLoopResponseProject(PI *this, const float Mov, const float ts2, const float Kol, const float wol);
float piControl(PI *this, const float setPoint, const float feedBack);
void pidInit(PI_D *this, const float samplingTime, const bool useSaturator, const bool usePreFilter);
void pidReset(PI_D *this);
void pidDiscreet(PI_D *this, const float Kp, const float Ti, const float Td, const float N);
void pidClosedLoopResponseProject(PI_D *this, const float Mov, const float ts2, const float Kol, const float wol);
float pidControl(PI_D *this, const float setPoint, const float feedBack);
void observerInit(FirstOrderObserver *this, const float samplingTime);
void observerReset(FirstOrderObserver *this);
void observerProject(FirstOrderObserver *this, const float Kol, const float wol);
float observerProcess(FirstOrderObserver *this, const float input);
void ifocInit(IFOC *this, const float samplingTime, const bool useSaturator, const bool useFeedforward);
void ifocReset(IFOC *this);
void ifocSetSaturators(IFOC *this, const float max_voltage, const float max_ids, const float min_ids);
void ifocProject(IFOC *this, const uint16_t p, const float fn, const float rs, const float rr, const float Xls, const float Xlr, const float Xm);
void ifocControl(IFOC *this, const float spTe, const float spIdm, const float currentA, const float currentB, const float wr, float *const uBeta, float *const uAlpha);
#endif // __CONTROLLERS_H
//------------------------------------------------------------------------------
// END
//------------------------------------------------------------------------------