HVBLDC_Sensored.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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_S1 60
  23. #define TIMER1_S2 500000
  24. #define TIMER1_PER 30000000
  25. #define MAINLOOPDIV 5 // 主中断40K 40K/MAINLOOPDIV 主循环
  26. // Prototype statements for functions found within this file.
  27. interrupt void MainISR(void);//主中断
  28. interrupt void xint1_isr(void);//FO中断
  29. interrupt void xint2_isr(void);//HALL A中断
  30. interrupt void EPWM1TZint_isr(void);//TZ中断 过流中断
  31. void DeviceInit();
  32. void HVDMC_Protection(void);
  33. void speed_direction(Uint16 hall,Uint16 hall_last);//使用中断计算转速
  34. void speed_cal(void);//使用中断计算转速
  35. void pwmlimit_speed(void);//
  36. void ClosePwm(void);
  37. void bldcpwm_close(void);
  38. void OpenPwm(void);
  39. void mainLoop(void);
  40. // State Machine function prototypes
  41. //------------------------------------
  42. int16 MastIsrTimePercent(void);
  43. void taskfree(void);
  44. void ServiceDog(void);
  45. void EnableDog(void);
  46. // Used for running BackGround in flash, and ISR in RAM
  47. //速度计算相关
  48. int16 t4 = 0;
  49. int16 Xint2Cnt = 0;
  50. int16 hallCmtnTrig = 0;
  51. extern Uint16 *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;
  52. int16 SerialCommsTimer;
  53. // Global variables used in this system
  54. HALL3 hall1 = HALL3_DEFAULTS;
  55. SPEED_MEAS_CAP speed1 = SPEED_MEAS_CAP_DEFAULTS;
  56. PWMGEN pwm1 = PWMGEN_DEFAULTS;
  57. _iq20 kscaler = _IQ20(90*3.3/4096/2.3);
  58. float32 T = 0.001/ISR_FREQUENCY; // Samping period (sec), see parameter.h
  59. Uint16 BackTicker = 0;
  60. Uint16 PreviousState;
  61. Uint16 ClosedFlag = 0;
  62. Uint32 VirtualTimer = 0;
  63. Uint16 ILoopFlag = FALSE;
  64. Uint16 SpeedLoopFlag = FALSE;
  65. int16 DFuncDesired = 0x0300; // Desired duty cycle (Q15)
  66. int16 DfuncTesting = 500;//0x0300;//占空比
  67. int16 Delay30 = 0x3FFF;
  68. int16 DelayFlag = 0;
  69. Uint16 AlignFlag = 0x000F;
  70. Uint16 LoopCount = 0;
  71. Uint16 TripFlagDMC=0; //PWM trip status
  72. #if (BUILDLEVEL<= LEVEL2)
  73. Uint32 CmtnPeriodTarget = 0x00000500;
  74. Uint32 CmtnPeriodSetpt = 0x00002000;//要大于CmtnPeriodTarget才能使用--
  75. Uint32 RampDelay = 10;
  76. #else
  77. Uint32 CmtnPeriodTarget = 0x00000450;
  78. Uint32 CmtnPeriodSetpt = 0x00001500;
  79. Uint32 RampDelay = 10;
  80. #endif
  81. _iq DCbus_current=0;
  82. _iq Speedgd=0;
  83. _iq tempIdc=0;
  84. int32 Rt;
  85. Uint16 ch1=0;
  86. Uint16 ch2=0;
  87. Uint16 ch3=0;
  88. // Used for ADC Configuration
  89. int ChSel[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  90. int TrigSel[16] = {5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5};
  91. int ACQPS[16] = {8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8};
  92. // Instance PI regulator to regulate the DC-bus current and speed
  93. // Instance a PWM driver instance
  94. // Instance a PWM DAC driver instance
  95. PWMDAC pwmdac1 = PWMDAC_DEFAULTS;
  96. // Instance a Hall effect driver
  97. // Instance a ramp controller to smoothly ramp the frequency
  98. // Instance a RAMP2 Module
  99. RMP2 rmp2 = RMP2_DEFAULTS;
  100. // Instance a RAMP3 Module
  101. RMP3 rmp3 = RMP3_DEFAULTS;
  102. // Instance a MOD6 Module
  103. MOD6CNT mod1 = MOD6CNT_DEFAULTS;
  104. // Instance a IMPULSE Module
  105. IMPULSE impl1 = IMPULSE_DEFAULTS;
  106. // Instance a SPEED_PR Module
  107. // Create an instance of DATALOG Module 950:3A 但是只有外界扭矩低于36N才能启动起来
  108. /*//50N
  109. Uint16 pwm_limit_table1[5] = {950,1300,1650,1930,2320};//0 50 100 150 200
  110. //// 0, 100, 200, 300, 400 500 600 700 800 900 1000
  111. Uint16 pwm_limit_table2[]= {950,1650,2320,2985,3668,4348,5024,5705,6383,7061,7760,
  112. 8463,9141,9820,10510,11187,11859,12684,13216,13895,14558,//1100-2000
  113. 15280,15955,16635,17290,17970,18645,19320,19960,20635,21299,
  114. 21970,22640,23280,24000,24700,24700};
  115. Uint16 pwm_limit_table_neg[]= {950,1616,2200,2930,3614,4286,4961,5639,6313,6987,7670,
  116. 8348,9022,9700,10361,11080,11714,12397,13080,13743,14438,//1100-2000
  117. 15117,15730,16425,17092,17757,18434,19100,19781,20425,21081,
  118. 21743,22428,23097,23781,24402,24402};*/
  119. /*//55N 后来测试为50N
  120. Uint16 pwm_limit_table1[5] = {950,1330,1650,2000,2300};//0 50 100 150 200
  121. Uint16 pwm_limit_table1_NEG[5] = {950,1300,1650,1935,2285};
  122. Uint16 pwm_limit_table2[]= {950,1650,2300,3030,3750,4455,5130,5830,6515,7210,7900,
  123. 8580,9268,9990,10667,11363,12045,12725,13420,14120,14800,//1100-2000
  124. 15500,16196,16875,17555,18120,18800,19450,20175,20850,21665,
  125. 22320,22950,23610,24260,24945,24945};
  126. Uint16 pwm_limit_table_neg[]= {950,1300,2290,2980,3700,4370,5070,5770,6420,7120,7800,
  127. 8500,9180,9860,10540,11235,11910,12570,13260,13945,14600,//1100-2000
  128. 15280,15940,16615,17290,17925,18600,19280,19950,20610,21260,
  129. 21930,22550,23230,23890,24560,24560};*/
  130. //first test ----------
  131. /*Uint16 pwm_limit_table2[]= {950,1650,2320,2985,3668,4348,5024,5705,6383,7058,7739,
  132. 8449,9122,9800,10480,11163,11826,12499,13181,13860,14515,
  133. 15192,15872,16530,17203,17861,18537,19183,19848,20525,21161,
  134. 21838,22488,23216,23850,24507,24507};*/
  135. //// 0, 100, 200, 300, 400 500 600 700 800 900 1000
  136. /*Uint16 pwm_limit_table_neg[]= {950,1616,2272,2961,3641,4305,4983,5659,6326,6994,7670,
  137. 8348,9022,9700,10361,11502,11714,12379,13066,13727,14426,//1100-2000
  138. 15085,15610,16325,17005,17660,18338,19000,19693,20390,21048,
  139. 21706,22428,23104,22823,24434,24434};*/
  140. //first test ----------
  141. //---55Nm_2-----------------------------------------------------------
  142. Uint16 pwm_limit_table1[5] = {1000,1340,1680,2020,2360};//0 200
  143. Uint16 pwm_limit_table1_NEG[5] = {1000,1350,1740,2070,2400};//0 200
  144. Uint16 pwm_limit_table2[]= {1000,1680,2360,3080,3780,4480,5200,5920,6630,7345,8045,
  145. 8765,9480,10160,10860,11560,12280,13000,13680,14400,15040,//1100-2000
  146. 15740,16440,17110,17830,18510,19210,19910,20610,21260,21950,
  147. 22600,23300,23990,24630,25320,25320};
  148. Uint16 pwm_limit_table_neg[]= {1000,1740,2400,3100,3800,4480,5190,5900,6600,7300,8000,
  149. 8700,9400,10100,10790,11470,12170,12870,13570,14260,14970,//1100-2000
  150. 15620,16300,17000,17660,18340,19038,19715,20395,21070,21750,
  151. 22400,23070,23760,24420,25100,25100};
  152. //---59Nm_2-----------------------------------------------------------
  153. /*Uint16 pwm_limit_table1[5] = {1000,2440};//0 200
  154. //Uint16 pwm_limit_table1_NEG[5] = {1000,2500};//0 200
  155. Uint16 pwm_limit_table2[]= {1000,1720,2440,3190,3890,4560,5290,6000,6730,7430,8150,
  156. 8835,9550,10260,10990,11690,12410,13100,13830,14530,15210,//1100-2000
  157. 15930,16610,17310,18000,18660,19360,20000,20740,21430,22100,
  158. 22810,23500,24150,24800,25320,25320};
  159. Uint16 pwm_limit_table_neg[]= {1000,1850,2500,3210,3890,4590,5290,5990,6690,7400,8110,
  160. 8800,9480,10180,10880,11560,12260,12950,13640,14350,15040,//1100-2000
  161. 15730,16390,17090,17770,18470,19120,19820,20490,21180,21800,
  162. 22470,23160,23800,24420,25100,25100};*/
  163. //------------------------------------------------------------------
  164. int32 pwmlimitrate = 24507;
  165. //NTC
  166. _iq20 kb = _IQ20(18.26);
  167. _iq12 ktable[] = {_IQ12(-0.901),_IQ12(-2.5013),_IQ12(-6.3342),_IQ12(-16.002),_IQ12(-38.551),_IQ12(-81.812)};
  168. _iq12 btable[] = {_IQ12(28.381),_IQ12(50.657),_IQ12(73.203),_IQ12(98.576),_IQ12(124.83),_IQ12(150.67)};
  169. Uint32 IsrTicker=0;
  170. Uint32 startuptimer=0;
  171. void main(void)
  172. {
  173. DeviceInit(); // Device Life support & GPIO
  174. InitCan();
  175. // Only used if running from FLASH
  176. // Note that the variable FLASH is defined by the compiler
  177. #ifdef FLASH
  178. // Copy time critical code and Flash setup code to RAM
  179. // The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
  180. // symbols are created by the linker. Refer to the linker files.
  181. MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
  182. // Call Flash Initialization to setup flash waitstates
  183. // This function must reside in RAM
  184. InitFlash(); // Call the flash wrapper init function
  185. #endif //(FLASH)
  186. // Initialize all the Device Peripherals:
  187. // This function is found in DSP280x_CpuTimers.c
  188. InitCpuTimers();
  189. // Configure CPU-Timer 0 to interrupt every ISR Period:
  190. // 60MHz CPU Freq, ISR Period (in uSeconds)
  191. // This function is found in DSP280x_CpuTimers.c
  192. ConfigCpuTimer(&CpuTimer0, 60, 1000/ISR_FREQUENCY);//40K
  193. StartCpuTimer0();
  194. // Configure CPU-Timer 1,2 for background loops
  195. ConfigCpuTimer(&CpuTimer1, TIMER1_S1, TIMER1_S2);//2hz
  196. ConfigCpuTimer(&CpuTimer2, 60, 1000000);//20HZ
  197. StartCpuTimer1();
  198. StartCpuTimer2();
  199. // Reassign ISRs.
  200. // Reassign the PIE vector for TINT0 to point to a different
  201. // ISR then the shell routine found in DSP280x_DefaultIsr.c.
  202. // This is done if the user does not want to use the shell ISR routine
  203. // but instead wants to use their own ISR.
  204. EALLOW; // This is needed to write to EALLOW protected registers
  205. PieVectTable.TINT0 = &MainISR;//40KHZ
  206. PieVectTable.XINT1 = &xint1_isr;
  207. PieVectTable.XINT2 = &xint2_isr;
  208. PieVectTable.EPWM1_TZINT = &EPWM1TZint_isr;
  209. EDIS; // This is needed to disable write to EALLOW protected registers
  210. // Enable PIE group 1 interrupt 7 for TINT0
  211. PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
  212. PieCtrlRegs.PIEIER1.bit.INTx4 = 1; // Enable PIE Gropu 1 INT4 //XINT1
  213. PieCtrlRegs.PIEIER1.bit.INTx5 = 1; // Enable PIE Gropu 1 INT5 //XINT2
  214. // Enable CPU INT1 for TINT0:
  215. IER |= M_INT1;
  216. PieCtrlRegs.PIEIER2.bit.INTx1 = 1;
  217. IER |= M_INT2;
  218. // Enable Global realtime interrupt DBGM
  219. EALLOW;
  220. GpioIntRegs.GPIOXINT1SEL.bit.GPIOSEL = 16; //定位到哪个引脚中断
  221. GpioIntRegs.GPIOXINT2SEL.bit.GPIOSEL = 24; // XINT2 is GPIO24
  222. EDIS;
  223. XIntruptRegs.XINT1CR.bit.POLARITY = 0; // 1为上升沿,0和2为下降沿,3为双边沿
  224. XIntruptRegs.XINT2CR.bit.POLARITY = 3; // 1为上升沿,0和2为下降沿,3为双边沿
  225. //中断配置步骤-----1,开启模块中断使能,
  226. XIntruptRegs.XINT1CR.bit.ENABLE = 1;
  227. XIntruptRegs.XINT2CR.bit.ENABLE = 1; // Enable XINT2
  228. // Enable global Interrupts and higher priority real-time debug events:
  229. EINT; // Enable Global interrupt INTM
  230. ERTM; // Enable Global realtime interrupt DBGM
  231. // Initialize PWM module
  232. pwm1.PeriodMax = (SYSTEM_FREQUENCY/PWM_FREQUENCY)*1000/2; // Asymmetric PWM 20kHZ 3000
  233. pwm1.DutyFunc = 0; // 占空比ALIGN_DUTY/32767 // DutyFunc = Q15
  234. BLDCPWM_INIT_MACRO(1,2,3,&pwm1);
  235. // Initialize PWMDAC module
  236. pwmdac1.PeriodMax = 500; // @60Mhz, 1500->20kHz, 1000-> 30kHz, 500->60kHz
  237. pwmdac1.HalfPerMax = pwmdac1.PeriodMax/2; // Needed to adjust the duty cycle range in the macro
  238. PWMDAC_INIT_MACRO(6,pwmdac1) ; // PWM 6A,6B
  239. PWMDAC_INIT_MACRO(7,pwmdac1) ; // PWM 7A,7B
  240. // Initialize Hall module
  241. hall1.DebounceAmount = 1;
  242. hall1.Revolutions = 1;
  243. HALL3_INIT_MACRO(&hall1);//读hall值 初始化HallGpioBuffer,HallGpioAccepted
  244. // Initialize the SPEED_PR module
  245. speed1.InputSelect = 0;
  246. speed1.BaseRpm = (Uint32)120*BASE_FREQ/POLES;//(Uint32)120*BASE_FREQ/POLES/6;//// 使用一个换相周期//额定转速 = 60*(基频/(极数/2)) = 60*f/极对数
  247. speed1.SpeedScaler = (Uint32)((Uint32)ISR_FREQUENCY*10000/BASE_FREQ);//保留一位小数//40000/f
  248. // For the kits < Rev 1.1 -------------------------------------------------
  249. ChSel[0]=15; // Dummy meas. avoid 1st sample issue Rev0 Picollo
  250. ChSel[1]=2; // ChSelect: ADC B7-> Phase A Voltage
  251. ChSel[2]=3; // ChSelect: ADC B6-> Phase B Voltage
  252. ChSel[3]=4; // ChSelect: ADC B4-> Phase C Voltage
  253. ChSel[4]=7; // ChSelect: ADC A2-> DC Bus vol
  254. ChSel[5]=6; // DC current 放大20倍 Imeasure = I*5mΩ*20
  255. ChSel[6]=5; // NTC
  256. ChSel[7]=1; // VOT
  257. //-------------------------------------------------------------------------
  258. ADC_MACRO_INIT(ChSel,TrigSel,ACQPS);
  259. // Initialize RMP2 module
  260. rmp2.Out = (int32)0;
  261. rmp2.Ramp2Delay = 0x00000001;
  262. rmp2.Ramp2Max = 32767/2;
  263. rmp2.Ramp2Min = -32768/2;
  264. // Initialize RMP3 module
  265. rmp3.DesiredInput = CmtnPeriodTarget;
  266. rmp3.Ramp3Delay = RampDelay;
  267. rmp3.Out = CmtnPeriodSetpt;
  268. rmp3.Ramp3Min = 0x00000010;
  269. InitVar();
  270. //Call HVDMC Protection function
  271. HVDMC_Protection();
  272. EnableDog();
  273. // IDLE loop. Just sit and loop forever:
  274. for(;;) //infinite loop
  275. {
  276. if(IsrTicker%MAINLOOPDIV ==0)
  277. {//8Khz
  278. mainLoop();
  279. }
  280. }
  281. } //END MAIN CODE
  282. //=================================================================================
  283. // STATE-MACHINE SEQUENCING AND SYNCRONIZATION FOR SLOW BACKGROUND TASKS
  284. //=================================================================================
  285. // ==============================================================================
  286. // =============================== MAIN ISR =====================================
  287. // ==============================================================================
  288. interrupt void MainISR(void)
  289. {
  290. //--------------------------------------------------------------------------------------
  291. ServiceDog();
  292. // Verifying the ISR
  293. startuptimer++;
  294. startuptimer = (startuptimer>60000)? 60000 : startuptimer;//1.5S
  295. IsrTicker++;
  296. IsrTicker = (IsrTicker>20000)? 0 : IsrTicker;
  297. VirtualTimer++;
  298. VirtualTimer &= 0x00007FFF;
  299. HALL3_READ_MACRO(&hall1);
  300. ClosedFlag = TRUE;
  301. if (ClosedFlag==TRUE)
  302. {
  303. if (hall1.CmtnTrigHall==0x7FFF)
  304. {
  305. hallCmtnTrig = 1;
  306. speed_direction(hall1.HallGpioAccepted,PreviousState);
  307. if (hall1.HallGpioAccepted==5)
  308. {
  309. pwm1.CmtnPointerIn = 5;
  310. }
  311. else if (hall1.HallGpioAccepted==1)
  312. { pwm1.CmtnPointerIn = 0;
  313. }
  314. else if (hall1.HallGpioAccepted==3)
  315. { pwm1.CmtnPointerIn = 1;
  316. }
  317. else if (hall1.HallGpioAccepted==2)
  318. { pwm1.CmtnPointerIn = 2;
  319. }
  320. else if (hall1.HallGpioAccepted==6)
  321. { pwm1.CmtnPointerIn = 3;
  322. }
  323. else if (hall1.HallGpioAccepted==4)
  324. {
  325. pwm1.CmtnPointerIn = 4;
  326. }
  327. PreviousState = hall1.HallGpioAccepted;
  328. } // delay
  329. } // ClosedFlag==TRUE
  330. if(Enable_ALLOW == 0 && pwm1.DutyFunc==0){
  331. bldcpwm_close();
  332. }else{
  333. BLDCPWM_MACRO(1,2,3,&pwm1);
  334. }
  335. // Acknowledge interrupt to recieve more interrupts from PIE group 1
  336. CpuTimer0.RegsAddr->TCR.bit.TIF=1;
  337. PieCtrlRegs.PIEACK.bit.ACK1 = 1;
  338. //PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
  339. }// ISR Ends Here
  340. /**********************************************************/
  341. /***************Protection Configuration*******************/
  342. /**********************************************************/
  343. void HVDMC_Protection(void)
  344. {
  345. // Configure Trip Mechanism for the Motor control software
  346. // -Cycle by cycle trip on CPU halt
  347. // -One shot IPM trip zone trip
  348. // These trips need to be repeated for EPWM1 ,2 & 3
  349. //===========================================================================
  350. //Motor Control Trip Config, EPwm1,2,3
  351. //===========================================================================
  352. EALLOW;
  353. // CPU Halt Trip 使能TZ6周期触发,周期触发可以自动清零 但是一次触发不能自动清零
  354. // EPwm1Regs.TZSEL.bit.CBC6=0x1;//Enable TZ6 as a CBC trip source for this ePWM module
  355. // EPwm2Regs.TZSEL.bit.CBC6=0x1;
  356. // EPwm3Regs.TZSEL.bit.CBC6=0x1;
  357. //使能故障的单次触发
  358. EPwm1Regs.TZSEL.bit.OSHT1 = 1; //enable TZ1 for OSHT
  359. EPwm2Regs.TZSEL.bit.OSHT1 = 1; //enable TZ1 for OSHT
  360. EPwm3Regs.TZSEL.bit.OSHT1 = 1; //enable TZ1 for OSHT
  361. EPwm1Regs.TZEINT.bit.OST = 1;
  362. // What do we want the OST/CBC events to do?
  363. // TZA events can force EPWMxA
  364. // TZB events can force EPWMxB
  365. //故障发生时的输出状态为低
  366. EPwm1Regs.TZCTL.bit.TZA = TZ_FORCE_LO; // EPWMxA will go low
  367. EPwm1Regs.TZCTL.bit.TZB = TZ_FORCE_LO; // EPWMxB will go low
  368. EPwm2Regs.TZCTL.bit.TZA = TZ_FORCE_LO; // EPWMxA will go low
  369. EPwm2Regs.TZCTL.bit.TZB = TZ_FORCE_LO; // EPWMxB will go low
  370. EPwm3Regs.TZCTL.bit.TZA = TZ_FORCE_LO; // EPWMxA will go low
  371. EPwm3Regs.TZCTL.bit.TZB = TZ_FORCE_LO; // EPWMxB will go low
  372. EDIS;
  373. // Clear any spurious OV trip 清除标志位
  374. EPwm1Regs.TZCLR.bit.OST = 1;
  375. EPwm2Regs.TZCLR.bit.OST = 1;
  376. EPwm3Regs.TZCLR.bit.OST = 1;
  377. //************************** End of Prot. Conf. ***************************//
  378. }
  379. //速度计算中断
  380. interrupt void xint2_isr(void)
  381. {
  382. static int32 last_XintTime = TIMER1_PER;
  383. static Uint32 last_hall = 0;
  384. Uint32 temp = 0;
  385. if(CpuTimer1.RegsAddr->TCR.bit.TIF == 1){
  386. // 速度为0
  387. XintTime = (int32)TIMER1_PER;
  388. CpuTimer1.RegsAddr->TCR.bit.TIF = 1;//写1清中断标志位
  389. }else{
  390. XintTime = (int32)TIMER1_PER - CpuTimer1.RegsAddr->TIM.all;
  391. }
  392. //XintTime = (int32)TIMER1_PER - CpuTimer1.RegsAddr->TIM.all;
  393. temp = (GpioDataRegs.GPADAT.all>>24)&0x00000007; /* read all three GPIOs at once*/
  394. if(_IQabs(XintTime)<36000 || temp == last_hall ){// 不应该超过10000rpm 也不应该和上一次的hall相同 否则说明有振动
  395. //filter 防抖
  396. XintTime = XintTime + last_XintTime;
  397. PieCtrlRegs.PIEACK.bit.ACK1 =1;
  398. last_XintTime = XintTime;
  399. last_hall = temp;
  400. if(Direction == -1){
  401. XintTime = -XintTime;
  402. }
  403. return ;
  404. }
  405. last_XintTime = XintTime;
  406. last_hall = temp;
  407. if(Direction == -1){
  408. XintTime = -XintTime;
  409. }
  410. Xint2Cnt = 1;
  411. CpuTimer1.RegsAddr->TCR.bit.TRB = 1; //重新装载
  412. CpuTimer1.RegsAddr->TCR.bit.TSS = 0;//定时器restart
  413. PieCtrlRegs.PIEACK.bit.ACK1 =1;
  414. }
  415. interrupt void xint1_isr(void)
  416. {
  417. FaultFlag.bit.Ipmfault = 1;
  418. PieCtrlRegs.PIEACK.bit.ACK1 =1;
  419. // PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
  420. }
  421. interrupt void EPWM1TZint_isr(void)
  422. {
  423. //過流
  424. FaultFlag.bit.OverCurFlag = 1;
  425. EALLOW;
  426. // EPwm1Regs.TZCLR.bit.OST = 1;
  427. // EPwm1Regs.TZCLR.bit.INT = 1;
  428. EDIS;
  429. PieCtrlRegs.PIEACK.bit.ACK2 = 1;//PIEACK_GROUP2;
  430. }
  431. void ServiceDog(void)
  432. {
  433. EALLOW;
  434. SysCtrlRegs.WDKEY = 0x0055;
  435. SysCtrlRegs.WDKEY = 0x00AA;
  436. EDIS;
  437. }
  438. //---------------------------------------------------------------------------
  439. // Example: DisableDog:
  440. //---------------------------------------------------------------------------
  441. // This function disables the watchdog timer.
  442. //---------------------------------------------------------------------------
  443. // 使能看门狗
  444. //---------------------------------------------------------------------------
  445. void EnableDog(void)
  446. {
  447. EALLOW;
  448. SysCtrlRegs.WDCR= 0x0028;
  449. EDIS;
  450. }
  451. void OpenPwm(void)
  452. {
  453. EALLOW;
  454. EPwm1Regs.TZCLR.bit.OST = 1; //EnablePWM1
  455. EPwm2Regs.TZCLR.bit.OST = 1; //EnablePWM2
  456. EPwm3Regs.TZCLR.bit.OST = 1; //EnablePWM3
  457. EPwm1Regs.TZCLR.bit.INT = 1;
  458. EDIS;
  459. }
  460. void ClosePwm(void)
  461. {
  462. EALLOW;
  463. EPwm1Regs.TZFRC.bit.OST = 0x1; //disabPWMl
  464. EPwm2Regs.TZFRC.bit.OST = 0x1; //disabPWM2
  465. EPwm3Regs.TZFRC.bit.OST = 0x1; //disabPWM3
  466. EDIS;
  467. }
  468. void speed_direction(Uint16 hall,Uint16 hall_last){
  469. //防抖
  470. static int16 i = 0;
  471. static int16 j=0;
  472. 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)
  473. {
  474. i++;
  475. j--;
  476. if(j<0){
  477. j=0;
  478. }
  479. if(i>5){
  480. i = 6;
  481. Direction = 1;
  482. }
  483. }
  484. 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)
  485. {
  486. i--;
  487. if(i<0){
  488. i=0;
  489. }
  490. j++;
  491. if(j>5){
  492. j= 6;
  493. Direction = -1;
  494. }
  495. }
  496. }
  497. void speed_cal_filter(void){
  498. int32 RotarRPMNew = 0;
  499. int32 RotarrpmOut = 0;
  500. static int32 last_speed = 0;
  501. static int32 last_RotarRPMNew = 0;
  502. Uint16 Halltemp = (Uint16)((GpioDataRegs.GPADAT.all>>24)&0x00000007); /* read all three GPIOs at once*/
  503. static Uint16 Hall_1 = 0;
  504. //static Uint16 Hall_2 = 0;
  505. static int32 Hall_2_cnt = 0;
  506. int16 directiontemp = 0;
  507. if(CpuTimer1.RegsAddr->TCR.bit.TIF == 1){
  508. // 速度为0
  509. XintTime = TIMER1_PER;
  510. Xint2Cnt = 1;
  511. RotarRPMNew =0;
  512. last_speed = 0;
  513. CpuTimer1.RegsAddr->TCR.bit.TIF = 1;//写1清零
  514. }else{
  515. if(Halltemp == hall1.HallGpioAccepted){//hall读取不变
  516. Hall_2_cnt++;
  517. if(Hall_2_cnt>MAINLOOPFREQ/2){//0.5s
  518. Hall_2_cnt = MAINLOOPFREQ/2+1;
  519. }
  520. }else{
  521. Hall_2_cnt = 0;
  522. }
  523. if(_IQabs(XintTime) == TIMER1_PER || Hall_2_cnt>=4000){
  524. RotarRPMNew = 0;
  525. last_speed = 0;
  526. }else{
  527. RotarRPMNew =(int32)360000000/XintTime;//计算新速度
  528. }
  529. }
  530. if(Xint2Cnt == 1){
  531. //防止突然反向
  532. Xint2Cnt = 0;
  533. if(Hall_1 == 2 && Halltemp == 6 || Hall_1 == 6 && Halltemp == 4 ||Hall_1 == 4 && Halltemp == 5 ||Hall_1 == 5 && Halltemp == 1||Hall_1 == 1 && Halltemp == 3||Hall_1 == 3 && Halltemp == 2)
  534. {
  535. directiontemp = 1;
  536. }
  537. else if(Hall_1 == 4 && Halltemp == 6 || Hall_1 == 6 && Halltemp == 2 ||Hall_1 == 2 && Halltemp == 3 ||Hall_1 == 3 && Halltemp == 1||Hall_1 == 1 && Halltemp == 5|| Hall_1 == 5 && Halltemp == 4)
  538. {
  539. directiontemp = -1;
  540. }
  541. if((Direction ==1 && directiontemp == -1 )||(Direction == -1 && directiontemp == 1 )){
  542. //说明在振动
  543. RotarRPMNew = 0;
  544. last_speed = 0;
  545. }
  546. Hall_1 = Halltemp;
  547. }
  548. test2 = RotarRPMNew;
  549. if(_IQabs(RotarRPMNew - last_RotarRPMNew)>1500 &&(_IQabs(RotarRPMNew)<300 ||_IQabs(last_RotarRPMNew)<300) ){
  550. //在0转速附近有大的阶跃
  551. test = RotarRPMNew;
  552. RotarRPMNew = 0;
  553. last_speed = 0;
  554. last_RotarRPMNew = 0;
  555. }else if(_IQabs(RotarRPMNew - last_RotarRPMNew)>1500&&(_IQabs(RotarRPMNew)<1500 ||_IQabs(last_RotarRPMNew)<1500)){
  556. //低转速附近有大的阶跃
  557. if(_IQabs(RotarRPMNew)>_IQabs(last_RotarRPMNew)){
  558. test3 = RotarRPMNew;
  559. test5 = last_RotarRPMNew;
  560. RotarRPMNew = last_RotarRPMNew;
  561. }
  562. }
  563. /* if(RotarRPMNew - last_RotarRPMNew>500){
  564. RotarRPMNew = last_RotarRPMNew+4;
  565. }else if(RotarRPMNew - last_RotarRPMNew<-500){
  566. RotarRPMNew = last_RotarRPMNew-4;
  567. }*/
  568. //last_RotarRPMNew = RotarRPMNew;
  569. RotarrpmOut = lowpassfilter(last_speed,RotarRPMNew,_IQ12(0.584),_IQ12(0.125));
  570. last_speed = RotarrpmOut;
  571. speed1.Speed = _IQdiv(RotarrpmOut,speed1.BaseRpm);
  572. speed1.SpeedRpm = RotarrpmOut;
  573. last_RotarRPMNew = speed1.SpeedRpm;
  574. // }
  575. }
  576. void speed_cal(void){
  577. int32 RotarRPMNew = 0;
  578. int32 RotarrpmOut = 0;
  579. static int32 RotarrpmSum = 0;
  580. static int32 RotarrpmArr[8] = {0,0,0,0,0,0,0,0};
  581. static int16 k = 0;
  582. if(CpuTimer1.RegsAddr->TCR.bit.TIF == 1){
  583. // 速度为0
  584. XintTime = TIMER1_PER;
  585. Xint2Cnt = 1;
  586. CpuTimer1.RegsAddr->TCR.bit.TIF = 1;//写1清零
  587. int count = 0;
  588. for(count = 0;count<8;count++){
  589. RotarrpmArr[count] = 0;
  590. }
  591. RotarrpmSum = 0;
  592. RotarRPMNew =0;
  593. }else{
  594. RotarRPMNew =(int32)360000000/XintTime;
  595. }
  596. if(Xint2Cnt == 1){
  597. Xint2Cnt = 0;
  598. RotarrpmArr[k] = RotarRPMNew;
  599. RotarrpmSum += RotarrpmArr[k];
  600. RotarrpmOut = RotarrpmSum>>3;
  601. k++;
  602. if (k >= 8)
  603. {
  604. k = 0;
  605. }
  606. RotarrpmSum -= RotarrpmArr[k];
  607. speed1.Speed = _IQdiv(RotarrpmOut,speed1.BaseRpm);
  608. speed1.SpeedRpm = RotarrpmOut;
  609. }
  610. }
  611. void pwmlimit_speed(void){
  612. if(Enable_ALLOW == FALSE){
  613. if(_IQabs(speed1.SpeedRpm)< 200){
  614. startuptimer = 0;
  615. pwmlimit = 1300;
  616. }
  617. }
  618. if(_IQabs(speed1.SpeedRpm)< 200 && startuptimer>=60000){
  619. Uint16 index = _IQabs(speed1.SpeedRpm)/50;
  620. if(speed1.SpeedRpm>0){
  621. pwmlimit = (Uint32)(_IQabs(speed1.SpeedRpm)-index*50)*(pwm_limit_table1[index+1] - pwm_limit_table1[index])/50+pwm_limit_table1[index];
  622. }else{
  623. pwmlimit = (Uint32)(_IQabs(speed1.SpeedRpm)-index*50)*(pwm_limit_table1_NEG[index+1] - pwm_limit_table1_NEG[index])/50+pwm_limit_table1_NEG[index];
  624. }
  625. }else if(_IQabs(speed1.SpeedRpm)< 3500 && startuptimer>=60000){
  626. Uint16 index = _IQabs(speed1.SpeedRpm)/100;
  627. if(speed1.SpeedRpm>0){
  628. pwmlimit = (Uint32)(_IQabs(speed1.SpeedRpm)-index*100)*(pwm_limit_table2[index+1] - pwm_limit_table2[index])/100+pwm_limit_table2[index];
  629. }else{
  630. pwmlimit = (Uint32)(_IQabs(speed1.SpeedRpm)-index*100)*(pwm_limit_table_neg[index+1] - pwm_limit_table_neg[index])/100+pwm_limit_table_neg[index];
  631. }
  632. }else if(startuptimer>=60000){
  633. if(speed1.SpeedRpm>0){
  634. pwmlimit = pwm_limit_table2[36];//最大值
  635. }else{
  636. pwmlimit = pwm_limit_table_neg[36];//最大值
  637. }
  638. }else{
  639. }
  640. }
  641. void taskfree(void){
  642. if(SerialCommsTimer>(MAINLOOPFREQ/4)){
  643. GpioDataRegs.GPATOGGLE.bit.GPIO22 = 1;
  644. SerialCommsTimer = 0;
  645. DCbus_voltage = AdcResult.ADCRESULT4/10;// U = Uadc * 0.099964
  646. Tvot = ((Uint32)AdcResult.ADCRESULT7 *kscaler - kb)>>20;
  647. int32 R = 10000;//分压电阻
  648. int32 Rtadc = AdcResult.ADCRESULT6;
  649. Rt= Rtadc*R/(4096-Rtadc);
  650. if(Rt>32116){
  651. Tmotor = 0;
  652. }
  653. else if(Rt>15652){
  654. Tmotor = (ktable[0]*Rt/1000 + btable[0])>>12;
  655. }else if(Rt>6523){
  656. Tmotor = (ktable[1]*Rt/1000 + btable[1])>>12;
  657. }else if(Rt>2968){
  658. Tmotor = (ktable[2]*Rt/1000 + btable[2])>>12;
  659. }else if(Rt>1228){
  660. Tmotor = (ktable[3]*Rt/1000 + btable[3])>>12;
  661. }else if(Rt > 657){
  662. Tmotor = (ktable[4]*Rt/1000 + btable[4])>>12;
  663. }else if(Rt > 324){
  664. Tmotor = (ktable[5]*Rt/1000 + btable[5])>>12;
  665. }else{
  666. Tmotor = 125;
  667. }
  668. }
  669. }
  670. int16 MastIsrTimePercent(void)
  671. {
  672. Uint32 T1 = 0;
  673. int16 MasterIsrT =0;
  674. static int32 T1_L = 60000000;
  675. T1 = CpuTimer2.RegsAddr->TIM.all;
  676. if(T1_L < T1){
  677. MasterIsrT = (Uint32)(T1_L +CpuTimer2.RegsAddr->PRD.all -T1)* 100 /9000;
  678. }else{
  679. MasterIsrT = (Uint32)(T1_L-T1)* 100 /9000;
  680. }
  681. T1_L = T1;
  682. // MasterIsrT = (Uint32)MasterIsrT* 100 /3000;
  683. return MasterIsrT;
  684. }
  685. void mainLoop(void){
  686. Uint32 T1 = 0;
  687. Uint32 T2 = 0;
  688. T1 = CpuTimer2.RegsAddr->TIM.all;
  689. CanMaster();
  690. SerialCommsTimer++;
  691. if(SerialCommsTimer>0x3FFF){
  692. SerialCommsTimer = 0x3FFF;
  693. }
  694. //speed_cal();
  695. speed_cal_filter();
  696. pwmlimit_speed();
  697. taskfree();
  698. FaultTreat();
  699. // =============================== LEVEL 4 ======================================
  700. // Level 4 verifies the closed current loop and current PI controller.
  701. // ==============================================================================
  702. //-------------平均电流和can-------------------------------
  703. static _iq last_DC_current=0;
  704. static Uint16 DC_Current_cnt = 0;
  705. //static int32 DC_Current_sum = 0;
  706. static int32 DC_Current_sum_filter = 0;
  707. int32 dt = _IQ12(0.1);
  708. int32 cutoff = _IQ12(1);
  709. DC_current_real = (Uint32)AdcResult.ADCRESULT5*33;//Q12 I真实 = adc * 3.3 / 4096 /0.1
  710. DC_current_filer = lowpassfilter(last_DC_current,DC_current_real,cutoff,dt);
  711. last_DC_current = DC_current_filer;
  712. DC_Current_sum_filter += DC_current_filer*pwm1.DutyFunc>>15;
  713. DC_Current_cnt++;//由于stall最多16384/20K秒 所以最多0.819715秒计算一次平均值
  714. if(hallCmtnTrig==1){
  715. hallCmtnTrig = 0;
  716. DC_current_filter_avr = DC_Current_sum_filter/DC_Current_cnt;
  717. DC_Current_sum_filter =0;
  718. DC_Current_cnt = 0;
  719. }
  720. #ifdef DEBUG_CAN
  721. carveData1();
  722. CanCurve();
  723. #endif
  724. UserCANprocess();
  725. #if (BUILDLEVEL==LEVEL5)
  726. // ------------------------------------------------------------------------------
  727. // ADC conversion and offset adjustment (observing back-emfs is optinal for this prj.)
  728. // ------------------------------------------------------------------------------
  729. BemfA = AdcResult.ADCRESULT1;
  730. BemfB = AdcResult.ADCRESULT2;
  731. BemfC = AdcResult.ADCRESULT3;
  732. #ifdef SPEEDCLOSED
  733. pid1_spd.Ref = SpeedRef;//rc1.SetpointValue;
  734. pid1_spd.Fbk = speed1.Speed;
  735. #endif
  736. if(EnableFlag == 0 || FaultFlag.all !=0){
  737. Enable_ALLOW = FALSE;
  738. }else{
  739. Enable_ALLOW = TRUE;
  740. }
  741. if(Enable_ALLOW == FALSE){
  742. resetPI(&pid1_spd);
  743. rmp2.DesiredInput = 0;
  744. RC2_MACRO(&rmp2);
  745. pwm1.DutyFuncIn = (int16)_IQtoIQ15(rmp2.Out); // controlled speed duty-cycle
  746. }else{
  747. int16 pwmtmp = 0;
  748. pid1_spd.Umax = _IQ15toIQ(pwmlimit);//_IQ((float)pwmlimit/32767);
  749. pid1_spd.Umin = -pid1_spd.Umax;//_IQ((float)(-pwmlimit)/32767);
  750. rmp2.Ramp2Max = pwmlimit;
  751. rmp2.Ramp2Min = -pwmlimit;
  752. #ifdef SPEEDCLOSED
  753. PI_MACRO(&pid1_spd);
  754. pwmtmp =(int16)_IQtoIQ15(pid1_spd.Out);
  755. rmp2.DesiredInput = pwmtmp;
  756. #else
  757. rmp2.DesiredInput = PwmSet;
  758. #endif
  759. RC2_MACRO(&rmp2);
  760. pwmtmp = rmp2.Out;
  761. pwm1.DutyFuncIn = ((int32)pwmtmp*LimitMotCtrTemp(600000))>>15;//1分钟降一次
  762. if(pwm1.DutyFuncIn>pwmlimit){
  763. pwm1.DutyFuncIn = pwmlimit; // controlled speed duty-cycle
  764. }else if(pwm1.DutyFuncIn<-pwmlimit){
  765. pwm1.DutyFuncIn = -pwmlimit; // controlled speed duty-cycle
  766. }
  767. //rmp2.Out = pwm1.DutyFuncIn;
  768. }
  769. #endif // (BUILDLEVEL==LEVEL5)
  770. //程序占用率
  771. T2 = CpuTimer2.RegsAddr->TIM.all;
  772. if(T1 < T2){
  773. IsrTime = (Uint32)(T1 +CpuTimer2.RegsAddr->PRD.all -T2)* 100 /7500;
  774. }else{
  775. IsrTime = (Uint32)(T1-T2)* 100 /7500;
  776. }
  777. }
  778. void bldcpwm_close(void){
  779. EPwm1Regs.AQCSFRC.bit.CSFA = 1; /* Forcing a continuous Low on output A of EPWM3 */
  780. EPwm1Regs.AQCSFRC.bit.CSFB = 1; /* Forcing a continuous Low on output B of EPWM3 */
  781. EPwm2Regs.AQCSFRC.bit.CSFA = 1; /* Forcing a continuous Low on output A of EPWM3 */
  782. EPwm2Regs.AQCSFRC.bit.CSFB = 1; /* Forcing a continuous Low on output B of EPWM3 */
  783. EPwm3Regs.AQCSFRC.bit.CSFA = 1; /* Forcing a continuous Low on output A of EPWM3 */
  784. EPwm3Regs.AQCSFRC.bit.CSFB = 1; /* Forcing a continuous Low on output B of EPWM3 */
  785. }