-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_any_link.py
151 lines (122 loc) · 5.49 KB
/
plot_any_link.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
148
149
150
151
# This script reads packet data from the listen node through the serial port
# and prints one RSS measurement on each line for all tx, rx, ch combination,
# where the rx != tx, both rx and tx are in the list of sensors, and ch is in
# the list of channels.
#
# Operation: Run this script in the terminal. There are no command line parameters
# We assume that you are only using a 2 node setup.
# The channelList must match the channels that are programmed on the nodes
# You can also select which channels to plot using the ch_list. Select any ordered numbers between 1 and the number of channels programmed on the nodes
#
#
#
# Version History:
#
# Version 1.1: Initial Release, 27 Jan 2016
#
import sys
import serial
import time
import rss as rss
import network_class_v1 as aNetwork
import rss_editor_class as aRssEdit
import myPlotter as aPlotter
import numpy as np
# What channels are measured by your nodes, in order of their measurement?
# USER: SET THIS TO THE CHANNELS IN YOUR CHANNEL GROUP
# channelList = [26, 11, 16, 21]
# channelList = [11,13,15,17,19,20,21,23,25,26]
# maxNodes = int(sys.argv[1])
# Automate process
print "Initializing..."
maxNodes, channelList = rss.run_sniffer()
print "Ready to plot."
# USER: The following serial "file name" changes depending on your operating
# system, and what name is assigned to the serial port when your listen
# node is plugged in.
serial_filename = rss.serialFileName()
sys.stderr.write('Using USB port file: ' + serial_filename + '\n')
ser = serial.Serial(serial_filename,38400)
# How many nodes the sensors have as the max # nodes (what # they're programmed with)
# USER: THIS SHOULD NOT BE CHANGED, IT IS 6 FOR ALL GROUPS IN OUR CLASS
# What node numbers are yours, that you want to see output to the file.
# USER: SET THIS TO THE NODE IDS ASSIGNED TO YOU. DO NOT INCLUDE THE LISTEN NODE NUMBER
nodeList = range(1,maxNodes+1) # 1, ..., 30
# Parameters that are due to our implementation of the listen node.
numNodes = len(nodeList)
numChs = len(channelList)
numLinks = numNodes*(numNodes-1)*numChs
rssIndex = 3
string_length = maxNodes + 7
suffix = ['ef','be'] # "0xBEEF"
# Initialize data, output file
nodeSet = set(nodeList)
channelSet = set(channelList)
currentLine = [] # Init serial data buffer "currentLine" as empty.
currentLinkRSS = [127] * numLinks
###############################
# Set up network
###############################
node_locs = np.random.random((numNodes,2))
num_nodes = numNodes
num_ch = numChs
node_list = np.array([1,4])
ch_list = np.array([6,7,8,9,10])
link_order_choice = 'f'
myNetwork = aNetwork.aNetwork(node_locs, num_nodes, num_ch, node_list, ch_list, link_order_choice)
################################
# Set up RSS editor
################################
myRssEdit = aRssEdit.RssEditor(myNetwork)
################################
# Set up RSS editor
################################
num_samples = 80
plot_obj = aPlotter.MYPLOTTER(myRssEdit,num_samples)
# Run forever, adding one integer at a time from the serial port,
# whenever an integer is available.
while(1):
tempInt = ser.read().encode('hex')
currentLine.append(tempInt)
# Whenever the end-of-line sequence is read, operate on the "packet" of data.
if currentLine[-2:] == suffix:
if len(currentLine) != string_length:
sys.stderr.write('packet corrupted - wrong string length\n')
del currentLine[:]
continue
currentLineInt = [int(x, 16) for x in currentLine]
rxId = currentLineInt[2]
currentCh = currentLineInt[-4]
if (rxId not in nodeSet) or (currentCh not in channelSet):
del currentLine[:]
continue
# Each line in the serial data has RSS values for multiple txids.
# Output one line per txid, rxid, ch combo.
for txId in nodeList:
# If the rxId is after the txId, then no problem -- currentCh
# is also the channel that node txId was transmitting on when
# node rxId made the measurement, because nodes transmit on a
# channel in increasing order.
if rxId > txId:
ch = currentCh
else:
ch = rss.prevChannel(channelList, currentCh)
# If the link (tx, rx, ch) is one we are supposed to watch
if txId != rxId:
i = rss.linkNumForTxRxChLists(txId, rxId, ch, nodeList, channelList)
# If the RSS has already been recorded for this link on
# this "line", then output the line first, and then restart
# with a new line.
if currentLinkRSS[i] < 127:
cur_line = ' '.join(map(str,currentLinkRSS)) + ' ' + str(time.time()) + '\n'
myRssEdit.observe(cur_line)
plot_obj.plot_current_image(myRssEdit.get_rss())
# sys.stdout.write(str(myRssEdit.get_rss().astype('int')) + '\n')
# sys.stdout.flush()
# Output currentLinkRSS vector
# Restart with a new line by resetting currentLinkRSS
currentLinkRSS = [127] * numLinks
# Store the RSS
currentLinkRSS[i] = rss.hex2signedint(currentLine[rssIndex+txId-1])
# Remove serial data from the buffer.
currentLine = []