📄 matlab.html
字号:
<html><title>TinyOS Tutorial Lesson 8: Data logging application</title></head><body bgcolor="#f8f8ff" link="#005BB7" vlink="#005BB7"><table border=0 hspace=4 cellspacing=2 width="100%" cellpadding=3><tr bgcolor="#e0e0ff"><td width="100%"><font face="tahoma,arial,helvetica"><b><big><big>Using Matlab with TinyOS</big></big></b><p>Last updated 21 Sept 2002</font></td></tr></table><p> This tutorial explains how to interact with a mote or sensor network using Matlab. There are three parts to the tutorial, but the second two are for advanced users. You can focus on the first section, but before you begin, make sure you <a href="#Setting up your Matlab/Java environment">set up</a> your Matlab environment to work with TinyOS.<ol> <li>The first section explains how to connect to your network to send and receive packets.</li> <li>The second part shows you how to use the TinyOS Java tools from Matlab.</li> <li>The third section explains how to get data out of your Java tools and into your Matlab environment. </li> </ol><table border=0 hspace=4 cellspacing=2 width="100%" cellpadding=3><tr bgcolor="#e0e0ff"><td width="100%"><b><font face="arial,helvetica">Connecting to the Mote Network</font></b></td></tr></table><h3>Connecting to a port</h3><p>All you need to do is use the connect command. For example:</p><blockquote> <blockquote><p><b><font face="Courier New">connect('COM1');<br>connect('localhost:9000');</font></b></p> </blockquote></blockquote><h3>Sending a message to the network</h3><p>First, you need to instantiate a message, which should be a subclass of the net.tinyos.message.Message class. In general, one can use the MIG tool to generate these classes directly from the NesC code (use net.tinyos.oscope.OscopeResetMsg an example).</p><blockquote> <blockquote><p><b><font face="Courier New">msg=net.tinyos.oscope.OscopeResetMsg<br>send(1, msg)</font></b></p> </blockquote></blockquote><p>If you have an Oscilloscope mote with moteID 1 and a generic base attached to COM1 or localhost:9000, the Oscilloscope will reset.</p><h3>Receiving messages from the network</h3><p>You need to instantiate a message of the type you want to receive. You also need to create a function that takes two parameters: an address and a message. (Look at tools/matlab/util/printMsg.m as an example). Then, type the following</p><blockquote> <blockquote><p><b><font face="Courier New">omsg = net.tinyos.oscope.OscopeMsg<br>receive('printMsg', msg);</font></b></p> </blockquote></blockquote><p>Whenever a message of type msg is received, the printMsg function will be called with the message passed to it as a parameter. Typing the above command right now will cause all OscopeMsg messages to be passed to the printMsg function, which prints them to the screen. If you have an Oscilloscopemote attached to COM1 or localhost:9000, the messages will start appearing on the screen.</p><p>Notice that both the send and receive commands take a port, e.g. COM1 or localhost:9000, as optional third arguments. When this argument is passed, the connect function does not need to be used. Furthermore, the send and receive commands are limited to the ports specified.</p><table border=0 hspace=4 cellspacing=2 width="100%" cellpadding=3><tr bgcolor="#e0e0ff"><td width="100%"><b><font face="arial,helvetica">Using the Java tools from Matlab</font></b></td></tr></table><p>In this part of the tutorial we see how to use Matlab simply as a Java interpreter from which we can use the TinyOS Java tools. There are two parts:</p> <ol> <li>An overview of how to use Java from Matlab in general</li> <li>An example in which we use the Oscilloscope application (i.e. <a href="lesson6.html">Lesson 6</a>)</li> </ol><h3><b><font size="4">Overview of the Matlab/Java interface</font></b></h3><p>The following table provides an overview of the Matlab syntax for using Java. It is essentially the same as Java syntax except that there is no "new" operator and functions with no arguments do not terminate with empty parenthesis "( )". Type these commands into your Matlab command window to see what they do.</p> <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="80%" id="AutoNumber1"> <tr> <td width="100%" align="center" colspan="2" bgcolor="#E0E0FF">Overview of Matlab syntax for using Java objects</td> </tr> <tr> <td width="39%" align="left"> <blockquote> <p>Instantiate an Object</p> </blockquote> </td> <td width="61%"> <blockquote> <blockquote> <p align="left"><font face="Courier New">f = java.awt.Frame<br> f = java.awt.Frame("My Frame")</font></p> </blockquote> </blockquote> </td> </tr> <tr> <td width="39%" align="left"> <blockquote> <p>Call a function</p> </blockquote> </td> <td width="61%"> <blockquote> <blockquote> <p align="left"><font face="Courier New">f.show<br> f.resize(500,500)<br> f.hide</font></p> </blockquote> </blockquote> </td> </tr> <tr> <td width="39%" align="left"> <blockquote> <p>Call a static function</p> </blockquote> </td> <td width="61%"> <blockquote> <blockquote><p align="left"><font face="Courier New">java.awt.Frame.getFrames</font></p> </blockquote> </blockquote> </td> </tr> <tr> <td width="39%" align="left"> <blockquote> <p>Access public member variable</p> </blockquote> </td> <td width="61%"> <blockquote> <blockquote> <p><font face="Courier New">f.WIDTH</font></p> </blockquote> </blockquote> </td> </tr> <tr> <td width="39%" align="left"> <blockquote> <p>Introsection: class name</p> </blockquote> </td> <td width="61%"> <blockquote> <blockquote> <p><font face="Courier New">classname = class(f)</font></p> </blockquote> </blockquote> </td> </tr> <tr> <td width="39%" align="left"> <blockquote> <p>Introspection: methods</p> </blockquote> </td> <td width="61%"> <blockquote> <blockquote> <p><font face="Courier New">methods java.awt.Frame<br> methods java.awt.Frame -full<br> methodsview java.awt.Frame</font></p> </blockquote> </blockquote> </td> </tr> <tr> <td width="39%" align="left"> <blockquote> <p>Introspection: variables</p> </blockquote> </td> <td width="61%"> <blockquote> <blockquote> <p><font face="Courier New">fields(f)</font></p> </blockquote> </blockquote> </td> </tr> <tr> <td width="39%" align="left"> <blockquote> <p>Import </p> </blockquote> </td> <td width="61%"> <blockquote> <blockquote> <p><font face="Courier New">import java.awt.*</font></p> </blockquote> </blockquote> </td> </tr> </table><p>Conversion of function parameters and return values are taken care of automatically. Essentially, all primitive types, including strings and arrays, are automatically converted and are passed by value. Java objects are left as Java objects and are passed by reference. Using Java from Matlab is completely supported by Mathworks and is well documented in the the "Matlab/Java interface" section of the Matlab help files. </p><h3>Example: Using Oscilloscope</h3><p>Here, we see an example of using Java tools from Matlab. Before beginning, be sure that </p><ol> <li>The net.tinyos.oscope Java classes are compiled, including OscopeMsg.java and OscopeResetMsg.java</li> <li>A mote has been programmed with the OscilloscopeRF application </li> <li>A GenericBase mote is plugged into your serial port</li></ol><p>Now, go to your matlab command window. The following two commands will call the static <font face="Courier New">main()</font> functions of the SerialForwarder and Oscilloscope classes. We pass command-line arguments directly to 'main( )' as follows.</p><blockquote> <blockquote><p><font face="Courier New"><b>net.tinyos.sf.SerialForward.main({'-comm','COM1'})<br>net.tinyos.oscope.oscilloscope.main({'125'})</b></font></p> </blockquote></blockquote><p>This starts the two applications. After the first command, you should see a SerialForwarder GUI appear and say that it connected to COM1 and is waiting for clients. <font size="1"><a href="../../tools/matlab/doc/tutorial/SerialForwarderProblems.html">What if this didn't happen?</a></font> Then the second command will create the oscilloscope client that will connect to the SerialForwarder, receive packets from it, and plot them on the screen. <font size="1"><a href="file:///E:/My%20Documents/tinyos-1.x/tools/matlab/doc/tutorial/OscilloscopeProblems.html">What if this didn't happen?</a></font> </p><p>Now, let's inject a message to our Mote. The following commands will </p> <ol> <li>Instantiate a moteIF object and pass parameters to its constructor telling it to connect to localhost:9000 and listen to group ID 125</li> <li>Instantiate an "OscopeResetMsg"</li> <li>Inject that message to our OscilloscopeRF mote, where XX is the node ID of that mote. </li> </ol>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -