var.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Tmotor = 0;
  18. PI_CONTROLLER pid1_idc = PI_CONTROLLER_DEFAULTS;
  19. PI_CONTROLLER pid1_spd = PI_CONTROLLER_DEFAULTS;
  20. RMPCNTL rc1 = RMPCNTL_DEFAULTS;
  21. int32 pwmlimit = 32768;
  22. _iq SpeedRef=_IQ(0.0);//初始速度
  23. int16 Direction = 1;
  24. _iq BemfA=0;
  25. _iq BemfB=0;
  26. _iq BemfC=0;
  27. _iq Iphase=0;
  28. _iq DC_current_real = 0;
  29. _iq DC_current_filer = 0;
  30. int32 DC_current_avr = 0;
  31. int32 DC_current_filter_avr = 0;
  32. int16 Tvot = 0;
  33. _iq CurrentSet = _IQ(0.0);
  34. Uint16 Fault_clear = 0;
  35. int32 XintTime = 60000000;
  36. volatile Uint16 EnableFlag = FALSE;
  37. volatile Uint16 Enable_ALLOW = FALSE;
  38. void InitVar(void){
  39. FaultFlag.all = 0;
  40. // Initialize the PI module for dc-bus current
  41. pid1_idc.Kp = _IQ(0.05);
  42. pid1_idc.Ki = _IQ(0.0002);
  43. pid1_idc.Umax = _IQ((float)pwmlimit/32767);
  44. pid1_idc.Umin = _IQ((float)(-pwmlimit)/32767);
  45. // Initialize the PI module for speed
  46. pid1_spd.Kp = _IQ(1.0);
  47. pid1_spd.Ki = 360;//_IQ(0.0002);
  48. pid1_spd.Umax = _IQ(1.0);
  49. pid1_spd.Umin = _IQ(0.0);
  50. // Initialize RMPCNTL module
  51. rc1.RampDelayMax = 5;
  52. rc1.RampLowLimit = _IQ(-1.0);
  53. rc1.RampHighLimit = _IQ(1.0);
  54. }
  55. void FaultTreat(void){
  56. OverVoltage();
  57. OverMotorRpm();
  58. OverMotCtrTemp();
  59. Fault_clear_fuc();
  60. }
  61. void OverVoltage(void)
  62. {
  63. //过压不可恢复 欠压可恢复
  64. static int16 j = 0;
  65. if (DCbus_voltage >= 360)
  66. {
  67. FaultFlag.bit.OverVolFlag = 1;
  68. }
  69. else if (DCbus_voltage <= 200) //80
  70. {
  71. j = 10000;
  72. FaultFlag.bit.LowVolFlag = 1;
  73. }
  74. else
  75. {
  76. if (FaultFlag.bit.LowVolFlag == 1)
  77. {
  78. j--;
  79. if (j <= 0)
  80. {
  81. j = 0;
  82. FaultFlag.bit.LowVolFlag = 0;
  83. }
  84. }
  85. else
  86. {
  87. j = 0;
  88. }
  89. }
  90. }
  91. void OverMotorRpm(void)
  92. {//飞车不可恢复
  93. if (speed1.BaseRpm >= 3800)
  94. {
  95. FaultFlag.bit.OverRpmFlag = 1;
  96. }
  97. }
  98. void OverMotCtrTemp(void)
  99. {//过温可恢复
  100. if (Tmotor >= 100)
  101. {
  102. FaultFlag.bit.OverMotTempFlag = 1;
  103. }
  104. else if (Tmotor < 50)
  105. {
  106. FaultFlag.bit.OverMotTempFlag = 0;
  107. }
  108. if (Tvot >= 70)
  109. {
  110. FaultFlag.bit.IgbtTempFaultFlag = 1;
  111. }
  112. else if (Tvot < 50)
  113. {
  114. FaultFlag.bit.IgbtTempFaultFlag = 0;
  115. }
  116. }
  117. void Fault_clear_fuc(void){
  118. if(Fault_clear == 1) {
  119. static int16 fault_cnt = 0;
  120. GpioDataRegs.GPBCLEAR.bit.GPIO32 = 1;
  121. FaultFlag.bit.OverCurFlag = 0;
  122. fault_cnt++;
  123. if(fault_cnt>20000){
  124. Fault_clear = 0;
  125. fault_cnt = 0;
  126. OpenPwm();
  127. if (speed1.BaseRpm < 700)
  128. {
  129. FaultFlag.bit.OverRpmFlag = 0;
  130. }
  131. if(GpioDataRegs.GPADAT.bit.GPIO16==1){
  132. FaultFlag.bit.Ipmfault = 0;
  133. }
  134. }
  135. if(FaultFlag.bit.OverVolFlag == 1 && DCbus_voltage<320){
  136. FaultFlag.bit.OverVolFlag = 0;
  137. }
  138. }else{
  139. GpioDataRegs.GPBSET.bit.GPIO32 = 1;
  140. }
  141. }
  142. int16 LimitMotCtrTemp(int32 period){
  143. static int16 result = 32767;
  144. static int32 cnt1=0;
  145. static int32 cnt2=0;
  146. if(Tmotor>30){
  147. cnt1++;
  148. cnt2 = 0;
  149. if(cnt1>period){
  150. result = (int32)result*31128>>15;
  151. cnt1 =0;
  152. }
  153. }else{
  154. cnt1 =0;
  155. if(result<31128){
  156. cnt2++;
  157. if(cnt2>period){
  158. cnt2 = 0;
  159. int32 tmp = (int32)result*34491>>15;
  160. if(tmp>32767){
  161. result = 32767;
  162. }else{
  163. result = tmp;
  164. }
  165. }
  166. }else{
  167. result = 32767;
  168. cnt2 = 0;
  169. }
  170. }
  171. test = result;
  172. return result;
  173. }