<body>
<div class="title">Lesson 11: Simulation with TOSSIM</div>
-<div class="subtitle">Last Modified: 18 May 2006</div>
+<div class="subtitle">Last Modified: 20 April 2007</div>
<p>This lesson introduces the TOSSIM simulator. You will become
familiar with how to compile TOSSIM and use some of its
<li>Inject packets.</li>
</ul>
</p>
-
-
+
+ <p><b>Note:</b> This tutorial is for TOSSIM in TinyOS 2.0.1. If
+ you are using TinyOS 2.0.0, it has a slightly different
+ A HREF="lesson11-200.html">tutorial.</A> The principal difference
+ between the two is how you specify noise in RF simulation.
+ </p>
<h1>Introduction</h1>
TOSSIM simulates entire TinyOS applications. It works by
</pre>
-
<p>Currently, the only platform TOSSIM supports is the
micaz. You should see output similar to this:</p>
<pre>
not properly compile on the first try. <A HREF="#appendix">Appendix A</A>
addresses some of the common causes and gives possible solutions.</p>
- <A name="#compiling"></A>
+
+ <A name="#compiling"></A>
<p>Compiling TOSSIM has five basic steps. Let's go through
<pre>
>>> m = t.getNode(32);
>>> m.bootAtTime(45654);
->>> t.runNextEVent()
+>>> t.runNextEvent()
1
</pre>
+ <p><b>Segmentation faults:</b>If trying to do this causes TOSSIM
+ to throw a segmentation violation (segfault), then chances are
+ you are using a version of gcc that does not work well with the
+ dynamic linking that TOSSIM is doing. In particular, it has been
+ verified to work properly with 4.0.2 and 3.6, but some people
+ have encountered problems with gcc 4.1.1.</p>
+
<p>Instead of using raw simulation ticks, you can also use the
call <code>ticksPerSecond()</code>. However, you want to be careful
to add some random bits into this number: having every node
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__swig_getmethods__', '__swig_setmethods__', '__weakref__', 'addChannel',
'currentNode', 'getNode', 'init', 'mac', 'newPacket', 'radio', 'removeChannel',
-'runNextEvent', 'setCurrentNode', 'setTime', 'this', 'thisown', 'time', 'timeStr']
+'runNextEvent', 'setCurrentNode', 'setTime', 'this', 'thisown', 'ticksPerSecond', 'time', 'timeStr']
</pre>
<p>The most common utility functions are:</p>
which is a noise trace taken from Meyer Library at Stanford University:</p>
<pre>
--97
+-39
+-98
+-98
-98
+-99
+-98
+-94
-98
--86
--90
--91
--87
--87
-98
-98
</pre>
t.getNode(i).createNoiseModel()</pre>
- <p>CPM can use a good deal of RAM: using the entire meyer-heavy trace as
- input has a cost of approximately 10MB. You can reduce this overhead
- by using a shorter trace; this will of course reduce simulation fidelity.</p>
-
- <p>The Radio object only deals with physical layer
+ <p>CPM can use a good deal of RAM: using the entire meyer-heavy
+ trace as input has a cost of approximately 10MB per node. You
+ can reduce this overhead by using a shorter trace; this will of
+ course reduce simulation fidelity. <u>The trace
+ must be at least 100 entries long, or CPM will not work as it does
+ not have enough data to generate a statistical model.</u> </p>
+
+ <p>You can also use
+ <p>The Radio object only deals with physical layer
propagation. The MAC object deals with the data link layer,
packet lengths, and radio bandwidth. The default TOSSIM MAC
object is for a CSMA protocol. You get a reference to the MAC
t.getNode(i).addNoiseTraceReading(val)
for i in range(1, 4):
+ print "Creating noise model for ",i;
t.getNode(i).createNoiseModel()
t.getNode(1).bootAtTime(100001);
format:
<pre>
-f = open("mirage-1.txt", "r")
+f = open("15-15-tight-mica2-grid.txt", "r")
lines = f.readlines()
for line in lines:
<p>The format of a configuration file is beyond the scope of
this document: the tool has its own <A
- HREF="usc-topologies.html">documentation</A>. TOSSIM has two sample configuration
+ HREF="usc-topologies.html">documentation</A>. TOSSIM has a few sample configuration
files generated from the tool in
- <code>tos/lib/tossim/topologies</code>. The first is <code><A
- HREF="">grid.txt</A></code>, which is a 10x10 grid of nodes
- spaced roughly 40 feet apart. Each node is placed randomly
- within a 40'x40' "cell." The cells follow a strict grid. The
- second file is <code><A HREF="">scatter.txt</A></code>, which is
- 100 nodes scattered randomly (with a uniform distribution)
- over a 360'x360' area. Note that the tool uses random numbers,
+ <code>tos/lib/tossim/topologies</code>.
+ Note that the tool uses random numbers,
these configuration files can generate multiple different
network topologies. Network topology files generated from the
- tool follow the same format as <code>mirage-1.txt</code>.</p>
+ tool follow the same format as <code>15-15-tight-mica2-grid.txt</code>.
+ If you have topologies measured from real networks, we would love
+ to include them in the TOSSIM distribution.</p>
<h1>Variables</h1>
variables properly. You do this by instantiating a Python
object that parses the XML file to extract all of the relevant
information. You have to import the Python support package for
- TOSSIM to do this:</p>
+ TOSSIM to do this. First, set your PYTHONPATH environment variable
+ to point to <tt>tinyos-2.x/support/sdk/python</tt>. This tells
+ Python where to find the TOSSIM packages. Then, in an interpreter
+ type this:</p>
<pre>
from tinyos.tossim.TossimApp import *
if (len(s) > 0):
if (s[0] == "gain"):
r.add(int(s[1]), int(s[2]), float(s[3]))
- elif (s[0] == "noise"):
- r.setNoise(int(s[1]), float(s[2]), float(s[3]))
+
+noise = open("meyer-heavy.txt", "r")
+lines = noise.readlines()
+for line in lines:
+ str = line.strip()
+ if (str != ""):
+ val = int(str)
+ for i in range(0, 4):
+ t.getNode(i).addNoiseTraceReading(val)
for i in range (0, 4):
- m = t.getNode(i)
- m.bootAtTime(randint(1000, 2000) * 1000000)
+ t.getNode(i).createNoiseModel()
+ t.getNode(i).bootAtTime(i * 2351217 + 23542399)
m = t.getNode(0)
v = m.getVariable("RadioCountToLedsC.counter")
+
+
while (v.getData() < 10):
t.runNextEvent()
+print "Counter variable at node 0 reached 10."
</pre>
<p>The TOSSIM <A
if (len(s) > 0):
if (s[0] == "gain"):
r.add(int(s[1]), int(s[2]), float(s[3]))
- elif (s[0] == "noise"):
- r.setNoise(int(s[1]), float(s[2]), float(s[3]))
+
+noise = open("meyer-heavy.txt", "r")
+lines = noise.readlines()
+for line in lines:
+ str = line.strip()
+ if (str != ""):
+ val = int(str)
+ for i in range(0, 4):
+ t.getNode(i).addNoiseTraceReading(val)
+
+for i in range (0, 4):
+ t.getNode(i).createNoiseModel()
for i in range(0, 60):
t.runNextEvent();
pkt.setType(msg.get_amType())
pkt.setDestination(0)
-print "Delivering " + msg.__str__() + " to 0 at " + str(t.time() + 3);
+print "Delivering " + str(msg) + " to 0 at " + str(t.time() + 3);
pkt.deliver(0, t.time() + 3)
want to deliver a packet through C++, you can do so.</p>
<p>Usually, the C++ and Python versions of a program look pretty
- similar. For example:</p>
+ similar. For example (note that this program will use a lot of RAM
+ and take a long time to start due to its noise models):</p>
<table WIDTH=800>
<PRE>
import TOSSIM
import sys
+import random
from RadioCountMsg import *
for i in range(0, 999):
m = t.getNode(i);
m.bootAtTime(5000003 * i + 1);
- r.setNoise(i, -99.0, 3.0);
+
for j in range (0, 2):
if (j != i):
r.add(i, j, -50.0);
-
-
+ # Create random noise stream
+ for j in range (0, 500):
+ m.addNoiseTraceReading(int(random.random() * 20) - 70);
+ m.createNoiseModel()
for i in range(0, 1000000):
t.runNextEvent();
<TD VALIGN=TOP>
<PRE>
#include <tossim.h>
-
+#include <stdlib.h>
int main() {
for (int i = 0; i < 999; i++) {
Mote* m = t->getNode(i);
m->bootAtTime(5000003 * i + 1);
- r->setNoise(i, -99.0, 3);
for (int j = 0; j < 2; j++) {
if (i != j) {
r->add(i, j, -50.0);
}
}
+ for (int j = 0; j < 500; j++) {
+ m->addNoiseTraceReading((char)(drand48() * 20) - 70);
+ }
+ m->createNoiseModel();
}
+
for (int i = 0; i < 1000000; i++) {
t->runNextEvent();
}
<i>slow</i>:</p>
<pre>
-(gdb) watch UscGainInterferenceModelC$receiving[23]
-Hardware watchpoint 2: UscGainInterferenceModelC$receiving[23]
+(gdb) watch CpmModelC$receiving[23]
+Hardware watchpoint 2: CpmModelC$receiving[23]
</pre>
<p>This variable happens to be an internal variable in the