forked from GrandviewIoT/MotorLib_Embedded
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotorLib_Bldc_FOC.c
More file actions
416 lines (367 loc) · 16.8 KB
/
Copy pathMotorLib_Bldc_FOC.c
File metadata and controls
416 lines (367 loc) · 16.8 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//*******1*********2*********3*********4*********5*********6*********7**********
//
// MotorLib_Bldc_FOC.c
//
// Motor Control Library: High Level support for FOC
// BLDC support.
//
// This is a portable code library, designed to work across a number of
// processors, including: TI Tiva Cortex-M
// TI MSP432 with Cortex-M core
// TI MSP430 F5529, FR5969, FR6989, FR5994
// STM32 F3, F4, L4, L7 Cortex-M series
//
// Low level detailed support is provided by the associated MCU platform's
// MotorLib_LL_xxxx_yyyy.c module, where xxxx = Chip type and
// yyyyy = MCU type, e.g. MotorLib_LL_L6230_STM32.c
/*******************************************************************************
*
* Filename: foc.c
* Owner : Luffy Li (lufl@cypress.com)
*
* Version: V1.0
* Description: Sensorless Foc Motor Control
* Support 2-shunt sensorless FOC
* -------------------------------------------------------------------------------
* ChangeList:
* V1.0 Initial version
* -------------------------------------------------------------------------------
* Known issues:
* V1.0 N/A
* -------------------------------------------------------------------------------
* Hardare Dependency:
* 1. CY8CKIT-042(MCU board) + CY8CKIT-037(Driver Board)
* -------------------------------------------------------------------------------
* Related documents:
* N/A
* -------------------------------------------------------------------------------
* Code Tested with: PSoC Creator 3.2 (3.2.0.724)
* ARM GCC 4.8.4;
*
********************************************************************************
* Copyright (2014), Cypress Semiconductor Corporation.
********************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its products for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
*******************************************************************************/
#include <project.h>
#include "math.h"
#include "Cymc.h"
#define ADC_IA_CHAN (uint8) (0)
#define ADC_IB_CHAN (uint8) (1)
#define ADC_VBUS_CHAN (uint8) (2)
#define ADC_VR_CHAN (uint8) (3)
#define ALARMCURRENT (int16)(MAX_PHASE_CURRENT*ADC_RESOLUTION*SENSING_RESISTOR*OPAMP_GAIN/VDD)
#define MIN_SPEED_STEP _FQ(0.02)
#define SLOW_REGULATION _FQ(0.1)
#define MIDDLE_REGULATION _FQ(0.2)
#define FAST_REGULATION _FQ(0.4)
#define VBUS_LOW 20 /* 15V */
#define VBUS_HIGH 30 /* 30V */
#define SEC_COUNTER 5000 /* 500ms */
uint16 speedLoopCount = 1;
/* phase current U, V in q15 format */
q15_t phaseCurrentU = 0, phaseCurrentV = 0;
q15_t phaseCurrentU0 = 0, phaseCurrentV0 = 0;
/* current angle in q15 format */
q15_t curAngle = 0;
/* seconds counter*/
uint16 pwmCounter = 0;
uint8 secFlag = 0;
/****************************************************************************************
* *
* Function: updateSpeedrefFromVR *
* *
* Description: update speed reference *
* *
* Paramters: None *
* *
* Returns: None *
* *
****************************************************************************************/
void updateSpeedrefFromVR()
{
q15_t temp;
q15_t refTemp;
if (motor.runState != M_SPEEDLOOP)
return;
refTemp = adcRawData[ADC_VR_CHAN] << 3;
refTemp = Cymc_GFL_Mul_Q15(refTemp,_FQ(1.1));
temp = refTemp - motor.speedRef;
if (temp < 0)
temp = -temp;
if (temp > MIN_SPEED_STEP)
motor.speedRef = refTemp;
temp = motor.speedRef - rampUp.output;
if (temp < 0)
temp = -temp; // flip from negative to positive
if (temp > SLOW_REGULATION)
{
rampUp.delayPrescaler = SLOW_RAMPUP_DELAY;
}
else if(temp > MIDDLE_REGULATION)
{
rampUp.delayPrescaler = MIDDLE_RAMPUP_DELAY;
}
else if(temp > FAST_REGULATION)
{
rampUp.delayPrescaler = FAST_RAMPUP_DELAY;
}
else
{
rampUp.delayPrescaler = DEFAULT_RAMPUP_DELAY;
}
if (motor.speedRef > SPEEDREF_MAX)
motor.speedRef = SPEEDREF_MAX;
else if (motor.speedRef < SPEEDREF_MIN)
motor.speedRef = SPEEDREF_MIN;
}
/****************************************************************************************
* *
* Function: phaseCurrentOverProtect *
* *
* Description: phase current over protection *
* *
* Paramters: None *
* *
* Returns: None *
* *
****************************************************************************************/
void phaseCurrentOverProtect()
{
if (phaseCurrentU > ALARMCURRENT || phaseCurrentU < -ALARMCURRENT)
{
errState |= ERR_OVER_CURRENT;
Cymc_MAL_MotorStop();
}
if (phaseCurrentV > ALARMCURRENT || phaseCurrentV < -ALARMCURRENT )
{
Cymc_MAL_MotorStop();
errState |= ERR_OVER_CURRENT;
}
}
/****************************************************************************************
* *
* Function: VbusProtect *
* *
* Description: Under and Over Voltage protection *
* *
* Paramters: None *
* *
* Returns: None *
* *
****************************************************************************************/
void VbusProtect()
{
uint16 vBus = 0;
// Read in Vbus voltage.
// VDD is in mV units, so divide by 1000 to convert to V units
vBus = ((uint32) adcRawData[ADC_VBUS_CHAN] * VDD * DC_BUS_SCALER) / (1000 * ADC_RESOLUTION);
if (vBus < VBUS_LOW)
{
Cymc_MAL_MotorStop();
errState |= ERR_UNDER_VOLTAGE;
}
else if(vBus > VBUS_HIGH)
{
Cymc_MAL_MotorStop();
errState |= ERR_OVER_VOLTAGE;
}
}
/****************************************************************************************
* *
* Function: FOC_MainLoop_ISR *
* *
* Description: PWM main ISR, implement base FOC flow *
* *
* Paramters: None *
* *
* Returns: None *
* *
****************************************************************************************/
CY_ISR(FOC_MainLoop_ISR)
{
/* system ticker, every count indicates 1ms */
pwmCounter++;
if (pwmCounter >= SEC_COUNTER)
{
pwmCounter = 0;
secFlag = 1;
}
/* Read ADC and Get phase current */
Cymc_HAL_ADCReadSample();
Cymc_BCL_GetADCOffset(adcRawData[ADC_IA_CHAN], adcRawData[ADC_IB_CHAN]);
phaseCurrentU = adcRawData[ADC_IA_CHAN];
phaseCurrentV = ((adcOffsetU + adcOffsetV + adcOffsetV)) - adcRawData[ADC_IB_CHAN] - adcRawData[ADC_IA_CHAN];
phaseCurrentU = (phaseCurrentU - adcOffsetU);
phaseCurrentV = (phaseCurrentV - adcOffsetV);
phaseCurrentOverProtect();
phaseCurrentU0 = phaseCurrentU << 3;
phaseCurrentV0 = phaseCurrentV << 3;
/* Clark Transformation uvw->αβ */
clarkTrans.inU = phaseCurrentU0 << 2; /* Phase U current. */
clarkTrans.inV = phaseCurrentV0 << 2; /* Phase V current. */
Cymc_BCL_Clark(&clarkTrans);
/* Speed Ramp Up */
rampUp.RefSpeed = motor.speedRef;
Cymc_BCL_RampUp(&rampUp);
if (rampUp.output < _FQ(0.2))
motor.vqRef = (rampUp.output>>1) + _FQ(0.06);
/*Switch to Speed Loop*/
if (motor.runState == M_OPENLOOP)
{
pidId.iOut = 0;
pidIq.iOut = motor.vqRef;
if (rampUp.output >= motor.speedRef - SPEED_MIN_STEP)
{
motor.runState = M_SPEEDLOOP;
if (motor.runState != M_SPEEDLOOP || motor.runState != M_CURRENTLOOP)
{
pidId.iOut = 0;
pidIq.iOut = motor.vqRef;
}
}
}
/* Position Calculation via SMO observer */
smoEst.Vbus = _FQ(1.0);
smoEst.speedRef = rampUp.output;
smoEst.volAlpha = invParkTrans.outAlpha;
smoEst.volBeta = invClarkTrans.InBeta;
smoEst.curAlpha = clarkTrans.outAlpha;
smoEst.curBeta = clarkTrans.outBeta;
Cymc_ACL_SMOCal(&smoEst);
/*angle given*/
if (motor.runState == M_SPEEDLOOP)
{
curAngle = smoEst.Theta;
}
else
{
/* Angle Generator For Open Loop */
angleGen.speed = rampUp.output;
Cymc_BCL_AngleGenerator (&angleGen);
curAngle = angleGen.angle;
pidSpeed.iOut = 0;
}
/* Speed Calculation */
if (motor.runState != M_STOP)
{
speedEst.curTheta = curAngle;
Cymc_BCL_SpeedCal (&speedEst);
motor.speedRPM = speedEst.estimatedSpeedRpm;
/* speed value cannot be negative */
if (motor.speedRPM < 0)
motor.speedRPM = 0;
if ((uint16)motor.speedRPM > SPEED_RPM_MAX)
motor.speedRPM = SPEED_RPM_MAX;
}
else
{
speedEst.estimatedSpeed = 0;
motor.speedRPM = 0;
}
/* Park Transformation αβ->dq */
parkTrans.inAlpha = clarkTrans.outAlpha;
parkTrans.inBeta = clarkTrans.outBeta;
Cymc_GFL_SinCosCal_Q15 (curAngle, &parkTrans.inSin, &parkTrans.inCos);
Cymc_BCL_Park (&parkTrans);
/* Speed PID Controller */
if (speedLoopCount == speedLoopPrescaler)
{
pidSpeed.ref = rampUp.output; /* motor.speedRef; */
pidSpeed.fbk = speedEst.estimatedSpeed;
Cymc_GFL_PICal (&pidSpeed);
speedLoopCount = 0;
updateSpeedrefFromVR();
}
speedLoopCount++;
/* Iq PID Controller */
if(motor.runState == M_SPEEDLOOP)
{
pidIq.ref = pidSpeed.out;
}
else
{
pidIq.ref = motor.vqRef;
}
pidIq.fbk = parkTrans.outQ;
Cymc_GFL_PICal(&pidIq);
/* Id PID Controller */
pidId.ref = motor.vdRef;
pidId.fbk = parkTrans.outD;
Cymc_GFL_PICal(&pidId);
/* Inverse Park Transformation dq->αβ */
if(motor.runState == M_OPENLOOP || motor.runState == M_STOP)
{
invParkTrans.inD = motor.vdRef;
invParkTrans.inQ = motor.vqRef;
}
else
{
invParkTrans.inD = pidId.out;
invParkTrans.inQ = pidIq.out;
}
invParkTrans.inSin = parkTrans.inSin;
invParkTrans.inCos = parkTrans.inCos;
Cymc_BCL_InvPark(&invParkTrans);
/* Inverse Clark Transformation αβ->uvw */
invClarkTrans.InAlpha = invParkTrans.outAlpha;
invClarkTrans.InBeta = invParkTrans.outBeta;
Cymc_BCL_InvClark(&invClarkTrans);
/* SVPWM Calculation */
svpwmCal.Uu = invClarkTrans.outU;
svpwmCal.Uv = invClarkTrans.outV;
svpwmCal.Uw = invClarkTrans.outW;
Cymc_BCL_SVPWM(&svpwmCal);
/* Update PWM Duty */
pwmCmp[0] = svpwmCal.CmpU;
pwmCmp[1] = svpwmCal.CmpV;
pwmCmp[2] = svpwmCal.CmpW;
Cymc_HAL_PWMOutputUpdate();
}
/****************************************************************************************
* ISR *
* Function: Ibus_Over_ISR *
* *
* Description: comprator ISR for bus current over protection *
* *
* Paramters: None *
* *
* Returns: None *
* *
****************************************************************************************/
uint32 IsrTimes = 0;
CY_ISR(Ibus_Over_ISR)
{
LPComp_IbusPt_ClearInterrupt (LPComp_IbusPt_INTR);
IsrTimes++;
if (LPComp_IbusPt_GetCompare())
{
errState |= ERR_OVER_CURRENT;
Cymc_MAL_MotorStop();
LPComp_IbusPt_ClearInterrupt (LPComp_IbusPt_INTR);
}
LPComp_IbusPt_ClearInterrupt (LPComp_IbusPt_INTR);
}