You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.5 KiB
94 lines
2.5 KiB
import os
|
|
import random
|
|
import datetime
|
|
import json
|
|
|
|
work_dir = 'Z:\\share\\workspace\\pycharm\\jam_det\\jammerDetect'
|
|
nodes_file = 'Z:\\share\\workspace\\pycharm\\jam_det\\jammerDetect\\jammerDetect.nodes'
|
|
app_file = 'Z:\\share\\workspace\\pycharm\\jam_det\\jammerDetect\\jammerDetect.app'
|
|
save_dir = 'Z:\\share\\workspace\\pycharm\\jam_det\\'
|
|
|
|
basic = [[500, 600, 0], [500, 400, 0], [600, 600, 0], [600, 400, 0], [800, 400, 0]]
|
|
DATA = []
|
|
scop = (-20, 20)
|
|
|
|
def write_node(locations, fnode):
|
|
l = []
|
|
for i in range(5):
|
|
l.append(f'{i+1} 0 ({locations[i][0]}, {locations[i][1]}, {locations[i][2]}) 0 0 0\n')
|
|
with open(fnode, 'w') as file:
|
|
file.writelines(l)
|
|
|
|
def set_jam(fapp, power):
|
|
with open(fapp, 'w') as file:
|
|
l1 = 'CBR 1 2 100 512 1S 1S 15S\n'
|
|
l2 = 'CBR 3 4 100 512 1S 1S 15S\n'
|
|
file.writelines(l1)
|
|
file.writelines(l2)
|
|
l3 = f'JAMMER 5 0S 15S 0 POWER {power}'
|
|
file.writelines(l3)
|
|
|
|
def run(name):
|
|
cmd = f'D:\\exata_build\\bin\\exata.exe .\\jammerDetect.config --simulation {name}'
|
|
os.chdir(work_dir)
|
|
out = os.popen(cmd).read()
|
|
det_all = out.count("detect")
|
|
det4 = out.count('detect jamming around node 4')
|
|
print(f'detect jamming {det_all} times; node4 {det4} times')
|
|
return det_all, det4
|
|
|
|
|
|
def gen_loc():
|
|
loc = [[0, 0, 0] for i in range(5)]
|
|
for i in range(5):
|
|
b0 = random.randint(scop[0], scop[1])
|
|
b1 = random.randint(scop[0], scop[1])
|
|
loc[i][0] = basic[i][0] + b0
|
|
loc[i][1] = basic[i][1] + b1
|
|
return loc
|
|
|
|
|
|
def dis(a, b):
|
|
x = a[0]-b[0]
|
|
y = a[1]-b[1]
|
|
return pow(pow(x, 2) + pow(y, 2), 0.5)
|
|
|
|
|
|
def show_dis(locations):
|
|
d12 = dis(locations[0], locations[1])
|
|
d25 = dis(locations[1], locations[4])
|
|
d34 = dis(locations[2], locations[3])
|
|
d45 = dis(locations[3], locations[4])
|
|
diss = {'d12': d12, 'd34': d34, 'd45': d45, 'd25': d25}
|
|
print(diss)
|
|
return diss
|
|
|
|
|
|
def experiment(name):
|
|
l = gen_loc()
|
|
print(l)
|
|
diss = show_dis(l)
|
|
write_node(l, nodes_file)
|
|
|
|
set_jam(app_file, 0)
|
|
res2 = run(name+'_0')
|
|
set_jam(app_file, 15)
|
|
res1 = run(name + '_1')
|
|
DATA.append((l, diss, res1, res2))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
curr_time = datetime.datetime.now()
|
|
time_str = datetime.datetime.strftime(curr_time, '%Y%m%d-%H_%M')
|
|
DATA.append(basic)
|
|
DATA.append(scop)
|
|
|
|
for i in range(2):
|
|
name = time_str+f'-{str(i).zfill(2)}'
|
|
experiment(name)
|
|
|
|
with open('Z:\\share\\workspace\\pycharm\\jam_det\\'+time_str, 'w', encoding='utf-8') as f:
|
|
json.dump(DATA, f)
|
|
|
|
|