You are on page 1of 41

Li ni u

Trong lnh vc truyn thng, cc qu trnh m ha v gii m c s dng nhiu vi cc chc nng khc nhau nhm m bo tnh bo mt, tin cy cao khi truyn tin. Do , vic xy dng cc phng php m ha, gii m, vic chn la cc tham s cho b to m (encoder) v cc b gii m (decoder) l rt quan trng trong truyn tin an ton. Cho n nay, thng thng cc phng php m ha v gii m ch c phn tch trn c s phng php x l tn hiu truyn thng, biu din theo thi gian, tn s hay khng gian. Tuy vy, d c phn tch trn c s no, th vic thit lp cc b to m hay gii m u c thc hin da vo cc m hnh khi bc ban u. Hay ni cch khc, vic thit lp cc b to m hay gii m chnh l vic xy dng mt m hnh h thng vi u vo l tin tc, u ra l cc t m v cc bin trng thi c trong h thng. V vy, vic nhn nhn m ha v gii m trn quan im l thuyt h thng l c c s thc hin. c bit, vic nghin cum sa sai trn quan im l thuyt h thng c tm quan trng i vi vic tm cc m c kh nng sa sai tt, cn t bit d tha, hay tm ra cc m mnh kt hp vi kh nng quan st, iu khin ng thi ti u ha qu trnh m ha v gii m. Trong khun kh ca mn hc, m chp c s dng phn tch v lm r thm v vic biu din m trong khng gian trng thi. Ngoi ra, project cn biu din mt b to m v gii m chp s dng phn mm Matlab2010 nhm c ci nhn r hn v qu trnh to m v gii m. Do hn ch v thi gian tm hiu nn ti cn nhiu thiu st v knh mong c ch bo ti c b sung trong thi gian ti.

Page 1

Mc Lc
Chng 1.L thuyt v m chp............................................................................................ 3 1.1. 1.2. B to m chp ................................................................................................. 3 B gii m chp................................................................................................. 5

Chng 2: M phng b m ha v gii m m chp ............................................................. 8 2.1. 2.2. M hnh ca b m ha ..................................................................................... 8 Kt qu m phng ........................................................................................... 10

Kt lun ............................................................................................................................. 12 Ti liu tham kho ............................................................................................................. 13

Page 2

Chng 1.L thuyt v m chp


1.1. B to m chp M chp l mt loi m sa sai c s dng trong m ha knh vi t m c to ra nh vic cng cc bit thng tin v cc bit c trong cc thanh ghi dch. to ra m chp, ta dng b lp m chp c nguyn tc hot ng da vo vic chp dng bit thng tin u vo vi p ng xung ca b to m. Do , mt dy thng tin nh phn u c chia thnh k t dk=(dk(0),..,dk(k-1)). Cc k t ny c nh x thng qua b to m gm n bit. Hai tham s quan trng ca mt t m chp l tc m R = k/n v di rng buc Kc = M+1 vi M l s lng cc thanh ghi dch c trong b m ( di rng buc l thi gian tri rng ca cc bit m mi bt u vo nh hng ln). Hnh 1 m t mt b gii m c tc l , di rng buc Kc = 3.

Hnh 1. S khi v s chuyn trng thi ca b to m chp Hnh 1 cng ch ra s chuyn trng thi tng ng vi cc thanh ghi dch pha bn tri.Vi cc m chp, vic m t b to m bng s trng thi s thun li hn. Nu b lp m c M thanh ghi dch th s c 2M trng thi S(m) vi m = 0,,2M-1. Mi trng thi c biu din bi mt nt v s chuyn i cc trng thi c biu din bng cc mi tn. Trn cc mi tn m t bit u vo dk tng ng vi cc k t u ra xk c vit l dk/xk(0)x(1)k. Biu mt co (trellis diagram) trong hnh 2 m t s chuyn i cc trng thi theo thi gian. Vi mi u vo dk, biu mt co s c tt c cc kh nng dch chuyn trng thi c th xy ra.

Page 3

Hnh 2. Biu mt co th hin qu trnh chuyn trng thi theo thi gian

Gi s trng thi ban u ca b to m l trng thi S(0). Biu mt co c th c chia thnh 3 pha: Vi mt bn tin c chiu di l L, sau pha khi to ca M bit c m ha dk, s c L M segment ging h nhau. Sau khi bn tin c chiu di L c m ha ht, th b to m s chuyn v trng thi ban u bng cch thm vo M k t. Do , chiu di ca t m c tng ln LT = L + M. K thut thm M k t ny s lm ci thin kh nng b li ti im cui ca bn tin c m ha. hiu r hn v biu mt co, ta xem xt hnh 3. Hnh 3 biu din qu trnh to t m s dng biu mt co vi bn tin c di 12 bit vi ni dung d= (0,1,0,1,1,0,0,1,1,1,0,0) v c 24 bit t m u ra xk

Hnh 3. V d v s dng biu mt co vi u vo cho trc

Page 4

1.2.

B gii m chp C nhiu phng php gii m chp. Tuy nhin, trong project ny, chng ta

s cp n thut ton gii m chp Viterbi. L thuyt chung ca thut ton ny l nhm tm ra ng ngn nht c trong biu mt co , ng dn n mt trng thi S(k) phi l ng ngn nht c th. Thut ton ny c th c chia thnh hai bc: 1) tnh ton gi nhnh (branch metric), gi ng (path metric) v cc bit quyt nh. 2) traceback. Trong pha 1, s sai khc gia k t nhn yk v tt c cc k t khc m c kh nng nhn c c tnh ton. Trong trng hp hard decision m cc k t ch l bit 0 hoc 1, th khong cch Hamming c s dng tnh brach metric. Khong cch Hamming s tnh s bit sai khc trong k t nhn c vi s bit trong cc k t c kh nng nhn c. Trong v d phn 1, khong cch Hamming c th l 0, 1, 2. Kt qu s c lu trong path metric.Vi mi bc k, khong cch Hamming cho mi trng thi Sk c tnh ton v c cng vo path metric ca cc trng thi trc (k-1). Trong pha th 2, thut ton Viterbi s d ngc li xc nh ra chui k t gn nht. Trng thi ban u trong pha d ngc l trng thi c path metric thp nht. hiu r hn v thut ton Viterbi. Chng ta tip tc xt v d trong phn 1 vi t m cui cng nhn c yk b sai khc so vi t m sau b lp m xk do qu trnh truyn d liu. (Hnh 4)

Hnh 4. V d v thut ton Virterbi

Page 5

Sau qu trnh truyn, yk b sai khc so vi xk ti nhng bit c t mu nh trn hnh 4. Bc u gii m k t thu l to ra bng tra LUT m t s chuyn trng thi. Bng tra ny c dng tnh ton khong cch Hamming.

Hnh 5. Ma trn chuyn trng thi Nh trong hnh 4, S(0) s chuyn n 1 trong hai trng thi l S(0) hoc S(2) sau khi nhn k t truyn u tin. Do k t nhn c y1 = 00, nn k t ny s c so snh vi hai k t ti hai trng thi chuyn c th c l S(0) v S(2). Nu S(0)-> S(0) th khong cch Hamming l 0 v nu chuyn ti S(2) th khong cch Hamming l 2. Gi tr ny s c lu li ti cc nt tng ng bn trong mt co.Gi tr ny s c tip tc tnh ton ti cc mt khc nhau sau mi ln chuyn trng thi v c cng dn c gi tr ca trng thi trc. Ti mi trng thi, s c nhng gi tr path metric ca cc trng thi trc c cng dn , nhng gi tr ny c so snh vi nhau v gi tr nh hn s c trn l path metric ti thi im . Ni tt c cc mt co c path metric nh nht ti tng thi im, ta s c s chuyn trng thi ti u nht. (ng mu trn Hnh 6).

Hnh 6. Kt qu thc hin pha 1 ca thut ton Virterbi

Page 6

Sau khi kt thc pha 1, pha 2 s thc hin vic traceback da theo s chuyn trng thi c ca pha 1. khi to traceback, trng thi c path metric nh nht s c a vo mt thanh ghi dch 2 bit, trong v d ny l 00. Gi tr ny chnh l con tr chn cc bit la chn. Qu trnh traceback c m t trong hnh 7.

Hnh 7. Qu trnh Traceback T hp cc bit c y ra khi thanh ghi dch 2 bit to thnh bn tin ban u (cc bit mu vng). T ta kt thc qu trnh gii m m chp.

Page 7

Chng 2: M phng b m ha v gii m m chp


Trong chng ny, mt b to m chp v gii m c xy dng v m phng s dng cng c Simulink ca Matlab 2010.u tin, m hnh ca b m ha c a ra.Sau ; chng ta thit lp s khi s dng cc khi trong Simulink thc hin vic m ha v gii m m chp.Cui cng, kt qu m phng s c a ra kim chng. 2.1. M hnh ca b m ha B m ha m chp c m t trong hnh 8 nh sau:

Hnh 8. M hnh b m ha m chp M hnh trn biu din b to m c tc code rate = 1/2 (1 u vo, 2 u ra), c di rng buc l Kc = 7 (6 thanh ghi dch + 1 u vo). V mt ton hc, b m chp trn c hnh thnh bi ma trn a thc c dng:

Ts m hnh b m ha m chp trn, chng ta s dng cng c Simulink ca Matlab nhm xy dng mt b m ha v gii m m chp vi m hnh nh trong hnh 8 nh sau:

Page 8

Hnh 9.M hnh m ha v gii m m chp Trong m hnh trn, qu trnh m ha v gii m m chp c m t thng qua 4 khi.u tin, tn hiu bn tin bt k c to ra mt cch ngu nhin thn qua b to m nh phn Bernoulli. Sau , chui bit nh phn ny c a ln lt vo b m ha m chp ri a qu knh truyn nh phn vi mt xc sut li knh truyn nht nh v cui cng c gii m qua b gii m s dng thut ton Virterbi. B m ha m chp c xy dng bng vic khai bo cc thng s cho biu chuyn trng thi (Trellis structure) c c trong b m ha m chp. Cch khai bo nh sau: Trellis = poly2trellis ([chiu di rng buc], [ h s trong a thc sinh di dng bt phn]) Hm Poly2trellis c nhim v chuyn i dng a thc sang s chuyn trng thi ca b m ha. Cu trc Trellis bao gm cc trng sau (hnh 10).

Field in trellis Structure

Dimensions

Meaning

numInputSymbols numOutputSymbols numStates nextStates outputs

Scalar Scalar Scalar

Number of input symbols to the encoder: 2k Number of output symbols from the encoder: 2n Number of states in the encoder
k

numStates-by-2 matrix numStates-by-2 matrix


k

Next states for all combinations of current state and curr

Outputs (in octal) for all combinations of current state an

Hnh 10. Cc trng trong cu trc Trellis

Page 9

Trong cc trng trn th chiu di rng buc ca m chp s quy nh s u vo m chp. Nu s chiu di rng buc l 2 th s u vo m chp s l 2 v s k t u vo s l 4Ngoi ra cc h s trong a thc sinh di dng nh phn s c biu din di dng bt phn trong cu trc Trellis. Vi d, vi b lp m trong hnh 8 c hai a thc sinh vi cc h s lp thnh dng nh phn l (1 1 1 1 0 0 1) ng vi u ra ca bit th nht v (1 0 1 1 0 1 1), trong khai bo Trellis, dng nh phn ny s c i sang dng bt phn l hai s 171 v 133. Vi b m ha nh trn, cu trc trellis s c 27 trng thi trong b m ha. hiu r hn v qu trnh m ha v gii m m chp, kt qu m phng ca b m ha ny s c a ra trong phn sau. 2.2. Kt qu m phng Vi m hnh c cho trong hnh 9, chng ta thit lp cc thng s cho tng khi nh sau: Khi to chui bit nh phn: Proballity of a zero: 0.5 Initial seed: dng hm randseed Sample time: 0.5

Khi to m chp: thit lp Trellis poly2trellis ( 7, [171 133]) Khi knh truyn i xng: xt xc sut li: 0.02 Khi gii m s dng thut ton Virterbi: thit lp tng t khi m chp Chn thi gian m phng t 0 n 100 s. Sau khi thit lp cc khi, ta tin hnh m phng v thu c kt qu nh trn hnh 11

Page 10

Bit thng tin u vo Hai chui bit u ra b m chp Tn hiu sau khi i qua knh truyn Chui bit thng tin sau gii m

Hnh 11. Kt qu m phng T kt qu m phng, ta thy rng qu trnh m ha v gii m mt chui bit thng tin bt k thnh cng. Vi xc sut li knh truyn l 2% th bit thng tin sau khi gii m bng thut ton Virterbi gi nguyn c ni dung tin truyn.

Page 11

Kt lun
Nh vy, qu trnh m phng vic m ha v gii m m chp c thc hin.Vic thit lp m v gii m u da vo biu mt co v thut ton gii m Virterbi.S tin li ca cng c Simulink trong phn mm Matlab cho php chng ta c th xy dng c bt k b to m chp ch bng cch thay i cc tham s trong s khi ca b to m. Tuy nhin, project cn nhiu thiu st. l cha nu ra c vai tr lm gim thiu li knh truyn ca m chp bng cch thit lp mi quan h th gia t s tn hiu trn nhiu SNR v t l li bit BER. Ngoi ra, vic xy dng b to m chp theo quan im l thuyt h thng cha c thc hin. y l nhng hng cn khc phc trong thi gian ti ca project.

Page 12

Ti liu tham kho


[1] Eric V. York (1997), Algebraic Description and Construction of Error Correcting Codes: A Linear Systems point of view, Dissertation for the degree of Philosophy Doctor of the University of Notre Dame, Notre Dame Indiana, USA. [2] Brian M. Allen, (1999). Linear systems analysis and decoding of convolutional codes. [3] www.mathwork.com

Page 13

PH LC CODE /* This code was developed as part of graduate coursework at EmbryRiddle Aeronautical University * under the guidance of Dr. Pat Anderson and Dr. Hever Moncayo. * * Author: Robert F. Hartley * Assistant Developers: Francois Hugon, Brian DeRosa, and Christopher Carvalho * Support: apm2.simulink@gmail.com * * Oct. 25, 2012 * Version 1.0 * */

/* * File: Arduino_IMU_sfcn.c * * * * --- THIS FILE GENERATED BY S-FUNCTION BUILDER: 3.0 --* * This file is an S-function produced by the S-Function * Builder which only recognizes certain fields. Changes made * outside these fields will be lost the next time the block is * used to load, edit, and resave this file. This file will be overwritten * by the S-function Builder block. If you want to edit this file by hand, * you must change it only in the area defined as: * * * %%%-SFUNWIZ_defines_Changes_BEGIN #define NAME 'replacement text'
Page 14

* *

%%% SFUNWIZ_defines_Changes_END

* DO NOT change NAME--Change the 'replacement text' only. * * For better compatibility with the Simulink Coder, the * "wrapper" S-function technique is used. This is discussed * in the Simulink Coder's Manual in the Chapter titled, * "Wrapper S-functions". * * ------------------------------------------------------------------------* | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template | * ------------------------------------------------------------------------* Created: Thu Aug 9 02:31:58 2012 * * */

#define S_FUNCTION_LEVEL 2 #define S_FUNCTION_NAME Arduino_IMU_sfcn /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<*/ /* %%%-SFUNWIZ_defines_Changes_BEGIN --- EDIT HERE TO _END */ #define NUM_INPUTS /* Input Port 0 */ #define IN_PORT_0_NAME #define INPUT_0_WIDTH #define INPUT_DIMS_0_COL #define INPUT_0_DTYPE #define INPUT_0_COMPLEX 1 1 p_degs 7

real_T COMPLEX_NO
Page 15

#define IN_0_FRAME_BASED #define IN_0_BUS_BASED #define IN_0_BUS_NAME #define IN_0_DIMS 1-D 0

FRAME_NO

#define INPUT_0_FEEDTHROUGH 1 #define IN_0_ISSIGNED 1 8

#define IN_0_WORDLENGTH

#define IN_0_FIXPOINTSCALING 1 #define IN_0_FRACTIONLENGTH 3 #define IN_0_BIAS #define IN_0_SLOPE /* Input Port 1 */ #define IN_PORT_1_NAME #define INPUT_1_WIDTH #define INPUT_DIMS_1_COL #define INPUT_1_DTYPE #define INPUT_1_COMPLEX #define IN_1_FRAME_BASED #define IN_1_BUS_BASED #define IN_1_BUS_NAME #define IN_1_DIMS 1-D 0 1 1 q_degs 0 0.125

real_T COMPLEX_NO FRAME_NO

#define INPUT_1_FEEDTHROUGH 1 #define IN_1_ISSIGNED 1 8

#define IN_1_WORDLENGTH

#define IN_1_FIXPOINTSCALING 1 #define IN_1_FRACTIONLENGTH 3 #define IN_1_BIAS #define IN_1_SLOPE /* Input Port 2 */ #define IN_PORT_2_NAME #define INPUT_2_WIDTH 1
Page 16

0 0.125

r_degs

#define INPUT_DIMS_2_COL #define INPUT_2_DTYPE #define INPUT_2_COMPLEX #define IN_2_FRAME_BASED #define IN_2_BUS_BASED #define IN_2_BUS_NAME #define IN_2_DIMS 1-D 0

real_T COMPLEX_NO FRAME_NO

#define INPUT_2_FEEDTHROUGH 1 #define IN_2_ISSIGNED 1 8

#define IN_2_WORDLENGTH

#define IN_2_FIXPOINTSCALING 1 #define IN_2_FRACTIONLENGTH 3 #define IN_2_BIAS #define IN_2_SLOPE /* Input Port 3 */ #define IN_PORT_3_NAME #define INPUT_3_WIDTH #define INPUT_DIMS_3_COL #define INPUT_3_DTYPE #define INPUT_3_COMPLEX #define IN_3_FRAME_BASED #define IN_3_BUS_BASED #define IN_3_BUS_NAME #define IN_3_DIMS 1-D 0 1 1 nx_g 0 0.125

real_T COMPLEX_NO FRAME_NO

#define INPUT_3_FEEDTHROUGH 1 #define IN_3_ISSIGNED 1 8

#define IN_3_WORDLENGTH

#define IN_3_FIXPOINTSCALING 1 #define IN_3_FRACTIONLENGTH 3 #define IN_3_BIAS #define IN_3_SLOPE 0 0.125


Page 17

/* Input Port 4 */ #define IN_PORT_4_NAME #define INPUT_4_WIDTH #define INPUT_DIMS_4_COL #define INPUT_4_DTYPE #define INPUT_4_COMPLEX #define IN_4_FRAME_BASED #define IN_4_BUS_BASED #define IN_4_BUS_NAME #define IN_4_DIMS 1-D 0 1 1 ny_g

real_T COMPLEX_NO FRAME_NO

#define INPUT_4_FEEDTHROUGH 1 #define IN_4_ISSIGNED 1 8

#define IN_4_WORDLENGTH

#define IN_4_FIXPOINTSCALING 1 #define IN_4_FRACTIONLENGTH 3 #define IN_4_BIAS #define IN_4_SLOPE /* Input Port 5 */ #define IN_PORT_5_NAME #define INPUT_5_WIDTH #define INPUT_DIMS_5_COL #define INPUT_5_DTYPE #define INPUT_5_COMPLEX #define IN_5_FRAME_BASED #define IN_5_BUS_BASED #define IN_5_BUS_NAME #define IN_5_DIMS 1-D 0 1 1 nz_g 0 0.125

real_T COMPLEX_NO FRAME_NO

#define INPUT_5_FEEDTHROUGH 1 #define IN_5_ISSIGNED 1 8

#define IN_5_WORDLENGTH

#define IN_5_FIXPOINTSCALING 1
Page 18

#define IN_5_FRACTIONLENGTH 3 #define IN_5_BIAS #define IN_5_SLOPE /* Input Port 6 */ #define IN_PORT_6_NAME #define INPUT_6_WIDTH #define INPUT_DIMS_6_COL #define INPUT_6_DTYPE #define INPUT_6_COMPLEX #define IN_6_FRAME_BASED #define IN_6_BUS_BASED #define IN_6_BUS_NAME #define IN_6_DIMS 1-D 0 1 1 temp_degC 0 0.125

real_T COMPLEX_NO FRAME_NO

#define INPUT_6_FEEDTHROUGH 1 #define IN_6_ISSIGNED 1 8

#define IN_6_WORDLENGTH

#define IN_6_FIXPOINTSCALING 1 #define IN_6_FRACTIONLENGTH 3 #define IN_6_BIAS #define IN_6_SLOPE 0 0.125

#define NUM_OUTPUTS /* Output Port 0 */ #define OUT_PORT_0_NAME #define OUTPUT_0_WIDTH

p_degs 1 1

#define OUTPUT_DIMS_0_COL #define OUTPUT_0_DTYPE #define OUTPUT_0_COMPLEX #define OUT_0_FRAME_BASED #define OUT_0_BUS_BASED #define OUT_0_BUS_NAME 0

real_T COMPLEX_NO FRAME_NO

Page 19

#define OUT_0_DIMS #define OUT_0_ISSIGNED

1-D 1 8

#define OUT_0_WORDLENGTH

#define OUT_0_FIXPOINTSCALING 1 #define OUT_0_FRACTIONLENGTH 3 #define OUT_0_BIAS #define OUT_0_SLOPE /* Output Port 1 */ #define OUT_PORT_1_NAME #define OUTPUT_1_WIDTH #define OUTPUT_DIMS_1_COL #define OUTPUT_1_DTYPE #define OUTPUT_1_COMPLEX #define OUT_1_FRAME_BASED #define OUT_1_BUS_BASED #define OUT_1_BUS_NAME #define OUT_1_DIMS #define OUT_1_ISSIGNED 1-D 1 8 0 1 1 q_degs 0 0.125

real_T COMPLEX_NO FRAME_NO

#define OUT_1_WORDLENGTH

#define OUT_1_FIXPOINTSCALING 1 #define OUT_1_FRACTIONLENGTH 3 #define OUT_1_BIAS #define OUT_1_SLOPE /* Output Port 2 */ #define OUT_PORT_2_NAME #define OUTPUT_2_WIDTH #define OUTPUT_DIMS_2_COL #define OUTPUT_2_DTYPE #define OUTPUT_2_COMPLEX #define OUT_2_FRAME_BASED #define OUT_2_BUS_BASED 0
Page 20

0 0.125

r_degs 1 1

real_T COMPLEX_NO FRAME_NO

#define OUT_2_BUS_NAME #define OUT_2_DIMS #define OUT_2_ISSIGNED 1-D 1 8

#define OUT_2_WORDLENGTH

#define OUT_2_FIXPOINTSCALING 1 #define OUT_2_FRACTIONLENGTH 3 #define OUT_2_BIAS #define OUT_2_SLOPE /* Output Port 3 */ #define OUT_PORT_3_NAME #define OUTPUT_3_WIDTH #define OUTPUT_DIMS_3_COL #define OUTPUT_3_DTYPE #define OUTPUT_3_COMPLEX #define OUT_3_FRAME_BASED #define OUT_3_BUS_BASED #define OUT_3_BUS_NAME #define OUT_3_DIMS #define OUT_3_ISSIGNED 1-D 1 8 0 1 1 nx_g 0 0.125

real_T COMPLEX_NO FRAME_NO

#define OUT_3_WORDLENGTH

#define OUT_3_FIXPOINTSCALING 1 #define OUT_3_FRACTIONLENGTH 3 #define OUT_3_BIAS #define OUT_3_SLOPE /* Output Port 4 */ #define OUT_PORT_4_NAME #define OUTPUT_4_WIDTH #define OUTPUT_DIMS_4_COL #define OUTPUT_4_DTYPE #define OUTPUT_4_COMPLEX #define OUT_4_FRAME_BASED 1 1 ny_g 0 0.125

real_T COMPLEX_NO FRAME_NO


Page 21

#define OUT_4_BUS_BASED #define OUT_4_BUS_NAME #define OUT_4_DIMS #define OUT_4_ISSIGNED 1-D 1

#define OUT_4_WORDLENGTH

#define OUT_4_FIXPOINTSCALING 1 #define OUT_4_FRACTIONLENGTH 3 #define OUT_4_BIAS #define OUT_4_SLOPE /* Output Port 5 */ #define OUT_PORT_5_NAME #define OUTPUT_5_WIDTH #define OUTPUT_DIMS_5_COL #define OUTPUT_5_DTYPE #define OUTPUT_5_COMPLEX #define OUT_5_FRAME_BASED #define OUT_5_BUS_BASED #define OUT_5_BUS_NAME #define OUT_5_DIMS #define OUT_5_ISSIGNED 1-D 1 8 0 1 1 nz_g 0 0.125

real_T COMPLEX_NO FRAME_NO

#define OUT_5_WORDLENGTH

#define OUT_5_FIXPOINTSCALING 1 #define OUT_5_FRACTIONLENGTH 3 #define OUT_5_BIAS #define OUT_5_SLOPE /* Output Port 6 */ #define OUT_PORT_6_NAME #define OUTPUT_6_WIDTH #define OUTPUT_DIMS_6_COL #define OUTPUT_6_DTYPE #define OUTPUT_6_COMPLEX 1 1 temp_degC 0 0.125

real_T COMPLEX_NO
Page 22

#define OUT_6_FRAME_BASED #define OUT_6_BUS_BASED #define OUT_6_BUS_NAME #define OUT_6_DIMS #define OUT_6_ISSIGNED 1-D 1 0

FRAME_NO

#define OUT_6_WORDLENGTH

#define OUT_6_FIXPOINTSCALING 1 #define OUT_6_FRACTIONLENGTH 3 #define OUT_6_BIAS #define OUT_6_SLOPE 0 0.125

#define NPARAMS /* Parameter 1 */

#define PARAMETER_0_NAME #define PARAMETER_0_DTYPE

lpf_filt_freq_hz uint32_T

#define PARAMETER_0_COMPLEX COMPLEX_NO /* Parameter 2 */ #define PARAMETER_1_NAME #define PARAMETER_1_DTYPE gyro_scale uint32_T

#define PARAMETER_1_COMPLEX COMPLEX_NO /* Parameter 3 */ #define PARAMETER_2_NAME #define PARAMETER_2_DTYPE accel_scale uint32_T

#define PARAMETER_2_COMPLEX COMPLEX_NO /* Parameter 4 */ #define PARAMETER_3_NAME #define PARAMETER_3_DTYPE SampleTime real_T

#define PARAMETER_3_COMPLEX COMPLEX_NO

#define SAMPLE_TIME_0 #define NUM_DISC_STATES

SampleTime 0
Page 23

#define DISC_STATES_IC

[0] 0 [0]

#define NUM_CONT_STATES #define CONT_STATES_IC

#define SFUNWIZ_GENERATE_TLC 0 #define SOURCEFILES "__SFB__" #define PANELINDEX #define USE_SIMSTRUCT 6 0

#define SHOW_COMPILE_STEPS 0 #define CREATE_DEBUG_MEXFILE 0 #define SAVE_CODE_ONLY #define SFUNWIZ_REVISION 1 3.0

/* %%%-SFUNWIZ_defines_Changes_END --- EDIT HERE TO _BEGIN */ /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<*/ #include "simstruc.h" #define PARAM_DEF0(S) ssGetSFcnParam(S, 0) #define PARAM_DEF1(S) ssGetSFcnParam(S, 1) #define PARAM_DEF2(S) ssGetSFcnParam(S, 2) #define PARAM_DEF3(S) ssGetSFcnParam(S, 3)

#define IS_PARAM_DOUBLE(pVal) (mxIsNumeric(pVal) && !mxIsLogical(pVal) &&\ !mxIsEmpty(pVal) && !mxIsSparse(pVal) && !mxIsComplex(pVal) && mxIsDouble(pVal))

/*====================* * S-function methods * *====================*/


Page 24

#define MDL_CHECK_PARAMETERS #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE) /* Function: mdlCheckParameters ============================================= * Abstract: * */ static void mdlCheckParameters(SimStruct *S) { int paramIndex = 0; bool validParam = false; /* All parameters must match the S-function Builder Dialog */ Validate our parameters to verify they are okay.

/*

{ const mxArray *pVal0 = ssGetSFcnParam(S,0); if (!IS_PARAM_DOUBLE(pVal0)) { validParam = true; paramIndex = 0; goto EXIT_POINT; } }

{ const mxArray *pVal1 = ssGetSFcnParam(S,1); if (!IS_PARAM_DOUBLE(pVal1)) { validParam = true; paramIndex = 1; goto EXIT_POINT; }
Page 25

{ const mxArray *pVal2 = ssGetSFcnParam(S,2); if (!IS_PARAM_DOUBLE(pVal2)) { validParam = true; paramIndex = 2; goto EXIT_POINT; } } */ { const mxArray *pVal3 = ssGetSFcnParam(S,3); if (!mxIsDouble(pVal3)) { ssSetErrorStatus(S,"Sample time parameter SampleTime must be of type double"); return; } }

{ const mxArray *pVal3 = ssGetSFcnParam(S,3); if (!IS_PARAM_DOUBLE(pVal3)) { validParam = true; paramIndex = 3; goto EXIT_POINT; } }

EXIT_POINT: if (validParam) {
Page 26

char parameterErrorMsg[1024]; sprintf(parameterErrorMsg, "The data type and or complexity of parameter %d does not match the " "information specified in the S-function Builder dialog. " "For non-double parameters you will need to cast them using int8, int16, " "int32, uint8, uint16, uint32 or boolean.", paramIndex + 1); ssSetErrorStatus(S,parameterErrorMsg); } return; } #endif /* MDL_CHECK_PARAMETERS */ /* Function: mdlInitializeSizes =============================================== * Abstract: * Setup sizes of the various vectors. */ static void mdlInitializeSizes(SimStruct *S) {

DECL_AND_INIT_DIMSINFO(outputDimsInfo); ssSetNumSFcnParams(S, NPARAMS); /* Number of expected parameters */ #if defined(MATLAB_MEX_FILE) if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S)) { mdlCheckParameters(S); if (ssGetErrorStatus(S) != NULL) { return; } } else { return; /* Parameter mismatch will be reported by Simulink */
Page 27

} #endif

ssSetNumContStates(S, NUM_CONT_STATES); ssSetNumDiscStates(S, NUM_DISC_STATES);

if (!ssSetNumInputPorts(S, NUM_INPUTS)) return; /* Input Port 0 */ ssSetInputPortWidth(S, 0, INPUT_0_WIDTH); ssSetInputPortDataType(S, 0, SS_DOUBLE); ssSetInputPortComplexSignal(S, 0, INPUT_0_COMPLEX); ssSetInputPortDirectFeedThrough(S, 0, INPUT_0_FEEDTHROUGH); ssSetInputPortRequiredContiguous(S, 0, 1); /*direct input signal access*/

/* Input Port 1 */ ssSetInputPortWidth(S, 1, INPUT_1_WIDTH); ssSetInputPortDataType(S, 1, SS_DOUBLE); ssSetInputPortComplexSignal(S, 1, INPUT_1_COMPLEX); ssSetInputPortDirectFeedThrough(S, 1, INPUT_1_FEEDTHROUGH); ssSetInputPortRequiredContiguous(S, 1, 1); /*direct input signal access*/

/* Input Port 2 */ ssSetInputPortWidth(S, 2, INPUT_2_WIDTH); ssSetInputPortDataType(S, 2, SS_DOUBLE); ssSetInputPortComplexSignal(S, 2, INPUT_2_COMPLEX); ssSetInputPortDirectFeedThrough(S, 2, INPUT_2_FEEDTHROUGH); ssSetInputPortRequiredContiguous(S, 2, 1); /*direct input signal access*/

Page 28

/* Input Port 3 */ ssSetInputPortWidth(S, 3, INPUT_3_WIDTH); ssSetInputPortDataType(S, 3, SS_DOUBLE); ssSetInputPortComplexSignal(S, 3, INPUT_3_COMPLEX); ssSetInputPortDirectFeedThrough(S, 3, INPUT_3_FEEDTHROUGH); ssSetInputPortRequiredContiguous(S, 3, 1); /*direct input signal access*/

/* Input Port 4 */ ssSetInputPortWidth(S, 4, INPUT_4_WIDTH); ssSetInputPortDataType(S, 4, SS_DOUBLE); ssSetInputPortComplexSignal(S, 4, INPUT_4_COMPLEX); ssSetInputPortDirectFeedThrough(S, 4, INPUT_4_FEEDTHROUGH); ssSetInputPortRequiredContiguous(S, 4, 1); /*direct input signal access*/

/* Input Port 5 */ ssSetInputPortWidth(S, 5, INPUT_5_WIDTH); ssSetInputPortDataType(S, 5, SS_DOUBLE); ssSetInputPortComplexSignal(S, 5, INPUT_5_COMPLEX); ssSetInputPortDirectFeedThrough(S, 5, INPUT_5_FEEDTHROUGH); ssSetInputPortRequiredContiguous(S, 5, 1); /*direct input signal access*/

/* Input Port 6 */ ssSetInputPortWidth(S, 6, INPUT_6_WIDTH); ssSetInputPortDataType(S, 6, SS_DOUBLE); ssSetInputPortComplexSignal(S, 6, INPUT_6_COMPLEX); ssSetInputPortDirectFeedThrough(S, 6, INPUT_6_FEEDTHROUGH); ssSetInputPortRequiredContiguous(S, 6, 1); /*direct input signal access*/
Page 29

if (!ssSetNumOutputPorts(S, NUM_OUTPUTS)) return; /* Output Port 0 */ ssSetOutputPortWidth(S, 0, OUTPUT_0_WIDTH); ssSetOutputPortDataType(S, 0, SS_DOUBLE); ssSetOutputPortComplexSignal(S, 0, OUTPUT_0_COMPLEX); /* Output Port 1 */ ssSetOutputPortWidth(S, 1, OUTPUT_1_WIDTH); ssSetOutputPortDataType(S, 1, SS_DOUBLE); ssSetOutputPortComplexSignal(S, 1, OUTPUT_1_COMPLEX); /* Output Port 2 */ ssSetOutputPortWidth(S, 2, OUTPUT_2_WIDTH); ssSetOutputPortDataType(S, 2, SS_DOUBLE); ssSetOutputPortComplexSignal(S, 2, OUTPUT_2_COMPLEX); /* Output Port 3 */ ssSetOutputPortWidth(S, 3, OUTPUT_3_WIDTH); ssSetOutputPortDataType(S, 3, SS_DOUBLE); ssSetOutputPortComplexSignal(S, 3, OUTPUT_3_COMPLEX); /* Output Port 4 */ ssSetOutputPortWidth(S, 4, OUTPUT_4_WIDTH); ssSetOutputPortDataType(S, 4, SS_DOUBLE); ssSetOutputPortComplexSignal(S, 4, OUTPUT_4_COMPLEX); /* Output Port 5 */ ssSetOutputPortWidth(S, 5, OUTPUT_5_WIDTH); ssSetOutputPortDataType(S, 5, SS_DOUBLE); ssSetOutputPortComplexSignal(S, 5, OUTPUT_5_COMPLEX); /* Output Port 6 */ ssSetOutputPortWidth(S, 6, OUTPUT_6_WIDTH); ssSetOutputPortDataType(S, 6, SS_DOUBLE); ssSetOutputPortComplexSignal(S, 6, OUTPUT_6_COMPLEX);

Page 30

ssSetNumSampleTimes(S, 1); ssSetNumRWork(S, 0); ssSetNumIWork(S, 0); ssSetNumPWork(S, 0); ssSetNumModes(S, 0); ssSetNumNonsampledZCs(S, 0);

/* Take care when specifying exception free code - see sfuntmpl_doc.c */ ssSetOptions(S, (SS_OPTION_EXCEPTION_FREE_CODE | SS_OPTION_WORKS_WITH_CODE_REUSE)); }

/* Function: mdlInitializeSampleTimes ========================================= * Abstract: * */ static void mdlInitializeSampleTimes(SimStruct *S) { ssSetSampleTime(S, 0, *mxGetPr(ssGetSFcnParam(S, 3))); ssSetOffsetTime(S, 0, 0.0); } Specifiy the sample time.

#define MDL_SET_OUTPUT_PORT_DATA_TYPE static void mdlSetOutputPortDataType(SimStruct *S, int port, DTypeId dType) { ssSetOutputPortDataType(S, 0, dType); }

Page 31

#define MDL_SET_INPUT_PORT_DATA_TYPE static void mdlSetInputPortDataType(SimStruct *S, int port, DTypeId dType) { ssSetInputPortDataType(S, 0, dType); }

#define MDL_SET_DEFAULT_PORT_DATA_TYPES static void mdlSetDefaultPortDataTypes(SimStruct *S) { ssSetOutputPortDataType(S, 0, SS_DOUBLE); ssSetInputPortDataType(S, 0, SS_DOUBLE); } /* Function: mdlOutputs ===================================================== == * */ static void mdlOutputs(SimStruct *S, int_T tid) { real_T real_T real_T real_T real_T real_T real_T *p_degs = (real_T *)ssGetOutputPortRealSignal(S,0); *q_degs = (real_T *)ssGetOutputPortRealSignal(S,1); *r_degs = (real_T *)ssGetOutputPortRealSignal(S,2); *nx_g = (real_T *)ssGetOutputPortRealSignal(S,3); *ny_g = (real_T *)ssGetOutputPortRealSignal(S,4); *nz_g = (real_T *)ssGetOutputPortRealSignal(S,5); *temp_degC = (real_T *)ssGetOutputPortRealSignal(S,6);

const int_T p_width0 = mxGetNumberOfElements(PARAM_DEF0(S)); const int_T p_width1 = mxGetNumberOfElements(PARAM_DEF1(S));


Page 32

const int_T p_width2 = mxGetNumberOfElements(PARAM_DEF2(S)); const int_T p_width3 = mxGetNumberOfElements(PARAM_DEF3(S)); const uint32_T *lpf_filt_freq_hz = (const uint32_T *)mxGetData(PARAM_DEF0(S)); const uint32_T *gyro_scale = (const uint32_T *)mxGetData(PARAM_DEF1(S)); const uint32_T *accel_scale = (const uint32_T *)mxGetData(PARAM_DEF2(S)); const real_T *SampleTime = (const real_T *)mxGetData(PARAM_DEF3(S));

const real_T *p_degs_in = (real_T *)ssGetInputPortRealSignal(S,0); const real_T *q_degs_in = (real_T *)ssGetInputPortRealSignal(S,1); const real_T *r_degs_in = (real_T *)ssGetInputPortRealSignal(S,2); const real_T *nx_g_in = (real_T *)ssGetInputPortRealSignal(S,3); const real_T *ny_g_in = (real_T *)ssGetInputPortRealSignal(S,4); const real_T *nz_g_in = (real_T *)ssGetInputPortRealSignal(S,5); const real_T *temp_degC_in = (real_T *)ssGetInputPortRealSignal(S,6);

p_degs[0] = p_degs_in[0]; q_degs[0] = q_degs_in[0]; r_degs[0] = r_degs_in[0]; nx_g[0] = nx_g_in[0]; ny_g[0] = ny_g_in[0]; nz_g[0] = nz_g_in[0]; temp_degC[0] = temp_degC_in[0]; }
Page 33

#define MDL_SET_WORK_WIDTHS #if defined(MDL_SET_WORK_WIDTHS) && defined(MATLAB_MEX_FILE)

/* Function: mdlSetWorkWidths ============================================= * Abstract: * * * * * * * Run-time parameters are registered in this method using methods ssSetNumRunTimeParams, ssSetRunTimeParamInfo, and related The optional method, mdlSetWorkWidths is called after input port width, output port width, and sample times of the S-function have been determined to set any state and work vector sizes which are a function of the input, output, and/or sample times.

methods. */ static void mdlSetWorkWidths(SimStruct *S) { /* Set number of run-time parameters */ if (!ssSetNumRunTimeParams(S, 4)) return;

/* * Register the run-time parameters */ ssRegDlgParamAsRunTimeParam(S, 0, 0, "lpf_filt_freq_hz", ssGetDataTypeId(S, "uint32")); ssRegDlgParamAsRunTimeParam(S, 1, 1, "gyro_scale", ssGetDataTypeId(S, "uint32"));

Page 34

ssRegDlgParamAsRunTimeParam(S, 2, 2, "accel_scale", ssGetDataTypeId(S, "uint32")); ssRegDlgParamAsRunTimeParam(S, 3, 3, "SampleTime", ssGetDataTypeId(S, "double")); }

#endif

/* Function: mdlTerminate ===================================================== * Abstract: * * * */ static void mdlTerminate(SimStruct *S) { } #ifdef MATLAB_MEX_FILE file? */ #include "simulink.c" #else #include "cg_sfun.h" #endif /* FreeRTOS.org V5.0.4 - Copyright (C) 2003-2008 Richard Barry. This file is part of the FreeRTOS.org distribution. FreeRTOS.org is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
Page 35

In this function, you should perform any actions that are necessary at the termination of a simulation. For example, if memory was allocated in mdlStart, this is the place to free it.

/* Is this file being compiled as a MEX-

/* MEX-file interface mechanism */

/* Code generation registration function */

the Free Software Foundation; either version 2 of the License, or (at your option) any later version. FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA A special exception to the GPL can be applied should you wish to distribute a combined work that includes FreeRTOS.org, without being obliged to provide the source code for any proprietary components. See the licensing section of http://www.FreeRTOS.org for full details of how and when the exception can be applied.

************************************************************ *************** ************************************************************ *************** * * * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, * * and even write all or part of your application on your behalf. * * See http://www.OpenRTOS.com for details of the services we provide to * * expedite your project. * * * ************************************************************ ***************
Page 36

************************************************************ *************** Please ensure to read the configuration and relevant port sections of the online documentation. http://www.FreeRTOS.org - Documentation, latest information, license and contact details. http://www.SafeRTOS.com - A version that is certified for use in safety critical systems. http://www.OpenRTOS.com - Commercial support, development, porting, licensing and training services. */ /* FreeRTOS.org includes. */ #include "FreeRTOS.h" #include "task.h" #include "semphr.h" /* Compiler includes. */ #include <stdlib.h> #include <stdio.h> /* The task that sends messages to the stdio gatekeeper. Two instances of this task are created. */ static void prvPrintTask( void *pvParameters ); /* The gatekeeper task itself. */ static void prvStdioGatekeeperTask( void *pvParameters ); /* Define the strings that the tasks and interrupt will print out via the gatekeeper. */ static char *pcStringsToPrint[] = {

Page 37

"Task 1 ****************************************************\r\n", "Task 2 ----------------------------------------------------\r\n", "Message printed from the tick hook interrupt ##############\r\n" }; /*-----------------------------------------------------------*/ /* Declare a variable of type xQueueHandle. This is used to send messages from the print tasks to the gatekeeper task. */ xQueueHandle xPrintQueue;

int main( void ) { /* Before a queue is used it must be explicitly created. The queue is created to hold a maximum of 5 character pointers. */ xPrintQueue = xQueueCreate( 5, sizeof( char * ) ); /* The tasks are going to use a pseudo random delay, seed the random number generator. */ srand( 567 ); /* Check the queue was created successfully. */ if( xPrintQueue != NULL ) { /* Create two instances of the tasks that send messages to the gatekeeper. The index to the string they attempt to write is passed in as the task parameter (4th parameter to xTaskCreate()). The tasks are created at different priorities so some pre-emption will occur. */ xTaskCreate( prvPrintTask, "Print1", 1000, ( void * ) 0, 1, NULL ); xTaskCreate( prvPrintTask, "Print2", 1000, ( void * ) 1, 2, NULL ); /* Create the gatekeeper task. This is the only task that is permitted
Page 38

to access standard out. */ xTaskCreate( prvStdioGatekeeperTask, "Gatekeeper", 1000, NULL, 0, NULL ); /* Start the scheduler so the created tasks start executing. */ vTaskStartScheduler(); } /* If all is well we will never reach here as the scheduler will now be running the tasks. If we do reach here then it is likely that there was insufficient heap memory available for a resource to be created. */ for( ;; ); return 0; } /*-----------------------------------------------------------*/ static void prvStdioGatekeeperTask( void *pvParameters ) { char *pcMessageToPrint; /* This is the only task that is allowed to write to the terminal output. Any other task wanting to write to the output does not access the terminal directly, but instead sends the output to this task. As only one task writes to standard out there are no mutual exclusion or serialization issues to consider within this task itself. */ for( ;; ) { /* Wait for a message to arrive. */ xQueueReceive( xPrintQueue, &pcMessageToPrint, portMAX_DELAY ); /* There is no need to check the return will block indefinitely and only run again when a message has arrived. When the next line is executed there will be a message to be output. */ printf( "%s", pcMessageToPrint ); fflush( stdout ); /* Now simply go back to wait for the next message. */
Page 39

value as the task

} } /*-----------------------------------------------------------*/ void vApplicationTickHook( void ) { static int iCount = 0; portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE; /* Print out a message every 200 ticks. The message is not written out directly, but sent to the gatekeeper task. */ iCount++; if( iCount >= 200 ) { /* In this case the last parameter (xHigherPriorityTaskWoken) is not actually used but must still be supplied. */ xQueueSendToFrontFromISR( xPrintQueue, &( pcStringsToPrint[ 2 ] ), &xHigherPriorityTaskWoken ); /* Reset the count ready to print out the string again in 200 ticks time. */ iCount = 0; } } /*-----------------------------------------------------------*/ static void prvPrintTask( void *pvParameters ) { int iIndexToString; /* Two instances of this task are created so the index to the string the task will send to the gatekeeper task is passed in the task parameter. Cast this to the required type. */ iIndexToString = ( int ) pvParameters; for( ;; ) {

Page 40

/* Print out the string, not directly but by passing the string to the gatekeeper task on the queue. The queue is created before the scheduler is started so will already exist by the time this task executes. A block time is not specified as there should always be space in the queue. */ xQueueSendToBack( xPrintQueue, &( pcStringsToPrint[ iIndexToString ] ), 0 ); /* Wait a pseudo random time. Note that rand() is not necessarily re-entrant, but in this case it does not really matter as the code does not care what value is returned. In a more secure application a version of rand() that is known to be re-entrant should be used - or calls to rand() should be protected using a critical section. */ vTaskDelay( ( rand() & 0x1FF ) ); } }

Page 41

You might also like