-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mission3.py
168 lines (137 loc) · 4.54 KB
/
Mission3.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 20 15:23:08 2020
@author: ckielasjensen
"""
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import numpy as np
from agent import Agent
from parameters import Parameters
from target import Target
def plot1():
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.set_xlim(-60, 110)
ax.set_ylim(-60, 110)
# Initialize classes
params = Parameters()
target = Target(25, 25, 0)
agents = []
for i in range(params.nveh):
agents.append(Agent(0, 25*i, 0, params.monSpeed, 0, params, ax=ax))
# Give the target a commanded speed
target.send_cmd(3, 0)
# Get first plan
for i, agent in enumerate(agents):
agent.detect_target(target.get_state())
agent.compute_flight_traj(tf=params.tflight + i*params.tmon)
# Plot initial states
pts = target.get_state()
trgtPlot = ax.plot(pts[0], pts[1], 'r*', markersize=10, label='Target')
for i, agent in enumerate(agents):
agent.plot_arrow()
# Plot the inner and outer radii
cir1 = Circle(target.get_state()[:2], ls=':', fill=False, ec='r',
label='Outer Radius', radius=params.outerR)
cir2 = Circle(target.get_state()[:2], ls=':', fill=False, ec='r',
label='Outer Radius', radius=params.innerR)
cir3 = Circle(target.get_state()[:2], lw=None, fc='gray', alpha=0.5,
label='No Fly Zone', radius=params.noflyR)
ax.add_artist(cir1)
ax.add_artist(cir2)
ax.add_artist(cir3)
# Draw legend and clean up Agent class
ax.legend([trgtPlot[0]] + [agent._arrow for agent in agents],
['Target',
'Agent 1',
'Agent 2',
'Agent 3'])
Agent.agentIdx = 0
Agent.trajList = []
Agent.timeList = []
plt.title('$t = 0$')
return
def plot2():
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.set_xlim(50, 250)
ax.set_ylim(50, 250)
# Initialize classes
params = Parameters()
target = Target(25, 25, 0)
agents = []
for i in range(params.nveh):
agents.append(Agent(0, 25*i, 0, params.monSpeed, 0, params, ax=ax))
# Give the target a commanded speed
target.send_cmd(3, 0)
# Get first plan
for i, agent in enumerate(agents):
agent.detect_target(target.get_state())
agent.compute_flight_traj(tf=params.tflight + i*params.tmon)
agent.plot_arrow()
# Plot initial states
pts = target.get_state()
trgtPlot = ax.plot(pts[0], pts[1], 'r*', markersize=10, label='Target')
for i, agent in enumerate(agents):
agent.plot_arrow()
# Run the simulation
for t in np.arange(0, params.tflight + params.nveh*params.tmon + 0.1, 0.1):
# Update states
target.update(t)
for agent in agents:
agent.update(t)
# Detect target
if t % params.detPer < 1e-6:
for agent in agents:
agent.detect_target(target.get_state())
# Update plots
pts = target.get_state()
trgtPlot[0].set_data(pts[0], pts[1])
for i, agent in enumerate(agents):
agent.plot_arrow()
if t >= 1:
target.send_cmd(3, np.pi/2)
if t >= 1.5:
target.send_cmd(3, 0)
plt.pause(0.01)
# Plot the inner and outer radii
cir1 = Circle(target.get_state()[:2], ls=':', fill=False, ec='r',
label='Outer Radius', radius=params.outerR)
cir2 = Circle(target.get_state()[:2], ls=':', fill=False, ec='r',
label='Inner Radius', radius=params.innerR)
cir3 = Circle(target.get_state()[:2], lw=None, fc='gray', alpha=0.5,
label='No Fly Zone', radius=params.noflyR)
ax.add_artist(cir1)
ax.add_artist(cir2)
ax.add_artist(cir3)
# Draw legend and clean up Agent class
ax.legend([trgtPlot[0]] + [agent._arrow for agent in agents],
['Target',
'Agent 1',
'Agent 2',
'Agent 3'])
Agent.agentIdx = 0
Agent.trajList = []
Agent.timeList = []
plt.title(f'$t = {t}$')
return
def main():
# plot2()
plot1()
plot2()
if __name__ == '__main__':
plt.rcParams.update({
'font.size': 20,
'pdf.fonttype': 42,
'ps.fonttype': 42,
'xtick.labelsize': 20,
'ytick.labelsize': 20,
'lines.linewidth': 2,
'lines.markersize': 9
})
# plt.rcParams.update(plt.rcParamsDefault)
# plt.ion()
plt.close('all')
main()