HVBLDC_Sensored.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /* ==============================================================================
  2. System Name: HVBLDC_Sensored
  3. File Name: HVBLDC_Sensored.C
  4. Description: Primary system file for the (Trapezoidal) Sensored BLDC Control
  5. Using Hall Effect Sensors
  6. ===================================================================================*/
  7. // Include header files used in the main function
  8. #include "PeripheralHeaderIncludes.h"
  9. #define MATH_TYPE IQ_MATH
  10. #include "IQmathLib.h"
  11. #include "HVBLDC_Sensored.h"
  12. #include "HVBLDC_Sensored-Settings.h"
  13. #include <math.h>
  14. #include "UserCan.h"
  15. #include "lowpass.h"
  16. #include "var.h"
  17. #ifdef FLASH
  18. #pragma CODE_SECTION(MainISR,"ramfuncs");
  19. void MemCopy();
  20. void InitFlash();
  21. #endif
  22. #define TIMER1_PER1 60
  23. #define TIMER1_PER2 500000
  24. #define TIMER1_PER 30000000
  25. // Prototype statements for functions found within this file.
  26. interrupt void MainISR(void);//主中断
  27. interrupt void xint1_isr(void);//FO中断
  28. interrupt void xint2_isr(void);//HALL A中断
  29. interrupt void EPWM1TZint_isr(void);//TZ中断 过流中断
  30. void DeviceInit();
  31. void HVDMC_Protection(void);
  32. void speed_direction(Uint16 hall,Uint16 hall_last);//使用中断计算转速
  33. void speed_cal(void);//使用中断计算转速
  34. void pwmlimit_speed(void);//
  35. void ClosePwm(void);
  36. void OpenPwm(void);
  37. // State Machine function prototypes
  38. //------------------------------------
  39. int16 MastIsrTimePercent(void);
  40. void taskfree(void);
  41. void ServiceDog(void);
  42. void EnableDog(void);
  43. // Used for running BackGround in flash, and ISR in RAM
  44. //速度计算相关
  45. int16 Xint2Cnt = 0;
  46. extern Uint16 *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;
  47. int16 SerialCommsTimer;
  48. // Global variables used in this system
  49. HALL3 hall1 = HALL3_DEFAULTS;
  50. SPEED_MEAS_CAP speed1 = SPEED_MEAS_CAP_DEFAULTS;
  51. PWMGEN pwm1 = PWMGEN_DEFAULTS;
  52. _iq20 kscaler = _IQ20(90*3.3/4096/2.3);
  53. float32 T = 0.001/ISR_FREQUENCY; // Samping period (sec), see parameter.h
  54. Uint16 BackTicker = 0;
  55. Uint16 PreviousState;
  56. Uint16 ClosedFlag = 0;
  57. Uint32 VirtualTimer = 0;
  58. Uint16 ILoopFlag = FALSE;
  59. Uint16 SpeedLoopFlag = FALSE;
  60. int16 DFuncDesired = 0x0300; // Desired duty cycle (Q15)
  61. int16 DfuncTesting = 500;//0x0300;//占空比
  62. int16 Delay30 = 0x3FFF;
  63. int16 DelayFlag = 0;
  64. Uint16 AlignFlag = 0x000F;
  65. Uint16 LoopCount = 0;
  66. Uint16 TripFlagDMC=0; //PWM trip status
  67. #if (BUILDLEVEL<= LEVEL2)
  68. Uint32 CmtnPeriodTarget = 0x00000500;
  69. Uint32 CmtnPeriodSetpt = 0x00002000;//要大于CmtnPeriodTarget才能使用--
  70. Uint32 RampDelay = 10;
  71. #else
  72. Uint32 CmtnPeriodTarget = 0x00000450;
  73. Uint32 CmtnPeriodSetpt = 0x00001500;
  74. Uint32 RampDelay = 10;
  75. #endif
  76. _iq DCbus_current=0;
  77. _iq Speedgd=0;
  78. _iq tempIdc=0;
  79. int32 Rt;
  80. Uint16 ch1=0;
  81. Uint16 ch2=0;
  82. Uint16 ch3=0;
  83. // Used for ADC Configuration
  84. int ChSel[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  85. int TrigSel[16] = {5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5};
  86. int ACQPS[16] = {8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8};
  87. // Instance PI regulator to regulate the DC-bus current and speed
  88. // Instance a PWM driver instance
  89. // Instance a PWM DAC driver instance
  90. PWMDAC pwmdac1 = PWMDAC_DEFAULTS;
  91. // Instance a Hall effect driver
  92. // Instance a ramp controller to smoothly ramp the frequency
  93. // Instance a RAMP2 Module
  94. RMP2 rmp2 = RMP2_DEFAULTS;
  95. // Instance a RAMP3 Module
  96. RMP3 rmp3 = RMP3_DEFAULTS;
  97. // Instance a MOD6 Module
  98. MOD6CNT mod1 = MOD6CNT_DEFAULTS;
  99. // Instance a IMPULSE Module
  100. IMPULSE impl1 = IMPULSE_DEFAULTS;
  101. // Instance a SPEED_PR Module
  102. // Create an instance of DATALOG Module 950:3A 但是只有外界扭矩低于36N才能启动起来
  103. Uint16 pwm_limit_table1[5] = {950,1300,1650,1930,2320};//0 50 100 150 200
  104. //// 0, 100, 200, 300, 400 500 600 700 800 900 1000
  105. Uint16 pwm_limit_table2[]= {950,1650,2320,3050,3750,4460,5200,5929,6623,7358,8050,
  106. 8744,9400,10165,10848,11600,12290,12953,13684,14310,15050,
  107. 15760,16434,17054,17750,18493,19134,19830,20507,21260,21926,
  108. 22602,23232,23935,24609,25240};
  109. int32 pwmlimitrate = 32768;
  110. void main(void)
  111. {
  112. DeviceInit(); // Device Life support & GPIO
  113. InitCan();
  114. // Only used if running from FLASH
  115. // Note that the variable FLASH is defined by the compiler
  116. #ifdef FLASH
  117. // Copy time critical code and Flash setup code to RAM
  118. // The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
  119. // symbols are created by the linker. Refer to the linker files.
  120. MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
  121. // Call Flash Initialization to setup flash waitstates
  122. // This function must reside in RAM
  123. InitFlash(); // Call the flash wrapper init function
  124. #endif //(FLASH)
  125. // Initialize all the Device Peripherals:
  126. // This function is found in DSP280x_CpuTimers.c
  127. InitCpuTimers();
  128. // Configure CPU-Timer 0 to interrupt every ISR Period:
  129. // 60MHz CPU Freq, ISR Period (in uSeconds)
  130. // This function is found in DSP280x_CpuTimers.c
  131. ConfigCpuTimer(&CpuTimer0, 60, 1000/ISR_FREQUENCY);//40K
  132. StartCpuTimer0();
  133. // Configure CPU-Timer 1,2 for background loops
  134. ConfigCpuTimer(&CpuTimer1, TIMER1_PER1, TIMER1_PER2);//2hz
  135. // ConfigCpuTimer(&CpuTimer2, 60, 50000);//20HZ
  136. StartCpuTimer1();
  137. // StartCpuTimer2();
  138. // Reassign ISRs.
  139. // Reassign the PIE vector for TINT0 to point to a different
  140. // ISR then the shell routine found in DSP280x_DefaultIsr.c.
  141. // This is done if the user does not want to use the shell ISR routine
  142. // but instead wants to use their own ISR.
  143. EALLOW; // This is needed to write to EALLOW protected registers
  144. PieVectTable.TINT0 = &MainISR;//40KHZ
  145. PieVectTable.XINT1 = &xint1_isr;
  146. PieVectTable.XINT2 = &xint2_isr;
  147. PieVectTable.EPWM1_TZINT = &EPWM1TZint_isr;
  148. EDIS; // This is needed to disable write to EALLOW protected registers
  149. // Enable PIE group 1 interrupt 7 for TINT0
  150. PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
  151. PieCtrlRegs.PIEIER1.bit.INTx4 = 1; // Enable PIE Gropu 1 INT4 //XINT1
  152. PieCtrlRegs.PIEIER1.bit.INTx5 = 1; // Enable PIE Gropu 1 INT5 //XINT2
  153. // Enable CPU INT1 for TINT0:
  154. IER |= M_INT1;
  155. PieCtrlRegs.PIEIER2.bit.INTx1 = 1;
  156. IER |= M_INT2;
  157. // Enable Global realtime interrupt DBGM
  158. EALLOW;
  159. GpioIntRegs.GPIOXINT1SEL.bit.GPIOSEL = 16; //定位到哪个引脚中断
  160. GpioIntRegs.GPIOXINT2SEL.bit.GPIOSEL = 24; // XINT2 is GPIO24
  161. EDIS;
  162. XIntruptRegs.XINT1CR.bit.POLARITY = 1; // 1为上升沿,0和2为下降沿,3为双边沿
  163. XIntruptRegs.XINT2CR.bit.POLARITY = 3; // 1为上升沿,0和2为下降沿,3为双边沿
  164. //中断配置步骤-----1,开启模块中断使能,
  165. XIntruptRegs.XINT1CR.bit.ENABLE = 1;
  166. XIntruptRegs.XINT2CR.bit.ENABLE = 1; // Enable XINT2
  167. // Enable global Interrupts and higher priority real-time debug events:
  168. EINT; // Enable Global interrupt INTM
  169. ERTM; // Enable Global realtime interrupt DBGM
  170. // Initialize PWM module
  171. pwm1.PeriodMax = (SYSTEM_FREQUENCY/PWM_FREQUENCY)*1000/2; // Asymmetric PWM 20kHZ 3000
  172. pwm1.DutyFunc = 0; // 占空比ALIGN_DUTY/32767 // DutyFunc = Q15
  173. BLDCPWM_INIT_MACRO(1,2,3,&pwm1);
  174. // Initialize PWMDAC module
  175. pwmdac1.PeriodMax = 500; // @60Mhz, 1500->20kHz, 1000-> 30kHz, 500->60kHz
  176. pwmdac1.HalfPerMax = pwmdac1.PeriodMax/2; // Needed to adjust the duty cycle range in the macro
  177. PWMDAC_INIT_MACRO(6,pwmdac1) ; // PWM 6A,6B
  178. PWMDAC_INIT_MACRO(7,pwmdac1) ; // PWM 7A,7B
  179. // Initialize Hall module
  180. hall1.DebounceAmount = 0;
  181. hall1.Revolutions = 1;
  182. HALL3_INIT_MACRO(&hall1);//读hall值 初始化HallGpioBuffer,HallGpioAccepted
  183. // Initialize the SPEED_PR module
  184. speed1.InputSelect = 0;
  185. speed1.BaseRpm = (Uint32)120*BASE_FREQ/POLES;//(Uint32)120*BASE_FREQ/POLES/6;//// 使用一个换相周期//额定转速 = 60*(基频/(极数/2)) = 60*f/极对数
  186. speed1.SpeedScaler = (Uint32)((Uint32)ISR_FREQUENCY*10000/BASE_FREQ);//保留一位小数//40000/f
  187. // For the kits < Rev 1.1 -------------------------------------------------
  188. ChSel[0]=15; // Dummy meas. avoid 1st sample issue Rev0 Picollo
  189. ChSel[1]=2; // ChSelect: ADC B7-> Phase A Voltage
  190. ChSel[2]=3; // ChSelect: ADC B6-> Phase B Voltage
  191. ChSel[3]=4; // ChSelect: ADC B4-> Phase C Voltage
  192. ChSel[4]=7; // ChSelect: ADC A2-> DC Bus vol
  193. ChSel[5]=6; // DC current 放大20倍 Imeasure = I*5mΩ*20
  194. ChSel[6]=5; // NTC
  195. ChSel[7]=1; // VOT
  196. //-------------------------------------------------------------------------
  197. ADC_MACRO_INIT(ChSel,TrigSel,ACQPS);
  198. // Initialize RMP2 module
  199. rmp2.Out = (int32)0;
  200. rmp2.Ramp2Delay = 0x00000001;
  201. rmp2.Ramp2Max = 32767/2;
  202. rmp2.Ramp2Min = -32768/2;
  203. // Initialize RMP3 module
  204. rmp3.DesiredInput = CmtnPeriodTarget;
  205. rmp3.Ramp3Delay = RampDelay;
  206. rmp3.Out = CmtnPeriodSetpt;
  207. rmp3.Ramp3Min = 0x00000010;
  208. InitVar();
  209. //Call HVDMC Protection function
  210. HVDMC_Protection();
  211. EnableDog();
  212. // IDLE loop. Just sit and loop forever:
  213. for(;;) //infinite loop
  214. {
  215. //BackTicker++;
  216. taskfree();
  217. // State machine entry & exit point
  218. //===========================================================
  219. //(*Alpha_State_Ptr)(); // jump to an Alpha state (A0,B0,...)
  220. //===========================================================
  221. }
  222. } //END MAIN CODE
  223. //=================================================================================
  224. // STATE-MACHINE SEQUENCING AND SYNCRONIZATION FOR SLOW BACKGROUND TASKS
  225. //=================================================================================
  226. // ==============================================================================
  227. // =============================== MAIN ISR =====================================
  228. // ==============================================================================
  229. Uint32 IsrTicker=0;
  230. interrupt void MainISR(void)
  231. {
  232. //--------------------------------------------------------------------------------------
  233. // Verifying the ISR
  234. IsrTicker++;
  235. CanMaster();
  236. SerialCommsTimer++;
  237. if(SerialCommsTimer>0x3FFF){
  238. SerialCommsTimer = 0x3FFF;
  239. }
  240. ServiceDog();
  241. speed_cal();
  242. pwmlimit_speed();
  243. taskfree();
  244. FaultTreat();
  245. #if (BUILDLEVEL==LEVEL1)
  246. // ------------------------------------------------------------------------------
  247. // Connect inputs of the RMP3 module and call the RAMP Control 3 macro.
  248. // ------------------------------------------------------------------------------
  249. rmp3.DesiredInput = CmtnPeriodTarget;
  250. rmp3.Ramp3Delay = RampDelay;
  251. RC3_MACRO(&rmp3);
  252. // ------------------------------------------------------------------------------
  253. // Connect inputs of the IMPULSE module and call the Impulse macro.
  254. // ------------------------------------------------------------------------------
  255. impl1.Period = rmp3.Out;
  256. IMPULSE_MACRO(&impl1);
  257. HALL3_READ_MACRO(&hall1);
  258. // ------------------------------------------------------------------------------
  259. // Connect inputs of the MOD6 module and call the Mod 6 counter macro.
  260. // ------------------------------------------------------------------------------
  261. mod1.TrigInput = impl1.Out;
  262. MOD6CNT_MACRO(&mod1);
  263. // ------------------------------------------------------------------------------
  264. // Connect inputs of the PWM_DRV module and call the PWM signal generation
  265. // update macro.
  266. // ------------------------------------------------------------------------------
  267. pwm1.CmtnPointerIn = test2;//(int16)mod1.Counter;
  268. if(EnableFlag == 0){
  269. pwm1.DutyFuncIn = 0;
  270. }else{
  271. pwm1.DutyFuncIn = test;
  272. }
  273. BLDCPWM_MACRO(1,2,3,&pwm1);
  274. #endif // (BUILDLEVEL==LEVEL1)
  275. // =============================== LEVEL 3 ======================================
  276. // Level 3 describes the closed-loop operation of sensored trapezoidal
  277. // drive of BLDC motor using Hall sensor.
  278. // ==============================================================================
  279. #if (BUILDLEVEL==LEVEL3)
  280. // ------------------------------------------------------------------------------
  281. // ADC conversion and offset adjustment (observing back-emfs is optinal for this prj.)
  282. // ------------------------------------------------------------------------------
  283. BemfA = _IQ12toIQ(AdcResult.ADCRESULT1);
  284. BemfB = _IQ12toIQ(AdcResult.ADCRESULT2);
  285. BemfC = _IQ12toIQ(AdcResult.ADCRESULT3);
  286. DCbus_current = _IQ12toIQ(AdcResult.ADCRESULT4);//-_IQ(0.5); //1.65V offset added on HVDMC board.
  287. // ------------------------------------------------------------------------------
  288. // Connect inputs of the RMP3 module and call the Ramp control 3 macro.
  289. // ------------------------------------------------------------------------------
  290. HALL3_READ_MACRO(&hall1);
  291. ClosedFlag=TRUE;
  292. // ------------------------------------------------------------------------------
  293. // Connect inputs of the RMP2 module and call the Ramp control 2 macro.
  294. // ------------------------------------------------------------------------------
  295. rmp2.DesiredInput = (int32)DFuncDesired;
  296. RC2_MACRO(&rmp2);
  297. // ------------------------------------------------------------------------------
  298. // Connect inputs of the PWM_DRV module and call the PWM signal generation
  299. // update macro.
  300. // ------------------------------------------------------------------------------
  301. if (ClosedFlag==TRUE) {
  302. if (hall1.CmtnTrigHall==0x7FFF) {
  303. PreviousState = pwm1.CmtnPointer;
  304. // Comment the following if-else-if statements in case of
  305. // inverted Hall logics for commutation states.
  306. /* if (hall1.HallGpioAccepted==5)
  307. { pwm1.CmtnPointer = 0;
  308. }
  309. else if (hall1.HallGpioAccepted==1)
  310. { pwm1.CmtnPointer = 1;
  311. }
  312. else if (hall1.HallGpioAccepted==3)
  313. { pwm1.CmtnPointer = 2;
  314. }
  315. else if (hall1.HallGpioAccepted==2)
  316. { pwm1.CmtnPointer = 3;
  317. }
  318. else if (hall1.HallGpioAccepted==6)
  319. { pwm1.CmtnPointer = 4;
  320. }
  321. else if (hall1.HallGpioAccepted==4)
  322. {
  323. pwm1.CmtnPointer = 5;
  324. }*/
  325. // Comment the following if-else-if statements in case of
  326. // non-inverted Hall logics for commutation states.
  327. if (hall1.HallGpioAccepted==2)
  328. pwm1.CmtnPointer = 0;
  329. else if (hall1.HallGpioAccepted==6)
  330. pwm1.CmtnPointer = 1;
  331. else if (hall1.HallGpioAccepted==4)
  332. pwm1.CmtnPointer = 2;
  333. else if (hall1.HallGpioAccepted==5)
  334. pwm1.CmtnPointer = 3;
  335. else if (hall1.HallGpioAccepted==1)
  336. pwm1.CmtnPointer = 4;
  337. else if (hall1.HallGpioAccepted==3)
  338. pwm1.CmtnPointer = 5;
  339. } //hall1.CmtnTrigHall == 0x7FFF
  340. } // ClosedFlag==TRUE
  341. else{
  342. pwm1.CmtnPointer = (int16)mod1.Counter;
  343. }
  344. pwm1.DutyFunc = DfuncTesting;
  345. BLDCPWM_MACRO(1,2,3,&pwm1);
  346. // ------------------------------------------------------------------------------
  347. // Connect inputs of the SPEED_PR module and call the speed calculation macro.
  348. // ------------------------------------------------------------------------------
  349. if ((pwm1.CmtnPointer==5)&&(PreviousState==4)&&(hall1.CmtnTrigHall==0x7FFF))
  350. {
  351. speed1.TimeStamp = VirtualTimer;
  352. SPEED_PR_MACRO(&speed1);
  353. test = (test ==0 )? 1 : 0;
  354. }
  355. // ------------------------------------------------------------------------------
  356. // Connect inputs of the PWMDAC module
  357. // ------------------------------------------------------------------------------
  358. pwmdac1.MfuncC1 = (int32)hall1.HallGpioAccepted<<20;
  359. pwmdac1.MfuncC2 = BemfA;
  360. PWMDAC_MACRO(6,pwmdac1) // PWMDAC 6A, 6B
  361. pwmdac1.MfuncC1 = DCbus_current;
  362. pwmdac1.MfuncC2 = (int32)mod1.Counter<<20;
  363. PWMDAC_MACRO(7,pwmdac1) // PWMDAC 7A, 7B
  364. // ------------------------------------------------------------------------------
  365. // Connect inputs of the DATALOG module
  366. // ------------------------------------------------------------------------------
  367. DlogCh2 = (int16)mod1.Counter;
  368. DlogCh1 = (int16)hall1.HallGpioAccepted;
  369. DlogCh3 = (int16)_IQtoIQ15(BemfA);
  370. DlogCh4 = (int16)_IQtoIQ15(BemfB);
  371. #endif // (BUILDLEVEL==LEVEL3)
  372. // =============================== LEVEL 4 ======================================
  373. // Level 4 verifies the closed current loop and current PI controller.
  374. // ==============================================================================
  375. //-------------平均电流和can-------------------------------
  376. static _iq last_DC_current=0;
  377. static Uint16 DC_Current_cnt = 0;
  378. //static int32 DC_Current_sum = 0;
  379. static int32 DC_Current_sum_filter = 0;
  380. DC_current_real = (Uint32)AdcResult.ADCRESULT5*33;//Q12 I真实 = adc * 3.3 / 4096 /0.1
  381. DC_current_filer = lowpassfilter(last_DC_current,DC_current_real,_IQ12(1),_IQ12(0.05));
  382. last_DC_current = DC_current_filer;
  383. carveData1();
  384. CanCurve();
  385. UserCANprocess();
  386. if(Fault_clear == 1) {
  387. static int16 fault_cnt = 0;
  388. GpioDataRegs.GPBCLEAR.bit.GPIO32 = 1;
  389. FaultFlag.bit.OverCurFlag = 0;
  390. fault_cnt++;
  391. if(fault_cnt>20000){
  392. Fault_clear = 0;
  393. fault_cnt = 0;
  394. }
  395. }else{
  396. GpioDataRegs.GPBSET.bit.GPIO32 = 1;
  397. }
  398. DC_Current_sum_filter += DC_current_filer*pwm1.DutyFunc>>15;
  399. // DC_Current_sum += DC_current_real*pwm1.DutyFunc>>15;
  400. DC_Current_cnt++;//由于stall最多16384/20K秒 所以最多0.819715秒计算一次平均值
  401. #if (BUILDLEVEL==LEVEL4)
  402. BemfA = AdcResult.ADCRESULT1;
  403. BemfB = AdcResult.ADCRESULT2;
  404. BemfC = AdcResult.ADCRESULT3;
  405. HALL3_READ_MACRO(&hall1);
  406. if (hall1.Revolutions>=0)
  407. ClosedFlag=TRUE;
  408. // ------------------------------------------------------------------------------
  409. // 电流闭环
  410. // ------------------------------------------------------------------------------
  411. pid1_spd.Ref = _IQ(0.3);
  412. pid1_spd.Fbk = _IQabs(speed1.Speed);
  413. // tempIdc=pid1_idc.Fbk;
  414. pid1_idc.Umax = pwmlimit<<9;
  415. pid1_idc.Umin = (-pwmlimit)<<9;
  416. pid1_idc.Ref = CurrentSet;//给定电流 反转给-
  417. if(speed1.SpeedRpm<0){
  418. pid1_idc.Fbk = -_IQ12toIQ(DC_current_filter_avr);// 电流改为Q15格式
  419. }else{
  420. pid1_idc.Fbk = _IQ12toIQ(DC_current_filter_avr);// 电流改为Q15格式
  421. }
  422. // if(pid1_idc.Fbk<0) pid1_idc.Fbk=tempIdc; // Eliminate negative values
  423. step++;
  424. if(step == 1){
  425. PI_MACRO(&pid1_idc);
  426. }else if(step == 2){
  427. PI_MACRO(&pid1_spd);
  428. step = 0;
  429. }
  430. if(EnableFlag == 0){
  431. SetPI(&pid1_spd);
  432. resetPI(&pid1_idc);
  433. rmp2.DesiredInput = 0;
  434. RC2_MACRO(&rmp2);
  435. pwm1.DutyFuncIn = (int16)_IQtoIQ15(rmp2.Out); // controlled speed duty-cycle
  436. }else{
  437. pwm1.DutyFuncIn = (int16)_IQtoIQ15(_IQmpy(pid1_idc.Out,pid1_spd.Out)); // controlled speed duty-cycle
  438. rmp2.Out = pwm1.DutyFuncIn;
  439. // pwm1.DutyFuncIn = (int16)(_IQtoIQ15(pid1_idc.Out));
  440. }
  441. if (ClosedFlag==TRUE)
  442. {
  443. if (hall1.CmtnTrigHall==0x7FFF)
  444. {
  445. DC_current_avr = DC_Current_sum/DC_Current_cnt;
  446. DC_current_filter_avr = DC_Current_sum_filter/DC_Current_cnt;
  447. DC_Current_sum = 0;
  448. DC_Current_sum_filter =0;
  449. DC_Current_cnt = 0;
  450. //计算速度
  451. speed1.TimeStamp = VirtualTimer;
  452. SPEED_PR_MACRO(&speed1,hall1.HallGpioAccepted,PreviousState,hall1.Stall_status);
  453. PreviousState = hall1.HallGpioAccepted;
  454. if (hall1.HallGpioAccepted==2)
  455. pwm1.CmtnPointerIn = 0;
  456. else if (hall1.HallGpioAccepted==6)
  457. pwm1.CmtnPointerIn = 1;
  458. else if (hall1.HallGpioAccepted==4)
  459. pwm1.CmtnPointerIn = 2;
  460. else if (hall1.HallGpioAccepted==5)
  461. pwm1.CmtnPointerIn = 3;
  462. else if (hall1.HallGpioAccepted==1)
  463. pwm1.CmtnPointerIn = 4;
  464. else if (hall1.HallGpioAccepted==3)
  465. { pwm1.CmtnPointerIn = 5;
  466. }//hall1.HallGpioAccepted==3
  467. }
  468. }
  469. BLDCPWM_MACRO(1,2,3,&pwm1);
  470. #endif // (BUILDLEVEL==LEVEL4)
  471. // =============================== LEVEL 5 ======================================
  472. // Level 5 verifies the closed speed loop and speed PI controller.
  473. // ==============================================================================
  474. #if (BUILDLEVEL==LEVEL5)
  475. // ------------------------------------------------------------------------------
  476. // ADC conversion and offset adjustment (observing back-emfs is optinal for this prj.)
  477. // ------------------------------------------------------------------------------
  478. BemfA = AdcResult.ADCRESULT1;
  479. BemfB = AdcResult.ADCRESULT2;
  480. BemfC = AdcResult.ADCRESULT3;
  481. //DCbus_current = _IQ12toIQ(AdcResult.ADCRESULT5); //采样值<<12
  482. rc1.TargetValue = SpeedRef;
  483. RC_MACRO(&rc1);
  484. HALL3_READ_MACRO(&hall1);
  485. pid1_spd.Ref = rc1.SetpointValue;
  486. pid1_spd.Fbk = speed1.Speed;
  487. if(EnableFlag == 0){
  488. resetPI(&pid1_spd);
  489. rmp2.DesiredInput = 0;
  490. RC2_MACRO(&rmp2);
  491. pwm1.DutyFuncIn = (int16)_IQtoIQ15(rmp2.Out); // controlled speed duty-cycle
  492. }else{
  493. pid1_spd.Umax = _IQ((float)pwmlimit/32767);
  494. pid1_spd.Umin = _IQ((float)(-pwmlimit)/32767);
  495. PI_MACRO(&pid1_spd);
  496. if((int16)_IQtoIQ15(pid1_spd.Out)>pwmlimit){
  497. pwm1.DutyFuncIn = pwmlimit; // controlled speed duty-cycle
  498. }else if((int16)_IQtoIQ15(pid1_spd.Out)<-pwmlimit){
  499. pwm1.DutyFuncIn = -pwmlimit; // controlled speed duty-cycle
  500. }else{
  501. pwm1.DutyFuncIn = (int16)_IQtoIQ15(pid1_spd.Out); // controlled speed duty-cycle
  502. }
  503. //pwm1.DutyFuncIn = test;
  504. rmp2.Out = pwm1.DutyFuncIn;
  505. }
  506. if(pwm1.DutyFunc>0){
  507. OpenPwm();
  508. }else{
  509. ClosePwm();
  510. }
  511. ClosedFlag = TRUE;
  512. if (ClosedFlag==TRUE)
  513. {
  514. if (hall1.CmtnTrigHall==0x7FFF)
  515. {
  516. // DC_current_avr = DC_Current_sum/DC_Current_cnt;
  517. DC_current_filter_avr = DC_Current_sum_filter/DC_Current_cnt;
  518. // DC_Current_sum = 0;
  519. DC_Current_sum_filter =0;
  520. DC_Current_cnt = 0;
  521. speed_direction(hall1.HallGpioAccepted,PreviousState);
  522. if (hall1.HallGpioAccepted==5)
  523. { pwm1.CmtnPointerIn = 5;
  524. speed1.TimeStamp = VirtualTimer;
  525. // SPEED_PR_MACRO(&speed1,hall1.HallGpioAccepted,PreviousState,hall1.Stall_status);
  526. }
  527. else if (hall1.HallGpioAccepted==1)
  528. { pwm1.CmtnPointerIn = 0;
  529. }
  530. else if (hall1.HallGpioAccepted==3)
  531. { pwm1.CmtnPointerIn = 1;
  532. }
  533. else if (hall1.HallGpioAccepted==2)
  534. { pwm1.CmtnPointerIn = 2;
  535. }
  536. else if (hall1.HallGpioAccepted==6)
  537. { pwm1.CmtnPointerIn = 3;
  538. }
  539. else if (hall1.HallGpioAccepted==4)
  540. {
  541. pwm1.CmtnPointerIn = 4;
  542. }
  543. PreviousState = hall1.HallGpioAccepted;
  544. /*
  545. if (hall1.HallGpioAccepted==2)
  546. pwm1.CmtnPointerIn = 0;
  547. else if (hall1.HallGpioAccepted==6)
  548. pwm1.CmtnPointerIn = 1;
  549. else if (hall1.HallGpioAccepted==4)
  550. pwm1.CmtnPointerIn = 2;
  551. else if (hall1.HallGpioAccepted==5)
  552. pwm1.CmtnPointerIn = 3;
  553. else if (hall1.HallGpioAccepted==1)
  554. pwm1.CmtnPointerIn = 4;
  555. else if (hall1.HallGpioAccepted==3)
  556. { pwm1.CmtnPointerIn = 5;
  557. }//hall1.HallGpioAccepted==3
  558. */
  559. //}//direction == Normal
  560. } // delay
  561. } // ClosedFlag==TRUE
  562. BLDCPWM_MACRO(1,2,3,&pwm1);
  563. // ------------------------------------------------------------------------------
  564. // Connect inputs of the PWMDAC module
  565. // ------------------------------------------------------------------------------
  566. pwmdac1.MfuncC1 = BemfA;
  567. pwmdac1.MfuncC2 = BemfB;
  568. PWMDAC_MACRO(6,pwmdac1) // PWMDAC 6A, 6B
  569. pwmdac1.MfuncC1 = DCbus_current;
  570. pwmdac1.MfuncC2 = mod1.Counter<<20;
  571. PWMDAC_MACRO(7,pwmdac1) // PWMDAC 7A, 7B
  572. // ------------------------------------------------------------------------------
  573. // Connect inputs of the DATALOG module
  574. // ------------------------------------------------------------------------------
  575. #endif // (BUILDLEVEL==LEVEL5)
  576. // ------------------------------------------------------------------------------
  577. // Call the DATALOG update function.
  578. // ------------------------------------------------------------------------------
  579. // ------------------------------------------------------------------------------
  580. // Increase virtual timer and force 15 bit wrap around
  581. // ------------------------------------------------------------------------------
  582. VirtualTimer++;
  583. VirtualTimer &= 0x00007FFF;
  584. IsrTime = MastIsrTimePercent();
  585. // Acknowledge interrupt to recieve more interrupts from PIE group 1
  586. CpuTimer0.RegsAddr->TCR.bit.TIF=1;
  587. PieCtrlRegs.PIEACK.bit.ACK1 = 1;
  588. //PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
  589. }// ISR Ends Here
  590. /**********************************************************/
  591. /***************Protection Configuration*******************/
  592. /**********************************************************/
  593. void HVDMC_Protection(void)
  594. {
  595. // Configure Trip Mechanism for the Motor control software
  596. // -Cycle by cycle trip on CPU halt
  597. // -One shot IPM trip zone trip
  598. // These trips need to be repeated for EPWM1 ,2 & 3
  599. //===========================================================================
  600. //Motor Control Trip Config, EPwm1,2,3
  601. //===========================================================================
  602. EALLOW;
  603. // CPU Halt Trip 使能TZ6周期触发,周期触发可以自动清零 但是一次触发不能自动清零
  604. // EPwm1Regs.TZSEL.bit.CBC6=0x1;//Enable TZ6 as a CBC trip source for this ePWM module
  605. // EPwm2Regs.TZSEL.bit.CBC6=0x1;
  606. // EPwm3Regs.TZSEL.bit.CBC6=0x1;
  607. //使能故障的单次触发
  608. EPwm1Regs.TZSEL.bit.OSHT1 = 1; //enable TZ1 for OSHT
  609. EPwm2Regs.TZSEL.bit.OSHT1 = 1; //enable TZ1 for OSHT
  610. EPwm3Regs.TZSEL.bit.OSHT1 = 1; //enable TZ1 for OSHT
  611. EPwm1Regs.TZEINT.bit.OST = 1;
  612. // What do we want the OST/CBC events to do?
  613. // TZA events can force EPWMxA
  614. // TZB events can force EPWMxB
  615. //故障发生时的输出状态为低
  616. EPwm1Regs.TZCTL.bit.TZA = TZ_FORCE_LO; // EPWMxA will go low
  617. EPwm1Regs.TZCTL.bit.TZB = TZ_FORCE_LO; // EPWMxB will go low
  618. EPwm2Regs.TZCTL.bit.TZA = TZ_FORCE_LO; // EPWMxA will go low
  619. EPwm2Regs.TZCTL.bit.TZB = TZ_FORCE_LO; // EPWMxB will go low
  620. EPwm3Regs.TZCTL.bit.TZA = TZ_FORCE_LO; // EPWMxA will go low
  621. EPwm3Regs.TZCTL.bit.TZB = TZ_FORCE_LO; // EPWMxB will go low
  622. EDIS;
  623. // Clear any spurious OV trip 清除标志位
  624. EPwm1Regs.TZCLR.bit.OST = 1;
  625. EPwm2Regs.TZCLR.bit.OST = 1;
  626. EPwm3Regs.TZCLR.bit.OST = 1;
  627. //************************** End of Prot. Conf. ***************************//
  628. }
  629. //速度计算中断
  630. interrupt void xint2_isr(void)
  631. {
  632. static int32 last_XintTime = TIMER1_PER;
  633. if(CpuTimer1.RegsAddr->TCR.bit.TIF == 1){
  634. // 速度为0
  635. XintTime = (int32)TIMER1_PER;
  636. CpuTimer1.RegsAddr->TCR.bit.TIF = 1;//写1清中断标志位
  637. }else{
  638. XintTime = (int32)TIMER1_PER - CpuTimer1.RegsAddr->TIM.all;
  639. }
  640. if(XintTime<30000){
  641. //filter 防抖
  642. XintTime = last_XintTime;
  643. }
  644. last_XintTime = XintTime;
  645. if(Direction == -1){
  646. XintTime = -XintTime;
  647. }
  648. Xint2Cnt = 1;
  649. CpuTimer1.RegsAddr->TCR.bit.TRB = 1; //重新装载
  650. CpuTimer1.RegsAddr->TCR.bit.TSS = 0;//定时器restart
  651. PieCtrlRegs.PIEACK.bit.ACK1 =1;
  652. }
  653. interrupt void xint1_isr(void)
  654. {
  655. FaultFlag.bit.IgbtTempFaultFlag = 1;
  656. PieCtrlRegs.PIEACK.bit.ACK1 =1;
  657. // PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
  658. }
  659. interrupt void EPWM1TZint_isr(void)
  660. {
  661. //過流
  662. FaultFlag.bit.OverCurFlag = 1;
  663. EPwm1Regs.TZCLR.bit.OST = 1;
  664. EPwm1Regs.TZCLR.bit.INT = 1;
  665. PieCtrlRegs.PIEACK.bit.ACK2 = 1;//PIEACK_GROUP2;
  666. }
  667. void ServiceDog(void)
  668. {
  669. EALLOW;
  670. SysCtrlRegs.WDKEY = 0x0055;
  671. SysCtrlRegs.WDKEY = 0x00AA;
  672. EDIS;
  673. }
  674. //---------------------------------------------------------------------------
  675. // Example: DisableDog:
  676. //---------------------------------------------------------------------------
  677. // This function disables the watchdog timer.
  678. //---------------------------------------------------------------------------
  679. // 使能看门狗
  680. //---------------------------------------------------------------------------
  681. void EnableDog(void)
  682. {
  683. EALLOW;
  684. SysCtrlRegs.WDCR= 0x0028;
  685. EDIS;
  686. }
  687. void OpenPwm(void)
  688. {
  689. EALLOW;
  690. EPwm1Regs.TZCLR.bit.OST = 1; //EnablePWM1
  691. EPwm2Regs.TZCLR.bit.OST = 1; //EnablePWM2
  692. EPwm3Regs.TZCLR.bit.OST = 1; //EnablePWM3
  693. EPwm1Regs.TZCLR.bit.INT = 1;
  694. EDIS;
  695. }
  696. void ClosePwm(void)
  697. {
  698. EALLOW;
  699. EPwm1Regs.TZFRC.bit.OST = 0x1; //disabPWMl
  700. EPwm2Regs.TZFRC.bit.OST = 0x1; //disabPWM2
  701. EPwm3Regs.TZFRC.bit.OST = 0x1; //disabPWM3
  702. EDIS;
  703. }
  704. void speed_direction(Uint16 hall,Uint16 hall_last){
  705. if(hall_last == 2 && hall == 6 || hall_last == 6 && hall == 4 ||hall_last == 4 && hall == 5 ||hall_last == 5 && hall == 1||hall_last == 1 && hall == 3||hall_last == 3 && hall == 2)
  706. {
  707. Direction = 1;
  708. }
  709. else if(hall_last == 4 && hall == 6 || hall_last == 6 && hall == 2 ||hall_last == 2 && hall == 3 ||hall_last == 3 && hall == 1||hall_last == 1 && hall == 5|| hall_last == 5 && hall == 4)
  710. {
  711. Direction = -1;
  712. }
  713. }
  714. void speed_cal(void){
  715. /* int32 RotarfreOut = 0;
  716. int32 RotarfreqNew = 0;//电周期频率
  717. static int32 RotarfreSum = 0;
  718. static int32 RotarfreqArr[8] = {0,0,0,0,0,0,0,0};
  719. static int16 j = 0;*/
  720. int32 RotarRPMNew = 0;
  721. int32 RotarrpmOut = 0;
  722. static int32 RotarrpmSum = 0;
  723. static int32 RotarrpmArr[16] = {0,0,0,0,0,0,0,0,0,0,
  724. 0,0,0,0,0,0};
  725. static int16 k = 0;
  726. if(CpuTimer1.RegsAddr->TCR.bit.TIF == 1){
  727. // 速度为0
  728. XintTime = TIMER1_PER;
  729. Xint2Cnt = 1;
  730. CpuTimer1.RegsAddr->TCR.bit.TIF = 1;//写1清零
  731. // RotarfreqNew =0;
  732. RotarRPMNew =0;
  733. }else{
  734. //RotarfreqNew = (int32)(30000000)/XintTime;//60M clock /N
  735. RotarRPMNew =(int32)360000000/XintTime;
  736. }
  737. if(Xint2Cnt == 1){
  738. Xint2Cnt = 0;
  739. /* RotarfreqArr[j] = RotarfreqNew;
  740. RotarfreSum += RotarfreqArr[j];
  741. RotarfreOut = RotarfreSum >> 3;
  742. j++;
  743. if (j >= 8)
  744. {
  745. j = 0;
  746. }
  747. RotarfreSum -= RotarfreqArr[j];
  748. speed1.Speed = _IQdiv(RotarfreOut,BASE_FREQ);
  749. speed1.SpeedRpm = _IQmpy(speed1.BaseRpm,speed1.Speed);
  750. */
  751. RotarrpmArr[k] = RotarRPMNew;
  752. RotarrpmSum += RotarrpmArr[k];
  753. RotarrpmOut = RotarrpmSum>>4;
  754. k++;
  755. if (k >= 16)
  756. {
  757. k = 0;
  758. }
  759. RotarrpmSum -= RotarrpmArr[k];
  760. speed1.Speed = _IQdiv(RotarrpmOut,speed1.BaseRpm);
  761. speed1.SpeedRpm = RotarrpmOut;
  762. }
  763. }
  764. void pwmlimit_speed(void){
  765. if(_IQabs(speed1.SpeedRpm)< 200){
  766. Uint16 index = _IQabs(speed1.SpeedRpm)/50;
  767. pwmlimit = (Uint32)(_IQabs(speed1.SpeedRpm)-index*50)*(pwm_limit_table1[index+1] - pwm_limit_table1[index])/50+pwm_limit_table1[index];
  768. }else if(_IQabs(speed1.SpeedRpm)< 3500){
  769. Uint16 index = _IQabs(speed1.SpeedRpm)/100;
  770. pwmlimit = (Uint32)(_IQabs(speed1.SpeedRpm)-index*100)*(pwm_limit_table2[index+1] - pwm_limit_table2[index])/100+pwm_limit_table2[index];
  771. }else{
  772. pwmlimit = 25240;//最大值
  773. }
  774. }
  775. void taskfree(void){
  776. if(SerialCommsTimer>4000){
  777. GpioDataRegs.GPATOGGLE.bit.GPIO22 = 1;
  778. SerialCommsTimer = 0;
  779. DCbus_voltage = AdcResult.ADCRESULT4/10;// U = Uadc * 0.099964
  780. _iq20 kb = _IQ20(18.26);
  781. Tvot = ((Uint32)AdcResult.ADCRESULT7 *kscaler - kb)>>20;
  782. int32 R = 10000;//分压电阻
  783. int32 Rtadc = AdcResult.ADCRESULT6;
  784. Rt= Rtadc*R/(4096-Rtadc);
  785. if(Rt>32116){
  786. Tmotor = 0;
  787. }
  788. else if(Rt>15652){
  789. Tmotor = (_IQ12(-0.901)*Rt/1000 + _IQ12(28.381))>>12;
  790. }else if(Rt>6523){
  791. Tmotor = (_IQ12(-2.5013)*Rt/1000 + _IQ12(50.657))>>12;
  792. }else if(Rt>2968){
  793. Tmotor = (_IQ12(-6.3342)*Rt/1000 + _IQ12(73.203))>>12;
  794. }else if(Rt>1228){
  795. Tmotor = (_IQ12(-16.002)*Rt/1000 + _IQ12(98.576))>>12;
  796. }else if(Rt > 657){
  797. Tmotor = (_IQ12(-38.551)*Rt/1000 + _IQ12(124.83))>>12;
  798. }else if(Rt > 324){
  799. Tmotor = (_IQ12(-81.812)*Rt/1000 + _IQ12(150.67))>>12;
  800. }else{
  801. Tmotor = 125;
  802. }
  803. }
  804. }
  805. int16 MastIsrTimePercent(void)
  806. {
  807. int16 MasterIsrT = 0;
  808. MasterIsrT = 3000 - CpuTimer0.RegsAddr->TIM.all;
  809. MasterIsrT = (Uint32)MasterIsrT* 100 /3000;
  810. return MasterIsrT;
  811. }