PMSM_Controller.c 70 KB

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