58
TinyViz Programming TinyViz Programming Speaker Speaker Fish (Yun-Shih Fish (Yun-Shih Hsu) Hsu)

TinyViz Programming

  • Upload
    denzel

  • View
    103

  • Download
    2

Embed Size (px)

DESCRIPTION

TinyViz Programming. Speaker : Fish (Yun-Shih Hsu). Install TinyOS. http://www.tinyos.net. Install TinyOS. Download TinyOS. Tutorial. Install TinyOS. Install TinyOS. Install TinyOS. Install TinyOS. Install TinyOS. Install TinyOS. TinyOS 1.1.0 Stable, but dated TinyOS 1.1.11 - PowerPoint PPT Presentation

Citation preview

Page 1: TinyViz Programming

TinyViz ProgrammingTinyViz Programming

SpeakerSpeaker :: Fish (Yun-Shih Hsu)Fish (Yun-Shih Hsu)

Page 2: TinyViz Programming

Install TinyOSInstall TinyOShttp://www.tinyos.net

Page 3: TinyViz Programming

Install TinyOSInstall TinyOS

Download TinyOS

Tutorial

Page 4: TinyViz Programming

Install TinyOSInstall TinyOS

Page 5: TinyViz Programming

Install TinyOSInstall TinyOS

Page 6: TinyViz Programming

Install TinyOSInstall TinyOS

Page 7: TinyViz Programming

Install TinyOSInstall TinyOS

Page 8: TinyViz Programming

Install TinyOSInstall TinyOS

Page 9: TinyViz Programming

Install TinyOSInstall TinyOSTinyOS 1.1.0

Stable, but datedTinyOS 1.1.11

TinyOS 1.1.11-3isLess tested, but fresh

Page 10: TinyViz Programming

Install TinyOSInstall TinyOS

Page 11: TinyViz Programming

Install TinyOSInstall TinyOS

Page 12: TinyViz Programming

Install TinyOSInstall TinyOSSetup Cygwin with TinyOSExecute Cygwin

Page 13: TinyViz Programming

About Cygwin / LinuxAbout Cygwin / Linux$ : command sign

$lsls : list files and directories in current directo

rypwd : show the path of current directory fro

m rootman : search information from manual

man [command / library]man grep

Page 14: TinyViz Programming

About Cygwin / LinuxAbout Cygwin / Linuxcd : change directory

cd [directory path]cd ./tinyos-1.xcd /opt/tinyos-1.x

grep :grep [-r] “string” [filename]

grep “Fish” TestTinyViz.ncgrep “Fish” *grep -r “Fish” *

Page 15: TinyViz Programming

About Cygwin / LinuxAbout Cygwin / Linux[Tab]

When typing command, [Tab] key could complete the file name or directory name

If there are many file / directory name are similar, [Tab] key will list all of files and directories which are match the non-complete name we typed

Page 16: TinyViz Programming

Upgrade CygwinUpgrade Cygwin$cd /opt[/opt]$

cvs -z3 -d:pserver:anonymous

@tinyos.cvs.sourceforge.net:

/cvsroot/tinyos co -P tinyos-1.x

TinyOS 1.1.15

Page 17: TinyViz Programming

Make TinyVizMake TinyVizCygwin

$/opt/tinyos-1.x/tools/java/net/tinyos/sim$make all

Root of Cygwin in WindowsC:\Program Files\UCB\cygwin

Page 18: TinyViz Programming

Using TinyVizUsing TinyViz$export

PATH=$PATH:/opt/tinyos-1.x/tools/java/net/tinyos/sim

Edit /etc/profileat the end of file add

export PATH=$PATH:/opt/tinyos-1.x/tools/java/net/tinyos/sim

At the end line of file is empty line

Page 19: TinyViz Programming

Using TinyVizUsing TinyViz$cd /opt/tinyos-1.x/appsFor example, we want to use Blink module

$cd ./blink$make pc$tinyviz –run ./build/pc/main.exe 10

Page 20: TinyViz Programming

Structure of TinyVizStructure of TinyVizAll of files about TinyViz are in

/opt/tinyviz-1.x/tools/java/net/tinyos/sim

主要程式位置都在上述之目錄有關 plugin 的程式都在 ./plugin有關 event 的程式都在 ./event

Page 21: TinyViz Programming

TinyViz TinyViz 主程式主程式TinyViz.java

TinyViz tv;

SimDriver.javaSimDriver driver;

Page 22: TinyViz Programming

TinyViz mote TinyViz mote 相關相關Layout

MoteLayoutPlugin.java

Mote 的長相,以及底圖顏色等MoteSimObject.java

螢幕以及地圖位置的比例轉換CoordinateTransformer.java

Page 23: TinyViz Programming

TinyViz plugin TinyViz plugin 相關相關呼叫各個 plugin

SimProtocol.java新增 Plugin 的 GUI 時,可以研究如何修改這部分

其餘相關 plugin./plugin/

例如,要修改 Debug Message 的 Plugin ,就要修改DebugMsgPlugin.java

其中部分物件與 ./MessageList.java 有關

Page 24: TinyViz Programming

TinyVizTinyViz為了確認修改的值是否正確

可以多利用System.out.println();

Page 25: TinyViz Programming

TinyViz & NesCTinyViz & NesC可以參考 Blink 、 PongMsg 、 TestTivyViz

其中 TestTinyViz 中,包含 FakeLocation 以及 TestTinyViz

Page 26: TinyViz Programming

NesC ProgrammingNesC Programming

Page 27: TinyViz Programming

NesC ProgrammingNesC Programming基礎的 NesC Programming

參考 Tutorial參考 /opt/tinyos-1.x/apps/ 裡的程式

Page 28: TinyViz Programming

NesC ProgrammingNesC Programming所有要用的變數只能宣告成全域變數

NesC 不存在區域變數

event result_t abc { uint8_t a; return SUCCESS;}

command result_t cba { uint8_t b; return SUCCESS;}

Page 29: TinyViz Programming

NesC ProgrammingNesC Programming變數最好給予初始值

以免使用時有不正常的數值存在變數初始只能在 StdControl.init()

Page 30: TinyViz Programming

NesC ProgrammingNesC Programming變數型態

unsigned integeruint8_t 、 uint16_t 、 uint32_t

signed integerInt8_t 、 int16_t 、 int32_t

布林回傳型態result_t

SUCCESSFAULT

有無其他型態不確定

Page 31: TinyViz Programming

NesC ProgrammingNesC Programming確定可用之基本功能

迴圈for

判斷式ifelseelse if

其他breakcontinue

Page 32: TinyViz Programming

NesC ProgrammingNesC ProgrammingNesC programming 除了基本的 *.nc 以及

*M.nc 以外,可以使用標頭檔 (*.h)需要使用其他變數型態,或是需要自定義一些結構等等

Page 33: TinyViz Programming

NesC ProgrammingNesC Programming*.h

不可以使用 #define常在標頭檔宣告使用的東西

enumtypedef struct

enum{

a = 7,b = 8,c= 9,

};

typedef struct test{

uint16_t a;uint8_t b;double src_x;double src_y;

}test_t;

Page 34: TinyViz Programming

NesC ProgrammingNesC Programming*.h

標頭檔裡大致上是可以使用 C 的變數型態*.h 和 *.nc 以及 *M.nc 一樣,其檔案最後一行必須是空白行,若沒有留空白行,會 compile錯誤

Page 35: TinyViz Programming

NesC ProgrammingNesC Programming標頭檔的引入方式

在 *.nc 中includes [ 標頭檔名 ];Ex: includes test; ( 假設標頭檔名為 test.h)

Page 36: TinyViz Programming

NesC ProgrammingNesC ProgrammingNesC 可以將已經寫好的 module 直接引入使用Ex: 有另一個寫好的 module Y.h 、 Y.nc

Y.nc :

includes Y;

interface {command result_t test();}

Y.h :

uint8_t cc;

Page 37: TinyViz Programming

NesC ProgrammingNesC Programming引入方式

Ex : X 要引入 module Y ,必須在 X.nc 和 XM.nc 加上

configuration X { provides interface Y;} implementation {

Y = XM;}

module XM {provides {

interface Y;}uses { }

}implementation {

command result_t Y.test(){

return SUCCESS;}

}

Page 38: TinyViz Programming

NesC ProgrammingNesC ProgrammingNesC 中有兩種型態的 task

command相當於固定的命令,可以在任何時候使用Ex :每秒執行一次 command A()

event臨時觸發的事件,只有在特定事件發生時才會啟動Ex :有接收到封包時,執行 event

event 發生時,將會插斷正在執行的 command

Page 39: TinyViz Programming

NesC ProgrammingNesC Programming為了避免因 event 發生而導致 command 的某些運算錯誤atomic

atomic {for(i=0; i<6; i++)

a+=i;}

atomic a = a * b + c;

Page 40: TinyViz Programming

NesC ProgrammingNesC Programmingatomic 使用時要注意

由於 atomic 可以保證一定做完才准許被插斷,因此有可能遺漏發生的 event

Ex :程式正在執行 command 中某個被 atomic包住的運算,此時有封包傳過來,會造成 mote無法接收此封包並進行處理。

所以 atomic 使用的時機必須恰當

Page 41: TinyViz Programming

NesC ProgrammingNesC Programming在 command 中,若有進行 assign 動作的程式碼,最好用 atomic 包起來,以免出錯

atomic a = a * b + c;

Page 42: TinyViz Programming

NesC ProgrammingNesC Programming如何進行封包傳遞

首先,必須在 *.h 內,宣告要使用的「頻道」為何

這個動作並非必要,但若程式中有要發送多種格式的封包時,每個使用的頻道就會不同,列在標頭檔可以清楚知道使用了哪些頻道

enum{

AM_InitMsg = 7,};

Page 43: TinyViz Programming

NesC ProgrammingNesC Programming在 *.nc 內, implementation 中,在 components內使用 GenericComm ,並且將自訂的函式與 GenericComm 中的函示連結關係發送和接收都要連結清楚

implementation {components GenericComm;…………………XM.SendInitMsg -> GenericComm.SendMsg[AM_InitMsg];XM.ReceiveInitMsg -> GenericComm.ReceiveMsg[AM_InitMsg];

}

Page 44: TinyViz Programming

NesC ProgrammingNesC Programming在 *M.nc 中

module XM {provides {

interface StdControl;………………

}uses {

interface SendMsg as SendInitMsg;interface ReceiveMsg as ReceiveInitMsg;

} }

Page 45: TinyViz Programming

NesC ProgrammingNesC Programming在 *M.nc 的 implementation 中,必須要有以下三個函式發送封包

發送完成

event result_t SendInitMsg.sendDone( TOS_MsgPtr sMsg, result_t success ){

return SUCCESS;}

call SendInitMsg.send(TOS_BCAST_ADDR, sizeof(InitMsg_t), msg);

Page 46: TinyViz Programming

NesC ProgrammingNesC Programming接收封包

event TOS_MsgPtr ReceiveInitMsg.receive( TOS_MsgPtr rMsg ){

………………return rMsg;

}

Page 47: TinyViz Programming

NesC ProgrammingNesC Programming*.h 必須自訂封包內容

typedef struct initMsg{

uint16_t src;double src_x;double src_y;

}initMsg_t;

Page 48: TinyViz Programming

NesC ProgrammingNesC Programming*M.nc 宣告全域變數

注意:星號必須貼著 struct 名稱

TOS_MsgPtr msg;TOS_Msg msgBuf;

initMsg_t* initMsg;

initMsg_t * initMsg;initMsg_t *initMsg;

Page 49: TinyViz Programming

NesC ProgrammingNesC Programmingtypedef struct TOS_Msg {// The following fields are transmitted/received on the radio.uint16_t addr;uint8_t type;uint8_t group;uint8_t length;int8_t data[TOSH_DATA_LENGTH];uint16_t crc;

uint16_t strength;uint8_t ack;uint16_t time;uint8_t sendSecurityMode;uint8_t receiveSecurityMode;} TOS_Msg;

Page 50: TinyViz Programming

NesC ProgrammingNesC Programming在 StdControl.init()

在需要傳送封包的地方,將需要傳遞的值塞入封包

atomic initMsg = (initMsg _t *)msgBuf.data;

initMsg->src=TOS_LOCAL_ADDRESS;

Page 51: TinyViz Programming

NesC ProgrammingNesC Programming讓 mote向模擬器取得資訊

我們必須利用 ADCC 的模組來協助我們傳遞資訊

Page 52: TinyViz Programming

NesC ProgrammingNesC Programming首先,在 *.h 宣告要使用的 port

enum {FAKE_LOCATION_X_PORT = 128,FAKE_LOCATION_Y_PORT = 129,FAKE_LOCATION_Z_PORT = 130,

};

Page 53: TinyViz Programming

NesC ProgrammingNesC Programming在 *.nc 內, implementation 中,在 compone

nts 內使用 ADCC ,並且將自訂的函式與 ADCC 中的函示連結關係

implementation {components ADCC;…………………XM.ADC_X -> ADCC.ADC[FAKE_LOCATION_X_PORT];

XM.ADC_Y -> ADCC.ADC[FAKE_LOCATION_Y_PORT];XM.ADC_Z -> ADCC.ADC[FAKE_LOCATION_Z_PORT];

}

Page 54: TinyViz Programming

NesC ProgrammingNesC Programming實際取得資料

command result_t Location.getLocation() {call ADC_X.getData();return SUCCESS;

}

Page 55: TinyViz Programming

NesC ProgrammingNesC ProgrammingTinyViz 部分

必須在適當的程式裡加上與 NesC溝通的部分Ex :取得 mote 的位置,必須在 LocationPlugin.

java 中加上

simComm.sendCommand(new SetADCPortValueCommand((short)mote.getID(), 0L, PORT_LOCATION_X, x));

public static final byte PORT_LOCATION_X = (byte)128;

Page 56: TinyViz Programming

NesC ProgrammingNesC ProgrammingTinyViz 利用 ADCC 與 mote溝通必須使用 t

ry-catch 才能正常使用

try {simComm.sendCommand(

new SetADCPortValueCommand((short)mote.getID(), 0L, PORT_Border, isBorder));

} catch (java.io.IOException ioe) {// Just ignore itreturn;

}

Page 57: TinyViz Programming

NesC ProgrammingNesC Programming注意:利用 ADCC向 TinyViz取得資料,如果不是利用 event驅動,而是使用 command通常為了驅動 command ,會將驅動條件放至 Ti

mer.fired() ,在 console mode 下,會有 warning產生,但是可以不理會他

Page 58: TinyViz Programming

NesC ProgrammingNesC Programming如果 mote 有資料想要傳給 TinyViz

可以利用 debug message 加上 MessagePlugindbg(DBG_USR1, “[ 格式 ]”, 變數 );

“[ 格式 ]”, 變數這部分同 C 的 printf(“[ 格式 ]”, 變數 )%d 為整數%x 為十六進位