PMSM_Controller.c 71 KB

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