-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathg29_mode.py
More file actions
158 lines (143 loc) · 6.3 KB
/
Copy pathg29_mode.py
File metadata and controls
158 lines (143 loc) · 6.3 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
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
import os
import platform
import yaml
import time
import threading
import argparse
import pygame
from awsdeepracer_control import Client
from core.logger import Logger
logger = Logger(logger="Gamepad_mode").getlog()
steer = 0
drive = 0
max_speed = 0.25
done = False
get_os = platform.system()
pygame.init()
joystick_count = pygame.joystick.get_count()
for i in range(joystick_count):
j = pygame.joystick.Joystick(i)
j.init()
def event_loop():
global done, drive, steer, max_speed
logger.info(u"Running On %s", get_os)
while not done:
pygame.init()
events = pygame.event.get()
for event in events:
if get_os == "Windows":
if event.type == pygame.JOYAXISMOTION and event.axis == 0: #stering_angle
if event.value < -1:
event.value = -1
else:
steer = event.value
drive = drive
max_speed = max_speed
if event.type == pygame.JOYBUTTONDOWN and event.button == 12: # D1
max_speed = 0.25
print(max_speed)
elif event.type == pygame.JOYBUTTONDOWN and event.button == 13: # D2
max_speed = 0.3
print(max_speed)
elif event.type == pygame.JOYBUTTONDOWN and event.button == 14: # D3
max_speed = 0.45
print(max_speed)
elif event.type == pygame.JOYBUTTONDOWN and event.button == 15: # D4
max_speed = 0.60
print(max_speed)
elif event.type == pygame.JOYBUTTONDOWN and event.button == 16: # D5
max_speed = 0.75
print(max_speed)
elif event.type == pygame.JOYBUTTONDOWN and event.button == 17: # D6
max_speed = 1.0
print(max_speed)
elif event.type == pygame.JOYAXISMOTION and event.axis == 1 and event.value > -1:#throttle
if event.value == 1.0:
event.value = 0
elif event.value > 0:
event.value = -0.1
drive = event.value
elif event.type == pygame.JOYBUTTONDOWN and event.button == 4: # + button hold it to back
drive *= -1
elif event.type == pygame.JOYBUTTONDOWN and event.button == 5: # - button hold it to back
drive *= -1
elif event.type == pygame.JOYAXISMOTION and event.axis == 2: # break to parking
drive = 0
elif event.type == pygame.JOYAXISMOTION and event.axis == 3 and event.value > -1: #clutch to back
if event.value == 1.0:
event.value = 0
elif event.value > 0:
event.value = -0.1
drive = event.value
drive *= -1
elif get_os == "Darwin":
if event.type == pygame.JOYAXISMOTION and event.axis == 0: #stering_angle
if event.value < -1:
event.value = -1
else:
steer = event.value
drive = drive
max_speed = max_speed
# print('\t', event.ev_type, event.code, event.state)
if event.type == pygame.JOYBUTTONDOWN and event.button == 12: # D1
max_speed = 0.25
print(max_speed)
elif event.type == pygame.JOYBUTTONDOWN and event.button == 13: # D2
max_speed = 0.3
print(max_speed)
elif event.type == pygame.JOYBUTTONDOWN and event.button == 14: # D3
max_speed = 0.45
print(max_speed)
elif event.type == pygame.JOYBUTTONDOWN and event.button == 15: # D4
max_speed = 0.60
print(max_speed)
elif event.type == pygame.JOYBUTTONDOWN and event.button == 16: # D5
max_speed = 0.75
print(max_speed)
elif event.type == pygame.JOYBUTTONDOWN and event.button == 17: # D6
max_speed = 1.0
print(max_speed)
elif event.type == pygame.JOYAXISMOTION and event.axis == 2 and event.value > -1: # throttle to go
if event.value == 1.0:
event.value = 0
elif event.value > 0:
event.value = -0.1
drive = event.value
elif event.type == pygame.JOYBUTTONDOWN and event.button == 4:# + button hold it to back
drive *= -1
elif event.type == pygame.JOYBUTTONDOWN and event.button == 5: # - button hold it to back
drive *= -1
elif event.type == pygame.JOYAXISMOTION and event.axis == 3: # break to parking
drive = 0
elif event.type == pygame.JOYAXISMOTION and event.axis == 1 and event.value > -1: #clutch to back
if event.value == 1.0:
event.value = 0
elif event.value > 0:
event.value = -0.1
drive = event.value
drive *= -1
return max_speed
def main():
#dir_path = os.path.dirname(os.path.realpath(__file__))
#with open(os.path.join(dir_path, "config.yaml"), "r") as ymlfile:
# cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
client = Client(password=os.getenv("password"), ip=os.getenv("hostIp"))
client.set_manual_mode()
client.start_car()
t1 = threading.Thread(target=event_loop, name="t1")
t1.start()
global done, drive, steer
while not done:
client.move(steer, drive, max_speed)
print(
"Steering command: "
+ str(steer)
+ " Throttle command: "
+ str(drive)
+ " Max_speed: "
+ str(max_speed)
)
client.stop_car()
t1.join
if __name__ == "__main__":
main()