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

📄 lesson6.html

📁 tinyos中文手册,是根据tinyos系统自带手册翻译过来的,虽然质量不好,但是对英文不强的人还是有用的
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><font face="arial,helvetica">Starting theOscilloscope GUI</font></b></td>    </tr>  </tbody></table></p><p>It is now time to graphically display the data coming from themotes. Leaving the serial forwarder running, execute the command </p><pre>&nbsp; java net.tinyos.oscope.oscilloscope</pre>This will pop up a window containing a graphical display of the sensorreadings from the mote (If you get an error like "port COM1 busy", youprobably forgot to unset the MOTECOM environment variable at the end ofthe Listen example. Do that now). It connects to the serial forwarderover the network and retrieves packet data, parses the sensor readingsfrom each packet, and draws it on the graph:<center><p><img width="600" height="600" border="0" src="imgs/oscilloscope.JPG"></p></center><p>The <i>x</i>-axis of the graph is the packet counter number and the<i>y</i>-axisis the sensor light reading. If the mote has been running for a while,its packet counter might be quite large, so the readings might notappear on the graph; just power-cycle the mote to reset its packetcounter to 0. If you don't see any light readings on the display, tryzooming out on the Y axis (the values might be out of the displayedrange) or selecting the "Scrolling" push button (the sample number maybe out of the displayed range, "Scrolling" automatically scrolls to themost recent values).<br>&nbsp;<table hspace="4" width="100%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><font face="arial,helvetica">Using MIG tocommunicate with motes</font></b></td>    </tr>  </tbody></table></p><p><a href="../../nesc/doc/mig.html">MIG (Message Interface Generator)</a>is a tool that is used to automatically generate Java classes thatcorrespond to Active Message types that you use in your moteapplications. MIG reads in the nesC <tt>struct</tt> definitions formessage types in your mote application and generates a Java class foreach message type that takes care of the gritty details of packing andunpacking fields in the message's byte format. Using MIG saves you fromthe trouble of parsing message formats in your Java application. </p><p>MIG is used in conjunction with the <tt><a href="../../tools/java/net/tinyos/message">net.tinyos.message</a></tt>package, which provides a number of routines for sending and receivingmessages through the MIG-generated message classes. <tt><a href="../../nesc/doc/ncg.html">NCG(nesC Constant Generator)</a></tt>isa tool to extract constants from nesC files for use with otherapplications and is typically used in conjunction with <tt>MIG</tt>. </p><p>Let's look at the code from <tt><a href="../../tools/java/net/tinyos/oscope/GraphPanel.java">tools/java/net/tinyos/oscope/GraphPanel.java</a></tt>(part of the <tt>oscilloscope</tt> program) that communicates with theserial forwarder. First, the program connects to the serial forwarderand registers a handler to be invoked when a packet arrives. All ofthisis done through the <tt><a href="../../tools/java/net/tinyos/message/MoteIF.java">net.tinyos.message.MoteIF</a></tt>interface: </p><center><table hspace="4" width="80%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0e0">      <td width="100%"><b>GraphPanel.java</b>      <pre>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // OK, connect to the serial forwarder and start receiving data<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mote = new MoteIF(PrintStreamMessenger.err, oscilloscope.group_id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mote.registerListener(new OscopeMsg(), this);</pre>      </td>    </tr>  </tbody></table></center><p><tt>MoteIF</tt> represents a Java interface for sending andreceivingmessages to and from motes. The host and port number of the serialforwarder are obtained from the environment variable <tt>MOTECOM</tt>.You initialize <tt>MoteIF</tt> with&nbsp; <tt><a href="../../tools/java/net/tinyos/util/PrintStreamMessenger.java">PrintStreamMessenger</a></tt>which indicates where to send status messages (<tt>System.err</tt>), aswell as an (optional) Active Message group ID. This group ID mustcorrespond to the group ID used by your motes. </p><p>We register a message listener (<tt>this</tt>) for the message type <tt>OscopeMsg</tt>.<tt>OscopeMsg</tt>is automatically generated by <tt>MIG</tt> from the nesC definitionfor <tt>structOscopeMsg</tt>, which we saw earlier in <tt>OscopeMsg.h</tt>. Look at <tt><a href="../../tools/java/net/tinyos/oscope/Makefile">tools/java/net/tinyos/oscope/Makefile</a></tt>for an example of how this class is generated. You will see: </p><pre>&nbsp; OscopeMsg.java:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $(MIG) -java-classname=$(PACKAGE).OscopeMsg $(APP)/OscopeMsg.h OscopeMsg -o $@</pre>Essentially, this generates <tt>OscopeMsg.java</tt> from the messagetype <tt>struct OscopeMsg</tt> in the header file <a href="../../apps/Oscilloscope/OscopeMsg.h">apps<tt>/Oscilloscope/OscopeMsg.h</tt></a>.<p><tt>GraphPanel</tt> implements the <tt>MessageListener</tt>interface, which defines the interface for receiving messages from theserial forwarder. Each time a message of the appropriate type isreceived, the <tt>messageReceived()</tt> method is invoked in <tt>GraphPanel</tt>.It looks like this: </p><center><table hspace="4" width="80%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0e0">      <td width="100%"><b>GraphPanel.java</b>      <pre>  public void messageReceived(int dest_addr, Message msg) {<br>    if (msg instanceof OscopeMsg) {<br>      oscopeReceived( dest_addr, (OscopeMsg)msg);<br>    } else {<br>      throw new RuntimeException("messageReceived: Got bad message type: "+msg);<br>    }<br>  }<br><br>  public void oscopeReceived(int dest_addr, OscopeMsg omsg) {<br>    boolean foundPlot = false;<br>    int moteID, packetNum, channelID, channel = -1, i;<br><br>    moteID = omsg.get_sourceMoteID();<br>    channelID = omsg.get_channel();<br>    packetNum = omsg.get_lastSampleNumber();<br>&nbsp;&nbsp;  <br>&nbsp;&nbsp;&nbsp; /* ... */</pre>      </td>    </tr>  </tbody></table></center><p><tt>messageReceived()</tt> is called with two arguments: thedestination address of the packet, and the message itself (<tt>net.tinyos.message.Message</tt>).<tt>Message</tt>is just the base class for the application-defined message types; inthis case we want to cast it to <tt>OscopeMsg</tt> which representstheactual message format we are using here. </p><p>Once we have the <tt>OscopeMsg</tt> we can extract fields from itusing the handy <tt>get_sourceMoteID()</tt>, <tt>get_lastSampleNumber()</tt>,and <tt>get_channel()</tt> methods. If we look at <tt>struct OscopeMsg</tt>in <tt>OscopeMsg.h</tt> we'll see that each of these methodscorrespondsto a field in the message type: </p><center><table hspace="4" width="80%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0e0">      <td width="100%"><b>OscopeMsg.h</b>      <pre>struct OscopeMsg<br>{<br>&nbsp;&nbsp;&nbsp; uint16_t sourceMoteID;<br>&nbsp;&nbsp;&nbsp; uint16_t lastSampleNumber;<br>&nbsp;&nbsp;&nbsp; uint16_t channel;<br>&nbsp;&nbsp;&nbsp; uint16_t data[BUFFER_SIZE];<br>};</pre>      </td>    </tr>  </tbody></table></center><p>Each field in a MIG-generated class has at least eight methodsassociated with it: </p><ul>  <li> <tt>isSigned_<i>fieldname</i></tt> - Indicates whether or notthis field is a signed quantity.</li>  <li> <tt>isArray_<i>fieldname</i></tt> - Indicates whether or notthisfield is an array.</li>  <li> <tt>get_<i>fieldname</i></tt> - Return the value of this field.</li>  <li> <tt>set_<i>fieldname</i></tt> - Set the value of this field.</li>  <li> <tt>offset_<i>fieldname</i></tt> - Return the offset (in bytes)for this field.</li>  <li> <tt>offsetBits_<i>fieldname</i></tt> - Return the offset (inbits) for this field.</li>  <li> <tt>size_<i>fieldname</i></tt> - Return the length (in bytes)ofthis field.</li>  <li> <tt>sizeBits_<i>fieldname</i></tt> - Return the length (inbits)of this field.</li></ul>Note that additional methods are generated for fields that are arrays.<br><p>The remainder of <tt>messageReceived()</tt> pulls the sensorreadings out of the message and places them on the graph.<table hspace="4" width="100%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><font face="arial,helvetica">Sending amessage through MIG</font></b></td>    </tr>  </tbody></table></p><p>It is also possible to send a message to the motes using MIG. The <tt>Oscilloscope</tt>application sends messages of type <tt>AM_OSCOPERESETMSG</tt>, whichcauses the mote to reset its packet counter. Looking at <tt>clear_data()</tt>in <tt>GraphPanel.java</tt>, we see how messages are sent to themotes: </p><center><table hspace="4" width="80%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0e0">      <td width="100%"><b>GraphPanel.java</b>      <pre>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mote.send(MoteIF.TOS_BCAST_ADDR, new OscopeResetMsg());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IOException ioe) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.err.println("Warning: Got IOException sending reset message: "+ioe);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ioe.printStackTrace();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</pre>      </td>    </tr>  </tbody></table></center><p>All we need to do is invoke <tt>MoteIF.send()</tt> with thedestination address and the message that we wish to send. Here, <tt>MoteIF.TOS_BCAST_ADDR</tt>is used to represent the broadcast destination address, which isidentical to <tt>TOS_BCAST_ADDR</tt> used in the nesC code. <br>&nbsp;<table hspace="4" width="100%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><nobr><font face="arial,helvetica">Exercises</font></nobr></b></td>    </tr>  </tbody></table></p><p>Transmit the light sensor readings over the radio to another motethat sends them over the serial port! </p><p>The <tt>apps/Oscilloscope</tt> mote application is written to usethe serial port and the light sensor. Instead, look at <tt><a href="../../apps/OscilloscopeRF">apps/OscillosopeRF</a></tt>, whichtransmits the sensor readings over the radio. In order to use thisapplication, you need to use one mote as a gateway that receives datapackets over the radio and transmits them over the serial port. <tt><a href="../../apps/TOSBase">apps/TOSBase</a></tt> is an application thatdoes this; it simply forwards packets between the radio and the UART(inboth directions). </p><p><b>Extra Credit</b>: Can you figure out how to display sensorreadings on the oscilloscope GUI from two motes simultaneously? (Notethat the oscillosope GUI is already capable of displaying sensorreadings from multiple motes. You have to ensure that those readingsarecorrectly transmitted and received over the network.) This setup wouldlook like the following diagram. </p><center><p><img width="720" height="540" src="imgs/Serial_Forward_topology.jpg"></p></center><p> </p><hr><b><a href="lesson5.html">&lt; Previous Lesson</a></b> | <b><a href="lesson7.html">Next Lesson &gt;</a></b> | <b><a href="index.html">Top</a></b><!--  LocalWords:  TinyOS nesC nc async norace BlinkM FooM ncc SingleTimer Leds --><!--  LocalWords:  LedsC StdControl tos init TimerC redOn redOff uint redToggle --><!--  LocalWords:  metadata html nesdoc gcc exe avr objcopy srec uisp mib dprog --><!--  LocalWords:  towards dapa xff ThinkPad dlpt Makelocal micasb Wshadow DDEF --><!--  LocalWords:  finline fnesc cfile lm Atmel ATmega nesC's nesc SenseM rdata --><!--  LocalWords:  ADCControl SounderControl dataReady getData rcombine someval --><!--  LocalWords:  ADControl fooControl barControl MyTimer uniqueCount basicsb --><!--  LocalWords:  sensorboard Makerules sensorboards SenseTask taskname INTMSG --><!--  LocalWords:  SenseTaskM putdata processData CntToLedsAndRfm RfmToLeds Msg --><!--  LocalWords:  CntToRfmAndLeds IntToLeds IntToRfm IntOutput outputComplete --><!--  LocalWords:  IntToRfmM GenericComm SendMsg bool struct IntMsg src BCAST --><!--  LocalWords:  ADDR sizeof TOSH sendDone GenricComm AMStandard RfmToInt pc --><!--  LocalWords:  UARTNoCRCPacket RadioCRCPacket ActiveMessage RfmToIntM xfff --><!--  LocalWords:  ReceiveIntMsg ReceiveMsg MsgPtr addr SenseToRfm TOSSIM DBG --><!--  LocalWords:  TinyViz ffff crc usr dbg const printf prepended bitmask gdb --><!--  LocalWords:  ledsOn gdbinit plugin API TestTinyViz cd tinyviz plugins RFM --><!--  LocalWords:  FakeLocation RadioModelPlugin LocationPlugin afterwards disc --><!--  LocalWords:  DebugMsgEvent TestTinyVizM AutoRun autorun numsec stopstring --><!--  LocalWords:  gridrandom DebugMsgPlugin RadioLinkPlugin radiomodel precmd --><!--  LocalWords:  radioscaling nummotes logfile txt logfiles arConfig TOSSIM's --></body></html>

⌨️ 快捷键说明

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