-
Notifications
You must be signed in to change notification settings - Fork 0
/
playing_with_joysticks.py
90 lines (74 loc) · 5.88 KB
/
playing_with_joysticks.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
from djitellopy import Tello
import cv2
import pygame
import numpy as np
import time
import sys
'''
bottom of stick = 1
top of stick = -1
right of stick = 1
left of stick = -1
left stick x-axis = axis 0
left stick y-axis = axis 1
right stick x-axis = axis 2
right stick y-axis = axis 3
button0 =
button1 =
button2 =
button3 =
button4 =
button5 =
button6 =
button7 =
button8 =
button9 =
'''
pygame.init()
max_speed = 120
def get_joystick_power(joystick, axis):
global max_speed
if abs(np.round((joystick.get_axis(axis) * max_speed), 0)) < 15:
power = 0
else:
power = np.round((joystick.get_axis(axis) * max_speed), 0)
return power
def get_joystick_button_state(joystick, button):
return joystick.get_button(button)
joystick_count=pygame.joystick.get_count()
if joystick_count == 0:
# No joysticks!
print ("Error, I didn't find any joysticks.")
else:
# Use joystick #0 and initialize it
my_joystick = pygame.joystick.Joystick(0)
my_joystick.init()
should_stop = False
while not should_stop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
should_stop = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
should_stop = True
print(("left_X_axis {} left_Y_axis {} right_X_axis {} right_Y_axis {}".format(get_joystick_power(my_joystick, 0),
get_joystick_power(my_joystick, 1) * -1,
get_joystick_power(my_joystick, 2),
get_joystick_power(my_joystick, 3) * -1)))
# print("button0 {} button1 {} button2 {} button3 {} button4 {} button5 {} button6 {} button7 {} button8 {} button9 {} button10 {} button11 {} button12 {} button13 {} button14 {} button15 {}".format(get_joystick_button_state(my_joystick, 0),
# get_joystick_button_state(my_joystick, 1),
# get_joystick_button_state(my_joystick, 2),
# get_joystick_button_state(my_joystick, 3),
# get_joystick_button_state(my_joystick, 4),
# get_joystick_button_state(my_joystick, 5),
# get_joystick_button_state(my_joystick, 6),
# get_joystick_button_state(my_joystick, 7),
# get_joystick_button_state(my_joystick, 8),
# get_joystick_button_state(my_joystick, 9),
# get_joystick_button_state(my_joystick, 10),
# get_joystick_button_state(my_joystick, 11),
# get_joystick_button_state(my_joystick, 12),
# get_joystick_button_state(my_joystick, 13),
# get_joystick_button_state(my_joystick, 14),
# get_joystick_button_state(my_joystick, 15)))
time.sleep(0.05)