📄 mate-manual.tex
字号:
\documentclass[12pt]{article}\usepackage{graphicx}\usepackage{graphics}\usepackage{multicol}\usepackage{epsfig,amsmath,amsfonts}\usepackage{xspace}\usepackage{subfigure}\makeatletter % Make '@' accessible. \oddsidemargin=0in % Left margin minus 1 inch.\evensidemargin=0in % Same for even-numbered pages.\marginparsep=0pt % Space between margin & text\renewcommand{\baselinestretch}{1.2} % Double spacing\textwidth=6.5in % Text width (8.5in - margins).\textheight=9in % Body height (incl. footnotes)\topmargin=0in % Top margin minus 1 inch.\headsep=0.0in % Distance from header to body.\skip\footins=4ex % Space above first footnote.\hbadness=10000 % No "underfull hbox" messages.\makeatother % Make '@' special again.\newcommand{\mate}{Mat\'{e}\xspace}\newcommand{\bomb}{Bombilla\xspace}\title{Mat\'{e}: Safe and Rapid Sensor Network Scripting}\author{Philip Levis\\pal@cs.berkeley.edu}\date{}\begin{document}%\fontfamily{cmss} % Make text sans serif.%\fontseries{m} % Medium spacing.%\fontshape{n} % Normal: not bold, etc.%\fontsize{10}{10} % 10pt font, 10pt line spacing \maketitle\vspace{2in}\begin{center}Version 1.0\\March 22, 2004\end{center}\fontfamily{cmr} % Make text Roman (serif).\fontseries{m} % Medium spacing.\fontshape{n} % Normal: not bold, etc.\fontsize{10}{10} % 10pt font, 10pt line spacing\thispagestyle{empty}\newpage\tableofcontents\newpage\section{Introduction}Programming sensor networks is difficult. They are large scale,distributed systems with limited resources, lossy communication, whosetesting and debugging is made especially difficult by their beingembedded in uncontrolled environments. Often, it is hard to know exactlywhat a certain algorithm or application will do without deploying it;changes that seem minor can greatly change system behavior.Deployment, however, is a time consuming and arduous process.Sensor networks have a wide range of users, most of whom are not expertprogrammers. However, these users need to use sensor networks in theirapplications, such as habitat or structural monintoring, and thereforeneed be able to program. This programming cannot involve many low-leveldetails of the system, or require expert knowledge of the system hardwareor a great deal of programming experience.\mate is a system designed to provide an accessible programminginterface to sensor network end users. The core of \mate is a tiny VMthat runs on top of TinyOS, a sensor network operating system. Thebytecode interpreter can be customized for specific applications ordeployments. End users write programs has simple, high-level scripts,which compile to the VM instruction set. These programs thenself-propagate through a network. Reprogramming only requiresintroducing a single copy of a new program. This copy will theninstall itself across the entire network.This document describes \mate and how to use it. Its purpose is toserve as an introduction to the system for users. It is divided intofour major sections, with increasing degrees of technicaldetail. Section~\ref{sec:tinyscript} describes the TinyScriptlanguage, and how one uses the Scripter tool to inject programs into a\mate network. This section is intended for end users, and requires noknowledge of TinyOS. Section~\ref{sec:building} describes how to builda \mate VM and scripting environment. As programs will be constrainedby decisions made during this process, it is intended for users whoare knowledgeable about the targeted application domain.Finally, Section~\ref{sec:extending} describes how to extend \mate VMs byadding new function primitives and execution events by writing TinyOScomponents in nesC.\section{Programming with \mate}There are two basic parts to programming a \mate network. The first isa \mate VM installed on the network's motes. The second is a Java UIfor writing and injecting programs into the network. \mate nesC code(for VMs) can be found in {\tt tos/lib/VM} while the Java tool codecan be found in {\tt tools/java/net/tinyos/script}.Section~\ref{sec:building} describes how users can build customizedVMs; to start, however, you may want to use Bombilla, a simple exampleVM. \bomb can be found in {\tt apps/Bombilla}. To get started, go to{\tt apps/Bombilla} and compile for your desired platform (e.g., {\ttmake pc} or {\tt make mica2}). This will build a TinyOS VM as well asa scripting environment.If you're using motes, then you should install the VM on a few nodeswith {\tt make reinstall.x} where {\tt x} is the moteID for eachnode. You shouldn't use a GenericBase as a programming point for a\mate network. For example, if you're using a serial port to send data, you shouldplug a \mate node into the programming board. To efficiently program anetwork, you need to be able to reliably install new programs on asingle node (e.g., over the serial cable). If you use a GenericBase,it's possible that some fragments of programs may be lost, preventingthe network from reprogramming.Once you've set up your programming point, start the Scripter from theBombilla directory (it must be from this directory, to correctly findall of the VM-specific configuration files), specifying thecommunication port. For example,\begin{verbatim}java net.tinyos.script.Scripter -comm tossim-serial\end{verbatim}starts a Scripter to talk to TOSSIM through a virtual serial port tomote 0. You can then write programs and inject them into a network.\section{End Users: TinyScript}\label{sec:tinyscript}\mate has an event-driven execution model. Every \mate VMhas a set of events that trigger execution, such as receiving apacket, or a timer firing. When one of these events occur, it triggersthe appropriate handler script to run. Handlers are written in asimple BASIC-like language called TinyScript.\subsection{Scripter}Scripter is a Java tool that compiles TinyScript programs to theinstruction set of a \mate VM. As \mate VMs are customizable, programsmust be compiled for a specific VM, and will only run properly on thatVM. Compiling the same program to two different VMs will probablyresult in different binary code for each.\begin{figure}\centering\includegraphics[width=6in]{fig/scripter.jpg}\caption{The Scripter Interface}\label{fig:scripter}\end{figure}Figure~\ref{fig:scripter} shows a screenshot of the current ScripterGUI. Scripter {\bf must be run from the VM application directory} andhas an optional communication argument:\begin{verbatim} java net.tinyos.script.Scripter [-comm source] <vm-description>\end{verbatim}The comm parameter tells the Scripter where to send code packets,whether it be to TOSSIM, a serial port, or a SerialForwarder.The Scripter window is divided into three parts. The left subwindow iswhere you select what event handler you want to install, and theversion number. Motes ignore handlers with older version numbers thanwhat they currently have. Normally, the Scripter automatically handlesthese; each successful compilation of a given handler increments itsversion number. However, you can manually change it if need be.The left subwindow also keeps a tally of all currently used sharedvariables (these are discussed in Section~\ref{sec:variables}). When ashared variable is no longer used by any handler, the Scripterdeallocates it.The center window is where you write TinyScript, which the rest ofthis section descibes. The right window provides the list of availableprimitives (library functions). Clicking on one of the primitivesbrings up a brief description of its use in the lower text area.To install a new event handler, you choose which event you wish towrite for, write its code in the Program Text window and press theInject button. TinyScript programs have a maximum size (approximately240 bytes). Writing programs that are too long will cause acompile-time error when the Scripter tries to inject them into anetwork.If you quit Scripter using the File menu, then the interface saves itscurrent state for later use. That is, it keeps track of the currenthandler code, versions, and shared variables; you can quit and restartthe interface, resuming where you left off.\subsection{Script Structure}This is an example TinyScript program that increments a counter:\begin{quotation}\begin{verbatim}! Define a shared variable, countershared counter;! Increment itcounter = counter + 1;\end{verbatim}\end{quotation}In TinyScript programs, all variables must be declared before anyprogram statements. For example, the following program is invalid (andwill throw a compilation error):\begin{quotation}\begin{verbatim}! Define a shared variable, countershared counter;! Increment itcounter = counter + 1;! Define a shared variable, index: ERRORshared index;\end{verbatim}\end{quotation}There can only be one statement per line, and statements generally endwith a semicolon. {\tt !} declares the start of a comment, which extendsto the end of a line. For example,\begin{quotation}\begin{verbatim}shared counter; ! Define a shared variable, countercounter = counter + 1; ! Increment it\end{verbatim}\end{quotation}is valid TinyScript code.Certain words are TinyScript keywords, and cannot be used in programsto name variables or functions. In the above scripts, {\tt shared} isa keyword, declaring {\tt counter} to be a shared variable.TinyScript programs are for the most part case-sensitive. Keywordsexist as both their uppercase and lowercase versions: {\tt shared} canalso be written {\tt SHARED}, but cannot be written {\tt sHarEd}.\subsection{Variables}\label{sec:variables}TinyScript programs have two basic variable types: values and buffers. In theabove programs, the keyword {\tt shared} declared the variables to bevalues. Values can also be declared with the keyword {\tt private}:Section~\ref{sec:parallelism} describes the distinction between the two. Thekeyword {\tt buffer} declares a data buffer. Values represent a singledata item, such as an integer or a sensor reading. Buffers are smallcollections of values.Both values and buffers are dynamically typed. That is, the variablesthemselves have no explicit type in a program; instead, their typeis determined dynamically as a program runs. In this program,\begin{quotation}\begin{verbatim}shared counter; shared sensor;counter = call random();sensor = call light();\end{verbatim}\end{quotation}the variable {\tt counter} takes the type integer ({\tt rand()} returns aninteger) while {\tt sensor} takes the type light.Types constrain how values can be modified, and how buffers can beaccessed. There is one basic type, integer (16-bit, signed).Additionally, every sensor has its own type. Integers can be modifiedfreely, through arithmetic, assignment, and othertransformation. Sensor readings, however, are immutable. You cannotadd two sensor readings, even if from the same sensor.The idea is that sensor readings should only be what is actually readfrom a sensor. Transformations on these readings should be distinguishablefrom actual readings. Additionally, allowing transformation raisesproblematic questions: what is the type of a light reading added to ahumidity reading?To modify sensor readings, they must be cast to an integer. For example,this program computes an exponentially weighted moving average of thelight sensor:\begin{quotation}\begin{verbatim}shared sensor; shared aggregate;sensor = call light();aggregate += call cast(sensor); ! Cast light reading to integeraggregate = aggregate / 2;\end{verbatim}\end{quotation}Buffers also have a type, which defines what values can be placed init. A cleared buffer has no type, and takes the type of the firstvalue placed in it. In the following program, {\tt aggBuffer} iscleared, which clears its type. An integer ({\tt aggregate}) is addedto the buffer, making the buffer of the type integer.\begin{quotation}\begin{verbatim}shared sensor;shared aggregate;buffer aggBuffer;sensor = call light();aggregate += call cast(sensor); ! Cast light reading to integeraggregate = aggregate / 2;call bclear(aggBuffer); ! Clear all buffer entries and typeaggBuffer += aggregate; ! Append aggregate value to buffer ! Buffer is now of type integer\end{verbatim}\end{quotation}Buffers have a fixed maximum size of ten values. The function {\ttbfull()} can be used to see if a buffer is full. Individual buffervalues can be accessed by indexing into a buffer. The followingprogram obtains the median value stored in a buffer:\begin{quotation}\begin{verbatim}shared size;shared median;buffer aggBuffer; call bsorta(aggBuffer); ! Sort buffer entries in ascending ordersize = call bsize(aggBuffer); ! Number of entries in buffermedian = aggBuffer[size / 2]; ! Return median value\end{verbatim}\end{quotation}An empty index value implies the tail (last value) of a buffer on
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -