-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoystick-movement.py
672 lines (572 loc) · 26.8 KB
/
joystick-movement.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
from colorama import Style, Fore
from throttle import get_current_throttle, throttle_precision
import pydirectinput
import pyautogui
import threading
import pyglet
BRIGHT = Style.BRIGHT
NORMAL = Style.NORMAL
DIM = Style.DIM
BLACK = Fore.LIGHTBLACK_EX
RED = Fore.LIGHTRED_EX
DARK_RED = Fore.RED
GREEN = Fore.LIGHTGREEN_EX
DARK_GREEN = Fore.GREEN
YELLOW = Fore.LIGHTYELLOW_EX
BLUE = Fore.LIGHTBLUE_EX
DARK_BLUE = Fore.BLUE
MAGENTA = Fore.LIGHTMAGENTA_EX
DARK_MAGENTA = Fore.MAGENTA
CYAN = Fore.LIGHTCYAN_EX
DARK_CYAN = Fore.CYAN
WHITE = Fore.LIGHTWHITE_EX
RESET = Style.RESET_ALL
joysticks = pyglet.input.get_joysticks()
joystick = list(filter(lambda joystick: joystick.device.name == "T.A320 Copilot", joysticks))[0]
quadrant = list(filter(lambda joystick: joystick.device.name == "TCA Q-Eng 1&2", joysticks))[0]
joystick.open()
quadrant.open()
default_x = 1920 // 2
min_x = 120
max_x = 1800
default_y = 574
min_y = 80
max_y = 1000
x = default_x
y = default_y
joystick_name = "T.A320 Copilot"
quadrant_name = "TCA Q-Eng 1&2"
state = "Ground"
ground_rudder = "Rudder"
current_flap_configuration = 0
current_spoiler_configuration = 0
throttle_1_percentage = 0
throttle_2_percentage = 0
external = False
camera = False
camera_hat = (0, 0)
rev_throttle = (-0.06, -0.5)
ground_throttle = (0.44, 0.07)
clb_lvr = (0.94, 0.56)
flex_detent = (1.5, 1.06)
"""
Airliners:
- landing speed: 50%
- taxi speed: 15%
Jets:
- landing speed: 35%
- taxi speed: 9%
"""
############################################################
max_ground_taxi = 15
throttle_detents = [50, 80, 100]
############################################################
needed_throttle = 0
current_throttle = 0
ignition = False
engines = [False, False]
autopilot = False
twin_engine = True
pushback = False
parking_brake = True
third_event = threading.Event()
secondary_event = threading.Event()
event = threading.Event()
def camera_thread():
global camera_hat
while True:
if camera_hat != (0, 0):
pydirectinput.mouseDown(button="right", _pause=False)
value1 = 5 if camera_hat[0] == 1 else -5 if camera_hat[0] == -1 else 0
value2 = 5 if camera_hat[1] == 1 else -5 if camera_hat[1] == -1 else 0
if external is False:
if value2 > 0:
value2 = -value2
else:
value2 = abs(value2)
pydirectinput.moveRel(value1, value2, _pause=False, relative=True)
if not camera:
third_event.wait(0.2)
else:
third_event.wait(0.005)
def throttle_thread():
global current_throttle, pushback
while True:
if (not twin_engine) or (twin_engine and all(engines)):
if needed_throttle >= 0: # Forward Thrust
if current_throttle < needed_throttle:
throttle_difference = abs(current_throttle - needed_throttle)
duration = throttle_precision[throttle_difference]
event.clear()
while not event.is_set():
pydirectinput.keyDown("w", _pause=False)
event.wait(duration)
break
pydirectinput.keyUp("w", _pause=False)
event.clear()
secondary_event.wait(0.10)
current_throttle = get_current_throttle()
continue
if current_throttle > needed_throttle:
throttle_difference = abs(current_throttle - needed_throttle)
duration = throttle_precision[throttle_difference]
event.clear()
while not event.is_set():
pydirectinput.keyDown("s", _pause=False)
event.wait(duration)
break
pydirectinput.keyUp("s", _pause=False)
event.clear()
secondary_event.wait(0.10)
current_throttle = get_current_throttle()
continue
if pushback:
pushback = False
pydirectinput.press("p", _pause=False)
secondary_event.wait(0.10)
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{YELLOW}Pushback Disengaged.{RESET}")
elif needed_throttle < 0: # Reverse Thrust
if not pushback:
pushback = True
pydirectinput.press("p", _pause=False)
secondary_event.wait(0.10)
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{YELLOW}Pushback Engaged.{RESET}")
if current_throttle < abs(needed_throttle):
throttle_difference = abs(current_throttle - abs(needed_throttle))
duration = throttle_precision[throttle_difference]
event.clear()
while not event.is_set():
pydirectinput.keyDown("w", _pause=False)
event.wait(duration)
break
pydirectinput.keyUp("w", _pause=False)
event.clear()
secondary_event.wait(0.10)
current_throttle = get_current_throttle()
continue
if current_throttle > abs(needed_throttle):
throttle_difference = abs(current_throttle - abs(needed_throttle))
duration = throttle_precision[throttle_difference]
event.clear()
while not event.is_set():
pydirectinput.keyDown("s", _pause=False)
event.wait(duration)
break
pydirectinput.keyUp("s", _pause=False)
event.clear()
secondary_event.wait(0.10)
current_throttle = get_current_throttle()
continue
secondary_event.wait(0.1)
def get_overall_throttle(throttle_1, throttle_2):
return (throttle_1 + throttle_2) // 2
@quadrant.event
def on_joybutton_press(joystick, button):
global ignition, twin_engine, parking_brake, needed_throttle
if button == 2: # Engine 1 start
if ignition:
engines[0] = True
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{GREEN}Engine 1 ignition.{RESET}")
if (not twin_engine) or (twin_engine and all(engines)): # Engine START
pydirectinput.press("e", _pause=False)
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{MAGENTA}Engine start.{RESET}")
else:
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{RED}Failed to start Engine 1.{RESET}")
elif button == 3: # Engine 2 start
if ignition:
engines[1] = True
if not twin_engine:
engines[1] = False
print(f"{RED}For {YELLOW}twin engine mode{RED}, engine 1 must be active ONLY.{RESET}")
else:
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{GREEN}Engine 2 ignition.{RESET}")
if twin_engine and all(engines): # Engine START
pydirectinput.press("e", _pause=False)
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{MAGENTA}Engine start.{RESET}")
else:
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{RED}Failed to start Engine 2.{RESET}")
elif button == 4:
if twin_engine:
print(f"{CYAN}Twin engine mode {RED}disabled{CYAN}.{RESET}")
twin_engine = False
else:
print(f"{CYAN}Twin engine mode {GREEN}enabled{CYAN}.{RESET}")
twin_engine = True
elif button == 7: # IGN/START
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{GREEN}IGN/START.{RESET}")
ignition = True
elif button == 19: # Parking Brake On
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Parking brake ON.{RESET}")
parking_brake = True
if state == "Ground":
if needed_throttle > max_ground_taxi:
needed_throttle -= max_ground_taxi
elif needed_throttle >= 0:
needed_throttle = 0
elif needed_throttle < 0:
needed_throttle = -round((8 / 100) * abs(needed_throttle))
elif button == 20: # Landing Gear Up
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Landing gear UP.{RESET}")
pydirectinput.press("g", _pause=False)
@quadrant.event
def on_joybutton_release(joystick, button):
global ignition, parking_brake, needed_throttle, throttle_1_percentage, throttle_2_percentage, pushback
if button == 2: # Engine 1 cutoff
if engines[0] is True:
engines[0] = False
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{RED}Engine 1 cutoff.{RESET}")
if twin_engine and not any(engines): # Engine STOP
pydirectinput.press("e", _pause=False)
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{DARK_RED}Engine shutdown.{RESET}")
if pushback:
pushback = False
pydirectinput.press("p", _pause=False)
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{YELLOW}Pushback Disengaged.{RESET}")
elif not twin_engine:
pydirectinput.press("e", _pause=False)
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{RED}Engine cutoff.{RESET}")
if pushback:
pushback = False
pydirectinput.press("p", _pause=False)
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{YELLOW}Pushback Disengaged.{RESET}")
else:
throttle_1_percentage = 0
if twin_engine:
throttle = get_overall_throttle(throttle_1_percentage, throttle_2_percentage)
else:
throttle = throttle_1_percentage
if throttle < 0:
throttle = -round((32 / 100) * abs(throttle))
elif button == 3: # Engine 2 cutoff
if engines[1] is True:
engines[1] = False
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{RED}Engine 2 cutoff.{RESET}")
if twin_engine and not any(engines): # Engine STOP
pydirectinput.press("e", _pause=False)
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{DARK_RED}Engine shutdown.{RESET}")
if pushback:
pushback = False
pydirectinput.press("p", _pause=False)
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{YELLOW}Pushback Disengaged.{RESET}")
else:
throttle_2_percentage = 0
if twin_engine:
throttle = get_overall_throttle(throttle_1_percentage, throttle_2_percentage)
else:
throttle = throttle_1_percentage
if throttle < 0:
throttle = -round((32 / 100) * abs(throttle))
elif button == 7: # MODE NORM
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{CYAN}MODE NORM.{RESET}")
ignition = False
elif button == 19: # Parking Brake Off
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Parking brake OFF.{RESET}")
parking_brake = False
if state == "Ground":
if twin_engine:
throttle = get_overall_throttle(throttle_1_percentage, throttle_2_percentage)
else:
throttle = throttle_1_percentage
if throttle < 0:
throttle = -round((32 / 100) * abs(throttle))
if throttle >= 0 and throttle <= max_ground_taxi:
throttle = max_ground_taxi
needed_throttle = throttle
elif button == 20: # Landing Gear Down
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Landing gear DOWN.{RESET}")
pydirectinput.press("g", _pause=False)
@quadrant.event
def on_joyaxis_motion(joystick, axis, value):
global current_flap_configuration, current_spoiler_configuration, throttle_1_percentage, throttle_2_percentage, current_throttle, needed_throttle, pushback
if axis == "z": # Flaps
if value < -0.82: # Flaps 0
if current_flap_configuration == 0:
return
if 0 > current_flap_configuration:
pydirectinput.press("h", _pause=False) # Extend Flap
else:
pydirectinput.press("y", _pause=False) # Retract Flap
current_flap_configuration = 0
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Flaps set to 0.{RESET}")
elif -0.62 < value < -0.38: # Flaps 1
if current_flap_configuration == 1:
return
if 1 > current_flap_configuration:
pydirectinput.press("h", _pause=False) # Extend Flap
else:
pydirectinput.press("y", _pause=False) # Retract Flap
current_flap_configuration = 1
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Flaps set to 1.{RESET}")
elif -0.12 < value < 0.12: # Flaps 2
if current_flap_configuration == 2:
return
if 2 > current_flap_configuration:
pydirectinput.press("h", _pause=False) # Extend Flap
else:
pydirectinput.press("y", _pause=False) # Retract Flap
current_flap_configuration = 2
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Flaps set to 2.{RESET}")
elif 0.38 < value < 0.62: # Flaps 3
if current_flap_configuration == 3:
return
if 3 > current_flap_configuration:
pydirectinput.press("h", _pause=False) # Extend Flap
else:
pydirectinput.press("y", _pause=False) # Retract Flap
current_flap_configuration = 3
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Flaps set to 3.{RESET}")
elif value > 0.82: # Flaps 4
if current_flap_configuration == 4:
return
if 4 > current_flap_configuration:
pydirectinput.press("h", _pause=False) # Extend Flap
else:
pydirectinput.press("y", _pause=False) # Retract Flap
current_flap_configuration = 4
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Flaps set to 4.{RESET}")
elif axis == "rz": # Spoilers
if value < -0.82: # Spoilers 0%
if current_spoiler_configuration == 0:
return
current_spoiler_configuration = 0
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Spoilers set to 0%.{RESET}")
elif -0.62 < value < -0.38: # Spoilers 25%
if current_spoiler_configuration == 25:
return
current_spoiler_configuration = 25
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Spoilers set to 25%.{RESET}")
elif -0.12 < value < 0.12: # Spoilers 50%
if current_spoiler_configuration == 50:
return
current_spoiler_configuration = 50
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Spoilers set to 50%.{RESET}")
elif 0.38 < value < 0.62: # Spoilers 75%
if current_spoiler_configuration == 75:
return
current_spoiler_configuration = 75
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Spoilers set to 75%.{RESET}")
elif value > 0.82: # Spoilers 100%
if current_spoiler_configuration == 100:
return
current_spoiler_configuration = 100
print(f"{WHITE}[{YELLOW}CONFIG{WHITE}] {BRIGHT}{CYAN}Spoilers set to 100%.{RESET}")
elif axis == "x": # Throttle 1
if all(engines) or (not twin_engine and engines[0] is True):
real_value = round(1.5 - (value + 1), 2)
if real_value < -0.05: # Reverse Thrust
throttle_1_percentage = round((round(real_value, 2)/0.5) * 100)
elif real_value >= -0.05 and real_value <= 0.06: # Idle Thrust
throttle_1_percentage = 0
elif real_value >= 0.45 and real_value <= 0.55: # CLB (LANDING)
throttle_1_percentage = throttle_detents[0]
elif real_value >= 0.95 and real_value <= 1.05: # CLB
throttle_1_percentage = throttle_detents[1]
else:
minimum = 0
if real_value >= flex_detent[0]:
minimum = throttle_detents[2]
elif real_value >= clb_lvr[0]:
minimum = throttle_detents[1]
elif real_value >= ground_throttle[0]:
minimum = throttle_detents[0]
if ground_throttle[1] < real_value < ground_throttle[0]:
percent = round(((0.5 - abs(real_value - ground_throttle[0])) / 0.5) * 100, 2)
throttle_1_percentage = round(minimum + ((throttle_detents[0] / 100) * percent))
elif clb_lvr[1] < real_value < clb_lvr[0]:
percent = round(((0.5 - abs(real_value - clb_lvr[0])) / 0.5) * 100, 2)
throttle_1_percentage = round(minimum + ((abs(throttle_detents[0] - throttle_detents[1]) / 100) * percent))
elif flex_detent[1] < real_value < flex_detent[0]:
percent = round(((0.5 - abs(real_value - flex_detent[0])) / 0.5) * 100, 2)
throttle_1_percentage = round(minimum + ((abs(throttle_detents[1] - throttle_detents[2]) / 100) * percent))
else:
throttle_1_percentage = minimum
elif axis == "y": # Throttle 2
if all(engines):
real_value = round(1.5 - (value + 1), 2)
if real_value < -0.05: # Reverse Thrust
throttle_2_percentage = round((round(real_value, 2) / 0.5) * 100)
elif real_value >= -0.05 and real_value <= 0.06: # Idle Thrust
throttle_2_percentage = 0
elif real_value >= 0.45 and real_value <= 0.55: # CLB (LANDING)
throttle_2_percentage = throttle_detents[0]
elif real_value >= 0.95 and real_value <= 1.05: # CLB
throttle_2_percentage = throttle_detents[1]
else:
minimum = 0
if real_value >= flex_detent[0]:
minimum = throttle_detents[2]
elif real_value >= clb_lvr[0]:
minimum = throttle_detents[1]
elif real_value >= ground_throttle[0]:
minimum = throttle_detents[0]
if ground_throttle[1] < real_value < ground_throttle[0]:
percent = round(((0.5 - abs(real_value - ground_throttle[0])) / 0.5) * 100, 2)
throttle_2_percentage = round(minimum + ((throttle_detents[0] / 100) * percent))
elif clb_lvr[1] < real_value < clb_lvr[0]:
percent = round(((0.5 - abs(real_value - clb_lvr[0])) / 0.5) * 100, 2)
throttle_2_percentage = round(minimum + ((abs(throttle_detents[0] - throttle_detents[1]) / 100) * percent))
elif flex_detent[1] < real_value < flex_detent[0]:
percent = round(((0.5 - abs(real_value - flex_detent[0])) / 0.5) * 100, 2)
throttle_2_percentage = round(minimum + ((abs(throttle_detents[1] - throttle_detents[2]) / 100) * percent))
else:
throttle_2_percentage = minimum
if twin_engine:
throttle = get_overall_throttle(throttle_1_percentage, throttle_2_percentage)
else:
throttle = throttle_1_percentage
if state == "Ground":
if parking_brake:
if throttle > max_ground_taxi:
throttle -= max_ground_taxi
elif throttle >= 0:
throttle = 0
elif throttle < 0:
throttle = -round((8 / 100) * abs(throttle))
else:
if throttle >= 0 and throttle <= max_ground_taxi:
throttle = max_ground_taxi
elif throttle < 0:
throttle = -round((32 / 100) * abs(throttle))
elif state == "Airborne":
if throttle > 0 and throttle <= 10:
throttle -= round((throttle/100) * current_spoiler_configuration)
elif throttle > 10:
throttle -= round((10 / 100) * current_spoiler_configuration)
elif throttle < 0:
pushback_value = 32
pushback_value += round((68 / 100) * current_spoiler_configuration)
throttle = -round((pushback_value / 100) * abs(throttle))
event.set()
needed_throttle = throttle
@joystick.event
def on_joybutton_press(joystick, button):
global ground_rudder, state, autopilot, external, camera
if button == 0: # Switch between Rudder control on the ground (ONLY) and airborne mode meaning full joystick rotation & rudder control.
if state == "Airborne":
state = "Ground"
else:
state = "Airborne"
print(f"{BLUE}Switching to aircraft control state: {MAGENTA}{state}{RESET}")
elif button == 1:
if ground_rudder == "Rudder":
ground_rudder = "Joystick"
else:
ground_rudder = "Rudder"
print(f"{BLUE}Switching to Ground Rudder Control: {MAGENTA}{ground_rudder}{RESET}")
elif button == 2: # External Camera
if external:
external = False
camera = False
for i in range(7):
pyautogui.scroll(777, _pause=False)
third_event.wait(0.1)
pydirectinput.mouseUp(button="right", _pause=False)
print(f"{BLUE}External Camera: {RED}INACTIVE{RESET}")
else:
external = True
camera = True
pydirectinput.mouseDown(button="right", _pause=False)
third_event.wait(0.1)
for i in range(7):
pyautogui.scroll(-777, _pause=False)
third_event.wait(0.1)
print(f"{BLUE}External Camera: {MAGENTA}ACTIVE{RESET}")
elif button == 3: # Altitude Hold (autopilot)
if autopilot:
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{RED}Autopilot disconnected.{RESET}")
autopilot = False
else:
print(f"{WHITE}[{DARK_RED}IMPORTANT{WHITE}] {BRIGHT}{GREEN}Autopilot connected.{RESET}")
autopilot = True
pydirectinput.press("r", _pause=False)
@joystick.event
def on_joyhat_motion(joystick, hat_x, hat_y):
global camera, camera_hat
camera = True
if hat_x == 0 and hat_y == 0 and external is False:
camera = False
pydirectinput.mouseUp(button="right", _pause=False)
return
elif hat_x == 0 and hat_y == 0 and external is True:
pydirectinput.mouseDown(button="right", _pause=False)
third_event.wait(0.1)
elif external is False:
pydirectinput.mouseDown(button="right", _pause=False)
camera_hat = (hat_x, hat_y)
@joystick.event
def on_joyaxis_motion(joystick, axis, value):
global x, y
if state == "Airborne" and camera is False:
if axis == "x":
if value in [1.5259021896696368e-05, -1.5259021896696368e-05]: # Idle
pydirectinput.moveTo(default_x, y, _pause=False)
x = default_x
else:
if 0 < value < 1: # Yaw Right
joystick_percentage = (value / 1) * 100
x_rel = ((max_x - default_x) / 100) * joystick_percentage
x = round(default_x + x_rel)
elif -1 < value < 0: # Yaw Left
joystick_percentage = (value / -1) * 100
x_rel = ((default_x - min_x) / 100) * joystick_percentage
x = round(default_x - x_rel)
pydirectinput.moveTo(x, y, _pause=False)
elif axis == "y":
if value in [1.5259021896696368e-05, -1.5259021896696368e-05]: # Idle
pydirectinput.moveTo(x, default_y, _pause=False)
y = default_y
else:
if 0 < value < 1: # Pitch Up
joystick_percentage = (value / 1) * 100
y_rel = ((max_y - default_y) / 100) * joystick_percentage
y = round(default_y - y_rel)
elif -1 < value < 0: # Pitch Down
joystick_percentage = (value / -1) * 100
y_rel = ((default_y - min_y) / 100) * joystick_percentage
y = round(default_y + y_rel)
pydirectinput.moveTo(x, y, _pause=False)
elif axis == "rz":
if value in [1.5259021896696368e-05, -1.5259021896696368e-05]: # Idle
pydirectinput.keyUp("a", _pause=False)
pydirectinput.keyUp("d", _pause=False)
elif 0 < value < 1: # Rudder Right
pydirectinput.keyDown("d", _pause=False)
elif -1 < value < 0: # Rudder Left
pydirectinput.keyDown("a", _pause=False)
else:
pydirectinput.mouseUp(button="right", _pause=False)
if ground_rudder == "Rudder":
if axis == "rz":
if value in [1.5259021896696368e-05, -1.5259021896696368e-05]: # Idle
pydirectinput.moveTo(default_x, default_y, _pause=False)
x = default_x
else:
if 0 < value < 1: # Rudder Right
joystick_percentage = (value / 1) * 100
x_rel = ((max_x - default_x) / 100) * joystick_percentage
x = round(default_x + x_rel)
elif -1 < value < 0: # Rudder Left
joystick_percentage = (value / -1) * 100
x_rel = ((default_x - min_x) / 100) * joystick_percentage
x = round(default_x - x_rel)
pydirectinput.moveTo(x, y, _pause=False)
else:
if axis == "x":
if value in [1.5259021896696368e-05, -1.5259021896696368e-05]: # Idle
pydirectinput.moveTo(default_x, default_y, _pause=False)
x = default_x
else:
if 0 < value < 1: # Rudder Right
joystick_percentage = (value / 1) * 100
x_rel = ((max_x - default_x) / 100) * joystick_percentage
x = round(default_x + x_rel)
elif -1 < value < 0: # Rudder Left
joystick_percentage = (value / -1) * 100
x_rel = ((default_x - min_x) / 100) * joystick_percentage
x = round(default_x - x_rel)
pydirectinput.moveTo(x, y, _pause=False)
thread = threading.Thread(target=throttle_thread)
thread.start()
cam_thread = threading.Thread(target=camera_thread)
cam_thread.start()
pyglet.app.run()