-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgamepad_microntek.py
More file actions
282 lines (250 loc) · 11.3 KB
/
Copy pathgamepad_microntek.py
File metadata and controls
282 lines (250 loc) · 11.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
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
# -*- coding: utf-8 -*-
#
# Control the VISCA PTZ camera using a standard (non-XBox) gamepad
# By Samarthya Lykamanuella (groaking)
# Licensed under GPL-3.0
#
# For Microntek USB Joystick types
# ---
# Gamepad event listener using Python
# -> SOURCE: https://github.com/zeth/inputs/blob/master/examples/gamepad_example.py
# Multithreading tips for gamepads
# -> SOURCE: https://gist.github.com/effedebe/6cae2a5849923fb373ab749594b9ed50
# SOURCE: https://www.pygame.org/docs/ref/joystick.html
import pygame
pygame.init()
from pyvisca import visca
from threading import Thread
from tkinter import messagebox
from tkinter.simpledialog import askstring
import numpy
import sys
import time
class GPad(Thread):
''' This class listens to the gamepad event without blocking the main code (using multithreading). '''
def __init__(self):
Thread.__init__(self)
self.ABS_HAT0 = (0, 0)
self.ABS_JOY_R_Y = 128
self.ABS_JOY_L_X = 128
self.ABS_JOY_L_Y = 128
self.ABS_JOY_R_X = 128
self.BTN_JOY_L = 0
self.BTN_JOY_R = 0
self.CIRCLE = 0
self.CROSS = 0
self.L1 = 0
self.L2 = 0
self.MENU = 0
self.R1 = 0
self.R2 = 0
self.SQUARE = 0
self.START = 0
self.TRIANGLE = 0
def run(self):
# This dict can be left as-is, since pygame will generate a
# pygame.JOYDEVICEADDED event for every joystick connected
# at the start of the program.
joysticks = {}
try:
while True:
# Event processing step.
# Possible joystick events: JOYAXISMOTION, JOYBALLMOTION, JOYBUTTONDOWN,
# JOYBUTTONUP, JOYHATMOTION, JOYDEVICEADDED, JOYDEVICEREMOVED
for event in pygame.event.get():
# Handle hotplugging
if event.type == pygame.JOYDEVICEADDED:
# This event will be generated when the program starts for every
# joystick, filling up the list without needing to create them manually.
joy = pygame.joystick.Joystick(event.device_index)
joysticks[joy.get_instance_id()] = joy
print(f"Joystick {joy.get_instance_id()} connencted")
for joystick in joysticks.values():
# Category of binary respond values
self.L1 = joystick.get_button(4)
self.L2 = joystick.get_button(6)
self.R1 = joystick.get_button(5)
self.R2 = joystick.get_button(7)
self.MENU = joystick.get_button(8)
self.START = joystick.get_button(9)
self.BTN_JOY_L = joystick.get_button(10)
self.BTN_JOY_R = joystick.get_button(11)
self.CIRCLE = joystick.get_button(1)
self.CROSS = joystick.get_button(2)
self.SQUARE = joystick.get_button(3)
self.TRIANGLE = joystick.get_button(0)
# Category of analog values
self.ABS_HAT0 = joystick.get_hat(0)
self.ABS_JOY_L_X = float( str(f"{ ('%.3f' % joystick.get_axis(0)) }") )
self.ABS_JOY_L_Y = float( str(f"{ ('%.3f' % joystick.get_axis(1)) }") )
self.ABS_JOY_R_X = float( str(f"{ ('%.3f' % joystick.get_axis(2)) }") )
self.ABS_JOY_R_Y = float( str(f"{ ('%.3f' % joystick.get_axis(3)) }") )
except Exception as e:
messagebox.showerror('Unknown gamepad error', f'Unknown error is detected. Please check your gamepad console connection: {e}')
sys.exit()
def get_speed(val, max_speed):
'''
Calculate the absolute speed according to the analog joystick's input voltage.
If val == 0.004, then the joystick is at rest.
The range of value (val) is within -1 and 1.
'''
i = numpy.abs( val )
return float( max_speed * float(i) )
def main(port='/dev/ttyUSB0'):
''' Actually controls the VISCA PTZ camera using joystick/gamepad. '''
try:
# Establish the non-blocking multithreading for analog input
game_pad = GPad()
game_pad.start()
# Establish and initialize the VISCA object
# (Change the port value according to your system's availability.)
cam = visca.PTZ(port)
# Set the max speed (pixel per 100 ms) of the X-Y joystick movement
MAX_MOVEMENT_SPEED = 7
MAX_ZOOM_SPEED = 7
# Set the delay time for movement speed
MOVEMENT_REDUNDANT_DELAY = 0.01
# Set the delay time after each movement, before stopping any continuous command
MOVEMENT_STOP_DELAY = 0.0005
MOVEMENT_STOP_DELAY_LONG = 0.5
# If val == 0.004, then the joystick is at rest.
JOYSTICK_REST_VAL = 0.004
JOYSTICK_MIN_VAL = -1
JOYSTICK_MAX_VAL = 1
# Fail-safe error catching with infinite loop
while True:
# Recalling presets: left hand
if game_pad.ABS_HAT0 == (0, 1) and game_pad.MENU == 0:
cam.preset_recall(4)
game_pad.ABS_HAT0 = (0, 0) # --- blocking
if game_pad.ABS_HAT0 == (1, 0) and game_pad.MENU == 0:
cam.preset_recall(5)
game_pad.ABS_HAT0 = (0, 0) # --- blocking
if game_pad.ABS_HAT0 == (0, -1) and game_pad.MENU == 0:
cam.preset_recall(6)
game_pad.ABS_HAT0 = (0, 0) # --- blocking
if game_pad.ABS_HAT0 == (-1, 0) and game_pad.MENU == 0:
cam.preset_recall(7)
game_pad.ABS_HAT0 = (0, 0) # --- blocking
# Recalling presets: right hand
if game_pad.TRIANGLE == 1 and game_pad.MENU == 0:
cam.preset_recall(0)
game_pad.TRIANGLE = 0 # --- blocking
if game_pad.CIRCLE == 1 and game_pad.MENU == 0:
cam.preset_recall(1)
game_pad.CIRCLE = 0 # --- blocking
if game_pad.CROSS == 1 and game_pad.MENU == 0:
cam.preset_recall(2)
game_pad.CROSS = 0 # --- blocking
if game_pad.SQUARE == 1 and game_pad.MENU == 0:
cam.preset_recall(3)
game_pad.SQUARE = 0 # --- blocking
# Setting/assigning presets: left hand
if game_pad.ABS_HAT0 == (0, 1) and game_pad.MENU == 1:
cam.preset_set(4)
game_pad.ABS_HAT0 = (0, 0) # --- blocking
if game_pad.ABS_HAT0 == (1, 0) and game_pad.MENU == 1:
cam.preset_set(5)
game_pad.ABS_HAT0 = (0, 0) # --- blocking
if game_pad.ABS_HAT0 == (0, -1) and game_pad.MENU == 1:
cam.preset_set(6)
game_pad.ABS_HAT0 = (0, 0) # --- blocking
if game_pad.ABS_HAT0 == (-1, 0) and game_pad.MENU == 1:
cam.preset_set(7)
game_pad.ABS_HAT0 = (0, 0) # --- blocking
# Setting/assigning presets: right hand
if game_pad.TRIANGLE == 1 and game_pad.MENU == 1:
cam.preset_set(0)
game_pad.TRIANGLE = 0 # --- blocking
if game_pad.CIRCLE == 1 and game_pad.MENU == 1:
cam.preset_set(1)
game_pad.CIRCLE = 0 # --- blocking
if game_pad.CROSS == 1 and game_pad.MENU == 1:
cam.preset_set(2)
game_pad.CROSS = 0 # --- blocking
if game_pad.SQUARE == 1 and game_pad.MENU == 1:
cam.preset_set(3)
game_pad.SQUARE = 0 # --- blocking
# Adjusting speed
# ---
# Low speed
if game_pad.R1 == 0 and game_pad.R2 == 0:
MOVEMENT_STOP_DELAY = 0.0001
MOVEMENT_STOP_DELAY_LONG = 0.05
MAX_ZOOM_SPEED = 1
MAX_MOVEMENT_SPEED = 1
# Medium speed
if game_pad.R1 == 1:
MOVEMENT_STOP_DELAY = 0.05
MOVEMENT_STOP_DELAY_LONG = 0.1
MAX_ZOOM_SPEED = 4
MAX_MOVEMENT_SPEED = 3
# Max speed
if game_pad.R2 == 1:
MOVEMENT_STOP_DELAY = 0.3
MOVEMENT_STOP_DELAY_LONG = 0.5
MAX_ZOOM_SPEED = 7
MAX_MOVEMENT_SPEED = 14
# Movement actions (left-right panning)
if game_pad.ABS_JOY_L_X != JOYSTICK_REST_VAL:
val = game_pad.ABS_JOY_L_X
i = get_speed(val, MAX_MOVEMENT_SPEED)
# Do the movement
if val >= JOYSTICK_MIN_VAL and val < JOYSTICK_REST_VAL:
cam.left(round(i))
time.sleep(MOVEMENT_STOP_DELAY)
cam.stop()
elif val > JOYSTICK_REST_VAL and val <= JOYSTICK_MAX_VAL:
cam.right(round(i))
time.sleep(MOVEMENT_STOP_DELAY)
cam.stop()
# Movement actions (up-down tilting)
if game_pad.ABS_JOY_L_Y != 128:
val = game_pad.ABS_JOY_L_Y
i = get_speed(val, MAX_MOVEMENT_SPEED)
# Do the movement
if val >= JOYSTICK_MIN_VAL and val < JOYSTICK_REST_VAL:
cam.up(round(i))
time.sleep(MOVEMENT_STOP_DELAY)
cam.stop()
elif val > JOYSTICK_REST_VAL and val <= JOYSTICK_MAX_VAL:
cam.down(round(i))
time.sleep(MOVEMENT_STOP_DELAY)
cam.stop()
# Movement actions (zoom)
if game_pad.ABS_JOY_R_Y != 128:
val = game_pad.ABS_JOY_R_Y
i = get_speed(val, MAX_ZOOM_SPEED)
# Do the movement
if val >= JOYSTICK_MIN_VAL and val < JOYSTICK_REST_VAL:
cam.zoom_in(round(i))
time.sleep(MOVEMENT_STOP_DELAY_LONG)
cam.zoom_stop()
elif val > JOYSTICK_REST_VAL and val <= JOYSTICK_MAX_VAL:
cam.zoom_out(round(i))
time.sleep(MOVEMENT_STOP_DELAY_LONG)
cam.zoom_stop()
# Analog center button press actions
# ---
# Move camera to home/default position
if game_pad.BTN_JOY_L == 1:
cam.home()
game_pad.BTN_JOY_L = 0 # --- blocking
# Perform autofocus
if game_pad.BTN_JOY_R == 1:
cam.autofocus_sens_low()
game_pad.BTN_JOY_R = 0 # --- blocking
# Prevent too fast a movement
time.sleep(MOVEMENT_REDUNDANT_DELAY)
# Wait until the end of the game_pad thread
game_pad.joint()
except:
messagebox.showerror('Unknown error', 'Unknown error is detected. Please check your PTZ connection')
sys.exit()
if __name__ == "__main__":
# Prompt for the PTZ's USB serial port
port = askstring(
'Serial USB Input',
'Please enter the VISCA PTZ\'s registered serial port\ne.g. Windows: "COM1", "COM2", etc.\ne.g. Linux: "/dev/ttyUSB0", "/dev/ttyUSB1", etc.'
)
main(port)