var.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * var.c
  3. *
  4. * Created on: 2023年5月17日
  5. * Author: wangd
  6. */
  7. #include "var.h" // DSP2803x Examples Include File
  8. #ifndef TRUE
  9. #define FALSE 0
  10. #define TRUE 1
  11. #endif
  12. union FAULTFLAG_UNI FaultFlag;
  13. Uint16 DCbus_voltage=0;
  14. Uint32 IsrTime = 0;
  15. _iq test =0;
  16. int16 test2 =0;
  17. int16 test3 =0;
  18. int16 test4 =0;
  19. int16 Tmotor = 0;
  20. PI_CONTROLLER pid1_idc = PI_CONTROLLER_DEFAULTS;
  21. PI_CONTROLLER pid1_spd = PI_CONTROLLER_DEFAULTS;
  22. RMPCNTL rc1 = RMPCNTL_DEFAULTS;
  23. int32 pwmlimit = 32768;
  24. _iq SpeedRef=_IQ(0.0);//初始速度
  25. int16 Direction = 1;
  26. _iq BemfA=0;
  27. _iq BemfB=0;
  28. _iq BemfC=0;
  29. _iq Iphase=0;
  30. _iq DC_current_real = 0;
  31. _iq DC_current_filer = 0;
  32. int32 DC_current_avr = 0;
  33. int32 DC_current_filter_avr = 0;
  34. int16 Tvot = 0;
  35. _iq CurrentSet = _IQ(0.0);
  36. Uint16 Fault_clear = 0;
  37. int32 XintTime = 60000000;
  38. volatile Uint16 EnableFlag = FALSE;
  39. volatile Uint16 Enable_ALLOW = FALSE;
  40. void InitVar(void){
  41. FaultFlag.all = 0;
  42. // Initialize the PI module for dc-bus current
  43. pid1_idc.Kp = _IQ(0.05);
  44. pid1_idc.Ki = _IQ(0.0002);
  45. pid1_idc.Umax = _IQ((float)pwmlimit/32767);
  46. pid1_idc.Umin = _IQ((float)(-pwmlimit)/32767);
  47. // Initialize the PI module for speed
  48. pid1_spd.Kp = 16384;
  49. pid1_spd.Ki = 1638;
  50. pid1_spd.Umax = _IQ(1.0);
  51. pid1_spd.Umin = _IQ(0.0);
  52. // Initialize RMPCNTL module
  53. rc1.RampDelayMax = 5;
  54. rc1.RampLowLimit = _IQ(-1.0);
  55. rc1.RampHighLimit = _IQ(1.0);
  56. }
  57. void FaultTreat(void){
  58. if(hall1.HallGpio == 0 || hall1.HallGpio == 7){
  59. FaultFlag.bit.HallFlag =1;
  60. }
  61. OverVoltage();
  62. OverMotorRpm();
  63. OverMotCtrTemp();
  64. SoftOverCurFlag();
  65. Fault_clear_fuc();
  66. }
  67. //I真实 = adc * 3.3 / 4096 /0.1
  68. void SoftOverCurFlag(void){
  69. static int16 cnt = 0;
  70. if(DC_current_filer>((Uint32)SOFTCURlIMIT<<12)){
  71. cnt++;
  72. if(cnt>10000/MAINLOOPTIME){// us
  73. cnt = 10000/MAINLOOPTIME+1;
  74. FaultFlag.bit.SoftOverCurFlag = 1;
  75. t4 = DC_current_filer;
  76. }
  77. }else{
  78. cnt = 0;
  79. }
  80. }
  81. void OverVoltage(void)
  82. {
  83. //过压不可恢复 欠压可恢复
  84. static int16 j = 0;
  85. if (DCbus_voltage >= VOLTAGEUP)
  86. {
  87. FaultFlag.bit.OverVolFlag = 1;
  88. }
  89. else if (DCbus_voltage <= VOLTAGELOW)
  90. {
  91. j = MAINLOOPFREQ;
  92. FaultFlag.bit.LowVolFlag = 1;
  93. }
  94. else
  95. {
  96. if (FaultFlag.bit.LowVolFlag == 1)
  97. {
  98. j--;
  99. if (j <= 0)
  100. {
  101. j = 0;
  102. FaultFlag.bit.LowVolFlag = 0;
  103. }
  104. }
  105. else
  106. {
  107. j = 0;
  108. }
  109. }
  110. }
  111. void OverMotorRpm(void)
  112. {//飞车不可恢复
  113. if (speed1.BaseRpm >= SPEEDUP)
  114. {
  115. FaultFlag.bit.OverRpmFlag = 1;
  116. }
  117. }
  118. void OverMotCtrTemp(void)
  119. {//过温可恢复
  120. if (Tmotor >= MOTORTEMPOVER)
  121. {
  122. FaultFlag.bit.OverMotTempFlag = 1;
  123. }
  124. else if (Tmotor < MOTORTEMPLIMIT)
  125. {
  126. FaultFlag.bit.OverMotTempFlag = 0;
  127. }
  128. if (Tvot >= IGBTTEMPOVER)
  129. {
  130. FaultFlag.bit.IgbtTempFaultFlag = 1;
  131. }
  132. else if (Tvot < 50)
  133. {
  134. FaultFlag.bit.IgbtTempFaultFlag = 0;
  135. }
  136. }
  137. void Fault_clear_fuc(void){
  138. if(Fault_clear == 1) {
  139. static int16 fault_cnt = 0;
  140. GpioDataRegs.GPBCLEAR.bit.GPIO32 = 1;
  141. FaultFlag.bit.OverCurFlag = 0;
  142. fault_cnt++;
  143. if(fault_cnt>MAINLOOPFREQ){
  144. Fault_clear = 0;
  145. fault_cnt = 0;
  146. OpenPwm();
  147. if (speed1.BaseRpm < 700)
  148. {
  149. FaultFlag.bit.OverRpmFlag = 0;
  150. }
  151. if(GpioDataRegs.GPADAT.bit.GPIO16==1){
  152. FaultFlag.bit.Ipmfault = 0;
  153. }
  154. }
  155. if(FaultFlag.bit.OverVolFlag == 1 && DCbus_voltage<320){
  156. FaultFlag.bit.OverVolFlag = 0;
  157. }
  158. FaultFlag.bit.HallFlag =0;
  159. }else{
  160. GpioDataRegs.GPBSET.bit.GPIO32 = 1;
  161. }
  162. if(EnableFlag == 0 && pwm1.DutyFunc==0){
  163. FaultFlag.bit.SoftOverCurFlag = 0;//失能清软件过流
  164. }
  165. }
  166. int16 LimitMotCtrTemp(int32 period){
  167. static int16 result = 32767;
  168. static int32 cnt1=0;
  169. static int32 cnt2=0;
  170. if(Tmotor>MOTORTEMPLIMIT){
  171. cnt1++;
  172. cnt2 = 0;
  173. if(cnt1>period){
  174. result = (int32)result*31128>>15;
  175. FaultFlag.bit.LimitMotTempFlag = 1;
  176. cnt1 =0;
  177. }
  178. }else{
  179. cnt1 =0;
  180. if(result<31128){
  181. cnt2++;
  182. if(cnt2>period){
  183. cnt2 = 0;
  184. int32 tmp = (int32)result*34491>>15;
  185. if(tmp>32767){
  186. result = 32767;
  187. }else{
  188. result = tmp;
  189. }
  190. }
  191. }else{
  192. result = 32767;
  193. FaultFlag.bit.LimitMotTempFlag = 0;
  194. cnt2 = 0;
  195. }
  196. }
  197. //test = result;
  198. return result;
  199. }