⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fuzzy_ni1o.tlc

📁 simulink real-time workshop for dragon12 development board from
💻 TLC
字号:
%% File : fuzzy_ni1o.tlc
%%
%%
%implements  fuzzy_ni1o "C"

%% P1 SAMPLE_TIME      /* Sample time in seconds */
%%
%% === RTWdata ===
%% 'InputMSFs'			textBlock
%% 'OutputMSFs'			textBlock
%% 'InputMSFdef'		textBlock
%% 'OutputSingletons'	textBlock
%% 'Rules'				textBlock
%% 'blockName'			...
%% 'numMSFsInput1'      ...
%% 'numMSFsInput2'      ...
%% 'numMSFsInput3'      ...
%% 'numMSFsOutput'      ...

%% add required include files... -- fw-09-06
%<LibAddToCommonIncludes("<mc9s12dp256.h>")>
%<LibAddToCommonIncludes("tmwtypes.h")>


%% Function: BlockTypeSetup =================================================
%%
%% Purpose:
%%      fuzzy_ni1o block setup code.
%%
%function BlockTypeSetup(block, system) void

  %% Place function prototypes in the model's header file
  
  %openfile buffer
	/* declare function Fuzzy_%<block.RTWdata.blockName>() */
	%%
	%if %<block.RTWdata.numInputs> == 1
	extern short Fuzzy_%<block.RTWdata.blockName>(short x1);
	%endif
	%%
	%if %<block.RTWdata.numInputs> == 2
	extern short Fuzzy_%<block.RTWdata.blockName>(short x2, short x1);
	%endif
	%%
	%if %<block.RTWdata.numInputs> == 3
	extern short Fuzzy_%<block.RTWdata.blockName>(short x3, short x2, short x1);
	%endif
	%%
  %closefile buffer
    
  %<LibCacheFunctionPrototype(buffer)>


  %openfile fuzzEng = "fuzzyEngine.asm"     /% Opens "fuzzyEngine.asm" for writing. %/
; ********* FuzzyEngine.asm ******************
; automatically generated by fuzzy_ni1o.tlc --  (fw-09-06)

RAM:     section

; -------------------------------------------------------------
; Fuzzy Membership sets
; -------------------------------------------------------------
; input membership variables
%<block.RTWdata.InputMSFs>

;output membership variables
%<block.RTWdata.OutputMSFs>
; -------------------------------------------------------------


EEPROM:   section  

; -------------------------------------------------------------
; fuzzification
; -------------------------------------------------------------
%<block.RTWdata.InputMSFdef>
; -------------------------------------------------------------


; -------------------------------------------------------------
; rule base
; -------------------------------------------------------------
%<block.RTWdata.blockName>_rules:
%<block.RTWdata.Rules>
; -------------------------------------------------------------


; -------------------------------------------------------------
; defuzzification
; -------------------------------------------------------------
%<block.RTWdata.blockName>_output_sgtn:
     ; MoM -- Mean of Maximum (using 0 ... 255 range)
%<block.RTWdata.OutputSingletons>
; -------------------------------------------------------------


; -------------------------------------------------------------
; FuzzyController
; order of call-up parameters:  FuzzyController(x3, x2, x1)
;
; Register 'B':          8-bit input x3, 0 to 255, 128 -> 0
; Stack parameter 6,sp:  8-bit input x2, 0 to 255, 128 -> 0
; Stack parameter 8,sp:  8-bit input x1, 0 to 255, 128 -> 0
; Returns dU:            8-bit output, -128 to +127
; -------------------------------------------------------------
          absentry Fuzzy_%<block.RTWdata.blockName>
Fuzzy_%<block.RTWdata.blockName>:
    pshx
	%%
	%if %<block.RTWdata.numInputs> == 2
	pshb      		; x2 is passed into the function in register 'B'
    ldaa 6,sp		; x1
	%endif
	%if %<block.RTWdata.numInputs> == 3
	pshb      		; x3 is passed into the function in register 'B'
    ldaa 8,sp		; x1
	%endif
    %%
    %%
    ldx  #%<block.RTWdata.blockName>_x1_tab
    ldy  #%<block.RTWdata.blockName>_fuzvar
	%%
	%foreach kk = %<block.RTWdata.numMSFsInput1>
    mem				; Input1, MSF%<kk+1>
	%endforeach
	%%
	%%
	%if %<block.RTWdata.numInputs> == 2
    pula		    ; x2
	%endif
	%if %<block.RTWdata.numInputs> == 3
    ldaa 6,sp		; x2
	%endif
	%%
	%if %<block.RTWdata.numInputs> > 1
    ldx  #%<block.RTWdata.blockName>_x2_tab
	%%
	%foreach kk = %<block.RTWdata.numMSFsInput2>
    mem				; Input2, MSF%<kk+1>
	%endforeach
	%endif
	%%
	%%
	%if %<block.RTWdata.numInputs> > 2
    pula			; x3
    ldx  #%<block.RTWdata.blockName>_x3_tab
	%foreach kk = %<block.RTWdata.numMSFsInput3>
    mem				; Input3, MSF%<kk+1>
	%endforeach
	%endif
	%%
	%%
	%%
    ldab #%<block.RTWdata.numMSFsOutput>     	; number of output fuzzy membership sets
cloop:
    clr  1,y+   	; clear output fuzzy variables
    dbne b,cloop
    ldx  #%<block.RTWdata.blockName>_rules
    ldy  #%<block.RTWdata.blockName>_fuzvar
    ldaa #$FF
    rev
    ldy  #%<block.RTWdata.blockName>_fuzout
    ldx  #%<block.RTWdata.blockName>_output_sgtn
    ldab #%<block.RTWdata.numMSFsOutput>
    wav
    ediv         	; Y has weighted average, 0 to 255
    leay -128,y  	; adjust for offset, -128 to +127
    tfr  y,d     	; return dU
    pulx
    rts

  %closefile fuzzEng

%endfunction



%% Function: Start ==========================================================
%%
%% Purpose:
%%      RFComms Server communication initialization code.
%%
%function Start(block, system) Output

%endfunction


%% Function: Outputs ==========================================================
%%
%% Purpose:
%%      Code generation rules for mdlOutputs function.
%%
%function Outputs(block, system) Output

	%assign u1 = LibBlockInputSignal(0, "", "", 0)
	%assign u2 = LibBlockInputSignal(1, "", "", 0)
	%assign y = LibBlockOutputSignal(0, "", "", 0)
	
	/* S-Function "fuzzy_ni1o" Block: %<Name> */
	{
		
		%foreach kk = %<block.RTWdata.numInputs>
		short x%<kk+1>;					/* 0 to 255 (8-bit), 128 means 'zero' */
		%endforeach
		
		%foreach kk = %<block.RTWdata.numInputs>
		/* scale input 'x%<kk+1>':
		   - add offset (an input value of -128 becomes 0, an input value of +127 becomes 255)
		   - ensure that adjusted value is within the valid bounds (0 ... 255)
		*/
		x%<kk+1> = (int)(%<u1> * 255) + 128;
		if(x%<kk+1> > 255)  x%<kk+1> = 255;		/* make sure data is within 8-bit bounds */
		if(x%<kk+1> < 0)    x%<kk+1> = 0;		/* 0 <= data <= +255 */
		%endforeach
		
		/* fuzzy inference, output: -128 ... 127 */
		%if %<block.RTWdata.numInputs> == 1
		%<y> = (real_T)Fuzzy_%<block.RTWdata.blockName>(x1);
		%endif
		
		%if %<block.RTWdata.numInputs> == 2
		%<y> = (real_T)Fuzzy_%<block.RTWdata.blockName>(x2, x1);
		%endif
		
		%if %<block.RTWdata.numInputs> == 3
		%<y> = (real_T)Fuzzy_%<block.RTWdata.blockName>(x3, x2, x1);
		%endif
		
	}
  
%endfunction


%% Function: Terminate ==========================================================
%%
%% Purpose:
%%      Code generation rules for mdlTerminate function.
%%
%function Terminate(block, system) Output
	
%endfunction

%% [EOF] fuzzy_ni1o.tlc

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -