cmdgennew.c 8.3 KB

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