⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manual.html

📁 tinyos中文手册,是根据tinyos系统自带手册翻译过来的,虽然质量不好,但是对英文不强的人还是有用的
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<HR><H1><A NAME="sec:plugins">Tython, SimDriver, plugins, and the radio</A></H1><P>The core of SimDriver is an event bus. Java plugins can connect tothis event bus, receiving TOSSIM events and sending TOSSIM commands;many of the Tython abstractions are built on top of SimDriver plugins.TinyViz, the visualization environment, also uses plugins, so userscan write new visualizations. Correspondingly, there are two classesof SimDriver plugins: GUI and non-GUI. In addition, scripts canregister event handlers as well, as described in a later <ahref="#sec:event_handlers">section</a>.<P>Examples of GUI plugins include TinyViz's radio quality visualization,its communication visualization, and the basic plugin that draws moteson the screen. Examples of non-GUI plugins include the radio modelplugin and the packet logger plugin. GUI plugins are only loadablethrough TinyViz, while non-GUI plugins are accessible through Tython,and are used to actuate TOSSIM in response to network changes.SimDriver maintains a list of mote objects, which store statepertinent to the scripting environment, such as position and LEDvalues.<P>For example, calling mote.moveTo() modifies that mote's position. Thiscan change radio connectivity. The radio model notices the event thatindicates a Mote's position has changed, and then calculates theappropriate bit error rates based on new distances. The plugin willthen automatically communicate the new link qualities to TOSSIM, whichis internally unaware of mote position.<P><H2><A NAME="sec:radio_models">Position-based Radio Models</A></H2><P>Tython and TinyViz provide two position-based radio models: disc andempirical. The first models the network using "perfect disc"connectivity, at three different disc radii ("disc10", "disc100", and"disc1000"). Connectivity is perfect in that links are eithererror-free or not present, depending on whether the target is in oroutside of the disc. Although links are perfect, collisions can stilloccur, which will corrupt a packet.<P>The second model, "empirical", uses an error distribution based onempirical data. Essentially, for any particular distance there is adistribution of possible bit error rates: two links of identicallength can have different rates. These bit errors can cause a packetto be corrupted so that a receiver does not recover it properly. Linksare directed: the error rate from A to B is distinct from the ratefrom B to A. Roughly speaking, motes closer than 16 units will havegood connectivity, motes between 16 and 28 units will have highlyvariable and possibly asymmetric connectivity, and motes 30 units andhigher away will have poor connectivity.<P>These position models boil down to a simple function: given a distancebetween two motes, produce a bit error rate. For disc-based models,this is a simple cutoff, which results in symmetric and perfect links.For the empirical model, the function returns a sample from thedistribution corresponding to the distance.<P>Scripts can also explicitly specify connectivity qualities with the<TT>radio</TT> object. <TT>setLossRate(src, dest, prob)</TT> sets thebit error rate from ID <TT>src</TT> to ID <TT>dest</TT> to beprobability <TT>prob</TT>. <TT>comm.packetLossToBitError(prob)</TT>calculates bit error rates from a packet loss rate*.<P><DT><EM>*(This is actually a non-trivial calculation. It makes severalassumptions about the data link encoding (the mica stack, with SecDedbased encoding and a 9 bit start symbol) and packet length.Specifically, the packet error rate represents a 36-byte packet. Aspackets grow longer, their loss rate increases, as there is a set biterror rate.)</EM></DT><H2><A NAME="sec:update_radio">Updating the Radio Model</A></H2><P>It is important to keep in mind that there at any one time, there areactually two radio models for a given simulation. TOSSIM internallymaintains a table of bit error rates and uses this table to determineif any corruption should be applied to a radio message. In addition,SimDriver's Radio Model Plugin also maintains a table of error rates,driven by the above-described models. The correlation of these twodata structures is controllable by the script (and also by the TinyVizGUI).<P>The <TT>radio.setLossRate()</TT> function only affects the datastructure within SimDriver. Calling <TT>radio.publishModel()</TT>.propagates the entire connectivity graph to TOSSIM, which is<B>O(n<SUP>2</SUP>)</B>. Therefore, if a large number of links areupdated, it's best to only publish the model once, after all of theupdates.<P>To avoid this complexity and inefficiency, the Radio model alsosupports an <em>auto-publish</em> mode. When enabled, any changes tothe connectivity graph within SimDriver are immediately propagated toTOSSIM. Note however, that auto publish is not enabled by default, anda script must enable it if desired:<pre>radio.setAutoPublish(True)</pre><P>In addition, as mentioned previously, the radio models willautomatically recalculate connectivity rates when Mote objectsreposition themselves in the virtual simulator space. Note that ifmotes are moving around the space often, then auto-publish may resultin a large number of messages being sent to TOSSIM to update themodel.<H2><A NAME="sec:gui_labels">GUI Labels</A></H2><P>When using Tython with TinyViz, scripts can add annotations to motevisualizations. The <TT>setLabel()</TT> method of the mote class takesthree parameters: a string, an X offset, and a Y offset. For example,<P><PRE>motes[2].setLabel("queue depth: " + depth, 10, 0)</PRE><P>will put a small label 10 pixels to the right of mote 2 in the TinyVizvisualization panel, stating what its queue depth is (assuming thevariable <TT>depth</TT> has been set to this value previously).<HR><H1><A NAME="sec:complex_scripting">Complex Scripting</A></H1><P>The previous sections described how to control and monitor TOSSIMthrough Tython. However, the examples given were fairly simple. Byusing some of Python's expressive power, one can write much moreintricate and complex scripts.<P><H2><A NAME="sec:event_handlers"></A><BR>Event Handlers</H2><P>Fundamentally, both TOSSIM and </TT>SimDriver</TT> are event-drivenprograms. Internal to TOSSIM is a master event queue that manages thevarious simulated motes. When <TT>SimDriver</TT> is connected toTOSSIM, events are exchanged over the network connection. In this way,the <TT>SimDriver</TT> core is structured as a set of event handlersfor events both coming from TOSSIM and internally generated.<P>Many Tython primitives depend on being able to handle TOSSIM eventswhich update the known state about the simulation. For example, everytime a mote transmits a packet, TOSSIM sends an event to<TT>SimDriver</TT>. Internally, a plugin within <TT>SimDriver</tt>subscribes to packet event notifications and uses them to update the<TT>packets</TT> variable (discussed in a <AHREF="manual.html#sec:packets">previous section</A>).<P>Tython scripts can also register event handlers through the<TT>simcore.interp</TT> object. Event handlers must be functions thatexpect a single parameter, the event handled. Scripts registerhandlers with <TT>interp.addEventHandler()</TT>. The first parameteris the event handling function, the second, optional parameter is theJava event class type. Passing an event type will register the handlerfor only events of that type; omitting it will have it handle allevent types. For example:<P><PRE>def print_handler(event):  print event# Print all eventsinterp.addEventHandler(print_handler)# Print only LED eventsfrom net.tinyos.sim.event import LedEventinterp.addEventHandler(print_handler, LedEvent())</PRE><P>The <TT>net.tinyos.sim.event</TT> package contains all of the eventsthat TOSSIM generates (as well as a few which <TT>SimDriver</TT> usesinternally). The TOSSIM events are:<P><BR><P></P><DIV ALIGN="CENTER"><A NAME="227"></A><TABLE><CAPTION><STRONG>Table:</STRONG>TOSSIM Events</CAPTION><TR><TD><DIV ALIGN="CENTER"><TABLE CELLPADDING=3 BORDER="1"><TR><TD ALIGN="LEFT">Name</TD><TD ALIGN="LEFT">When?</TD></TR><TR><TD ALIGN="LEFT">ADCDataReadyEvent</TD><TD ALIGN="LEFT">When a mote receives an ADC reading</TD></TR><TR><TD ALIGN="LEFT">AttributeEvent</TD><TD ALIGN="LEFT">(Internal to SimDriver) When a mote attribute such as power, location, etc changes.</TD></TR><TR><TD ALIGN="LEFT">DebugMsgEvent</TD><TD ALIGN="LEFT">Each enabled <TT>dbg</TT> statement</TD></TR><TR><TD ALIGN="LEFT">InterruptEvent</TD><TD ALIGN="LEFT">Unique scheduled event. See the next <a href="sec:future_actions">section</a></TD></TR><TR><TD ALIGN="LEFT">RadioMsgSentEvent</TD><TD ALIGN="LEFT">When a mote finishes sending a radio packet</TD></TR><TR><TD ALIGN="LEFT">SimObjectDraggedEvent</TD><TD ALIGN="LEFT">When a mote is dragged in the GUI</TD></TR><TR><TD ALIGN="LEFT">SimObjectSelectedEvent</TD><TD ALIGN="LEFT">When a mote is selected in the GUI</TD></TR><TR><TD ALIGN="LEFT">TossimInitEvent</TD><TD ALIGN="LEFT">When Tython connects to TOSSIM</TD></TR><TR><TD ALIGN="LEFT">UARTMsgSentEvent</TD><TD ALIGN="LEFT">When a mote finishes sending a UART packet</TD></TR></TABLE><A NAME="table:events"></A></DIV></TD></TR></TABLE></DIV><P></P><BR><P>Event handlers can be useful for waiting for a condition, or forlogging information. For example, if you want to examine why a motesends a specific (perhaps malformed) packet, you can register ahandler for send events, and when the handler detects the packet,pause TOSSIM.<P><H2><A NAME="sec:future_actions"></A><BR>Future Actions</H2><P>Most of the events described in the previous section are issued when amote performs a certain action, or when the user makes an action inthe TinyViz GUI. TOSSIM also provides a command for external programsto receive a callback in the future, the <TT>InterruptEvent</TT>. Themethod <TT>sim.interruptInFuture()</TT> command sends a command toTOSSIM which schedules an <TT>InterruptEvent</TT> at a designated time(in 4MHz simulator ticks). The command also takes an integer ID as aparameter, which is then used to distinguish between theInterruptEvents that fire. The <TT>sim.getInterruptID()</TT> APIfunction can be used to get a unique id.<P>Here is an example use of these pause events, in this case where auser wants to print out mote state one minute into simulation:<P><PRE>import simcorefrom net.tinyos.sim.event import InterruptEventid = simcore.interp.getInterruptID()def time_handler(event):  global id  if event.get_id() == id:    print "Printing motes 1-20"    for i in range(0, 20):      print motes[i]simcore.interp.addEventHandler(time_handler, InterruptEvent)simcore.interp.interruptInFuture(60 * 4000000, id)</PRE><P>To help manage some of the event and id bookkeeping, the<TT>simutil</TT> package provides a simpler python class interface toaccomplish the same task:<P><PRE>import simutil, simtimedef time_handler():  print "Printing motes 1-20"  for i in range(0, 20):    print motes[i]simutil.CallIn(simtime.onemin, time_handler);</PRE><P>In this example, <TT>CallIn</TT> is a simple python class that managesthe pause and event identifiers. Besides the constructor method thatschedules the callback, it also exports a <TT>cancel()</TT> method,which allows a script to nullify a previously scheduled action.<P>In addition, note that the first parameter to <TT>simutil.CallIn</TT>uses the <TT>simtime</TT> module, specifically the <TT>onemin</TT>constant. <TT>simtime</TT> is a simple module that has predefinedconstants and conversion functions for manipulating 4MHz simulatorclock ticks. Also, the first parameter to <TT>CallIn</TT>is an offsetfrom the current time, so the above call will occur one minute in thefuture. This is in contrast to both <TT>interp.interruptInFuture</TT>and the simple wrapper <TT>simutil.CallAt</TT> which both take aabsolute time value.<P><H2><A NAME="sec:periodic_actions"></A><BR>Periodic Actions</H2><P>The <TT>InterruptEvent</TT> interface is the primary means by which ascript can schedule actions. Event handlers can also be made toreschedule themselves for periodic actions. For example, a simplechange to the previous handler will cause the mote state to be printedevery minute instead of only once:<P><PRE>def time_handler(event):

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -