-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathforza.py
448 lines (371 loc) · 17.7 KB
/
forza.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import os
import sys
import time
from concurrent.futures.thread import ThreadPoolExecutor
from os import listdir
from os.path import isfile, join
import matplotlib.pyplot as plt
sys.path.append(r'./forza_motorsport')
from fdp import ForzaDataPacket
import constants
import gear_helper
import helper
import keyboard_helper
from car_info import CarInfo
from constants import ConfigVersion
from logger import Logger
debug_properties = [
'gear', 'current_engine_rpm', 'speed', 'tire_slip_ratio_RL', 'tire_slip_ratio_RR', 'tire_slip_ratio_FL', 'tire_slip_ratio_FR', 'tire_slip_angle_RL', 'tire_slip_angle_RR', 'tire_slip_angle_FL', 'tire_slip_angle_FR', 'acceleration_x', 'acceleration_y',
'acceleration_z', 'velocity_x', 'velocity_y', 'velocity_z', 'accel', 'surface_rumble_FL', 'surface_rumble_FR', 'surface_rumble_RL', 'surface_rumble_RR', 'norm_driving_line', 'norm_ai_brake_diff', 'brake',
]
class Forza(CarInfo):
def __init__(self, threadPool: ThreadPoolExecutor, logger: Logger = None, packet_format='fh4', enable_clutch=False):
"""initialization
Args:
threadPool (ThreadPoolExecutor): threadPool
packet_format (str, optional): packet_format. Defaults to 'fh4'.
enable_clutch (bool, optional): enable_clutch. Defaults to False.
"""
super().__init__()
# === socket ===
self.ip = constants.ip
self.port = constants.port
# === logger ===
self.logger = (Logger()('Forza5')) if logger is None else logger
self.packet_format = packet_format
self.isRunning = False
self.threadPool = threadPool
self.enable_clutch = enable_clutch
self.farming = False
self.shift_point_factor = constants.shift_factor
# shortcuts
self.clutch = constants.clutch
self.upshift = constants.upshift
self.downshift = constants.downshift
self.boundKeys = lambda: [self.clutch, self.upshift, self.downshift]
# constant
self.config_folder = os.path.join(constants.root_path, 'config')
# create folders if not existed
if not os.path.exists(self.config_folder):
os.makedirs(self.config_folder)
# init constants from config if existed
helper.load_settings(self)
# === car data ===
self.gear_ratios = {}
self.rpm_torque_map = {}
self.shift_point = {}
self.records = []
self.last_upshift = time.time()
self.last_downshift = time.time()
# === exp farm setting ===
self.reset_car = 0
self.isBrake = False
self.reset_timer = time.time()
self.break_timer = time.time()
def test_gear(self, update_car_gui_func=None):
"""collect gear information
Args:
update_car_gui_func (optional): callback to update car gui. Defaults to None.
"""
try:
self.logger.debug(f'{self.test_gear.__name__} started')
helper.create_socket(self)
self.records = []
while self.isRunning:
fdp = helper.nextFdp(self.server_socket, self.packet_format)
if fdp is None:
continue
if fdp.speed > 0.1:
self.__update_forza_info(fdp, dump=False)
if update_car_gui_func is not None:
update_car_gui_func(fdp)
info = {
'gear': fdp.gear,
'rpm': fdp.current_engine_rpm,
'time': time.time(),
'speed': fdp.speed * 3.6,
'slip': min(1.1, (fdp.tire_slip_ratio_RL + fdp.tire_slip_ratio_RR) / 2),
'clutch': fdp.clutch,
'power': fdp.power / 1000.0,
'torque': fdp.torque,
'speed/rpm': fdp.speed * 3.6 / fdp.current_engine_rpm
}
self.records.append(info)
self.logger.debug(info)
except BaseException as e:
self.logger.exception(e)
finally:
self.isRunning = False
helper.close_socket(self)
self.logger.debug(f'{self.test_gear.__name__} finished')
def analyze(self, performance_profile: bool = True, is_gui: bool = False):
"""analyze data
Args:
performance_profile (bool, optional): plot figures or not. Defaults to True.
is_guid (bool, optional): is gui. Defaults to False
"""
try:
self.logger.debug(f'{self.analyze.__name__} started')
self.shift_point = gear_helper.calculate_optimal_shift_point(self)
helper.dump_config(self)
if performance_profile:
plt.close()
if is_gui:
plt.ion()
fig, ax = plt.subplots(2, 2)
fig.tight_layout()
# # gear vs ratio at 0, 0
helper.plot_gear_ratio(self, ax, 0, 0)
# torque vs rpm at 0, 1
helper.plot_torque_rpm(self, ax, 0, 1)
# torque vs speed at 1, 0
helper.plot_torque_speed(self, ax, 1, 0)
# rpm vs speed at 1, 1
helper.plot_rpm_speed(self, ax, 1, 1)
plt.show()
except Exception as e:
self.logger.exception(e)
self.logger.error("something went wrong. please re-test the car")
finally:
self.logger.debug(f'{self.analyze.__name__} ended')
def __update_forza_info(self, fdp: ForzaDataPacket, update_tree_func=lambda *args: None, dump: bool = True, first_load: bool = False):
"""update forza info while running
# try to load config if:
# self.ordinal != fdp.car_ordinal or self.car_perf != fdp.car_performance_index or self.car_class != fdp.car_class or self.car_drivetrain != fdp.drivetrain_type
Args:
fdp (ForzaDataPacket): datapackage
"""
if first_load or self.ordinal != fdp.car_ordinal or self.car_perf != fdp.car_performance_index or self.car_class != fdp.car_class or self.car_drivetrain != fdp.drivetrain_type:
self.ordinal = fdp.car_ordinal
self.car_perf = fdp.car_performance_index
self.car_class = fdp.car_class
self.car_drivetrain = fdp.drivetrain_type
res = True
if dump:
res = self.__try_auto_load_config(fdp)
if not res:
self.shift_point = {}
if update_tree_func is not None:
self.threadPool.submit(update_tree_func)
return res
else:
return True
def __try_auto_load_config(self, fdp: ForzaDataPacket):
"""auto load config while driving
Args:
fdp (ForzaDataPacket): fdp
Returns:
[bool]: success or failure
"""
try:
self.logger.debug(f'{self.__try_auto_load_config.__name__} started')
configs = [f for f in listdir(self.config_folder) if (isfile(join(self.config_folder, f)) and str(fdp.car_ordinal) in f)]
if len(configs) <= 0:
self.logger.warning(f'config ({fdp.car_ordinal}) is not found at folder {self.config_folder}. Please run gear test ({constants.collect_data}) and/or analysis ({constants.analysis}) first!!')
return False
elif len(configs) > 0:
self.logger.info(f'found ({fdp.car_ordinal}) config(s): {configs}')
# latest config version: ordinal-perf-drivetrain.json, v2
filename = helper.get_config_name(self)
if filename in configs:
if self.__try_loading_config(filename):
# remove legacy config if necessary
if len(configs) > 1:
self.__cleanup_legacy_config(configs)
return True
else:
return False
# if latest config version not existed. like only ordinal.json, v1
filename = helper.get_config_name(self, ConfigVersion.v1)
if filename in configs:
if self.__try_loading_config(filename):
self.car_perf = fdp.car_performance_index
self.car_class = fdp.car_class
self.car_drivetrain = fdp.drivetrain_type
# dump to latest config version
helper.dump_config(self)
self.__cleanup_legacy_config(configs)
return True
else:
return False
# unknown config
self.logger.warning(f'valid ({fdp.car_ordinal}) config(s) not found at {self.config_folder}: {configs}. Please run gear test ({constants.collect_data}) and/or analysis ({constants.analysis}) to create a new one!!')
return False
finally:
self.logger.debug(f'{self.__try_auto_load_config.__name__} ended')
def __cleanup_legacy_config(self, configs, latest_version: ConfigVersion = constants.default_config_version):
"""cleanup legacy configs
Args:
configs (list): list of configs
latest_version (ConfigVersion, optional): config version. Defaults to constants.default_config_version.
"""
for config in configs:
version = helper.get_config_version(self, config)
if version != latest_version:
try:
self.logger.warning(f'removing legacy config {config}')
path = os.path.join(self.config_folder, config)
os.remove(path)
except Exception as e:
self.logger.warning(f'failed to remove legacy config {config}: {e}')
def __try_loading_config(self, config):
"""try to load config
Args:
config (str): config file name
Returns:
bool: success or failure
"""
self.logger.info(f'loading config {config}')
helper.load_config(self, os.path.join(self.config_folder, config))
if len(self.shift_point) <= 0:
self.logger.warning(f'Config is invalid. Please run gear test ({constants.collect_data}) and/or analysis ({constants.analysis}) to create a new one!!')
return False
self.logger.info(f'loaded config {config}')
return True
def shifting(self, iteration, fdp):
"""shifting func
Args:
iteration (int): iteration
fdp (ForzaDataPacket): fdp
Returns:
[int]: iteration
"""
gear = fdp.gear
if len(self.shift_point) > 0 and fdp.speed > 0.1 and gear >= self.minGear:
iteration = iteration + 1
# prepare shifting params
slip = (fdp.tire_slip_ratio_RL + fdp.tire_slip_ratio_RR) / 2
f_slip = (fdp.tire_slip_ratio_FL + fdp.tire_slip_ratio_FR) / 2
angle_slip = abs((fdp.tire_slip_angle_RL + fdp.tire_slip_angle_RR) / 2)
f_angle_slip = abs((fdp.tire_slip_angle_FL + fdp.tire_slip_angle_FR) / 2)
slips = [slip, f_slip, angle_slip, f_angle_slip]
speed = fdp.speed * 3.6
rpm = fdp.current_engine_rpm
accel = fdp.accel
fired = False
debug_log = fdp.to_list(debug_properties)
self.logger.debug(f'[{iteration}] {debug_log}')
# up shift logic
if gear < self.maxGear and accel:
target_rpm = self.shift_point[gear]['rpmo'] * self.shift_point_factor
target_up_speed = int(self.shift_point[gear]['speed'] * self.shift_point_factor)
if self.car_drivetrain == 1:
# RWD logic
# When gear < 3, the upshift target rpm and speed would be a little bit (95%) lower than AWD when (slip >= 1 or angle_slip >= 1)
# at low gear (<= 3)
if gear < 3 and (angle_slip >= 1 or slip >= 1):
fired = self.__up_shift(rpm, target_rpm, speed, target_up_speed, slips, iteration, gear, fdp)
else:
fired = self.__up_shift(rpm, target_rpm, speed, target_up_speed, slips, iteration, gear, fdp)
else:
# AWD, FWD logic
fired = self.__up_shift(rpm, target_rpm, speed, target_up_speed, slips, iteration, gear, fdp)
# down shift logiciqw
if not fired and gear > self.minGear:
available_gears = self.shift_point.keys()
if gear - 1 in available_gears:
lower_gear = gear - 1
else:
lower_gear = min(available_gears, key=lambda x: abs(x - (gear - 1)))
target_down_speed = self.shift_point[lower_gear]['speed'] * self.shift_point_factor
# RWD logic
if self.car_drivetrain == 1:
# don't down shift to gear 1 when RWD
if gear >= 3:
self.__down_shift(speed, target_down_speed, slips, iteration, gear, fdp)
else:
self.__down_shift(speed, target_down_speed, slips, iteration, gear, fdp)
return iteration
def __up_shift(self, rpm, target_rpm, speed, target_up_speed, slips, iteration, gear, fdp):
"""up shift
Args:
rpm (float): rpm
target_rpm (float): target rpm to up shifting
speed (float): speed
target_up_speed (float): target speed to up shifting
slips (float): total combined slip/angles slip of front/rear tires
iteration (int): package iteration
gear (int): current gear
fdp (ForzaPackage): Forza Package
Returns:
_type_: _description_
"""
if rpm > target_rpm and slips[0] < 1 and speed > target_up_speed:
self.logger.debug(f'[{iteration}] up shift triggered. rpm > target rmp({rpm} > {target_rpm}), speed > target up speed ({speed} > {target_up_speed}), slips {slips}')
gear_helper.up_shift_handle(gear, self)
return True
else:
return False
def __down_shift(self, speed, target_down_speed, slips, iteration, gear, fdp):
"""down shift
Args:
speed (float): speed
target_down_speed (float): target speed to down shifting
slips (float): total combined slip/angles slip of front/rear tires
iteration (int): package iteration
gear (int): current gear
fdp (ForzaPackage): Forza Package
"""
if speed < target_down_speed * 0.95 and slips[0] < 1:
self.logger.debug(f'[{iteration}] down shift triggered. speed < target down speed ({speed} < {target_down_speed}), slips {slips}')
gear_helper.down_shift_handle(gear, self)
def __exp_farming_setup(self, fdp):
"""exp farming setup
Args:
fdp (ForzaDataPacket): datapackage
"""
if self.farming and fdp.car_ordinal > 0:
# enable reset car if exp or sp farming is True
if abs(fdp.norm_driving_line) >= 127 or fdp.speed < 20:
self.reset_car = self.reset_car + 1
# reset car position
if self.reset_car >= 100 and time.time() - self.reset_time > 10:
self.reset_car = 0
self.threadPool.submit(keyboard_helper.resetcar, self)
self.reset_time = time.time()
else:
self.reset_car = 0
# exp or sp farming to avoid afk detection, 30s interval
if time.time() - self.break_timer > 30 and fdp.norm_ai_brake_diff > 0:
self.threadPool.submit(keyboard_helper.press_brake, self)
self.break_timer = time.time()
def run(self, update_tree_func=lambda *args: None, update_car_gui_func=lambda *args: None):
"""run the auto shifting
Args:
update_tree_func (, optional): update tree view callback. Defaults to None.
update_car_gui_func (, optional): update car gui callback. Defaults to None.
"""
try:
self.logger.debug(f'{self.run.__name__} started')
helper.create_socket(self)
iteration = -1
self.reset_car = 0
self.reset_time = time.time()
refresh_time = time.time()
first_load = True
if self.farming:
keyboard_helper.pressdown_str('w')
while self.isRunning:
fdp = helper.nextFdp(self.server_socket, self.packet_format)
# fdp is not car information
if fdp is None or fdp.car_ordinal <= 0:
continue
# UI refresh, every 0.1s
if update_car_gui_func is not None and time.time() - refresh_time > 0.1:
self.threadPool.submit(update_car_gui_func, fdp)
refresh_time = time.time()
# load car config
self.__update_forza_info(fdp, update_tree_func, first_load=first_load)
first_load = False
# enable reset car if exp or sp farming is True
self.__exp_farming_setup(fdp)
# shifting
iteration = self.shifting(iteration, fdp)
except BaseException as e:
self.logger.exception(e)
finally:
self.isRunning = False
if self.farming:
keyboard_helper.release_str('w')
helper.close_socket(self)
self.logger.debug(f'{self.run.__name__} finished')