-
Notifications
You must be signed in to change notification settings - Fork 1
/
guigearstats.py
307 lines (245 loc) · 10.8 KB
/
guigearstats.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 15 10:37:54 2022
@author: RTB
"""
import tkinter
import tkinter.ttk
from collections import deque
import statistics
import constants
import math
from fdp import ForzaDataPacketDeque
MAXGEARS = 10
GEARLIST = range(MAXGEARS+1)
GEARLABELS = ['R'] + list(GEARLIST[1:]) #R,1,2,3,4,5,6,7,8,9,10
'''
TODO:
- update shiftdelay to be more accurate
- a full shift is longer than the distance between
- add option to hide unused gears for known cars through greying out rows
- add clutch display given known gear ratios for cars where the ratio doesn't float
'''
class GearRow():
def __init__(self, gear):
self.gear = gear #TODO:necessary?
self.label = GEARLABELS[gear]
self.ratio = 0 #separate to force an output format of 2.3f in GUI
self.ratio_var = tkinter.StringVar()
self.shiftrpm = tkinter.IntVar()
self.lastshiftrpm = tkinter.IntVar() #TODO: extend to deque?
self.reset()
def get_label(self):
return self.label.get()
def get_gearratio(self):
return self.ratio
def get_shiftrpm(self):
return self.shiftrpm.get()
def get_lastshiftrpm(self):
return self.lastshiftrpm.get()
def set_gearratio(self, val):
self.ratio = val
self.ratio_var.set(f'{val:2.3f}')
def set_shiftrpm(self, val):
self.shiftrpm.set(int(round(val, 0)))
def set_lastshiftrpm(self, val):
self.lastshiftrpm.set(int(round(val, 0)))
def set_canvas(self, frame, opts):
grid = {'row':self.gear+1, 'sticky':tkinter.EW}
tkinter.Label(frame, text=self.label, **opts).grid(column=0, **grid)
tkinter.Label(frame, textvariable=self.ratio_var, width=5, **opts).grid(column=1, **grid)
tkinter.Label(frame, textvariable=self.shiftrpm, **opts).grid(column=2, **grid)
tkinter.Label(frame, textvariable=self.lastshiftrpm, **opts).grid(column=3, **grid)
def reset(self):
self.set_gearratio(0)
if self.gear in [0, 10]:
self.shiftrpm.set('-')
self.lastshiftrpm.set('-')
else:
self.set_shiftrpm(0)
self.set_lastshiftrpm(0)
class GearTable():
FIRSTROW = ['Gear', 'Ratio', 'Optimal\nshiftrpm', 'Last\nshiftrpm']
def __init__(self):
self.rows = [GearRow(gear) for gear in GEARLIST]
def set_gearratio(self, gear, ratio):
self.rows[gear].set_gearratio(ratio)
def set_lastshiftrpm(self, gear, rpm):
self.rows[gear].set_lastshiftrpm(rpm)
def get_gearratios(self):
return [row.get_gearratio() for row in self.rows]
def set_gearratios(self, array):
for row, ratio in zip(self.rows, array):
row.set_gearratio(ratio)
def get_shiftrpms(self):
return [row.get_shiftrpm() for row in self.rows]
def set_shiftrpms(self, array):
for row, ratio in zip(self.rows, array):
row.set_shiftrpm(ratio)
def set_canvas(self, frame):
self.frame = tkinter.Frame(frame, border=0, bg=constants.background_color, relief="groove",
highlightthickness=True, highlightcolor=constants.text_color)
opts = {'bg':constants.background_color, 'fg':constants.text_color,
'font':('Helvetica 11 bold'), 'relief':tkinter.SUNKEN}
for column, name in enumerate(self.FIRSTROW):
tkinter.Label(self.frame, text=name, **opts).grid(row=0, column=column, sticky=tkinter.NSEW)
self.frame.columnconfigure(column, weight=1)
opts.update({'font':('Helvetica 11'), 'justify': tkinter.RIGHT, 'anchor':tkinter.E}) #the sticky EW should override anchor E?
for row in self.rows:
row.set_canvas(self.frame, opts)
def reset(self):
for row in self.rows:
row.reset()
class GUIGearStats:
deque_attributes = []
def __init__(self, logger, *args, **kwargs):
self.logger = logger
self.gatherratios = False
self.deque = deque(maxlen=120)
self.table = GearTable()
self.shiftdelay_deque = deque(maxlen=120)
self.shiftdelay_var = tkinter.StringVar()
self.shiftdelay_median = 0
self.shiftdelay_latest = 0
self.shiftdelay = deque(maxlen=20)
self.shiftdelay_step = 0
self.shiftdelay_gear = 0
self.shiftdelay_rpm = 0
self.shiftdelay_counter = 0
self.gear = 1
self.gear_var = tkinter.StringVar()
self.reset()
def display(self):
self.gear_var.set(f'Gear: {GEARLABELS[self.gear]}')
self.shiftdelay_var.set(f'Shiftdelay: {self.shiftdelay_latest/60:.2f}s {self.shiftdelay_median/60:.2f}s')
#return array of 11 elements: R, 1 to 10. R is generally 0
#currently, other code may assume R is guaranteed 0
def get_gearratios(self):
return self.table.get_gearratios()
#if array is stripped of empty gears, fill back up
#assume R and higher gears are empty
def set_gearratios(self, array):
if len(array) < len(GEARLIST):
array = [0] + array + [0]*(MAXGEARS - len(array))
self.table.set_gearratios(array)
#returns an array stripped of gear R and unused gears
def get_shiftrpms(self):
return [rpm for rpm in self.table.get_shiftrpms() if rpm != 0]
#if array is stripped of empty gears, fill back up
#assume R and higher gears are empty
def set_shiftrpms(self, array):
if len(array) < len(GEARLIST):
array = [0] + array + [0]*(MAXGEARS - len(array))
self.table.set_shiftrpms(array)
#//Corresponds to EDrivetrainType; 0 = FWD, 1 = RWD, 2 = AWD
#NOTE: ratio is off by relative front/back size if AWD conversion and front/rear are different sizes
def update_gearratios(self, fdp):
rpm = fdp.current_engine_rpm
if abs(fdp.speed) < 2 or rpm == 0: #if speed below 2 m/s assume faulty data
return
rad = 0
if fdp.drivetrain_type == 0: #FWD
rad = (fdp.wheel_rotation_speed_FL + fdp.wheel_rotation_speed_FR) / 2.0
elif fdp.drivetrain_type == 1: #RWD
rad = (fdp.wheel_rotation_speed_RL + fdp.wheel_rotation_speed_RR) / 2.0
else:
#rad = (fdp.wheel_rotation_speed_RL + fdp.wheel_rotation_speed_RR) / 2.0
rad = (fdp.wheel_rotation_speed_FL + fdp.wheel_rotation_speed_FR +
fdp.wheel_rotation_speed_RL + fdp.wheel_rotation_speed_RR) / 4.0
if abs(rad) <= 1e-6:
return
if rad < 0: #in the case of reverse
rad = -rad
self.deque.append(2 * math.pi * rpm / (rad * 60))
self.table.set_gearratio(fdp.gear, statistics.median(self.deque))
#we assume power is negative between gear change and first frame of shift
#accel has to be positive at all times, otherwise we don't know for sure
#where the shift starts
def derive_shiftrpm(self, fdp):
if (len(self.shiftdelay_deque) == 0 or self.shiftdelay_deque[0].gear >= fdp.gear
or self.shiftdelay_deque[0].gear == 0): #case gear reverse
self.shiftdelay_deque.appendleft(fdp)
return
#case gear has gone up
prev_packet = fdp
rpm = None
for packet in self.shiftdelay_deque:
if packet.accel == 0:
return
if prev_packet.power < 0 and packet.power >= 0:
rpm = packet.current_engine_rpm
break
prev_packet = packet
if rpm is not None:
self.table.set_lastshiftrpm(self.gear, rpm)
self.shiftdelay_deque.clear()
#TODO: update:
def update_car_info_shiftdelay(self, fdp):
if fdp.accel <= 0:
self.shiftdelay_step = 0
return
#fdp.accel > 0:
if self.shiftdelay_step == 0:
if fdp.power > 0:
self.shiftdelay_gear = fdp.gear
self.shiftdelay_rpm = fdp.current_engine_rpm
self.shiftdelay_counter = 0
elif fdp.power < 0:
self.shiftdelay_step = 1
if self.shiftdelay_step == 1:
if fdp.power < 0:
if fdp.gear == self.shiftdelay_gear:
self.shiftdelay_counter += 1
if fdp.gear == self.shiftdelay_gear+1:
self.shiftdelay_step = 2
else:
self.shiftdelay_step = 0
if self.shiftdelay_step == 2:
if fdp.power < 0:
self.shiftdelay_counter += 1
elif fdp.power > 0:
# self.table.set_lastshiftrpm(self.shiftdelay_gear, self.shiftdelay_rpm)
self.shiftdelay.append(self.shiftdelay_counter)
self.shiftdelay_latest = self.shiftdelay_counter
self.shiftdelay_median = statistics.median(self.shiftdelay)
self.shiftdelay_step = 0
else:
self.shiftdelay_step = 0
def update(self, fdp):
if fdp.is_race_on == 0:
return
if self.gatherratios:
if self.gear != fdp.gear: #self.gear is fdp.gear from last packet
self.deque.clear()
self.update_gearratios(fdp)
self.derive_shiftrpm(fdp)
self.update_car_info_shiftdelay(fdp)
self.gear = fdp.gear
def toggle_gatherratios(self):
self.gatherratios = not(self.gatherratios)
if self.gatherratios:
self.logger.info("Updating ratios")
else:
self.logger.info("Ratios not updating")
def set_canvas(self, frame):
self.frame = tkinter.Frame(frame, border=0, bg=constants.background_color, relief="groove",
highlightthickness=True, highlightcolor=constants.text_color)
self.table.set_canvas(self.frame)
self.table.frame.pack(fill='both', expand=True)
opts = {'bg':constants.background_color, 'fg':constants.text_color, 'justify':tkinter.LEFT, 'width':8}
tkinter.Label(self.frame, textvariable=self.gear_var, **opts, font=('Helvetica 20 bold')).pack(fill='x')
tkinter.Label(self.frame, textvariable=self.shiftdelay_var, **opts, font=('Helvetica 12')).pack(fill='x')
def reset(self):
self.gear = 1
self.shiftdelay.clear()
self.shiftdelay_median = 0
self.shiftdelay_latest = 0
self.shiftdelay_step = 0
self.shiftdelay_gear = 0
self.shiftdelay_rpm = 0
self.shiftdelay_counter = 0
self.counter = 0
self.table.reset()
self.deque.clear()
self.shiftdelay_deque.clear()
self.display()