-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrol.py
executable file
·314 lines (269 loc) · 8.37 KB
/
control.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
#!/usr/bin/env python3
# coding=utf-8
""" Control the Sphero RVR with a Gamepad (Microsoft XBox Controller) """
#-------------------
# RVR settings
#-------------------
# Valid speed values are 0-255
driveSpeed = 100
# Valid heading values are 0-359
driveHeading = 0
# the robot is "disarmed" at first; all Gamepad buttons are ignored, except the red one
armed = False
#-------------------
#Joystick stuff
#-------------------
import os, struct, array, sys
from fcntl import ioctl
# Iterate over the joystick devices.
print('Available Gamepad devices:')
for fn in os.listdir('/dev/input'):
if fn.startswith('js'):
print((' /dev/input/%s' % (fn)))
#else:
# print("None.")
# sys.exit(0)
# We'll store the states here.
axis_states = {}
button_states = {}
# These constants were borrowed from linux/input.h
axis_names = {
0x00 : 'x',
0x01 : 'y',
0x02 : 'z',
0x03 : 'rx',
0x04 : 'ry',
0x05 : 'rz',
0x06 : 'trottle',
0x07 : 'rudder',
0x08 : 'wheel',
0x09 : 'gas',
0x0a : 'brake',
0x10 : 'hat0x',
0x11 : 'hat0y',
0x12 : 'hat1x',
0x13 : 'hat1y',
0x14 : 'hat2x',
0x15 : 'hat2y',
0x16 : 'hat3x',
0x17 : 'hat3y',
0x18 : 'pressure',
0x19 : 'distance',
0x1a : 'tilt_x',
0x1b : 'tilt_y',
0x1c : 'tool_width',
0x20 : 'volume',
0x28 : 'misc',
}
button_names = {
0x120 : 'trigger',
0x121 : 'thumb',
0x122 : 'thumb2',
0x123 : 'top',
0x124 : 'top2',
0x125 : 'pinkie',
0x126 : 'base',
0x127 : 'base2',
0x128 : 'base3',
0x129 : 'base4',
0x12a : 'base5',
0x12b : 'base6',
0x12f : 'dead',
0x130 : 'a',
0x131 : 'b',
0x132 : 'c',
0x133 : 'x',
0x134 : 'y',
0x135 : 'z',
0x136 : 'tl',
0x137 : 'tr',
0x138 : 'tl2',
0x139 : 'tr2',
0x13a : 'select',
0x13b : 'start',
0x13c : 'mode',
0x13d : 'thumbl',
0x13e : 'thumbr',
0x220 : 'dpad_up',
0x221 : 'dpad_down',
0x222 : 'dpad_left',
0x223 : 'dpad_right',
# XBox 360 controller uses these codes.
0x2c0 : 'dpad_left',
0x2c1 : 'dpad_right',
0x2c2 : 'dpad_up',
0x2c3 : 'dpad_down',
}
axis_map = []
button_map = []
# Open the joystick device.
fn = '/dev/input/js0'
print(('Opening %s...' % fn))
jsdev = open(fn, 'rb')
# Get the device name.
#buf = bytearray(63)
buf = array.array('B', [0] * 64)
ioctl(jsdev, 0x80006a13 + (0x10000 * len(buf)), buf) # JSIOCGNAME(len)
js_name = buf.tostring().rstrip(b'\x00').decode('utf-8')
print(('Device name: %s' % js_name))
# Get number of axes and buttons.
buf = array.array('B', [0])
ioctl(jsdev, 0x80016a11, buf) # JSIOCGAXES
num_axes = buf[0]
buf = array.array('B', [0])
ioctl(jsdev, 0x80016a12, buf) # JSIOCGBUTTONS
num_buttons = buf[0]
# Get the axis map.
buf = array.array('B', [0] * 0x40)
ioctl(jsdev, 0x80406a32, buf) # JSIOCGAXMAP
for axis in buf[:num_axes]:
axis_name = axis_names.get(axis, 'unknown(0x%02x)' % axis)
axis_map.append(axis_name)
axis_states[axis_name] = 0.0
# Get the button map.
buf = array.array('H', [0] * 200)
ioctl(jsdev, 0x80406a34, buf) # JSIOCGBTNMAP
for btn in buf[:num_buttons]:
btn_name = button_names.get(btn, 'unknown(0x%03x)' % btn)
button_map.append(btn_name)
button_states[btn_name] = 0
#print(('%d axes found: %s' % (num_axes, ', '.join(axis_map))))
#print(('%d buttons found: %s' % (num_buttons, ', '.join(button_map))))
#-------------------
# signal handling
#-------------------
import signal
import sys
from time import sleep
# my signal handler
def sig_handler(_signo, _stack_frame):
# turn off RVR LEDs
turnLEDs(0, 0, 0)
# close RVR
print("Closing conection to RVR...")
rvr.close()
print("...done")
print("\n\ncontrol.py terminated clean.\n")
sys.exit(0)
# signals to be handled
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGHUP, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)
#-------------------
# RVR stuff
#-------------------
import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), './lib/')))
from sphero_sdk import SpheroRvrObserver
from sphero_sdk import Colors
from sphero_sdk import RvrLedGroups
from sphero_sdk import DriveFlagsBitmask
# create the RVR object.
# This also lets the robot do a firmware check every now and then.
print("Starting RVR observer...")
rvr = SpheroRvrObserver()
def turnLEDs(R, G, B):
rvr.led_control.set_all_leds_rgb(R, G, B)
# Delay to show LEDs change
sleep(1)
#-------------------------
# Wake up, Mr. Freeman!
#-------------------------
print("Waking up RVR...")
rvr.wake()
# Give RVR time to wake up
sleep(2)
print("...done")
# All LEDs to green
rvr.led_control.set_all_leds_color(color=Colors.green)
# reset yaw
print("Resetting yaw...")
rvr.reset_yaw()
print("...done")
# joystick loop
while True:
evbuf = jsdev.read(8)
if evbuf:
time, value, type, number = struct.unpack('IhBB', evbuf)
# initial button state
#if type & 0x80:
# print("(initial)")
# button change
if type & 0x01:
button = button_map[number]
if button:
button_states[button] = value
# pressed
if value:
# red butoon
if button == 'b':
# arm/disarm RVR
if armed == False:
armed = True
print("+++armed+++")
# set all LEDs to red
rvr.led_control.set_all_leds_color(color=Colors.red)
sleep(1)
else:
armed = False
print("+++disarmed+++")
# set all LEDs to green
rvr.led_control.set_all_leds_color(color=Colors.green)
sleep(1)
# RVR armed?
if armed:
if button == 'dpad_up':
print("FORWARD")
# drive
rvr.drive_with_heading(
speed=driveSpeed,
heading=driveHeading,
flags=DriveFlagsBitmask.none.value
)
elif button == 'dpad_down':
print("BACKWARD")
# drive
rvr.drive_with_heading(
speed=driveSpeed,
heading=driveHeading,
flags=DriveFlagsBitmask.drive_reverse.value
)
elif button == 'dpad_left':
print("LEFT")
# drive
rvr.drive_with_heading(
speed = driveSpeed,
heading = 270,
flags=DriveFlagsBitmask.none.value
)
elif button == 'dpad_right':
print("RIGHT")
# drive
rvr.drive_with_heading(
speed = driveSpeed,
heading = 90,
flags=DriveFlagsBitmask.none.value
)
else:
print(("%s pressed" % (button)))
else:
# button released
# RVR armed?
if armed:
if button == 'dpad_up' or button == 'dpad_down' or button == 'dpad_left' or button == 'dpad_right':
print("STOP")
# drive
rvr.drive_with_heading(
speed=0,
heading=0,
flags=DriveFlagsBitmask.none.value
)
#print(("%s released" % (button)))
# axis moved
#if type & 0x02:
# axis = axis_map[number]
# if axis:
# fvalue = value / 32767.0
# axis_states[axis] = fvalue
# print(("%s: %.3f" % (axis, fvalue)))