📄 manual.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><!--Converted with jLaTeX2HTML 2002 (1.62) JA patch-1.4patched version by: Kenshi Muto, Debian Project.LaTeX2HTML 2002 (1.62),original version by: Nikos Drakos, CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><!-- Then edited (a lot) by hand by Mike Demmer --><HTML><HEAD><TITLE>Tython: Scripting TOSSIM</TITLE><LINK REL="STYLESHEET" HREF="manual.css"></HEAD><BODY><P><H1 ALIGN=CENTER>Tython: Scripting TOSSIM</H1><P ALIGN=CENTER><STRONG>Michael Demmer and Philip Levis</STRONG></P><P ALIGN=LEFT></P><DIV ALIGN="CENTER">Version 0.6<BR>February 5, 2004</DIV><BR><H2>Contents</A></H2><!--Table of Contents--><UL><LI><A HREF="manual.html#sec:intro">Introduction</A><LI><A HREF="manual.html#sec:getting_started">Getting Started</A><UL><LI><A HREF="manual.html#sec:invoking">Invoking SimDriver</A><LI><A HREF="manual.html#sec:connecting">Connecting to TOSSIM</A><LI><A HREF="manual.html#sec:execution">Scripting an Execution</A><LI><A HREF="manual.html#sec:pause">Pausing and Resuming</A><LI><A HREF="manual.html#sec:execution">Scripting an Execution</A><LI><A HREF="manual.html#sec:basic_commands">Some Basic Commands</A><LI><A HREF="manual.html#sec:script_files">Script Files</A></UL><LI><A HREF="manual.html#sec:caffeinated_snake">The Caffeinated Snake</A><UL><LI><A HREF="manual.html#sec:exploring_objects">Exploring Objects</A><LI><A HREF="manual.html#sec:importing_java">Importing and Instantiating Java</A><LI><A HREF="manual.html#sec:java_vs_python">Java vs. Python</A><LI><A HREF="manual.html#sec:packets">The Packet History Structure</A></UL><LI><A HREF="manual.html#sec:plugins">Tython, SimDriver, plugins, and the radio</A><UL><LI><A HREF="manual.html#sec:radio_models">Position-based Radio Models</A><LI><A HREF="manual.html#sec:update_radio">Updating the Radio Model</A><LI><A HREF="manual.html#sec:gui_labels">GUI Labels</A></UL><LI><A HREF="manual.html#sec:complex_scripting">Complex Scripting</A><UL><LI><A HREF="manual.html#sec:event_handlers">Event Handlers</A><LI><A HREF="manual.html#sec:future_actions">Future Actions</A><LI><A HREF="manual.html#sec:periodic_actions">Periodic Actions</A><LI><A HREF="manual.html#sec:behavior_objects">Behavior Objects</A><LI><A HREF="manual.html#sec:mote_variables">Mote Frame Variables</A></UL><LI><A HREF="javadoc/index.html">Javadoc Reference for simcore</A><LI><A HREF="manual.html#sec:design">Appendix A: Design Ideology</A><LI><A HREF="manual.html#sec:about">About this document ...</A></UL><!--End of Table of Contents--><P><H1><A NAME="sec:intro">Introduction</A></H1><P>Experimenting with and testing sensor networks is hard. TOSSIM, aTinyOS simulator, allows users to run and test algorithms, protocols,and applications in a controlled, reproducible environment. However,by itself a TOSSIM simulation is static. Instead of modeling behaviorssuch as motion or changing sensor readings, TOSSIM provides asocket-based command API for other programs to do so. On one hand, thiskeeps TOSSIM simple and efficient; on the other, it puts the burdenof writing complex real-world models on the user.<P>One solution to this problem is TinyViz, a GUI that communicates withTOSSIM over the socket API. With TinyViz, users can interact with asimulation through a GUI panel, by dragging motes and setting options.These actions can be difficult to reproduce exactly (e.g., dragging amote). Additionally, TinyViz can (as its name suggests) visualize whatgoes on in the network. Users can write TinyViz ``plugins'' in Java toextend the GUI's functionality and issue commands to TOSSIM. However,because users must precompile their plugins, this only allows limitedinteractivity.<P>Tython (or, Tinython) complements TinyViz's vizualization by adding ascripting interace to TOSSIM. Users can interact with a runningsimulation through TinyViz, a Tython console, or both simultaneously.Tython is based on Jython, a Java implementation of the Pythonlanguage. In addition to being a complete scripting language (withplenty of documentation, literature, tutorials, etc.), Jython makes itvery easy to import and use Java classes within Python. This allowsusers to access the entire TinyOS Java tool chain, including packetsources, MIG-generated messages, and TinyViz.<P>This document explains how to get started with Tython, and explains thelibrary functions that can be used to access TOSSIM or TinyViz. Itdoes not contain an in-depth Python tutorial: many of those can befound at <TT>www.python.org</TT>.<P><HR><H1><A NAME="sec:getting_started">Getting Started</A></H1><P>TinyViz and Tython sit on top of <TT>SimDriver</TT>, a javaapplication that manages interactions with TOSSIM. To get started,just run the application:<P><PRE>java net.tinyos.sim.SimDriver</PRE><P>The Jython environment may take a little while to come up the firsttime; it scans all of the TinyOS and system jar files, but caches theinformation for later invocations. <em>(Passing<TT>-Dpython.cachedir=[dir]</TT> to the application will select adirectory besides the current one for the cached files.)</em><P>After booting, Tython will give you a script prompt:<P><PRE>>>></PRE><P><H2><A NAME="sec:invoking">Invoking SimDriver</A></H2><P><TT>SimDriver</TT> can be invoked with a variety of options. Forexample, <TT>-gui</TT> has it start a GUI interface (TinyViz). Forbackwards compatibility, TinyViz arguments such as <TT>-run</TT> and<TT>-autorun</TT> are still supported. The tython console will startby default, though it can be disabled with the <TT>-noconsole</TT>option. For example,<P><PRE># java net.tinyos.sim.SimDriver -noconsole -gui -run main.exe 20</PRE><P>Will start TinyViz without a console and start a TOSSIM simulation (inthis case, <TT>main.exe</TT>) of 20 motes. This is identical tostarting TinyViz in previous releases.<P>The <TT>-h</TT> flag option will proint the full set of<TT>SimDriver</TT> command line options. For TOSSIM options, refer tothe TOSSIM reference manual.<P>Note: when <TT>SimDriver</TT> invokes TOSSIM, it always enables theTOSSIM lossy radio model.<P><H2><A NAME="sec:connecting"></A>Connecting to TOSSIM</H2><P>As in TinyViz, one easy way to get <TT>SimDriver</TT> to communicatewith the simulation is to first run TOSSIM with the <TT>-gui</TT>option, then run <TT>SimDriver</TT>. At startup, <TT>SimDriver</TT>will indicate that it has successfully connected to TOSSIM:<pre>Starting SimDriver... Initializing simulator objects...Loading simulator plugins...status: Connecting to simulator...status: Connection to simulator establishedSimComm: TossimInitEvent received... initializing system.Welcome to Tython. Type 'quit' to exit.ESC on a line by itself will pause/resume the simulator.>>></pre><P>At this point, the simulation is running and you can start interactingwith the running simulation using the Tython API.<H2><A NAME="sec:execution"></A>Scripting an execution</H2><P>You can also use the scripting interface to <TT>SimDriver</TT> to runTOSSIM for you. This is useful to allow you to write a script thatruns a number of simulations.<P>First off, just run <TT>SimDriver</TT>. At the Tython prompt, run thefollowing:<pre>>>> from simcore import *>>> sim.exec("build/pc/main.exe", 10)</pre><p>The <tt>exec</tt> function takes as arguments the path to theexecutable and the number of motes to run, much as the <tt>-run</tt>command line option does. This will execute a new TOSSIM process andconnect to it. <p>You can also pass additional arguments to TOSSIM as follows:<pre>>>> from simcore import *>>> sim.exec("build/pc/main.exe", 10, "-l=1.0 -b=1")</pre><H2><A NAME="sec:pause"></A><BR>Pausing and Resuming</H2><P>The Tython interpreter runs concurrently with the TOSSIM simulationitself, hence you can interact with the running simulation. However,to add some more control to interactions, you can also pause thesimulation to examine state, control several settings at once, etc.<P>The <TT>sim</TT> object has function interfaces to pause, resume, andprobe the paused state:<P><PRE>>>> from simcore import *>>> sim.resume()>>> sim.pause()>>> if (sim.isPaused()):... sim.resume()</PRE>As a shortcut, hitting the escape key and hitting return will togglethe pause state of the simulation. If the simulator is running, itwill pause it; if paused, it will resume it.<P><H2><A NAME="sec:basic_commands"></A><BR>Some Basic Commands</H2><P>In addition to a basic Jython environment, Tython has several objectsthat provide functions to interact with TOSSIM. The previous sectionsshowed some of this, specifically how the <TT>sim</TT> object can beused to execute, pause and resume a simulation.<P>Notice that in the previous examples, the <tt>simcore</tt> module wasimported into the running environment. This module is, appropriatelyenough, the core interface that is used to interact with the Tythonenvironment. This module is in fact not Python code at all, instead, asingle instance of each class of the Java<tt>net.tinyos.sim.script.reflect</tt> package is created and is boundinto the <tt>simcore</tt> module.<P>Some example commands are shown in the following table. The <ahref="javadoc/index.html">javadoc</a> for the reflect package liststhe complete interface.<P><BR><P></P><DIV ALIGN="CENTER"><TABLE><CAPTION><STRONG>Table:</STRONG>Basic Tython Commands</CAPTION><TR><TD><P><DIV ALIGN="CENTER"></DIV><P><DIV ALIGN="CENTER"><TABLE CELLPADDING=3 BORDER="1"><TR><TD ALIGN="LEFT">Name</TD><TD ALIGN="LEFT">Effect</TD></TR><TR><TD ALIGN="LEFT">sim.pause()</TD><TD ALIGN="LEFT">Pause TOSSIM</TD></TR><TR><TD ALIGN="LEFT">sim.resume()</TD><TD ALIGN="LEFT">Resume TOSSIM</TD></TR><TR><TD ALIGN="LEFT">motes[i].turnOn()</TD><TD ALIGN="LEFT">Turn mote i on</TD></TR><TR><TD ALIGN="LEFT">motes[i].turnOff()</TD><TD ALIGN="LEFT">Turn mote i off</TD></TR><TR><TD ALIGN="LEFT">motes[i].moveTo(x, y)</TD><TD ALIGN="LEFT">Move mote i to <I>x,y</I></TD></TR><TR><TD ALIGN="LEFT">motes[i].move(x,y)</TD><TD ALIGN="LEFT">Move mote i <I>x,y</I> from its current position</TD></TR><TR><TD ALIGN="LEFT">comm.setSimRate(rate)</TD><TD ALIGN="LEFT" >Set the simulator rate to <I>rate</I><BR>(This is identical to TOSSIM's <TT>-l</TT> option)</TD></TR><TR><TD ALIGN="LEFT">radio.setLossRate(senderID, receiverID, prob)</TD><TD ALIGN="LEFT">Set the radio loss rate between two motes</TD></TR><TR><TD ALIGN="LEFT">comm.sendRadioMessage(mote, time, message)</TD><TD ALIGN="LEFT">Deliver message to mote over Radio</TD></TR><TR><TD ALIGN="LEFT">comm.sendUARTMessage(mote, time, message)</TD><TD ALIGN="LEFT">Deliver message to mote over UART</TD></TR></TABLE><A NAME="table:basic_commands"></A></DIV></TD></TR></TABLE></DIV><P></P><BR><H2><A NAME="sec:script_files"></A><BR>Script Files
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -