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
TinyViz ProgrammingTinyViz Programming
SpeakerSpeaker :: Fish (Yun-Shih Hsu)Fish (Yun-Shih Hsu)
Install TinyOSInstall TinyOShttp://www.tinyos.net
Install TinyOSInstall TinyOS
Download TinyOS
Tutorial
Install TinyOSInstall TinyOS
Install TinyOSInstall TinyOS
Install TinyOSInstall TinyOS
Install TinyOSInstall TinyOS
Install TinyOSInstall TinyOS
Install TinyOSInstall TinyOSTinyOS 1.1.0
Stable, but datedTinyOS 1.1.11
TinyOS 1.1.11-3isLess tested, but fresh
Install TinyOSInstall TinyOS
Install TinyOSInstall TinyOS
Install TinyOSInstall TinyOSSetup Cygwin with TinyOSExecute Cygwin
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
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” *
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
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
Make TinyVizMake TinyVizCygwin
$/opt/tinyos-1.x/tools/java/net/tinyos/sim$make all
Root of Cygwin in WindowsC:\Program Files\UCB\cygwin
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
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
Structure of TinyVizStructure of TinyVizAll of files about TinyViz are in
/opt/tinyviz-1.x/tools/java/net/tinyos/sim
主要程式位置都在上述之目錄有關 plugin 的程式都在 ./plugin有關 event 的程式都在 ./event
TinyViz TinyViz 主程式主程式TinyViz.java
TinyViz tv;
SimDriver.javaSimDriver driver;
TinyViz mote TinyViz mote 相關相關Layout
MoteLayoutPlugin.java
Mote 的長相,以及底圖顏色等MoteSimObject.java
螢幕以及地圖位置的比例轉換CoordinateTransformer.java
TinyViz plugin TinyViz plugin 相關相關呼叫各個 plugin
SimProtocol.java新增 Plugin 的 GUI 時,可以研究如何修改這部分
其餘相關 plugin./plugin/
例如,要修改 Debug Message 的 Plugin ,就要修改DebugMsgPlugin.java
其中部分物件與 ./MessageList.java 有關
TinyVizTinyViz為了確認修改的值是否正確
可以多利用System.out.println();
TinyViz & NesCTinyViz & NesC可以參考 Blink 、 PongMsg 、 TestTivyViz
其中 TestTinyViz 中,包含 FakeLocation 以及 TestTinyViz
NesC ProgrammingNesC Programming
NesC ProgrammingNesC Programming基礎的 NesC Programming
參考 Tutorial參考 /opt/tinyos-1.x/apps/ 裡的程式
NesC ProgrammingNesC Programming所有要用的變數只能宣告成全域變數
NesC 不存在區域變數
event result_t abc { uint8_t a; return SUCCESS;}
command result_t cba { uint8_t b; return SUCCESS;}
NesC ProgrammingNesC Programming變數最好給予初始值
以免使用時有不正常的數值存在變數初始只能在 StdControl.init()
NesC ProgrammingNesC Programming變數型態
unsigned integeruint8_t 、 uint16_t 、 uint32_t
signed integerInt8_t 、 int16_t 、 int32_t
布林回傳型態result_t
SUCCESSFAULT
有無其他型態不確定
NesC ProgrammingNesC Programming確定可用之基本功能
迴圈for
判斷式ifelseelse if
其他breakcontinue
NesC ProgrammingNesC ProgrammingNesC programming 除了基本的 *.nc 以及
*M.nc 以外,可以使用標頭檔 (*.h)需要使用其他變數型態,或是需要自定義一些結構等等
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;
NesC ProgrammingNesC Programming*.h
標頭檔裡大致上是可以使用 C 的變數型態*.h 和 *.nc 以及 *M.nc 一樣,其檔案最後一行必須是空白行,若沒有留空白行,會 compile錯誤
NesC ProgrammingNesC Programming標頭檔的引入方式
在 *.nc 中includes [ 標頭檔名 ];Ex: includes test; ( 假設標頭檔名為 test.h)
NesC ProgrammingNesC ProgrammingNesC 可以將已經寫好的 module 直接引入使用Ex: 有另一個寫好的 module Y.h 、 Y.nc
Y.nc :
includes Y;
interface {command result_t test();}
Y.h :
uint8_t cc;
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;}
}
NesC ProgrammingNesC ProgrammingNesC 中有兩種型態的 task
command相當於固定的命令,可以在任何時候使用Ex :每秒執行一次 command A()
event臨時觸發的事件,只有在特定事件發生時才會啟動Ex :有接收到封包時,執行 event
event 發生時,將會插斷正在執行的 command
NesC ProgrammingNesC Programming為了避免因 event 發生而導致 command 的某些運算錯誤atomic
atomic {for(i=0; i<6; i++)
a+=i;}
atomic a = a * b + c;
NesC ProgrammingNesC Programmingatomic 使用時要注意
由於 atomic 可以保證一定做完才准許被插斷,因此有可能遺漏發生的 event
Ex :程式正在執行 command 中某個被 atomic包住的運算,此時有封包傳過來,會造成 mote無法接收此封包並進行處理。
所以 atomic 使用的時機必須恰當
NesC ProgrammingNesC Programming在 command 中,若有進行 assign 動作的程式碼,最好用 atomic 包起來,以免出錯
atomic a = a * b + c;
NesC ProgrammingNesC Programming如何進行封包傳遞
首先,必須在 *.h 內,宣告要使用的「頻道」為何
這個動作並非必要,但若程式中有要發送多種格式的封包時,每個使用的頻道就會不同,列在標頭檔可以清楚知道使用了哪些頻道
enum{
AM_InitMsg = 7,};
NesC ProgrammingNesC Programming在 *.nc 內, implementation 中,在 components內使用 GenericComm ,並且將自訂的函式與 GenericComm 中的函示連結關係發送和接收都要連結清楚
implementation {components GenericComm;…………………XM.SendInitMsg -> GenericComm.SendMsg[AM_InitMsg];XM.ReceiveInitMsg -> GenericComm.ReceiveMsg[AM_InitMsg];
}
NesC ProgrammingNesC Programming在 *M.nc 中
module XM {provides {
interface StdControl;………………
}uses {
interface SendMsg as SendInitMsg;interface ReceiveMsg as ReceiveInitMsg;
} }
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);
NesC ProgrammingNesC Programming接收封包
event TOS_MsgPtr ReceiveInitMsg.receive( TOS_MsgPtr rMsg ){
………………return rMsg;
}
NesC ProgrammingNesC Programming*.h 必須自訂封包內容
typedef struct initMsg{
uint16_t src;double src_x;double src_y;
}initMsg_t;
NesC ProgrammingNesC Programming*M.nc 宣告全域變數
注意:星號必須貼著 struct 名稱
TOS_MsgPtr msg;TOS_Msg msgBuf;
initMsg_t* initMsg;
initMsg_t * initMsg;initMsg_t *initMsg;
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;
NesC ProgrammingNesC Programming在 StdControl.init()
在需要傳送封包的地方,將需要傳遞的值塞入封包
atomic initMsg = (initMsg _t *)msgBuf.data;
initMsg->src=TOS_LOCAL_ADDRESS;
NesC ProgrammingNesC Programming讓 mote向模擬器取得資訊
我們必須利用 ADCC 的模組來協助我們傳遞資訊
NesC ProgrammingNesC Programming首先,在 *.h 宣告要使用的 port
enum {FAKE_LOCATION_X_PORT = 128,FAKE_LOCATION_Y_PORT = 129,FAKE_LOCATION_Z_PORT = 130,
};
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];
}
NesC ProgrammingNesC Programming實際取得資料
command result_t Location.getLocation() {call ADC_X.getData();return SUCCESS;
}
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;
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;
}
NesC ProgrammingNesC Programming注意:利用 ADCC向 TinyViz取得資料,如果不是利用 event驅動,而是使用 command通常為了驅動 command ,會將驅動條件放至 Ti
mer.fired() ,在 console mode 下,會有 warning產生,但是可以不理會他
NesC ProgrammingNesC Programming如果 mote 有資料想要傳給 TinyViz
可以利用 debug message 加上 MessagePlugindbg(DBG_USR1, “[ 格式 ]”, 變數 );
“[ 格式 ]”, 變數這部分同 C 的 printf(“[ 格式 ]”, 變數 )%d 為整數%x 為十六進位