📄 matlab.html
字号:
<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> </b></font></p> </blockquote></blockquote><p>You've now seen how to use Java from Matlab. From this example you should understand how to use all the TinyOS Java tools, and in fact all Java objects, from Matlab. <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. We could also get data into the Matlab workspace by storing the return value of a java function. This type of interaction with Java is completely supported by Matlab. 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>. </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. This is <i><b>not </b></i>supported by Matlab but is often much more useful. For example, it allows the Java MIG tools to pass incoming messages into Matlab whenever they arrive. 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. 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. 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. 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. 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. 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. It automatically generates Java 'Message' objects that correspond to TinyOS messages. As data streams in on the serial port, MoteIF will give such Message objects to objects that implement the MessageListener interface. MoteIF will also send Message objects to motes over the serial port. </p><p>In this example, we will see how to use MoteIF to send data into Matlab instead of to a Java object. 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. You should find a file called <font face="Courier New"><a href="../../tools/matlab/doc/demo/messageReceived.m">messageReceived.m</a></font>. 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. Double-click it to open it in the Matlab editor. 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. 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. 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). </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. 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. You've essentially just created a new 1-line version of ForwarderListen.java! </p><p>Now, put a break point in the messageReceived function by clicking to the left of line 3 in the Matlab editor. 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. You are now in debug mode and the prompt in the Matlab command window should turn to a "<font face="Courier New">K>></font>" to indicate that. Turn your mote off so that you don't get any more packets while you are in debug mode. <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. 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! 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: Change your Matlab path</h3><p>You need to tell Matlab where your TinyOS tools are located. Open Matlab, go to the "file" menu and choose "set path." Click the "add with subfolders" button and choose your <b><font face="Courier New">tinyos-1.x/tools/matlab/</font></b> directory. click "save" and "close"</p><h3>Step 2: Change your Matlab classpath</h3><p>You need to tell Matlab where your TinyOS Java tools are located. 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. This file lists the directories where Matlab looks for the Java classes that it uses. You need to add the directory of both your TinyOS Java tools and of your comm.jar file. 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. Save this file and then close and restart Matlab for your changes to take place.</p><h3>Step 3: Compile MatlabControl.java</h3><p>The <font face="Courier New">net.tinyos.matlab.MatlabControl</font> class is needed to call matlab commands from Java. 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. 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): Changing Matlab's JRE</h3><p>Matlab comes with it's own JRE. To maximize consistency with your TinyOS programming environment, replace Matlab's JRE with the one you usually use with TinyOS (probably1.3.1_01). Execute the following two commands, where MATLAB and JAVA are the root directories of your matlab and java installations, respectively <font size="1"> (<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> </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 + -