| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489 |
- /*
- * File: PMSM_Controller.c
- *
- * Code generated for Simulink model 'PMSM_Controller'.
- *
- * Model version : 1.1200
- * Simulink Coder version : 9.4 (R2020b) 29-Jul-2020
- * C/C++ source code generated on : Mon Apr 4 09:18:28 2022
- *
- * Target selection: ert.tlc
- * Embedded hardware selection: ARM Compatible->ARM Cortex-M
- * Code generation objectives:
- * 1. Execution efficiency
- * 2. RAM efficiency
- * Validation result: Not run
- */
- #include "PMSM_Controller.h"
- #include "PMSM_Controller_private.h"
- /* Named constants for Chart: '<S3>/Control_Mode_Manager' */
- #define IN_ACTIVE ((uint8_T)1U)
- #define IN_NO_ACTIVE_CHILD ((uint8_T)0U)
- #define IN_OPEN ((uint8_T)2U)
- #define IN_SPEED_MODE ((uint8_T)1U)
- #define IN_TORQUE_MODE ((uint8_T)2U)
- #define OPEN_MODE ((uint8_T)0U)
- #define SPD_MODE ((uint8_T)1U)
- #define TRQ_MODE ((uint8_T)2U)
- uint16_T plook_u16s16_evencka(int16_T u, int16_T bp0, uint16_T bpSpace, uint32_T
- maxIndex)
- {
- uint16_T bpIndex;
- /* Prelookup - Index only
- Index Search method: 'even'
- Extrapolation method: 'Clip'
- Use previous index: 'off'
- Use last breakpoint for index at or above upper limit: 'on'
- Remove protection against out-of-range input in generated code: 'off'
- */
- if (u <= bp0) {
- bpIndex = 0U;
- } else {
- bpIndex = (uint16_T)((uint32_T)(uint16_T)(u - bp0) / bpSpace);
- if (bpIndex < maxIndex) {
- } else {
- bpIndex = (uint16_T)maxIndex;
- }
- }
- return bpIndex;
- }
- uint8_T plook_u8u16_evencka(uint16_T u, uint16_T bp0, uint16_T bpSpace, uint32_T
- maxIndex)
- {
- uint16_T fbpIndex;
- uint8_T bpIndex;
- /* Prelookup - Index only
- Index Search method: 'even'
- Extrapolation method: 'Clip'
- Use previous index: 'off'
- Use last breakpoint for index at or above upper limit: 'on'
- Remove protection against out-of-range input in generated code: 'off'
- */
- if (u <= bp0) {
- bpIndex = 0U;
- } else {
- fbpIndex = (uint16_T)((uint32_T)(uint16_T)((uint32_T)u - bp0) / bpSpace);
- if (fbpIndex < maxIndex) {
- bpIndex = (uint8_T)fbpIndex;
- } else {
- bpIndex = (uint8_T)maxIndex;
- }
- }
- return bpIndex;
- }
- int32_T div_nde_s32_floor(int32_T numerator, int32_T denominator)
- {
- return (((numerator < 0) != (denominator < 0)) && (numerator % denominator !=
- 0) ? -1 : 0) + numerator / denominator;
- }
- /*
- * System initialize for atomic system:
- * '<S39>/Counter'
- * '<S38>/Counter'
- */
- void Counter_Init(DW_Counter *localDW, uint16_T rtp_z_cntInit)
- {
- /* InitializeConditions for UnitDelay: '<S44>/UnitDelay' */
- localDW->UnitDelay_DSTATE = rtp_z_cntInit;
- }
- /*
- * Output and update for atomic system:
- * '<S39>/Counter'
- * '<S38>/Counter'
- */
- uint16_T Counter(uint16_T rtu_inc, uint16_T rtu_max, boolean_T rtu_rst,
- DW_Counter *localDW)
- {
- uint16_T rty_cnt_0;
- uint16_T rtu_rst_0;
- /* Switch: '<S44>/Switch1' incorporates:
- * Constant: '<S44>/Constant23'
- * UnitDelay: '<S44>/UnitDelay'
- */
- if (rtu_rst) {
- rtu_rst_0 = 0U;
- } else {
- rtu_rst_0 = localDW->UnitDelay_DSTATE;
- }
- /* End of Switch: '<S44>/Switch1' */
- /* Sum: '<S43>/Sum1' */
- rty_cnt_0 = (uint16_T)((uint32_T)rtu_inc + rtu_rst_0);
- /* MinMax: '<S43>/MinMax' */
- if (rty_cnt_0 < rtu_max) {
- /* Update for UnitDelay: '<S44>/UnitDelay' */
- localDW->UnitDelay_DSTATE = rty_cnt_0;
- } else {
- /* Update for UnitDelay: '<S44>/UnitDelay' */
- localDW->UnitDelay_DSTATE = rtu_max;
- }
- /* End of MinMax: '<S43>/MinMax' */
- return rty_cnt_0;
- }
- /*
- * Output and update for atomic system:
- * '<S35>/either_edge'
- * '<S34>/either_edge'
- */
- boolean_T either_edge(boolean_T rtu_u, DW_either_edge *localDW)
- {
- boolean_T rty_y_0;
- /* RelationalOperator: '<S40>/Relational Operator' incorporates:
- * UnitDelay: '<S40>/UnitDelay'
- */
- rty_y_0 = (rtu_u != localDW->UnitDelay_DSTATE);
- /* Update for UnitDelay: '<S40>/UnitDelay' */
- localDW->UnitDelay_DSTATE = rtu_u;
- return rty_y_0;
- }
- /* System initialize for atomic system: '<S34>/Debounce_Filter' */
- void Debounce_Filter_Init(DW_Debounce_Filter *localDW)
- {
- /* SystemInitialize for IfAction SubSystem: '<S35>/Qualification' */
- /* SystemInitialize for Atomic SubSystem: '<S39>/Counter' */
- Counter_Init(&localDW->Counter_f, 0);
- /* End of SystemInitialize for SubSystem: '<S39>/Counter' */
- /* End of SystemInitialize for SubSystem: '<S35>/Qualification' */
- /* SystemInitialize for IfAction SubSystem: '<S35>/Dequalification' */
- /* SystemInitialize for Atomic SubSystem: '<S38>/Counter' */
- Counter_Init(&localDW->Counter_d, 0);
- /* End of SystemInitialize for SubSystem: '<S38>/Counter' */
- /* End of SystemInitialize for SubSystem: '<S35>/Dequalification' */
- }
- /* Output and update for atomic system: '<S34>/Debounce_Filter' */
- void Debounce_Filter(boolean_T rtu_u, uint16_T rtu_tAcv, uint16_T rtu_tDeacv,
- boolean_T *rty_y, DW_Debounce_Filter *localDW)
- {
- uint16_T rtb_Sum1_n;
- boolean_T rtb_RelationalOperator_e;
- /* Outputs for Atomic SubSystem: '<S35>/either_edge' */
- rtb_RelationalOperator_e = either_edge(rtu_u, &localDW->either_edge_j);
- /* End of Outputs for SubSystem: '<S35>/either_edge' */
- /* If: '<S35>/If2' incorporates:
- * Constant: '<S38>/Constant6'
- * Constant: '<S39>/Constant6'
- * Inport: '<S37>/yPrev'
- * Logic: '<S35>/Logical Operator1'
- * Logic: '<S35>/Logical Operator2'
- * Logic: '<S35>/Logical Operator3'
- * Logic: '<S35>/Logical Operator4'
- * UnitDelay: '<S35>/UnitDelay'
- */
- if (rtu_u && (!localDW->UnitDelay_DSTATE)) {
- /* Outputs for IfAction SubSystem: '<S35>/Qualification' incorporates:
- * ActionPort: '<S39>/Action Port'
- */
- /* Outputs for Atomic SubSystem: '<S39>/Counter' */
- rtb_Sum1_n = Counter(1, rtu_tAcv, rtb_RelationalOperator_e,
- &localDW->Counter_f);
- /* End of Outputs for SubSystem: '<S39>/Counter' */
- /* Switch: '<S39>/Switch2' incorporates:
- * Constant: '<S39>/Constant6'
- * RelationalOperator: '<S39>/Relational Operator2'
- */
- *rty_y = ((rtb_Sum1_n > rtu_tAcv) || localDW->UnitDelay_DSTATE);
- /* End of Outputs for SubSystem: '<S35>/Qualification' */
- } else if ((!rtu_u) && localDW->UnitDelay_DSTATE) {
- /* Outputs for IfAction SubSystem: '<S35>/Dequalification' incorporates:
- * ActionPort: '<S38>/Action Port'
- */
- /* Outputs for Atomic SubSystem: '<S38>/Counter' */
- rtb_Sum1_n = Counter(1, rtu_tDeacv, rtb_RelationalOperator_e,
- &localDW->Counter_d);
- /* End of Outputs for SubSystem: '<S38>/Counter' */
- /* Switch: '<S38>/Switch2' incorporates:
- * Constant: '<S38>/Constant6'
- * RelationalOperator: '<S38>/Relational Operator2'
- */
- *rty_y = ((rtb_Sum1_n <= rtu_tDeacv) && localDW->UnitDelay_DSTATE);
- /* End of Outputs for SubSystem: '<S35>/Dequalification' */
- } else {
- /* Outputs for IfAction SubSystem: '<S35>/Default' incorporates:
- * ActionPort: '<S37>/Action Port'
- */
- *rty_y = localDW->UnitDelay_DSTATE;
- /* End of Outputs for SubSystem: '<S35>/Default' */
- }
- /* End of If: '<S35>/If2' */
- /* Update for UnitDelay: '<S35>/UnitDelay' */
- localDW->UnitDelay_DSTATE = *rty_y;
- }
- /* Output and update for atomic system: '<S45>/Low_Pass_Filter' */
- void Low_Pass_Filter(const int16_T rtu_u[2], uint16_T rtu_coef, int16_T rty_y[2],
- DW_Low_Pass_Filter *localDW)
- {
- int32_T tmp;
- /* Sum: '<S54>/Sum2' incorporates:
- * UnitDelay: '<S54>/UnitDelay1'
- */
- tmp = rtu_u[0] - localDW->UnitDelay1_DSTATE[0];
- if (tmp > 32767) {
- tmp = 32767;
- } else {
- if (tmp < -32768) {
- tmp = -32768;
- }
- }
- /* Product: '<S54>/Divide3' incorporates:
- * Sum: '<S54>/Sum2'
- */
- rty_y[0] = (int16_T)((rtu_coef * tmp) >> 16);
- /* Sum: '<S54>/Sum3' incorporates:
- * UnitDelay: '<S54>/UnitDelay1'
- */
- rty_y[0] += localDW->UnitDelay1_DSTATE[0];
- /* Update for UnitDelay: '<S54>/UnitDelay1' incorporates:
- * Sum: '<S54>/Sum3'
- */
- localDW->UnitDelay1_DSTATE[0] = rty_y[0];
- /* Sum: '<S54>/Sum2' incorporates:
- * UnitDelay: '<S54>/UnitDelay1'
- */
- tmp = rtu_u[1] - localDW->UnitDelay1_DSTATE[1];
- if (tmp > 32767) {
- tmp = 32767;
- } else {
- if (tmp < -32768) {
- tmp = -32768;
- }
- }
- /* Product: '<S54>/Divide3' incorporates:
- * Sum: '<S54>/Sum2'
- */
- rty_y[1] = (int16_T)((rtu_coef * tmp) >> 16);
- /* Sum: '<S54>/Sum3' incorporates:
- * UnitDelay: '<S54>/UnitDelay1'
- */
- rty_y[1] += localDW->UnitDelay1_DSTATE[1];
- /* Update for UnitDelay: '<S54>/UnitDelay1' incorporates:
- * Sum: '<S54>/Sum3'
- */
- localDW->UnitDelay1_DSTATE[1] = rty_y[1];
- }
- /* Output and update for atomic system: '<S58>/PI_iq' */
- void PI_iq(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb,
- int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_ext_limProt,
- int16_T *rty_pi_out, DW_PI_iq *localDW)
- {
- int64_T tmp;
- int32_T rtb_Divide4_k;
- int32_T rtb_Sum1_hy;
- /* Product: '<S63>/Divide4' */
- rtb_Divide4_k = (rtu_err * rtu_P) >> 6;
- /* Product: '<S63>/Divide1' incorporates:
- * Product: '<S63>/Divide4'
- */
- tmp = ((int64_T)rtb_Divide4_k * rtu_I) >> 10;
- if (tmp > 2147483647LL) {
- tmp = 2147483647LL;
- } else {
- if (tmp < -2147483648LL) {
- tmp = -2147483648LL;
- }
- }
- /* Sum: '<S63>/Sum2' incorporates:
- * Product: '<S63>/Divide1'
- * UnitDelay: '<S63>/UnitDelay'
- */
- tmp = (((int64_T)rtu_ext_limProt << 4) + (int32_T)tmp) +
- localDW->UnitDelay_DSTATE;
- if (tmp > 2147483647LL) {
- tmp = 2147483647LL;
- } else {
- if (tmp < -2147483648LL) {
- tmp = -2147483648LL;
- }
- }
- /* Sum: '<S65>/Sum1' incorporates:
- * Sum: '<S63>/Sum2'
- * UnitDelay: '<S65>/UnitDelay'
- */
- rtb_Sum1_hy = (int32_T)tmp + localDW->UnitDelay_DSTATE_i;
- /* Sum: '<S63>/Sum6' incorporates:
- * Product: '<S63>/Divide4'
- * Sum: '<S65>/Sum1'
- */
- tmp = (int64_T)rtb_Divide4_k + rtb_Sum1_hy;
- if (tmp > 2147483647LL) {
- tmp = 2147483647LL;
- } else {
- if (tmp < -2147483648LL) {
- tmp = -2147483648LL;
- }
- }
- /* Switch: '<S66>/Switch2' incorporates:
- * RelationalOperator: '<S66>/LowerRelop1'
- * RelationalOperator: '<S66>/UpperRelop'
- * Sum: '<S63>/Sum6'
- * Switch: '<S66>/Switch'
- */
- if ((int32_T)tmp > (rtu_satMax << 4)) {
- *rty_pi_out = rtu_satMax;
- } else if ((int32_T)tmp < (rtu_satMin << 4)) {
- /* Switch: '<S66>/Switch' */
- *rty_pi_out = rtu_satMin;
- } else {
- *rty_pi_out = (int16_T)((int32_T)tmp >> 4);
- }
- /* End of Switch: '<S66>/Switch2' */
- /* Update for UnitDelay: '<S63>/UnitDelay' incorporates:
- * Product: '<S63>/Divide2'
- * Sum: '<S63>/Sum3'
- * Sum: '<S63>/Sum6'
- */
- localDW->UnitDelay_DSTATE = (int32_T)(((int64_T)((*rty_pi_out << 4) - (int32_T)
- tmp) * rtu_Kb) >> 10);
- /* Update for UnitDelay: '<S65>/UnitDelay' incorporates:
- * Sum: '<S65>/Sum1'
- */
- localDW->UnitDelay_DSTATE_i = rtb_Sum1_hy;
- }
- /* Output and update for atomic system: '<S57>/PI_id' */
- void PI_id(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb,
- int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_ext_limProt,
- int16_T *rty_pi_out, DW_PI_id *localDW)
- {
- int64_T tmp;
- int32_T rtb_Divide4_e;
- int32_T rtb_Sum1_l;
- /* Product: '<S59>/Divide4' */
- rtb_Divide4_e = (rtu_err * rtu_P) >> 6;
- /* Product: '<S59>/Divide1' incorporates:
- * Product: '<S59>/Divide4'
- */
- tmp = ((int64_T)rtb_Divide4_e * rtu_I) >> 13;
- if (tmp > 2147483647LL) {
- tmp = 2147483647LL;
- } else {
- if (tmp < -2147483648LL) {
- tmp = -2147483648LL;
- }
- }
- /* Sum: '<S59>/Sum2' incorporates:
- * Product: '<S59>/Divide1'
- * UnitDelay: '<S59>/UnitDelay'
- */
- tmp = ((((int64_T)(int32_T)tmp << 3) + ((int64_T)rtu_ext_limProt << 3)) +
- localDW->UnitDelay_DSTATE_j) >> 3;
- if (tmp > 2147483647LL) {
- tmp = 2147483647LL;
- } else {
- if (tmp < -2147483648LL) {
- tmp = -2147483648LL;
- }
- }
- /* Sum: '<S61>/Sum1' incorporates:
- * Sum: '<S59>/Sum2'
- * UnitDelay: '<S61>/UnitDelay'
- */
- rtb_Sum1_l = (int32_T)tmp + localDW->UnitDelay_DSTATE;
- /* Sum: '<S59>/Sum6' incorporates:
- * Product: '<S59>/Divide4'
- * Sum: '<S61>/Sum1'
- */
- tmp = ((int64_T)rtb_Sum1_l << 3) + rtb_Divide4_e;
- if (tmp > 2147483647LL) {
- tmp = 2147483647LL;
- } else {
- if (tmp < -2147483648LL) {
- tmp = -2147483648LL;
- }
- }
- /* Switch: '<S62>/Switch2' incorporates:
- * RelationalOperator: '<S62>/LowerRelop1'
- * RelationalOperator: '<S62>/UpperRelop'
- * Sum: '<S59>/Sum6'
- * Switch: '<S62>/Switch'
- */
- if ((int32_T)tmp > (rtu_satMax << 4)) {
- *rty_pi_out = rtu_satMax;
- } else if ((int32_T)tmp < (rtu_satMin << 4)) {
- /* Switch: '<S62>/Switch' */
- *rty_pi_out = rtu_satMin;
- } else {
- *rty_pi_out = (int16_T)((int32_T)tmp >> 4);
- }
- /* End of Switch: '<S62>/Switch2' */
- /* Update for UnitDelay: '<S59>/UnitDelay' incorporates:
- * Product: '<S59>/Divide2'
- * Sum: '<S59>/Sum3'
- * Sum: '<S59>/Sum6'
- */
- localDW->UnitDelay_DSTATE_j = (int32_T)(((int64_T)((*rty_pi_out << 4) -
- (int32_T)tmp) * rtu_Kb) >> 10);
- /* Update for UnitDelay: '<S61>/UnitDelay' incorporates:
- * Sum: '<S61>/Sum1'
- */
- localDW->UnitDelay_DSTATE = rtb_Sum1_l;
- }
- /* System initialize for atomic system: '<S73>/pi_speed' */
- void pi_speed_Init(DW_pi_speed *localDW)
- {
- /* InitializeConditions for Delay: '<S77>/Resettable Delay' */
- localDW->icLoad = 1U;
- }
- /* Output and update for atomic system: '<S73>/pi_speed' */
- int16_T pi_speed(int16_T rtu_err, int16_T rtu_P, int16_T rtu_I, int16_T rtu_Kb,
- int16_T rtu_satMax, int16_T rtu_satMin, int16_T rtu_ext_limProt,
- uint8_T rtu_reset, const ConstB_pi_speed *localC, DW_pi_speed
- *localDW, ZCE_pi_speed *localZCE)
- {
- int16_T rty_pi_out_0;
- int64_T tmp;
- int32_T rtb_Divide4_hl;
- int32_T rtb_Sum1_b1;
- /* Product: '<S76>/Divide4' */
- rtb_Divide4_hl = (rtu_err * rtu_P) >> 2;
- /* Delay: '<S77>/Resettable Delay' incorporates:
- * DataTypeConversion: '<S77>/Data Type Conversion2'
- */
- if ((rtu_reset > 0) && (localZCE->ResettableDelay_Reset_ZCE != POS_ZCSIG)) {
- localDW->icLoad = 1U;
- }
- localZCE->ResettableDelay_Reset_ZCE = (ZCSigState)(rtu_reset > 0);
- if (localDW->icLoad != 0) {
- localDW->ResettableDelay_DSTATE = localC->DataTypeConversion2;
- }
- /* Product: '<S76>/Divide1' incorporates:
- * Product: '<S76>/Divide4'
- */
- tmp = ((int64_T)rtb_Divide4_hl * rtu_I) >> 10;
- if (tmp > 2147483647LL) {
- tmp = 2147483647LL;
- } else {
- if (tmp < -2147483648LL) {
- tmp = -2147483648LL;
- }
- }
- /* Sum: '<S76>/Sum2' incorporates:
- * Product: '<S76>/Divide1'
- * UnitDelay: '<S76>/UnitDelay'
- */
- tmp = (((int64_T)(int32_T)tmp + rtu_ext_limProt) + ((int64_T)
- localDW->UnitDelay_DSTATE << 2)) >> 2;
- if (tmp > 2147483647LL) {
- tmp = 2147483647LL;
- } else {
- if (tmp < -2147483648LL) {
- tmp = -2147483648LL;
- }
- }
- /* Sum: '<S77>/Sum1' incorporates:
- * Delay: '<S77>/Resettable Delay'
- * Sum: '<S76>/Sum2'
- */
- rtb_Sum1_b1 = (int32_T)tmp + localDW->ResettableDelay_DSTATE;
- /* Sum: '<S76>/Sum6' incorporates:
- * DataTypeConversion: '<S77>/Data Type Conversion1'
- * Product: '<S76>/Divide4'
- * Sum: '<S77>/Sum1'
- */
- tmp = ((int64_T)(rtb_Sum1_b1 >> 2) << 4) + rtb_Divide4_hl;
- if (tmp > 2147483647LL) {
- tmp = 2147483647LL;
- } else {
- if (tmp < -2147483648LL) {
- tmp = -2147483648LL;
- }
- }
- /* Switch: '<S78>/Switch2' incorporates:
- * RelationalOperator: '<S78>/LowerRelop1'
- * RelationalOperator: '<S78>/UpperRelop'
- * Sum: '<S76>/Sum6'
- * Switch: '<S78>/Switch'
- */
- if ((int32_T)tmp > (rtu_satMax << 4)) {
- rty_pi_out_0 = rtu_satMax;
- } else if ((int32_T)tmp < (rtu_satMin << 4)) {
- /* Switch: '<S78>/Switch' */
- rty_pi_out_0 = rtu_satMin;
- } else {
- rty_pi_out_0 = (int16_T)((int32_T)tmp >> 4);
- }
- /* End of Switch: '<S78>/Switch2' */
- /* Update for UnitDelay: '<S76>/UnitDelay' incorporates:
- * Product: '<S76>/Divide2'
- * Sum: '<S76>/Sum3'
- * Sum: '<S76>/Sum6'
- */
- localDW->UnitDelay_DSTATE = (int32_T)(((int64_T)((rty_pi_out_0 << 4) -
- (int32_T)tmp) * rtu_Kb) >> 12);
- /* Update for Delay: '<S77>/Resettable Delay' incorporates:
- * Sum: '<S77>/Sum1'
- */
- localDW->icLoad = 0U;
- localDW->ResettableDelay_DSTATE = rtb_Sum1_b1;
- return rty_pi_out_0;
- }
- real_T rt_roundd_snf(real_T u)
- {
- real_T y;
- if (fabs(u) < 4.503599627370496E+15) {
- if (u >= 0.5) {
- y = floor(u + 0.5);
- } else if (u > -0.5) {
- y = u * 0.0;
- } else {
- y = ceil(u - 0.5);
- }
- } else {
- y = u;
- }
- return y;
- }
- /* Model step function */
- void PMSM_Controller_step(RT_MODEL *const rtM)
- {
- DW *rtDW = rtM->dwork;
- PrevZCX *rtPrevZCX = rtM->prevZCSigState;
- ExtU *rtU = (ExtU *) rtM->inputs;
- ExtY *rtY = (ExtY *) rtM->outputs;
- int32_T sigIdx;
- int32_T tmp;
- int32_T tmp_0;
- int32_T tmp_1;
- uint32_T tmp_2;
- int16_T rtb_DataTypeConversion[2];
- int16_T rtb_TmpSignalConversionAtLow_Pa[2];
- int16_T rtb_Abs5;
- int16_T rtb_Abs5_h;
- int16_T rtb_Divide1_fi;
- int16_T rtb_Divide4_c;
- int16_T rtb_Gain1;
- int16_T rtb_Gain4;
- int16_T rtb_Max;
- int16_T rtb_Switch2_ip;
- int16_T rtb_Switch3_c;
- int16_T rtb_Switch_b;
- int16_T rtb_Switch_dr;
- int16_T rtb_Switch_oi;
- uint16_T rtb_LogicalOperator3;
- uint16_T rtb_MultiportSwitch_idx_0;
- uint16_T rtb_MultiportSwitch_idx_1;
- uint16_T rtb_Switch2_idx_1;
- uint16_T rtb_Switch2_idx_2;
- int8_T UnitDelay3;
- int8_T rtb_Sum2;
- int8_T rtb_Sum2_tmp;
- uint8_T rtb_Add_k;
- uint8_T rtb_DataTypeConversion1_c;
- uint8_T rtb_Switch2_fu;
- uint8_T rtb_UnitDelay;
- uint8_T rtb_z_ctrlMod;
- boolean_T rtb_LogicalOperator2;
- boolean_T rtb_LogicalOperator4;
- boolean_T rtb_LogicalOperator_p;
- boolean_T rtb_RelationalOperator4_f;
- boolean_T rtb_n_commDeacv;
- /* Outputs for Atomic SubSystem: '<Root>/PMSM_Controller' */
- /* Logic: '<S7>/Edge_Detect' incorporates:
- * Delay: '<S7>/Delay'
- * Delay: '<S7>/Delay1'
- * Delay: '<S7>/Delay2'
- * Inport: '<Root>/hall_a'
- * Inport: '<Root>/hall_b'
- * Inport: '<Root>/hall_c'
- */
- rtb_LogicalOperator_p = (boolean_T)((rtU->hall_a != 0) ^ (rtDW->Delay_DSTATE
- != 0) ^ (rtU->hall_b != 0) ^ (rtDW->Delay1_DSTATE != 0) ^ (rtU->hall_c != 0))
- ^ (rtDW->Delay2_DSTATE != 0);
- /* Sum: '<S9>/Add' incorporates:
- * Gain: '<S9>/Gain'
- * Gain: '<S9>/Gain1'
- * Inport: '<Root>/hall_a'
- * Inport: '<Root>/hall_b'
- * Inport: '<Root>/hall_c'
- */
- rtb_Add_k = (uint8_T)((uint32_T)(uint8_T)((uint32_T)(uint8_T)(rtU->hall_c << 2)
- + (uint8_T)(rtU->hall_b << 1)) + rtU->hall_a);
- /* If: '<S2>/If2' incorporates:
- * If: '<S10>/If2'
- * Inport: '<S15>/z_counterRawPrev'
- * UnitDelay: '<S10>/UnitDelay3'
- */
- if (rtb_LogicalOperator_p) {
- /* Outputs for IfAction SubSystem: '<S2>/Direction_Detection' incorporates:
- * ActionPort: '<S6>/Action Port'
- */
- /* UnitDelay: '<S6>/UnitDelay3' */
- UnitDelay3 = rtDW->Switch2_i;
- /* End of Outputs for SubSystem: '<S2>/Direction_Detection' */
- /* Selector: '<S9>/Selector' incorporates:
- * Constant: '<S9>/vec_hallToPos'
- */
- rtb_Sum2_tmp = rtConstP.vec_hallToPos_Value[rtb_Add_k];
- /* Outputs for IfAction SubSystem: '<S2>/Direction_Detection' incorporates:
- * ActionPort: '<S6>/Action Port'
- */
- /* Sum: '<S6>/Sum2' incorporates:
- * Constant: '<S9>/vec_hallToPos'
- * Selector: '<S9>/Selector'
- * UnitDelay: '<S6>/UnitDelay2'
- */
- rtb_Sum2 = (int8_T)(rtb_Sum2_tmp - rtDW->UnitDelay2_DSTATE_j);
- /* Switch: '<S6>/Switch2' incorporates:
- * Constant: '<S6>/Constant20'
- * Constant: '<S6>/Constant8'
- * Logic: '<S6>/Logical Operator3'
- * RelationalOperator: '<S6>/Relational Operator1'
- * RelationalOperator: '<S6>/Relational Operator6'
- */
- if ((rtb_Sum2 == 1) || (rtb_Sum2 == -5)) {
- /* Switch: '<S6>/Switch2' incorporates:
- * Constant: '<S6>/Constant24'
- */
- rtDW->Switch2_i = 1;
- } else {
- /* Switch: '<S6>/Switch2' incorporates:
- * Constant: '<S6>/Constant23'
- */
- rtDW->Switch2_i = -1;
- }
- /* End of Switch: '<S6>/Switch2' */
- /* Update for UnitDelay: '<S6>/UnitDelay2' */
- rtDW->UnitDelay2_DSTATE_j = rtb_Sum2_tmp;
- /* End of Outputs for SubSystem: '<S2>/Direction_Detection' */
- /* Outputs for IfAction SubSystem: '<S10>/Raw_Motor_Speed_Estimation' incorporates:
- * ActionPort: '<S15>/Action Port'
- */
- /* RelationalOperator: '<S15>/Relational Operator4' */
- rtb_RelationalOperator4_f = (rtDW->Switch2_i != UnitDelay3);
- rtDW->z_counterRawPrev = rtDW->UnitDelay3_DSTATE;
- /* Switch: '<S15>/Switch3' incorporates:
- * Constant: '<S15>/Constant4'
- * Inport: '<S15>/z_counterRawPrev'
- * Logic: '<S15>/Logical Operator1'
- * Switch: '<S15>/Switch2'
- * UnitDelay: '<S10>/UnitDelay3'
- * UnitDelay: '<S15>/UnitDelay1'
- */
- if (rtb_RelationalOperator4_f && rtDW->UnitDelay1_DSTATE_i) {
- rtb_Switch3_c = 0;
- } else if (rtb_RelationalOperator4_f) {
- /* Switch: '<S15>/Switch3' incorporates:
- * Switch: '<S15>/Switch2'
- * UnitDelay: '<S10>/UnitDelay4'
- */
- rtb_Switch3_c = rtDW->UnitDelay4_DSTATE;
- } else {
- /* Product: '<S15>/Divide13' incorporates:
- * Sum: '<S15>/Sum13'
- * Switch: '<S15>/Switch2'
- * UnitDelay: '<S15>/UnitDelay2'
- * UnitDelay: '<S15>/UnitDelay3'
- * UnitDelay: '<S15>/UnitDelay5'
- */
- tmp_2 = 8000000U / (((rtDW->UnitDelay2_DSTATE + rtDW->UnitDelay3_DSTATE_l)
- + rtDW->UnitDelay5_DSTATE) + rtDW->z_counterRawPrev);
- if (tmp_2 > 32767U) {
- tmp_2 = 32767U;
- }
- /* Switch: '<S15>/Switch3' incorporates:
- * Product: '<S15>/Divide13'
- * Switch: '<S15>/Switch2'
- */
- rtb_Switch3_c = (int16_T)tmp_2;
- }
- /* End of Switch: '<S15>/Switch3' */
- /* Product: '<S15>/Divide11' incorporates:
- * Switch: '<S15>/Switch3'
- */
- rtDW->Divide11 = (int16_T)(rtb_Switch3_c * rtDW->Switch2_i);
- /* Update for UnitDelay: '<S15>/UnitDelay1' */
- rtDW->UnitDelay1_DSTATE_i = rtb_RelationalOperator4_f;
- /* Update for UnitDelay: '<S15>/UnitDelay2' incorporates:
- * UnitDelay: '<S15>/UnitDelay3'
- */
- rtDW->UnitDelay2_DSTATE = rtDW->UnitDelay3_DSTATE_l;
- /* Update for UnitDelay: '<S15>/UnitDelay3' incorporates:
- * UnitDelay: '<S15>/UnitDelay5'
- */
- rtDW->UnitDelay3_DSTATE_l = rtDW->UnitDelay5_DSTATE;
- /* Update for UnitDelay: '<S15>/UnitDelay5' */
- rtDW->UnitDelay5_DSTATE = rtDW->z_counterRawPrev;
- /* End of Outputs for SubSystem: '<S10>/Raw_Motor_Speed_Estimation' */
- }
- /* End of If: '<S2>/If2' */
- /* Switch: '<S8>/Switch3' incorporates:
- * Constant: '<S8>/Constant16'
- * Constant: '<S8>/Constant2'
- * Constant: '<S9>/vec_hallToPos'
- * RelationalOperator: '<S8>/Relational Operator7'
- * Selector: '<S9>/Selector'
- * Sum: '<S8>/Sum1'
- */
- if (rtDW->Switch2_i == 1) {
- rtb_Sum2 = rtConstP.vec_hallToPos_Value[rtb_Add_k];
- } else {
- rtb_Sum2 = (int8_T)(rtConstP.vec_hallToPos_Value[rtb_Add_k] + 1);
- }
- /* End of Switch: '<S8>/Switch3' */
- /* MinMax: '<S8>/MinMax' incorporates:
- * Inport: '<Root>/hw_count'
- */
- if (rtU->hw_count < rtDW->z_counterRawPrev) {
- tmp_2 = rtU->hw_count;
- } else {
- tmp_2 = rtDW->z_counterRawPrev;
- }
- /* End of MinMax: '<S8>/MinMax' */
- /* Sum: '<S8>/Sum3' incorporates:
- * Product: '<S8>/Divide1'
- * Product: '<S8>/Divide3'
- */
- rtb_Switch3_c = (int16_T)(((int16_T)((int16_T)(((uint64_T)tmp_2 << 14) /
- rtDW->z_counterRawPrev) * rtDW->Switch2_i) + (rtb_Sum2 << 14)) >> 2);
- /* MinMax: '<S8>/MinMax1' incorporates:
- * Constant: '<S8>/Constant1'
- * Sum: '<S8>/Sum3'
- * Switch: '<S8>/Switch2'
- */
- if (rtb_Switch3_c <= 0) {
- rtb_Switch3_c = 0;
- }
- /* End of MinMax: '<S8>/MinMax1' */
- /* Sum: '<S11>/Add2' incorporates:
- * Constant: '<S11>/Constant2'
- * Product: '<S8>/Divide2'
- */
- rtb_Switch3_c = (int16_T)((((15 * rtb_Switch3_c) >> 4) + 3840) >> 2);
- /* If: '<S11>/If' incorporates:
- * Constant: '<S11>/Constant3'
- * DataTypeConversion: '<S11>/Data Type Conversion'
- * Inport: '<S12>/In1'
- * Merge: '<S11>/Merge'
- * Sum: '<S11>/Add'
- * Sum: '<S11>/Add2'
- */
- if ((int16_T)(rtb_Switch3_c >> 4) >= 360) {
- /* Outputs for IfAction SubSystem: '<S11>/If Action Subsystem' incorporates:
- * ActionPort: '<S12>/Action Port'
- */
- rtb_Switch3_c = (int16_T)(rtb_Switch3_c - 5760);
- /* End of Outputs for SubSystem: '<S11>/If Action Subsystem' */
- }
- /* End of If: '<S11>/If' */
- /* Switch: '<S10>/Switch2' incorporates:
- * Constant: '<S10>/Constant4'
- * Inport: '<Root>/hw_count'
- * Product: '<S15>/Divide11'
- * RelationalOperator: '<S10>/Relational Operator2'
- */
- if (rtU->hw_count >= 400000U) {
- rtb_Switch2_ip = 0;
- } else {
- rtb_Switch2_ip = rtDW->Divide11;
- }
- /* End of Switch: '<S10>/Switch2' */
- /* Abs: '<S10>/Abs5' incorporates:
- * Switch: '<S10>/Switch2'
- */
- if (rtb_Switch2_ip < 0) {
- rtb_Abs5 = (int16_T)-rtb_Switch2_ip;
- } else {
- rtb_Abs5 = rtb_Switch2_ip;
- }
- /* End of Abs: '<S10>/Abs5' */
- /* If: '<S10>/If1' */
- if (rtb_LogicalOperator_p) {
- /* Outputs for IfAction SubSystem: '<S10>/Subsystem' incorporates:
- * ActionPort: '<S16>/Action Port'
- */
- /* Relay: '<S16>/n_commDeacv' incorporates:
- * Abs: '<S10>/Abs5'
- */
- rtDW->n_commDeacv_Mode = ((rtb_Abs5 >= 120) || ((rtb_Abs5 > 60) &&
- rtDW->n_commDeacv_Mode));
- /* RelationalOperator: '<S18>/Compare' incorporates:
- * Constant: '<S18>/Constant'
- * Relay: '<S16>/n_commDeacv'
- * Sum: '<S16>/Sum13'
- * UnitDelay: '<S16>/UnitDelay2'
- * UnitDelay: '<S16>/UnitDelay3'
- * UnitDelay: '<S16>/UnitDelay5'
- */
- rtDW->Compare = ((uint16_T)((uint32_T)(uint16_T)((uint32_T)(uint16_T)
- ((uint32_T)rtDW->UnitDelay2_DSTATE_f + rtDW->UnitDelay3_DSTATE_lh) +
- rtDW->UnitDelay5_DSTATE_f) + rtDW->n_commDeacv_Mode) >= 4);
- /* Update for UnitDelay: '<S16>/UnitDelay2' incorporates:
- * UnitDelay: '<S16>/UnitDelay3'
- */
- rtDW->UnitDelay2_DSTATE_f = rtDW->UnitDelay3_DSTATE_lh;
- /* Update for UnitDelay: '<S16>/UnitDelay3' incorporates:
- * UnitDelay: '<S16>/UnitDelay5'
- */
- rtDW->UnitDelay3_DSTATE_lh = rtDW->UnitDelay5_DSTATE_f;
- /* Update for UnitDelay: '<S16>/UnitDelay5' incorporates:
- * Logic: '<S16>/Logical Operator3'
- * Relay: '<S16>/n_commDeacv'
- */
- rtDW->UnitDelay5_DSTATE_f = rtDW->n_commDeacv_Mode;
- /* End of Outputs for SubSystem: '<S10>/Subsystem' */
- }
- /* End of If: '<S10>/If1' */
- /* Switch: '<S2>/Switch' incorporates:
- * Inport: '<Root>/b_hall_calibrate'
- * Inport: '<Root>/open_theta'
- * Merge: '<S11>/Merge'
- */
- if (rtU->b_hall_calibrate) {
- rtb_Switch_b = (int16_T)(rtU->open_theta << 4);
- } else {
- rtb_Switch_b = rtb_Switch3_c;
- }
- /* End of Switch: '<S2>/Switch' */
- /* Abs: '<S3>/Abs2' incorporates:
- * Switch: '<S10>/Switch2'
- */
- if (rtb_Switch2_ip < 0) {
- rtb_LogicalOperator3 = (uint16_T)((uint32_T)-rtb_Switch2_ip >> 2);
- } else {
- rtb_LogicalOperator3 = (uint16_T)((uint32_T)rtb_Switch2_ip >> 2);
- }
- /* End of Abs: '<S3>/Abs2' */
- /* UnitDelay: '<S34>/UnitDelay' */
- rtb_UnitDelay = rtDW->UnitDelay_DSTATE_j;
- /* Outport: '<Root>/VqPrev' incorporates:
- * UnitDelay: '<S5>/UnitDelay2'
- */
- rtY->VqPrev = rtDW->UnitDelay2_DSTATE_p;
- /* Switch: '<S34>/Switch3' incorporates:
- * Abs: '<S10>/Abs5'
- * Abs: '<S34>/Abs4'
- * Constant: '<S34>/CTRL_COMM4'
- * Inport: '<Root>/b_motEna'
- * Logic: '<S34>/Logical Operator1'
- * RelationalOperator: '<S10>/Relational Operator9'
- * RelationalOperator: '<S34>/Relational Operator7'
- * S-Function (sfix_bitop): '<S34>/Bitwise Operator1'
- * UnitDelay: '<S5>/UnitDelay2'
- */
- if ((rtb_UnitDelay & 4U) != 0U) {
- rtb_LogicalOperator_p = true;
- } else {
- if (rtDW->UnitDelay2_DSTATE_p < 0) {
- /* Abs: '<S34>/Abs4' incorporates:
- * UnitDelay: '<S5>/UnitDelay2'
- */
- rtb_Divide1_fi = (int16_T)-rtDW->UnitDelay2_DSTATE_p;
- } else {
- /* Abs: '<S34>/Abs4' incorporates:
- * UnitDelay: '<S5>/UnitDelay2'
- */
- rtb_Divide1_fi = rtDW->UnitDelay2_DSTATE_p;
- }
- rtb_LogicalOperator_p = (rtU->b_motEna && (rtb_Abs5 < 12) && (rtb_Divide1_fi
- > 960));
- }
- /* End of Switch: '<S34>/Switch3' */
- /* Sum: '<S34>/Sum' incorporates:
- * Constant: '<S34>/CTRL_COMM'
- * Constant: '<S34>/CTRL_COMM1'
- * DataTypeConversion: '<S34>/Data Type Conversion3'
- * Gain: '<S34>/g_Hb'
- * Gain: '<S34>/g_Hb1'
- * RelationalOperator: '<S34>/Relational Operator1'
- * RelationalOperator: '<S34>/Relational Operator3'
- */
- rtb_DataTypeConversion1_c = (uint8_T)(((uint32_T)((rtb_Add_k == 7) << 1) +
- (rtb_Add_k == 0)) + (rtb_LogicalOperator_p << 2));
- /* Outputs for Atomic SubSystem: '<S34>/Debounce_Filter' */
- /* RelationalOperator: '<S34>/Relational Operator2' incorporates:
- * Constant: '<S34>/CTRL_COMM2'
- * Constant: '<S34>/t_errDequal'
- * Constant: '<S34>/t_errQual'
- */
- Debounce_Filter(rtb_DataTypeConversion1_c != 0, 1600, 12000,
- &rtb_RelationalOperator4_f, &rtDW->Debounce_Filter_i);
- /* End of Outputs for SubSystem: '<S34>/Debounce_Filter' */
- /* Logic: '<S21>/Logical Operator12' incorporates:
- * Inport: '<Root>/b_motEna'
- * Logic: '<S21>/Logical Operator7'
- */
- rtb_n_commDeacv = ((!rtb_RelationalOperator4_f) && rtU->b_motEna);
- /* Logic: '<S21>/Logical Operator4' incorporates:
- * Constant: '<S21>/constant8'
- * Inport: '<Root>/b_hall_calibrate'
- * Inport: '<Root>/n_ctrlModReq'
- * Logic: '<S21>/Logical Operator11'
- * Logic: '<S21>/Logical Operator8'
- * RelationalOperator: '<S21>/Relational Operator10'
- */
- rtb_LogicalOperator4 = (rtU->b_hall_calibrate || (!rtDW->Compare) ||
- (!rtb_n_commDeacv) || (rtU->n_ctrlModReq == 0));
- /* Relay: '<S21>/n_SpeedCtrl' */
- rtDW->n_SpeedCtrl_Mode = ((rtb_LogicalOperator3 >= 300) ||
- ((rtb_LogicalOperator3 > 200) && rtDW->n_SpeedCtrl_Mode));
- rtb_LogicalOperator_p = rtDW->n_SpeedCtrl_Mode;
- /* Logic: '<S21>/Logical Operator10' incorporates:
- * Inport: '<Root>/b_cruiseEna'
- */
- rtb_LogicalOperator_p = (rtb_LogicalOperator_p && rtU->b_cruiseEna);
- /* Logic: '<S21>/Logical Operator2' incorporates:
- * Constant: '<S21>/constant'
- * Inport: '<Root>/n_ctrlModReq'
- * Logic: '<S21>/Logical Operator5'
- * RelationalOperator: '<S21>/Relational Operator4'
- */
- rtb_LogicalOperator2 = ((rtU->n_ctrlModReq == 2) && (!rtb_LogicalOperator_p));
- /* Logic: '<S21>/Logical Operator1' incorporates:
- * Constant: '<S21>/constant1'
- * Inport: '<Root>/n_ctrlModReq'
- * RelationalOperator: '<S21>/Relational Operator1'
- */
- rtb_LogicalOperator_p = ((rtU->n_ctrlModReq == 1) || rtb_LogicalOperator_p);
- /* Chart: '<S3>/Control_Mode_Manager' incorporates:
- * Logic: '<S21>/Logical Operator3'
- * Logic: '<S21>/Logical Operator6'
- * Logic: '<S21>/Logical Operator9'
- */
- if (rtDW->is_active_c5_PMSM_Controller == 0U) {
- rtDW->is_active_c5_PMSM_Controller = 1U;
- rtDW->is_c5_PMSM_Controller = IN_OPEN;
- rtb_z_ctrlMod = OPEN_MODE;
- } else if (rtDW->is_c5_PMSM_Controller == 1) {
- if (rtb_LogicalOperator4) {
- rtDW->is_ACTIVE = IN_NO_ACTIVE_CHILD;
- rtDW->is_c5_PMSM_Controller = IN_OPEN;
- rtb_z_ctrlMod = OPEN_MODE;
- } else if (rtDW->is_ACTIVE == 1) {
- rtb_z_ctrlMod = SPD_MODE;
- if (!rtb_LogicalOperator_p) {
- if (rtb_LogicalOperator2) {
- rtDW->is_ACTIVE = IN_TORQUE_MODE;
- rtb_z_ctrlMod = TRQ_MODE;
- } else {
- rtDW->is_ACTIVE = IN_SPEED_MODE;
- }
- }
- } else {
- /* case IN_TORQUE_MODE: */
- rtb_z_ctrlMod = TRQ_MODE;
- if (!rtb_LogicalOperator2) {
- rtDW->is_ACTIVE = IN_SPEED_MODE;
- rtb_z_ctrlMod = SPD_MODE;
- }
- }
- } else {
- /* case IN_OPEN: */
- rtb_z_ctrlMod = OPEN_MODE;
- if ((!rtb_LogicalOperator4) && (rtb_LogicalOperator2 ||
- rtb_LogicalOperator_p)) {
- rtDW->is_c5_PMSM_Controller = IN_ACTIVE;
- if (rtb_LogicalOperator2) {
- rtDW->is_ACTIVE = IN_TORQUE_MODE;
- rtb_z_ctrlMod = TRQ_MODE;
- } else {
- rtDW->is_ACTIVE = IN_SPEED_MODE;
- rtb_z_ctrlMod = SPD_MODE;
- }
- }
- }
- /* End of Chart: '<S3>/Control_Mode_Manager' */
- /* Switch: '<S22>/Switch' incorporates:
- * Constant: '<S22>/Constant3'
- * Inport: '<Root>/input_target'
- */
- if (rtU->input_target > 60) {
- /* Switch: '<S22>/Switch1' incorporates:
- * Constant: '<S22>/Constant1'
- * DataTypeConversion: '<S22>/Data Type Conversion'
- * Switch: '<S22>/Switch'
- */
- if (rtb_n_commDeacv) {
- rtb_Switch_oi = rtU->input_target;
- } else {
- rtb_Switch_oi = 0;
- }
- /* End of Switch: '<S22>/Switch1' */
- } else {
- rtb_Switch_oi = 0;
- }
- /* End of Switch: '<S22>/Switch' */
- /* Switch: '<S22>/Switch3' incorporates:
- * Constant: '<S22>/Constant4'
- * DataTypeConversion: '<S22>/Data Type Conversion2'
- * Inport: '<Root>/vq_open_target'
- */
- if (rtb_n_commDeacv) {
- rtb_Abs5_h = rtU->vq_open_target;
- } else {
- rtb_Abs5_h = 0;
- }
- /* End of Switch: '<S22>/Switch3' */
- /* If: '<S23>/If' incorporates:
- * Inport: '<Root>/b_hall_calibrate'
- * Inport: '<S27>/vq_in'
- * Switch: '<S22>/Switch3'
- */
- if (rtU->b_hall_calibrate) {
- /* Switch: '<S22>/Switch2' incorporates:
- * Constant: '<S22>/Constant2'
- * DataTypeConversion: '<S22>/Data Type Conversion1'
- * Inport: '<Root>/vd_open_target'
- * Inport: '<S27>/vd_in'
- */
- if (rtb_n_commDeacv) {
- /* Outputs for IfAction SubSystem: '<S23>/If Action Subsystem' incorporates:
- * ActionPort: '<S27>/Action Port'
- */
- rtDW->Merge[0] = rtU->vd_open_target;
- /* End of Outputs for SubSystem: '<S23>/If Action Subsystem' */
- } else {
- /* Outputs for IfAction SubSystem: '<S23>/If Action Subsystem' incorporates:
- * ActionPort: '<S27>/Action Port'
- */
- rtDW->Merge[0] = 0;
- /* End of Outputs for SubSystem: '<S23>/If Action Subsystem' */
- }
- /* End of Switch: '<S22>/Switch2' */
- /* Outputs for IfAction SubSystem: '<S23>/If Action Subsystem' incorporates:
- * ActionPort: '<S27>/Action Port'
- */
- rtDW->Merge[1] = rtb_Abs5_h;
- /* End of Outputs for SubSystem: '<S23>/If Action Subsystem' */
- } else if (rtb_z_ctrlMod == 0) {
- /* Outputs for IfAction SubSystem: '<S23>/open_mode' incorporates:
- * ActionPort: '<S28>/Action Port'
- */
- /* RelationalOperator: '<S28>/Equal1' incorporates:
- * Switch: '<S22>/Switch3'
- * UnitDelay: '<S28>/Unit Delay'
- */
- rtb_LogicalOperator_p = (rtDW->UnitDelay_DSTATE != rtb_Abs5_h);
- /* If: '<S30>/If' */
- if (rtb_LogicalOperator_p) {
- /* Outputs for IfAction SubSystem: '<S30>/Subsystem' incorporates:
- * ActionPort: '<S32>/Action Port'
- */
- /* Sum: '<S32>/Add' incorporates:
- * Signum: '<S32>/Sign'
- * Switch: '<S22>/Switch3'
- * UnitDelay: '<S5>/UnitDelay2'
- */
- rtb_Divide1_fi = (int16_T)((rtb_Abs5_h - rtDW->UnitDelay2_DSTATE_p) >> 2);
- /* Signum: '<S32>/Sign' */
- if (rtb_Divide1_fi < 0) {
- rtb_Divide1_fi = -1;
- } else {
- rtb_Divide1_fi = (int16_T)(rtb_Divide1_fi > 0);
- }
- /* End of Signum: '<S32>/Sign' */
- /* Product: '<S32>/Divide' incorporates:
- * Constant: '<S28>/Constant5'
- */
- rtDW->Divide = (int16_T)(rtb_Divide1_fi * 6);
- /* Switch: '<S32>/Switch' incorporates:
- * Switch: '<S32>/Switch1'
- */
- if (rtb_Divide1_fi > 0) {
- /* Switch: '<S32>/Switch' incorporates:
- * Switch: '<S22>/Switch3'
- */
- rtDW->Switch = rtb_Abs5_h;
- /* Switch: '<S32>/Switch1' incorporates:
- * UnitDelay: '<S5>/UnitDelay2'
- */
- rtDW->Switch1 = rtDW->UnitDelay2_DSTATE_p;
- } else {
- /* Switch: '<S32>/Switch' incorporates:
- * UnitDelay: '<S5>/UnitDelay2'
- */
- rtDW->Switch = rtDW->UnitDelay2_DSTATE_p;
- /* Switch: '<S32>/Switch1' incorporates:
- * Switch: '<S22>/Switch3'
- */
- rtDW->Switch1 = rtb_Abs5_h;
- }
- /* End of Switch: '<S32>/Switch' */
- /* End of Outputs for SubSystem: '<S30>/Subsystem' */
- /* Switch: '<S33>/Switch1' incorporates:
- * UnitDelay: '<S5>/UnitDelay2'
- */
- rtb_Switch_dr = rtDW->UnitDelay2_DSTATE_p;
- } else {
- /* Switch: '<S33>/Switch1' incorporates:
- * UnitDelay: '<S33>/UnitDelay'
- */
- rtb_Switch_dr = rtDW->UnitDelay_DSTATE_d;
- }
- /* End of If: '<S30>/If' */
- /* Sum: '<S30>/Add2' incorporates:
- * Product: '<S32>/Divide'
- */
- sigIdx = ((rtb_Switch_dr << 1) + rtDW->Divide) >> 1;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- /* Switch: '<S28>/Switch' incorporates:
- * Switch: '<S22>/Switch'
- */
- if (rtb_Switch_oi > 0) {
- /* Switch: '<S31>/Switch2' incorporates:
- * RelationalOperator: '<S31>/LowerRelop1'
- * RelationalOperator: '<S31>/UpperRelop'
- * Sum: '<S30>/Add2'
- * Switch: '<S31>/Switch'
- * Switch: '<S32>/Switch'
- * Switch: '<S32>/Switch1'
- */
- if ((int16_T)sigIdx > rtDW->Switch) {
- /* Merge: '<S23>/Merge' incorporates:
- * Switch: '<S28>/Switch'
- */
- rtDW->Merge[1] = rtDW->Switch;
- } else if ((int16_T)sigIdx < rtDW->Switch1) {
- /* Merge: '<S23>/Merge' incorporates:
- * Switch: '<S28>/Switch'
- * Switch: '<S31>/Switch'
- * Switch: '<S32>/Switch1'
- */
- rtDW->Merge[1] = rtDW->Switch1;
- } else {
- /* Merge: '<S23>/Merge' incorporates:
- * Switch: '<S28>/Switch'
- */
- rtDW->Merge[1] = (int16_T)sigIdx;
- }
- /* End of Switch: '<S31>/Switch2' */
- } else {
- /* Merge: '<S23>/Merge' incorporates:
- * Constant: '<S28>/Constant1'
- */
- rtDW->Merge[1] = 0;
- }
- /* End of Switch: '<S28>/Switch' */
- /* Merge: '<S23>/Merge' incorporates:
- * Constant: '<S28>/Constant3'
- * SignalConversion generated from: '<S28>/open_voltage'
- */
- rtDW->Merge[0] = 0;
- /* Update for UnitDelay: '<S28>/Unit Delay' incorporates:
- * Switch: '<S22>/Switch3'
- */
- rtDW->UnitDelay_DSTATE = rtb_Abs5_h;
- /* Switch: '<S33>/Switch2' */
- if (rtb_LogicalOperator_p) {
- /* Update for UnitDelay: '<S33>/UnitDelay' incorporates:
- * UnitDelay: '<S5>/UnitDelay2'
- */
- rtDW->UnitDelay_DSTATE_d = rtDW->UnitDelay2_DSTATE_p;
- } else {
- /* Update for UnitDelay: '<S33>/UnitDelay' incorporates:
- * Sum: '<S30>/Add2'
- */
- rtDW->UnitDelay_DSTATE_d = (int16_T)sigIdx;
- }
- /* End of Switch: '<S33>/Switch2' */
- /* End of Outputs for SubSystem: '<S23>/open_mode' */
- } else {
- /* Outputs for IfAction SubSystem: '<S23>/torque_mode' incorporates:
- * ActionPort: '<S29>/Action Port'
- */
- /* Product: '<S29>/Divide1' incorporates:
- * Inport: '<Root>/i_dc_limit'
- * Inport: '<Root>/speed_limit'
- * Product: '<S29>/Divide4'
- * Switch: '<S22>/Switch'
- */
- sigIdx = ((uint16_T)((rtU->i_dc_limit << 8) / rtU->speed_limit) *
- rtb_Switch_oi) >> 8;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- /* Product: '<S29>/Divide1' */
- rtDW->Divide1 = (int16_T)sigIdx;
- /* End of Outputs for SubSystem: '<S23>/torque_mode' */
- }
- /* End of If: '<S23>/If' */
- /* Outputs for Atomic SubSystem: '<S34>/either_edge' */
- rtb_LogicalOperator_p = either_edge(rtb_RelationalOperator4_f,
- &rtDW->either_edge_f);
- /* End of Outputs for SubSystem: '<S34>/either_edge' */
- /* Switch: '<S34>/Switch1' */
- if (rtb_LogicalOperator_p) {
- rtb_UnitDelay = rtb_DataTypeConversion1_c;
- }
- /* End of Switch: '<S34>/Switch1' */
- /* Gain: '<S51>/Multiply' incorporates:
- * DataTypeConversion: '<S54>/Data Type Conversion'
- * Inport: '<Root>/adc_a'
- * Inport: '<Root>/adc_b'
- */
- sigIdx = (12351 * rtU->adc_a) >> 11;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- rtb_DataTypeConversion[0] = (int16_T)sigIdx;
- tmp_1 = (12351 * rtU->adc_b) >> 11;
- if (tmp_1 > 32767) {
- tmp_1 = 32767;
- } else {
- if (tmp_1 < -32768) {
- tmp_1 = -32768;
- }
- }
- rtb_DataTypeConversion[1] = (int16_T)tmp_1;
- /* Sum: '<S45>/Add' incorporates:
- * Gain: '<S51>/Multiply'
- */
- tmp = (int16_T)sigIdx + (int16_T)tmp_1;
- if (tmp > 32767) {
- tmp = 32767;
- } else {
- if (tmp < -32768) {
- tmp = -32768;
- }
- }
- /* Sum: '<S45>/Add1' incorporates:
- * Sum: '<S45>/Add'
- */
- tmp_0 = -tmp;
- if (-tmp > 32767) {
- tmp_0 = 32767;
- }
- /* Sum: '<S53>/Add3' incorporates:
- * Gain: '<S51>/Multiply'
- * Sum: '<S45>/Add1'
- */
- tmp = (int16_T)tmp_1 + (int16_T)tmp_0;
- if (tmp > 32767) {
- tmp = 32767;
- } else {
- if (tmp < -32768) {
- tmp = -32768;
- }
- }
- /* Sum: '<S53>/Add' incorporates:
- * Gain: '<S51>/Multiply'
- * Sum: '<S53>/Add3'
- */
- sigIdx = (((int16_T)sigIdx << 1) - tmp) >> 1;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- /* Gain: '<S53>/Gain1' incorporates:
- * Product: '<S55>/Divide1'
- * Sum: '<S53>/Add'
- */
- rtb_Divide1_fi = (int16_T)((21845 * sigIdx) >> 15);
- /* Gain: '<S53>/Gain2' incorporates:
- * Gain: '<S51>/Multiply'
- * Sum: '<S45>/Add1'
- * Sum: '<S53>/Add2'
- */
- sigIdx = ((((int16_T)tmp_1 - (int16_T)tmp_0) >> 1) * 18919) >> 14;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- /* PreLookup: '<S56>/a_elecAngle_XA' incorporates:
- * Switch: '<S2>/Switch'
- */
- rtb_LogicalOperator3 = plook_u16s16_evencka(rtb_Switch_b, 0, 4U, 1440U);
- /* Interpolation_n-D: '<S56>/r_cos_M1' */
- rtb_Switch_dr = rtConstP.r_cos_M1_Table[rtb_LogicalOperator3];
- /* Interpolation_n-D: '<S56>/r_sin_M1' incorporates:
- * Product: '<S67>/Divide4'
- */
- rtb_Abs5_h = rtConstP.r_sin_M1_Table[rtb_LogicalOperator3];
- /* Sum: '<S55>/Sum1' incorporates:
- * Gain: '<S53>/Gain2'
- * Interpolation_n-D: '<S56>/r_cos_M1'
- * Interpolation_n-D: '<S56>/r_sin_M1'
- * Product: '<S55>/Divide1'
- * Product: '<S55>/Divide2'
- * Product: '<S55>/Divide3'
- */
- tmp_1 = (int16_T)((rtb_Divide1_fi *
- rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]) >> 14) +
- (int16_T)(((int16_T)sigIdx * rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]) >>
- 14);
- if (tmp_1 > 32767) {
- tmp_1 = 32767;
- } else {
- if (tmp_1 < -32768) {
- tmp_1 = -32768;
- }
- }
- /* SignalConversion generated from: '<S45>/Low_Pass_Filter' incorporates:
- * Sum: '<S55>/Sum1'
- */
- rtb_TmpSignalConversionAtLow_Pa[0] = (int16_T)tmp_1;
- /* Sum: '<S55>/Sum6' incorporates:
- * Gain: '<S53>/Gain2'
- * Interpolation_n-D: '<S56>/r_cos_M1'
- * Interpolation_n-D: '<S56>/r_sin_M1'
- * Product: '<S55>/Divide1'
- * Product: '<S55>/Divide4'
- */
- sigIdx = (int16_T)(((int16_T)sigIdx *
- rtConstP.r_cos_M1_Table[rtb_LogicalOperator3]) >> 14) -
- (int16_T)((rtb_Divide1_fi * rtConstP.r_sin_M1_Table[rtb_LogicalOperator3]) >>
- 14);
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- /* SignalConversion generated from: '<S45>/Low_Pass_Filter' incorporates:
- * Sum: '<S55>/Sum6'
- */
- rtb_TmpSignalConversionAtLow_Pa[1] = (int16_T)sigIdx;
- /* Outputs for Atomic SubSystem: '<S45>/Low_Pass_Filter' */
- /* Constant: '<S45>/Constant' */
- Low_Pass_Filter(rtb_TmpSignalConversionAtLow_Pa, 26214, rtb_DataTypeConversion,
- &rtDW->Low_Pass_Filter_d);
- /* End of Outputs for SubSystem: '<S45>/Low_Pass_Filter' */
- /* Outport: '<Root>/VdPrev' incorporates:
- * UnitDelay: '<S5>/UnitDelay1'
- */
- rtY->VdPrev = rtDW->UnitDelay1_DSTATE;
- /* Interpolation_n-D: '<S46>/Vq_max_M1' incorporates:
- * Abs: '<S46>/Abs5'
- * PreLookup: '<S46>/Vq_max_XA'
- * UnitDelay: '<S5>/UnitDelay1'
- */
- if (rtDW->UnitDelay1_DSTATE < 0) {
- rtb_Divide1_fi = (int16_T)-rtDW->UnitDelay1_DSTATE;
- } else {
- rtb_Divide1_fi = rtDW->UnitDelay1_DSTATE;
- }
- rtb_Divide1_fi = rtConstP.Vq_max_M1_Table[plook_u16s16_evencka(rtb_Divide1_fi,
- 0, 64U, 45U)];
- /* End of Interpolation_n-D: '<S46>/Vq_max_M1' */
- /* Product: '<S46>/Divide4' incorporates:
- * Inport: '<Root>/i_dc_limit'
- * Product: '<S24>/Divide3'
- */
- sigIdx = rtDW->Divide3 << 16;
- sigIdx = (sigIdx == MIN_int32_T) && (rtU->i_dc_limit == -1) ? MAX_int32_T :
- sigIdx / rtU->i_dc_limit;
- if (sigIdx < 0) {
- sigIdx = 0;
- } else {
- if (sigIdx > 65535) {
- sigIdx = 65535;
- }
- }
- /* PreLookup: '<S46>/iq_maxSca_XA' incorporates:
- * Product: '<S46>/Divide4'
- */
- rtb_DataTypeConversion1_c = plook_u8u16_evencka((uint16_T)sigIdx, 0U, 1311U,
- 49U);
- /* Outport: '<Root>/PWM' incorporates:
- * Interpolation_n-D: '<S46>/iq_maxSca_M1'
- */
- rtY->PWM[6] = rtConstP.iq_maxSca_M1_Table[rtb_DataTypeConversion1_c];
- /* Product: '<S46>/Divide1' incorporates:
- * Inport: '<Root>/i_dc_limit'
- * Interpolation_n-D: '<S46>/iq_maxSca_M1'
- */
- rtb_Divide4_c = (int16_T)
- ((rtConstP.iq_maxSca_M1_Table[rtb_DataTypeConversion1_c] * rtU->i_dc_limit) >>
- 16);
- /* Switch: '<S52>/Switch2' */
- rtb_Switch2_fu = (uint8_T)(rtb_z_ctrlMod != 0);
- /* Delay: '<S80>/Delay' */
- rtb_RelationalOperator4_f = rtDW->Delay_DSTATE_n[0];
- /* DataTypeConversion: '<S52>/Data Type Conversion1' incorporates:
- * Delay: '<S80>/Delay'
- * Logic: '<S52>/Logical Operator'
- * Logic: '<S80>/Logical Operator'
- * UnitDelay: '<S80>/Unit Delay'
- */
- rtb_DataTypeConversion1_c = (uint8_T)((rtb_Switch2_fu != 0) && ((boolean_T)
- (rtDW->UnitDelay_DSTATE_f ^ rtDW->Delay_DSTATE_n[0])));
- /* If: '<S50>/If' incorporates:
- * Constant: '<S73>/Constant1'
- * Constant: '<S73>/Constant11'
- * Constant: '<S73>/Constant2'
- * Constant: '<S73>/Constant4'
- * Gain: '<S46>/Gain1'
- * Product: '<S46>/Divide1'
- * Sum: '<S73>/Add2'
- * Switch: '<S10>/Switch2'
- * Switch: '<S78>/Switch2'
- */
- if ((rtb_DataTypeConversion1_c > 0) && (rtb_z_ctrlMod == 1)) {
- /* Outputs for IfAction SubSystem: '<S50>/speed_mode' incorporates:
- * ActionPort: '<S73>/Action Port'
- */
- /* Switch: '<S75>/Switch2' incorporates:
- * Inport: '<Root>/speed_limit'
- * RelationalOperator: '<S75>/LowerRelop1'
- * RelationalOperator: '<S75>/UpperRelop'
- * Switch: '<S22>/Switch'
- * Switch: '<S75>/Switch'
- * Switch: '<S78>/Switch2'
- */
- if (rtb_Switch_oi > rtU->speed_limit) {
- rtb_Switch_oi = rtU->speed_limit;
- } else {
- if (rtb_Switch_oi < 0) {
- /* Switch: '<S75>/Switch' incorporates:
- * Constant: '<S73>/Constant5'
- * Switch: '<S78>/Switch2'
- */
- rtb_Switch_oi = 0;
- }
- }
- /* End of Switch: '<S75>/Switch2' */
- /* Outputs for Atomic SubSystem: '<S73>/pi_speed' */
- rtb_Switch_oi = pi_speed((int16_T)(rtb_Switch_oi - rtb_Switch2_ip), 3174, 10,
- 20, rtb_Divide4_c, (int16_T)-rtb_Divide4_c, 0, rtb_Switch2_fu,
- &rtConstB.pi_speed_g, &rtDW->pi_speed_g, &rtPrevZCX->pi_speed_g);
- /* End of Outputs for SubSystem: '<S73>/pi_speed' */
- /* Merge: '<S50>/Merge' incorporates:
- * Constant: '<S73>/Constant1'
- * Constant: '<S73>/Constant11'
- * Constant: '<S73>/Constant2'
- * Constant: '<S73>/Constant4'
- * Gain: '<S46>/Gain1'
- * Product: '<S46>/Divide1'
- * SignalConversion generated from: '<S73>/iq_target'
- * Sum: '<S73>/Add2'
- * Switch: '<S10>/Switch2'
- * Switch: '<S78>/Switch2'
- */
- rtDW->Merge_b = rtb_Switch_oi;
- /* End of Outputs for SubSystem: '<S50>/speed_mode' */
- } else {
- if ((rtb_DataTypeConversion1_c > 0) && (rtb_z_ctrlMod == 2)) {
- /* Outputs for IfAction SubSystem: '<S50>/torque_mode' incorporates:
- * ActionPort: '<S74>/Action Port'
- */
- /* Product: '<S74>/Divide' incorporates:
- * Constant: '<S74>/Constant2'
- * Sum: '<S74>/Sum2'
- * Switch: '<S10>/Switch2'
- * Switch: '<S22>/Switch'
- */
- sigIdx = ((int16_T)(rtb_Switch_oi - rtb_Switch2_ip) * 819) >> 6;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- /* Product: '<S74>/Divide1' incorporates:
- * Sum: '<S74>/Sum3'
- * Switch: '<S10>/Switch2'
- * Switch: '<S22>/Switch'
- */
- tmp_1 = ((int16_T)(rtb_Switch2_ip - rtb_Switch_oi) * -51) >> 5;
- if (tmp_1 > 32767) {
- tmp_1 = 32767;
- } else {
- if (tmp_1 < -32768) {
- tmp_1 = -32768;
- }
- }
- rtb_Switch_oi = (int16_T)tmp_1;
- /* End of Product: '<S74>/Divide1' */
- /* MinMax: '<S74>/Max' incorporates:
- * Product: '<S74>/Divide'
- * Product: '<S74>/Divide1'
- */
- if ((int16_T)sigIdx > rtb_Switch_oi) {
- rtb_Max = (int16_T)sigIdx;
- } else {
- rtb_Max = rtb_Switch_oi;
- }
- /* End of MinMax: '<S74>/Max' */
- /* MinMax: '<S74>/Max3' incorporates:
- * MinMax: '<S74>/Max'
- * Product: '<S46>/Divide1'
- * Switch: '<S79>/Switch2'
- */
- if (rtb_Divide4_c < rtb_Max) {
- rtb_Max = rtb_Divide4_c;
- }
- /* End of MinMax: '<S74>/Max3' */
- /* Switch: '<S79>/Switch2' incorporates:
- * Product: '<S29>/Divide1'
- * RelationalOperator: '<S79>/LowerRelop1'
- */
- if (rtDW->Divide1 <= rtb_Max) {
- /* MinMax: '<S74>/Max1' incorporates:
- * Product: '<S74>/Divide'
- * Product: '<S74>/Divide1'
- */
- if ((int16_T)sigIdx < rtb_Switch_oi) {
- rtb_Switch_oi = (int16_T)sigIdx;
- }
- /* End of MinMax: '<S74>/Max1' */
- /* MinMax: '<S74>/Max2' incorporates:
- * Gain: '<S46>/Gain1'
- * MinMax: '<S74>/Max1'
- * Product: '<S46>/Divide1'
- */
- if (rtb_Switch_oi <= (int16_T)-rtb_Divide4_c) {
- rtb_Switch_oi = (int16_T)-rtb_Divide4_c;
- }
- /* End of MinMax: '<S74>/Max2' */
- /* Switch: '<S79>/Switch' incorporates:
- * MinMax: '<S74>/Max2'
- * RelationalOperator: '<S79>/UpperRelop'
- */
- if (rtDW->Divide1 < rtb_Switch_oi) {
- rtb_Max = rtb_Switch_oi;
- } else {
- rtb_Max = rtDW->Divide1;
- }
- /* End of Switch: '<S79>/Switch' */
- }
- /* End of Switch: '<S79>/Switch2' */
- /* Merge: '<S50>/Merge' incorporates:
- * SignalConversion generated from: '<S74>/torque_iq'
- * Switch: '<S79>/Switch2'
- */
- rtDW->Merge_b = rtb_Max;
- /* End of Outputs for SubSystem: '<S50>/torque_mode' */
- }
- }
- /* End of If: '<S50>/If' */
- /* If: '<S47>/If' incorporates:
- * Constant: '<S47>/Constant3'
- * Constant: '<S57>/Constant3'
- * Constant: '<S57>/Constant4'
- * Constant: '<S57>/Constant6'
- * Constant: '<S57>/Constant9'
- * Constant: '<S58>/Constant1'
- * Constant: '<S58>/Constant7'
- * Constant: '<S58>/Constant8'
- * Gain: '<S46>/Gain3'
- * Gain: '<S46>/Gain5'
- * If: '<S47>/If1'
- * Inport: '<Root>/vbus_voltage'
- * Interpolation_n-D: '<S46>/Vq_max_M1'
- * Sum: '<S57>/Add'
- * Sum: '<S58>/Add1'
- * Switch: '<S60>/Switch2'
- * Switch: '<S64>/Switch2'
- */
- if (rtb_Switch2_fu == 1) {
- /* Outputs for IfAction SubSystem: '<S47>/iq_ctrl' incorporates:
- * ActionPort: '<S58>/Action Port'
- */
- /* Switch: '<S64>/Switch2' incorporates:
- * Merge: '<S50>/Merge'
- * Product: '<S46>/Divide1'
- * RelationalOperator: '<S64>/LowerRelop1'
- */
- if (rtDW->Merge_b <= rtb_Divide4_c) {
- /* Switch: '<S64>/Switch' incorporates:
- * Gain: '<S46>/Gain1'
- * RelationalOperator: '<S64>/UpperRelop'
- * Switch: '<S64>/Switch2'
- */
- if (rtDW->Merge_b < (int16_T)-rtb_Divide4_c) {
- rtb_Divide4_c = (int16_T)-rtb_Divide4_c;
- } else {
- rtb_Divide4_c = rtDW->Merge_b;
- }
- /* End of Switch: '<S64>/Switch' */
- }
- /* End of Switch: '<S64>/Switch2' */
- /* Outputs for Atomic SubSystem: '<S58>/PI_iq' */
- PI_iq((int16_T)(rtb_Divide4_c - rtb_DataTypeConversion[1]), 4096, 51, 1024,
- rtb_Divide1_fi, (int16_T)-rtb_Divide1_fi, 0, &rtDW->Switch2_m,
- &rtDW->PI_iq_g);
- /* End of Outputs for SubSystem: '<S58>/PI_iq' */
- /* End of Outputs for SubSystem: '<S47>/iq_ctrl' */
- /* Outputs for IfAction SubSystem: '<S47>/id_ctrl' incorporates:
- * ActionPort: '<S57>/Action Port'
- */
- /* Switch: '<S60>/Switch2' incorporates:
- * Constant: '<S47>/Constant3'
- * Constant: '<S58>/Constant1'
- * Constant: '<S58>/Constant7'
- * Constant: '<S58>/Constant8'
- * Gain: '<S46>/Gain4'
- * Gain: '<S46>/Gain5'
- * Inport: '<Root>/i_dc_limit'
- * Interpolation_n-D: '<S46>/Vq_max_M1'
- * Product: '<S24>/Divide3'
- * RelationalOperator: '<S60>/LowerRelop1'
- * RelationalOperator: '<S60>/UpperRelop'
- * Sum: '<S58>/Add1'
- * Switch: '<S60>/Switch'
- * Switch: '<S64>/Switch2'
- */
- if (rtDW->Divide3 > rtU->i_dc_limit) {
- rtb_Switch_oi = rtU->i_dc_limit;
- } else if (rtDW->Divide3 < (int16_T)-rtU->i_dc_limit) {
- /* Switch: '<S60>/Switch' incorporates:
- * Gain: '<S46>/Gain4'
- * Switch: '<S60>/Switch2'
- */
- rtb_Switch_oi = (int16_T)-rtU->i_dc_limit;
- } else {
- rtb_Switch_oi = rtDW->Divide3;
- }
- /* End of Switch: '<S60>/Switch2' */
- /* Outputs for Atomic SubSystem: '<S57>/PI_id' */
- PI_id((int16_T)(rtb_Switch_oi - rtb_DataTypeConversion[0]), 4096, 51, 1024,
- rtU->vbus_voltage, (int16_T)-rtU->vbus_voltage, 0, &rtDW->Switch2,
- &rtDW->PI_id_b);
- /* End of Outputs for SubSystem: '<S57>/PI_id' */
- /* End of Outputs for SubSystem: '<S47>/id_ctrl' */
- }
- /* End of If: '<S47>/If' */
- /* Switch: '<S5>/Switch1' incorporates:
- * Switch: '<S5>/Switch'
- * Switch: '<S62>/Switch2'
- * Switch: '<S66>/Switch2'
- */
- if (rtb_z_ctrlMod != 0) {
- rtb_Switch_oi = rtDW->Switch2_m;
- rtb_Divide1_fi = rtDW->Switch2;
- } else {
- rtb_Switch_oi = rtDW->Merge[1];
- rtb_Divide1_fi = rtDW->Merge[0];
- }
- /* End of Switch: '<S5>/Switch1' */
- /* Sum: '<S48>/Sum1' incorporates:
- * Interpolation_n-D: '<S56>/r_cos_M1'
- * Product: '<S48>/Divide2'
- * Product: '<S48>/Divide3'
- * Product: '<S67>/Divide4'
- * Switch: '<S5>/Switch'
- * Switch: '<S5>/Switch1'
- */
- sigIdx = (int16_T)((rtb_Divide1_fi * rtb_Abs5_h) >> 14) + (int16_T)
- ((rtb_Switch_oi * rtb_Switch_dr) >> 14);
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- /* Sum: '<S48>/Sum6' incorporates:
- * Interpolation_n-D: '<S56>/r_cos_M1'
- * Product: '<S48>/Divide1'
- * Product: '<S48>/Divide4'
- * Product: '<S67>/Divide4'
- * Switch: '<S5>/Switch'
- * Switch: '<S5>/Switch1'
- */
- tmp_1 = (int16_T)((rtb_Divide1_fi * rtb_Switch_dr) >> 14) - (int16_T)
- ((rtb_Switch_oi * rtb_Abs5_h) >> 14);
- if (tmp_1 > 32767) {
- tmp_1 = 32767;
- } else {
- if (tmp_1 < -32768) {
- tmp_1 = -32768;
- }
- }
- /* Product: '<S67>/Divide3' incorporates:
- * Constant: '<S67>/Constant1'
- * Product: '<S67>/Divide'
- * Sum: '<S48>/Sum6'
- */
- rtb_Switch_dr = (int16_T)((3547 * (int16_T)tmp_1) >> 12);
- /* Product: '<S67>/Divide2' incorporates:
- * Constant: '<S67>/Constant'
- * Sum: '<S48>/Sum1'
- */
- rtb_Max = (int16_T)((3547 * (int16_T)sigIdx) >> 12);
- /* Product: '<S67>/Divide4' incorporates:
- * Constant: '<S67>/Constant2'
- * Product: '<S67>/Divide2'
- */
- rtb_Abs5_h = (int16_T)((2365 * rtb_Max) >> 12);
- /* Sum: '<S67>/Add' incorporates:
- * Product: '<S67>/Divide'
- * Product: '<S67>/Divide4'
- */
- rtb_Gain1 = (int16_T)((rtb_Switch_dr + rtb_Abs5_h) >> 1);
- /* Sum: '<S67>/Add1' incorporates:
- * Product: '<S67>/Divide'
- * Product: '<S67>/Divide4'
- */
- rtb_Gain4 = (int16_T)((rtb_Abs5_h - rtb_Switch_dr) >> 1);
- /* Product: '<S67>/Divide7' incorporates:
- * Constant: '<S67>/Constant3'
- * Sum: '<S48>/Sum1'
- */
- rtb_Abs5_h = (int16_T)((2365 * (int16_T)sigIdx) >> 12);
- /* MATLAB Function: '<S67>/sector_select' incorporates:
- * Product: '<S67>/Divide7'
- * Sum: '<S48>/Sum1'
- * Sum: '<S48>/Sum6'
- */
- if ((int16_T)sigIdx >= 0) {
- if ((int16_T)tmp_1 >= 0) {
- if (rtb_Abs5_h > (int16_T)tmp_1) {
- /* DataTypeConversion: '<S67>/Data Type Conversion' */
- rtb_DataTypeConversion1_c = 2U;
- } else {
- /* DataTypeConversion: '<S67>/Data Type Conversion' */
- rtb_DataTypeConversion1_c = 1U;
- }
- } else if (-rtb_Abs5_h > (int16_T)tmp_1) {
- /* DataTypeConversion: '<S67>/Data Type Conversion' */
- rtb_DataTypeConversion1_c = 3U;
- } else {
- /* DataTypeConversion: '<S67>/Data Type Conversion' */
- rtb_DataTypeConversion1_c = 2U;
- }
- } else if ((int16_T)tmp_1 >= 0) {
- if (-rtb_Abs5_h > (int16_T)tmp_1) {
- /* DataTypeConversion: '<S67>/Data Type Conversion' */
- rtb_DataTypeConversion1_c = 5U;
- } else {
- /* DataTypeConversion: '<S67>/Data Type Conversion' */
- rtb_DataTypeConversion1_c = 6U;
- }
- } else if (rtb_Abs5_h > (int16_T)tmp_1) {
- /* DataTypeConversion: '<S67>/Data Type Conversion' */
- rtb_DataTypeConversion1_c = 4U;
- } else {
- /* DataTypeConversion: '<S67>/Data Type Conversion' */
- rtb_DataTypeConversion1_c = 5U;
- }
- /* End of MATLAB Function: '<S67>/sector_select' */
- /* Product: '<S67>/Divide' incorporates:
- * Inport: '<Root>/vbus_voltage'
- */
- rtb_Switch_dr = (int16_T)div_nde_s32_floor(40960000, rtU->vbus_voltage);
- /* Product: '<S67>/Divide1' incorporates:
- * Product: '<S67>/Divide'
- * Product: '<S67>/Divide2'
- * Product: '<S67>/Divide8'
- */
- rtb_Abs5_h = (int16_T)((((2365 * rtb_Max) >> 13) * rtb_Switch_dr) >> 10);
- /* Product: '<S67>/Divide5' incorporates:
- * Product: '<S67>/Divide'
- * Sum: '<S67>/Add'
- */
- rtb_Divide4_c = (int16_T)((rtb_Gain1 * rtb_Switch_dr) >> 11);
- /* Product: '<S67>/Divide6' incorporates:
- * Product: '<S67>/Divide'
- * Sum: '<S67>/Add1'
- */
- rtb_Gain1 = (int16_T)((rtb_Gain4 * rtb_Switch_dr) >> 11);
- /* MATLAB Function: '<S67>/phase_time' incorporates:
- * DataTypeConversion: '<S67>/Data Type Conversion1'
- */
- switch ((int8_T)rtb_DataTypeConversion1_c) {
- case 1:
- sigIdx = -rtb_Gain1;
- if (-rtb_Gain1 > 32767) {
- sigIdx = 32767;
- }
- tmp_1 = 10000 - (int16_T)sigIdx;
- if (10000 - (int16_T)sigIdx > 32767) {
- tmp_1 = 32767;
- }
- tmp_1 -= rtb_Abs5_h;
- if (tmp_1 > 32767) {
- tmp_1 = 32767;
- } else {
- if (tmp_1 < -32768) {
- tmp_1 = -32768;
- }
- }
- rtb_Switch_dr = (int16_T)rt_roundd_snf((real_T)tmp_1 / 4.0);
- rtb_Abs5_h = (int16_T)((int32_T)rt_roundd_snf((real_T)rtb_Abs5_h / 2.0) +
- rtb_Switch_dr);
- sigIdx = (int32_T)rt_roundd_snf((real_T)(int16_T)sigIdx / 2.0) + rtb_Abs5_h;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- rtb_Divide4_c = (int16_T)sigIdx;
- break;
- case 2:
- sigIdx = 10000 - rtb_Divide4_c;
- if (10000 - rtb_Divide4_c > 32767) {
- sigIdx = 32767;
- }
- sigIdx -= rtb_Gain1;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- rtb_Switch_dr = (int16_T)rt_roundd_snf((real_T)sigIdx / 4.0);
- rtb_Divide4_c = (int16_T)((int32_T)rt_roundd_snf((real_T)rtb_Divide4_c / 2.0)
- + rtb_Switch_dr);
- sigIdx = (int32_T)rt_roundd_snf((real_T)rtb_Gain1 / 2.0) + rtb_Divide4_c;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- rtb_Abs5_h = (int16_T)sigIdx;
- break;
- case 3:
- sigIdx = -rtb_Divide4_c;
- if (-rtb_Divide4_c > 32767) {
- sigIdx = 32767;
- }
- tmp_1 = 10000 - rtb_Abs5_h;
- if (10000 - rtb_Abs5_h > 32767) {
- tmp_1 = 32767;
- }
- tmp_1 -= (int16_T)sigIdx;
- if (tmp_1 > 32767) {
- tmp_1 = 32767;
- } else {
- if (tmp_1 < -32768) {
- tmp_1 = -32768;
- }
- }
- rtb_Divide4_c = (int16_T)rt_roundd_snf((real_T)tmp_1 / 4.0);
- rtb_Switch_dr = (int16_T)((int32_T)rt_roundd_snf((real_T)(int16_T)sigIdx /
- 2.0) + rtb_Divide4_c);
- sigIdx = (int32_T)rt_roundd_snf((real_T)rtb_Abs5_h / 2.0) + rtb_Switch_dr;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- rtb_Abs5_h = (int16_T)sigIdx;
- break;
- case 4:
- sigIdx = -rtb_Abs5_h;
- if (-rtb_Abs5_h > 32767) {
- sigIdx = 32767;
- }
- tmp_1 = 10000 - (int16_T)sigIdx;
- if (10000 - (int16_T)sigIdx > 32767) {
- tmp_1 = 32767;
- }
- tmp_1 -= rtb_Gain1;
- if (tmp_1 > 32767) {
- tmp_1 = 32767;
- } else {
- if (tmp_1 < -32768) {
- tmp_1 = -32768;
- }
- }
- rtb_Divide4_c = (int16_T)rt_roundd_snf((real_T)tmp_1 / 4.0);
- rtb_Abs5_h = (int16_T)((int32_T)rt_roundd_snf((real_T)rtb_Gain1 / 2.0) +
- rtb_Divide4_c);
- sigIdx = (int32_T)rt_roundd_snf((real_T)(int16_T)sigIdx / 2.0) + rtb_Abs5_h;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- rtb_Switch_dr = (int16_T)sigIdx;
- break;
- case 5:
- sigIdx = -rtb_Divide4_c;
- if (-rtb_Divide4_c > 32767) {
- sigIdx = 32767;
- }
- tmp_1 = -rtb_Gain1;
- if (-rtb_Gain1 > 32767) {
- tmp_1 = 32767;
- }
- tmp = 10000 - (int16_T)sigIdx;
- if (10000 - (int16_T)sigIdx > 32767) {
- tmp = 32767;
- }
- tmp -= (int16_T)tmp_1;
- if (tmp > 32767) {
- tmp = 32767;
- } else {
- if (tmp < -32768) {
- tmp = -32768;
- }
- }
- rtb_Abs5_h = (int16_T)rt_roundd_snf((real_T)tmp / 4.0);
- rtb_Divide4_c = (int16_T)((int32_T)rt_roundd_snf((real_T)(int16_T)tmp_1 /
- 2.0) + rtb_Abs5_h);
- sigIdx = (int32_T)rt_roundd_snf((real_T)(int16_T)sigIdx / 2.0) +
- rtb_Divide4_c;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- rtb_Switch_dr = (int16_T)sigIdx;
- break;
- default:
- sigIdx = -rtb_Abs5_h;
- if (-rtb_Abs5_h > 32767) {
- sigIdx = 32767;
- }
- tmp_1 = 10000 - rtb_Divide4_c;
- if (10000 - rtb_Divide4_c > 32767) {
- tmp_1 = 32767;
- }
- tmp_1 -= (int16_T)sigIdx;
- if (tmp_1 > 32767) {
- tmp_1 = 32767;
- } else {
- if (tmp_1 < -32768) {
- tmp_1 = -32768;
- }
- }
- rtb_Abs5_h = (int16_T)rt_roundd_snf((real_T)tmp_1 / 4.0);
- rtb_Switch_dr = (int16_T)((int32_T)rt_roundd_snf((real_T)(int16_T)sigIdx /
- 2.0) + rtb_Abs5_h);
- sigIdx = (int32_T)rt_roundd_snf((real_T)rtb_Divide4_c / 2.0) + rtb_Switch_dr;
- if (sigIdx > 32767) {
- sigIdx = 32767;
- } else {
- if (sigIdx < -32768) {
- sigIdx = -32768;
- }
- }
- rtb_Divide4_c = (int16_T)sigIdx;
- break;
- }
- /* Switch: '<S69>/Switch2' incorporates:
- * Constant: '<S67>/Constant6'
- * MATLAB Function: '<S67>/phase_time'
- * RelationalOperator: '<S69>/LowerRelop1'
- * RelationalOperator: '<S69>/UpperRelop'
- * Switch: '<S69>/Switch'
- */
- if (rtb_Divide4_c > 5000) {
- rtb_LogicalOperator3 = 5000U;
- } else if (rtb_Divide4_c < 0) {
- /* Switch: '<S69>/Switch' incorporates:
- * Constant: '<S67>/Constant5'
- */
- rtb_LogicalOperator3 = 0U;
- } else {
- rtb_LogicalOperator3 = (uint16_T)rtb_Divide4_c;
- }
- if (rtb_Abs5_h > 5000) {
- rtb_Switch2_idx_1 = 5000U;
- } else if (rtb_Abs5_h < 0) {
- /* Switch: '<S69>/Switch' incorporates:
- * Constant: '<S67>/Constant5'
- */
- rtb_Switch2_idx_1 = 0U;
- } else {
- rtb_Switch2_idx_1 = (uint16_T)rtb_Abs5_h;
- }
- if (rtb_Switch_dr > 5000) {
- rtb_Switch2_idx_2 = 5000U;
- } else if (rtb_Switch_dr < 0) {
- /* Switch: '<S69>/Switch' incorporates:
- * Constant: '<S67>/Constant5'
- */
- rtb_Switch2_idx_2 = 0U;
- } else {
- rtb_Switch2_idx_2 = (uint16_T)rtb_Switch_dr;
- }
- /* End of Switch: '<S69>/Switch2' */
- /* MultiPortSwitch: '<S68>/Multiport Switch' */
- switch (rtb_DataTypeConversion1_c) {
- case 1:
- rtb_MultiportSwitch_idx_0 = rtb_LogicalOperator3;
- rtb_MultiportSwitch_idx_1 = rtb_Switch2_idx_1;
- break;
- case 2:
- rtb_MultiportSwitch_idx_0 = rtb_Switch2_idx_1;
- rtb_MultiportSwitch_idx_1 = rtb_LogicalOperator3;
- break;
- case 3:
- rtb_MultiportSwitch_idx_0 = rtb_Switch2_idx_1;
- rtb_MultiportSwitch_idx_1 = rtb_Switch2_idx_2;
- break;
- case 4:
- rtb_MultiportSwitch_idx_0 = rtb_Switch2_idx_2;
- rtb_MultiportSwitch_idx_1 = rtb_Switch2_idx_1;
- break;
- case 5:
- rtb_MultiportSwitch_idx_0 = rtb_Switch2_idx_2;
- rtb_MultiportSwitch_idx_1 = rtb_LogicalOperator3;
- break;
- default:
- rtb_MultiportSwitch_idx_0 = rtb_LogicalOperator3;
- rtb_MultiportSwitch_idx_1 = rtb_Switch2_idx_2;
- break;
- }
- /* End of MultiPortSwitch: '<S68>/Multiport Switch' */
- /* Outport: '<Root>/PWM' incorporates:
- * Constant: '<S68>/Constant'
- * Constant: '<S68>/Constant1'
- * Constant: '<S68>/Constant2'
- * Constant: '<S68>/Constant3'
- * MATLAB Function: '<S68>/Shunt_Three_Sample_Point'
- * Outport: '<Root>/sector'
- */
- Shunt_Three_Sample_Point(rtb_MultiportSwitch_idx_0, rtb_MultiportSwitch_idx_1,
- rtb_DataTypeConversion1_c, 5000, 10, 10, 10, &rtY->PWM[6], &rtY->PWM[7],
- &rtY->sector);
- /* Update for Delay: '<S7>/Delay' incorporates:
- * Inport: '<Root>/hall_a'
- */
- rtDW->Delay_DSTATE = rtU->hall_a;
- /* Update for Delay: '<S7>/Delay1' incorporates:
- * Inport: '<Root>/hall_b'
- */
- rtDW->Delay1_DSTATE = rtU->hall_b;
- /* Update for Delay: '<S7>/Delay2' incorporates:
- * Inport: '<Root>/hall_c'
- */
- rtDW->Delay2_DSTATE = rtU->hall_c;
- /* Update for UnitDelay: '<S10>/UnitDelay3' incorporates:
- * Inport: '<Root>/hw_count'
- */
- rtDW->UnitDelay3_DSTATE = rtU->hw_count;
- /* Update for UnitDelay: '<S10>/UnitDelay4' incorporates:
- * Abs: '<S10>/Abs5'
- */
- rtDW->UnitDelay4_DSTATE = rtb_Abs5;
- /* Update for UnitDelay: '<S34>/UnitDelay' */
- rtDW->UnitDelay_DSTATE_j = rtb_UnitDelay;
- /* Update for UnitDelay: '<S5>/UnitDelay2' incorporates:
- * Switch: '<S5>/Switch1'
- */
- rtDW->UnitDelay2_DSTATE_p = rtb_Switch_oi;
- /* Update for UnitDelay: '<S5>/UnitDelay1' incorporates:
- * Switch: '<S5>/Switch'
- */
- rtDW->UnitDelay1_DSTATE = rtb_Divide1_fi;
- /* Update for UnitDelay: '<S80>/Unit Delay' incorporates:
- * Delay: '<S80>/Delay'
- */
- rtDW->UnitDelay_DSTATE_f = rtDW->Delay_DSTATE_n[0];
- /* Update for Delay: '<S80>/Delay' incorporates:
- * Logic: '<S80>/Logical Operator1'
- */
- for (sigIdx = 0; sigIdx < 19; sigIdx++) {
- rtDW->Delay_DSTATE_n[sigIdx] = rtDW->Delay_DSTATE_n[sigIdx + 1];
- }
- rtDW->Delay_DSTATE_n[19] = !rtb_RelationalOperator4_f;
- /* End of Update for Delay: '<S80>/Delay' */
- /* End of Outputs for SubSystem: '<Root>/PMSM_Controller' */
- /* Outport: '<Root>/PWM' */
- rtY->PWM[0] = rtb_LogicalOperator3;
- rtY->PWM[3] = rtb_LogicalOperator3;
- rtY->PWM[1] = rtb_Switch2_idx_1;
- rtY->PWM[4] = rtb_Switch2_idx_1;
- rtY->PWM[2] = rtb_Switch2_idx_2;
- rtY->PWM[5] = rtb_Switch2_idx_2;
- /* Outport: '<Root>/n_MotError' */
- rtY->n_MotError = rtb_UnitDelay;
- /* Outport: '<Root>/iq' */
- rtY->iq = rtb_DataTypeConversion[1];
- /* Outport: '<Root>/id' */
- rtY->id = rtb_DataTypeConversion[0];
- /* Outport: '<Root>/angle' incorporates:
- * Switch: '<S2>/Switch'
- */
- rtY->angle = rtb_Switch_b;
- /* Outport: '<Root>/rpm' incorporates:
- * Switch: '<S10>/Switch2'
- */
- rtY->rpm = rtb_Switch2_ip;
- /* Outport: '<Root>/hall_angle' incorporates:
- * Merge: '<S11>/Merge'
- */
- rtY->hall_angle = rtb_Switch3_c;
- /* Outport: '<Root>/hall_state' */
- rtY->hall_state = rtb_Add_k;
- }
- /* Model initialize function */
- void PMSM_Controller_initialize(RT_MODEL *const rtM)
- {
- DW *rtDW = rtM->dwork;
- PrevZCX *rtPrevZCX = rtM->prevZCSigState;
- {
- int32_T i;
- rtPrevZCX->pi_speed_g.ResettableDelay_Reset_ZCE = POS_ZCSIG;
- /* SystemInitialize for Atomic SubSystem: '<Root>/PMSM_Controller' */
- /* InitializeConditions for Delay: '<S80>/Delay' */
- for (i = 0; i < 20; i++) {
- rtDW->Delay_DSTATE_n[i] = true;
- }
- /* End of InitializeConditions for Delay: '<S80>/Delay' */
- /* SystemInitialize for IfAction SubSystem: '<S10>/Raw_Motor_Speed_Estimation' */
- /* SystemInitialize for Outport: '<S15>/z_counter' incorporates:
- * Inport: '<S15>/z_counterRawPrev'
- */
- rtDW->z_counterRawPrev = 200000U;
- /* End of SystemInitialize for SubSystem: '<S10>/Raw_Motor_Speed_Estimation' */
- /* SystemInitialize for Atomic SubSystem: '<S34>/Debounce_Filter' */
- Debounce_Filter_Init(&rtDW->Debounce_Filter_i);
- /* End of SystemInitialize for SubSystem: '<S34>/Debounce_Filter' */
- /* SystemInitialize for IfAction SubSystem: '<S50>/speed_mode' */
- /* SystemInitialize for Atomic SubSystem: '<S73>/pi_speed' */
- pi_speed_Init(&rtDW->pi_speed_g);
- /* End of SystemInitialize for SubSystem: '<S73>/pi_speed' */
- /* End of SystemInitialize for SubSystem: '<S50>/speed_mode' */
- /* End of SystemInitialize for SubSystem: '<Root>/PMSM_Controller' */
- }
- }
- /*
- * File trailer for generated code.
- *
- * [EOF]
- */
|