-
LI NI U
Cc i tng thng minh thng l cc thit b n gin, nh gn, gi thnh thp, s
dng ngun nng lng hn ch (pin), c thi gian hot ng lu di. Mt trong
nhng c im ni bt ca mt nt mng cc i tng thng minh l s hn ch v nng lc
tnh ton, ngun nng lng cung cp, v gi thnh sn xut. Mc d phn cng n gin
nhng cc i tng thng minh v ang c ng dng rng ri trong i sng.
Vic h tr c s h tng cho cc ng dng mng cc i tng thng minh trn h iu
hnh ngy cng tr nn quan trng. Bn cht, h iu hnh l cu ni gia phn cng n
gin vi phn ng dng phc tp.
H iu hnh cho mng cc i tng thng minh ng vai tr trung tm trong vic
xy dng cc ng dng phn tn vi kh nng m rng, c tin cy v hiu qu cao. Qua
nhiu nm, chng ta thy xut hin nhiu h iu hnh khc nhau nhm d dng cho
vic pht trin cc ng dng mng vi cc i tng thng minh. Cc chc nng c bn
ca h iu hnh bao gm vic tru tng ha ti nguyn cho cc thit b phn cng
khc nhau, qun l ngt v lp lch cc nhim v, iu khin ng thi, v h tr mng.
Da trn cc dch v c cung cp bi h iu hnh, nhng ngi lp trnh ng dng c th
thun tin s dng cc giao din lp trnh ng dng mc cao c lp vi phn cng lp
di.
H iu hnh Contiki l h iu hnh m ngun m, c nghin cu, thit k v pht
trin bi mt nhm cc nh pht trin t vin khoa hc my tnh Thy in, ngi ng u
l Adam Dunkels. Nhm pht trin Contiki gm nhiu thnh vin n t SICS,
CISCO, cng nhiu t chc v cc trng i hc khc trn th gii. H iu hnh
Contiki c thit k cho cc vi iu khin c b nh nh, vi thng s 2KB RAM v
40KB ROM. Nh , Contiki c s dng rng ri cho cc i tng thng minh.
Ni dung bi ging ny gm 4 chng:
Chng 1: Tng quan v h iu hnh nhng cho cc i tng thng minh. Chng ny
kho st cc h iu hnh hin c t a ra nhng so snh v nhng hng nghin cu
trong tng lai i vi cc h iu hnh ny.
Chng 2: Gii thiu v h iu hnh Contiki. Chng ny gii thiu tng quan v
h iu hnh Contiki bao gm lch s hnh thnh v pht trin, cc c im chnh, k
thut lp trnh trn h iu hnh Contiki, vn qun l b nh, cc bc ci t mi
trng pht trin ng dng vi Contiki trn Ubuntu.
1
-
Chng 3: Cc module trong h iu hnh Contiki. Chng ny trnh by cc
module chnh trong h iu hnh Contiki bao gm ngn xp truyn thng uIP,
RIME, cc giao din lp trnh ng dng, module qun l b nh, Contiki
system, cc th vin, cc nn tng phn cng c h tr trn h iu hnh
Contiki.
Chng 4: Xy dng chng trnh vi Contiki. Chng ny trnh by cch xy dng
chng trnh ng dng v cch m phng, thc thi chng trnh trn thit b
thc.
Mc du a ht sc c gng nhng khng th trnh khi cc thiu st. Mi kin ng
gp
MC LC
2
-
LI NI U
.....................................................................................................................
1 MC LC
...........................................................................................................................
2 Chng 1. TNG QUAN V H IU HNH CHO MNG CC I TNG THNG MINH
...................................................................................................................
4 Chng 2. H IU HNH CONTIKI
..........................................................................
19 Chng 3. CC MODULE TRONG H IU HNH CONTIKI
................................. 37 Chng 4. XY DNG CHNG TRNH
...................................................................
38 NG DNG VI CONTIKI
............................................................................................
38 [edit] The COOJA Simulator
.............................................................................................
38 [edit] Getting started
..........................................................................................................
38 [edit] Feature requests, bug reporting, and questions
........................................................ 41 Get
started with the Cooja simulator
.................................................................................
41 Example-Shell Guide
.........................................................................................................
60 Simple Contiki Data Collection on the Tmote Sky
........................................................... 62
Setting up a Low-Power IPv6/RPL Network
....................................................................
72 REST-Example Guide
.......................................................................................................
88 TI LIU THAM KHO
.................................................................................................
89
3
-
Chng 1. TNG QUAN V H IU HNH CHO MNG CC I TNG THNG MINH
1.1. Gii thiu v cc i tng thng minhCc i tng thng minh thng l cc
thit b n gin, nh gn, gi
thnh thp, s dng ngun nng lng hn ch (pin), c thi gian hot ng
lu
di (vi thng n vi nm).
Mt v d in hnh v cc i tng thng minh l cc nt cm bin
khng dy. Trong cc nt mng thng l cc thit b n gin, nh gn, gi
thnh thp v c s lng ln c phn b mt cch khng c h thng trn
phm vi hot ng rng, s dng ngun nng lng hn ch. Cc nt mng
thng c chc nng cm nhn, quan st mi trng xung quanh nh nhit ,
m, nh sng theo di hay nh v cc mc tiu c nh hoc di ng Cc nt
giao tip ad-hoc vi nhau v truyn d liu v trung tm (base station)
vi cch
thc giao tip bng k thut multi-hop.
Cu trc bn trong cc i tng thng minh thng c mt b vi iu
khin chy cc phn mm. Vi iu khin l mt b vi x l c b nh trong, b
nh thi, v phn cng cho vic kt ni cc thit b bn ngoi khc. Vi iu
khin
trng ging nh mt vi mch thng thng vi mt v bc bng nha, cc chn
kt ni bng kim loi nh trong hnh 1.1.
4
-
Hnh 1.1: Mt b vi iu khin Atmel ATTINY 2313 vi 20 chn. Cc b
ATTINY 2313 c 2kB ca ROM v 128byte b nh RAM.
Do hn ch v chi ph v nng lng, cc vi iu khin c s dng trong
cc i tng thng minh thng nh hn nhiu so cc b vi x l c s dng
trong cc my tnh c nhn. Thng thng, mt b vi iu khin s dng trong
nt
cm bin c mt vi KB b nh ca chip v c chy tc xung nhp mt vi
MHz. Trong khi , my tnh hin i c n hng Gbytes b nh v chy xung
nhp mt vi Ghz.
Vi iu khin c hai loi b nh: B nh ch c (ROM) v b nh truy
cp ngu nhin (RAM). ROM c s dng lu tr m chng trnh cn RAM
c s dng cho cc d liu tm thi ca chng trnh phn mm. D liu tm
thi bao gm vic lu tr cc bin chng trnh v b nh m x l lu lng
v tuyn. Ni dung ca ROM c ghi vo thit b khi n c sn xut v
thng khng b thay i sau khi nt c trin khai. Tuy nhin, vi iu
khin
hin i cung cp mt c ch ghi li ROM, m n rt c ch cp nht phn
mm sau khi cc nt c trin khai.
Ngoi b nh th b vi iu khin cn c b nh thi v c ch tng tc
vi cc thit b bn ngoi nh thit b truyn thng, cm bin. Cc b b nh
thi
c th c s dng bi cc phn mm chy trn vi iu khin. Cc thit b bn
ngoi c kt ni vt l vi cc chn ca vi iu khin. Cc phn mm giao
tip
vi cc thit b s dng cc c ch c cung cp bi vi iu khin, thng
thng
dng mt cng ni tip hoc cc Bus ni tip. Hu ht cc b vi iu khin
cung
cp b thu pht ng b/khng ng b chung (USART) giao tip vi cc cng
5
-
ni tip. Mt s USART c th c cu hnh lm vic nh mt giao din bus
ngoi vi ni tip (SPI) giao tip vi cm bin.
Ngy nay, Pin l ngun nng lng ph bin nht cho cc i tng thng
minh. Chng c nhiu hnh dng v kiu dng. Cc Pin Lithium hin nay l
ph
bin nht. Vi vic qun l nng lng thch hp, mt i tng thng minh c
th c tui i hng nm trn tiu chun t bo pin lithium.
Mi hot ng ca mt i tng thng minh c xc nh bi phn mm
chy trn b vi iu khin. Cc phn mm thng c vit tng t nh phn
mm cho cc my tnh. Cc chng trnh c vit bng mt ngn ng lp trnh,
chng hn nh C, v c bin dch vi mt trnh bin dch m my cho vi iu
khin. Cc m my c ghi vo ROM ca vi iu khin. Khi c bt ngun, th
b vi iu khin s chy cc phn mm. Qu trnh ny c minh ha trong hnh
1.2.
Mc d hon ton c th chy chng trnh cho vi iu khin m khng
cn s dng mt h iu hnh no, nhng hu ht cc i tng thng minh s
dng h iu hnh. Bi v cc hn ch v ti nguyn nn cc h iu hnh cho
chng rt khc vi cc h iu hnh cho my tnh. Cc h iu hnh cho cc i
tng thng minh nh gn hn nhiu v tiu tn t ti nguyn.
Hnh 1.2: Qu trnh pht trin phn mm cho i tng thng minh.
M ngun c c bin dch thnh m my m c ghi
vo ROM trong b vi iu khin.
6
-
1.2. H iu hnh nhng cho cc i tng thng minh1.2.1. Chc nng ca h iu
hnh
Mt mng cc i tng thng minh in hnh bao gm mt s lng ln cc nt vi kch
thc nh, ngun nng lng pin hn ch, mt b x l, v kh nng truyn thng.
Mc d phn cng ca cc i tng thng minh n gin nhng cc ng dng mng ca
cc i tng thng minh bao gm nhiu ng dng khc nhau v theo nhu cu. Vic h
tr c s h tng cho cc ng dng trn h iu hnh ngy cng tr nn quan trng. Bn
cht, h iu hnh l cu ni gia phn cng n gin vi phn ng dng phc tp.
Cc chc nng c bn ca h iu hnh bao gm vic tru tng ha ti nguyn cho
cc thit b phn cng khc nhau, qun l ngt v lp lch cc nhim v, iu khin
ng thi, v h tr mng. Da trn cc dch v c cung cp bi h iu hnh, nhng ngi
lp trnh ng dng c th thun tin s dng cc giao din lp trnh ng dng mc
cao (APIs) c lp vi phn cng lp di. Bi v hu ht cc thit b c ti nguyn
hn ch do vy mt h iu hnh khng nn ch cung cp cc dch v a dng thun li
cho vic lp trnh cc ng dng m cn tn dng ti nguyn mt cch ti u.
1.2.2. Nhng thch thc nh hng n vic thit k h iu hnh
Hin ti, vic nghin cu v pht trin h iu hnh cho cc i tng
thng minh ang gp phi mt s thch thc cn phi gii quyt. Nhng
thch
thc chnh l cc rng buc v ti nguyn ca phn cng v cc yu cu ca ng
dng. Nhng thch thc chnh nh hng n vic thit k h iu hnh c
quan tm nht l:
Kch thc nh: Vi b nh b gii hn ch vi KB nn i hi h iu
hnh phi c thit k vi kch thc rt nh. y l c im c bn ca
h iu hnh cho cc i tng thng minh v cng l l do chnh m
ti sao nhiu h iu hnh nhng tinh vi (nh uC/OS, VxWorks) khng
th
d dng ci t c trn cc thit b ny.
Hiu qu nng lng: Cc i tng thng minh thng hot ng bng
pin do vy vn nng lng cng cn phi quan tm.
m bo thi gian thc: Nhiu ng dng ca cc i tng thng minh
v d nh l ng dng gim st thng c khuynh hng nhy cm vi thi
7
-
gian. Vi cc ng dng , cc gi tin cn c chuyn tip v gi i mt
cch kp thi, vn m bo thi gian thc l yu cu p ng cho cc
ng dng .
Kh nng cu hnh li: L iu cn thit vi cc h thng mng ti
nguyn hn ch c th c lp trnh li sau khi mng c trin khai. Kh
nng cu hnh li h thng l mt c im cn thit ca h iu hnh lm
cho mng c th lp trnh li d dng v hiu qu.
S tin li cho lp trnh: Cc ng dng ca mng cc i tng thng
minh l khc nhau v ty theo yu cu. Do vy, s tin li cho lp trnh
l
mt iu quan trng nht rt ngn thi gian trin khai cc ng dng.
Thng th rt kh t c s ti u trong tt c cc vn nu ra
trn. Hu ht cc gii php hin ti c p dng cho cc kch bn ng dng c
th v to ra nhng tha hip nhm gii quyt cc thch thc thit k trn.
1.2.3. Cc thnh phn c bn ca mt h iu hnh
Cc thnh phn chnh cu thnh nn h iu hnh cho cc i tng thng minh c
lit k di y:
Lp lch nhim v: Thnh phn lp lch nhim v cung cp cho ngi lp trnh ng
dng mi trng nhim v cho vic thc thi on m ng dng. Hin ti, c ba c ch
lp lch nhim v l: Lp lch nhim v iu khin theo s kin (event-driven),
lp lch nhim v a lung (multi-threaded) v lp lch nhim v
Prototheads.
Np v lin kt ng: Thnh phn np v lin kt ng cho php cc phn ng dng c
np hon ton ng v ci thin kh nng t cu hnh h iu hnh khi lp trnh li
mng. Khi phn ng dng c np, trnh lin kt v np nh v khng gian nh cho
phn m v d liu ca ng dng, lin kt cc k hiu ti cc a ch vt l ca chng, v
sao chp phn c lin kt ti khng gian b nh dnh ring. C ch ny rt hu ch
khi lp trnh li mt ng dng trn cc nt mng: Khng phi truyn b c mt khi
hnh nh nhn ng dng, m n ch yu cu truyn b phn ng dng ph bin nh hn
nhiu do vy hiu qu hn v nng lng.
Qun l b nh: B nh trong cc thit b c th c phn loi gm: RAM (cho vic
lu tr d liu nhanh), b nh trong (cho vic lu tr m), EEPROM (cho lu tr
d liu), v b nh ngoi (cho vic lu tr d liu). V d nh, vi MicaZ c 4KB
RAM, 128KB b nh trong, 4KB EEPROM,
8
-
v 512KB b nh ngoi. B nh RAM l quan trng cho vic truy nhp d liu
nhanh trong khi b nh bn ngoi l quan trng cho lu tr d liu. Vic qun l
b nh RAM cng c th l tnh hoc ng. Vic qun l b nh ngoi c th cung cp s
tru tng mc cao chng hn nh tp (v d vi h iu hnh LiteOS) hoc nhng tru
tng mc thp nh khi (v d vi h iu hnh TinyOS).
Tru tng ha ti nguyn: Thnh phn tru tng ha ti nguyn cung cp giao
tip truy nhp mc cao v an ton vi cc thit b phn cng. V d nh, tru tng
ha ti nguyn trong TinyOS c phn loi thnh nhng tru tng ha ti nguyn
dnh ring, nhng tru tng ha ti nguyn o, v nhng tru tng ha ti nguyn
chia s. Ti nguyn dnh ring h tr cho mt ngi dng duy nht. Ti nguyn o h
tr nhiu ngi dng bng vic duy tr hng i v lp lch cho nhiu yu cu ng thi
theo kiu tun t. Ti nguyn chia s cng h tr nhiu ngi dng, nhng cc ngi
dng phi cnh tranh cc trnh iu khin thng qua mt kha.
Cc giao din cm bin: Cc giao din cm bin cung cp cho ngi lp trnh
ng dng cch thc truy cp c d liu cm bin mt cch d dng v theo dng chun.
Nhng m t chi tit mc thp c gn vi nhng cu hnh cm bin cng c tru tng ha
vi nhng ngi lp trnh ng dng.
Ngn xp mng: Ngn xp mng thun tin cho vic pht trin cc ng dng WSN
phn tn. N cng cn thit cho vic duy tr h thng t xa sau khi WSN c trin
khai. Ngn xp mng cho h iu hnh nn h tr cc dch v mc cao chng hn nh
vic thu thp v truyn d liu. N cng qun l cc chi tit mc thp nh cu hnh
chip thu pht v tuyn, iu khin truy nhp ng truyn (MAC), v qun l hng
i.
1.2.4. Kho st mt s h iu hnh
Mc ny ca bo co s m t cc h iu hnh tin tin hin nay. Cn phi
ch rng, hu ht cc h iu hnh vn ang c pht trin, do vy mc ny ch
m t nhng c im chnh trong cc phin bn hin ti ca cc h iu hnh
ny.
1.2.4.1. TinyOS
H iu hnh TinyOS c pht trin UC Berkeley, c l l h iu hnh
nhng cho cc i tng thng minh sm nht. N cho php mt kin trc mm
9
-
do v tiu th ti nguyn thp, vic lp trnh trn TinyOS da trn cc thnh
phn
c kt ni vi nhau to mt ng dng thi im thit k. Nhng tng tc
gia cc thnh phn xy ra theo hai chiu, tc l mt thnh phn s dng
lnh
c cung cp bi mt thnh phn khc; ngoi ra, mt thnh phn c th bo
hiu
cc s kin ti mt thnh phn khc. M hnh thc thi ca TinyOS bao gm
cc
ngt (interrupt) v cc nhim v (task). Cc ngt c thc thi mc u tin
cao
hn v c th c u tin thc thi trc cc nhim v. Cc nhim v c thc
thi mc u tin thp hn v c lp lch theo kiu vo trc ra trc FIFO
(First In First Out). Cc nhim v trong TinyOS c vit theo kiu
run-to-
completion (chy n khi hon thnh), v chng khng th ginh c quyn
u
tin trc (preempt) hoc tm ngng (suspend). V l do ny, vic xut
nhp
(I/Os) c chia thnh cc giai on. Tc l mt yu cu (request) c thc
hin
vo lc kt thc mt nhim v trong khi tn hiu (signal) gi n s bt u
ca
nhim v tip theo. h tr tt hn cho kin trc thnh phn v m hnh thc
thi
ca TinyOS th ngn ng nesC c thit k cho vic lp trnh da trn
TinyOS.
Phin bn 2 ca TinyOS (T2) ci tin sa vi phin bn 1 mt s mt. T2
cng cp s tra tng lng nhau, n l s lai ghp s phn chia theo
phng
ngang (cho mc thp hn h tr nhiu loi thit b phn cng khc nhau), v
s
phn chia theo phng ng (cho mc cao hn h tr chc nng nn tng phn
cng c lp), v lm cho n d dng h tr cc nn tng phn cng mi. Bn
cnh nhng ci tin kin trc, c mt s ci tin quan trng trong s thc
thi, bao
gm h tr c lung (v d TinyThreads, v TOSThreads trong TinyOS phin
bn
2.1), h tr bo v b nh. Hn na, T2 cung cp dch v phn tn v cung
cp
mt nhm cc thnh phn (v d cc dch v) ci thin tin cy ca h thng.
Bn cnh nhng ci tin kin trc, c mt s ci tin quan trng trong s thc
thi,
bao gm h tr c lung (v d TinyThreads, v TOSThreads trong TinyOS
phin
bn 2.1), h tr bo v b nh.
1.2.4.2. Contiki
Cc thnh phn trong TinyOS c kt ni hon ton tnh to mt ng
dng hay mt nhn n duy nht. Cch tip cn ny c th ti u vic tiu th
ti
nguyn nhng cng rt phc tp t ng cu hnh li hay cp nht cc ng
dng. gii quyt vn ny, Vin khoa hc my tnh Thy in nghin cu
10
-
v pht trin h iu hnh Contiki. Contiki h tr cc thnh phn c th np
t
ng. gii quyt nhng kh khn trong kiu lp trnh da trn s kin (v d
trong TinyOS), Contiki h tr hot ng a lung (tc l s chuyn i
tnh
hung nhng thi im c xc nh bi ngi dng) thng qua cc th vin
ngi dng. Contiki cng h tr c ch lung nh protothreads. Phin bn
mi
nht ca Contiki thc thi mt tp h thng trong b nh ROM gi l Coffee
v
kin trc Chameleon cho ngn xp v tuyn nng lng thp Rime.
1.2.4.3. SOS
H iu hnh SOS c pht trin i hc California, Los Angeles, cng
h tr cc module c th t ng np. N s dng kin trc da trn module.
C
mt iu lu l c s khc nhau gia cc thnh phn trong TinyOS v cc
modules trong SOS. Cc thnh phn trong TinyOS ch c th nhn thy t
gc
nhn ca nhng ngi lp trnh v chng khng thy khi c bin dch thnh
on m nh phn. Mt khc, cc modules trong SOS vn cn cc thng tin m
nh
phn (v d nh im bt u ca mt module, cc hm ca mt module) sau
khi
bin dch, cho php cc modules c np hoc np li t ng. h tr kin
trc ny, SOS cng h tr vic nh v b nh ng.
M hnh thc thi SOS phc tp hn khng nhiu so vi TinyOS. Cc trnh
x l bn tin SOS (ging nh cc cc tasks trong TinyOS) c gii quyt
theo ba
mc u tin. Phin bn mi nht ca SOS l phin bn 2.0.1, ci tin hn so
vi
phin bn c mt s im, bao gm s h tr nhiu nn phn cng v h tr
vic bo v b nh. Tuy nhin, k t 24/11/2008, SOS khng c pht trin
bi
v cc thnh vin pht trin chnh tt nghip.
1.2.4.4. MantisOS
Mantis OS c pht trin i hc Colorado. Mantis OS cho php h tr
a lung thc hin a lung phn chia theo thi gian u tin. cho php
lp
trnh a lung, nhn Mantis cng h tr ng b I/Os (ngc vi chia giai
on
I/Os), v mt tp cc b iu khin ng thi Semaphores. Phin bn Mantis
mi
nht ci thin s n nh ca h thng v sa mt s li. Ngoi ra, n thc
hin
giao thc CTP (Collection Tree Protocol) cho cc nn phn cng MicaZ
v
TelosB.
1.2.4.5. Nano-RK
11
-
H tr cc ng dng mng khng dy nhy cm vi thi gian nh gim st
mi trng v gim st an ninh. Nano-RK c pht trin i hc Carnegie
Mellon, thc hin h iu hnh thi gian thc. Nano-RK h tr quyn u tin
a
nhim m bo cc nhim v c p ng ng thi hn. N cng h tr vic
ginh trc rng di tn mng v CPU, tc l cc nhim v c th ch nh cc
yu cu ti nguyn ca chng v h iu hnh cung cp bng thng mng v vic
truy cp cc chu k CPU kp thi, m bo v c kim sot. thun tin cho
vic lp trnh, Nano-RK cung cp cc APIs cho cc tru tng ha ging
nh
socket, v h thng chung h tr nh tuyn v lp lch mng.
1.2.4.6. RETOS
RETOS c pht trin i hc Yonsei Hn Quc, c thit k ci
tin mt s im cc cng vic trc. N ci tin kh nng phc hi h thng
bi vic h tr ch vn hnh kp (tc l ch ht nhn v ch ngi dng)
cng nh vic kim tra m ng dng thi im thit k v thi im chy.
N ti u s thc thi a lung v h tr vic lp lch thi gian thc POSIX
1003.1b. N cng h tr kh nng np cc modules v cc dch v mng a
chng. Phin bn mi nht ca RETOS h tr nhiu nn phn cng khc nhau,
ti
u ha lp mng v ci thin s an ton h thng.
1.2.4.7. LiteOS
LiteOS c pht trin i hc Illinois ti Urbana Champaign, c thit
k cung cp mi trng lp trnh ng dng cho mng khng dy ging nh
UNIX truyn thng. N bao gm: H thng file phn cp v wireless shell
cho s
tng tc vi ngi dng s dng cc lnh ging Unix; H tr cc ht nhn cho
vic thc thi np t ng cc ng dng a lung; Ngn ng lp trnh hng i
tng c s dng mt tp con ca ngn ng C++. Phin bn mi nht ca
LiteOS c s h tr mt s nn tng phn cng IRIS ca Crossbow. Hn na,
n
h tr c ch Virtual Battery v h thng g ri t xa (Declarative
Tracepoints).
1.2.5. Tiu ch phn loi h iu hnh
Mc ny trnh by s phn loi cc h iu hnh da trn vic kho st mt
s c im h iu hnh quan trng.
12
-
Tnh hay ng: Trong cc h thng tnh (v d nh TinyOS, Nano-PR),
ngi lp trnh ng dng phi nh v tt c cc ti nguyn thi im thit
k. Mt khc, trong cc h thng ng (v d nh SOS, Contiki, Mantis,
RETOS, LiteOS), nhng ngi lp trnh ng dng c th nh v v nh v
li cc ti nguyn thi im chy ng dng. Cc h thng ng mm do
hn v do vy ph hp cho nhng mi trng thay i thng xuyn. Tuy
nhin, chng khng tin cy v d nh mt ng dng li c th d dng
ginh c qu nhiu ti nguyn ( thi gian chy ng dng), l nguyn
nhn lm cho ton b h thng b sp .
Hng s kin hay a lung: Trong cc h thng hng s kin (v d
nh TinyOS, SOS), ngi lp trnh ng dng cn phi duy tr trng thi
ng dng v s dng chia giai on I/Os. Mt khc, cc h thng a lung
(v d nh Mantis, Nano-RK, RETOS), ngi lp trnh ng dng c th s
dng cch lp trnh nh cc lung truyn thng. Do vy, cc h thng a
lung ngy cng quen thuc vi hu ht nhng ngi lp trnh v c
xem nh l thn thin vi ngi dng hn cc h thng hng s kin. C
mt lng ln cc d n tp trung ci tin h thng hng s kin bng
vic cung cp thm s h tr a lung. V d nh TinyThread,
TOSThreads l cc th vin lung da trn TinyOS. Contiki h tr a
lung u tin thng qua mt th vin trn ht nhn hng s kin, v n
cng thc thi mt c ch lung con c gi l protothreads. Cc h thng
hng s kin cng rt hu dng bi v: Chng ph hp cho cc ng dng
phn ng nhanh v iu quan trng hn l yu cu ti nguyn t v ph hp
cho cc thit b hn ch v ti nguyn. Do vy, mt s h thng a lung
cng vn h tr s kin v d nh LiteOS cung cp s h tr Events thng
qua cc hm Callback.
n khi hay module ha: Mt ng dng c th c bin dch vi h
iu hnh nh mt khi chng trnh (v d TinyOS) hoc c th c bin
dch thnh cc phn chng trnh ring l m chng c th c np bi
nhn h iu hnh (v d nh Contiki, SOS, RETOS). Cch tip cn n
khi c kh nng phn tch v ti u ha chng trnh y , v thch hp
khi ng dng t cn chnh sa. Cch tip moudle ha c quan tm khi
13
-
tng ng dng ring l cn chnh sa u n thng qua vic lp trnh li
mng. Mc d, n lm tng thm s phc tp cho h iu hnh bi vic h
tr lin kt v np, nhng n gim ng k chi ph khi lp trnh li mng.
H tr mng: Vi nhiu h iu hnh, TCP/IP l mt tiu chun. Hu ht
cc h iu hnh mng cc i tng thng minh cung cp vic truyn
thng n bc pha trn mng a chng. V d bao nh TinyOS, SOS,
Mantis. TinyOS s dng ngn xp truyn thng da trn vic s dng bn
tin AM (Active Message) . Ngoi d liu, mi AM cng cha AM ID c
s dng cho nt ch gi i cc AM ti b x l tng ng ca n khi
n ch. C mt iu ch rng mt s giao thc mng lp cao hn c
thc thi trn lp AM ny, bao gm c cc giao thc truyn tin trong
mng
v cc giao thc thu thp. Mt s h iu hnh mng h tr trc tip mng
multi-hop. V d nh Contiki, Nano-RK, RETOS. Contiki cha hai
ngn
xp truyn thng l uIP v Rime. uIP l ngn xp TCP/IP tng thch vi
RFC, gip cho h iu hnh Contiki c th truyn thng qua Internet.
Gn
y, Contiki cn thc hin thm uIPv6 h tr IPv6. Rime l ngn xp
truyn thng n gin c thit k cho truyn thng v tuyn nng lng
thp. Rime h tr nhiu giao thc truyn thng nguyn thy, t qung b
vng cc b best-effort, n ngp lt d liu multi-hop. LiteOS b sung
s
h tr cho vic truyn cc file gia cc nt mng, s dng cc lnh Shell
ging nh Unix truyn thng chng hn nh cp, mv.
H tr thi gian thc: Vic h tr thi gian thc cho cc h iu hnh c
th xy ra cp nt h iu hnh nn thc hin vic lp lch cc
nhim v u tin m bo vic h tr thi gian thc hiu qu ca mt
nt. Nano-RK l mt h iu hnh thi gian thc h tr quyn u tin v
kp thi thng qua vic phn tch vic lp lch off-line. RETOS h tr
giao
din lp lch thi gian thc 1003.1b ng thi cho php gn mc u tin
ca ngi lp trnh v qun l u tin ng ca ht nhn h iu hnh.
H tr ngn ng: Hu ht cc h iu hnh s dng ngn ng C cho c
vic pht trin h iu hnh v pht trin ng dng (v d nh Contiki,
SOS, Mantis, RETOS). Bi v ngn ng C l hiu qu, thun tin v l
ngn ng chnh cho vic pht trin cc h thng nhng. Tuy nhin, ngn
14
-
ng C cng c mt s im hn ch: Mt mt, n khng chuyn su trong
vic kim tra ng ngha, ti u ha, v ty chnh ht nhn; mt khc n t
thn thin vi ngi dng so vi cc ngn ng hng i tng chng hn
nh C++, Java. Ngn ng nesC, v LiteC++ trnh by hai gii php gii
quyt hai vn c cp trn. Ngn ng nesC tch hp vi m hnh
thc thi trong TinyOS v p dng m hnh lp trnh da trn thnh phn.
V
n c th thc hin kim tra tnh, ti u v phn tch ton b chng trnh,
nn sinh ra ton b hnh nh ht nhn ng dng vi kch thc rt nh. Tuy
nhin, ngn ng nesC c mt s im ring, n buc nhng ngi lp trnh
phi tm hiu thm. Ngn ng LiteC++ c thit k cho LiteOS pht
trin ng dng. N b sung nhng c im c ngn ng hng i tng
hin i. Do vy, n gn gi vi ngi lp trnh v thn thin vi ngi
dng hn c vi ngn ng C v nesC.
Cc tp tin h thng: Mt i tng thng minh c kh nng lu tr hn
ch, v d nh MicaZ ch c 4KB EEPROM v 512 KB b nh ngoi. Do
vy, cc tp h thng phi c chi ph ti nguyn thc hin rt nh. C
Matchbox v ELF u c thc thi da trn phin bn th nht trong
TinyOS. Chng cung cp s t chc tp cp ring l v nhng tru tng
c bn cho s hot ng ca tp chng hn nh ghi v c. LiteOS ci tin
hn so vi cc h iu hnh trc bng vic h tr t chc tp phn cp v
c nhiu hn cc APIs thao tc vi tp. Gn y, Contiki a ra h
thng tp da trn b nh flash (Coffee) cho vic lu tr d liu trong
mng cc i tng thng minh. H thng tp cho php nhiu tp trn cng
mt b nh vt l v c hiu sut gn ging vi thng lng truyn d liu
th ca b nh.
Vic lp trnh li khng dy: Vic lp trnh li mi c nghin cu
trong nhng nm gn y. Thng thng, cc ng dng c lp trnh trn
cc thit b thng qua kt ni c dy. Vi vic h tr lp trnh li th
nhng
ngi pht trin h thng c th ci t hay cp nht ng dng mi cho
mng hon ton khng dy. Deluge l mt chun c ch lp trnh li cho
TinyOS. Bi v nguyn l thit k tnh ca TinyOS nn cc ng dng c
ci t vi ht nhn h iu hnh nh l mt hnh nh y . Cch tip
15
-
cn ny phi chu chi ph ti nguyn ln bi v bao gm c chi ph ht
nhn. gii quyt vn ny, FlexCup h tr vic lin kt v np ng
cc thnh phn nh phn, do vy cho php cp nht m trn mi phn c
bn. C ch np v lin kt ng c h tr bi SOS, Contiki, RETOS, v
LiteOS.
1.2.6. Mt s vn cn tip tc nghin cu
Vic h tr h iu hnh l quan trng thun li cho vic trin khai v
bo tr mng cc i tng thng minh. Vi s h tr mnh t cc h iu hnh,
chng ta c th hnh dung c rng vic pht trin ng dng s v cng n
gin. Di y s l mt s hng nghin cu pht trin h iu hnh trong tng
lai.
1.2.6.1. H tr cc ng dng thi gian thc
C nhiu lnh vc ng dng thi gian thc cho mng cc i tng thng
minh, v dng nh trong t ng ha cng nghip, gim st qu trnh ha hc,
x
l v truyn dn d liu a phng tin. Trong mt vi h iu hnh, cc b lp
lch c thit k h tr cc phn mm cng nh i vi hot ng phn cng
thi gian thc. Trong tng lai, i hi cc thut ton lp lch c th thch
ng c
phn mm v yu cu phn cng thi gian thc ca cc ng dng.
1.2.6.2. Qun l b nh th cp
Thi gian qua, cc lnh vc mi cho mng cc i tng thng minh ang
ni ln v cc ng dng yu cu nhiu b nh nhiu hn na. Mt c s d liu
c yu cu b nh lu tr th cp i vi cc i tng thng minh. Ch c mt
vi h iu hnh cung cp mt file h thng qun l lu tr th cp. Nu
chng
ta xem xt nh lut Moores, trong tng lai khng xa, chng ta c th hi
vng s
c cc i tng thng minh s c b nh lu tr th cp ln hn. Do nhng
n lc nghin cu cn thc hin xy dng c mt h thng tp tin c kh
nng m rng (phn tn) cho mng cc i tng thng minh.
1.2.6.3. H tr b nh o
Mt i tng thng minh c b nh RAM b gii hn v cc ng dng
yu cu nhiu RAM hn na. Do , trong tng lai chng ta cn gii thiu
cc
khi nim v b nh o trong cc h iu hnh cho cc i tng thng minh.
16
-
Chng ta cn pht minh cc cng ngh qun l b nh o s dng nng lng
v b nh hiu qu.
1.2.6.4. H tr nhiu ng dng
H iu hnh cho cc i tng thng minh c pht trin vi gi nh
rng ch c mt ng dng s chy trn thit b. Nhng i vi s xut hin ca
cc
lnh vc ng dng mi v ang t ra nhiu thch thc. Chng ta xem xt
trng hp i tng thng minh l mt nt cm bin khng dy a phng tin,
trong cc nt cm bin c trang b vi mt b cm bin ging ni
(microphone), cm bin hnh nh nh l my nh, v cm bin v hng
(scalar
sensors). Do trn mt node cm bin duy nht chng ta c th c mt ng
dng n s dng l b cm bin hnh nh. Tc l, quay video, p dng k thut
x
l nh trn n v gi video nn ti trm c s. Tng t nh vy, chng ta c
th
c cc ng dng khc m chng ang s dng b cm bin ting ni v b cm
bin v hng. V vy, cc h iu hnh cn phi cha nhiu ng dng cng mt
lc.
1.2.6.5. H tr ngn xp giao thc truyn thng
Nhiu h iu hnh cho cc i tng thng minh cung cp mt giao din
lp trnh ng dng API truyn thng cho php cc ng dng gi v nhn d
liu. Mt khc, nhng API ny s dng ngn xp giao thc truyn thng c
cung cp bi h iu hnh. Khng c tha thun v mt chng giao thc thng
nht cho cc i tng thng minh, do hu nh mi h iu hnh u cung cp
cho ring mnh ci t ty chnh mt ngn xp giao thc truyn thng. Kt qu
l,
khng th truyn thng gia hai thit b s dng h iu hnh khc nhau.
Trc
y, Contiki cung cp ci t ca mt ngn xp micro IPv6 cho mng cc i
tng thng minh. iu ny cho php cc thit b s dng a ch IP v giao
tip
vi cc thit b cho php IP khc. Theo phng php tip cn Contiki, trong
phin
bn mi ca TinyOS c cung cp h tr cho IP ph hp vi chun
6LowPAN. Cung cp mt ci t giao thc ngn xp uIP cho php hai thit b
s
dng h iu hnh khc nhau c th giao tip vi nhau.
1.2.6.6. Qun l c s d liu h thng
17
-
Nhim v ca mt i tng thng minh v d nh mt nt cm bin
khng dy l cm nhn, thc hin tnh ton, truyn ti, v lu tr d liu.
Trong
mt s mng cm bin, d liu ch gi ti trm c s trong p ng ca mt
truy
vn, v vy mt nt cm bin phi c th lu tr d liu v hiu c ngn ng
truy vn. Mt n lc nghin cu l cn thit thit k mt h thng qun l c
s
d liu cho nt cm bin ph hp i vi cc c trng ca chng.
1.2.6.7. H tr cc giao din lp trnh ng dng APIs cho x l nh v tn
hiu
V cc lnh vc ng dng cho cc i tng thng minh trong lnh vc x
l hnh nh ang ni ln, do vy h iu hnh cn phi cung cp mt tp hp
phong ph cc API x l tn hiu to iu kin thun li cho cc nhim v
ca
nh pht trin chng trnh ng dng.
18
-
Chng 2. H IU HNH CONTIKI
2.1. Gii thiu v h iu hnh ContikiH iu hnh Contiki l h iu hnh m
ngun m, c nghin cu, thit
k v pht trin bi mt nhm cc nh pht trin t vin khoa hc my tnh Thy
in, ngi ng u l Adam Dunkels. Nhm pht trin Contiki gm nhiu thnh vin
n t SICS, CISCO, cng nhiu t chc v cc trng i hc khc trn th gii. H iu
hnh Contiki c thit k cho cc vi iu khin c b nh nh, vi thng s 2KB RAM
v 40KB ROM. Nh , Contiki c s dng cho cc h thng nhng v cc ng dng
trong mng cc i tng thng minh. Contiki bt u c nghin cu t nm 2001 v
pht hnh phin bn u tin Contiki 1.0 nm 2003. Hnh 2.1 cho thy lch s
pht trin ca Contiki trong nhng nm qua. Phin bn hin nay ca Contiki l
2.5, vi nhiu thay i, b sung v pht trin vt bc. Trong thc t, Contiki
c ng dng trong nhiu d n nh gim st ng hm xe la, theo di nc trong bin
Baltic, Nhiu c ch, tng trong Contiki c ng dng rng ri trong cng
nghip. in hnh nh m hnh uIP c pht hnh nm 2001 c s dng trong h thng
ng dng ca hng trm cng ty trong cc lnh vc hng hi, thng tin v tinh,
khai thc du m,; m hnh Protothreads c cng b ln u tin nm 2005, n nay
c s dng trong nhiu ng dng nh b gii m k thut s v i tng thng minh ni
chung v mng cm bin khng dy ni ring.
19
-
Hnh 2.1: Lch s pht trin Contiki.H iu hnh Contiki c lp trnh bng
ngn ng C v c nhng c
im ph hp vi cc i tng thng minh:
Contiki c chia thnh nhiu module hot ng c lp. Nh cc ng dng c th s
dng cc module mt cch linh ng v ch np nhng module cn thit.
C ch hot ng iu khin s kin lm gim nng lng tiu hao v hn ch dung
lng b nh cn s dng.
C th s dng IP trong mng thng qua uIP stack c xy dng da trn nn
TCP/IP.
C nhng module cho php c lng v qun l nng lng mt cch hiu qu.
Cc giao thc tng tc gia cc lp v cc node trong mng d dng hn.
S dng RIME stack phc v cc giao thc dnh cho mng nng lng thp mt
cch hiu qu.
Bn cnh , Contiki cn cung cp nhng cng c h tr m phng vi giao din n
gin, d s dng v h tr tt nhng thit b trong thc t, phc v nhng mc ch
nghin cu, m phng v trin khai nhng giao thc mi.
2.2. Cu trc h iu hnh ContikiBt k phin bn Contiki no cng gm 7 th
mc l: apps, core, cpu, docs,
example, platform v tools.
Th mc apps: Cha cc tp tin ngun ca cc tin ch pht trin cho
Contiki. Chng c sn s dng v bao gm cc thit lp c bn ca cc ng dng
20
-
cho mng cc i tng thng minh . ng dng tiu biu trong th mc ny l
trnh duyt web, my ch Web, FTP, email....
Th mc Core: Nh tn gi cho thy, n cha cc ht nhn ca h iu hnh
Contiki. N cha khong 300 files, gn mt na trong s l tp tin tiu cha
cc khai bo v cn li l cc tp tin ngun cha ci t.
Th mc CPU: Cha cc b x l c th cho vic thc hin cc chc nng khc nhau
c s dng trong h iu hnh.
Th mc Docs: c s dng trong vic chun b ti liu cho Contiki. N cha
thng tin s c s dng bi mt h thng ti liu in hnh nh Doxygen.
Th mc Examples: Cha cc chng trnh v d n gin bt u vi Hello-world,
nh l bc u tin hng ti lp trnh ng dng trn Contiki.
Th mc Platform: Bao gm thng tin c th lin quan n nn tng phn cng
cho cc nt cm bin nh ESB, Sky mote,
Th mc Tools: L th mc cha cc cng c phn mm c bit. V d nh 'Cooja' l
mt chng trnh Java m phng cho Contiki.Th mc ny cng cha cc cng c cho
cc nn tng phn cng c th. V d in hnh l cc cng c cho nt cm bin Tmote
Sky ca Sentilla.
2.3. Kin trc phn lp h iu hnh ContikiH iu hnh Contiki theo kiu
kin trc module. Ti mc ht nhn n theo
m hnh iu khin hng s kin (event-driven), nhng li cung cp cc
phng
tin ty chn lung ti cc tin trnh ring l. Nhn Contiki bao gm mt b
lp
lch s kin lm nhim v gi i cc s kin ti cc tin trnh ang chy. Cc
tin
trnh thc thi c kch hot bng cc s kin gi i bi ht nhn ti cc tin
trnh
hoc bng c ch hi vng. C ch hi vng c s dng trnh cc iu kin
tranh ua (race conditions). Bt k s kin no c lp lch s chy cho
n
khi n hon thnh. Tuy nhin, cc trnh x l s kin cng c th s dng cc
c
ch ni u tin.
C hai loi s kin c h tr bi h iu hnh Contiki: Cc s kin ng
b v khng ng b. S khc nhau gia hai loi s kin ny l, s kin loi
ng b c gi i ngay lp tc ti tin trnh ch bi v n c lp lch.
Cn i vi cc s kin khng ng b th chm hn, th tc gi c xp vo
hng i v sau cng c gi n tin trnh ch. C ch hi vng c s
21
-
dng trong Contiki c th xem nh l cc s kin c u tin cao n c lp
lch gia mi s kin khng ng b. Khi mt hi vng c lp lch th tt c
cc tin trnh thc hin mt trnh x l hi vng c gi l th t u tin ca
chng.
Tt c cc cng c ca h iu hnh v d nh, b cm bin x l d liu, truyn
thng, v cc trnh iu khin thit b c cung cp di dng cc dch v. Mi dch v
c mt giao din v ci t ca n. Cc ng dng s dng mt dch v c th cn hiu c
giao din dch v. Mt ng dng khng quan tm n ci t ca mt dch v. Hnh 2.2
minh ha kin trc phn lp ca h iu hnh Contiki.
Hnh 2.2: Kin trc h iu hnh Contiki
2.4. Ngn xp truyn thng trong h iu hnh ContikiContiki c bn gm 2
stack truyn thng l uIP vi TCP/UDP, IPV4, IPV6
gip h iu hnh truyn thng qua mng Internet v Rime c thit k cho
nhng lin kt khng dy nng lng thp, n cung cp mt phm vi rng ln
cc
truyn thng nguyn thy t nhng cch thc qung b ni vng hiu qu cao
n
flooding d liu ng tin cy trn nhiu nt mng.
22
-
Hnh 2.3: Kin trc giao thc mng trong h iu hnh Contiki.
2.4.1. Ngn xp uIP
Trong nhng nm gn y, cng vi s thnh cng ca Internet, giao thc
TCP/IP tr thnh tiu chun ton cu trong lnh vc truyn thng, TCP/IP
l
giao thc c bn c s dng cho nhng mc ch truyn ti cc trang web,
gi
v nhn email, truyn d liuCc h thng nhng s dng TCP/IP c kh nng
kt ni nhng h thng trc tip n mt mng ni b, hoc thm ch l mt mng
ton cu.
Nhng thit b nhng c kh nng p ng c y nhng c tnh
caTCP/IP s l nhng thit b c tnh u vit, c kh nng giao tip mt cch
y
vi tt c cc thit b khc trong mng.
Tuy nhin, s trin khai giao thc TCP/IP truyn thng i hi qu
nhiu
ti nguyn gm c dung lng code v b nh s dng, khng th c p ng
trong cc h thng nhng 8 hoc 16 bit.
Xut pht t tng , uIP c thit k da trn ngn ng C vi mc
tiu ti u ha tuyt i cc c tnh cn thit cho mt stack TCP/IP y .
uIP
ch c th hot ng vi mt giao din mng duy nht v bao gm cc giao thc
:
IP, ICMP, UDP, TCP.
Mt s hm trong API uIP
Khi to mt kt ni : tcp_connect(), tcp_listen(), udp_new().
Ngt mt kt ni : uip_close(), uip_abort().
23
-
Gi mt gi kiu TCP: uip_send().
Gi mt gi kiu UDP : uip_udp_packet_send().
Nhn mt gi t mng : uip_newdata(), uip_datalen().
Gi li mt gi d liu : uip_rexmit().
M v Kim tra cng kt ni : uip_listen() m mt cng kt ni ;
uip_connected()- kim tra kt ni.
2.4.2. Ngn xp RIME
Rime stack cung cp mt cu trc phn tng ca giao thc mng cm bin
khng dy, t mt b pht qung b n gin ti vic nh tuyn rc ri trong
ton
mng. Rime trin khai mt giao thc phc tp, vi nhiu phn, mi phn li
gm
nhng module phc tp c to nn t nhng module nh l n gin hn. Di
y l ton th t chc ca giao thc Rime:
Hnh 2.4: T chc ca RIME
Abc: pht sng qung b, n ch gi mt gi tin qua cc trnh iu khin v
tuyn v nhn tt c cc gi tin t cc trnh iu khin v tuyn khc.
Broadcast: pht sng xc nh, n thm a ch ngi gi gi i cc gi
d liu v chuyn n vo module abc.
24
-
Unicast: module ny cho bit thm mt a ch ch cho cc gi tin c
truyn cho khi pht sng. bn nhn, nu a ch ch ca gi tin khng
ph hp vi a ch ca nt th gi tin s b loi b.
Stunicast: l cc unicast cng u , khi c hi gi mt gi tin n mt
nt, n s gi nhiu ln vi mt khong thi gian nht nh cho n khi yu
cu dng li.
Runicast: l cc unicast ng tin cy, n s gi mt gi tin bng cch s
dng
cc stunicast ch mt gi tin xc nhn. Khi nhn c, n dng vic truyn
ti
lin tc ca cc gi tin. Mt s lng ti a cc gi tin truyn li phi c
xc nh, trnh gi v hn.
Polite v ipolite: hai module gn nh ging ht nhau, khi mt gi tin
c
gi i trong mt khung thi gian nht nh, module ch mt na thi
gian,
kim tra xem n c nhn c gi tin n nh gi hay khng. Nu trng, gi
tin khng c gi i, nu khng n s gi gi tin. iu ny rt hu ch cho
cc k thut flooding trnh vic truyn li khng cn thit.
Multihop: module ny i hi chc nng bng nh tuyn, v khi nh gi mt
gi tin, n yu cu bng nh tuyn cho hop tip theo v gi gi tin n n
bng cch unicast. Khi n nhn c mt gi tin, nu hop l ch, gi tin
s
c truyn ti cc lp trn, nu khng n s yu cu thng tin v hop tip
theo t bng nh tuyn v chuyn tip cc gi tin n n. Khi gi gi, cc
ng
dng lu gi vo b nh m v gi cc hm x l lin quan gi gi i. Khi
nhn c mt gi, gi nhn c c lu trong b m gi, ng thi
RIME stack gi cc hm callback tng ng x l gi u vo.
25
-
Hnh 2.5: Qu trnh truyn d liu ca RIME.
2.5. K thut lp trnh trn h iu hnh ContikiContiki hot ng da trn c
ch iu khin s kin. Cc Process hay cc
trnh x l c gi mi khi c mt s kin xy ra nh cc s kin v cm bin, trnh
khi ng, trnh kt thc, Qu trnh gi cc Process hon ton khng b ngn chn
hay cn tr trong cc chng trnh. Process hot ng da trn cc Protothread,
to ra cc lung iu khin lin tip theo c ch event -driven. Protothread
l s kt hp gia hai c ch iu khin: Multithreads Lp trnh a lung v event
driven Lp trnh hng s kin.
2.5.1. Lp trnh Event-driven
Lp trnh hng s kin (Event-driven) l mt phng php hiu qu v b nh vit
phn mm cho cc nt cm bin. Vi kiu lp trnh ny, phn mm c biu din nh l
cc trnh x l s kin: Cc on m ngn gn m t lm th no m h thng p ng c cc s
kin. V d v cc s kin nh l mt gi tin v tuyn n t mt nt ln cn, s kin c
d liu cm bin t mt trong cc b cm bin, v s kin t b nh thi. Khi s kin
din ra, nt cm bin tr li bng cch thc thi mt phn phn mm ca n.
Lp trnh hng s kin i hi t b nh hn so vi lp trnh a lung bi v khng
c lung no yu cu b nh ngn xp. Ton b h thng c th chy nh mt lung duy
nht, trong ch yu cu mt ngn xp duy nht.
Kiu lp trnh hng s kin cng ph hp t nhin vi bn cht hng s kin ca cc
nt cm bin. V nt cm bin tng tc vi mt mi trng iu khin hng s kin, nn m
hnh lp trnh nm bt c hnh vi c th quan st c ca h thng.
2.5.2. Lp trnh Multi-threaded
a lung l mt k thut lp trnh cho php nhiu chng trnh c th chy ng
thi trn mt b x l duy nht. Trong lp trnh a lung, mi chng trnh
26
-
c nh sn mt lung iu khin ring ca n, v lung chy cng vi tt c cc
lung khc trong h thng. Mi lung c nh sn thi gian nht nh chy trn b vi
x l. cho php chy nhiu chng trnh cng mt lc, h iu hnh chuyn i gia cc
lung chng cng nhau nhn c s chia s cng bng ca b vi x l.
Lp trnh a lung c s dng rng ri trong cc h thng iu hnh thng dng,
cc lung t bo v ln nhau sao cho mt lung khng th tip cn mt lung khc m
khng i qua cc giao din quy nh. Khi cc lung t bo v ln nhau, chng
thng c gi l cc tin trnh thay v gi l cc lung.
i vi cc i tng thng minh, tn ti mt vn a lung l mi lung yu cu mt
phn b nh ca ring mnh gi trng thi ca cc lung ny, c gi l ngn xp ca
lung. Cc ngn xp cha cc bin cc b m lung s dng v cc gi tr tr v cho cc
hm m lung gi n, nhng cng bao gm mt lng tng i ln b nh khng s dng. B
nh ny phi c cp pht bi v n cha bit trc c c bao nhiu b nh ngn xp m mi
lung cn dng. Do , b nh ngn xp thng vt qu s cp pht.
2.5.3. Lp trnh Prothreads
Protothreads l mt cch kt hp u im ca cc m hnh lp trnh hng s kin v
a lung. Protothreads l c ch lp trnh c pht trin cho cc h thng c b nh
hn ch, n kt hp m hnh lp trnh hng s kin v a lung theo mt phng thc
hiu qu v b nh. Vi Protothreads, chng trnh c cu trc theo tun t, ging
nh trong m hnh a lung, nhng s dng t b nh tng t nh m hnh hng s kin.
Protothreads c th thc hin c hiu qu trong ngn ng lp trnh C m khng cn
bt k ngn ng lp trnh bc thp hay cc thay i no vi trnh bin dch. iu hn
ch l cc lp trnh vin phi lu tr cc bin mt cch r rng khi cc
Protothreads dng. Bi v cc Protothreads c thc hin bi ngn ng C, nn
chng rt tin li trn cc nn tng phn cng khc nhau.
2.5.4. So snh ba m hnh lp trnh trong Contiki
M hnh lp trnh Multi-threaded c kh nng thc hin ng thi mt chui cc
threads. Tuy nhin, cc threads i hi phi c thc hin trn nhng ngn nh
ring, to ra chui cc lung iu khin tun t. Multi-threaded c xy dng nh
mt th vin, cung cp cho cc Process khi cn thit.
Trong khi , m hnh lp trnh Event-driven ch hot ng trn mt ngn nh v
thc hin cc lung iu khin ty theo cc s kin n. Do Event-
27
-
driven i hi b nh t hn v cung cp c ch iu khin linh hot theo cc s
kin.
Hnh 2.6: Phng thc s dng b nh ca Multi-threaded v
Event-driven.
Hnh 2.7: Cc lung iu khin trong Multi-Threaded v
Event-driven.
Nh s kt hp cc c tnh ca hai c ch Multi-threaded v Event-driven,
Protothreads c kh nng cung cp c ch iu khin kiu event-driven, cung
cp cc lung iu khin lin tc, ng thi s dng dung lng b nh nh vi mt ngn
nh duy nht.
Trong API ca Contiki bao gm 4 loi Protothreads c bn:
- PT_INIT (pt): khi to mt Protothread.
- PT_BEGIN (pt): bt u mt Protothread.
- PT_WAIT_UNTIL (pt, iu kin): iu khin i mt s kin.
- PT_END (pt): kt thc mt Protothread.
Hnh 2.8 l mt v d v Protothreads.
28
-
Hnh 2.8. V d lp trnh Protothreads.Hnh 2.9 trnh by mt v d ca mt
chng trnh c thc thi vi m
hnh lp trnh a lung v m hnh lp trnh hng s kin. Hnh 2.10 trnh by
mt chng trnh tng t c thc hin vi Protothreads. S khc bit gia m hnh
khng ch l cu trc on m m cn c chiu di ca on m. Mc d m hnh lp trnh
hng s kin c nhiu dng m hn, nhng n hiu qu v b nh hn so m hnh a
lung.
Hnh 2.9: V d v lp trnh a lung (tri) v lp trnh hng s kin
(phi).
29
-
Hnh 2.10: V d ca lp trnh Protothreads.
2.6. Hot ng nh thi trong Contiki
Trong Contiki s dng 4 loi nh thi:
Timer: l loi nh thi th ng, ch s dng lu li vt cc thi im khi b nh
thi ht hn.
Rtimer: l loi nh thi thi gian thc, s dng gi mt hm ti mt thi im c
th no .
Event timer (etimer): l loi nh thi hot ng, c kch hot trong cc
Process v s dng gi mt s kin n Process khi b nh thi ht hn.
Callback timer (ctimer): l loi nh thi hot ng, c th c s dng bt k
v tr no trong chng trnh, c chc nng gi mt hm x l mi khi b nh thi ht
hn. Ctimer c s dng trong module RIME ca Contiki.
Hnh 2.11 minh ha v d s dng Etimer trong Contiki:
Hnh 2.11. V d s dng Etimer.
30
-
V d trn cho thy cch s dng b nh thi kiu Etimer trong Contiki thay
i trng thi ca Led sau mi giy. Cc b nh thi kiu Etimer thng c khai bo
kiu tnh v c kch hot trong Process.
Hnh 2.12. V d s dng Ctimer trong Contiki.Hnh 2.12 minh ha v d
trn cho thy cch s dng b nh thi kiu
Ctimer trong cc chng trnh Contiki. Chng trnh trn c chc nng thay
i trng thi LED mi giy cho n khi c mt s kin no xy ra. B nh thi c s
dng gi hm led_toggler sau mi giy.
Bng cch s dng cc b nh thi mt cch ph hp, kt hp vi s dng cc
Protothreads, cc chng trnh Contiki c kh nng h tr hiu qu cho cc chng
trnh, giao thc ph hp vi nhng yu cu kht khe v b nh, kh nng x l, nng
lng, ca mng cc i tng thng minh.
2.7. Qun l b nh
Do nhng hn ch nghim trng nh nng lng tiu th, kch thc vt l v chi
ph ca cc b vi iu khin c s dng cho cc nt cm bin, nn b nh b hn ch. Do
, b nh phi c qun l mt cch hiu qu. C mt s k thut c thc hin cho hu ht
cc b nh hn ch trong mt nt cm bin. Khng ging nh cc my tnh thng thng,
m b nh c th hon i mt cch linh ng vo mt a cng, th b nh trong vi iu
khin ca cc i tng thng minh thng khng th c di chuyn ti b lu tr th
hai.
phn mm, b nh c cp pht c th l tnh ti thi im bin dch hoc cp pht ng
thi im chy. B nh c cp pht tnh cho php cc lp trnh vin bit trc l chng
trnh s ph hp vi b nh ca vi iu khin, nhng n khng cho php h thng p ng
mt cch linh hot vi cc yu cu trong thi gian chy. Mt khc, s cp pht b
nh ng c th p ng c b
31
-
nh m h thng yu cu thc t, nhng n khng th d on c hnh vi ca h thng
s nh th no.
Hnh 2.13: Cp pht b nh tnh (tri), s cp pht ng t mt vng b nh tnh
(gia) v cp pht ng t mt Heap (phi).
Bi v nhng u im v nhng hn ch khc nhau ca cc phng php cp pht ng v
tnh, nn mt phng php lai thng c s dng. Trong phn ny, chng ta hy xem
ba phng php:
Cp pht tnh: Tt c b nh c cp pht ti thi im bin dch v khng b nh no
c cp pht trong thi gian chy.
Cp pht ng t vng b nh tnh: B nh c th c cp pht ng trong thi gian
chy t mt tp hp c nh ca cc vng b nh tnh. Kch thc ca mi cp pht c xc
nh trc v khng th thay i trong thi gian chy.
Cp pht ng t mt vng lu tr c bit Heap: B nh c th c cp pht ng trong
thi gian chy v kch thc ca mi cp pht c th c xc nh ti thi gian
chy.
Hnh 2.13 cho thy b nh c cp pht vi cp pht tnh, cp pht ng t mt vng
b nh tnh v cp pht ng t mt vng lu tr c bit Heap. Hnh ny cho thy cc
vng nh A v B c cp pht vi ba phng php khc nhau. Vi cp pht tnh, hai
cp pht xut hin trong b nh t khi h thng c khi ng n khi h thng c tt.
B nh c dnh ring cho hai cp pht v khng th c s dng cho bt c chng trnh
no khc.
Vi cp pht ng, th b nh cho cc cp pht khng c dnh ring cho chng, m
vng nh cng c th c s dng bi cc cp pht khc. Khi b nh c cp pht ng t mt
vng b nh tnh, th cc vng b nh c cp pht tnh. Cc cp pht tnh ny sau s c
chia thnh cc phn on c nh v kch thc. B nh c th c cp pht t cc phn on
c kch thc c nh ny. Sau khi mt phn on c cp pht, n ch c th c s
dng
32
-
bi chng trnh c cp pht n. Khi chng trnh thc hin xong vi phn on,
chng trnh s tr li phn on n vng b nh. B cp pht b nh nh du s phn on l
nhn ri v c th cp n ti mt chng trnh khc ngh ly n.
Cp pht ng t mt vng lu tr c bit Heap phc tp hn cp pht ng t mt vng
nh. Vi cp pht ng t mt vng lu tr c bit Heap, b nh c cp pht l t mt
phn ca b nh c gi l Heap. Bt k kch thc no ca b nh c c th c cp pht t
Heap, min l c Byte trng lin tip trn Heap. Mi ln mt phn no ca Heap c
cp pht, phn ny ca b nh khng th c di chuyn hoc c cp pht bi chng trnh
khc. Khi chng trnh thc hin xong vi b nh ca n, n tr li b nh cho
Heap.
Hnh 2.14. Vn vi cp pht ng Heap: cp pht cho C khng th c cp pht,
thm ch nu c b nh trn Heap, bi v b nh b phn mnh.
Li ch ca vic cp pht ng Heap l cc phn on b nh c kch thc bt k c th
c cp pht. Ci gi cho cho u im ny l Heap c th b phn mnh do b nh khng
th c cp pht t Heap, ngay c khi c s Byte nhn ri cn li. iu ny c minh
ha trong hnh 2.14, ni cp pht b nh cho C khng th c cp pht bi v khng
c Byte lin tip li cn li trn Heap. Thm ch c khi s lng cc Byte nhn ri
trn Heap ln hn kch thc cp pht cho C, b nh c th khng c cp pht do s
phn mnh.
Bi v cc vn v phn mnh trong cp pht Heap, nn hu ht cc nt cm bin s
dng cp pht tnh cho hu ht cc mc ch v s dng vic cp pht vng nh ng khi
cp pht b nh ng l cn thit. Bi v cc nt cm bin thng c thit k cho mt
nhim v duy nht, nn cp pht tnh thng l mt chin lc cp pht b nh. Nhng
bi v khi lng cng vic c th thay i, nn cng cn thit mt lng nht nh cc
cp pht ng.
33
-
2.8. Ci t v thc hnh vi Contiki2.8.1. Ci t Instant-Contiki
Bc 1: Download v gii nn cc phn mm cn thit.
- Download VMware ti a ch:
http://www.vmware.com/download/player/
- Download Instant Contiki ti a ch:
http://www.sics.se/contiki/instant-contiki.html
- Gii nn Contiki.
Bc 2: Ci t cc phn mm cn thit.
- Ci t VMware.
Bc 3: M Contiki-Instant bng VMware.
- Chy VMware
Hnh 2.15. Giao din VMware Player.
- M Instant-Contiki: Chn Open v chn ng dn n th mc Contiki va gii
nn. Chn Instant-Contiki
34
-
Hnh 2.16. Chn ng dn n Instant-Contiki.
- in username l user ri n enter .Mn hnh ng nhp hin ln:
Hnh 2.17. Giao din ng nhp username
- in password l user ri n enter .
Hnh 2.18. Giao din nhp Password
- Giao din ca Instant-Contiki c ci trn Ubuntu.
35
-
Hnh 2.19. Giao din Instant-Contiki c ci trn Ubuntu.
2.8.2. Chng trnh iu khin LED trn Tmote Sky
Tmote Sky l mt nn tng phn cng cho nt cc i tng thng minh ca hng
Sentilla. Nn tng phn cng ny c gi lp trn cng c m phng Cooja. Trong
phn ny, em thc hin vit mt chng trnh n gin iu khin 3 LED trn Tmote
Sky. Di y l on m chng trnh:
#include "contiki.h"#include
"dev/leds.h"/*---------------------------------------------------------------------------*/PROCESS(blink_process,
"Blink");AUTOSTART_PROCESSES(&blink_process);/*---------------------------------------------------------------------------*/PROCESS_THREAD(blink_process,
ev, data){ PROCESS_EXITHANDLER(goto exit;) PROCESS_BEGIN();
while(1) { static struct etimer et; etimer_set(&et,
CLOCK_SECOND); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
leds_on(LEDS_ALL); etimer_set(&et, CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
leds_off(LEDS_ALL); }
exit: leds_off(LEDS_ALL); PROCESS_END();
36
-
}Hnh 2.20. Kt qu m phng vi Cooja.
Chng 3. CC MODULE TRONG H IU HNH CONTIKI
- Tham kho ti liu: Contiki2.x Reference Manual.
37
-
Chng 4. XY DNG CHNG TRNH
NG DNG VI CONTIKI
4.1. Gii thiu v cng c m phng Cooja
[edit] The COOJA Simulator
The section gives a high-level overview of sensor network
simulations in COOJA. This information is not strictly needed for
using COOJA, but may help users to more easily understand problems,
limitations, and features in the simulator.
[edit] Contiki level: the Contiki Mote Type
A simulated Contiki Mote in COOJA is an actual compiled and
executing Contiki system. The system is controlled and analyzed by
COOJA. This is performed by compiling Contiki for the native
platform as a shared library, and loading the library into Java
using Java Native Interfaces (JNI). Several different Contiki
libraries can be compiled and loaded in the same COOJA simulation,
representing different kind of sensor nodes (heterogeneous
networks). COOJA controls and analyzes a Contiki system via a few
functions. For instance, the simulator informs the Contiki system
to handle an event, or fetches the entire Contiki system memory for
analysis. This approach gives the simulator full control of
simulated systems. Unfortunately, using JNI also has some annoying
side-effects. The most significant is the dependency of external
tools such as compilers and linkers and their run-time arguments.
COOJA was originally developed for Cygwin/Windows and Linux
platform, but has later been ported to MacOS.
[edit] Getting started
Java version 1.6 or later is required to run COOJA. We recommend
using the latest version from Sun. In addition, the build tool ant
is also required for building COOJA. For Windows users, we
recommend using Cygwin. Add the Cygwin binaries path (for
38
-
example c:\cygwin\bin) to your PATH environmental variable. To
compile and start COOJA:
**> cd contiki-2.x/tools/cooja** **> ant run**
COOJA builds and the simulator is started. However, before you
can simulate a Contiki system, you need to configure COOJA to
correctly interface your toolchain.
Open //Menu >// //Settings > External tools paths//
This dialog displays your current COOJA configuration: compiler
paths and arguments, a bunch of regular expressions used to parse
information from these tools, etc. **These settings are stored in
your home directory: .cooja.user.properties**, such as in
///home/myuser// or //C:\Documents And Settings\myuser//.
[edit] Configuration Wizard
(This wizard replaces the old JNI tests in
/tools/cooja/examples/jni_tests.) The configuration wizard helps
configuring COOJA for using JNI, a challenging task on many
systems. The wizard consists of 4 steps and tests compiling,
linking, loading libraries, analyzing library memory, and
controlling library execution.
Open //Menu >// //Settings > Compiler configuration
wizard//
Complete all 4 steps. Note that you may have to change the
recommended settings. Any changes can later be reviewed in the
//External tools paths// dialog
[edit] Create a Hello World simulation
After completing the configuration wizard, we are now ready to
create a simulation.
Open //File > New simulation//, and click //Create//
A simulation without motes and using the default parameters is
created. Before adding motes to the simulation, we first need to
create a mote type. The mote type determines the type of sensor
hardware and which Contiki applications are to be simulated.
Open //Mote Types > Create mote type > Contiki Mote
Type//
A dialog allowing you to configure the new mote type
appears.
Enter a suitable description: "My first hello world mote type"
Click Browse, and select the Contiki Hello World application**:
hello-world.c** Compile the Contiki shared library by clicking
**Compile** When the compilation finishes, load the library and
create the mote type by
clicking **Create**.
We have now added a mote type, however, the simulation does not
yet contain any simulated motes.
39
-
Enter **10** and click **Create and Add**
You may later add further motes of this type by menu //Menu >
Motes > Add motes of type >// //My first hello world mote
type//. A few plugins are started: a control panel for starting and
pausing the simulation, a visualizer that shows the node positions,
and a log listener showing printouts of all simulated nodes.
Press **Start** (or CTRL+S) in the **Control Panel** plugin to
start simulating.
[edit] Save and load simulations
Cooja allows for saving and loading simulation configurations. A
simulation configuration contains the simulated modes and mote
types, radio medium configuration, active plugins, etc.
Note that only the configuration, not the state of the
simulation is saved. Hence, when a simulation is loaded, the
simulation will start over from time 0.
To save the current simulation:
Open //Menu >// //File > Save simulation// Enter suitable
name. Configuration files are stored with the file extension
**.csc**
To load a simulation:
Open //Menu >// //File > Open simulation > Browse...//
Select configuration file to load.
The simulation is loaded, and the plugins will appear after a
while. Loading a simulation with several mote types may take some
time, since Contiki is recompiled in the background.
A similar functionality as saving and loading simulation, is
**reloading** a simulation. To reload the current simulation:
Open //Menu >// //File > Reload simulation > same
random seed//
[edit] COOJA configs for Contiki development: sharing simulation
configs
When developing Contiki applications, you should normally keep
all your code in a project directory. Your project directory may be
a subfolder of Contiki (i.e. //contiki-2.x/examples/myproject///),
or external to Contiki (i.e. ///home/user/mycontikiproject//).
COOJA's leverages the Contiki project directories by storing any
simulation's external file references (such as to Contiki
applications) relative to the directory where you save the
simulation configuration. If you save a COOJA configuration in your
project directory, any references to Contiki code residing in that
directory will hence be stored as portable relative paths. This
feature enables moving and sharing project directories, for example
committing Contiki projects to CVS.
40
-
o Example:**
Contiki path: ///home/user/contiki-2.x// Contiki project path:
///home/user/mycontikiproject// Contiki application:
///home/user/mycontikiproject/myapp.c//
Create a COOJA simulation, compile //myapp.c// and finally save
the simulation configuration **in the project directory**.
COOJA config:
///home/user/mycontikiproject/mysimconfig.csc//
The Contiki application path will be stored as
"[CONFIG]/myapp.c" i.e. relative where the configuration file is
stored.
[edit] COOJA configs for Contiki development: simulation
quickstart
It is also possible to quickstart COOJA, instead of starting
COOJA from the ///tools/cooja/// directory.
To quickstart COOJA from a Contiki project directory:
///home/user/mycontikiproject///> **make myapp.cooja
TARGET=cooja**
or
///home/user/mycontikiproject///> **make mysimconfig.csc
TARGET=cooja**
[edit] Feature requests, bug reporting, and questions
Questions and requests should normally be posted to the main
Contiki developers mailing list:
[email protected]. See the Contiki website
for information of how to subscribe to this list. You may also mail
me directly, Fredrik sterlind, [email protected].
4.2. Hng dn s dng Cooja
Get started with the Cooja simulatorFrom ContikiWikiJump to:
navigation, search
Cooja is the Contiki network simulator. With Cooja, it is
possible to run Contiki systems without access to any actual
hardware. This tutorial will show you how you can use Contiki's
simulator Cooja to run two example simulations and how to set up
your own simulation.
Before doing this tutorial, you should Install Instant
Contiki.
41
-
Contents[hide]
1 Step 1: Start Cooja 2 Step 2: Run a Hello, World example 3
Step 3: Run a data collection example
4 Summary
[edit] Step 1: Start Cooja
First Start Instant Contiki.
To start Cooja, double-click on the "Run Cooja" icon to the
right on the screen. This will bring up a terminal window with some
text scrolling by. The Cooja Java code may need to be compiled,
which may take a few seconds. After a few seconds the Cooja window
appears. Maximize the window by double-clicking on the title bar.
The screen should now look like this:
[edit] Step 2: Run a Hello, World example
This first example shows how to open one of the existing
simulation examples in the Contiki code. We begin with the simple
Hello, World example. This example has a small network of two
nodes. The nodes only print out a Hello, World message and does
nothing more. To open the example, go to the Open Simulation menu
item on the File menu:
42
-
Click on the Browse item, and a file dialog box appears. Go to
the contiki-2.x/examples/hello-world directory and open the
hello-world-example.csc file:
Opening the simulation will cause Contiki to be compiled. The
compilation output is shown in a dialog box on the screen:
43
-
There may be a number of compilation warnings displayed. They
can be ignored by clicking the Hide compilation warnings button.
Close the dialog by clicking the close button:
Start the simulation by clicking the Start button:
44
-
After a few seconds, the two nodes have started and printed out
their Hello World message. The simulation can now be stopped by
pressing the Pause button.
Congratulations! You have just compiled and simulated your first
Contiki system!
45
-
[edit] Step 3: Run a data collection example
Data collection is the most commonly used sensor network
mechanism. With data collection, nodes report data to a sink node.
The Contiki distribution contains a quick-start data collection
example that does all the collection for you.
Go to the File menu and choose the Open simulation menu item,
and the Browse item. Open the example-collect.csc file in the
contiki-2.x/examples/rime directory:
Wait for the compilation to finish. The screen now shows the
data collection example:
46
-
Click the Start button in the Control Panel window. See the
collection network form in the Simulation Visualizer.
47
-
4.3. Cc bc xy dng ng dng u tin
This tutorial shows how to develop a simple Contiki program and
how to set up a Contiki project. We go through how to create a
project directory, write a simple Contiki application, compile and
run it in the MSPsim emulator, and on a Tmote Sky mote. If you
don't have a Tmote Sky mote available, it is enough to read how to
run the application in emulation.
In its simplest form, a Contiki project is a C source file and a
Makefile. The C source file contains the program code whereas the
Makefile contains the rules for compiling the program code into a
Contiki executable.
We assume that we are using Instant Contiki and that we have
already logged into the system.
48
-
Contents[hide]
1 Step 1: Create a project directory 2 Step 2: Create the
Makefile 3 Step 3: Create the program file 4 Step 4: Set the
default target 5 Step 5: Compile the project 6 Step 6: Test the
project in MSPsim
7 Step 7: Run the project on Tmote Sky hardware
[edit] Step 1: Create a project directory
Each Contiki project resides in its own project directory. We
first create the directory in a convenient location. Open a
terminal by clicking on the terminal icon on the menu bar at the
top of the screen.
Create two directories: one for holding Contiki projects, and
one for holding the Hello, World project. Do this by issuing the
following commands (if you choose to use different directories
change the paths accordingly):
mkdir projectscd projectsmkdir hello-world-projectcd
hello-world-project
49
-
[edit] Step 2: Create the Makefile
Once we have created our project directory, we can create the
Makefile that will ensure the program is compiled correctly.
Creating the Makefile can be done in any of the available editors
(gedit, vi, emacs, ). In this example, we use gedit. Issue the
command
gedit Makefile &
50
-
We type the following text into the Makefile:
CONTIKI=/home/user/contiki-2.xinclude
$(CONTIKI)/Makefile.include
Save the file by clicking on the Save button. The Makefile will
include the Contiki Make structure and automatically compile your
project. We can now start writing our first program.
51
-
[edit] Step 3: Create the program file
We have now finished creating the Makefile. Next, we create out
main program file by creating a new document called
"hello-world.c". If you are using gedit, click on the New document
menu item in the File menu.
Now we have to populate our program file by typing the
following:
#include "contiki.h"
PROCESS(hello_world_process, "Hello world
process");AUTOSTART_PROCESSES(&hello_world_process);
PROCESS_THREAD(hello_world_process, ev, data){ PROCESS_BEGIN();
printf("Hello, world!\n"); PROCESS_END();}
We will briefly outline what this actually does:
The first line includes the Contiki C header. This is needed to
include all the relevant Contiki definitions and libraries.
52
-
The second line declares a Contiki process. This process has a
variable name (hello_world_process), and a string name ("Hello
world process"). The string name is used in debugging and for the
Contiki shell's ps command.
The third line tells Contiki that the hello_world_process should
be automatically started when Contiki boots.
The fourth line defines the hello_world_process. The ev and data
arguments to the PROCESS_THREAD() macro are the variable names that
hold the event number and the event data for subsequent events that
this process may receive.
The process itself is defined between the PROCESS_BEGIN() and
PROCESS_END() macros. In this case, the process only prints out the
hello, world message and exits.
This should all look somewhat like this:
We save this as hello-world.c:
53
-
[edit] Step 4: Set the default target
We switch to the terminal window. We first set the default
platform for this project to the sky target by issuing the
command
make TARGET=sky savetarget
54
-
[edit] Step 5: Compile the project
Now we can compile our new project for the first time. We do
this by issuing the command
make hello-world
This will cause the entire Contiki system to be compiled and a
lot of compilation output to be produced in the terminal window.
When compilation has finished, the terminal window looks like
this:
55
-
[edit] Step 6: Test the project in MSPsim
We can now run the Hello, world program in MSPsim by running
make hello-world.mspsim
This brings up a number of windows. You may need to rearrange
the windows to see what is going on:
56
-
The USART1 Port Output window shows the output from the serial
port of the mote. We see the Contiki boot-up messages as well as
our Hello, world message.
The SkyGui window shows the Tmote Sky mote. The reset and user
buttons can be pressed by clicking on them. We press the reset
button to see the output:
The boot-up messages and our Hello, World message shows up again
in the serial port window.
We stop the simulation by bringing up the terminal window, hold
the Ctrl key, and press the C key.
57
-
[edit] Step 7: Run the project on Tmote Sky hardware
Insert a Tmote Sky mote into the USB port of the PC. We now have
to configure VMware Player to access the Tmote Sky. This is done
through the Virtual Machine menu as shown below:
The Tmote Sky should now be accessible from within Instant
Contiki. Test this by running
make sky-motelist
If the Tmote Sky shows up, it is possible to reprogram it with
the Hello, World application:
58
-
make hello-world.upload
This will upload Contiki and the Hello, World program to the
Tmote Sky mote.
When the programming is complete, Contiki and the Hello, world
program should now be running on the Tmote Sky. See the output of
the Tmote Sky by running
make login
In VMware garbage characters may sometimes appear at this point
appear:
59
-
To see the Hello, world program in action, we press the reset
button on the Tmote Sky. The following output should appear:
Congratulations! A complete Contiki project now runs on your
Tmote Sky mote!
Hold the Ctrl key and press C to get back to the prompt.
4.4. V d lm vic vi Shell trong Contiki
Example-Shell GuideFrom ContikiWikiJump to: navigation,
search
TODO: Cross-check for contiki 2.5 release
The following is an extract from the Contiki tutorial given at
the ACM/IEEE conference on Information Processing in Sensor
Networks (IPSN) in April 2009.
[edit] Contiki Mote Shell
The Contiki shell is an interactive on-mote shell that provides
a set of commands for interacting with the system. The shell can be
accessed either over a serial USB connection or over a network
using Telnet. In this tutorial, we first run the shell over a USB
serial connection.
First, compile and upload the shell:
60
-
cd cd contiki-2.x cd examples cd sky-shell make
sky-shell.upload
Wait for the compilation and uploading to finish. To connect to
the shell over the USB port, run:
make login
Press the return key to get a prompt. (We have found that with
Instant Contiki running in VMWare, garbage characters are sometimes
printed out when connecting. They can be safely ignored.) Next, we
try a few shell commands. To get a list of available commands,
run:
help
Try a bunch of other commands:
help sense | senseconv power | powerconv ls format echo test |
write file ls read file nodeid blink 10 reboot repeat 2 2 { echo
again } & ps
The power command prints out the current power profile from
Contiki's software-based power profiler. To print out the power
profile in decimal digits, run:
power | binprint
The output will look something like: 12 236 0 37421 0 4 0 380 0
0 0 380 0
The numbers that the power command outputs are:
1. The number of data items that follow, to make automatic
parsing of the output easier
2. CPU low-power mode time, least significant 16 bits 3. CPU
low-power mode time, most significant 16 bits 4. CPU active mode
time, least significant 16 bits 5. CPU active mode time, most
significant 16 bits 6. Radio transmission time, least significant
16 bits 7. Radio transmission time, most significant 16 bits 8.
Radio listen time, least significant 16 bits 9. Radio listen time,
most significant 16 bits 10. Radio transmission time in idle
listening, least significant 16 bits 11. Radio transmission time in
idle listening, most significant 16 bits
61
-
12. Radio listen time in idle listening, least significant 16
bits 13. Radio listen time in idle listening, most significant 16
bits
The output of the power command can be used to compute an
estimate of the mote's power consumption by multiplying the time
with pre-measured current draw metrics [11]. Because the power
command only measures the time during which the peripherals are
switched on, the power consumption estimate is not affected by
environmental noise such as temperature differences nor by noise
due to subtle differences between different versions of
hardware.4.5. Giao thc thu thp d liu trn Contiki
Simple Contiki Data Collection on the Tmote SkyFrom
ContikiWikiJump to: navigation, search
This tutorial shows how to collect sensor data using Contiki's
built-in data collection algorithm. The Contiki data collection is
as simple as it gets if you simply want to collect data without
having to do any programming or worry about networking.
This tutorial shows how to run CollectView under Instant
Contiki, but CollectView runs under native Windows and Linux too.
Download it and try it out!
Contents[hide]
1 Run Contiki on the Tmote Sky 2 Start the CollectView program 3
Upload Contiki to the Tmote Sky motes 4 Start the data collection
network 5 View the collected data
6 Summary
[edit] Run Contiki on the Tmote Sky
If you have a few Tmote Skys around, you can quickly use Contiki
to collect sensor data from them via the Contiki CollectView
program.
[edit] Start the CollectView program
In Instant Contiki, click on the Terminal icon. In the terminal
window, enter the command
cd Desktop/contiki-collect-sky-2.5.rc1/
62
-
In this directory, run the command
java -jar collect-demo.jar
63
-
This opens the CollectView application. If there are no Tmote
Sky motes connected to the PC, a dialog will be displayed. Close
this with its OK button.
[edit] Upload Contiki to the Tmote Sky motes
Our first step is to upload Contiki onto your Tmote Skys. The
CollectView program can upload Contiki in parallel to all motes
connected to the PC.
Plug in your Tmote Sky motes into the USB ports of your PC. A
USB hub can be used to increase the number of available USB
ports.
Next, we need to hook the motes into Instant Contiki. We do this
in the VMware Virtual Machine menu:
Connect all motes to Instant Contiki.
64
-
Now we use CollectView to upload Contiki to all connected motes.
Click the "Program Nodes..." button:
You will now be asked whether you want to program the nodes.
Obviously you do, so click the Yes button:
65
-
The uploading progress is shown in the window that opens.
Click the Close button when programming has finished.
66
-
If you need to program more motes, repeat the process. When all
motes have been programmed, remove all motes except one.
[edit] Start the data collection network
After having programmed all the nodes, we will now connect to
the nodes via the serial connection (via the node which is still
plugged into your computer). After that we will instruct the
network what exactly we want it to do and watch Contiki's magic
unfold.
So lets get started. At first, click the Connect to serial
button. This will connect your PC to the node and allow you to give
commands to the node as well as receive status updates from the
node itself. The "Connect" button will change to a "Disconnect"
button once successfully connected.
Once we have connected to the node, we can click on the "Start
collect" button. This will instruct the nodes to form a data
collection network, with the mote connected to the USB port as the
sink.
67
-
Click the "Send command to nodes" button. This sends a command
to the nodes in the network that will cause them to periodically
send sensor data to the sink. This data will be picked up by the
CollectView program and displayed.
68
-
[edit] View the collected data
Your network should now be forming a data collection tree and
data should start arriving at the sink. We will now guide you
through the information that is being collected, which is nicely
displayed in some of the tabs.
After a minute or so, a number of node IDs should have showed up
in the leftmost column. These are the nodes that have reported data
to the sink, with time all the nodes should be displayed if the
radio interference isn't too outrageous (WiFi sometimes overpower
these poor little nodes).
Click on the "Network Graph" tab. This shows the structure of
the collection network. The nodes are moving around, so it is a
good idea to fix the position of the sink node. The sink node cna
be identified by looking to what node the arrows are pointing.
Right click on the node and select the "Fixed node position" menu
item to fix its position. To make it easier to right click on the
node, it might be a good idea to uncheck the "Update layout" menu
item first, as this will stop the movement of the nodes.
69
-
The "Node info" tab shows a table with information for each
node. Here you can see how many packages have been received, how
many hops nodes are away amongst other data.
70
-
The "Sensors" tab shows the sensor data from the network.
The "Power" tab shows the power consumption of the motes.
71
-
The "Network" tab shows the status of the network.
4.6. Giao thc RPL trn Contiki
Setting up a Low-Power IPv6/RPL NetworkFrom ContikiWikiJump to:
navigation, search
One of the defining features of Contiki is that it natively
supports the Internet Protocol. This tutorial shows how to set up a
Contiki low-power IPv6 network. We will use the Cooja network
simulator, but the principles are the same for a network with real
hardware.
For simplicity, we use Instant Contiki as the development
environment.
72
-
Contents[hide]
1 The easy way: load a ready-made simulation o 1.1 Start Cooja
and open the simulation o 1.2 Start the simulation o 1.3 View the
data from the network
2 Set up your own simulation o 2.1 Create a new simulation o 2.2
Create the sink node o 2.3 Create sender nodes o 2.4 Show mote
types in simulation visualizer o 2.5 Start the CollectView program
o 2.6 Start the simulation
o 2.7 View the data
[edit] The easy way: load a ready-made simulation
Before going through the entire process, we will show easy way:
loading an already existing simulation in Cooja.
[edit] Start Cooja and open the simulation
Start Cooja by double-clicking on the Run Cooja icon.
73
-
In Cooja, open the File menu, the Open simulation menu item, and
click on Browse
Go to the directory contiki-2.x/examples/ipv6/rpl-collect and
open the file collect-tree-dense-noloss.csc
74
-
[edit] Start the simulation
After Contiki has been compiled and the simulation has loaded,
the CollectView window will appear on top of the Cooja window.
Click on the Cooja window to bring it to the front.
This simulation is a 25 node network, running IPv6 with RPL
routing, where one node is the sink node to which the others
periodically send sensor data.
Click the Start button to start the simulation.
75
-
[edit] View the data from the network
After the simulation time has reached some 70000 ms, data will
begin to appear at the sink node. The sink node prints out the
received data on its serial port. The CollectView program listens
to this (emulated) serial port and displays the data, along with
information about the network.
Bring up the CollectView window to see the data. Click the
Network Graph tab to see the structure of the IPv6/RPL network.
Right-click on the sink node (called 1.1) and click on the Fixed
Node Position menu item to fix its position. It is helpful to first
uncheck the Update Layout menu item this will cause the nodes to
stop moving around.
76
-
Click on the Network tab to explore the parameters in the
network. Click the item on the top left to select all nodes.
77
-
The Node Info tab has more information about the nodes.
The Power tab shows the power consumption of the nodes in the
network.
78
-
[edit] Set up your own simulation
Now that we know how a running simulation looks like, we can set
up our own. We will use the same example C code as in the previous
simulation, but we will set up the simulation ourselves.
[edit] Create a new simulation
Close the Cooja window that we previously opened. Open a new
Cooja.
When Cooja has started, select the New simulation menu item on
the File menu.
79
-
Create a new simulation. Give it a nice name.
80
-
[edit] Create the sink node
We now create the sink node.
When the new simulation has opened, click on the Mote Types
menu, select the Create mote type menu item, and the Sky Mote Type
item.
Give the mote type a name, and click on the Browse button.
81
-
Go to the directory contiki-2.x/examples/ipv6/rpl-collect and
select the udp-sink.c file. This file contains the code for the
sink node.
Click the Compile button to compile the sink code.
82
-
When the Compilation is complete, the Compile button has changed
into a Create button. Click this button.
Create one sink node.
[edit] Create sender nodes
Next, we create the sender nodes. We create a new mote type for
this. We use the Sky Mote Type as before.
83
-
The sender node uses the udp-sender.c file from the same
directory as the sink node. Click the Compile and Create
buttons.
Create a few nodes say, 8.
84
-
[edit] Show mote types in simulation visualizer
In the Simulation Visualizer window, open the visualizer skin
menu. Select Mote IDs and Mote Type. This will show the node ID of
the nodes in the window and will color the nodes differently
depending on what mote type they are. In this example, we have two
mote types, one sink mote type and one sender mote type.
85
-
[edit] Start the CollectView program
Right click on the sink node. The sink node has node ID 1 and is
green. Click the Open mote plugin menu. Select the Collect View
menu item.
This brings up the CollectView window.
86
-
[edit] Start the simulation
Click on the Cooja window in the background and start the
simulation.
[edit] View the data
Let the simulation run for 100000 ms or so, then bring up the
CollectView window again. The sensors should now have begun
reporting data. The Node Info tab has information about the nodes.
As before, explore the other tabs as well.
87
-
4.7 Giao thc CoAP trn Contiki
REST-Example GuideFrom ContikiWikiJump to: navigation,
search
Open a terminal and go to "/examples/rest-example/"
directory.
MAIN EXAMPLE: rest-server-example.c : A RESTful server example
showing how to use the REST layer to develop server-side
applications (possible to run it over either COAP or HTTP) To use
COAP as the underlying application protocol, one should define
WITH_COAP = 1 in rest-example/Makefile. Otherwise, HTTP is used.
Look at the source code to see which resources are available.
(check the RESOURCE macros in the code). Each resource has a
handler function which is called by the REST layer to serve the
request. (i.e. "helloworld" resource has a handler function named
"helloworld_handler" which is called when a web service request is
received for "helloworld" resource.)
To run REST examples in COOJA under Linux
Accessing the server from outside: 1. Start COOJA and load the
simulation "rest-server-example.csc" by the following command.
make TARGET=cooja rest-server-example.csc
2. After loading the COOJA file, open another another terminal
pointing to the same directory and connect to the COOJA simulation
using tunslip6:
make connect-router-cooja
3. You need to use a COAP or HTTP client to interact with the
COOJA nodes running REST code. In this setting, two servers are
available: IP addresses are aaaa::0212:7402:0002:0202 and
aaaa::0212:7403:0003:0303. COAP uses 61616, whereas HTTP uses 8080
port in default configuration. First, ping the COOJA nodes to test
the connectivity.
ping6 aaaa::0212:7402:0002:0202 ping6
aaaa::0212:7403:0003:0303
HTTP Examples You can use curl as an http client to interact
with the COOJA motes running REST code.
curl -H "User-Agent: curl"
aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain
text curl -H "User-Agent: curl"
aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn
off the green led curl -H "User-Agent: curl"
aaaa::0212:7402:0002:0202:8080/.well-known/core -i
88
-
curl -X POST -H "User-Agent: curl"
aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed
COAP Examples You should run a COAP client on your computer. You
can use the URLs and methods provided above in HTTP examples to
test the COAP Server.
Accessing the server inside the sensor network: (Note: Provided
only for COAP implementation) Start COOJA and load the simulation
"coap-client-server-example.csc" by the following command.
make TARGET=cooja coap-client-server-example.csc
coap-client-server-example.csc : Runs rest-server-example.c as
the server (over COAP) (IP:aaaa::0212:7401:0001:0101) in one node
and coap-client-example.c as the client (IP:
aaaa::0212:7402:0002:0202) in another node. Client periodically
accesses resources of server and prints the payload.
Note: If the generated binary is bigger than the MOTE code size,
then you will get a "region text is full" error. Right now,
REST+HTTP example uses (Contiki + ContikiMAC + uIPv6 + RPL + HTTP
Server + REST Layer) which does not fit in Tmote Sky memory. To
save same code space and make the example fit, you can define
static routes rather than using RPL or use nullrdc rather than
ContikiMAC.
To run REST server on real nodes under Linux
1. Program the nodes with the rest-server-example
make TARGET=sky rest-server-example.upload
2. Disconnect the nodes and program one node with the RPL border
router
(cd ../ipv6/rpl-border-router && make TARGET=sky
border-router.upload)
3. Connect to the border router using tunslip6: [Once tunslip6
has been built it can be invoked directly with useful debugging
switches, $CONTIKI/tools/tunslip6 --help]
make connect-router
4. Reconnect the motes, reboot them and note their IP
addresses.
5. Test the connectivity by pinging them.
ping6
5. Remaining parts are the same with the COOJA example. (i.e. if
it is a COAP Server, it is available at :61616)
TI LIU THAM KHO
89
-
[1]. Jean-Philippe Vasseur, Adam Dunkels. Interconnecting Smart
Objects with IP: the next Internet. Morgan Kaufmann Publishers,
2010.[2]. Abeill J, Durvy M, Hui J, Dawson-Haggerty S. Lightweight
IPv6 Stacks for Smart Objects: the Experience of Three Independent
and Interoperable Implementations; January 2009. IPSO Alliance
White Paper 2.
[3]. Adam Dunkels, Bjrn Grnvall, and Thiemo Voigt. Contiki - a
lightweight and flexible operating system for tiny networked
sensors. In Proceedings of the First IEEE Workshop on Embedded
Networked Sensors (Emnets-I), Tampa, Florida, USA, November
2004.
[4]. Tmote Sky. [Online]. Available:
http://www.bandwavetech.com/download/tmote-sky-datasheet.pdf
[5]. http://www.sics.se/contiki/.
90
LI NI UMC LCChng 1. TNG QUAN V H IU HNH CHO MNG CC I TNG THNG
MINH1.1. Gii thiu v cc i tng thng minh1.2. H iu hnh nhng cho cc i
tng thng minh1.2.1. Chc nng ca h iu hnh1.2.2. Nhng thch thc nh hng
n vic thit k h iu hnh1.2.3. Cc thnh phn c bn ca mt h iu hnh1.2.4.
Kho st mt s h iu hnh1.2.5. Tiu ch phn loi h iu hnh1.2.6. Mt s vn cn
tip tc nghin cu
Chng 2. H IU HNH CONTIKI 2.1. Gii thiu v h iu hnh Contiki2.2. Cu
trc h iu hnh Contiki2.3. Kin trc phn lp h iu hnh Contiki2.4. Ngn xp
truyn thng trong h iu hnh Contiki2.4.1. Ngn xp uIP 2.4.2. Ngn xp
RIME
2.5. K thut lp trnh trn h iu hnh Contiki2.5.1. Lp trnh
Event-driven2.5.2. Lp trnh Multi-threaded2.5.3. Lp trnh
Prothreads2.5.4. So snh ba m hnh lp trnh trong Contiki2.6. Hot ng
nh thi trong Contiki2.7. Qun l b nh
2.8. Ci t v thc hnh vi Contiki2.8.1. Ci t Instant-Contiki2.8.2.
Chng trnh iu khin LED trn Tmote Sky
Chng 3. CC MODULE TRONG H IU HNH CONTIKIChng 4. XY DNG CHNG TRNH
NG DNG VI CONTIKI4.1. Gii thiu v cng c m phng Cooja
[edit] The COOJA Simulator[edit] Contiki level: the Contiki Mote
Type
[edit] Getting started[edit] Configuration Wizard[edit] Create a
Hello World simulation[edit] Save and load simulations[edit] COOJA
configs for Contiki development: sharing simulation configs[edit]
COOJA configs for Contiki development: simulation quickstart
[edit] Feature requests, bug reporting, and questions4.2. Hng dn
s dng Cooja
Get started with the Cooja simulatorFrom
ContikiWikiContents[edit] Step 1: Start Cooja[edit] Step 2: Ru