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

📄 manual.html

📁 tinyos中文手册,是根据tinyos系统自带手册翻译过来的,虽然质量不好,但是对英文不强的人还是有用的
💻 HTML
📖 第 1 页 / 共 4 页
字号:
</H2><P>The previous examples of Tython scripting assumed the use of theconsole. While appropriate for interactive use, this is cumbersome forrepeated actions.<P>The builtin <tt>execfile</tt> command can be used to source a file andexecute the contents as Tython commands:<PRE>>>> execfile("myscript.py")</PRE><P>This will read and execute the contents of the specified file. As analternative, passing the command line argument <tt>-script</tt> toTython will cause it to automatically source and execute the namedscript file. For example:<PRE>java net.tinyos.sim.SimDriver -script "myscript.py"</PRE><HR><H1><A NAME="sec:caffeinated_snake">The Caffeinated Snake</A></H1><P>Jython's Java support allows a user to basically use it as a Javascripting language. It also provides the basic features of the Pythonenvironment, including builtin functions and data structures.<H2><A NAME="sec:exploring_objects">Exploring Objects</A></H2><P>As mentioned previously, the <tt>simcore</tt> module provides thebasic access to the simulator interactions. The builtin Python<TT>dir()</TT> function is a useful hook to see the set of functionsan object provides. First, type the name of the object:<P><PRE>>>> from simcore import *>>> simnet.tinyos.sim.script.reflect.Sim@5f3a5f53</PRE><P>This tells you that the <TT>sim</TT> object is actually an instance ofthe <TT>net.tinyos.sim.script.reflect.Sim</TT> Java class. To see thevariables and functions a <TT>Sim</TT> object provides, type<TT>dir(Sim)</TT>.<P><PRE>>>> dir(Sim)['__driver', 'argv', 'dumpDBG', 'exec', 'exit', 'getTossimTime', 'getWorldHeight', 'getWorldWidth', 'isPaused', 'pause', 'paused', 'reset', 'resume', 'setSimDelay', 'simDelay', 'stop', 'stopDBGDump', 'tossimTime', 'worldHeight', 'worldWidth']</PRE><P><TT>dir()</TT> can be used on both Java and Python objects. Forexample, <TT>dir(Sim)</TT> returns a Python list of strings, with eachelement being a method or attribute of the Sim class. So,<TT>dir(dir(Sim))</TT> lists the functions a Python list objectprovides:<P><PRE>>>> dir(dir(sim))['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']</PRE><P>Note that <TT>dir()</TT>'s semantics are slightly different for Pythonand Java. One can call <TT>dir()</TT> on a Python object to learn itsfunctions, but for Java, one must call it on the class. Specifically:<P><PRE>>>> simnet.tinyos.sim.script.reflect.Sim@5f3a5f53>>> dir(sim)[]>>> dir(Sim)['__driver', 'argv', 'dumpDBG', 'exec', 'exit', 'getTossimTime', 'getWorldHeight', 'getWorldWidth', 'isPaused', 'pause', 'paused', 'reset', 'resume', 'setSimDelay', 'simDelay', 'stop', 'stopDBGDump', 'tossimTime', 'worldHeight', 'worldWidth']>>> m = dir(Sim)>>> dir(m) ['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']</PRE><P>Calling <TT>dir()</TT> with no parameters lists the set of variables,functions, and modules currently bound in the Jython environment:<P><PRE>>>> dir()['Commands', 'Interp', 'Location', 'Mote', 'Motes', 'PacketType', 'Packets', 'Radio', 'Sim', 'SimBindings', 'SimReflect', '__doc__', '__driver', '__name__', 'comm', 'interp', 'location', 'motes', 'packets', 'radio', 'sim', 'simcore', 'sys']</PRE><P>Finally, calling <TT>dir()</TT> on a module lists the set of objectsand functions exposed by that module.</P><PRE>>>> import simcore>>> dir(simcore)['Commands', 'Interp', 'Motes', 'Packets', 'Radio', 'Sim', '__doc__', '__name__', 'comm', 'interp', 'motes', 'packets', 'radio', 'sim']</PRE><H2><A NAME="sec:importing_java">Importing and Instantiating Java</A></H2><P>Jython allows you to import Java classes with Python's import syntax.For example, to import <TT>java.util.Enumeration</TT>, or the entire<TT>java.util</TT> package:<P><PRE>>>> from java.util import Enumeration>>> from java.util import *</PRE><P>Python is a dynamically typed language. Therefore, variables do nothave type specifiers. While instantiating a <TT>StringBuffer</TT> inJava looks like this:<P><dt><em>(Note: from now on, Tython code will be shown as if it were in afile, that is, without a command line prompt. Each line could be justas easily typed into a console)</em></dt><P><PRE>StringBuffer buffer = new StringBuffer();</PRE><P>in Jython it looks like this,<P><PRE>buffer = StringBuffer()</PRE><P>Note, however, that Jython does not automatically import<TT>java.lang.*</TT>.<P>By importing Java classes, one can access the TinyOS Java toolchain:<P><PRE>from net.tinyos.message import *from net.tinyos.packet import *moteIF = MoteIF()msg = BaseTOSMsg()msg.set_addr(0)msg.set_type(5)moteIF.send(0, msg)</PRE><P>The above code will send a message of AM type 5 to mote 0 over thedefault MoteIF interface.<P>Access to the toolchain includes GUI-based programs. For example, onecan instatiate a SerialForwarder:<P><PRE>from net.tinyos.sf import *s = SerialForwarder([])</PRE><P>Note, however, that some classes (SerialForwader's GUI among them)expect to be stand-alone applications. This means that when you tellthem to quit, with the quit button, for example, they may cause theentire Jython environment to quit.<H2><A NAME="sec:java_vs_python">Java vs. Python</A></H2><P>Given that all Java objects are accessible through the pythoninterface, in many cases there is redundancy between the twolanguages, and there is more than one interface available for a givenprogramming task.<P>Being dynamically typed, the Jython environment loses many of thecorrectness benefits of the Java compilation process. For objects suchas strings, lists, and maps, it's often easier to use Python'sprimitives instead of Java's, as they are syntactically built into thelanguage. For example, compare the use of a Python map and a Javahashtable:<P><PRE># Javafrom java.util import *h = new Hashtable()h.put("Seed", getPacket())print(h.get("Seed"))# Pythonh = {}h["Seed"] = getPacket()print h["Seed"]</PRE><P>Similarly, one can use either Java or Python to write a file:<P><PRE># Javafrom java.io import *file = FileWriter("output.txt")file.write(packets[0])# Pythonfile = open("output.txt", "w")file.write(packets[0])</PRE><P>We've found that, for data collection and processing, the Pythonidioms are generally easier to use than Java. Among other things, itsdata structure primitives end up being a lot simpler and less errorprone. However as mentioned previously, Java allows you to interactwith all of the existing structure a TinyOS distribution provides,such as packet sources, message class generation, and interaction withTinyViz.<H2><A NAME="sec:packets"></A><BR>The Packet History Structure</H2><P>Much of the power of the Tython environment comes from the synergybetween the two languages. Specifically, the use of Java to implementinternal functionality, while presenting a Python interface forscripts.<P>The <TT>packets</TT> variable in the <tt>simcore</tt> module is anexample of this. This data structure allows scripts to access all ofthe packets that motes have transmitted. The variable is a dictionaryof lists, keyed by mote ID. Each list contains all of the packets agiven mote has sent, in temporal order (the first packet is the firstelement of the list).<P><PRE>print packets[3]</PRE><P>will print all of the packets mote 3 has sent. <P>Each element of a given list is a Java instance of<TT>net.tinyos.sim.event.RadioMsgSentEvent</TT>. This class is usedbecause it stores not only the message sent, but also metadata, suchas time. The time represents when the packet send <I>completed</I>,not when it began.<P><TT>RadioMsgSentEvent</TT> has three basic accessor functions:<TT>getMessage()</TT>, which returns a<TT>net.tinyos.message.TOSMsg</TT>, <TT>getMoteID()</TT>, whichreturns the sender's ID, and <TT>getTime()</TT>, which returns thesend time.<P><TT>packets</TT> is an immutable object; unlike normal dictionaries, ascript cannot assign to it. If a script needs to transform the packetdictionary, it can make a copy with <TT>packets.copy()</TT>, which isa normal Python dictionary and can be modified accordingly.<P>In addition to normal dictionary operations, <TT>packets</TT> has anextra function to support TinyOS messages, <TT>addPacketType(Messagemessage)</TT>. This function can create packet type specificreflections of <TT>packets</TT>. For example,<P><PRE>from net.tinyos.message import IntMsgpackets.addPacketType(IntMsg())</PRE><P>will create a new variable in the Tython environment,<TT>IntMsgs</TT>, which contains all of the messages in<TT>packets</TT> that are of type <TT>IntMsg</TT>, as defined byactive message type. This variable, <TT>IntMsgs</TT>, can be indexedand accessed just as <TT>packets</TT> is.<P>Note, however, that each element in the <TT>packets</TT> listscontains a <TT>TOSMsg</TT>; this means that in the above example,<TT>IntMsgs</TT> also has <TT>TOSMsgs</TT>; you cannot easilymanipulate the elements as if they were actually instances of<TT>IntMsg</TT>, by accessing their message type specific fields, etc.<P>In order to transform the <TT>IntMsgs</TT> into a dictionary of<TT>net.tinyos.message.IntMsg</TT>, you must call<TT>IntMsgs.downCast()</TT>. For example:<P><PRE>from net.tinyos.msg import IntMsgpackets.addPacketType(IntMsg())# Get a dictionary of IntMsgs, instead of TOSMsgsmyMsgs = IntMsgs.downCast()# Print the value in the first IntMsg that mote 0 transmittedprint myMsgs[0].pop().get_val()</PRE><P>Using a <TT>downCast()</TT> dictionary allows you to easily accessmessage fields. However, it discards the additional metadata (such astransmit time) that a <TT>RadioMsgSentEvent</TT> contains.<P>The variable defined by <TT>addPacketType()</TT> will update as moremotes send packets; it represents a filter on top of <TT>packets</TT>.The dictionary returned by <TT>downCast()</TT>, however, is static; itdoes not update as more motes send packets.

⌨️ 快捷键说明

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