var.c 4.8 KB

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