PMSM_Controller.c 78 KB

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