cmdgennew.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /************************************************************************
  2. Project: Welling FAN Motor Developing platform
  3. Filename: cmdgen.c
  4. Partner Filename: cmdgen.h
  5. Description: Partner file of cmdgen.h
  6. Complier: IAR Embedded Workbench for ARM 7.80, IAR Systems.
  7. CPU TYPE : ST32F0xx
  8. *************************************************************************
  9. Copyright (c) 2019 Welling Motor Technology(Shanghai) Co. Ltd.
  10. All rights reserved.
  11. *************************************************************************
  12. *************************************************************************
  13. Revising History (ECL of this file):
  14. 20191225, by cyf, create this file;
  15. ************************************************************************/
  16. #define FALSE (0U)
  17. #define TRUE (1U)
  18. #define RESET (0U)
  19. #define SET (1U)
  20. #define abs(A) (((A) < 0) ? - (A) : (A))
  21. /************************************************************************
  22. Beginning of File, do not put anything above here except notes
  23. Compiler Directives:
  24. *************************************************************************/
  25. #ifndef _CMDGENNEW_C_
  26. #define _CMDGENNEW_C_
  27. #endif
  28. /************************************************************************
  29. Included File
  30. *************************************************************************/
  31. #include "cmdgennew.h"
  32. #include "glbcof.h"
  33. /************************************************************************
  34. Exported Functions:
  35. *************************************************************************/
  36. /***************************************************************
  37. Function: cmd_voCmdInit;
  38. Description: Command initialization
  39. Call by: functions in main loop;
  40. Input Variables: N/A
  41. Output/Return Variables: N/A
  42. Subroutine Call: N/A
  43. Reference: N/A
  44. ****************************************************************/
  45. void cmd_voCmdInit(void)
  46. {
  47. cmd_stCmdOut.swNewCmdDir = 1;
  48. cmd_stCmdOut.swNewSpdRefPu = 0;
  49. cmd_stCmdOut.swOldCmdDir = 1;
  50. cmd_stCmdOut.swOldSpdRefPu = 0;
  51. cmd_stCmdOut.blConsVolBrakeFlg = RESET;
  52. cmd_stCmdOut.swMotorDir = 1;
  53. cmd_stCmdOut.swIntRefPu = 0;
  54. cmd_stCmdOut.slIntRefPu = 0;
  55. }
  56. /***************************************************************
  57. Function: cmd_voCmdCoef;
  58. Description: Coefficient calculation for Command
  59. Call by: functions in Mainloop;
  60. Input Variables: CMD_CMDCOFIN
  61. Output/Return Variables: CMD_CMDCOF
  62. Subroutine Call:
  63. Reference: N/A
  64. ****************************************************************/
  65. void cmd_voCmdCoef(CMD_CMDCOFIN *in, CMD_CMDCOF *coef)
  66. {
  67. coef->ulAccelPu = in->ulAccelPu; // Q29
  68. coef->ulDecelPu = in->ulDecelPu; // Q29
  69. coef->swBrakeSpdDeltaPu = in->swBrakeSpdDeltaPu; // Q15
  70. }
  71. /***************************************************************
  72. Function: cmd_voCmdOut;
  73. Description: Calculation for Command
  74. Call by: functions in TBS;
  75. Input Variables: CMD_CMDIN
  76. Output/Return Variables: CMD_CMDOUT
  77. Subroutine Call: N/A
  78. Reference: N/A
  79. ****************************************************************/
  80. void cmd_voCmdOut(CMD_CMDIN *in, CMD_CMDCOF *coef, CMD_CMDOUT *out)
  81. {
  82. SWORD newCMD;
  83. if (in->swSpdCmdRpm > 0)
  84. {
  85. out->swNewCmdDir = 1;
  86. }
  87. else if (in->swSpdCmdRpm < 0)
  88. {
  89. out->swNewCmdDir = -1;
  90. }
  91. else
  92. {
  93. out->swNewCmdDir = out->swOldCmdDir;
  94. }
  95. if (in->swSpdCmdRpm > 6000)
  96. {
  97. newCMD = 6000;
  98. }
  99. else if (in->swSpdCmdRpm < -6000)
  100. {
  101. newCMD = -6000;
  102. }
  103. else
  104. {
  105. newCMD = in->swSpdCmdRpm;
  106. }
  107. out->swNewSpdRefPu = ((SLONG)newCMD << 15) / cof_uwVbRpm; // Q15
  108. if (out->swNewSpdRefPu != out->swOldSpdRefPu)
  109. {
  110. if (out->swOldSpdRefPu != 0)
  111. {
  112. /*----if new cmd direction is same with the old-----*/
  113. if (out->swNewCmdDir == out->swOldCmdDir)
  114. {
  115. /*if new command speed lower than the old*/
  116. if ((abs(out->swOldSpdRefPu) - abs(out->swNewSpdRefPu)) > coef->swBrakeSpdDeltaPu)
  117. {
  118. out->blConsVolBrakeFlg = SET;
  119. /*actual speed lower than the new, disable CVB*/
  120. if ((in->swSpdNowPu * out->swNewCmdDir) < abs(out->swNewSpdRefPu))
  121. {
  122. out->blConsVolBrakeFlg = RESET;
  123. }
  124. }
  125. /*if new command speed higer than the old*/
  126. else
  127. {
  128. /*if CVB enabled,disable CVB and initial some parameters*/
  129. if (out->blConsVolBrakeFlg == SET)
  130. {
  131. out->blConsVolBrakeFlg = RESET;
  132. }
  133. }
  134. }
  135. else
  136. {}
  137. }
  138. }
  139. /*if the new command speed the same with the old*/
  140. else
  141. {
  142. /*if CVB enabled, disable CVB and initial some parameters*/
  143. if (out->blConsVolBrakeFlg == SET)
  144. {
  145. out->blConsVolBrakeFlg = RESET;
  146. }
  147. }
  148. /*-if CVB disabled, load the new command to the old-----*/
  149. if (out->blConsVolBrakeFlg == RESET)
  150. {
  151. out->swOldSpdRefPu = out->swNewSpdRefPu;
  152. out->swOldCmdDir = out->swNewCmdDir;
  153. }
  154. SWORD swIntRefPu; // Q15
  155. SLONG slIntRefPu; // Q29
  156. SWORD swUserSpdRefPu; // Q15
  157. SLONG slUserSpdRefPu; // Q29
  158. ULONG ulUserSpdRefAbsPu;
  159. ULONG ulIntRefAbsPu;
  160. ULONG ulAccDeltaPu, ulDecDeltaPu;
  161. /*--------------------------------------------------------------------------
  162. | COF->slAccDeltaPu | OUTPUT->swIntRefPu
  163. INPUT->swUSART_RefCmdPu | COF->slDecDeltaPu | OUTPUT->swUSART_RefCmdZ1Pu
  164. | | OUTPUT->swMotorDir
  165. --------------------------------------------------------------------------*/
  166. swIntRefPu = out->swIntRefPu; // Intermediant Speed
  167. slIntRefPu = out->slIntRefPu;
  168. swUserSpdRefPu = out->swNewSpdRefPu; // Target Speed
  169. slUserSpdRefPu = (SLONG)swUserSpdRefPu << 14; // Q29=Q15+Q14
  170. ulUserSpdRefAbsPu = abs(slUserSpdRefPu);
  171. ulIntRefAbsPu = abs(slIntRefPu);
  172. /* Command Referance Speed Run Direction*/
  173. if (swIntRefPu < 0)
  174. {
  175. out->swMotorDir = -1;
  176. }
  177. else if (swIntRefPu > 0)
  178. {
  179. out->swMotorDir = 1;
  180. }
  181. else
  182. {
  183. out->swMotorDir = out->swNewCmdDir;
  184. }
  185. ulAccDeltaPu = coef->ulAccelPu; // Q29
  186. ulDecDeltaPu = coef->ulDecelPu; // Q29
  187. /*-------------------------------------------------------------------------
  188. Usart Command Changing
  189. --------------------------------------------------------------------------*/
  190. if (out->swMotorDir != out->swNewCmdDir)
  191. {
  192. slIntRefPu += (ulDecDeltaPu * out->swNewCmdDir);
  193. }
  194. else
  195. {
  196. if (ulIntRefAbsPu < ulUserSpdRefAbsPu)
  197. {
  198. slIntRefPu += (ulAccDeltaPu * out->swNewCmdDir); // Q29
  199. if (abs(slIntRefPu) >= ulUserSpdRefAbsPu)
  200. {
  201. slIntRefPu = slUserSpdRefPu;
  202. }
  203. }
  204. else if (ulIntRefAbsPu > ulUserSpdRefAbsPu)
  205. {
  206. slIntRefPu -= (ulDecDeltaPu * out->swNewCmdDir); // Q29
  207. if (ulUserSpdRefAbsPu > 0)
  208. {
  209. if (abs(slIntRefPu) <= ulUserSpdRefAbsPu)
  210. {
  211. slIntRefPu = slUserSpdRefPu;
  212. }
  213. }
  214. else
  215. {
  216. if ((slIntRefPu * out->swNewCmdDir) <= 0)
  217. {
  218. slIntRefPu = 0;
  219. }
  220. }
  221. }
  222. else
  223. {
  224. slIntRefPu = slUserSpdRefPu;
  225. }
  226. }
  227. out->slIntRefPu = slIntRefPu; // Q29
  228. out->swIntRefPu = (out->slIntRefPu) >> 14; // Q29-Q14=Q15
  229. }
  230. /************************************************************************
  231. Local Functions:
  232. *************************************************************************/
  233. #ifdef _CMDGENNEW_C_
  234. #undef _CMDGENNEW_C_
  235. #endif
  236. /************************************************************************
  237. Copyright (c) 2019 Welling Motor Technology(Shanghai) Co. Ltd.
  238. All rights reserved.
  239. *************************************************************************
  240. End of this File (EOF)!
  241. Do not put anything after this part!
  242. *************************************************************************/