-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
1480 lines (1108 loc) · 41.4 KB
/
main.c
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* --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/proj_lab05b.c
//! \brief Adjusting the speed current controller
//!
//! (C) Copyright 2011, Texas Instruments, Inc.
//! \defgroup PROJ_LAB05b PROJ_LAB05b
//@{
//! \defgroup PROJ_LAB05b_OVERVIEW Project Overview
//!
//! Adjusting the supplied speed controller
//!
// **************************************************************************
// the includes
// system includes
#include <math.h>
#include "main.h"
#include <string.h>
#include "drivers/sci/sci.h"
#include "modules/usDelay/usDelay.h"
#include "modules/angle_gen/angle_gen.h"
#include "modules/vs_freq/vs_freq.h"
#ifdef FLASH
#pragma CODE_SECTION(mainISR,"ramfuncs");
#pragma CODE_SECTION(SCI_RX_ISR,"ramfuncs");
#endif
// Include header files used in the main function
// **************************************************************************
// the defines
#define LED_BLINK_FREQ_Hz 5
#define EE_READ 0x80 // 10 xxxxxx(A6-A0)
#define EE_WRITE 0x40 // 01 xxxxxx(A6-A0)
#define EE_EWEN 0x3F // 00 11XXXX(X is DONT CARE)
#define EE_EWDS 0x00 // 00 00XXXX(X is DONT CARE)
#define EE_ERASE 0xC0 // 11 xxxxxx(A6-A0)
// **************************************************************************
// the globals
#if (USER_MOTOR == multistar_4108_380kv_1)
uint16_t boardId = '1';
#elif (USER_MOTOR == multistar_4108_380kv_2)
uint16_t boardId = '2';
#elif (USER_MOTOR == multistar_4108_380kv_3)
uint16_t boardId = '3';
#elif (USER_MOTOR == multistar_4108_380kv_4)
uint16_t boardId = '4';
#else
uint16_t boardId = '5';
#endif
volatile uint16_t writeData = 0b0101010101010101;
volatile uint16_t eepromReadData = 0;
volatile uint16_t writeEEPROM = 0;
volatile uint16_t timeout = 0;
volatile uint16_t voltageTooLow = 1;
_iq lowVoltageThreshold = _IQ(0.01);
uint_least16_t gCounter_updateGlobals = 0;
bool Flag_Latch_softwareUpdate = true;
MATH_vec3 gAdcBiasI;
MATH_vec3 gAdcBiasV;
CTRL_Handle ctrlHandle;
CLARKE_Handle clarkeHandle_I; //!< the handle for the current Clarke transform
CLARKE_Obj clarke_I; //!< the current Clarke transform object
CLARKE_Handle clarkeHandle_V; //!< the handle for the voltage Clarke transform
CLARKE_Obj clarke_V; //!< the voltage Clarke transform object
EST_Handle estHandle; //!< the handle for the estimator
IPARK_Handle iparkHandle; //!< the handle for the inverse Park transform
IPARK_Obj ipark; //!< the inverse Park transform object
PARK_Handle parkHandle; //!< the handle for the Park object
PARK_Obj park; //!< the Park transform object
SVGEN_Handle svgenHandle; //!< the handle for the space vector generator
SVGEN_Obj svgen; //!< the space vector generator object
HAL_Handle halHandle;
SCI_Handle sciHandle;
USER_Params gUserParams;
HAL_PwmData_t gPwmData = {_IQ(0.0), _IQ(0.0), _IQ(0.0)};
HAL_AdcData_t gAdcData;
_iq gMaxCurrentSlope = _IQ(0.0);
#ifdef FAST_ROM_V1p6
CTRL_Obj *controller_obj;
#else
CTRL_Obj ctrl; //v1p7 format
CTRL_Obj *controller_obj;
#endif
uint16_t gLEDcnt = 0;
volatile MOTOR_Vars_t gMotorVars = MOTOR_Vars_INIT;
#ifdef FLASH
// Used for running BackGround in flash, and ISR in RAM
extern uint16_t *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;
#endif
#ifdef DRV8301_SPI
// Watch window interface to the 8301 SPI
DRV_SPI_8301_Vars_t gDrvSpi8301Vars;
#endif
_iq gFlux_pu_to_Wb_sf;
_iq gFlux_pu_to_VpHz_sf;
_iq gTorque_Ls_Id_Iq_pu_to_Nm_sf;
_iq gTorque_Flux_Iq_pu_to_Nm_sf;
char buf[16];
char returnBuf[32];
int counter = 0;
int rxIntCounter = 0;
int commandReceived = 0;
int commandStart = 0;
int isWaitingTxFifoEmpty = 0;
int txOffDelayCount = 2; // 1 count = 66.667us, 15 counts = 1ms
int txOffDelayCounter = 0;
int txOffDelayActive = 0;
int setTxOff = 0;
int sendSpeed = 0;
//uint32_t slowCount = 30000; // 2s
//uint32_t slowCounter = 0;
void scia_init(void);
void serialWrite(char *sendData, int length);
void eepromWriteEnable();
void eepromWriteDisable();
void eepromErase(char addr);
unsigned short eepromRead(char addr);
void eepromWrite(char addr, unsigned short data);
void eepromSend(char data);
bool isOpenLoop = false;
// define Angle Generate
ANGLE_GEN_Handle angle_genHandle;
ANGLE_GEN_Obj angle_gen;
// define Vs per Freq
VS_FREQ_Handle vs_freqHandle;
VS_FREQ_Obj vs_freq;
// **************************************************************************
// the functions
void main(void)
{
uint_least8_t estNumber = 0;
#ifdef FAST_ROM_V1p6
uint_least8_t ctrlNumber = 0;
#endif
// Only used if running from FLASH
// Note that the variable FLASH is defined by the project
#ifdef FLASH
// Copy time critical code and Flash setup code to RAM
// The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
// symbols are created by the linker. Refer to the linker files.
memCopy((uint16_t *)&RamfuncsLoadStart,(uint16_t *)&RamfuncsLoadEnd,(uint16_t *)&RamfuncsRunStart);
#endif
// initialize the hardware abstraction layer
halHandle = HAL_init(&hal,sizeof(hal));
// check for errors in user parameters
USER_checkForErrors(&gUserParams);
// store user parameter error in global variable
gMotorVars.UserErrorCode = USER_getErrorCode(&gUserParams);
// do not allow code execution if there is a user parameter error
if(gMotorVars.UserErrorCode != USER_ErrorCode_NoError)
{
for(;;)
{
gMotorVars.Flag_enableSys = false;
}
}
// initialize the user parameters
USER_setParams(&gUserParams);
// set the hardware abstraction layer parameters
HAL_setParams(halHandle,&gUserParams);
// initialize the controller
#ifdef FAST_ROM_V1p6
ctrlHandle = CTRL_initCtrl(ctrlNumber, estNumber); //v1p6 format (06xF and 06xM devices)
#else
ctrlHandle = CTRL_initCtrl(estNumber,&ctrl,sizeof(ctrl)); //v1p7 format default
#endif
controller_obj = (CTRL_Obj *)ctrlHandle;
{
CTRL_Version version;
// get the version number
CTRL_getVersion(ctrlHandle,&version);
gMotorVars.CtrlVersion = version;
}
// set the default controller parameters
CTRL_setParams(ctrlHandle,&gUserParams);
// initialize the angle generate module
angle_genHandle = ANGLE_GEN_init(&angle_gen,sizeof(angle_gen));
ANGLE_GEN_setParams(angle_genHandle, gUserParams.iqFullScaleFreq_Hz, gUserParams.ctrlPeriod_sec);
// initialize the Vs per Freq module
vs_freqHandle = VS_FREQ_init(&vs_freq,sizeof(vs_freq));
VS_FREQ_setParams(vs_freqHandle, gUserParams.iqFullScaleFreq_Hz, gUserParams.iqFullScaleVoltage_V, gUserParams.maxVsMag_pu);
VS_FREQ_setProfile(vs_freqHandle, USER_MOTOR_FREQ_LOW, USER_MOTOR_FREQ_HIGH, USER_MOTOR_VOLT_MIN, USER_MOTOR_VOLT_MAX);
// initialize the Clarke modules
clarkeHandle_I = CLARKE_init(&clarke_I,sizeof(clarke_I));
clarkeHandle_V = CLARKE_init(&clarke_V,sizeof(clarke_V));
// set the number of current sensors
setupClarke_I(clarkeHandle_I,gUserParams.numCurrentSensors);
// set the number of voltage sensors
setupClarke_V(clarkeHandle_V,gUserParams.numVoltageSensors);
#ifdef FAST_ROM_V1p6
estHandle = controller_obj->estHandle;
#else
estHandle = ctrl.estHandle;
#endif
// initialize the inverse Park module
iparkHandle = IPARK_init(&ipark,sizeof(ipark));
// initialize the Park module
parkHandle = PARK_init(&park,sizeof(park));
// initialize the space vector generator module
svgenHandle = SVGEN_init(&svgen,sizeof(svgen));
// setup faults
HAL_setupFaults(halHandle);
// initialize the interrupt vector table
HAL_initIntVectorTable(halHandle);
// enable the ADC interrupts
HAL_enableAdcInts(halHandle);
// enable global interrupts
HAL_enableGlobalInts(halHandle);
// enable debug interrupts
HAL_enableDebugInt(halHandle);
// disable the PWM
HAL_disablePwm(halHandle);
scia_init();
#ifdef DRV8301_SPI
// turn on the DRV8301 if present
HAL_enableDrv(halHandle);
// initialize the DRV8301 interface
HAL_setupDrvSpi(halHandle,&gDrvSpi8301Vars);
#endif
// enable DC bus compensation
CTRL_setFlag_enableDcBusComp(ctrlHandle, true);
// compute scaling factors for flux and torque calculations
gFlux_pu_to_Wb_sf = USER_computeFlux_pu_to_Wb_sf();
gFlux_pu_to_VpHz_sf = USER_computeFlux_pu_to_VpHz_sf();
gTorque_Ls_Id_Iq_pu_to_Nm_sf = USER_computeTorque_Ls_Id_Iq_pu_to_Nm_sf();
gTorque_Flux_Iq_pu_to_Nm_sf = USER_computeTorque_Flux_Iq_pu_to_Nm_sf();
gMotorVars.Kp_spd = _IQ(4.0);
gMotorVars.MaxAccel_krpmps = _IQ(10.0);
gMotorVars.SpeedRef_krpm = _IQ(0.0);
gMotorVars.Flag_enableSys = true;
gMotorVars.Flag_enableOffsetcalc = false;
for(;;)
{
// Waiting for enable system flag to be set
while(!(gMotorVars.Flag_enableSys));
// Enable the Library internal PI. Iq is referenced by the speed PI now
CTRL_setFlag_enableSpeedCtrl(ctrlHandle, true);
// loop while the enable system flag is true
while(gMotorVars.Flag_enableSys)
{
/*if (isWaitingTxFifoEmpty && SCI_getRxFifoStatus(sciHandle) == SCI_FifoStatus_Empty) {
//if (isWaitingTxFifoEmpty && SCI_txReady(sciHandle)) {
isWaitingTxFifoEmpty = 0;
txOffDelayActive = 1;
}*/
/*if (setTxOff) {
setTxOff = 0;
//GPIO_setLow(halHandle->gpioHandle,GPIO_Number_12);
AIO_setLow(halHandle->gpioHandle,AIO_Number_6);
}*/
//if (commandReceived) {
if (counter == 8) {
commandReceived = 0;
//SerialCommand cmd;
//memcpy(&cmd, buf + commandStart, 6);
//if (cmd.id == boardId && cmd.type == 's') {
if (buf[1] == boardId && buf[2] == 's') {
long value = ((long)buf[3]) | ((long)buf[4] << 8) | ((long)buf[5] << 16) | ((long)buf[6] << 24);
bool isRunIdentify = 1;
#if (USER_MOTOR == propdrive_v2_2836_1200kv)
if (value == _IQ(0.0)) {
isRunIdentify = 0;
}
else if (_IQabs(value) <= _IQ(0.2)) {
isOpenLoop = true;
}
else {
isOpenLoop = false;
}
#endif
gMotorVars.SpeedRef_krpm = value;
gMotorVars.Flag_Run_Identify = isRunIdentify;
/*returnBuf[0] = '<';
returnBuf[1] = boardId;
returnBuf[2] = 'd';
long returnValue = gMotorVars.Speed_krpm;
returnBuf[3] = returnValue;
returnBuf[4] = returnValue >> 8;
returnBuf[5] = returnValue >> 16;
returnBuf[6] = returnValue >> 24;
returnBuf[7] = '>';
serialWrite(returnBuf, 8);*/
//_IQtoa(returnBuf + 2, "%3.5f", gMotorVars.Speed_krpm);
//int n = strlen(returnBuf);
//returnBuf[n] = '\n';
//serialWrite(returnBuf, n + 1);
//long * intlocation = (long*)(&returnBuf[2]);
//*intlocation = gMotorVars.SpeedRef_krpm;
//*intlocation = 287392129l;
}
/*if (buf[commandStart] == boardId && buf[commandStart + 1] == 's') {
gMotorVars.SpeedRef_krpm = _atoIQ(buf + commandStart + 2);
gMotorVars.Flag_Run_Identify = 1;
returnBuf[0] = boardId;
returnBuf[1] = 's';
_IQtoa(returnBuf + 2, "%3.5f", gMotorVars.Speed_krpm);
int n = strlen(returnBuf);
returnBuf[n] = '\n';
serialWrite(returnBuf, n + 1);
}*/
commandStart = 0;
counter = 0;
}
if (sendSpeed) {
sendSpeed = 0;
if (buf[1] == boardId && buf[2] == 's') {
returnBuf[0] = '<';
returnBuf[1] = boardId;
returnBuf[2] = 'd';
long returnValue = gMotorVars.Speed_krpm;
returnBuf[3] = returnValue;
returnBuf[4] = returnValue >> 8;
returnBuf[5] = returnValue >> 16;
returnBuf[6] = returnValue >> 24;
returnBuf[7] = '>';
serialWrite(returnBuf, 8);
}
}
/*if (SCI_txReady(sciHandle)) {
SCI_write(sciHandle, 'a');
}*/
/*while (SCI_getRxFifoStatus(sciHandle)) {
char rev_data = SCI_read(sciHandle);
if (rev_data == '\n') {
buf[counter] = '\0';
counter = 0;
//CTRL_setSpd_ref_krpm(ctrlHandle, _atoIQ(buf));
if (buf[0] == boardId && buf[1] == 's') {
gMotorVars.SpeedRef_krpm = _atoIQ(buf + 2);
gMotorVars.Flag_Run_Identify = 1;
returnBuf[0] = boardId;
returnBuf[1] = 's';
_IQtoa(returnBuf + 2, "%3.5f", gMotorVars.Speed_krpm);
int n = strlen(returnBuf);
returnBuf[n] = '\n';
serialWrite(returnBuf, n + 1);
}
} else {
buf[counter] = rev_data;
counter++;
if (counter == 16) {
counter = 0;
}
}
if (SCI_txReady(sciHandle)) {
usDelay(5000);
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_12);
usDelay(5000);
SCI_write(sciHandle, rev_data);
//SCI_putDataNonBlocking(sciHandle, rev_data);
usDelay(5000);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_12);
usDelay(5000);
}
}*/
//GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_12);
/*if (SCI_txReady(sciHandle)) {
SCI_write(sciHandle, '3');
}*/
CTRL_Obj *obj = (CTRL_Obj *)ctrlHandle;
// increment counters
gCounter_updateGlobals++;
// enable/disable the use of motor parameters being loaded from user.h
CTRL_setFlag_enableUserMotorParams(ctrlHandle,gMotorVars.Flag_enableUserParams);
// enable/disable Rs recalibration during motor startup
EST_setFlag_enableRsRecalc(obj->estHandle,gMotorVars.Flag_enableRsRecalc);
// enable/disable automatic calculation of bias values
CTRL_setFlag_enableOffset(ctrlHandle,gMotorVars.Flag_enableOffsetcalc);
if(CTRL_isError(ctrlHandle)) {
// set the enable controller flag to false
CTRL_setFlag_enableCtrl(ctrlHandle,false);
// set the enable system flag to false
gMotorVars.Flag_enableSys = false;
// disable the PWM
HAL_disablePwm(halHandle);
} else if (voltageTooLow) {
// set the enable controller flag to false
CTRL_setFlag_enableCtrl(ctrlHandle,false);
// disable the PWM
HAL_disablePwm(halHandle);
gMotorVars.Flag_Run_Identify = false;
} else {
// update the controller state
bool flag_ctrlStateChanged = CTRL_updateState(ctrlHandle);
// enable or disable the control
CTRL_setFlag_enableCtrl(ctrlHandle, gMotorVars.Flag_Run_Identify);
if(flag_ctrlStateChanged)
{
CTRL_State_e ctrlState = CTRL_getState(ctrlHandle);
if(ctrlState == CTRL_State_OffLine)
{
// enable the PWM
HAL_enablePwm(halHandle);
}
else if(ctrlState == CTRL_State_OnLine)
{
if(gMotorVars.Flag_enableOffsetcalc == true)
{
uint_least16_t cnt;
// update the ADC bias values
HAL_updateAdcBias(halHandle);
// record the current bias
for(cnt=0;cnt<3;cnt++)
gAdcBiasI.value[cnt] = HAL_getBias(halHandle,HAL_SensorType_Current,cnt);
// record the voltage bias
for(cnt=0;cnt<3;cnt++)
gAdcBiasV.value[cnt] = HAL_getBias(halHandle,HAL_SensorType_Voltage,cnt);
gMotorVars.Flag_enableOffsetcalc = false;
}
else
{
// set the current bias
HAL_setBias(halHandle,HAL_SensorType_Current,0,_IQ(I_A_offset));
HAL_setBias(halHandle,HAL_SensorType_Current,1,_IQ(I_B_offset));
HAL_setBias(halHandle,HAL_SensorType_Current,2,_IQ(I_C_offset));
// set the voltage bias
HAL_setBias(halHandle,HAL_SensorType_Voltage,0,_IQ(V_A_offset));
HAL_setBias(halHandle,HAL_SensorType_Voltage,1,_IQ(V_B_offset));
HAL_setBias(halHandle,HAL_SensorType_Voltage,2,_IQ(V_C_offset));
}
// Return the bias value for currents
gMotorVars.I_bias.value[0] = HAL_getBias(halHandle,HAL_SensorType_Current,0);
gMotorVars.I_bias.value[1] = HAL_getBias(halHandle,HAL_SensorType_Current,1);
gMotorVars.I_bias.value[2] = HAL_getBias(halHandle,HAL_SensorType_Current,2);
// Return the bias value for voltages
gMotorVars.V_bias.value[0] = HAL_getBias(halHandle,HAL_SensorType_Voltage,0);
gMotorVars.V_bias.value[1] = HAL_getBias(halHandle,HAL_SensorType_Voltage,1);
gMotorVars.V_bias.value[2] = HAL_getBias(halHandle,HAL_SensorType_Voltage,2);
if (isOpenLoop) {
// set flag to disable speed controller
CTRL_setFlag_enableSpeedCtrl(ctrlHandle, false);
// set flag to disable current controller
CTRL_setFlag_enableCurrentCtrl(ctrlHandle, false);
} else {
// set flag to disable speed controller
CTRL_setFlag_enableSpeedCtrl(ctrlHandle, true);
// set flag to disable current controller
CTRL_setFlag_enableCurrentCtrl(ctrlHandle, true);
}
// enable the PWM
HAL_enablePwm(halHandle);
}
else if(ctrlState == CTRL_State_Idle)
{
// disable the PWM
HAL_disablePwm(halHandle);
gMotorVars.Flag_Run_Identify = false;
}
if((CTRL_getFlag_enableUserMotorParams(ctrlHandle) == true) &&
(ctrlState > CTRL_State_Idle) &&
(gMotorVars.CtrlVersion.minor == 6))
{
// call this function to fix 1p6
USER_softwareUpdate1p6(ctrlHandle);
}
}
}
if(EST_isMotorIdentified(obj->estHandle))
{
// set the current ramp
EST_setMaxCurrentSlope_pu(obj->estHandle,gMaxCurrentSlope);
gMotorVars.Flag_MotorIdentified = true;
// set the speed reference
CTRL_setSpd_ref_krpm(ctrlHandle,gMotorVars.SpeedRef_krpm);
// set the speed acceleration
CTRL_setMaxAccel_pu(ctrlHandle,_IQmpy(MAX_ACCEL_KRPMPS_SF,gMotorVars.MaxAccel_krpmps));
if(Flag_Latch_softwareUpdate)
{
Flag_Latch_softwareUpdate = false;
USER_calcPIgains(ctrlHandle);
// initialize the watch window kp and ki current values with pre-calculated values
gMotorVars.Kp_Idq = CTRL_getKp(ctrlHandle,CTRL_Type_PID_Id);
gMotorVars.Ki_Idq = CTRL_getKi(ctrlHandle,CTRL_Type_PID_Id);
// initialize the watch window kp and ki values with pre-calculated values
//gMotorVars.Kp_spd = CTRL_getKp(ctrlHandle,CTRL_Type_PID_spd);
gMotorVars.Ki_spd = CTRL_getKi(ctrlHandle,CTRL_Type_PID_spd);
}
}
else
{
Flag_Latch_softwareUpdate = true;
// the estimator sets the maximum current slope during identification
gMaxCurrentSlope = EST_getMaxCurrentSlope_pu(obj->estHandle);
}
// when appropriate, update the global variables
if(gCounter_updateGlobals >= NUM_MAIN_TICKS_FOR_GLOBAL_VARIABLE_UPDATE)
{
// reset the counter
gCounter_updateGlobals = 0;
updateGlobalVariables_motor(ctrlHandle);
if (voltageTooLow && gMotorVars.VdcBus_kV > lowVoltageThreshold) {
voltageTooLow = 0;
// Power restored, reset to start with fresh parameters
// disable the PWM
HAL_disablePwm(halHandle);
// set the default controller parameters (Reset the control to re-identify the motor)
CTRL_setParams(ctrlHandle,&gUserParams);
gMotorVars.Flag_Run_Identify = false;
} else if (!voltageTooLow && gMotorVars.VdcBus_kV < lowVoltageThreshold) {
voltageTooLow = 1;
// Power lost, disable control
if (gMotorVars.Flag_Run_Identify) {
// disable the PWM
HAL_disablePwm(halHandle);
CTRL_setFlag_enableCtrl(ctrlHandle,false);
// set the default controller parameters (Reset the control to re-identify the motor)
CTRL_setParams(ctrlHandle,&gUserParams);
gMotorVars.Flag_Run_Identify = false;
}
}
}
// update Kp and Ki gains
updateKpKiGains(ctrlHandle);
// enable/disable the forced angle
EST_setFlag_enableForceAngle(obj->estHandle,gMotorVars.Flag_enableForceAngle);
// enable or disable power warp
CTRL_setFlag_enablePowerWarp(ctrlHandle,gMotorVars.Flag_enablePowerWarp);
#ifdef DRV8301_SPI
//GPIO_setLow(halHandle->gpioHandle,GPIO_Number_19);
HAL_writeDrvData(halHandle,&gDrvSpi8301Vars);
HAL_readDrvData(halHandle,&gDrvSpi8301Vars);
//GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_19);
//usDelay(5000);
//if (writeEEPROM) {
/*writeEEPROM = 0;
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_34);
SPI_resetRxFifo(halHandle->drv8301Handle->spiHandle);
SPI_enableRxFifo(halHandle->drv8301Handle->spiHandle);
SPI_write(halHandle->drv8301Handle->spiHandle,0b1001100000000000); //Enable write
usDelay(100);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_34);
usDelay(200);
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_34);
SPI_resetRxFifo(halHandle->drv8301Handle->spiHandle);
SPI_enableRxFifo(halHandle->drv8301Handle->spiHandle);
usDelay(100);
SPI_write(halHandle->drv8301Handle->spiHandle,0xA000 | writeData >> 11);
SPI_write(halHandle->drv8301Handle->spiHandle, writeData << 5);
usDelay(300);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_34);
usDelay(20);
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_34);
while (!GPIO_getData(halHandle->gpioHandle, GPIO_Number_17)) {
usDelay(10);
}
usDelay(10);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_34);
usDelay(20);
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_34);
timeout = 0;
//const uint16_t data = 0;
volatile uint16_t WaitTimeOut = 0;
volatile SPI_FifoStatus_e RxFifoCnt = SPI_FifoStatus_Empty;
// reset the Rx fifo pointer to zero
SPI_resetRxFifo(halHandle->drv8301Handle->spiHandle);
SPI_enableRxFifo(halHandle->drv8301Handle->spiHandle);
// write the command
SPI_write(halHandle->drv8301Handle->spiHandle,0xC000);
SPI_write(halHandle->drv8301Handle->spiHandle,0x0000);
WaitTimeOut = 0;
// wait for two words to populate the RX fifo, or a wait timeout will occur
while((RxFifoCnt < SPI_FifoStatus_2_Words) && (WaitTimeOut < 0xffff)) {
RxFifoCnt = SPI_getRxFifoStatus(halHandle->drv8301Handle->spiHandle);
if (++WaitTimeOut > 0xfffe) {
//halHandle->drv8301Handle->RxTimeOut = true;
timeout = 1;
}
}
eepromReadData = 0;
usDelay(1);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_34);
//eepromReadData = (0b0000000000011111 & SPI_readEmu(halHandle->drv8301Handle->spiHandle)) << 11;
//eepromReadData = eepromReadData | (0b1111111111100000 & SPI_readEmu(halHandle->drv8301Handle->spiHandle)) >> 5;
eepromReadData = SPI_readEmu(halHandle->drv8301Handle->spiHandle);
eepromReadData = SPI_readEmu(halHandle->drv8301Handle->spiHandle);*/
/*writeEEPROM = 0;
eepromWriteEnable();
usDelay(100);
eepromWrite(0x00, writeData);
usDelay(100);
eepromReadData = eepromRead(0x00);*/
//}
#endif
} // end of while(gFlag_enableSys) loop
// disable the PWM
HAL_disablePwm(halHandle);
// set the default controller parameters (Reset the control to re-identify the motor)
CTRL_setParams(ctrlHandle,&gUserParams);
gMotorVars.Flag_Run_Identify = false;
} // end of for(;;) loop
} // end of main() function
void eepromWriteEnable() {
eepromSend(EE_EWEN);
usDelay(1);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_34); //_eecs=0;
}
void eepromWriteDisable() {
eepromSend(EE_EWDS);
usDelay(1);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_34); //_eecs=0;
}
void eepromErase(char addr) {
eepromSend(EE_ERASE | addr);
usDelay(1);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_34); //_eecs=0;
/** wait busy flag clear */
usDelay(1); // tcs > 250ns @2.7V
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_34); //_eecs=1;
usDelay(1); // tsv < 250ns @2.7V
//while(_eedo==0); // 0.1ms < twp < 10ms
while (!GPIO_read(halHandle->gpioHandle,GPIO_Number_17));
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_34); //_eecs=0;
}
unsigned short eepromRead(char addr) {
unsigned short data = 0;
signed char i = 15;
eepromSend(EE_READ | addr);
usDelay(1);
for (i = 15; i >= 0; i--) {
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_18); //_eeck=1;
usDelay(1);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_18); //_eeck=0;
//data = data | (_eedo<<i);
data = data | (GPIO_read(halHandle->gpioHandle,GPIO_Number_17) << i);
usDelay(1);
}
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_34); //_eecs=0;
return data;
}
void eepromWrite(char addr, unsigned short data) {
signed char i = 15;
eepromSend(EE_WRITE | addr);
usDelay(1);
for (i = 15; i >= 0; i--){
//_eedi = (int)( (data>>i)&0x0001 );
if ((data >> i) & 0x0001) {
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_16);
} else {
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_16);
}
usDelay(1);
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_18); //_eeck=1;
usDelay(1);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_18); //_eeck=0;
}
usDelay(50);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_34); //_eecs=0;
/** wait busy flag clear */
usDelay(1); // tcs > 250ns @2.7V
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_34); //_eecs=1;
usDelay(1); // tsv < 250ns @2.7V
//while(_eedo==0); // 0.1ms < twp < 10ms
while (!GPIO_read(halHandle->gpioHandle,GPIO_Number_17));
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_34); //_eecs=0;
}
void eepromSend(char data) {
signed char i = 7;
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_16); //_eedi=1;
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_34); //_eecs=1; // fall is in function
usDelay(1);
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_18); //_eeck=1;
usDelay(1);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_18); //_eeck=0;
while(i>=0){
//_eedi = (data>>i)&0x01;
if ((data >> i) & 0x0001) {
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_16);
} else {
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_16);
}
i--;
usDelay(1);
GPIO_setHigh(halHandle->gpioHandle,GPIO_Number_18); //_eeck=1;
usDelay(1);
GPIO_setLow(halHandle->gpioHandle,GPIO_Number_18); //_eeck=0;
}
}
interrupt void mainISR(void)
{
_iq angle_pu = 0;
_iq speed_pu;
_iq speed_ref_pu = TRAJ_getIntValue(((CTRL_Obj *)ctrlHandle)->trajHandle_spd);
_iq speed_outMax_pu = TRAJ_getIntValue(((CTRL_Obj *)ctrlHandle)->trajHandle_spdMax);
MATH_vec2 Iab_pu;
MATH_vec2 Vab_pu;
MATH_vec2 Vdq_out_pu;
MATH_vec2 Vab_out_pu;
MATH_vec2 phasor;
// toggle status LED
if(gLEDcnt++ > (uint_least32_t)(USER_ISR_FREQ_Hz / LED_BLINK_FREQ_Hz))
{
HAL_toggleLed(halHandle,(GPIO_Number_e)HAL_Gpio_LED2);
gLEDcnt = 0;
}
/*if (slowCounter++ > slowCount) {
slowCounter = 0;
AIO_toggle(halHandle->gpioHandle,AIO_Number_4);
AIO_toggle(halHandle->gpioHandle,AIO_Number_6);
}*/
// acknowledge the ADC interrupt
HAL_acqAdcInt(halHandle,ADC_IntNumber_1);
// convert the ADC data
HAL_readAdcData(halHandle,&gAdcData);
if (isOpenLoop) {
uint_least16_t count_isr = CTRL_getCount_isr(ctrlHandle);
uint_least16_t numIsrTicksPerCtrlTick = CTRL_getNumIsrTicksPerCtrlTick(ctrlHandle);
// if needed, run the controller
if (count_isr >= numIsrTicksPerCtrlTick) {
CTRL_State_e ctrlState = CTRL_getState(ctrlHandle);