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

📄 lesson4.html

📁 tinyos中文手册,是根据tinyos系统自带手册翻译过来的,虽然质量不好,但是对英文不强的人还是有用的
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE doctype 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="Microsoft FrontPage 5.0">  <title>Lesson 4: Component Composition and Radio Communication</title></head><body bgcolor="#f8f8ff" link="#005bb7" vlink="#005bb7">&nbsp;<table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><font face="tahoma,arial,helvetica"><font size="-1">Lesson 4: Component Composition and Radio Communication</font></font></b>      <p><font face="tahoma,arial,helvetica">Last updated 31 July 2003</font></p>      </td>    </tr>  </tbody></table><p>This lesson introduces two concepts: hierarchical decomposition ofcomponent graphs, and using radio communication. The applications thatwe will consider are <tt>CntToLedsAndRfm</tt> and <tt>RfmToLeds</tt>. <tt>CntToLedsAndRfm</tt>is a variant of <tt>Blink</tt> that outputs the current counter value tomultiple output interfaces: both the LEDs, and the radio communicationstack. <tt>RfmToLeds</tt> receives data from the radio and displays iton the LEDs. Programming one mote with <tt>CntToLedsAndRfm</tt> willcause it to transmit its counter value over the radio; programminganother with <tt>RfmToLeds</tt> causes it to display the receivedcounter on its LEDs - your first distributed application!<br></p><p>If you're using mica2 or mica2dot motes, you will need to ensurethat you've selected a radio frequency compatible with your motes(433MHz vs 916MHz motes). If your motes are unlabeled, see "<a href="../mica2radio/mica2freq.html">How to determine the operatingfrequency range of a MICA2 or MICA2DOT mote</a>" for information onrecognizing which kind of mote you have. To tell the compiler whichfrequency you are using, edit the <a href="buildenv.html">Makelocal</a>file in the apps directory, defining either <span style="font-family: monospace;">CC1K_DEF</span><span style="font-family: monospace">_PRESET</span> (see <a href="../../tos/platform/mica2/CC1000Const.h">tinyos-1.x/tos/platform/mica2/CC1000Const.h</a>for preset values), or <span style="font-family: monospace;">CC1K_DEF_FREQ</span> with an explicit frequency(see example in <a href="file:///home/dgay/motes/tinyos-1.x/doc/tutorial/buildenv.html">Makelocal</a>).<br></p><p><table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><nobr><font face="arial,helvetica">TheCntToRfmAndLeds Application</font></nobr></b></td>    </tr>  </tbody></table></p><p>Look at <tt>CntToRfmAndLeds.nc</tt>. Note that this applicationonly consists of a configuration; all of the component modules arelocated in libraries! </p><center><table border="0" cellspacing="2" cellpadding="3" width="80%" hspace="4">  <tbody>    <tr bgcolor="#e0e0e0">      <td width="100%"><b>CntToLedsAndRfm.nc</b>      <pre>configuration CntToLedsAndRfm {<br>}<br>implementation {<br>&nbsp; components Main, Counter, IntToLeds, IntToRfm, TimerC;<br><br>&nbsp; Main.StdControl -&gt; Counter.StdControl;<br>&nbsp; Main.StdControl -&gt; IntToLeds.StdControl;<br>&nbsp; Main.StdControl -&gt; IntToRfm.StdControl;<br>&nbsp; Main.StdControl -&gt; TimerC.StdControl;<br>&nbsp; Counter.Timer -&gt; TimerC.Timer[unique("Timer")];<br>&nbsp; IntToLeds &lt;- Counter.IntOutput;<br>&nbsp; Counter.IntOutput -&gt; IntToRfm;<br>}</pre>      </td>    </tr>  </tbody></table></center><p>The first thing to note is that a single interface requirement (suchas <tt>Main.StdControl</tt> or <tt>Counter.IntOutput</tt>) can be <i>fannedout</i> to multiple implementations. Here we wire <tt>Main.StdControl</tt>to the <tt>Counter</tt>, <tt>IntToLeds</tt>, <tt>IntToRfm</tt>, and <tt>TimerC</tt>components.(All of these components can be found in the <tt><a href="../../tos/lib/Counters">tos/lib/Counters</a></tt> directory.) Thenames of the various components tell you what they do: <tt>Counter</tt>receives <tt>Timer.fired()</tt> events to maintain a counter. <tt>IntToLeds</tt>and <tt>IntToRfm</tt> provide the <tt>IntOutput</tt> interface, that hasone command, <tt>output()</tt>, which is called with a 16-bit value,and one event, <tt>outputComplete()</tt>, which is called with <tt>result_t</tt>. <tt>IntToLeds</tt> displays the lower three bits of its value on theLEDs, and <tt>IntToRfm</tt> broadcasts the 16-bit value over the radio. </p><p>So we're wiring the <tt>Counter.Timer</tt> interface to <tt>TimerC.Timer</tt>,and <tt>Counter.IntOutput</tt> to <b>both</b> <tt>IntToLeds</tt> and <tt>IntToRfm</tt>.The NesC compiler generates code so that all invocations of the <tt>Counter.IntOutput.output()</tt>command will invoke the command in both <tt>IntToLeds</tt> and <tt>IntToRfm</tt>.Also note that the wiring arrow can go in either direction: the arrowalways points <i>from</i> a used interface <i>to</i> a providedimplementation. </p><p>Assuming you are using a Mica mote, try building and installing thisapplication with <tt>make mica install</tt>; you should see a 3-bitbinary counter on the mote's LEDs. Of course the mote is alsotransmitting the value over the radio, which we describe next. <br>&nbsp;<table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><nobr><font face="arial,helvetica">IntToRfm:Sending a message</font></nobr></b></td>    </tr>  </tbody></table><tt>IntToRfm</tt> is a simple component that receives an output value(through the <tt>IntOutput</tt> interface) and broadcasts it over theradio. Radio communication in TinyOS follows the Active Message (AM)model, in which each packet on the network specifies a <i>handler ID</i>that will be invoked on recipient nodes. Think of the handler ID as aninteger or "port number" that is carried in the header of the message.When a message is received, the receive event associated with thathandler ID is signaled. Different motes can associate different receiveevents with the same handler ID. </p><p>In any messaging layer, there are 5 aspects involved in successfulcommunication: </p><ol>  <li> Specifying the message data to send;</li>  <li> Specifying which node is to receive the message;</li>  <li> Determining when the memory associated with the outgoing messagecan be reused;</li>  <li> Buffering the incoming message; and,</li>  <li> Processing the message on reception</li></ol>In Tiny Active Messages, memory management is very constrained as youwould expect from a small-scale embedded environment.<p>Let's look at <tt>IntToRfm.nc</tt>: </p><center><table border="0" cellspacing="2" cellpadding="3" width="80%" hspace="4">  <tbody>    <tr bgcolor="#e0e0e0">      <td width="100%"><b>IntToRfm.nc</b>      <pre>configuration IntToRfm<br>{<br>&nbsp; provides interface IntOutput;<br>&nbsp; provides interface StdControl;<br>}<br>implementation<br>{<br>&nbsp; components IntToRfmM, GenericComm as Comm;<br><br>&nbsp; IntOutput = IntToRfmM;<br>&nbsp; StdControl = IntToRfmM;<br><br>&nbsp; IntToRfmM.Send -&gt; Comm.SendMsg[AM_INTMSG];<br>&nbsp; IntToRfmM.StdControl -&gt; Comm;<br>}</pre>      </td>    </tr>  </tbody></table></center><p>This component provides the <tt>IntOutput</tt> and <tt>StdControl</tt>interfaces. This is the first time that we have seen a <i>configuration</i>provide an <i>interface</i>. In the previous lessons we have alwaysused configurations just to wire other components together; in thiscase, the <tt>IntToRfm</tt> configuration is itself a component thatanother configuration can wire to. Got it? </p><p>In the implementation section, we see: </p><pre>&nbsp; components IntToRfmM, GenericComm as Comm;</pre>The phrase "<tt>GenericComm as Comm</tt>" is stating that thisconfiguration uses the <tt>GenericComm</tt> component, but gives it the(local) name <tt>Comm</tt>. The idea here is that you can easily swapin a different communication module in place of <tt>GenericComm</tt>,and only need to change this one line to do so; you don't need tochange every line that wires to <tt>Comm</tt>.<p>We also see some new syntax here in the lines: </p><pre>&nbsp; IntOutput = IntToRfmM;<br>&nbsp; StdControl = IntToRfmM;</pre>The equal sign (<tt>=</tt>) is used to indicate that the <tt>IntOutput</tt>interface provided by <tt>IntToRfm</tt> is<b> "equivalent to"</b> theimplementation in <tt>IntToRfmM</tt>. We can't use the arrow (<tt>-&gt;</tt>)here, because the arrow is used to wire a used interface to a providedimplementation. In this case we are "equating" the interfaces providedby <tt>IntToRfm</tt> with the implementation found in <tt>IntToRfmM</tt>.<p>The last two lines of the configuration are: </p><pre>&nbsp; IntToRfmM.Send -&gt; Comm.SendMsg[AM_INTMSG];<br>&nbsp; IntToRfmM.StdControl -&gt; Comm;</pre>The last line is simple; we're wiring <tt>IntToRfmM.StdControl</tt> to <tt>GenericComm.StdControl</tt>.The first line shows us another use of <b>parameterized interfaces</b>,in this case, wiring up the <tt>Send</tt> interface of <tt>IntToRfmM</tt>to the <tt>SendMsg</tt> interface provided by <tt>Comm</tt>.<p>The <tt>GenericComm</tt> component declares: </p><pre>&nbsp; provides {</pre><pre>&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp; interface SendMsg[uint8_t id];</pre><pre>&nbsp;&nbsp;&nbsp; ...<br>&nbsp; }</pre>In other words, it provides 256 different instances of the <tt>SendMsg</tt>interface, one for each <tt>uint8_t</tt> value. This is the way thatActive Message handler IDs are wired together. In <tt>IntToRfm</tt>,we are wiring the <tt>SendMsg</tt> interface corresponding to thehandler ID <tt>AM_INTMSG</tt> to <tt>GenericComm.SendMsg</tt>. (<tt>AM_INTMSG</tt>is a global value defined in <tt><a href="../../tos/lib/Counters/IntMsg.h">tos/lib/Counters/IntMsg.h</a></tt>.)When the <tt>SendMsg</tt> command is invoked, the handler ID isprovided to it, essentially as an extra argument. You can see how thisworks by looking at <tt>tos/system/AMStandard.nc</tt> (theimplementation module for <tt>GenericComm</tt>):<pre>&nbsp; command result_t SendMsg.send[uint8_t id]( ... ) { ... };</pre>Of course, parameterized interfaces aren't strictly necessary here -the same thing could be accomplished if <tt>SendMsg.send</tt> took thehandler ID as an argument. This is just an example of the use ofparameterized interfaces in nesC. <br>&nbsp;<table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><nobr><font face="arial,helvetica">IntToRfmM:Implementing Network Communication</font></nobr></b></td>    </tr>  </tbody></table><p>Now we know how <tt>IntToRfm</tt> is wired up, but we don't know howmessage communication is implemented. Take a look at the <tt>IntOutput.output()</tt>command in <tt>IntToRfmM.nc</tt>: </p><center>

⌨️ 快捷键说明

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