forked from ApolloAuto/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.py
148 lines (118 loc) · 4.38 KB
/
item.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
#!/usr/bin/env python3
###############################################################################
# Copyright 2017 The Apollo Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################
"""
Time Value Item
"""
import numpy
from matplotlib import lines
class Item(object):
"""
Specific item to plot
"""
def __init__(self, ax, title, xlabel, ylabel, ymin, ymax):
self.ax = ax
self.title = title
self.ax.set_title(title)
self.ax.set_xlabel(xlabel, fontsize=10)
self.ax.set_ylabel(ylabel, fontsize=10)
self.ymin = ymin
self.ymax = ymax
self.ax.set_ylim([ymin, ymax])
self.lines = []
self.cartimehist = []
self.carvaluehist = []
self.targettime = []
self.targethist = []
self.targethistidx = -1
self.histidx = -1
self.prev_auto = False
self.planningavailable = False
def reset(self):
"""
Reset
"""
del self.lines[:]
del self.cartimehist[:]
del self.carvaluehist[:]
del self.targettime[:]
del self.targethist[:]
self.ax.cla()
self.ax.set_ylim([self.ymin, self.ymax])
self.targethistidx = -1
self.histidx = -1
self.prev_auto = False
self.planningavailable = False
def new_planning(self, time, values):
"""
new planning
"""
self.planningtime = time
self.planningvalues = values
if self.planningavailable == False:
self.ax.set_xlim([time[0] - 1, time[-1] + 10])
self.current_line = lines.Line2D(time, values, color='red', lw=1.5)
self.ax.add_line(self.current_line)
else:
self.current_line.set_data(time, values)
xmin, xmax = self.ax.get_xlim()
if (time[-1] >= (xmax - 1)):
self.ax.set_xlim([time[0] - 1, time[-1] + 10])
self.planningavailable = True
def new_carstatus(self, time, value, autodriving):
"""
new carstatus
"""
if autodriving and not self.prev_auto:
self.starttime = time
self.endtime = time + 50
self.ax.axvspan(self.starttime, self.endtime, fc='0.1', alpha=0.3)
elif autodriving and time >= (self.endtime - 20):
self.endtime = time + 50
self.ax.patches[-1].remove()
self.ax.axvspan(self.starttime, self.endtime, fc='0.1', alpha=0.3)
elif not autodriving and self.prev_auto:
self.endtime = time
self.ax.patches[-1].remove()
self.ax.axvspan(self.starttime, self.endtime, fc='0.1', alpha=0.3)
self.prev_auto = autodriving
self.cartimehist.append(time)
self.carvaluehist.append(value)
if self.planningavailable:
target = numpy.interp(time, self.planningtime, self.planningvalues)
self.targettime.append(time)
self.targethist.append(target)
if self.targethistidx == -1:
self.ax.plot(
self.targettime, self.targethist, color='green', lw=1.5)
self.targethistidx = len(self.ax.lines) - 1
else:
self.ax.lines[self.targethistidx].set_data(
self.targettime, self.targethist)
if self.histidx == -1:
self.ax.plot(self.cartimehist, self.carvaluehist, color='blue')
self.histidx = len(self.ax.lines) - 1
else:
self.ax.lines[self.histidx].set_data(self.cartimehist,
self.carvaluehist)
def draw_lines(self):
"""
plot lines
"""
for polygon in self.ax.patches:
self.ax.draw_artist(polygon)
for line in self.ax.lines:
self.ax.draw_artist(line)