Control.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. @copyright None
  3. @file Control.c
  4. @author Comment Vivre
  5. @date 2025-11-01
  6. @brief None
  7. */
  8. #include <MyProject.h>
  9. /**
  10. @function Ref_Ramp
  11. @brief 控制量步进控制
  12. @date 2025-11-08
  13. */
  14. void Ref_Ramp(void)
  15. {
  16. if ( motorControl.ActualRef < ( motorControl.TargetRef - motorControl.RampInc))
  17. { motorControl.ActualRef += motorControl.RampInc; }
  18. else if (motorControl.ActualRef > ( motorControl.TargetRef + motorControl.RampDec))
  19. { motorControl.ActualRef -= motorControl.RampDec; }
  20. else
  21. { motorControl.ActualRef = motorControl.TargetRef; }
  22. }
  23. /**
  24. @function Loop_Control
  25. @brief 环路控制
  26. @date 2025-11-08
  27. */
  28. void Loop_Control(void)
  29. {
  30. if ((mcState == mcRun) || (mcState == mcStop))
  31. {
  32. switch (motorControl.LoopState)
  33. {
  34. case OPEN_MODE:
  35. {
  36. if (motorControl.ActualSpeed > MOTOR_LOOP_RPM)
  37. {
  38. motorControl.LoopTime ++;
  39. if (motorControl.LoopTime > 15)
  40. {
  41. motorControl.ISRef = FOC_IQREF;
  42. #if (LOOP_MODE == SPEED_CONTROL_MODE)
  43. motorControl.ActualRef = motorControl.ActualSpeed + _Q15(200.0 / MOTOR_SPEED_BASE);
  44. #endif
  45. motorControl.LoopTime = 10;
  46. motorControl.LoopState = 1;
  47. PI0_UKH = motorControl.ISRef;
  48. }
  49. }
  50. }
  51. break;
  52. case CLOSE_MODE:
  53. {
  54. motorControl.LoopTime++;
  55. if (motorControl.LoopTime > LOOP_CYCLE)
  56. {
  57. #if (OUT_LOOP_CONTROL)
  58. {
  59. Ref_Ramp();
  60. #if (LOOP_MODE == SPEED_CONTROL_MODE)
  61. {
  62. // 速度外环
  63. motorControl.ISRef = HW_Zero_Calc(motorControl.ActualRef - motorControl.ActualSpeed);
  64. FOC_IQREF = motorControl.ISRef;
  65. #if (FiledWeakenCompEnable)
  66. {
  67. FiledWeakenControl(FOC__UD, FOC__UQ, udc.WeakenUsRef, motorControl.ISRef);
  68. if (mcFieldWeaken.mcIdref < ID_Limit)
  69. {mcFieldWeaken.mcIdref = ID_Limit;}
  70. FOC_IQREF = mcFieldWeaken.mcIdref;
  71. FOC_IDREF = mcFieldWeaken.mcIqref;
  72. }
  73. #else
  74. {
  75. FOC_IQREF = motorControl.ISRef;
  76. FOC_IDREF = 0;
  77. }
  78. #endif
  79. }
  80. #endif
  81. }
  82. #else
  83. {
  84. // 纯电流环
  85. motorControl.ActualQOutValue = FOC_IQREF;
  86. if (motorControl.ActualQOutValue < (motorControl.QOutRef - QOUTINC))
  87. { motorControl.ActualQOutValue = motorControl.QOutRef + QOUTINC; }
  88. else if (motorControl.ActualQOutValue > (motorControl.QOutRef + QOUTINC))
  89. { motorControl.ActualQOutValue = motorControl.QOutRef - QOUTINC; }
  90. else
  91. { motorControl.ActualQOutValue = motorControl.QOutRef; }
  92. FOC_IQREF = motorControl.ActualQOutValue;
  93. }
  94. #endif
  95. motorControl.LoopTime = 0;
  96. }
  97. }
  98. break;
  99. }
  100. }
  101. }
  102. /**
  103. @function Get_Target_Ref
  104. @brief 速度给定
  105. @date 2025-11-01
  106. */
  107. void Get_Target_Ref(void)
  108. {
  109. if (powerControl.PowerSate == POWER_RUN)
  110. {
  111. #if (CONTROL_MODE == NONE_MODE)
  112. isCtrlPowOn = true;
  113. motorControl.TargetRef = TARGET_SPEED_SET;
  114. motorControl.QOutRef = I_Value(2.0);
  115. #elif (CONTROL_MODE == UART_MODE)
  116. // 接收到数据
  117. if(RecMessageFalg)
  118. {
  119. }
  120. #endif
  121. }
  122. }