-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.h
More file actions
361 lines (271 loc) · 9.56 KB
/
main.h
File metadata and controls
361 lines (271 loc) · 9.56 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#ifndef _MAIN_H_
#define _MAIN_H_
/* --COPYRIGHT--,BSD
* Copyright (c) 2012, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* --/COPYRIGHT--*/
//! \file solutions/instaspin_foc/src/main.h
//! \brief Defines the structures, global initialization, and functions used in MAIN
//!
//! (C) Copyright 2011, Texas Instruments, Inc.
// **************************************************************************
// the includes
// modules
#include <mode09.h>
#include "math.h"
#include "memCopy.h"
#include "est.h"
#include "svgen_current.h"
#include "fw.h"
#include "fem.h"
#include "cpu_usage.h"
// drivers
#include "F2802x_Device.h"
#include "f2802x_examples.h"
// platforms
#include "ctrl.h"
#include "hal.h"
#include "user.h"
//misc
#include "cadence.h"
#include "speed.h"
#include "Message.h"
#include "mode01.h"
#include "mode08.h"
#include "mode21.h"
// **************************************************************************
// the defines
//! \brief Defines the number of main iterations before global variables are updated
//!
#define NUM_MAIN_TICKS_FOR_GLOBAL_VARIABLE_UPDATE 10
//! \brief Defines the speed acceleration scale factor.
//!
#define MAX_ACCEL_KRPMPS_SF _IQ(USER_MOTOR_NUM_POLE_PAIRS*1000.0/USER_TRAJ_FREQ_Hz/USER_IQ_FULL_SCALE_FREQ_Hz/60.0)
//! \brief Initialization values of global variables
//!
#define MOTOR_Vars_INIT {true, \
false, \
false, \
true, \
false, \
false, \
true, \
true, \
false, \
false, \
CTRL_State_Idle, \
EST_State_Idle, \
USER_ErrorCode_NoError, \
{0,CTRL_TargetProc_Unknown,0,0}, \
_IQ(0.0), \
_IQ(0.0), \
_IQ(0.0), \
_IQ(0.1), \
_IQ(0.0), \
_IQ(0.2), \
_IQ(0.0), \
_IQ(USER_MAX_VS_MAG_PU), \
_IQ(0.1 * USER_MOTOR_MAX_CURRENT), \
400, \
_IQ(0.0), \
_IQ(0.0), \
0.0, \
0.0, \
0.0, \
0.0, \
0.0, \
0.0, \
0.0, \
0.0, \
_IQ(0.0), \
_IQ(0.0), \
_IQ(0.0), \
0.0, \
0.0, \
_IQ(0.0), \
_IQ(0.0), \
_IQ(0.0), \
_IQ(0.0), \
_IQ(0.0), \
_IQ(0.0), \
_IQ(0.0), \
_IQ(0.8 * USER_MAX_VS_MAG_PU), \
_IQ(0.0), \
_IQ(0.0), \
_IQ(0.0), \
_IQ17(0.0), \
_IQ30(0.0), \
_IQ(0.0), \
_IQ(0.0), \
_IQ(0.0), \
{0,0,0}, \
{0,0,0}, \
_IQ(0.0)}
// **************************************************************************
// the typedefs
typedef struct _MOTOR_Vars_t_
{
bool Flag_enableSys;
bool Flag_Run_Identify;
bool Flag_MotorIdentified;
bool Flag_enableForceAngle;
bool Flag_enableFieldWeakening;
bool Flag_enableRsRecalc;
bool Flag_enableUserParams;
bool Flag_enableOffsetcalc;
bool Flag_enablePowerWarp;
bool Flag_enableSpeedCtrl;
CTRL_State_e CtrlState;
EST_State_e EstState;
USER_ErrorCode_e UserErrorCode;
CTRL_Version CtrlVersion;
_iq IdRef_A;
_iq IqRef_A;
_iq SpeedRef_pu;
_iq SpeedRef_krpm;
_iq SpeedTraj_krpm;
_iq MaxAccel_krpmps;
_iq Speed_krpm;
_iq OverModulation;
_iq RsOnLineCurrent_A;
_iq SvgenMaxModulation_ticks;
_iq Flux_Wb;
_iq Torque_Nm;
float_t MagnCurr_A;
float_t Rr_Ohm;
float_t Rs_Ohm;
float_t RsOnLine_Ohm;
float_t Lsd_H;
float_t Lsq_H;
float_t Flux_VpHz;
float_t ipd_excFreq_Hz;
_iq ipd_Kspd;
_iq ipd_excMag_coarse_pu;
_iq ipd_excMag_fine_pu;
float ipd_waitTime_coarse_sec;
float ipd_waitTime_fine_sec;
_iq Kp_spd;
_iq Ki_spd;
_iq Kp_Idq;
_iq Ki_Idq;
_iq Vd;
_iq Vq;
_iq Vs;
_iq VsRef;
_iq Vdc;
_iq Vdc_v0;
_iq Idc;
_iq17 mAh;
_iq30 kWh;
_iq Id_A;
_iq Iq_A;
_iq Is_A;
MATH_vec3 I_bias;
MATH_vec3 V_bias;
_iq Idc_bias;
}MOTOR_Vars_t;
// **************************************************************************
// the globals
// **************************************************************************
// the function prototypes
//! \brief The main interrupt service (ISR) routine
//!
interrupt void mainISR(void);
interrupt void sciaRxFifoIsr(void);
void runCurrentIgnore(void);
void runCurrentReconstruction(void);
void runFieldWeakening(void);
void runOffsetsCalculation(void);
void runSetTrigger(void);
//! \brief Updates the global motor variables
//!
void updateGlobalVariables_motor(CTRL_Handle handle);
void readSensorsCallback();
//! \brief Updates the global variables
//!
void updateGlobalVariables(EST_Handle handle);
//! \brief Updates version 1p6 of library
//!
void softwareUpdate1p6(EST_Handle handle);
//! \brief Reset Ls Q format to a higher value when Ls identification starts
//!
void CTRL_resetLs_qFmt(CTRL_Handle handle, const uint_least8_t qFmt);
//! \brief Recalculate Kp and Ki gains to fix the R/L limitation of 2000.0 and Kp limitation of 0.11
//! \brief as well as recalculates gains based on estimator state to allow low inductance pmsm to id
//!
void recalcKpKiPmsm(CTRL_Handle handle);
//! \brief Recalculate Kp and Ki gains to fix the R/L limitation of 2000.0 and Kp limitation of 0.11
//!
void recalcKpKi(CTRL_Handle handle);
//! \brief Calculates the maximum qFmt value for Ls identification, to get a more accurate Ls per unit
//!
void CTRL_calcMax_Ls_qFmt(CTRL_Handle handle, uint_least8_t *p_qFmt);
//! \brief Updates Iq reference and also sets the right sign to the speed reference for correct force angle
//!
void updateIqRef(CTRL_Handle handle);
//! \brief Updates Kp and Ki gains in the controller object
//!
void updateKpKiGains(CTRL_Handle handle);
//! \brief Runs Rs online
//!
void runRsOnLine(CTRL_Handle handle);
//! \brief Updates CPU usage
//!
void updateCPUusage(void);
//! \brief Set electrical frequency limit to zero while identifying an induction motor
//!
void setFeLimitZero(CTRL_Handle handle);
//! \brief Calculates Dir_qFmt for ACIM
//!
void acim_Dir_qFmtCalc(CTRL_Handle handle);
//! \brief Sets up the Clarke transform for current
//!
void setupClarke_I(CLARKE_Handle,const uint_least8_t);
//! \brief Sets up the Clarke transform for voltage
//!
void setupClarke_V(CLARKE_Handle,const uint_least8_t);
_iq angleDelayComp(const _iq fm_pu, const _iq angleUncomp_pu);
CTRL_Handle CTRL_init(void *pMemory,const size_t numBytes);
void EST_getIdq_pu(EST_Handle handle,MATH_vec2 *pIdq_pu);
#if !defined(FAST_ROM_V1p6)
void EST_setEstParams(EST_Handle handle,USER_Params *pUserParams);
void EST_setupEstIdleState(EST_Handle handle);
#endif
EST_Handle EST_init(void *pMemory,const size_t numBytes);
//! \brief Runs PowerWarp
//! \param[in] handle The estimator (EST) handle
//! \param[in] Id_int_pu The intermediate value along the Id trajectory in pu
//! \param[in] Iq_pu The measured Iq value in pu
//! \return The target value for the Id trajectory in pu
_iq EST_runPowerWarp(EST_Handle handle,const _iq Id_int_pu,const _iq Iq_pu);
_iq getAbsElecAngle(const _iq angle_pu);
_iq getAbsMechAngle(_iq *pAngle_mech_poles, _iq *pAngle_z1_pu, const _iq angle_pu);
//@} //defgroup
#endif // end of _MAIN_H_ definition