📄 lesson5.html
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"><html><head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="GENERATOR" content="Mozilla/4.78 [en] (X11; U; Linux 2.4.7-10 i686) [Netscape]"> <title>Lesson 5: Simulating TinyOS Applications in TOSSIM</title></head><body bgcolor="#F8F8FF" link="#005BB7" vlink="#005BB7"> <table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="100%" hspace="4" ><tr BGCOLOR="#E0E0FF"><td WIDTH="100%"><b><font face="tahoma,arial,helvetica"><font size=-1>Lesson5: Simulating TinyOS Applications in TOSSIM</font></font></b><p><font face="tahoma,arial,helvetica">Last updated 25 August 2003</font></td></tr></table><table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="100%" hspace="4" ><tr BGCOLOR="#E0E0FF"><td WIDTH="100%"><b><nobr><font face="arial,helvetica">Introduction</font></nobr></b></td></tr></table><p>TOSSIM, the TinyOS simulator, compiles directly from TinyOS code. Builtwith <tt>make pc</tt>, the simulation runs natively on a desktop or laptop.TOSSIM can simulate thousands of nodes simultaneously. Every mote in asimulation runs the same TinyOS program.<p>TOSSIM provides run-time configurable debugging output, allowing a userto examine the execution of an application from different perspectiveswithout needing to recompile. TinyViz is a Java-based GUI that allows youto visualize and control the simulation as it runs, inspecting debug messages,radio and UART packets, and so forth. The simulation provides several mechanismsfor interacting with the network; packet traffic can be monitored, packetscan be statically or dynamically injected into the network. In this lesson,we won't be dealing with packet injection, which is discussed in <a href="lesson7.html">Lesson7</a> .<br> <table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="100%" hspace="4" ><tr BGCOLOR="#E0E0FF"><td WIDTH="100%"><b><nobr><font face="arial,helvetica">Building and Runningan Application</font></nobr></b></td></tr></table><p>TOSSIM is compiled by typing <tt>make pc</tt> in an application directory.In addition to the expected TinyOS components, a few simulator-specificfiles are compiled; these files provide functionality such as support fornetwork monitoring over TCP sockets.<p>Enter the <tt><a href="../../apps/CntToLedsAndRfm">apps/CntToLedsAndRfm</a></tt>directory. This application runs a 4Hz counter. It assumes a Mica motewhich has 3 LEDs. On each counter tick, the application displays the leastsignificant three bits of the counter on the three mote LEDs and sendsthe entire 16-bit value in a packet. Build and install the applicationon a Mica mote as in Lesson 4. You should see the LEDs blink.<p>Build a TOSSIM version of the application with <tt>make pc</tt>. TheTOSSIM executable is <tt>build/pc/main.exe</tt>. Type <tt>build/pc/main.exe--help</tt> to see a brief summary of its command-line usage. TOSSIM hasa single required parameter, the number of nodes to simulate. Type <tt>build/pc/main.exe1</tt> to run a simulation of a single node. You should see a long streamof output fly by, most of which refer to radio bit events. Hit control-Cto stop the simulation.<p>By default, TOSSIM prints out all debugging information. As radio bitevents are fired at 20 or 40 KHz, these are the most frequent events inthe simulator, they comprise most of the output in CntToLedsAndRfm. Giventhe application, we're more concerned with the packet output and mote LEDsthan individual radio bits. TOSSIM output can be configured by settingthe <tt>DBG</tt> environment variable in a shell. Type <tt>export DBG=am,led</tt>in your shell; this makes only LED and AM (active messages) packet outputenabled. Run the one-mote simulation again. You should see output similarto this:<br> <center><table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="80%" hspace="4" ><tr BGCOLOR="#E0E0E0"><td WIDTH="100%"><pre><tt>0: LEDS: Yellow off.0: LEDS: Green off.0: LEDS: Red off.0: Sending message: ffff, 4 ff ff 04 7d 08 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0000 00 00 00 00 00 00 00 00 00 00 00 3b f3 00 00 01 00 00 000: LEDS: Yellow off.0: LEDS: Green off.0: LEDS: Red on.0: Sending message: ffff, 4 ff ff 04 7d 08 21 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0000 00 00 00 00 00 00 00 00 00 00 00 ac e6 00 00 01 00 00 00 </tt></pre></td></tr></table></center><p>The sixth byte of the packet contains the least significant byte ofthe two byte counter; in this example the first packet is hex 0x20 (32),in the second, it's hex 0x21 (33). As the LEDs show the bottom three bitsof the counter, they are all off for the first packet and bit one is onfor the second.<p>Almost every message is preceded by a <tt>0:</tt>; this means that themessage pertains to the execution of mote 0. Run a simulation of two motes(<tt>build/pc/main.exe 2</tt>); after it runs for a few seconds, stop thesimulation with control-C. You should see messages for both mote 0 andmote 1.<p>Set <tt>DBG</tt> to <tt>crc</tt>. Run two mote simulation of <tt>CntToLedsAndRfm</tt>.You should see output indicating that both nodes are successfully receivingpackets from each-other.<p>The full set of DBG modes can be seen by typing <tt>build/pc/main.exe--help</tt>; they are listed at the bottom of the output.<br> <table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="100%" hspace="4" ><tr BGCOLOR="#E0E0FF"><td WIDTH="100%"><b><nobr><font face="arial,helvetica">Adding DebuggingStatements</font></nobr></b></td></tr></table><p>Four DBG modes are reserved for application components and debugginguse: <tt>usr1</tt>, <tt>usr2</tt>, <tt>usr3</tt>, and <tt>temp</tt>. InTinyOS code, debug message commands have this syntax:<br> <center><table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="80%" hspace="4" ><tr BGCOLOR="#E0E0E0"><td WIDTH="100%"><pre>dbg(<mode>, const char* format, ...);</pre></td></tr></table></center><p>The mode parameter specifies which under which DBG modes this messagewill be printed. The full set of modes can be found in <tt><a href="../../tos/types/dbg_modes.h">tos/types/dbg_modes.h</a></tt>.The format and following parameters specify the string to output and have<tt>printf()</tt>semantics. For example, open <tt><a href="../../tos/lib/Counters/Counter.nc">tos/lib/Counters/Counter.nc</a></tt>inyour editor. In <tt>Timer.fired()</tt>, add this line just before the returnstatement:<br> <center><table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="80%" hspace="4" ><tr BGCOLOR="#E0E0E0"><td WIDTH="100%"><pre>dbg(DBG_TEMP, "Counter: Value is %i\n", (int)state);</pre></td></tr></table></center><p>Set <tt>DBG</tt> to be <tt>temp</tt> and run a single mote simulation.You'll see the counter increment. In general, the DBG mode name in TinyOScode is the name used when you run the simulator, with <tt>DBG_</tt>prepended.For example, <tt>am</tt> is <tt>DBG_AM</tt>, <tt>packet</tt> is <tt>DBG_PACKET</tt>and <tt>boot</tt> is <tt>DBG_BOOT</tt>.<p>Just as you can enable multiple modes when running the simulator, asingle debug message can be activated on multiple modes. Each mode is abit in a large bitmask; one can use all of the standard logical operators(e.g. <tt>|</tt>, <tt>~</tt>) . For example, change the debug message youjust added to:<br> <center><table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="80%" hspace="4" ><tr BGCOLOR="#E0E0E0"><td WIDTH="100%"><pre>dbg(DBG_TEMP|DBG_USR1, "Counter: Value is %i\n", (int)state);</pre></td></tr></table></center><p>It will now be printed if either <tt>temp</tt> or <tt>usr1</tt> is enabled.Run the application to see this is the case.<br> <table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="100%" hspace="4" ><tr BGCOLOR="#E0E0FF"><td WIDTH="100%"><b><nobr><font face="arial,helvetica">Using gdb with TOSSIM</font></nobr></b></td></tr></table><p>One significant advantage of TOSSIM is that, because it runs nativelyon a PC, you can use traditional debugging tools such as <tt>gdb</tt>.However, because TOSSIM is a discrete event simulation for large numbersof motes, traditional step-through debugging techniques only work on anevent basis, and not cross-events.<p>Unfortunately, <tt>gdb</tt> is generally designed for C and not nesC;the component model of nesC means that a single command can have multipleproviders; referring to a specific command requires specifying the component,interface, and command. For example, to break on entry to the <tt>redOff</tt>command of the <tt>Leds</tt> interface of <tt>LedsC</tt>, one must type:<br> <center><table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="80%" hspace="4" ><tr BGCOLOR="#E0E0E0"><td WIDTH="100%"><pre> gdb build/pc/main.exe // start gdb(gdb) break *LedsC$Leds$redOffBreakpoint 1 at 0x804c644: file tos/system/LedsC.td, line 97.run 1 // run CntToLedsAndRfm with one mote</pre></td></tr></table></center><p>The leading * is necessary so <tt>gdb</tt> can parse the function namecorrectly; otherwise, it looks for the function <tt>LedsC</tt>.<p>Variables are similarly named. For example, to print the <tt>ledsOn</tt>variable of <tt>LedsC</tt> (which keeps track of on/off for the togglecommands), one types:<br> <center><table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="80%" hspace="4" ><tr BGCOLOR="#E0E0E0"><td WIDTH="100%"><pre>(gdb) print LedsC$ledsOn$3 = '\0' <repeats 999 times></pre></td></tr></table></center><p>Actually, this isn't quite correct, as the output above shows; in TOSSIM,<tt>ledsOn</tt>isn't a single <tt>uint8_t</tt>, but an array of 1000 of them. This ishow TOSSIM handles the state of many motes; it compiles fields to be arraysof <i>n</i> elements, where <i>n</i> is the maximum simulation size. Whenevera mote accesses a component's state, it indexes into the array based onits node ID. Therefore, to refer to a specific mote's state, one needsto index into the array properly:<br> <center><table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="80%" hspace="4" ><tr BGCOLOR="#E0E0E0"><td WIDTH="100%"><pre>(gdb) print LedsC$ledsOn[tos_state.current_node]$2 = 0 '\0'</pre></td></tr></table></center><p>We've supplied a simple <tt>gdb</tt> macro named <tt>VAR</tt> that handlesthis for you. Copy <tt><a href="../../tos/platform/pc/.gdbinit">tos/platform/pc/.gdbinit</a></tt>to your home directory (if there's already a <tt>.gdbinit</tt> there, justappend this file). Type <tt>quit</tt> and start <tt>gdb</tt> it again.Break in <tt>LedsC$Leds$redOff</tt> as before. Now, instead of the abovecommand line, you can type:<br> <center><table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="80%" hspace="4" ><tr BGCOLOR="#E0E0E0"><td WIDTH="100%"><pre>(gdb) VAR LedsC$ledsOn$3 = 0 '\0'</pre></td></tr></table></center><br> <table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="100%" hspace="4" ><tr BGCOLOR="#E0E0FF"><td WIDTH="100%"><b><nobr><font face="arial,helvetica">TinyViz: The TOSSIMUser Interface</font></nobr></b></td></tr></table><p>TinyViz provides an extensible graphical user interface for debugging,visualizing, and interacting with TOSSIM simulations of TinyOS applications.Using TinyViz, you can easily trace the execution of TinyOS apps, set breakpointswhen interesting events occur, visualize radio messages, and manipulatethe virtual position and radio connectivity of motes. In addition, TinyVizsupports a simple "plugin" API that allows you to write your own TinyVizmodules to visualize data in an application-specific way, or interact withthe running simulation.<br> <table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="100%" hspace="4" ><tr BGCOLOR="#E0E0FF"><td WIDTH="100%"><b><nobr><font face="arial,helvetica">Getting Started</font></nobr></b></td></tr></table><p>To get started, look at the <tt><a href="../../apps/TestTinyViz/TestTinyVizM.nc">apps/TestTinyViz</a></tt>application, which causes motes to periodically send a message to a randomneighbor. There isn't anything interesting about the application itself,but it will allow us to demonstrate the basic features of TinyViz. Go aheadand build the app with <tt>make pc</tt>.<p>To compile TinyViz, <tt>cd</tt> to the <tt>tools/java/net/tinyos/sim</tt>directory and type <tt>make</tt>. This will build the TinyOS program as<tt>tinyviz.jar</tt>,a stand-alone Java JAR file that you can run with the <tt>tinyviz</tt>script, found in this directory. Place this script on your PATH and youwill be able to run <tt>tinyviz</tt> directly from the command line.<p>Start up TinyViz, running the <tt>TestTinyViz</tt> app, as follows:<center><table BORDER=0 CELLSPACING=2 CELLPADDING=3 WIDTH="80%" hspace="4" ><tr BGCOLOR="#E0E0E0"><td WIDTH="100%"><pre><tt>export DBG=usr1tinyviz -run build/pc/main.exe 30</tt></pre></td></tr></table></center><p>You will see a window looking something like the following:<center><a href="imgs/tinyviz-screenshot1.gif"><img SRC="imgs/tinyviz-screenshot1-small.png" height=356 width=636></a></center><p>On the left is the graphical display of the sensor network. On the rightis the control panel where you can interact with a series of<b>plugins</b>that control how TinyViz works.<p>In the mote window, you can select motes by clicking on them, or selecta group of motes by dragging a rectangle around the group. You can movemotes around by dragging them around. Selecting motes is meaningful forcertain plugins, and other operations, such as toggling the power stateof the motes.<p>The "pause/play" button pauses or resumes the simulation. The "gridbutton" toggles grid-lines on the display. The "clear" button clears outthe display state. The "stop" button kills the simulation. The "delay"slider introduces a delay between the handling of each TOSSIM event, whichwill slow down the display -- useful in cases where you have a small numberof motes and want to watch the simulation operating in "real time". The"On/off" button toggles the power state of the selected motes.<p><b>Plugins</b><p>A TinyViz <b>plugin</b> is a software module that watches for eventscoming from the simulation -- such as debug messages, radio messages, andso forth -- and reacts by drawing information on the display, setting simulationparameters, or actuating the simulation itself, for example, by settingthe sensor values that simulated motes will read. TinyViz comes with asuite of built-in plugins, in the<tt><a href="../../tools/java/net/tinyos/sim/plugins">tools/java/net/tinyos/sim/plugins</a></tt>directory, and you can write your own. Not all plugins are used for allapplications -- for example, the <tt>Calamari</tt> plugin is used mainly
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -