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

📄 matlab.html

📁 tinyos中文手册,是根据tinyos系统自带手册翻译过来的,虽然质量不好,但是对英文不强的人还是有用的
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<blockquote>  <blockquote><p><font face="Courier New"><b>mIF = net.tinyos.message.MoteIF('localhost', 9000, 125)<br>msg = net.tinyos.oscope.OscopeResetMsg<br>mIF.send( XX , msg)<br>&nbsp;</b></font></p>  </blockquote></blockquote><p>You've now seen how to use Java from Matlab.&nbsp; From this example you should understand how to use all the TinyOS Java tools, and in fact all Java objects, from Matlab.&nbsp; <font size="1">See </font><a href="../../tools/matlab/doc/tutorial/shellVsMatlab.html"><font size="1">Making Java classes Matlab Friendly</font></a><font size="1"> for a few other important facts.</font></p><table border=0 hspace=4 cellspacing=2 width="100%" cellpadding=3><tr bgcolor="#e0e0ff"><td width="100%"><b>Using the MatlabControl class</b></td></tr></table><p>In the previous section we saw that we could easily manipulate Java objects from Matlab.&nbsp; We could also get data into the Matlab workspace by storing the return value of a java function.&nbsp; This type of interaction with Java is completely supported by Matlab.&nbsp; For example</p><blockquote>  <blockquote><p><font face="Courier New"><b>am=msg.</b></font><b><font face="Courier New">amType</font></b></p>  </blockquote></blockquote><p>gets the AM type of your OscopeResetMsg message and stores it in the Matlab variable <font face="Courier New">am</font>.&nbsp;</p><p>However, there are times when we would like to pass data into Matlab by calling a Matlab function from Java and passing the data as a parameter.&nbsp; This is <i><b>not </b></i>supported by Matlab but is often much more useful.&nbsp; For example, it allows the Java MIG tools to pass incoming messages into Matlab whenever they arrive.&nbsp; In this section, we see how to use the<font face="Courier New"><b><a href="../../tools/java/net/tinyos/matlab/MatlabControl.java">net.tinyos.matlab.MatlabControl.java</a></b> </font>class to call Matlab commands from Java.&nbsp; There are two parts to this section:</p>  <ol>    <li>An overview of the MatlabControl class itself</li>    <li>An example in which we use MatlabControl with MIG, the Message Interface     Generator</li>  </ol><h3>MatlabControl.java</h3><p>MatlabControl only has two functions</p>  <blockquote>    <blockquote><p><font face="Courier New" size="4">void MatlabControl.eval(String)<br>void MatlabControl.feval(String, Object[])</font></p>    </blockquote>  </blockquote><p>The first function 'eval' takes a string and evaluates it in the Matlab workspace.&nbsp; Create a MatlabControl object and test this function as follows:</p>  <blockquote>    <blockquote><p><font face="Courier New" size="4">matlabControl = net.tinyos.matlab.MatlabControl<br>matlabControl.eval('x=255')<br>x</font></p>    </blockquote>  </blockquote><p>The last command illustrates that our 'eval' command actually created a variable 'x' and assigned it the value 255.</p><p>The second function 'feval' takes a string and an array of java.lang.Object.&nbsp; The string must be a function name. Matlab will call this function as pass each element in the array of Objects as a parameter to this function.&nbsp; For example</p>  <blockquote>    <blockquote><p><font size="4" face="Courier New">matlabControl.feval('help', {'plot', 'axis'})</font></p>    </blockquote>  </blockquote><p>will call matlab 'help' for 'plot' and 'axis'.</p><p>Note that MatlabControl or at least the object that creates MatlabControl must be instantiated from Matlab.&nbsp; Since you can have many Matlab sessions running at once, this is the only way for MatlabControl to know <i>which </i>Matlab session it should control.</p><h3>Example: Using MatlabControl with MIG</h3><p>Recall that MIG is the Message Interface Generator.&nbsp; It automatically generates Java 'Message' objects that correspond to TinyOS messages.&nbsp; As data streams in on the serial port, MoteIF will give such Message objects to objects that implement the MessageListener interface.&nbsp; MoteIF will also send Message objects to motes over the serial port.&nbsp;&nbsp; </p><p>In this example, we will see how to use MoteIF to send data into Matlab instead of to a Java object.&nbsp; F<i>rom the Matlab command window</i>, go to your<font face="Courier New"> matlab/doc/demo/</font> directory using a command like this one:</p><blockquote>  <blockquote><p><font face="Courier New"><b>cd TINYOS/tools/matlab/doc/demo</b></font></p>  </blockquote></blockquote><p>where TINYOS is the location of your tinyos-1.x directory.&nbsp; You should find a file called <font face="Courier New"><a href="../../tools/matlab/doc/demo/messageReceived.m">messageReceived.m</a></font>.&nbsp; This file defines a Matlab function called <font face="Courier New">messageReceived</font>, which is the function we want MoteIF to call whenever a new message is received.&nbsp; Double-click it to open it in the Matlab editor.&nbsp; Notice that it takes two arguments,<font face="Courier New">address</font> and <font face="Courier New">message</font>, just like the <a href="../../tools/java/net/tinyos/message/MessageListener.java">messageListener.java</a> interface requires.&nbsp; The body of the function simply prints the message to the screen.</p><p>Now, let's instantiate a Java Message object that will be used to parse the incoming data.&nbsp; We will be getting data from OscilloscopeRF so we use</p><blockquote>  <blockquote><p><font face="Courier New"><b>OscopeMsg = net.tinyos.oscope.OscopeMsg</b></font></p>  </blockquote></blockquote><p>Now, let's create a MoteIF object to receive data from our SerialForwarder (which should still be running from the previous section of the tutorial).&nbsp; </p><blockquote>  <blockquote><p><font face="Courier New"><b>mIF = net.tinyos.message.MoteIF('localhost', 9000, 125)</b></font></p>  </blockquote></blockquote><p>Normally  we would register a Java object that implements the MessageListener interface with MoteIF.&nbsp; Instead, let's instantiate a wrapper class called<a href="../../tools/java/net/tinyos/matlab/MatlabMessageListener.java">MatlabMessageListener</a> which implements the MessageListener interface and then uses MatlabControl to call our Matlab function whenever a message is received.</p><blockquote>  <blockquote><p><font face="Courier New"><b>ml = net.tinyos.matlab.MatlabMessageListener<br>ml.registerMessageListener('messageReceived')</b></font></p>  </blockquote></blockquote><p>Finally, let's  register our messageListener to listen to OscopeMsg messages and start the MoteIF object</p><blockquote>  <blockquote><p><font face="Courier New"><b>mIF.registerListener(OscopeMsg, ml)<br>mIF.start</b></font></p>  </blockquote></blockquote><p>You should see the packets printing to the screen.&nbsp; You've essentially just created a new 1-line version of ForwarderListen.java!&nbsp; </p><p>Now, put a break point in the messageReceived function by clicking to the left of line 3 in the Matlab editor.&nbsp; A red dot should appear indicating a break point has been placed, and a green arrow should appear indicating that program control has reached the break point and stopped.&nbsp; You are now in debug mode and the prompt in the Matlab command window should turn to a &quot;<font face="Courier New">K&gt;&gt;</font>&quot; to indicate that.&nbsp; Turn your mote off so that you don't get any more packets while you are in debug mode.&nbsp; <font size="1"><a href="../../tools/matlab/doc/tutorial/whyTurnMoteOff.html">Why do I need to turn my mote off?</a></font></p><p>From debug mode, type <font face="Courier New">message </font>to print the message contents to the screen.&nbsp; Now, get the data out of the message with the following commands</p><blockquote>  <blockquote><p><font face="Courier New"><b>id = message.get_sourceMoteID<br>sampleNumber = message.get_lastSampleNumber<br>channel = message.get_channel<br>dataReadings = message.get_data</b></font></p>  </blockquote></blockquote><p>Now, you have your sensor data in the Matlab workspace and you can start analyzing it using all the Matlab commands!&nbsp;&nbsp; For example</p><blockquote>  <blockquote><p><font face="Courier New"><b>plot(message.get_data)</b></font><b><font face="Courier New"><br>hist(message.get_data)</font></b></p>  </blockquote></blockquote><table border=0 hspace=4 cellspacing=2 width="100%" cellpadding=3><tr bgcolor="#e0e0ff"><td width="100%"><b><font face="arial,helvetica"><a name="Setting up your Matlab/Java environment">Setting up your Matlab/Java environment</a></font></b></td></tr></table><h3>Step 1:&nbsp; Change your Matlab path</h3><p>You need to tell Matlab where your TinyOS tools  are located.&nbsp; Open Matlab, go to the &quot;file&quot; menu and choose &quot;set path.&quot;&nbsp;&nbsp; Click the &quot;add with subfolders&quot; button and choose your <b><font face="Courier New">tinyos-1.x/tools/matlab/</font></b> directory.&nbsp; click &quot;save&quot; and &quot;close&quot;</p><h3>Step 2:&nbsp; Change your Matlab classpath</h3><p>You need to tell Matlab where your TinyOS Java tools  are located.&nbsp; Open Matlab and type the following command <i>in the Matlab command window</i>:</p><blockquote>  <blockquote>    <p><b><font face="Courier New">edit classpath.txt</font></b></p>  </blockquote></blockquote><p>The Matlab editor  will open with the classath.txt file.&nbsp; This file lists the directories where Matlab looks for the Java classes that it uses.&nbsp; You need to add the directory of both your TinyOS Java tools and of your comm.jar file.&nbsp; The resulting file should look something like <a href="../../tools/matlab/doc/tutorial/classpathtxt.html">this</a>, where the <b>bold </b>lines are the ones you just added.&nbsp;&nbsp; Save this file and then close and restart Matlab for your changes to take place.</p><h3>Step 3:&nbsp; Compile MatlabControl.java</h3><p>The <font face="Courier New">net.tinyos.matlab.MatlabControl</font> class is needed to call matlab commands from Java.&nbsp; When you compile it, you must have the .jar file <font face="Courier New">C:\MATLAB\java\jar\jmi.jar</font> in your CLASSPATH environment variable variable.&nbsp; Once this is done, do the following:</p><blockquote>  <blockquote><p><b><font face="Courier New">cd ~/TINYOS/tools/java<br>javac net/tinyos/matlab/MatlabControl.java</font></b></p>  </blockquote></blockquote><h3>Step 4 (optional):&nbsp; Changing Matlab's JRE</h3><p>Matlab comes with it's own JRE.&nbsp; To maximize consistency with your TinyOS programming environment,  replace Matlab's JRE with the one you usually use with TinyOS (probably1.3.1_01).&nbsp; Execute the following two commands, where MATLAB and JAVA are the root directories of your matlab and java installations, respectively <font size="1">&nbsp;(<a href="../../tools/matlab/doc/tutorial/jreIssues.html">What if I don't want to do this?</a>)</font>:</p><blockquote>  <blockquote>    <p><b><font face="Courier New">mv C:\MATLAB\sys\java\jre\win32\jre C:\MATLAB\sys\java\jre\win32\jreOld</font></b></p>    <p><b><font face="Courier New">cp C:\JAVA\jre    <a href="file:///C:/MATLAB/sys/java/jre/win32/jre">C:\MATLAB\sys\java\jre\win32\jre</a></font></b></p>  </blockquote></blockquote><p>&nbsp;</p><p><font size="5"><b>More Info</b></font></p><p><b>For the curious</b></p><ul>  <li>  <a href="../../tools/matlab/doc/tutorial/fileStructure.html">Where should I keep my   Matlab files?</a></li>  <li><a href="../../tools/matlab/doc/tutorial/defineTOSEnv.html">What does defineTOSEnvironment   do?</a></li>  <li>  <a href="../../tools/matlab/doc/tutorial/matlabClock.html">  How do I use Matlab timer and MatlabClock?</a></li>  <li>  <a href="../../tools/matlab/doc/tutorial/eventDrivenApplications.html">How do   I write Matlab applications for TinyOS?</a></li></ul><p><b>For the advanced</b></p><ul>  <li>  <a href="../../tools/matlab/doc/tutorial/shellVsMatlab.html">Making Java   classes Matlab Friendly </a></li>  <li><a href="../../tools/matlab/doc/tutorial/debugging.html">How about debugging?</a></li>  <li><a href="../../tools/matlab/doc/tutorial/speedingItUp.html">How can I make it run faster?</a>  </li></ul><p><b>For the beginner</b></p><ul>  <li>  <a href="../../tools/matlab/doc/tutorial/whatIsMatlab.html">What is Matlab, where do I get it, how do I install it, etc?</a></li>  <li>  <a href="../../tools/matlab/doc/tutorial/commands.html">What are the Matlab commands, syntax,   programming model, environment, etc?</a></li></ul><p><hr><b><a href="index.html"> Tutorial Index </a></b><hr></body></html>

⌨️ 快捷键说明

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