PMSM_Controller.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  1. /*
  2. * File: PMSM_Controller.c
  3. *
  4. * Code generated for Simulink model 'PMSM_Controller'.
  5. *
  6. * Model version : 1.1235
  7. * Simulink Coder version : 9.4 (R2020b) 29-Jul-2020
  8. * C/C++ source code generated on : Wed Apr 6 15:47:34 2022
  9. *
  10. * Target selection: ert.tlc
  11. * Embedded hardware selection: ARM Compatible->ARM Cortex-M
  12. * Code generation objectives:
  13. * 1. Execution efficiency
  14. * 2. RAM efficiency
  15. * Validation result: Not run
  16. */
  17. #include "PMSM_Controller.h"
  18. /* Named constants for Chart: '<S3>/Control_Mode_Manager' */
  19. #define IN_ACTIVE ((uint8_T)1U)
  20. #define IN_NO_ACTIVE_CHILD ((uint8_T)0U)
  21. #define IN_OPEN ((uint8_T)2U)
  22. #define IN_SPEED_MODE ((uint8_T)1U)
  23. #define IN_TORQUE_MODE ((uint8_T)2U)
  24. #define OPEN_MODE ((uint8_T)0U)
  25. #define SPD_MODE ((uint8_T)1U)
  26. #define TRQ_MODE ((uint8_T)2U)
  27. #ifndef UCHAR_MAX
  28. #include <limits.h>
  29. #endif
  30. #if ( UCHAR_MAX != (0xFFU) ) || ( SCHAR_MAX != (0x7F) )
  31. #error Code was generated for compiler with different sized uchar/char. \
  32. Consider adjusting Test hardware word size settings on the \
  33. Hardware Implementation pane to match your compiler word sizes as \
  34. defined in limits.h of the compiler. Alternatively, you can \
  35. select the Test hardware is the same as production hardware option and \
  36. select the Enable portable word sizes option on the Code Generation > \
  37. Verification pane for ERT based targets, which will disable the \
  38. preprocessor word size checks.
  39. #endif
  40. #if ( USHRT_MAX != (0xFFFFU) ) || ( SHRT_MAX != (0x7FFF) )
  41. #error Code was generated for compiler with different sized ushort/short. \
  42. Consider adjusting Test hardware word size settings on the \
  43. Hardware Implementation pane to match your compiler word sizes as \
  44. defined in limits.h of the compiler. Alternatively, you can \
  45. select the Test hardware is the same as production hardware option and \
  46. select the Enable portable word sizes option on the Code Generation > \
  47. Verification pane for ERT based targets, which will disable the \
  48. preprocessor word size checks.
  49. #endif
  50. #if ( UINT_MAX != (0xFFFFFFFFU) ) || ( INT_MAX != (0x7FFFFFFF) )
  51. #error Code was generated for compiler with different sized uint/int. \
  52. Consider adjusting Test hardware word size settings on the \
  53. Hardware Implementation pane to match your compiler word sizes as \
  54. defined in limits.h of the compiler. Alternatively, you can \
  55. select the Test hardware is the same as production hardware option and \
  56. select the Enable portable word sizes option on the Code Generation > \
  57. Verification pane for ERT based targets, which will disable the \
  58. preprocessor word size checks.
  59. #endif
  60. #if ( ULONG_MAX != (0xFFFFFFFFU) ) || ( LONG_MAX != (0x7FFFFFFF) )
  61. #error Code was generated for compiler with different sized ulong/long. \
  62. Consider adjusting Test hardware word size settings on the \
  63. Hardware Implementation pane to match your compiler word sizes as \
  64. defined in limits.h of the compiler. Alternatively, you can \
  65. select the Test hardware is the same as production hardware option and \
  66. select the Enable portable word sizes option on the Code Generation > \
  67. Verification pane for ERT based targets, which will disable the \
  68. preprocessor word size checks.
  69. #endif
  70. /* Skipping ulong_long/long_long check: insufficient preprocessor integer range. */
  71. static uint16_T plook_u16s16_evencka(int16_T u, int16_T bp0, uint16_T bpSpace,
  72. uint32_T maxIndex);
  73. static uint8_T plook_u8u16_evencka(uint16_T u, uint16_T bp0, uint16_T bpSpace,
  74. uint32_T maxIndex);
  75. static void Counter_Init(DW_Counter *localDW, uint16_T rtp_z_cntInit);
  76. static uint16_T Counter(uint16_T rtu_inc, uint16_T rtu_max, boolean_T rtu_rst,
  77. DW_Counter *localDW);
  78. static boolean_T either_edge(boolean_T rtu_u, DW_either_edge *localDW);
  79. static void Debounce_Filter_Init(DW_Debounce_Filter *localDW);
  80. static void Debounce_Filter(boolean_T rtu_u, uint16_T rtu_tAcv, uint16_T
  81. rtu_tDeacv, boolean_T *rty_y, DW_Debounce_Filter *localDW);
  82. static void Low_Pass_Filter(const int16_T rtu_u[2], uint16_T rtu_coef, int16_T
  83. rty_y[2], DW_Low_Pass_Filter *localDW);
  84. static void PI_iq(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb,
  85. int16_T rtu_satMax, int16_T rtu_satMin, int16_T
  86. rtu_ext_limProt, int16_T *rty_pi_out, DW_PI_iq *localDW);
  87. static void PI_id(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb,
  88. int16_T rtu_satMax, int16_T rtu_satMin, int16_T
  89. rtu_ext_limProt, int16_T *rty_pi_out, DW_PI_id *localDW);
  90. static void pi_speed_Init(DW_pi_speed *localDW);
  91. static int16_T pi_speed(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T
  92. rtu_Kb, int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_ext_limProt,
  93. uint8_T rtu_reset, const ConstB_pi_speed *localC, DW_pi_speed *localDW,
  94. ZCE_pi_speed *localZCE);
  95. static uint16_T plook_u16s16_evencka(int16_T u, int16_T bp0, uint16_T bpSpace,
  96. uint32_T maxIndex)
  97. {
  98. uint16_T bpIndex;
  99. /* Prelookup - Index only
  100. Index Search method: 'even'
  101. Extrapolation method: 'Clip'
  102. Use previous index: 'off'
  103. Use last breakpoint for index at or above upper limit: 'on'
  104. Remove protection against out-of-range input in generated code: 'off'
  105. */
  106. if (u <= bp0) {
  107. bpIndex = 0U;
  108. } else {
  109. bpIndex = (uint16_T)((uint32_T)(uint16_T)(u - bp0) / bpSpace);
  110. if (bpIndex < maxIndex) {
  111. } else {
  112. bpIndex = (uint16_T)maxIndex;
  113. }
  114. }
  115. return bpIndex;
  116. }
  117. static uint8_T plook_u8u16_evencka(uint16_T u, uint16_T bp0, uint16_T bpSpace,
  118. uint32_T maxIndex)
  119. {
  120. uint16_T fbpIndex;
  121. uint8_T bpIndex;
  122. /* Prelookup - Index only
  123. Index Search method: 'even'
  124. Extrapolation method: 'Clip'
  125. Use previous index: 'off'
  126. Use last breakpoint for index at or above upper limit: 'on'
  127. Remove protection against out-of-range input in generated code: 'off'
  128. */
  129. if (u <= bp0) {
  130. bpIndex = 0U;
  131. } else {
  132. fbpIndex = (uint16_T)((uint32_T)(uint16_T)((uint32_T)u - bp0) / bpSpace);
  133. if (fbpIndex < maxIndex) {
  134. bpIndex = (uint8_T)fbpIndex;
  135. } else {
  136. bpIndex = (uint8_T)maxIndex;
  137. }
  138. }
  139. return bpIndex;
  140. }
  141. /*
  142. * System initialize for atomic system:
  143. * '<S39>/Counter'
  144. * '<S38>/Counter'
  145. */
  146. static void Counter_Init(DW_Counter *localDW, uint16_T rtp_z_cntInit)
  147. {
  148. /* InitializeConditions for UnitDelay: '<S44>/UnitDelay' */
  149. localDW->UnitDelay_DSTATE = rtp_z_cntInit;
  150. }
  151. /*
  152. * Output and update for atomic system:
  153. * '<S39>/Counter'
  154. * '<S38>/Counter'
  155. */
  156. static uint16_T Counter(uint16_T rtu_inc, uint16_T rtu_max, boolean_T rtu_rst,
  157. DW_Counter *localDW)
  158. {
  159. uint16_T rty_cnt_0;
  160. uint16_T rtu_rst_0;
  161. /* Switch: '<S44>/Switch1' incorporates:
  162. * Constant: '<S44>/Constant23'
  163. * UnitDelay: '<S44>/UnitDelay'
  164. */
  165. if (rtu_rst) {
  166. rtu_rst_0 = 0U;
  167. } else {
  168. rtu_rst_0 = localDW->UnitDelay_DSTATE;
  169. }
  170. /* End of Switch: '<S44>/Switch1' */
  171. /* Sum: '<S43>/Sum1' */
  172. rty_cnt_0 = (uint16_T)((uint32_T)rtu_inc + rtu_rst_0);
  173. /* MinMax: '<S43>/MinMax' */
  174. if (rty_cnt_0 < rtu_max) {
  175. /* Update for UnitDelay: '<S44>/UnitDelay' */
  176. localDW->UnitDelay_DSTATE = rty_cnt_0;
  177. } else {
  178. /* Update for UnitDelay: '<S44>/UnitDelay' */
  179. localDW->UnitDelay_DSTATE = rtu_max;
  180. }
  181. /* End of MinMax: '<S43>/MinMax' */
  182. return rty_cnt_0;
  183. }
  184. /*
  185. * Output and update for atomic system:
  186. * '<S35>/either_edge'
  187. * '<S34>/either_edge'
  188. */
  189. static boolean_T either_edge(boolean_T rtu_u, DW_either_edge *localDW)
  190. {
  191. boolean_T rty_y_0;
  192. /* RelationalOperator: '<S40>/Relational Operator' incorporates:
  193. * UnitDelay: '<S40>/UnitDelay'
  194. */
  195. rty_y_0 = (rtu_u != localDW->UnitDelay_DSTATE);
  196. /* Update for UnitDelay: '<S40>/UnitDelay' */
  197. localDW->UnitDelay_DSTATE = rtu_u;
  198. return rty_y_0;
  199. }
  200. /* System initialize for atomic system: '<S34>/Debounce_Filter' */
  201. static void Debounce_Filter_Init(DW_Debounce_Filter *localDW)
  202. {
  203. /* SystemInitialize for IfAction SubSystem: '<S35>/Qualification' */
  204. /* SystemInitialize for Atomic SubSystem: '<S39>/Counter' */
  205. Counter_Init(&localDW->Counter_f, 0);
  206. /* End of SystemInitialize for SubSystem: '<S39>/Counter' */
  207. /* End of SystemInitialize for SubSystem: '<S35>/Qualification' */
  208. /* SystemInitialize for IfAction SubSystem: '<S35>/Dequalification' */
  209. /* SystemInitialize for Atomic SubSystem: '<S38>/Counter' */
  210. Counter_Init(&localDW->Counter_d, 0);
  211. /* End of SystemInitialize for SubSystem: '<S38>/Counter' */
  212. /* End of SystemInitialize for SubSystem: '<S35>/Dequalification' */
  213. }
  214. /* Output and update for atomic system: '<S34>/Debounce_Filter' */
  215. static void Debounce_Filter(boolean_T rtu_u, uint16_T rtu_tAcv, uint16_T
  216. rtu_tDeacv, boolean_T *rty_y, DW_Debounce_Filter *localDW)
  217. {
  218. uint16_T rtb_Sum1_n;
  219. boolean_T rtb_RelationalOperator_e;
  220. /* Outputs for Atomic SubSystem: '<S35>/either_edge' */
  221. rtb_RelationalOperator_e = either_edge(rtu_u, &localDW->either_edge_j);
  222. /* End of Outputs for SubSystem: '<S35>/either_edge' */
  223. /* If: '<S35>/If2' incorporates:
  224. * Constant: '<S38>/Constant6'
  225. * Constant: '<S39>/Constant6'
  226. * Inport: '<S37>/yPrev'
  227. * Logic: '<S35>/Logical Operator1'
  228. * Logic: '<S35>/Logical Operator2'
  229. * Logic: '<S35>/Logical Operator3'
  230. * Logic: '<S35>/Logical Operator4'
  231. * UnitDelay: '<S35>/UnitDelay'
  232. */
  233. if (rtu_u && (!localDW->UnitDelay_DSTATE)) {
  234. /* Outputs for IfAction SubSystem: '<S35>/Qualification' incorporates:
  235. * ActionPort: '<S39>/Action Port'
  236. */
  237. /* Outputs for Atomic SubSystem: '<S39>/Counter' */
  238. rtb_Sum1_n = Counter(1, rtu_tAcv, rtb_RelationalOperator_e,
  239. &localDW->Counter_f);
  240. /* End of Outputs for SubSystem: '<S39>/Counter' */
  241. /* Switch: '<S39>/Switch2' incorporates:
  242. * Constant: '<S39>/Constant6'
  243. * RelationalOperator: '<S39>/Relational Operator2'
  244. */
  245. *rty_y = ((rtb_Sum1_n > rtu_tAcv) || localDW->UnitDelay_DSTATE);
  246. /* End of Outputs for SubSystem: '<S35>/Qualification' */
  247. } else if ((!rtu_u) && localDW->UnitDelay_DSTATE) {
  248. /* Outputs for IfAction SubSystem: '<S35>/Dequalification' incorporates:
  249. * ActionPort: '<S38>/Action Port'
  250. */
  251. /* Outputs for Atomic SubSystem: '<S38>/Counter' */
  252. rtb_Sum1_n = Counter(1, rtu_tDeacv, rtb_RelationalOperator_e,
  253. &localDW->Counter_d);
  254. /* End of Outputs for SubSystem: '<S38>/Counter' */
  255. /* Switch: '<S38>/Switch2' incorporates:
  256. * Constant: '<S38>/Constant6'
  257. * RelationalOperator: '<S38>/Relational Operator2'
  258. */
  259. *rty_y = ((rtb_Sum1_n <= rtu_tDeacv) && localDW->UnitDelay_DSTATE);
  260. /* End of Outputs for SubSystem: '<S35>/Dequalification' */
  261. } else {
  262. /* Outputs for IfAction SubSystem: '<S35>/Default' incorporates:
  263. * ActionPort: '<S37>/Action Port'
  264. */
  265. *rty_y = localDW->UnitDelay_DSTATE;
  266. /* End of Outputs for SubSystem: '<S35>/Default' */
  267. }
  268. /* End of If: '<S35>/If2' */
  269. /* Update for UnitDelay: '<S35>/UnitDelay' */
  270. localDW->UnitDelay_DSTATE = *rty_y;
  271. }
  272. /* Output and update for atomic system: '<S45>/Low_Pass_Filter' */
  273. static void Low_Pass_Filter(const int16_T rtu_u[2], uint16_T rtu_coef, int16_T
  274. rty_y[2], DW_Low_Pass_Filter *localDW)
  275. {
  276. int32_T tmp;
  277. /* Sum: '<S54>/Sum2' incorporates:
  278. * UnitDelay: '<S54>/UnitDelay1'
  279. */
  280. tmp = rtu_u[0] - localDW->UnitDelay1_DSTATE[0];
  281. if (tmp > 32767) {
  282. tmp = 32767;
  283. } else {
  284. if (tmp < -32768) {
  285. tmp = -32768;
  286. }
  287. }
  288. /* Product: '<S54>/Divide3' incorporates:
  289. * Sum: '<S54>/Sum2'
  290. */
  291. rty_y[0] = (int16_T)((rtu_coef * tmp) >> 16);
  292. /* Sum: '<S54>/Sum3' incorporates:
  293. * UnitDelay: '<S54>/UnitDelay1'
  294. */
  295. rty_y[0] += localDW->UnitDelay1_DSTATE[0];
  296. /* Update for UnitDelay: '<S54>/UnitDelay1' incorporates:
  297. * Sum: '<S54>/Sum3'
  298. */
  299. localDW->UnitDelay1_DSTATE[0] = rty_y[0];
  300. /* Sum: '<S54>/Sum2' incorporates:
  301. * UnitDelay: '<S54>/UnitDelay1'
  302. */
  303. tmp = rtu_u[1] - localDW->UnitDelay1_DSTATE[1];
  304. if (tmp > 32767) {
  305. tmp = 32767;
  306. } else {
  307. if (tmp < -32768) {
  308. tmp = -32768;
  309. }
  310. }
  311. /* Product: '<S54>/Divide3' incorporates:
  312. * Sum: '<S54>/Sum2'
  313. */
  314. rty_y[1] = (int16_T)((rtu_coef * tmp) >> 16);
  315. /* Sum: '<S54>/Sum3' incorporates:
  316. * UnitDelay: '<S54>/UnitDelay1'
  317. */
  318. rty_y[1] += localDW->UnitDelay1_DSTATE[1];
  319. /* Update for UnitDelay: '<S54>/UnitDelay1' incorporates:
  320. * Sum: '<S54>/Sum3'
  321. */
  322. localDW->UnitDelay1_DSTATE[1] = rty_y[1];
  323. }
  324. /* Output and update for atomic system: '<S58>/PI_iq' */
  325. static void PI_iq(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb,
  326. int16_T rtu_satMax, int16_T rtu_satMin, int16_T
  327. rtu_ext_limProt, int16_T *rty_pi_out, DW_PI_iq *localDW)
  328. {
  329. int64_T tmp;
  330. int32_T rtb_Divide4_k;
  331. int32_T rtb_Sum1_hy;
  332. /* Product: '<S63>/Divide4' */
  333. rtb_Divide4_k = (rtu_err * rtu_P) >> 6;
  334. /* Product: '<S63>/Divide1' incorporates:
  335. * Product: '<S63>/Divide4'
  336. */
  337. tmp = ((int64_T)rtb_Divide4_k * rtu_I) >> 10;
  338. if (tmp > 2147483647LL) {
  339. tmp = 2147483647LL;
  340. } else {
  341. if (tmp < -2147483648LL) {
  342. tmp = -2147483648LL;
  343. }
  344. }
  345. /* Sum: '<S63>/Sum2' incorporates:
  346. * Product: '<S63>/Divide1'
  347. * UnitDelay: '<S63>/UnitDelay'
  348. */
  349. tmp = (((int64_T)rtu_ext_limProt << 4) + (int32_T)tmp) +
  350. localDW->UnitDelay_DSTATE;
  351. if (tmp > 2147483647LL) {
  352. tmp = 2147483647LL;
  353. } else {
  354. if (tmp < -2147483648LL) {
  355. tmp = -2147483648LL;
  356. }
  357. }
  358. /* Sum: '<S65>/Sum1' incorporates:
  359. * Sum: '<S63>/Sum2'
  360. * UnitDelay: '<S65>/UnitDelay'
  361. */
  362. rtb_Sum1_hy = (int32_T)tmp + localDW->UnitDelay_DSTATE_i;
  363. /* Sum: '<S63>/Sum6' incorporates:
  364. * Product: '<S63>/Divide4'
  365. * Sum: '<S65>/Sum1'
  366. */
  367. tmp = (int64_T)rtb_Divide4_k + rtb_Sum1_hy;
  368. if (tmp > 2147483647LL) {
  369. tmp = 2147483647LL;
  370. } else {
  371. if (tmp < -2147483648LL) {
  372. tmp = -2147483648LL;
  373. }
  374. }
  375. /* Switch: '<S66>/Switch2' incorporates:
  376. * RelationalOperator: '<S66>/LowerRelop1'
  377. * RelationalOperator: '<S66>/UpperRelop'
  378. * Sum: '<S63>/Sum6'
  379. * Switch: '<S66>/Switch'
  380. */
  381. if ((int32_T)tmp > (rtu_satMax << 4)) {
  382. *rty_pi_out = rtu_satMax;
  383. } else if ((int32_T)tmp < (rtu_satMin << 4)) {
  384. /* Switch: '<S66>/Switch' */
  385. *rty_pi_out = rtu_satMin;
  386. } else {
  387. *rty_pi_out = (int16_T)((int32_T)tmp >> 4);
  388. }
  389. /* End of Switch: '<S66>/Switch2' */
  390. /* Update for UnitDelay: '<S63>/UnitDelay' incorporates:
  391. * Product: '<S63>/Divide2'
  392. * Sum: '<S63>/Sum3'
  393. * Sum: '<S63>/Sum6'
  394. */
  395. localDW->UnitDelay_DSTATE = (int32_T)(((int64_T)((*rty_pi_out << 4) - (int32_T)
  396. tmp) * rtu_Kb) >> 10);
  397. /* Update for UnitDelay: '<S65>/UnitDelay' incorporates:
  398. * Sum: '<S65>/Sum1'
  399. */
  400. localDW->UnitDelay_DSTATE_i = rtb_Sum1_hy;
  401. }
  402. /* Output and update for atomic system: '<S57>/PI_id' */
  403. static void PI_id(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb,
  404. int16_T rtu_satMax, int16_T rtu_satMin, int16_T
  405. rtu_ext_limProt, int16_T *rty_pi_out, DW_PI_id *localDW)
  406. {
  407. int64_T tmp;
  408. int32_T rtb_Divide4_e;
  409. int32_T rtb_Sum1_l;
  410. /* Product: '<S59>/Divide4' */
  411. rtb_Divide4_e = (rtu_err * rtu_P) >> 6;
  412. /* Product: '<S59>/Divide1' incorporates:
  413. * Product: '<S59>/Divide4'
  414. */
  415. tmp = ((int64_T)rtb_Divide4_e * rtu_I) >> 10;
  416. if (tmp > 2147483647LL) {
  417. tmp = 2147483647LL;
  418. } else {
  419. if (tmp < -2147483648LL) {
  420. tmp = -2147483648LL;
  421. }
  422. }
  423. /* Sum: '<S59>/Sum2' incorporates:
  424. * Product: '<S59>/Divide1'
  425. * UnitDelay: '<S59>/UnitDelay'
  426. */
  427. tmp = (((int64_T)rtu_ext_limProt << 3) + (int32_T)tmp) +
  428. localDW->UnitDelay_DSTATE;
  429. if (tmp > 2147483647LL) {
  430. tmp = 2147483647LL;
  431. } else {
  432. if (tmp < -2147483648LL) {
  433. tmp = -2147483648LL;
  434. }
  435. }
  436. /* Sum: '<S61>/Sum1' incorporates:
  437. * Sum: '<S59>/Sum2'
  438. * UnitDelay: '<S61>/UnitDelay'
  439. */
  440. rtb_Sum1_l = (int32_T)tmp + localDW->UnitDelay_DSTATE_p;
  441. /* Sum: '<S59>/Sum6' incorporates:
  442. * Product: '<S59>/Divide4'
  443. * Sum: '<S61>/Sum1'
  444. */
  445. tmp = (int64_T)rtb_Divide4_e + rtb_Sum1_l;
  446. if (tmp > 2147483647LL) {
  447. tmp = 2147483647LL;
  448. } else {
  449. if (tmp < -2147483648LL) {
  450. tmp = -2147483648LL;
  451. }
  452. }
  453. /* Switch: '<S62>/Switch2' incorporates:
  454. * RelationalOperator: '<S62>/LowerRelop1'
  455. * RelationalOperator: '<S62>/UpperRelop'
  456. * Sum: '<S59>/Sum6'
  457. * Switch: '<S62>/Switch'
  458. */
  459. if ((int32_T)tmp > (rtu_satMax << 4)) {
  460. *rty_pi_out = rtu_satMax;
  461. } else if ((int32_T)tmp < (rtu_satMin << 4)) {
  462. /* Switch: '<S62>/Switch' */
  463. *rty_pi_out = rtu_satMin;
  464. } else {
  465. *rty_pi_out = (int16_T)((int32_T)tmp >> 4);
  466. }
  467. /* End of Switch: '<S62>/Switch2' */
  468. /* Update for UnitDelay: '<S59>/UnitDelay' incorporates:
  469. * Product: '<S59>/Divide2'
  470. * Sum: '<S59>/Sum3'
  471. * Sum: '<S59>/Sum6'
  472. */
  473. localDW->UnitDelay_DSTATE = (int32_T)(((int64_T)((*rty_pi_out << 4) - (int32_T)
  474. tmp) * rtu_Kb) >> 10);
  475. /* Update for UnitDelay: '<S61>/UnitDelay' incorporates:
  476. * Sum: '<S61>/Sum1'
  477. */
  478. localDW->UnitDelay_DSTATE_p = rtb_Sum1_l;
  479. }
  480. /* System initialize for atomic system: '<S77>/pi_speed' */
  481. static void pi_speed_Init(DW_pi_speed *localDW)
  482. {
  483. /* InitializeConditions for Delay: '<S81>/Resettable Delay' */
  484. localDW->icLoad = 1U;
  485. }
  486. /* Output and update for atomic system: '<S77>/pi_speed' */
  487. static int16_T pi_speed(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T
  488. rtu_Kb, int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_ext_limProt,
  489. uint8_T rtu_reset, const ConstB_pi_speed *localC, DW_pi_speed *localDW,
  490. ZCE_pi_speed *localZCE)
  491. {
  492. int16_T rty_pi_out_0;
  493. int64_T tmp;
  494. int32_T rtb_Divide4_hl;
  495. int32_T rtb_Sum1_b1;
  496. /* Product: '<S80>/Divide4' */
  497. rtb_Divide4_hl = (rtu_err * rtu_P) >> 2;
  498. /* Delay: '<S81>/Resettable Delay' incorporates:
  499. * DataTypeConversion: '<S81>/Data Type Conversion2'
  500. */
  501. if ((rtu_reset > 0) && (localZCE->ResettableDelay_Reset_ZCE != POS_ZCSIG)) {
  502. localDW->icLoad = 1U;
  503. }
  504. localZCE->ResettableDelay_Reset_ZCE = (ZCSigState)(rtu_reset > 0);
  505. if (localDW->icLoad != 0) {
  506. localDW->ResettableDelay_DSTATE = localC->DataTypeConversion2;
  507. }
  508. /* Product: '<S80>/Divide1' incorporates:
  509. * Product: '<S80>/Divide4'
  510. */
  511. tmp = ((int64_T)rtb_Divide4_hl * rtu_I) >> 10;
  512. if (tmp > 2147483647LL) {
  513. tmp = 2147483647LL;
  514. } else {
  515. if (tmp < -2147483648LL) {
  516. tmp = -2147483648LL;
  517. }
  518. }
  519. /* Sum: '<S80>/Sum2' incorporates:
  520. * Product: '<S80>/Divide1'
  521. * UnitDelay: '<S80>/UnitDelay'
  522. */
  523. tmp = (((int64_T)(int32_T)tmp + rtu_ext_limProt) + ((int64_T)
  524. localDW->UnitDelay_DSTATE << 2)) >> 2;
  525. if (tmp > 2147483647LL) {
  526. tmp = 2147483647LL;
  527. } else {
  528. if (tmp < -2147483648LL) {
  529. tmp = -2147483648LL;
  530. }
  531. }
  532. /* Sum: '<S81>/Sum1' incorporates:
  533. * Delay: '<S81>/Resettable Delay'
  534. * Sum: '<S80>/Sum2'
  535. */
  536. rtb_Sum1_b1 = (int32_T)tmp + localDW->ResettableDelay_DSTATE;
  537. /* Sum: '<S80>/Sum6' incorporates:
  538. * DataTypeConversion: '<S81>/Data Type Conversion1'
  539. * Product: '<S80>/Divide4'
  540. * Sum: '<S81>/Sum1'
  541. */
  542. tmp = ((int64_T)(rtb_Sum1_b1 >> 2) << 4) + rtb_Divide4_hl;
  543. if (tmp > 2147483647LL) {
  544. tmp = 2147483647LL;
  545. } else {
  546. if (tmp < -2147483648LL) {
  547. tmp = -2147483648LL;
  548. }
  549. }
  550. /* Switch: '<S82>/Switch2' incorporates:
  551. * RelationalOperator: '<S82>/LowerRelop1'
  552. * RelationalOperator: '<S82>/UpperRelop'
  553. * Sum: '<S80>/Sum6'
  554. * Switch: '<S82>/Switch'
  555. */
  556. if ((int32_T)tmp > (rtu_satMax << 4)) {
  557. rty_pi_out_0 = rtu_satMax;
  558. } else if ((int32_T)tmp < (rtu_satMin << 4)) {
  559. /* Switch: '<S82>/Switch' */
  560. rty_pi_out_0 = rtu_satMin;
  561. } else {
  562. rty_pi_out_0 = (int16_T)((int32_T)tmp >> 4);
  563. }
  564. /* End of Switch: '<S82>/Switch2' */
  565. /* Update for UnitDelay: '<S80>/UnitDelay' incorporates:
  566. * Product: '<S80>/Divide2'
  567. * Sum: '<S80>/Sum3'
  568. * Sum: '<S80>/Sum6'
  569. */
  570. localDW->UnitDelay_DSTATE = (int32_T)(((int64_T)((rty_pi_out_0 << 4) -
  571. (int32_T)tmp) * rtu_Kb) >> 12);
  572. /* Update for Delay: '<S81>/Resettable Delay' incorporates:
  573. * Sum: '<S81>/Sum1'
  574. */
  575. localDW->icLoad = 0U;
  576. localDW->ResettableDelay_DSTATE = rtb_Sum1_b1;
  577. return rty_pi_out_0;
  578. }
  579. /* Model step function */
  580. void PMSM_Controller_step(RT_MODEL *const rtM)
  581. {
  582. DW *rtDW = rtM->dwork;
  583. PrevZCX *rtPrevZCX = rtM->prevZCSigState;
  584. ExtU *rtU = (ExtU *) rtM->inputs;
  585. ExtY *rtY = (ExtY *) rtM->outputs;
  586. int32_T rtb_Divide;
  587. int32_T rtb_Gain1;
  588. int32_T rtb_MultiportSwitch_idx_0;
  589. int32_T rtb_MultiportSwitch_idx_1;
  590. uint32_T tmp;
  591. int16_T rtb_DataTypeConversion[2];
  592. int16_T rtb_TmpSignalConversionAtLow_Pa[2];
  593. int16_T rtb_Abs5;
  594. int16_T rtb_Abs5_h;
  595. int16_T rtb_Divide1_fi;
  596. int16_T rtb_Gain4;
  597. int16_T rtb_Max;
  598. int16_T rtb_Sign;
  599. int16_T rtb_Switch2_ip;
  600. int16_T rtb_Switch3_c;
  601. int16_T rtb_Switch_b;
  602. int16_T rtb_Switch_oi;
  603. uint16_T rtb_LogicalOperator3;
  604. int8_T UnitDelay3;
  605. int8_T rtb_Sum2;
  606. int8_T rtb_Sum2_tmp;
  607. uint8_T rtb_Add_cr;
  608. uint8_T rtb_DataTypeConversion1_c;
  609. uint8_T rtb_Switch2_fu;
  610. uint8_T rtb_UnitDelay;
  611. uint8_T rtb_z_ctrlMod;
  612. boolean_T rtb_LogicalOperator2;
  613. boolean_T rtb_LogicalOperator4;
  614. boolean_T rtb_LogicalOperator_p;
  615. boolean_T rtb_RelationalOperator4_f;
  616. boolean_T rtb_n_commDeacv;
  617. /* Outputs for Atomic SubSystem: '<Root>/PMSM_Controller' */
  618. /* Logic: '<S7>/Edge_Detect' incorporates:
  619. * Delay: '<S7>/Delay'
  620. * Delay: '<S7>/Delay1'
  621. * Delay: '<S7>/Delay2'
  622. * Inport: '<Root>/hall_a'
  623. * Inport: '<Root>/hall_b'
  624. * Inport: '<Root>/hall_c'
  625. */
  626. rtb_LogicalOperator_p = (boolean_T)((rtU->hall_a != 0) ^ (rtDW->Delay_DSTATE
  627. != 0) ^ (rtU->hall_b != 0) ^ (rtDW->Delay1_DSTATE != 0) ^ (rtU->hall_c != 0))
  628. ^ (rtDW->Delay2_DSTATE != 0);
  629. /* Sum: '<S9>/Add' incorporates:
  630. * Gain: '<S9>/Gain'
  631. * Gain: '<S9>/Gain1'
  632. * Inport: '<Root>/hall_a'
  633. * Inport: '<Root>/hall_b'
  634. * Inport: '<Root>/hall_c'
  635. */
  636. rtb_Add_cr = (uint8_T)((uint32_T)(uint8_T)((uint32_T)(uint8_T)(rtU->hall_c <<
  637. 2) + (uint8_T)(rtU->hall_b << 1)) + rtU->hall_a);
  638. /* If: '<S2>/If2' incorporates:
  639. * If: '<S10>/If2'
  640. * Inport: '<S15>/z_counterRawPrev'
  641. * UnitDelay: '<S10>/UnitDelay3'
  642. */
  643. if (rtb_LogicalOperator_p) {
  644. /* Outputs for IfAction SubSystem: '<S2>/Direction_Detection' incorporates:
  645. * ActionPort: '<S6>/Action Port'
  646. */
  647. /* UnitDelay: '<S6>/UnitDelay3' */
  648. UnitDelay3 = rtDW->Switch2_i;
  649. /* End of Outputs for SubSystem: '<S2>/Direction_Detection' */
  650. /* Selector: '<S9>/Selector' incorporates:
  651. * Constant: '<S9>/vec_hallToPos'
  652. */
  653. rtb_Sum2_tmp = rtConstP.vec_hallToPos_Value[rtb_Add_cr];
  654. /* Outputs for IfAction SubSystem: '<S2>/Direction_Detection' incorporates:
  655. * ActionPort: '<S6>/Action Port'
  656. */
  657. /* Sum: '<S6>/Sum2' incorporates:
  658. * Constant: '<S9>/vec_hallToPos'
  659. * Selector: '<S9>/Selector'
  660. * UnitDelay: '<S6>/UnitDelay2'
  661. */
  662. rtb_Sum2 = (int8_T)(rtb_Sum2_tmp - rtDW->UnitDelay2_DSTATE_j);
  663. /* Switch: '<S6>/Switch2' incorporates:
  664. * Constant: '<S6>/Constant20'
  665. * Constant: '<S6>/Constant8'
  666. * Logic: '<S6>/Logical Operator3'
  667. * RelationalOperator: '<S6>/Relational Operator1'
  668. * RelationalOperator: '<S6>/Relational Operator6'
  669. */
  670. if ((rtb_Sum2 == 1) || (rtb_Sum2 == -5)) {
  671. /* Switch: '<S6>/Switch2' incorporates:
  672. * Constant: '<S6>/Constant24'
  673. */
  674. rtDW->Switch2_i = 1;
  675. } else {
  676. /* Switch: '<S6>/Switch2' incorporates:
  677. * Constant: '<S6>/Constant23'
  678. */
  679. rtDW->Switch2_i = -1;
  680. }
  681. /* End of Switch: '<S6>/Switch2' */
  682. /* Update for UnitDelay: '<S6>/UnitDelay2' */
  683. rtDW->UnitDelay2_DSTATE_j = rtb_Sum2_tmp;
  684. /* End of Outputs for SubSystem: '<S2>/Direction_Detection' */
  685. /* Outputs for IfAction SubSystem: '<S10>/Raw_Motor_Speed_Estimation' incorporates:
  686. * ActionPort: '<S15>/Action Port'
  687. */
  688. /* RelationalOperator: '<S15>/Relational Operator4' */
  689. rtb_RelationalOperator4_f = (rtDW->Switch2_i != UnitDelay3);
  690. rtDW->z_counterRawPrev = rtDW->UnitDelay3_DSTATE;
  691. /* Switch: '<S15>/Switch3' incorporates:
  692. * Constant: '<S15>/Constant4'
  693. * Inport: '<S15>/z_counterRawPrev'
  694. * Logic: '<S15>/Logical Operator1'
  695. * Switch: '<S15>/Switch2'
  696. * UnitDelay: '<S10>/UnitDelay3'
  697. * UnitDelay: '<S15>/UnitDelay1'
  698. */
  699. if (rtb_RelationalOperator4_f && rtDW->UnitDelay1_DSTATE_i) {
  700. rtb_Switch3_c = 0;
  701. } else if (rtb_RelationalOperator4_f) {
  702. /* Switch: '<S15>/Switch3' incorporates:
  703. * Switch: '<S15>/Switch2'
  704. * UnitDelay: '<S10>/UnitDelay4'
  705. */
  706. rtb_Switch3_c = rtDW->UnitDelay4_DSTATE;
  707. } else {
  708. /* Product: '<S15>/Divide13' incorporates:
  709. * Sum: '<S15>/Sum13'
  710. * Switch: '<S15>/Switch2'
  711. * UnitDelay: '<S15>/UnitDelay2'
  712. * UnitDelay: '<S15>/UnitDelay3'
  713. * UnitDelay: '<S15>/UnitDelay5'
  714. */
  715. tmp = 8000000U / (((rtDW->UnitDelay2_DSTATE + rtDW->UnitDelay3_DSTATE_l) +
  716. rtDW->UnitDelay5_DSTATE) + rtDW->z_counterRawPrev);
  717. if (tmp > 32767U) {
  718. tmp = 32767U;
  719. }
  720. /* Switch: '<S15>/Switch3' incorporates:
  721. * Product: '<S15>/Divide13'
  722. * Switch: '<S15>/Switch2'
  723. */
  724. rtb_Switch3_c = (int16_T)tmp;
  725. }
  726. /* End of Switch: '<S15>/Switch3' */
  727. /* Product: '<S15>/Divide11' incorporates:
  728. * Switch: '<S15>/Switch3'
  729. */
  730. rtDW->Divide11 = (int16_T)(rtb_Switch3_c * rtDW->Switch2_i);
  731. /* Update for UnitDelay: '<S15>/UnitDelay1' */
  732. rtDW->UnitDelay1_DSTATE_i = rtb_RelationalOperator4_f;
  733. /* Update for UnitDelay: '<S15>/UnitDelay2' incorporates:
  734. * UnitDelay: '<S15>/UnitDelay3'
  735. */
  736. rtDW->UnitDelay2_DSTATE = rtDW->UnitDelay3_DSTATE_l;
  737. /* Update for UnitDelay: '<S15>/UnitDelay3' incorporates:
  738. * UnitDelay: '<S15>/UnitDelay5'
  739. */
  740. rtDW->UnitDelay3_DSTATE_l = rtDW->UnitDelay5_DSTATE;
  741. /* Update for UnitDelay: '<S15>/UnitDelay5' */
  742. rtDW->UnitDelay5_DSTATE = rtDW->z_counterRawPrev;
  743. /* End of Outputs for SubSystem: '<S10>/Raw_Motor_Speed_Estimation' */
  744. }
  745. /* End of If: '<S2>/If2' */
  746. /* Switch: '<S8>/Switch3' incorporates:
  747. * Constant: '<S8>/Constant16'
  748. * Constant: '<S8>/Constant2'
  749. * Constant: '<S9>/vec_hallToPos'
  750. * RelationalOperator: '<S8>/Relational Operator7'
  751. * Selector: '<S9>/Selector'
  752. * Sum: '<S8>/Sum1'
  753. */
  754. if (rtDW->Switch2_i == 1) {
  755. rtb_Sum2 = rtConstP.vec_hallToPos_Value[rtb_Add_cr];
  756. } else {
  757. rtb_Sum2 = (int8_T)(rtConstP.vec_hallToPos_Value[rtb_Add_cr] + 1);
  758. }
  759. /* End of Switch: '<S8>/Switch3' */
  760. /* MinMax: '<S8>/MinMax' incorporates:
  761. * Inport: '<Root>/hw_count'
  762. */
  763. if (rtU->hw_count < rtDW->z_counterRawPrev) {
  764. tmp = rtU->hw_count;
  765. } else {
  766. tmp = rtDW->z_counterRawPrev;
  767. }
  768. /* End of MinMax: '<S8>/MinMax' */
  769. /* Sum: '<S8>/Sum3' incorporates:
  770. * Product: '<S8>/Divide1'
  771. * Product: '<S8>/Divide3'
  772. */
  773. rtb_Switch3_c = (int16_T)(((int16_T)((int16_T)(((uint64_T)tmp << 14) /
  774. rtDW->z_counterRawPrev) * rtDW->Switch2_i) + (rtb_Sum2 << 14)) >> 2);
  775. /* MinMax: '<S8>/MinMax1' incorporates:
  776. * Constant: '<S8>/Constant1'
  777. * Sum: '<S8>/Sum3'
  778. * Switch: '<S8>/Switch2'
  779. */
  780. if (rtb_Switch3_c <= 0) {
  781. rtb_Switch3_c = 0;
  782. }
  783. /* End of MinMax: '<S8>/MinMax1' */
  784. /* Sum: '<S11>/Add2' incorporates:
  785. * Constant: '<S11>/Constant2'
  786. * Product: '<S8>/Divide2'
  787. */
  788. rtb_Switch3_c = (int16_T)((((15 * rtb_Switch3_c) >> 4) + 3840) >> 2);
  789. /* If: '<S11>/If' incorporates:
  790. * Constant: '<S11>/Constant3'
  791. * DataTypeConversion: '<S11>/Data Type Conversion'
  792. * Inport: '<S12>/In1'
  793. * Merge: '<S11>/Merge'
  794. * Sum: '<S11>/Add'
  795. * Sum: '<S11>/Add2'
  796. */
  797. if ((int16_T)(rtb_Switch3_c >> 4) >= 360) {
  798. /* Outputs for IfAction SubSystem: '<S11>/If Action Subsystem' incorporates:
  799. * ActionPort: '<S12>/Action Port'
  800. */
  801. rtb_Switch3_c = (int16_T)(rtb_Switch3_c - 5760);
  802. /* End of Outputs for SubSystem: '<S11>/If Action Subsystem' */
  803. }
  804. /* End of If: '<S11>/If' */
  805. /* Switch: '<S10>/Switch2' incorporates:
  806. * Constant: '<S10>/Constant4'
  807. * Inport: '<Root>/hw_count'
  808. * Product: '<S15>/Divide11'
  809. * RelationalOperator: '<S10>/Relational Operator2'
  810. */
  811. if (rtU->hw_count >= 400000U) {
  812. rtb_Switch2_ip = 0;
  813. } else {
  814. rtb_Switch2_ip = rtDW->Divide11;
  815. }
  816. /* End of Switch: '<S10>/Switch2' */
  817. /* Abs: '<S10>/Abs5' incorporates:
  818. * Switch: '<S10>/Switch2'
  819. */
  820. if (rtb_Switch2_ip < 0) {
  821. rtb_Abs5 = (int16_T)-rtb_Switch2_ip;
  822. } else {
  823. rtb_Abs5 = rtb_Switch2_ip;
  824. }
  825. /* End of Abs: '<S10>/Abs5' */
  826. /* If: '<S10>/If1' */
  827. if (rtb_LogicalOperator_p) {
  828. /* Outputs for IfAction SubSystem: '<S10>/Subsystem' incorporates:
  829. * ActionPort: '<S16>/Action Port'
  830. */
  831. /* Relay: '<S16>/n_commDeacv' incorporates:
  832. * Abs: '<S10>/Abs5'
  833. */
  834. rtDW->n_commDeacv_Mode = ((rtb_Abs5 >= 120) || ((rtb_Abs5 > 60) &&
  835. rtDW->n_commDeacv_Mode));
  836. /* RelationalOperator: '<S18>/Compare' incorporates:
  837. * Constant: '<S18>/Constant'
  838. * Relay: '<S16>/n_commDeacv'
  839. * Sum: '<S16>/Sum13'
  840. * UnitDelay: '<S16>/UnitDelay2'
  841. * UnitDelay: '<S16>/UnitDelay3'
  842. * UnitDelay: '<S16>/UnitDelay5'
  843. */
  844. rtDW->Compare = ((uint16_T)((uint32_T)(uint16_T)((uint32_T)(uint16_T)
  845. ((uint32_T)rtDW->UnitDelay2_DSTATE_f + rtDW->UnitDelay3_DSTATE_lh) +
  846. rtDW->UnitDelay5_DSTATE_f) + rtDW->n_commDeacv_Mode) >= 4);
  847. /* Update for UnitDelay: '<S16>/UnitDelay2' incorporates:
  848. * UnitDelay: '<S16>/UnitDelay3'
  849. */
  850. rtDW->UnitDelay2_DSTATE_f = rtDW->UnitDelay3_DSTATE_lh;
  851. /* Update for UnitDelay: '<S16>/UnitDelay3' incorporates:
  852. * UnitDelay: '<S16>/UnitDelay5'
  853. */
  854. rtDW->UnitDelay3_DSTATE_lh = rtDW->UnitDelay5_DSTATE_f;
  855. /* Update for UnitDelay: '<S16>/UnitDelay5' incorporates:
  856. * Logic: '<S16>/Logical Operator3'
  857. * Relay: '<S16>/n_commDeacv'
  858. */
  859. rtDW->UnitDelay5_DSTATE_f = rtDW->n_commDeacv_Mode;
  860. /* End of Outputs for SubSystem: '<S10>/Subsystem' */
  861. }
  862. /* End of If: '<S10>/If1' */
  863. /* Switch: '<S2>/Switch' incorporates:
  864. * Inport: '<Root>/b_hall_calibrate'
  865. * Inport: '<Root>/open_theta'
  866. * Merge: '<S11>/Merge'
  867. */
  868. if (rtU->b_hall_calibrate) {
  869. rtb_Switch_b = (int16_T)(rtU->open_theta << 4);
  870. } else {
  871. rtb_Switch_b = rtb_Switch3_c;
  872. }
  873. /* End of Switch: '<S2>/Switch' */
  874. /* Abs: '<S3>/Abs2' incorporates:
  875. * Switch: '<S10>/Switch2'
  876. */
  877. if (rtb_Switch2_ip < 0) {
  878. rtb_LogicalOperator3 = (uint16_T)((uint32_T)-rtb_Switch2_ip >> 2);
  879. } else {
  880. rtb_LogicalOperator3 = (uint16_T)((uint32_T)rtb_Switch2_ip >> 2);
  881. }
  882. /* End of Abs: '<S3>/Abs2' */
  883. /* UnitDelay: '<S34>/UnitDelay' */
  884. rtb_UnitDelay = rtDW->UnitDelay_DSTATE_j;
  885. /* Outport: '<Root>/VqPrev' incorporates:
  886. * UnitDelay: '<S5>/UnitDelay2'
  887. */
  888. rtY->VqPrev = rtDW->UnitDelay2_DSTATE_p;
  889. /* Switch: '<S34>/Switch3' incorporates:
  890. * Abs: '<S10>/Abs5'
  891. * Abs: '<S34>/Abs4'
  892. * Constant: '<S34>/CTRL_COMM4'
  893. * Inport: '<Root>/b_motEna'
  894. * Logic: '<S34>/Logical Operator1'
  895. * RelationalOperator: '<S10>/Relational Operator9'
  896. * RelationalOperator: '<S34>/Relational Operator7'
  897. * S-Function (sfix_bitop): '<S34>/Bitwise Operator1'
  898. * UnitDelay: '<S5>/UnitDelay2'
  899. */
  900. if ((rtb_UnitDelay & 4U) != 0U) {
  901. rtb_LogicalOperator_p = true;
  902. } else {
  903. if (rtDW->UnitDelay2_DSTATE_p < 0) {
  904. /* Abs: '<S34>/Abs4' incorporates:
  905. * UnitDelay: '<S5>/UnitDelay2'
  906. */
  907. rtb_Divide1_fi = (int16_T)-rtDW->UnitDelay2_DSTATE_p;
  908. } else {
  909. /* Abs: '<S34>/Abs4' incorporates:
  910. * UnitDelay: '<S5>/UnitDelay2'
  911. */
  912. rtb_Divide1_fi = rtDW->UnitDelay2_DSTATE_p;
  913. }
  914. rtb_LogicalOperator_p = (rtU->b_motEna && (rtb_Abs5 < 12) && (rtb_Divide1_fi
  915. > 960));
  916. }
  917. /* End of Switch: '<S34>/Switch3' */
  918. /* Sum: '<S34>/Sum' incorporates:
  919. * Constant: '<S34>/CTRL_COMM'
  920. * Constant: '<S34>/CTRL_COMM1'
  921. * DataTypeConversion: '<S34>/Data Type Conversion3'
  922. * Gain: '<S34>/g_Hb'
  923. * Gain: '<S34>/g_Hb1'
  924. * RelationalOperator: '<S34>/Relational Operator1'
  925. * RelationalOperator: '<S34>/Relational Operator3'
  926. */
  927. rtb_DataTypeConversion1_c = (uint8_T)(((uint32_T)((rtb_Add_cr == 7) << 1) +
  928. (rtb_Add_cr == 0)) + (rtb_LogicalOperator_p << 2));
  929. /* Outputs for Atomic SubSystem: '<S34>/Debounce_Filter' */
  930. /* RelationalOperator: '<S34>/Relational Operator2' incorporates:
  931. * Constant: '<S34>/CTRL_COMM2'
  932. * Constant: '<S34>/t_errDequal'
  933. * Constant: '<S34>/t_errQual'
  934. */
  935. Debounce_Filter(rtb_DataTypeConversion1_c != 0, 1600, 12000,
  936. &rtb_RelationalOperator4_f, &rtDW->Debounce_Filter_i);
  937. /* End of Outputs for SubSystem: '<S34>/Debounce_Filter' */
  938. /* Logic: '<S21>/Logical Operator12' incorporates:
  939. * Inport: '<Root>/b_motEna'
  940. * Logic: '<S21>/Logical Operator7'
  941. */
  942. rtb_n_commDeacv = ((!rtb_RelationalOperator4_f) && rtU->b_motEna);
  943. /* Logic: '<S21>/Logical Operator4' incorporates:
  944. * Constant: '<S21>/constant8'
  945. * Inport: '<Root>/b_hall_calibrate'
  946. * Inport: '<Root>/n_ctrlModReq'
  947. * Logic: '<S21>/Logical Operator11'
  948. * Logic: '<S21>/Logical Operator8'
  949. * RelationalOperator: '<S21>/Relational Operator10'
  950. */
  951. rtb_LogicalOperator4 = (rtU->b_hall_calibrate || (!rtDW->Compare) ||
  952. (!rtb_n_commDeacv) || (rtU->n_ctrlModReq == 0));
  953. /* Relay: '<S21>/n_SpeedCtrl' */
  954. rtDW->n_SpeedCtrl_Mode = ((rtb_LogicalOperator3 >= 300) ||
  955. ((rtb_LogicalOperator3 > 200) && rtDW->n_SpeedCtrl_Mode));
  956. rtb_LogicalOperator_p = rtDW->n_SpeedCtrl_Mode;
  957. /* Logic: '<S21>/Logical Operator10' incorporates:
  958. * Inport: '<Root>/b_cruiseEna'
  959. */
  960. rtb_LogicalOperator_p = (rtb_LogicalOperator_p && rtU->b_cruiseEna);
  961. /* Logic: '<S21>/Logical Operator2' incorporates:
  962. * Constant: '<S21>/constant'
  963. * Inport: '<Root>/n_ctrlModReq'
  964. * Logic: '<S21>/Logical Operator5'
  965. * RelationalOperator: '<S21>/Relational Operator4'
  966. */
  967. rtb_LogicalOperator2 = ((rtU->n_ctrlModReq == 2) && (!rtb_LogicalOperator_p));
  968. /* Logic: '<S21>/Logical Operator1' incorporates:
  969. * Constant: '<S21>/constant1'
  970. * Inport: '<Root>/n_ctrlModReq'
  971. * RelationalOperator: '<S21>/Relational Operator1'
  972. */
  973. rtb_LogicalOperator_p = ((rtU->n_ctrlModReq == 1) || rtb_LogicalOperator_p);
  974. /* Chart: '<S3>/Control_Mode_Manager' incorporates:
  975. * Logic: '<S21>/Logical Operator3'
  976. * Logic: '<S21>/Logical Operator6'
  977. * Logic: '<S21>/Logical Operator9'
  978. */
  979. if (rtDW->is_active_c5_PMSM_Controller == 0U) {
  980. rtDW->is_active_c5_PMSM_Controller = 1U;
  981. rtDW->is_c5_PMSM_Controller = IN_OPEN;
  982. rtb_z_ctrlMod = OPEN_MODE;
  983. } else if (rtDW->is_c5_PMSM_Controller == 1) {
  984. if (rtb_LogicalOperator4) {
  985. rtDW->is_ACTIVE = IN_NO_ACTIVE_CHILD;
  986. rtDW->is_c5_PMSM_Controller = IN_OPEN;
  987. rtb_z_ctrlMod = OPEN_MODE;
  988. } else if (rtDW->is_ACTIVE == 1) {
  989. rtb_z_ctrlMod = SPD_MODE;
  990. if (!rtb_LogicalOperator_p) {
  991. if (rtb_LogicalOperator2) {
  992. rtDW->is_ACTIVE = IN_TORQUE_MODE;
  993. rtb_z_ctrlMod = TRQ_MODE;
  994. } else {
  995. rtDW->is_ACTIVE = IN_SPEED_MODE;
  996. }
  997. }
  998. } else {
  999. /* case IN_TORQUE_MODE: */
  1000. rtb_z_ctrlMod = TRQ_MODE;
  1001. if (!rtb_LogicalOperator2) {
  1002. rtDW->is_ACTIVE = IN_SPEED_MODE;
  1003. rtb_z_ctrlMod = SPD_MODE;
  1004. }
  1005. }
  1006. } else {
  1007. /* case IN_OPEN: */
  1008. rtb_z_ctrlMod = OPEN_MODE;
  1009. if ((!rtb_LogicalOperator4) && (rtb_LogicalOperator2 ||
  1010. rtb_LogicalOperator_p)) {
  1011. rtDW->is_c5_PMSM_Controller = IN_ACTIVE;
  1012. if (rtb_LogicalOperator2) {
  1013. rtDW->is_ACTIVE = IN_TORQUE_MODE;
  1014. rtb_z_ctrlMod = TRQ_MODE;
  1015. } else {
  1016. rtDW->is_ACTIVE = IN_SPEED_MODE;
  1017. rtb_z_ctrlMod = SPD_MODE;
  1018. }
  1019. }
  1020. }
  1021. /* End of Chart: '<S3>/Control_Mode_Manager' */
  1022. /* Switch: '<S22>/Switch' incorporates:
  1023. * Constant: '<S22>/Constant3'
  1024. * Inport: '<Root>/input_target'
  1025. */
  1026. if (rtU->input_target > 60) {
  1027. /* Switch: '<S22>/Switch1' incorporates:
  1028. * Constant: '<S22>/Constant1'
  1029. * DataTypeConversion: '<S22>/Data Type Conversion'
  1030. * Switch: '<S22>/Switch'
  1031. */
  1032. if (rtb_n_commDeacv) {
  1033. rtb_Switch_oi = rtU->input_target;
  1034. } else {
  1035. rtb_Switch_oi = 0;
  1036. }
  1037. /* End of Switch: '<S22>/Switch1' */
  1038. } else {
  1039. rtb_Switch_oi = 0;
  1040. }
  1041. /* End of Switch: '<S22>/Switch' */
  1042. /* Switch: '<S22>/Switch3' incorporates:
  1043. * Constant: '<S22>/Constant4'
  1044. * DataTypeConversion: '<S22>/Data Type Conversion2'
  1045. * Inport: '<Root>/vq_open_target'
  1046. */
  1047. if (rtb_n_commDeacv) {
  1048. rtb_Abs5_h = rtU->vq_open_target;
  1049. } else {
  1050. rtb_Abs5_h = 0;
  1051. }
  1052. /* End of Switch: '<S22>/Switch3' */
  1053. /* If: '<S23>/If' incorporates:
  1054. * Inport: '<Root>/b_hall_calibrate'
  1055. * Inport: '<S27>/vq_in'
  1056. * Switch: '<S22>/Switch3'
  1057. */
  1058. if (rtU->b_hall_calibrate) {
  1059. /* Switch: '<S22>/Switch2' incorporates:
  1060. * Constant: '<S22>/Constant2'
  1061. * DataTypeConversion: '<S22>/Data Type Conversion1'
  1062. * Inport: '<Root>/vd_open_target'
  1063. * Inport: '<S27>/vd_in'
  1064. */
  1065. if (rtb_n_commDeacv) {
  1066. /* Outputs for IfAction SubSystem: '<S23>/If Action Subsystem' incorporates:
  1067. * ActionPort: '<S27>/Action Port'
  1068. */
  1069. rtDW->Merge[0] = rtU->vd_open_target;
  1070. /* End of Outputs for SubSystem: '<S23>/If Action Subsystem' */
  1071. } else {
  1072. /* Outputs for IfAction SubSystem: '<S23>/If Action Subsystem' incorporates:
  1073. * ActionPort: '<S27>/Action Port'
  1074. */
  1075. rtDW->Merge[0] = 0;
  1076. /* End of Outputs for SubSystem: '<S23>/If Action Subsystem' */
  1077. }
  1078. /* End of Switch: '<S22>/Switch2' */
  1079. /* Outputs for IfAction SubSystem: '<S23>/If Action Subsystem' incorporates:
  1080. * ActionPort: '<S27>/Action Port'
  1081. */
  1082. rtDW->Merge[1] = rtb_Abs5_h;
  1083. /* End of Outputs for SubSystem: '<S23>/If Action Subsystem' */
  1084. } else if (rtb_z_ctrlMod == 0) {
  1085. /* Outputs for IfAction SubSystem: '<S23>/open_mode' incorporates:
  1086. * ActionPort: '<S28>/Action Port'
  1087. */
  1088. /* RelationalOperator: '<S28>/Equal1' incorporates:
  1089. * Switch: '<S22>/Switch3'
  1090. * UnitDelay: '<S28>/Unit Delay'
  1091. */
  1092. rtb_LogicalOperator_p = (rtDW->UnitDelay_DSTATE != rtb_Abs5_h);
  1093. /* If: '<S30>/If' */
  1094. if (rtb_LogicalOperator_p) {
  1095. /* Outputs for IfAction SubSystem: '<S30>/Subsystem' incorporates:
  1096. * ActionPort: '<S32>/Action Port'
  1097. */
  1098. /* Sum: '<S32>/Add' incorporates:
  1099. * Signum: '<S32>/Sign'
  1100. * Switch: '<S22>/Switch3'
  1101. * UnitDelay: '<S5>/UnitDelay2'
  1102. */
  1103. rtb_Sign = (int16_T)((rtb_Abs5_h - rtDW->UnitDelay2_DSTATE_p) >> 2);
  1104. /* Signum: '<S32>/Sign' */
  1105. if (rtb_Sign < 0) {
  1106. rtb_Sign = -1;
  1107. } else {
  1108. rtb_Sign = (int16_T)(rtb_Sign > 0);
  1109. }
  1110. /* End of Signum: '<S32>/Sign' */
  1111. /* Product: '<S32>/Divide' incorporates:
  1112. * Constant: '<S28>/Constant5'
  1113. */
  1114. rtDW->Divide = (int16_T)(rtb_Sign * 6);
  1115. /* Switch: '<S32>/Switch' incorporates:
  1116. * Switch: '<S32>/Switch1'
  1117. */
  1118. if (rtb_Sign > 0) {
  1119. /* Switch: '<S32>/Switch' incorporates:
  1120. * Switch: '<S22>/Switch3'
  1121. */
  1122. rtDW->Switch = rtb_Abs5_h;
  1123. /* Switch: '<S32>/Switch1' incorporates:
  1124. * UnitDelay: '<S5>/UnitDelay2'
  1125. */
  1126. rtDW->Switch1 = rtDW->UnitDelay2_DSTATE_p;
  1127. } else {
  1128. /* Switch: '<S32>/Switch' incorporates:
  1129. * UnitDelay: '<S5>/UnitDelay2'
  1130. */
  1131. rtDW->Switch = rtDW->UnitDelay2_DSTATE_p;
  1132. /* Switch: '<S32>/Switch1' incorporates:
  1133. * Switch: '<S22>/Switch3'
  1134. */
  1135. rtDW->Switch1 = rtb_Abs5_h;
  1136. }
  1137. /* End of Switch: '<S32>/Switch' */
  1138. /* End of Outputs for SubSystem: '<S30>/Subsystem' */
  1139. /* Switch: '<S33>/Switch1' incorporates:
  1140. * UnitDelay: '<S5>/UnitDelay2'
  1141. */
  1142. rtb_Sign = rtDW->UnitDelay2_DSTATE_p;
  1143. } else {
  1144. /* Switch: '<S33>/Switch1' incorporates:
  1145. * UnitDelay: '<S33>/UnitDelay'
  1146. */
  1147. rtb_Sign = rtDW->UnitDelay_DSTATE_d;
  1148. }
  1149. /* End of If: '<S30>/If' */
  1150. /* Sum: '<S30>/Add2' incorporates:
  1151. * Product: '<S32>/Divide'
  1152. */
  1153. rtb_Gain1 = ((rtb_Sign << 1) + rtDW->Divide) >> 1;
  1154. if (rtb_Gain1 > 32767) {
  1155. rtb_Gain1 = 32767;
  1156. } else {
  1157. if (rtb_Gain1 < -32768) {
  1158. rtb_Gain1 = -32768;
  1159. }
  1160. }
  1161. /* Switch: '<S28>/Switch' incorporates:
  1162. * Switch: '<S22>/Switch'
  1163. */
  1164. if (rtb_Switch_oi > 0) {
  1165. /* Switch: '<S31>/Switch2' incorporates:
  1166. * RelationalOperator: '<S31>/LowerRelop1'
  1167. * RelationalOperator: '<S31>/UpperRelop'
  1168. * Sum: '<S30>/Add2'
  1169. * Switch: '<S31>/Switch'
  1170. * Switch: '<S32>/Switch'
  1171. * Switch: '<S32>/Switch1'
  1172. */
  1173. if ((int16_T)rtb_Gain1 > rtDW->Switch) {
  1174. /* Merge: '<S23>/Merge' incorporates:
  1175. * Switch: '<S28>/Switch'
  1176. */
  1177. rtDW->Merge[1] = rtDW->Switch;
  1178. } else if ((int16_T)rtb_Gain1 < rtDW->Switch1) {
  1179. /* Merge: '<S23>/Merge' incorporates:
  1180. * Switch: '<S28>/Switch'
  1181. * Switch: '<S31>/Switch'
  1182. * Switch: '<S32>/Switch1'
  1183. */
  1184. rtDW->Merge[1] = rtDW->Switch1;
  1185. } else {
  1186. /* Merge: '<S23>/Merge' incorporates:
  1187. * Switch: '<S28>/Switch'
  1188. */
  1189. rtDW->Merge[1] = (int16_T)rtb_Gain1;
  1190. }
  1191. /* End of Switch: '<S31>/Switch2' */
  1192. } else {
  1193. /* Merge: '<S23>/Merge' incorporates:
  1194. * Constant: '<S28>/Constant1'
  1195. */
  1196. rtDW->Merge[1] = 0;
  1197. }
  1198. /* End of Switch: '<S28>/Switch' */
  1199. /* Merge: '<S23>/Merge' incorporates:
  1200. * Constant: '<S28>/Constant3'
  1201. * SignalConversion generated from: '<S28>/open_voltage'
  1202. */
  1203. rtDW->Merge[0] = 0;
  1204. /* Update for UnitDelay: '<S28>/Unit Delay' incorporates:
  1205. * Switch: '<S22>/Switch3'
  1206. */
  1207. rtDW->UnitDelay_DSTATE = rtb_Abs5_h;
  1208. /* Switch: '<S33>/Switch2' */
  1209. if (rtb_LogicalOperator_p) {
  1210. /* Update for UnitDelay: '<S33>/UnitDelay' incorporates:
  1211. * UnitDelay: '<S5>/UnitDelay2'
  1212. */
  1213. rtDW->UnitDelay_DSTATE_d = rtDW->UnitDelay2_DSTATE_p;
  1214. } else {
  1215. /* Update for UnitDelay: '<S33>/UnitDelay' incorporates:
  1216. * Sum: '<S30>/Add2'
  1217. */
  1218. rtDW->UnitDelay_DSTATE_d = (int16_T)rtb_Gain1;
  1219. }
  1220. /* End of Switch: '<S33>/Switch2' */
  1221. /* End of Outputs for SubSystem: '<S23>/open_mode' */
  1222. } else {
  1223. /* Outputs for IfAction SubSystem: '<S23>/torque_mode' incorporates:
  1224. * ActionPort: '<S29>/Action Port'
  1225. */
  1226. /* Product: '<S29>/Divide1' incorporates:
  1227. * Inport: '<Root>/i_dc_limit'
  1228. * Inport: '<Root>/speed_limit'
  1229. * Product: '<S29>/Divide4'
  1230. * Switch: '<S22>/Switch'
  1231. */
  1232. rtb_Gain1 = ((uint16_T)((rtU->i_dc_limit << 8) / rtU->speed_limit) *
  1233. rtb_Switch_oi) >> 8;
  1234. if (rtb_Gain1 > 32767) {
  1235. rtb_Gain1 = 32767;
  1236. } else {
  1237. if (rtb_Gain1 < -32768) {
  1238. rtb_Gain1 = -32768;
  1239. }
  1240. }
  1241. /* Product: '<S29>/Divide1' */
  1242. rtDW->Divide1 = (int16_T)rtb_Gain1;
  1243. /* End of Outputs for SubSystem: '<S23>/torque_mode' */
  1244. }
  1245. /* End of If: '<S23>/If' */
  1246. /* Outputs for Atomic SubSystem: '<S34>/either_edge' */
  1247. rtb_LogicalOperator_p = either_edge(rtb_RelationalOperator4_f,
  1248. &rtDW->either_edge_f);
  1249. /* End of Outputs for SubSystem: '<S34>/either_edge' */
  1250. /* Switch: '<S34>/Switch1' */
  1251. if (rtb_LogicalOperator_p) {
  1252. rtb_UnitDelay = rtb_DataTypeConversion1_c;
  1253. }
  1254. /* End of Switch: '<S34>/Switch1' */
  1255. /* Gain: '<S51>/Multiply' incorporates:
  1256. * DataTypeConversion: '<S54>/Data Type Conversion'
  1257. * Inport: '<Root>/adc_a'
  1258. * Inport: '<Root>/adc_b'
  1259. */
  1260. rtb_Gain1 = (12351 * rtU->adc_a) >> 11;
  1261. if (rtb_Gain1 > 32767) {
  1262. rtb_Gain1 = 32767;
  1263. } else {
  1264. if (rtb_Gain1 < -32768) {
  1265. rtb_Gain1 = -32768;
  1266. }
  1267. }
  1268. rtb_DataTypeConversion[0] = (int16_T)rtb_Gain1;
  1269. rtb_MultiportSwitch_idx_0 = (12351 * rtU->adc_b) >> 11;
  1270. if (rtb_MultiportSwitch_idx_0 > 32767) {
  1271. rtb_MultiportSwitch_idx_0 = 32767;
  1272. } else {
  1273. if (rtb_MultiportSwitch_idx_0 < -32768) {
  1274. rtb_MultiportSwitch_idx_0 = -32768;
  1275. }
  1276. }
  1277. rtb_DataTypeConversion[1] = (int16_T)rtb_MultiportSwitch_idx_0;
  1278. /* Sum: '<S45>/Add' incorporates:
  1279. * Gain: '<S51>/Multiply'
  1280. */
  1281. rtb_MultiportSwitch_idx_1 = (int16_T)rtb_Gain1 + (int16_T)
  1282. rtb_MultiportSwitch_idx_0;
  1283. if (rtb_MultiportSwitch_idx_1 > 32767) {
  1284. rtb_MultiportSwitch_idx_1 = 32767;
  1285. } else {
  1286. if (rtb_MultiportSwitch_idx_1 < -32768) {
  1287. rtb_MultiportSwitch_idx_1 = -32768;
  1288. }
  1289. }
  1290. /* Sum: '<S45>/Add1' incorporates:
  1291. * Sum: '<S45>/Add'
  1292. */
  1293. rtb_Divide = -rtb_MultiportSwitch_idx_1;
  1294. if (-rtb_MultiportSwitch_idx_1 > 32767) {
  1295. rtb_Divide = 32767;
  1296. }
  1297. /* Sum: '<S53>/Add3' incorporates:
  1298. * Gain: '<S51>/Multiply'
  1299. * Sum: '<S45>/Add1'
  1300. */
  1301. rtb_MultiportSwitch_idx_1 = (int16_T)rtb_MultiportSwitch_idx_0 + (int16_T)
  1302. rtb_Divide;
  1303. if (rtb_MultiportSwitch_idx_1 > 32767) {
  1304. rtb_MultiportSwitch_idx_1 = 32767;
  1305. } else {
  1306. if (rtb_MultiportSwitch_idx_1 < -32768) {
  1307. rtb_MultiportSwitch_idx_1 = -32768;
  1308. }
  1309. }
  1310. /* Sum: '<S53>/Add' incorporates:
  1311. * Gain: '<S51>/Multiply'
  1312. * Sum: '<S53>/Add3'
  1313. */
  1314. rtb_Gain1 = (((int16_T)rtb_Gain1 << 1) - rtb_MultiportSwitch_idx_1) >> 1;
  1315. if (rtb_Gain1 > 32767) {
  1316. rtb_Gain1 = 32767;
  1317. } else {
  1318. if (rtb_Gain1 < -32768) {
  1319. rtb_Gain1 = -32768;
  1320. }
  1321. }
  1322. /* Gain: '<S53>/Gain1' incorporates:
  1323. * Product: '<S55>/Divide1'
  1324. * Sum: '<S53>/Add'
  1325. */
  1326. rtb_Divide1_fi = (int16_T)((21845 * rtb_Gain1) >> 15);
  1327. /* Gain: '<S53>/Gain2' incorporates:
  1328. * Gain: '<S51>/Multiply'
  1329. * Sum: '<S45>/Add1'
  1330. * Sum: '<S53>/Add2'
  1331. */
  1332. rtb_Gain1 = ((((int16_T)rtb_MultiportSwitch_idx_0 - (int16_T)rtb_Divide) >> 1)
  1333. * 18919) >> 14;
  1334. if (rtb_Gain1 > 32767) {
  1335. rtb_Gain1 = 32767;
  1336. } else {
  1337. if (rtb_Gain1 < -32768) {
  1338. rtb_Gain1 = -32768;
  1339. }
  1340. }
  1341. /* PreLookup: '<S56>/a_elecAngle_XA' incorporates:
  1342. * Switch: '<S2>/Switch'
  1343. */
  1344. rtb_LogicalOperator3 = plook_u16s16_evencka(rtb_Switch_b, 0, 4U, 1440U);
  1345. /* Interpolation_n-D: '<S56>/r_cos_M1' */
  1346. rtb_Sign = rtConstP.r_cos_M1_Table[rtb_LogicalOperator3];
  1347. /* Interpolation_n-D: '<S56>/r_sin_M1' incorporates:
  1348. * Product: '<S67>/Divide4'
  1349. */
  1350. rtb_Abs5_h = rtConstP.r_sin_M1_Table[rtb_LogicalOperator3];
  1351. /* Sum: '<S55>/Sum1' incorporates:
  1352. * Gain: '<S53>/Gain2'
  1353. * Interpolation_n-D: '<S56>/r_cos_M1'
  1354. * Interpolation_n-D: '<S56>/r_sin_M1'
  1355. * Product: '<S55>/Divide1'
  1356. * Product: '<S55>/Divide2'
  1357. * Product: '<S55>/Divide3'
  1358. */
  1359. rtb_MultiportSwitch_idx_0 = (int16_T)((rtb_Divide1_fi *
  1360. rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]) >> 14) + (int16_T)(((int16_T)
  1361. rtb_Gain1 * rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]) >> 14);
  1362. if (rtb_MultiportSwitch_idx_0 > 32767) {
  1363. rtb_MultiportSwitch_idx_0 = 32767;
  1364. } else {
  1365. if (rtb_MultiportSwitch_idx_0 < -32768) {
  1366. rtb_MultiportSwitch_idx_0 = -32768;
  1367. }
  1368. }
  1369. /* SignalConversion generated from: '<S45>/Low_Pass_Filter' incorporates:
  1370. * Sum: '<S55>/Sum1'
  1371. */
  1372. rtb_TmpSignalConversionAtLow_Pa[0] = (int16_T)rtb_MultiportSwitch_idx_0;
  1373. /* Sum: '<S55>/Sum6' incorporates:
  1374. * Gain: '<S53>/Gain2'
  1375. * Interpolation_n-D: '<S56>/r_cos_M1'
  1376. * Interpolation_n-D: '<S56>/r_sin_M1'
  1377. * Product: '<S55>/Divide1'
  1378. * Product: '<S55>/Divide4'
  1379. */
  1380. rtb_Gain1 = (int16_T)(((int16_T)rtb_Gain1 *
  1381. rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]) >> 14) - (int16_T)
  1382. ((rtb_Divide1_fi * rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]) >> 14);
  1383. if (rtb_Gain1 > 32767) {
  1384. rtb_Gain1 = 32767;
  1385. } else {
  1386. if (rtb_Gain1 < -32768) {
  1387. rtb_Gain1 = -32768;
  1388. }
  1389. }
  1390. /* SignalConversion generated from: '<S45>/Low_Pass_Filter' incorporates:
  1391. * Sum: '<S55>/Sum6'
  1392. */
  1393. rtb_TmpSignalConversionAtLow_Pa[1] = (int16_T)rtb_Gain1;
  1394. /* Outputs for Atomic SubSystem: '<S45>/Low_Pass_Filter' */
  1395. /* Constant: '<S45>/Constant' */
  1396. Low_Pass_Filter(rtb_TmpSignalConversionAtLow_Pa, 26214, rtb_DataTypeConversion,
  1397. &rtDW->Low_Pass_Filter_d);
  1398. /* End of Outputs for SubSystem: '<S45>/Low_Pass_Filter' */
  1399. /* Outport: '<Root>/VdPrev' incorporates:
  1400. * UnitDelay: '<S5>/UnitDelay1'
  1401. */
  1402. rtY->VdPrev = rtDW->UnitDelay1_DSTATE;
  1403. /* Abs: '<S46>/Abs5' incorporates:
  1404. * UnitDelay: '<S5>/UnitDelay1'
  1405. */
  1406. if (rtDW->UnitDelay1_DSTATE < 0) {
  1407. rtb_Divide1_fi = (int16_T)-rtDW->UnitDelay1_DSTATE;
  1408. } else {
  1409. rtb_Divide1_fi = rtDW->UnitDelay1_DSTATE;
  1410. }
  1411. /* End of Abs: '<S46>/Abs5' */
  1412. /* PreLookup: '<S46>/Vq_max_XA' */
  1413. rtb_LogicalOperator3 = plook_u16s16_evencka(rtb_Divide1_fi, 0, 64U, 45U);
  1414. /* Interpolation_n-D: '<S46>/iq_maxSca_M1' incorporates:
  1415. * Inport: '<Root>/i_dc_limit'
  1416. * Product: '<S24>/Divide3'
  1417. * Product: '<S46>/Divide4'
  1418. */
  1419. rtb_Gain1 = rtDW->Divide3 << 16;
  1420. rtb_Gain1 = (rtb_Gain1 == MIN_int32_T) && (rtU->i_dc_limit == -1) ?
  1421. MAX_int32_T : rtb_Gain1 / rtU->i_dc_limit;
  1422. if (rtb_Gain1 < 0) {
  1423. rtb_Gain1 = 0;
  1424. } else {
  1425. if (rtb_Gain1 > 65535) {
  1426. rtb_Gain1 = 65535;
  1427. }
  1428. }
  1429. /* Product: '<S46>/Divide1' incorporates:
  1430. * Inport: '<Root>/i_dc_limit'
  1431. * Interpolation_n-D: '<S46>/iq_maxSca_M1'
  1432. * PreLookup: '<S46>/iq_maxSca_XA'
  1433. * Product: '<S46>/Divide4'
  1434. */
  1435. rtb_Divide1_fi = (int16_T)((rtConstP.iq_maxSca_M1_Table[plook_u8u16_evencka
  1436. ((uint16_T)rtb_Gain1, 0U, 1311U, 49U)] * rtU->i_dc_limit) >> 16);
  1437. /* Switch: '<S52>/Switch2' */
  1438. rtb_Switch2_fu = (uint8_T)(rtb_z_ctrlMod != 0);
  1439. /* Delay: '<S84>/Delay' */
  1440. rtb_RelationalOperator4_f = rtDW->Delay_DSTATE_n[0];
  1441. /* DataTypeConversion: '<S52>/Data Type Conversion1' incorporates:
  1442. * Delay: '<S84>/Delay'
  1443. * Logic: '<S52>/Logical Operator'
  1444. * Logic: '<S84>/Logical Operator'
  1445. * UnitDelay: '<S84>/Unit Delay'
  1446. */
  1447. rtb_DataTypeConversion1_c = (uint8_T)((rtb_Switch2_fu != 0) && ((boolean_T)
  1448. (rtDW->UnitDelay_DSTATE_f ^ rtDW->Delay_DSTATE_n[0])));
  1449. /* If: '<S50>/If' incorporates:
  1450. * Constant: '<S77>/Constant1'
  1451. * Constant: '<S77>/Constant11'
  1452. * Constant: '<S77>/Constant2'
  1453. * Constant: '<S77>/Constant4'
  1454. * Gain: '<S46>/Gain1'
  1455. * Product: '<S46>/Divide1'
  1456. * Sum: '<S77>/Add2'
  1457. * Switch: '<S10>/Switch2'
  1458. * Switch: '<S82>/Switch2'
  1459. */
  1460. if ((rtb_DataTypeConversion1_c > 0) && (rtb_z_ctrlMod == 1)) {
  1461. /* Outputs for IfAction SubSystem: '<S50>/speed_mode' incorporates:
  1462. * ActionPort: '<S77>/Action Port'
  1463. */
  1464. /* Switch: '<S79>/Switch2' incorporates:
  1465. * Inport: '<Root>/speed_limit'
  1466. * RelationalOperator: '<S79>/LowerRelop1'
  1467. * RelationalOperator: '<S79>/UpperRelop'
  1468. * Switch: '<S22>/Switch'
  1469. * Switch: '<S79>/Switch'
  1470. * Switch: '<S82>/Switch2'
  1471. */
  1472. if (rtb_Switch_oi > rtU->speed_limit) {
  1473. rtb_Switch_oi = rtU->speed_limit;
  1474. } else {
  1475. if (rtb_Switch_oi < 0) {
  1476. /* Switch: '<S79>/Switch' incorporates:
  1477. * Constant: '<S77>/Constant5'
  1478. * Switch: '<S82>/Switch2'
  1479. */
  1480. rtb_Switch_oi = 0;
  1481. }
  1482. }
  1483. /* End of Switch: '<S79>/Switch2' */
  1484. /* Outputs for Atomic SubSystem: '<S77>/pi_speed' */
  1485. rtb_Switch_oi = pi_speed((int16_T)(rtb_Switch_oi - rtb_Switch2_ip), 3174, 10,
  1486. 20, rtb_Divide1_fi, (int16_T)-rtb_Divide1_fi, 0, rtb_Switch2_fu,
  1487. &rtConstB.pi_speed_g, &rtDW->pi_speed_g, &rtPrevZCX->pi_speed_g);
  1488. /* End of Outputs for SubSystem: '<S77>/pi_speed' */
  1489. /* Merge: '<S50>/Merge' incorporates:
  1490. * Constant: '<S77>/Constant1'
  1491. * Constant: '<S77>/Constant11'
  1492. * Constant: '<S77>/Constant2'
  1493. * Constant: '<S77>/Constant4'
  1494. * Gain: '<S46>/Gain1'
  1495. * Product: '<S46>/Divide1'
  1496. * SignalConversion generated from: '<S77>/iq_target'
  1497. * Sum: '<S77>/Add2'
  1498. * Switch: '<S10>/Switch2'
  1499. * Switch: '<S82>/Switch2'
  1500. */
  1501. rtDW->Merge_b = rtb_Switch_oi;
  1502. /* End of Outputs for SubSystem: '<S50>/speed_mode' */
  1503. } else {
  1504. if ((rtb_DataTypeConversion1_c > 0) && (rtb_z_ctrlMod == 2)) {
  1505. /* Outputs for IfAction SubSystem: '<S50>/torque_mode' incorporates:
  1506. * ActionPort: '<S78>/Action Port'
  1507. */
  1508. /* Product: '<S78>/Divide' incorporates:
  1509. * Constant: '<S78>/Constant2'
  1510. * Sum: '<S78>/Sum2'
  1511. * Switch: '<S10>/Switch2'
  1512. * Switch: '<S22>/Switch'
  1513. */
  1514. rtb_Gain1 = ((int16_T)(rtb_Switch_oi - rtb_Switch2_ip) * 819) >> 6;
  1515. if (rtb_Gain1 > 32767) {
  1516. rtb_Gain1 = 32767;
  1517. } else {
  1518. if (rtb_Gain1 < -32768) {
  1519. rtb_Gain1 = -32768;
  1520. }
  1521. }
  1522. /* Product: '<S78>/Divide1' incorporates:
  1523. * Sum: '<S78>/Sum3'
  1524. * Switch: '<S10>/Switch2'
  1525. * Switch: '<S22>/Switch'
  1526. */
  1527. rtb_MultiportSwitch_idx_0 = ((int16_T)(rtb_Switch2_ip - rtb_Switch_oi) *
  1528. -51) >> 5;
  1529. if (rtb_MultiportSwitch_idx_0 > 32767) {
  1530. rtb_MultiportSwitch_idx_0 = 32767;
  1531. } else {
  1532. if (rtb_MultiportSwitch_idx_0 < -32768) {
  1533. rtb_MultiportSwitch_idx_0 = -32768;
  1534. }
  1535. }
  1536. rtb_Switch_oi = (int16_T)rtb_MultiportSwitch_idx_0;
  1537. /* End of Product: '<S78>/Divide1' */
  1538. /* MinMax: '<S78>/Max' incorporates:
  1539. * Product: '<S78>/Divide'
  1540. * Product: '<S78>/Divide1'
  1541. */
  1542. if ((int16_T)rtb_Gain1 > rtb_Switch_oi) {
  1543. rtb_Max = (int16_T)rtb_Gain1;
  1544. } else {
  1545. rtb_Max = rtb_Switch_oi;
  1546. }
  1547. /* End of MinMax: '<S78>/Max' */
  1548. /* MinMax: '<S78>/Max3' incorporates:
  1549. * MinMax: '<S78>/Max'
  1550. * Product: '<S46>/Divide1'
  1551. * Switch: '<S83>/Switch2'
  1552. */
  1553. if (rtb_Divide1_fi < rtb_Max) {
  1554. rtb_Max = rtb_Divide1_fi;
  1555. }
  1556. /* End of MinMax: '<S78>/Max3' */
  1557. /* Switch: '<S83>/Switch2' incorporates:
  1558. * Product: '<S29>/Divide1'
  1559. * RelationalOperator: '<S83>/LowerRelop1'
  1560. */
  1561. if (rtDW->Divide1 <= rtb_Max) {
  1562. /* MinMax: '<S78>/Max1' incorporates:
  1563. * Product: '<S78>/Divide'
  1564. * Product: '<S78>/Divide1'
  1565. */
  1566. if ((int16_T)rtb_Gain1 < rtb_Switch_oi) {
  1567. rtb_Switch_oi = (int16_T)rtb_Gain1;
  1568. }
  1569. /* End of MinMax: '<S78>/Max1' */
  1570. /* MinMax: '<S78>/Max2' incorporates:
  1571. * Gain: '<S46>/Gain1'
  1572. * MinMax: '<S78>/Max1'
  1573. * Product: '<S46>/Divide1'
  1574. */
  1575. if (rtb_Switch_oi <= (int16_T)-rtb_Divide1_fi) {
  1576. rtb_Switch_oi = (int16_T)-rtb_Divide1_fi;
  1577. }
  1578. /* End of MinMax: '<S78>/Max2' */
  1579. /* Switch: '<S83>/Switch' incorporates:
  1580. * MinMax: '<S78>/Max2'
  1581. * RelationalOperator: '<S83>/UpperRelop'
  1582. */
  1583. if (rtDW->Divide1 < rtb_Switch_oi) {
  1584. rtb_Max = rtb_Switch_oi;
  1585. } else {
  1586. rtb_Max = rtDW->Divide1;
  1587. }
  1588. /* End of Switch: '<S83>/Switch' */
  1589. }
  1590. /* End of Switch: '<S83>/Switch2' */
  1591. /* Merge: '<S50>/Merge' incorporates:
  1592. * SignalConversion generated from: '<S78>/torque_iq'
  1593. * Switch: '<S83>/Switch2'
  1594. */
  1595. rtDW->Merge_b = rtb_Max;
  1596. /* End of Outputs for SubSystem: '<S50>/torque_mode' */
  1597. }
  1598. }
  1599. /* End of If: '<S50>/If' */
  1600. /* If: '<S47>/If' incorporates:
  1601. * Constant: '<S47>/Constant3'
  1602. * Constant: '<S57>/Constant3'
  1603. * Constant: '<S57>/Constant4'
  1604. * Constant: '<S57>/Constant6'
  1605. * Constant: '<S57>/Constant9'
  1606. * Constant: '<S58>/Constant1'
  1607. * Constant: '<S58>/Constant7'
  1608. * Constant: '<S58>/Constant8'
  1609. * Gain: '<S46>/Gain3'
  1610. * Gain: '<S46>/Gain5'
  1611. * If: '<S47>/If1'
  1612. * Inport: '<Root>/vbus_voltage'
  1613. * Interpolation_n-D: '<S46>/Vq_max_M1'
  1614. * Sum: '<S57>/Add'
  1615. * Sum: '<S58>/Add1'
  1616. * Switch: '<S60>/Switch2'
  1617. * Switch: '<S64>/Switch2'
  1618. */
  1619. if (rtb_Switch2_fu == 1) {
  1620. /* Outputs for IfAction SubSystem: '<S47>/iq_ctrl' incorporates:
  1621. * ActionPort: '<S58>/Action Port'
  1622. */
  1623. /* Switch: '<S64>/Switch2' incorporates:
  1624. * Merge: '<S50>/Merge'
  1625. * Product: '<S46>/Divide1'
  1626. * RelationalOperator: '<S64>/LowerRelop1'
  1627. */
  1628. if (rtDW->Merge_b <= rtb_Divide1_fi) {
  1629. /* Switch: '<S64>/Switch' incorporates:
  1630. * Gain: '<S46>/Gain1'
  1631. * RelationalOperator: '<S64>/UpperRelop'
  1632. * Switch: '<S64>/Switch2'
  1633. */
  1634. if (rtDW->Merge_b < (int16_T)-rtb_Divide1_fi) {
  1635. rtb_Divide1_fi = (int16_T)-rtb_Divide1_fi;
  1636. } else {
  1637. rtb_Divide1_fi = rtDW->Merge_b;
  1638. }
  1639. /* End of Switch: '<S64>/Switch' */
  1640. }
  1641. /* End of Switch: '<S64>/Switch2' */
  1642. /* Outputs for Atomic SubSystem: '<S58>/PI_iq' */
  1643. PI_iq((int16_T)(rtb_Divide1_fi - rtb_DataTypeConversion[1]), 4096, 51, 1024,
  1644. rtConstP.Vq_max_M1_Table[rtb_LogicalOperator3], (int16_T)
  1645. -rtConstP.Vq_max_M1_Table[rtb_LogicalOperator3], 0, &rtDW->Switch2_m,
  1646. &rtDW->PI_iq_g);
  1647. /* End of Outputs for SubSystem: '<S58>/PI_iq' */
  1648. /* End of Outputs for SubSystem: '<S47>/iq_ctrl' */
  1649. /* Outputs for IfAction SubSystem: '<S47>/id_ctrl' incorporates:
  1650. * ActionPort: '<S57>/Action Port'
  1651. */
  1652. /* Switch: '<S60>/Switch2' incorporates:
  1653. * Constant: '<S47>/Constant3'
  1654. * Constant: '<S58>/Constant1'
  1655. * Constant: '<S58>/Constant7'
  1656. * Constant: '<S58>/Constant8'
  1657. * Gain: '<S46>/Gain4'
  1658. * Gain: '<S46>/Gain5'
  1659. * Inport: '<Root>/i_dc_limit'
  1660. * Interpolation_n-D: '<S46>/Vq_max_M1'
  1661. * Product: '<S24>/Divide3'
  1662. * RelationalOperator: '<S60>/LowerRelop1'
  1663. * RelationalOperator: '<S60>/UpperRelop'
  1664. * Sum: '<S58>/Add1'
  1665. * Switch: '<S60>/Switch'
  1666. * Switch: '<S64>/Switch2'
  1667. */
  1668. if (rtDW->Divide3 > rtU->i_dc_limit) {
  1669. rtb_Switch_oi = rtU->i_dc_limit;
  1670. } else if (rtDW->Divide3 < (int16_T)-rtU->i_dc_limit) {
  1671. /* Switch: '<S60>/Switch' incorporates:
  1672. * Gain: '<S46>/Gain4'
  1673. * Switch: '<S60>/Switch2'
  1674. */
  1675. rtb_Switch_oi = (int16_T)-rtU->i_dc_limit;
  1676. } else {
  1677. rtb_Switch_oi = rtDW->Divide3;
  1678. }
  1679. /* End of Switch: '<S60>/Switch2' */
  1680. /* Outputs for Atomic SubSystem: '<S57>/PI_id' */
  1681. PI_id((int16_T)(rtb_Switch_oi - rtb_DataTypeConversion[0]), 4096, 51, 1024,
  1682. rtU->vbus_voltage, (int16_T)-rtU->vbus_voltage, 0, &rtDW->Switch2,
  1683. &rtDW->PI_id_b);
  1684. /* End of Outputs for SubSystem: '<S57>/PI_id' */
  1685. /* End of Outputs for SubSystem: '<S47>/id_ctrl' */
  1686. }
  1687. /* End of If: '<S47>/If' */
  1688. /* Switch: '<S5>/Switch1' incorporates:
  1689. * Switch: '<S5>/Switch'
  1690. * Switch: '<S62>/Switch2'
  1691. * Switch: '<S66>/Switch2'
  1692. */
  1693. if (rtb_z_ctrlMod != 0) {
  1694. rtb_Switch_oi = rtDW->Switch2_m;
  1695. rtb_Divide1_fi = rtDW->Switch2;
  1696. } else {
  1697. rtb_Switch_oi = rtDW->Merge[1];
  1698. rtb_Divide1_fi = rtDW->Merge[0];
  1699. }
  1700. /* End of Switch: '<S5>/Switch1' */
  1701. /* Sum: '<S48>/Sum1' incorporates:
  1702. * Interpolation_n-D: '<S56>/r_cos_M1'
  1703. * Product: '<S48>/Divide2'
  1704. * Product: '<S48>/Divide3'
  1705. * Product: '<S67>/Divide4'
  1706. * Switch: '<S5>/Switch'
  1707. * Switch: '<S5>/Switch1'
  1708. */
  1709. rtb_Gain1 = (int16_T)((rtb_Divide1_fi * rtb_Abs5_h) >> 14) + (int16_T)
  1710. ((rtb_Switch_oi * rtb_Sign) >> 14);
  1711. if (rtb_Gain1 > 32767) {
  1712. rtb_Gain1 = 32767;
  1713. } else {
  1714. if (rtb_Gain1 < -32768) {
  1715. rtb_Gain1 = -32768;
  1716. }
  1717. }
  1718. /* Sum: '<S48>/Sum6' incorporates:
  1719. * Interpolation_n-D: '<S56>/r_cos_M1'
  1720. * Product: '<S48>/Divide1'
  1721. * Product: '<S48>/Divide4'
  1722. * Product: '<S67>/Divide4'
  1723. * Switch: '<S5>/Switch'
  1724. * Switch: '<S5>/Switch1'
  1725. */
  1726. rtb_MultiportSwitch_idx_0 = (int16_T)((rtb_Divide1_fi * rtb_Sign) >> 14) -
  1727. (int16_T)((rtb_Switch_oi * rtb_Abs5_h) >> 14);
  1728. if (rtb_MultiportSwitch_idx_0 > 32767) {
  1729. rtb_MultiportSwitch_idx_0 = 32767;
  1730. } else {
  1731. if (rtb_MultiportSwitch_idx_0 < -32768) {
  1732. rtb_MultiportSwitch_idx_0 = -32768;
  1733. }
  1734. }
  1735. /* Product: '<S67>/Divide3' incorporates:
  1736. * Constant: '<S67>/Constant1'
  1737. * Product: '<S67>/Divide'
  1738. * Sum: '<S48>/Sum6'
  1739. */
  1740. rtb_Sign = (int16_T)((3547 * (int16_T)rtb_MultiportSwitch_idx_0) >> 12);
  1741. /* Product: '<S67>/Divide2' incorporates:
  1742. * Constant: '<S67>/Constant'
  1743. * Sum: '<S48>/Sum1'
  1744. */
  1745. rtb_Max = (int16_T)((3547 * (int16_T)rtb_Gain1) >> 12);
  1746. /* Product: '<S67>/Divide4' incorporates:
  1747. * Constant: '<S67>/Constant2'
  1748. * Product: '<S67>/Divide2'
  1749. */
  1750. rtb_Abs5_h = (int16_T)((2365 * rtb_Max) >> 12);
  1751. /* Sum: '<S67>/Add' incorporates:
  1752. * Product: '<S67>/Divide'
  1753. * Product: '<S67>/Divide4'
  1754. */
  1755. rtb_Gain4 = (int16_T)((rtb_Sign + rtb_Abs5_h) >> 1);
  1756. /* Sum: '<S67>/Add1' incorporates:
  1757. * Product: '<S67>/Divide'
  1758. * Product: '<S67>/Divide4'
  1759. */
  1760. rtb_Abs5_h = (int16_T)((rtb_Abs5_h - rtb_Sign) >> 1);
  1761. /* Product: '<S67>/Divide7' incorporates:
  1762. * Constant: '<S67>/Constant3'
  1763. * Sum: '<S48>/Sum1'
  1764. */
  1765. rtb_Sign = (int16_T)((2365 * (int16_T)rtb_Gain1) >> 12);
  1766. /* MATLAB Function: '<S67>/sector_select' incorporates:
  1767. * Product: '<S67>/Divide7'
  1768. * Sum: '<S48>/Sum1'
  1769. * Sum: '<S48>/Sum6'
  1770. */
  1771. if ((int16_T)rtb_Gain1 >= 0) {
  1772. if ((int16_T)rtb_MultiportSwitch_idx_0 >= 0) {
  1773. if (rtb_Sign > (int16_T)rtb_MultiportSwitch_idx_0) {
  1774. /* DataTypeConversion: '<S67>/Data Type Conversion' */
  1775. rtb_DataTypeConversion1_c = 2U;
  1776. } else {
  1777. /* DataTypeConversion: '<S67>/Data Type Conversion' */
  1778. rtb_DataTypeConversion1_c = 1U;
  1779. }
  1780. } else if (-rtb_Sign > (int16_T)rtb_MultiportSwitch_idx_0) {
  1781. /* DataTypeConversion: '<S67>/Data Type Conversion' */
  1782. rtb_DataTypeConversion1_c = 3U;
  1783. } else {
  1784. /* DataTypeConversion: '<S67>/Data Type Conversion' */
  1785. rtb_DataTypeConversion1_c = 2U;
  1786. }
  1787. } else if ((int16_T)rtb_MultiportSwitch_idx_0 >= 0) {
  1788. if (-rtb_Sign > (int16_T)rtb_MultiportSwitch_idx_0) {
  1789. /* DataTypeConversion: '<S67>/Data Type Conversion' */
  1790. rtb_DataTypeConversion1_c = 5U;
  1791. } else {
  1792. /* DataTypeConversion: '<S67>/Data Type Conversion' */
  1793. rtb_DataTypeConversion1_c = 6U;
  1794. }
  1795. } else if (rtb_Sign > (int16_T)rtb_MultiportSwitch_idx_0) {
  1796. /* DataTypeConversion: '<S67>/Data Type Conversion' */
  1797. rtb_DataTypeConversion1_c = 4U;
  1798. } else {
  1799. /* DataTypeConversion: '<S67>/Data Type Conversion' */
  1800. rtb_DataTypeConversion1_c = 5U;
  1801. }
  1802. /* End of MATLAB Function: '<S67>/sector_select' */
  1803. /* Product: '<S67>/Divide' incorporates:
  1804. * Inport: '<Root>/vbus_voltage'
  1805. */
  1806. rtb_Sign = (int16_T)(24576000 / rtU->vbus_voltage);
  1807. /* Product: '<S67>/Divide1' incorporates:
  1808. * Product: '<S67>/Divide'
  1809. * Product: '<S67>/Divide2'
  1810. * Product: '<S67>/Divide8'
  1811. */
  1812. rtb_Max = (int16_T)((((2365 * rtb_Max) >> 13) * rtb_Sign) >> 10);
  1813. /* Product: '<S67>/Divide5' incorporates:
  1814. * Product: '<S67>/Divide'
  1815. * Sum: '<S67>/Add'
  1816. */
  1817. rtb_Gain4 = (int16_T)((rtb_Gain4 * rtb_Sign) >> 11);
  1818. /* Product: '<S67>/Divide6' incorporates:
  1819. * Product: '<S67>/Divide'
  1820. * Sum: '<S67>/Add1'
  1821. */
  1822. rtb_Abs5_h = (int16_T)((rtb_Abs5_h * rtb_Sign) >> 11);
  1823. /* MultiPortSwitch: '<S69>/Multiport Switch' incorporates:
  1824. * DataTypeConversion: '<S67>/Data Type Conversion1'
  1825. * Gain: '<S71>/Gain'
  1826. * Gain: '<S74>/Gain'
  1827. * Gain: '<S75>/Gain1'
  1828. * Product: '<S71>/Divide2'
  1829. * Product: '<S72>/Divide2'
  1830. * Product: '<S73>/Divide2'
  1831. * Product: '<S74>/Divide2'
  1832. * Product: '<S75>/Divide2'
  1833. * Product: '<S76>/Divide2'
  1834. * Sum: '<S71>/Add3'
  1835. * Sum: '<S72>/Add3'
  1836. * Sum: '<S73>/Add3'
  1837. * Sum: '<S74>/Add3'
  1838. * Sum: '<S75>/Add3'
  1839. * Sum: '<S76>/Add3'
  1840. */
  1841. switch (rtb_DataTypeConversion1_c) {
  1842. case 1:
  1843. /* Product: '<S71>/Divide' incorporates:
  1844. * Gain: '<S71>/Gain'
  1845. * Sum: '<S71>/Add'
  1846. * Sum: '<S71>/Add1'
  1847. */
  1848. rtb_Gain1 = (6000 - (rtb_Max - rtb_Abs5_h)) >> 2;
  1849. /* Sum: '<S71>/Add2' incorporates:
  1850. * Product: '<S71>/Divide1'
  1851. */
  1852. rtb_Divide = (rtb_Max >> 1) + rtb_Gain1;
  1853. rtb_MultiportSwitch_idx_0 = (-rtb_Abs5_h >> 1) + rtb_Divide;
  1854. rtb_MultiportSwitch_idx_1 = rtb_Divide;
  1855. break;
  1856. case 2:
  1857. /* Product: '<S72>/Divide' incorporates:
  1858. * Sum: '<S72>/Add'
  1859. * Sum: '<S72>/Add1'
  1860. */
  1861. rtb_Sign = (int16_T)((int16_T)(6000 - (int16_T)(rtb_Abs5_h + rtb_Gain4)) >>
  1862. 2);
  1863. /* Sum: '<S72>/Add2' incorporates:
  1864. * Product: '<S72>/Divide1'
  1865. */
  1866. rtb_Max = (int16_T)((rtb_Gain4 >> 1) + rtb_Sign);
  1867. rtb_MultiportSwitch_idx_0 = rtb_Max;
  1868. rtb_MultiportSwitch_idx_1 = (int16_T)((rtb_Abs5_h >> 1) + rtb_Max);
  1869. rtb_Gain1 = rtb_Sign;
  1870. break;
  1871. case 3:
  1872. /* Product: '<S73>/Divide' incorporates:
  1873. * Gain: '<S73>/Gain'
  1874. * Sum: '<S73>/Add'
  1875. * Sum: '<S73>/Add1'
  1876. */
  1877. rtb_Divide = (6000 - (rtb_Max - rtb_Gain4)) >> 2;
  1878. /* Sum: '<S73>/Add2' incorporates:
  1879. * Gain: '<S73>/Gain'
  1880. * Product: '<S73>/Divide1'
  1881. */
  1882. rtb_Gain1 = (-rtb_Gain4 >> 1) + rtb_Divide;
  1883. rtb_MultiportSwitch_idx_0 = rtb_Divide;
  1884. rtb_MultiportSwitch_idx_1 = (rtb_Max >> 1) + rtb_Gain1;
  1885. break;
  1886. case 4:
  1887. /* Product: '<S74>/Divide' incorporates:
  1888. * Gain: '<S74>/Gain'
  1889. * Sum: '<S74>/Add'
  1890. * Sum: '<S74>/Add1'
  1891. */
  1892. rtb_Gain1 = (6000 - (rtb_Abs5_h - rtb_Max)) >> 2;
  1893. /* Sum: '<S74>/Add2' incorporates:
  1894. * Product: '<S74>/Divide1'
  1895. */
  1896. rtb_Divide = (rtb_Abs5_h >> 1) + rtb_Gain1;
  1897. rtb_MultiportSwitch_idx_0 = rtb_Gain1;
  1898. rtb_MultiportSwitch_idx_1 = rtb_Divide;
  1899. rtb_Gain1 = (-rtb_Max >> 1) + rtb_Divide;
  1900. break;
  1901. case 5:
  1902. /* Product: '<S75>/Divide' incorporates:
  1903. * Gain: '<S75>/Gain'
  1904. * Gain: '<S75>/Gain1'
  1905. * Sum: '<S75>/Add1'
  1906. */
  1907. rtb_Gain1 = (6000 - (-rtb_Abs5_h - rtb_Gain4)) >> 2;
  1908. /* Sum: '<S75>/Add2' incorporates:
  1909. * Gain: '<S75>/Gain'
  1910. * Product: '<S75>/Divide1'
  1911. */
  1912. rtb_Divide = (-rtb_Abs5_h >> 1) + rtb_Gain1;
  1913. rtb_MultiportSwitch_idx_0 = rtb_Divide;
  1914. rtb_MultiportSwitch_idx_1 = rtb_Gain1;
  1915. rtb_Gain1 = (-rtb_Gain4 >> 1) + rtb_Divide;
  1916. break;
  1917. default:
  1918. /* Product: '<S76>/Divide' incorporates:
  1919. * Gain: '<S76>/Gain1'
  1920. * Sum: '<S76>/Add'
  1921. * Sum: '<S76>/Add1'
  1922. */
  1923. rtb_Divide = (6000 - (rtb_Gain4 - rtb_Max)) >> 2;
  1924. /* Sum: '<S76>/Add2' incorporates:
  1925. * Gain: '<S76>/Gain1'
  1926. * Product: '<S76>/Divide1'
  1927. */
  1928. rtb_Gain1 = (-rtb_Max >> 1) + rtb_Divide;
  1929. rtb_MultiportSwitch_idx_0 = (rtb_Gain4 >> 1) + rtb_Gain1;
  1930. rtb_MultiportSwitch_idx_1 = rtb_Divide;
  1931. break;
  1932. }
  1933. /* End of MultiPortSwitch: '<S69>/Multiport Switch' */
  1934. /* Update for Delay: '<S7>/Delay' incorporates:
  1935. * Inport: '<Root>/hall_a'
  1936. */
  1937. rtDW->Delay_DSTATE = rtU->hall_a;
  1938. /* Update for Delay: '<S7>/Delay1' incorporates:
  1939. * Inport: '<Root>/hall_b'
  1940. */
  1941. rtDW->Delay1_DSTATE = rtU->hall_b;
  1942. /* Update for Delay: '<S7>/Delay2' incorporates:
  1943. * Inport: '<Root>/hall_c'
  1944. */
  1945. rtDW->Delay2_DSTATE = rtU->hall_c;
  1946. /* Update for UnitDelay: '<S10>/UnitDelay3' incorporates:
  1947. * Inport: '<Root>/hw_count'
  1948. */
  1949. rtDW->UnitDelay3_DSTATE = rtU->hw_count;
  1950. /* Update for UnitDelay: '<S10>/UnitDelay4' incorporates:
  1951. * Abs: '<S10>/Abs5'
  1952. */
  1953. rtDW->UnitDelay4_DSTATE = rtb_Abs5;
  1954. /* Update for UnitDelay: '<S34>/UnitDelay' */
  1955. rtDW->UnitDelay_DSTATE_j = rtb_UnitDelay;
  1956. /* Update for UnitDelay: '<S5>/UnitDelay2' incorporates:
  1957. * Switch: '<S5>/Switch1'
  1958. */
  1959. rtDW->UnitDelay2_DSTATE_p = rtb_Switch_oi;
  1960. /* Update for UnitDelay: '<S5>/UnitDelay1' incorporates:
  1961. * Switch: '<S5>/Switch'
  1962. */
  1963. rtDW->UnitDelay1_DSTATE = rtb_Divide1_fi;
  1964. /* Update for UnitDelay: '<S84>/Unit Delay' incorporates:
  1965. * Delay: '<S84>/Delay'
  1966. */
  1967. rtDW->UnitDelay_DSTATE_f = rtDW->Delay_DSTATE_n[0];
  1968. /* Update for Delay: '<S84>/Delay' incorporates:
  1969. * Logic: '<S84>/Logical Operator1'
  1970. */
  1971. for (rtb_Divide = 0; rtb_Divide < 19; rtb_Divide++) {
  1972. rtDW->Delay_DSTATE_n[rtb_Divide] = rtDW->Delay_DSTATE_n[rtb_Divide + 1];
  1973. }
  1974. rtDW->Delay_DSTATE_n[19] = !rtb_RelationalOperator4_f;
  1975. /* End of Update for Delay: '<S84>/Delay' */
  1976. /* Switch: '<S68>/Switch2' incorporates:
  1977. * RelationalOperator: '<S68>/LowerRelop1'
  1978. * RelationalOperator: '<S68>/UpperRelop'
  1979. * Switch: '<S68>/Switch'
  1980. */
  1981. if (rtb_MultiportSwitch_idx_0 > 3000) {
  1982. /* Outport: '<Root>/PWM' incorporates:
  1983. * Constant: '<S67>/Constant6'
  1984. */
  1985. rtY->PWM[0] = 3000U;
  1986. } else if (rtb_MultiportSwitch_idx_0 < 0) {
  1987. /* Switch: '<S68>/Switch' incorporates:
  1988. * Constant: '<S67>/Constant5'
  1989. * Outport: '<Root>/PWM'
  1990. */
  1991. rtY->PWM[0] = 0U;
  1992. } else {
  1993. /* Outport: '<Root>/PWM' */
  1994. rtY->PWM[0] = (uint16_T)rtb_MultiportSwitch_idx_0;
  1995. }
  1996. if (rtb_MultiportSwitch_idx_1 > 3000) {
  1997. /* Outport: '<Root>/PWM' incorporates:
  1998. * Constant: '<S67>/Constant6'
  1999. */
  2000. rtY->PWM[1] = 3000U;
  2001. } else if (rtb_MultiportSwitch_idx_1 < 0) {
  2002. /* Switch: '<S68>/Switch' incorporates:
  2003. * Constant: '<S67>/Constant5'
  2004. * Outport: '<Root>/PWM'
  2005. */
  2006. rtY->PWM[1] = 0U;
  2007. } else {
  2008. /* Outport: '<Root>/PWM' */
  2009. rtY->PWM[1] = (uint16_T)rtb_MultiportSwitch_idx_1;
  2010. }
  2011. if (rtb_Gain1 > 3000) {
  2012. /* Outport: '<Root>/PWM' incorporates:
  2013. * Constant: '<S67>/Constant6'
  2014. */
  2015. rtY->PWM[2] = 3000U;
  2016. } else if (rtb_Gain1 < 0) {
  2017. /* Switch: '<S68>/Switch' incorporates:
  2018. * Constant: '<S67>/Constant5'
  2019. * Outport: '<Root>/PWM'
  2020. */
  2021. rtY->PWM[2] = 0U;
  2022. } else {
  2023. /* Outport: '<Root>/PWM' */
  2024. rtY->PWM[2] = (uint16_T)rtb_Gain1;
  2025. }
  2026. /* End of Switch: '<S68>/Switch2' */
  2027. /* End of Outputs for SubSystem: '<Root>/PMSM_Controller' */
  2028. /* Outport: '<Root>/sector' */
  2029. rtY->sector = rtb_DataTypeConversion1_c;
  2030. /* Outport: '<Root>/n_MotError' */
  2031. rtY->n_MotError = rtb_UnitDelay;
  2032. /* Outport: '<Root>/iq' */
  2033. rtY->iq = rtb_DataTypeConversion[1];
  2034. /* Outport: '<Root>/id' */
  2035. rtY->id = rtb_DataTypeConversion[0];
  2036. /* Outport: '<Root>/angle' incorporates:
  2037. * Switch: '<S2>/Switch'
  2038. */
  2039. rtY->angle = rtb_Switch_b;
  2040. /* Outport: '<Root>/rpm' incorporates:
  2041. * Switch: '<S10>/Switch2'
  2042. */
  2043. rtY->rpm = rtb_Switch2_ip;
  2044. /* Outport: '<Root>/hall_angle' incorporates:
  2045. * Merge: '<S11>/Merge'
  2046. */
  2047. rtY->hall_angle = rtb_Switch3_c;
  2048. /* Outport: '<Root>/hall_state' */
  2049. rtY->hall_state = rtb_Add_cr;
  2050. /* Outport: '<Root>/running_mode' */
  2051. rtY->running_mode = rtb_z_ctrlMod;
  2052. }
  2053. /* Model initialize function */
  2054. void PMSM_Controller_initialize(RT_MODEL *const rtM)
  2055. {
  2056. DW *rtDW = rtM->dwork;
  2057. PrevZCX *rtPrevZCX = rtM->prevZCSigState;
  2058. {
  2059. int32_T i;
  2060. rtPrevZCX->pi_speed_g.ResettableDelay_Reset_ZCE = POS_ZCSIG;
  2061. /* SystemInitialize for Atomic SubSystem: '<Root>/PMSM_Controller' */
  2062. /* InitializeConditions for Delay: '<S84>/Delay' */
  2063. for (i = 0; i < 20; i++) {
  2064. rtDW->Delay_DSTATE_n[i] = true;
  2065. }
  2066. /* End of InitializeConditions for Delay: '<S84>/Delay' */
  2067. /* SystemInitialize for IfAction SubSystem: '<S10>/Raw_Motor_Speed_Estimation' */
  2068. /* SystemInitialize for Outport: '<S15>/z_counter' incorporates:
  2069. * Inport: '<S15>/z_counterRawPrev'
  2070. */
  2071. rtDW->z_counterRawPrev = 200000U;
  2072. /* End of SystemInitialize for SubSystem: '<S10>/Raw_Motor_Speed_Estimation' */
  2073. /* SystemInitialize for Atomic SubSystem: '<S34>/Debounce_Filter' */
  2074. Debounce_Filter_Init(&rtDW->Debounce_Filter_i);
  2075. /* End of SystemInitialize for SubSystem: '<S34>/Debounce_Filter' */
  2076. /* SystemInitialize for IfAction SubSystem: '<S50>/speed_mode' */
  2077. /* SystemInitialize for Atomic SubSystem: '<S77>/pi_speed' */
  2078. pi_speed_Init(&rtDW->pi_speed_g);
  2079. /* End of SystemInitialize for SubSystem: '<S77>/pi_speed' */
  2080. /* End of SystemInitialize for SubSystem: '<S50>/speed_mode' */
  2081. /* End of SystemInitialize for SubSystem: '<Root>/PMSM_Controller' */
  2082. }
  2083. }
  2084. /*
  2085. * File trailer for generated code.
  2086. *
  2087. * [EOF]
  2088. */