-
Notifications
You must be signed in to change notification settings - Fork 1
/
RunSimulationScript.py
147 lines (117 loc) · 3.95 KB
/
RunSimulationScript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
print "********************************************"
print "* *"
print "* TOSSIM Script *"
print "* *"
print "********************************************"
import sys
import time
print "********************************************"
print "* *"
print "* TOSSIM Script *"
print "* *"
print "********************************************"
import sys
import time
from TOSSIM import *
t = Tossim([])
topofile = "topology.txt"
modelfile = "meyer-heavy.txt"
print "Initializing mac...."
mac = t.mac()
print "Initializing radio channels...."
radio = t.radio()
print " using topology file:", topofile;
print " using noise file:", modelfile;
print "Initializing simulator...."
t.init()
simulation_outfile = "tossim_log.txt"
print "Saving sensors simulation output to:", simulation_outfile;
#simulation_out = open(simulation_outfile, "w");
out = open(simulation_outfile, "w")
#out = sys.stdout;
# Add debug channel
print "Activate debug message on channel init"
t.addChannel("init", out)
print "Activate debug message on channel boot"
t.addChannel("boot", out)
print "Activate debug message on channel timer1"
t.addChannel("timer1", out)
print "Activate debug message on channel radio"
t.addChannel("radio", out)
print "Activate debug message on channel radio_send"
t.addChannel("radio_send", out)
print "Activate debug message on channel radio_rec"
t.addChannel("radio_rec", out)
print "Activate debug message on channel leds"
t.addChannel("leds", out)
print "Creating node 1..."
node1 = t.getNode(1)
time1 = 0 # instant at which each node should be turned on
node1.bootAtTime(time1)
print ">>>Will boot at time", time1, "[sec]"
print "Creating node 2..."
node2 = t.getNode(2)
time2 = 0
node2.bootAtTime(time2)
print ">>>Will boot at time", time2, "[sec]"
print "Creating node 3..."
node3 = t.getNode(3)
time3 = 0 # instant at which each node should be turned on
node3.bootAtTime(time3)
print ">>>Will boot at time", time3, "[sec]"
print "Creating node 4..."
node4 = t.getNode(4)
time4 = 0 # instant at which each node should be turned on
node4.bootAtTime(time4)
print ">>>Will boot at time", time4, "[sec]"
print "Creating node 5..."
node5 = t.getNode(5)
time5 = 0 # instant at which each node should be turned on
node5.bootAtTime(time5)
print ">>>Will boot at time", time5, "[sec]"
print "Creating node 6..."
node6 = t.getNode(6)
time6 = 0 # instant at which each node should be turned on
node6.bootAtTime(time6)
print ">>>Will boot at time", time6, "[sec]"
print "Creating node 7..."
node7 = t.getNode(7)
time7 = 0 # instant at which each node should be turned on
node7.bootAtTime(time7)
print ">>>Will boot at time", time7, "[sec]"
print "Creating radio channels..."
f = open(topofile, "r")
lines = f.readlines()
for line in lines:
s = line.split()
if (len(s) > 0):
print ">>>Setting radio channel from node ", s[0], " to node ", s[1], " with gain ", s[2], " dBm"
radio.add(int(s[0]), int(s[1]), float(s[2]))
# creation of channel model
print "Initializing Closest Pattern Matching (CPM)..."
noise = open(modelfile, "r")
lines = noise.readlines()
compl = 0
mid_compl = 0
print "Reading noise model data file:", modelfile;
print "Loading:",
for line in lines:
str = line.strip()
if (str != "") and (compl < 10000):
val = int(str)
mid_compl = mid_compl + 1
if (mid_compl > 5000):
compl = compl + mid_compl
mid_compl = 0
sys.stdout.write("#")
sys.stdout.flush()
for i in range(1, 8):
t.getNode(i).addNoiseTraceReading(val)
print "Done!"
for i in range(1, 8):
print ">>>Creating noise model for node:", i;
t.getNode(i).createNoiseModel()
print "Start simulation with TOSSIM! \n\n\n"
for i in range(0, 2400):
t.runNextEvent()
print "\n\n\nSimulation finished!"