-
Notifications
You must be signed in to change notification settings - Fork 0
/
autosteer-schwad.py
103 lines (92 loc) · 2.97 KB
/
autosteer-schwad.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
#img = io.imread('./images/img5.jpg')
from autosteerfunctions import calculate_difference_line,rectangle_roi_center,get_schwad_distance_from_center
from simple_pid import PID
import cv2
import numpy as np
import win32gui
from PIL import ImageGrab
import time
import matplotlib.pyplot as plt
import vgamepad as vg
from win32api import GetKeyState
import win32api
import time
time.sleep(3)
gamepad = vg.VX360Gamepad()
windows_list = []
toplist = []
def enum_win(hwnd, result):
win_text = win32gui.GetWindowText(hwnd)
windows_list.append((hwnd, win_text))
win32gui.EnumWindows(enum_win, toplist)
def limit(value):
limit_val_up = 1
limit_val_down = -1
if value >= limit_val_up:
val = limit_val_up
else:
if value <= limit_val_down:
val = limit_val_down
else:
val = value
return val
# Game handle
game_hwnd = 0
for (hwnd, win_text) in windows_list:
#print(hwnd)
print(win_text)
if win_text == "Farming Simulator 22":
print("found game")
game_hwnd = hwnd
break
position = win32gui.GetWindowRect(game_hwnd)
pid = PID(1, 0.1, 0.01, setpoint=0)
#pid = PID(5, 1, 0.01, setpoint=93)
#setpoint = 93
#use_pid = True
gamepad_x = 0
freigabe = False
f = open("regelung_schwad.csv", "w")
output_text = "schwad_fehler;pid_out;gamepad_steering"
f.write(output_text + '\n')
while True:
if win32api.GetAsyncKeyState(13):
print("enter pressed")
freigabe = not freigabe
gamepad.left_joystick_float(x_value_float=0, y_value_float=0)
gamepad.update()
time.sleep(0.1)
screenshot = ImageGrab.grab(position)
screenshot = np.array(screenshot)
screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
#screenshot = cv2.resize(screenshot,(960,540))
roi = rectangle_roi_center(screenshot,0.06666,0.08666,-150,0)
schwad_fehler,roi = get_schwad_distance_from_center(roi,50,False)
if freigabe:
print("schwadfehler: ",schwad_fehler)
pid_out = pid(schwad_fehler)
print("PID_Out: ",pid_out)
new_gamepad_x = limit(round(pid_out/300,2))
if abs(new_gamepad_x - gamepad_x)>1:
print("Fehler aufgetreten")
gamepad.left_joystick_float(x_value_float=0, y_value_float=0)
gamepad.update()
else:
print("newgamepadx: ",new_gamepad_x)
output_text = "" + str(schwad_fehler) + ";" + str(pid_out) + ";" + str(new_gamepad_x)
f.write(output_text + '\n')
gamepad.left_joystick_float(x_value_float=new_gamepad_x, y_value_float=0)
gamepad.update()
gamepad_x = new_gamepad_x
#screenshot = cv2.resize(roi,(960,540))
window_name = "Screen"
cv2.namedWindow(window_name)
cv2.moveWindow(window_name, -1200,30)
cv2.imshow(window_name, roi)
cv2.waitKey(1)
cv2.destroyAllWindows()
f.close()
#diff_line,image = calculate_difference_line(img,False)
#if image is not None:
# plt.imshow(image)
#print(diff_line)