PMSM_Controller.c 72 KB

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