-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine_sim.py
More file actions
81 lines (67 loc) · 2.49 KB
/
Copy pathengine_sim.py
File metadata and controls
81 lines (67 loc) · 2.49 KB
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
import can
import time
import struct
import random
# CONNECT
bus = can.interface.Bus(channel='vcan0', interface='socketcan')
def send_telemetry(rpm, speed):
# Add the "Noise" we developed earlier
r_noise = rpm + random.randint(-50, 50)
s_noise = speed + random.uniform(-1, 1)
# Pack and Fire
data = struct.pack('>HH', int(r_noise), int(s_noise))
msg = can.Message(arbitration_id=0x100, data=data, is_extended_id=False)
bus.send(msg)
def run_track_test():
print("ENGINE: Track Mode Engaged. 5-Speed Transmission active.")
# CONFIGURATION
gear = 1
rpm = 900 # Idle
speed = 0
# Gear Ratios (How much speed you get per 1 RPM)
ratios = [0, 0.010, 0.016, 0.023, 0.032, 0.045]
while True:
# 1. PHYSICS: The engine revs up
rpm += random.randint(60, 80)
# 2. MATH: Speed is strictly defined by Gear Ratio
speed = rpm * ratios[gear]
# 3. TRANSMISSION LOGIC (The State Machine)
if rpm > 5500: # Redline hit!
if gear < 5:
print(f" -> SHIFTING to Gear {gear+1}...")
gear += 1
# CRITICAL: Speed stays same, RPM drops to match new gear
# New RPM = Current Speed / New Ratio
rpm = int(speed / ratios[gear])
else:
# We are in Top Gear, bounce off the limiter
rpm = 5400
# 4. RESET (Simulate coming to a stop at the end of the straight)
if speed > 220:
print(" << BRAKING ZONE >>")
# Rapid deceleration loop
while speed > 10:
speed -= 3
# Calculate RPM based on current gear
rpm = int(speed / ratios[gear])
# Automatic Downshifting
if rpm < 2000 and gear > 1:
gear -= 1
print(f" <- Downshift to Gear {gear}")
# Rev match (RPM jumps up on downshift)
rpm = int(speed / ratios[gear])
send_telemetry(rpm, speed)
time.sleep(0.02)
# Reset to start line
gear = 1
rpm = 900
print(" -- IDLE --")
time.sleep(1)
# Send the packet
send_telemetry(rpm, speed)
time.sleep(0.05) # 20Hz refresh
if __name__ == "__main__":
try:
run_track_test()
except KeyboardInterrupt:
print("Track Test Ended.")