-
Notifications
You must be signed in to change notification settings - Fork 1
/
view.py
163 lines (116 loc) · 5.46 KB
/
view.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
import matplotlib
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.lines import Line2D
def figKwargs(fig, ax, **kwargs):
if 'title' in kwargs:
fig.suptitle = kwargs['title']
class SigView:
""" Signal Interface """
def __init__(self, timeYMax, freqYMax, avgFreqYMax, maxFreq, maxTime, linecolor, figname='Waveform', **kwargs):
self.fig, self.ax = plt.subplots(3, 1, num=figname, figsize=(5,5))
figKwargs(self.fig, self.ax, **kwargs)
## Axis 0: Signal in Time Domain
self.ax[0].set_xlim(0, maxTime)
self.ax[0].set_ylim(0, timeYMax)
self.ax[0].set_xlabel('Time (s)')
# self.ax[0].ticklabel_format(axis='x', style='sci', scilimits=(0,0), useMathText=True)
## Axis 1: Signal in Frequency Domain
self.ax[1].set_xlim(0, maxFreq)
self.ax[1].set_ylim(0, freqYMax)
self.ax[1].set_xlabel('Frequency (Hz)')
## Axis 2: Signal in Average Frequency Domain
self.ax[2].set_xlim(0, maxFreq)
self.ax[2].set_ylim(0, avgFreqYMax)
self.ax[1].set_xlabel('Frequency (Hz)')
self.fig.subplots_adjust(hspace=0.3)
self.timeLine, = self.ax[0].plot([], [], linecolor)
self.freqLine, = self.ax[1].plot([], [], linecolor)
self.avgFreqLine, = self.ax[2].plot([], [], linecolor)
def figShow(self):
plt.pause(1)
def init(self):
""" Return elements to matplotlib.Animation.FuncAnimation """
return self.timeLine, self.freqLine, self.avgFreqLine,
def update(self, frame, sigDict):
""" Return elements to matplotlib.Animation.FuncAnimation """
# print('frame', frame)
## if you needs to set ax.x_lim dynamically, blit has to be False
# self.ax[0].set_xlim(0, sigDict['timeAxis'][-1])
# self.ax[1].set_xlim(0, sigDict['freqAxis'][-1])
# self.ax[2].set_xlim(0, sigDict['freqAxis'][-1])
self.timeLine.set_data(sigDict['timeAxis'], sigDict['timeSig'])
self.freqLine.set_data(sigDict['freqAxis'], sigDict['freqSig'])
self.avgFreqLine.set_data(sigDict['freqAxis'], sigDict['processedSig'])
return self.timeLine, self.freqLine, self.avgFreqLine,
class ObjView:
""" Radar Interface """
def __init__(self, maxR, maxV, figname='Objects'):
self.fig, self.ax = plt.subplots(1, 1, num=figname, figsize=(5,3))
self.ax.set_xlim(0, maxR)
self.ax.set_ylim(-2, maxV)
self.ax.set_xlabel('Distance (m)')
self.ax.set_ylabel('Speed (m/s)')
# self.cmap = plt.get_cmap('magma')
self.objData = self.ax.scatter([], [], marker='o',s=30, c='m')
legend_elements = [ Line2D([0], [0], marker='o', color='w', label='915 MHz Frequency Radar',
markerfacecolor='r', markersize=8),
Line2D([0], [0], marker='o', color='w', label='5.8 GHz Frequency Radar',
markerfacecolor='g', markersize=8),]
self.ax.legend(handles=legend_elements, loc='upper right')
def figShow(self):
plt.pause(1)
def init(self):
""" Return elements to matplotlib.Animation.FuncAnimation """
return self.objData,
def update(self, frame, lowInfo, highInfo):
""" Return elements to matplotlib.Animation.FuncAnimation """
rangeData = []
veloData = []
colorData = []
if lowInfo is not None:
rangeData.extend([i[0] for i in lowInfo])
veloData.extend([i[1] for i in lowInfo])
colorData.extend(['r' for i in lowInfo])
if highInfo is not None:
rangeData.extend([i[0] for i in highInfo])
veloData.extend([i[1] for i in highInfo])
colorData.extend(['g' for i in highInfo])
self.objData.set_offsets(np.c_[rangeData, veloData])
self.objData.set_color(colorData)
return self.objData,
class PPIView:
""" Radar Interface """
def __init__(self, maxR, figname='PPI'):
self.fig = plt.figure(figname)
self.ax = self.fig.add_subplot(111, polar=True)
self.cax = self.fig.add_axes([0.85, 0.1, 0.03, 0.8])
self.ax.set_theta_zero_location("N")
self.ax.set_rmax(maxR)
self.ax.set_rticks(np.arange(0, maxR, maxR//5))
self.maxV = 25
self.cmap = plt.get_cmap('magma')
self.ppiData = self.ax.scatter([], [], marker='o',s=20, c=[], cmap=self.cmap)
self.cbar = plt.colorbar(self.ppiData, cax=self.cax)
self.cbar.set_ticks(np.linspace(0, self.maxV, 6)/self.maxV)
self.cbar.set_ticklabels(np.linspace(0, self.maxV, 6).astype(int))
self.cbar.set_label('Velocity (m/s)')
def figShow(self):
plt.pause(1)
def init(self):
""" Return elements to matplotlib.Animation.FuncAnimation """
return self.ppiData,
def update(self, frame, infoDict: dict):
""" Return elements to matplotlib.Animation.FuncAnimation """
directionData = []
rangeData = []
veloData = []
for direction, info in infoDict.items():
# info type: [(range, velo),]
if info is not None:
directionData.extend([direction/180*np.pi for i in info])
rangeData.extend([i[0] for i in info])
veloData.extend([i[1] for i in info])
self.ppiData.set_offsets(np.c_[directionData, rangeData])
self.ppiData.set_color([self.cmap(i/self.maxV) for i in veloData])
return self.ppiData,