Commit aafbc658 authored by VABorisov's avatar VABorisov
Browse files

Update labRLS.py, results.txt, testlabRLS.py files

parents
import math
class TTarget:
x=y=t0=0
kind=str
def __init__(self,v,x0,y0,k):
self.v=v
self.x0=x0
self.y0=y0
self.k=k
"""def __del__(self):
print("destruct target")"""
def move(self,t):
pass
class TAircraft(TTarget):
def __init__(self, v, x0, y0, k):
super().__init__(v, x0, y0, k)
def move(self,t):
self.x=self.x0-self.v*9.8*math.cos(self.k)*9.8*(t-self.t0)
self.y=self.y0-self.v*9.8*math.sin(self.k)*9.8*(t-self.t0)
return(self.x,self.y)
class TMissle(TTarget):
def __init__(self, v, x0, y0, k, a):
super().__init__(v, x0, y0, k)
self.a=a
def move(self,t):
self.x=self.x0-(self.v+self.a*(t-self.t0))*9.8*math.cos(self.k)*9.8*(t-self.t0)
self.y=self.y0-(self.v+self.a*(t-self.t0))*9.8*math.sin(self.k)*9.8*(t-self.t0)
return(self.x,self.y)
class RLS:
def __init__(self,xrls,yrls,r,targets):
self.xrls=xrls
self.yrls=yrls
self.r=r
self.targets=targets
def __del__(self):
print('destruct RLS')
def Peleng(self,T0,TK):
f=open("results.txt","w")
dt=0.1
T=T0+0.1
TTarget.t0=T0
while T<=TK:
for object in self.targets:
TTarget.kind=(object[0])[:1]
if TTarget.kind=='A':
aircraft=TAircraft(object[1],object[2],object[3],object[4])
result=aircraft.move(T)
D=math.sqrt(((aircraft.x0-result[0])**2)+((aircraft.y0-result[1])**2))
if D<=self.r:
az=math.atan((aircraft.y0-result[1])/(aircraft.x0-result[0]))
f.write("Aircraft"+" "+str((object[0])[1:])+" "+":"+" "+"D="+str(D)+" "+"A="+str(az)+'\n')
if TTarget.kind=='M':
missle=TMissle(object[1],object[2],object[3],object[4],object[5])
result=missle.move(T)
D=math.sqrt(((missle.x0-result[0])**2)+((missle.y0-result[1])**2))
if D<=self.r:
az=math.atan((missle.y0-result[1])/(missle.x0-result[0]))
f.write("Missle"+" "+str((object[0])[1:])+" "+":"+" "+"D="+str(D)+" "+"A="+str(az)+'\n')
T=round(T+dt,3)
targets=[['A1', 14, 8, 11, 16], ['M1', 9, 7, 28, 32, 48],['A2', 21, 17, 22, 17], ['M2', 19, 16, 1, 17, 14],['A3', 46, 44, 9, 5], ['M3', 25, 14, 31, 5, 17]]
rls=RLS(16,23,200,targets=targets)
rls.Peleng(1,2)
Aircraft 1 : D=134.45600000000013 A=0.29203673205103375
Missle 1 : D=132.53520000000015 A=0.5840734641020676
Missle 2 : D=195.9216000000002 A=1.2920367320510338
import pytest
from numpy.testing import assert_almost_equal
from labRLS import TTarget,TAircraft,TMissle,RLS
def testaircraft():
aircraft1=TAircraft(5,10,42,1.05)
aircraft2=TAircraft(9,4,36,0.79)
TTarget.t0=2
resultA1=aircraft1.move(5)
resultA2=aircraft2.move(8)
assert_almost_equal(resultA1[0],-706.8008515928219, 8)
assert_almost_equal(resultA2[0],-3646.2544222230017, 8)
assert_almost_equal(resultA1[1],-1207.609898790741, 8)
assert_almost_equal(resultA2[1],-3648.0057272813015, 8)
def testmissle():
missle1=TMissle(5,10,42,1.05,2)
missle2=TMissle(9,4,36,0.79,4)
TTarget.t0=1
resultM1=missle1.move(6)
resultM2=missle2.move(4)
assert_almost_equal(resultM1[0],-3574.0042579641095, 8)
assert_almost_equal(resultM2[0],-4254.630159260168, 8)
assert_almost_equal(resultM1[1],-6206.049493953705, 8)
assert_almost_equal(resultM2[1],-4262.006681828185, 8)
def testrls():
targets=[['A1', 14, 8, 11, 16], ['M1', 9, 7, 28, 32, 48],['A2', 21, 17, 22, 17], ['M2', 19, 16, 1, 17, 14],['A3', 46, 44, 9, 5], ['M3', 25, 14, 31, 5, 17]]
rls=RLS(16,23,200,targets=targets)
rls.Peleng(1,2) #шаг 0.1
d=[]
az=[]
with open('results.txt','r') as file:
for strr in file:
if strr[0]=='A':
line=strr[1:-1]
dn=line.find('D')+2
dk=line.find('A')-1
D=float(line[dn:dk])
azn=line.find('A')+2
Az=float(line[azn:])
d.append(D)
az.append(Az)
if strr[0]=='M':
line=strr[1:-1]
dn=line.find('D')+2
dk=line.find('A')-1
D=float(line[dn:dk])
azn=line.find('A')+2
Az=float(line[azn:])
d.append(D)
az.append(Az)
Dcalculate=[134.456000000000135,132.53520000000015,195.9216000000002]
Azcalculate=[0.29203673205103375,0.5840734641020676,1.2920367320510338]
for i in range(len(d)):
assert_almost_equal(d[i],Dcalculate[i],8)
assert_almost_equal(az[i],Azcalculate[i],8)
testmissle()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment