-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSNLEDsPWM.py
More file actions
207 lines (169 loc) · 5.7 KB
/
SNLEDsPWM.py
File metadata and controls
207 lines (169 loc) · 5.7 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
# See http://stackoverflow.com/questions/29345325/raspberry-pyusb-gets-resource-busy#29347455
# Run python3.4 as root (sudo -i ...)
# (requires python3.4 for pypubsub)
# This sample reads the 3D mouse and uses Pulse Width Modulation to light the LEDs relative to the magnitude of the half-axis value
#
# It is a bit more complex because threads need to be run for each axis to implement the PWM.
# The main loop reads the USB device and sends the data to the threads using pypubsub.
#
import usb.core
import usb.util
import sys
from time import gmtime, strftime
import time
import RPi.GPIO as GPIO
import threading
from pubsub import pub
############################ Classes #######################################
class PWMThread(threading.Thread):
# class variables
updateRate = 100 # Hz
period = 1 / updateRate
axisRange = 350
def __init__(self, pos, neg, event):
self.posPin = pos
self.negPin = neg
self.axisVal = 0
theThread = threading.Thread(target=self.PWMAxis)
theThread.daemon = True
theThread.start()
pub.subscribe(self.setAxisValue, event)
def setAxisValue(self, val):
self.axisVal = val
#print("The axis Val is now ", self.axisVal);
return
def PWMAxis(self):
"PWM the LED on an axis"
# Need fast cycle rates (>100Hz) to make the LEDs look like they are dimming
while True:
if self.axisVal < 0:
pin = self.negPin
val = -self.axisVal
onTime = val/PWMThread.axisRange * PWMThread.period
offTime = PWMThread.period - onTime
GPIO.output(self.posPin, GPIO.LOW)
elif self.axisVal > 0:
pin = self.posPin
val = self.axisVal
onTime = val/PWMThread.axisRange * PWMThread.period
offTime = PWMThread.period - onTime
GPIO.output(self.negPin, GPIO.LOW)
else:
pin = 0
GPIO.output(self.posPin, GPIO.LOW)
GPIO.output(self.negPin, GPIO.LOW)
time.sleep(.5) # wait longer to let other threads run
if pin > 0:
GPIO.output(pin, GPIO.HIGH)
time.sleep(onTime)
GPIO.output(pin, GPIO.LOW)
time.sleep(offTime)
return
######################## Main ##############################################
# Look for SpaceNavigator
dev = usb.core.find(idVendor=0x46d, idProduct=0xc626)
if dev is None:
raise ValueError('SpaceNavigator not found');
else:
print ('SpaceNavigator found')
print (dev)
# Setup GPIO pins
TX_NEG_PIN = 11
TX_POS_PIN = 8
TY_NEG_PIN = 5
TY_POS_PIN = 7
TZ_NEG_PIN = 6
TZ_POS_PIN = 12
RX_NEG_PIN = 13
RX_POS_PIN = 16
RY_NEG_PIN = 19
RY_POS_PIN = 20
RZ_NEG_PIN = 26
RZ_POS_PIN = 21
GPIO.setmode(GPIO.BCM) # logical numbering
GPIO.setwarnings(False);
GPIO.setup(TX_NEG_PIN,GPIO.OUT)
GPIO.setup(TX_POS_PIN,GPIO.OUT)
GPIO.setup(TY_NEG_PIN,GPIO.OUT)
GPIO.setup(TY_POS_PIN,GPIO.OUT)
GPIO.setup(TZ_NEG_PIN,GPIO.OUT)
GPIO.setup(TZ_POS_PIN,GPIO.OUT)
GPIO.setup(RX_NEG_PIN,GPIO.OUT)
GPIO.setup(RX_POS_PIN,GPIO.OUT)
GPIO.setup(RY_NEG_PIN,GPIO.OUT)
GPIO.setup(RY_POS_PIN,GPIO.OUT)
GPIO.setup(RZ_NEG_PIN,GPIO.OUT)
GPIO.setup(RZ_POS_PIN,GPIO.OUT)
# Don't need all this but may want it for a full implementation
cfg = dev.get_active_configuration()
print ('cfg is ', cfg)
intf = cfg[(0,0)]
print ('intf is ', intf)
ep = usb.util.find_descriptor(intf, custom_match = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN)
print ('ep is ', ep)
reattach = False
if dev.is_kernel_driver_active(0):
reattach = True
dev.detach_kernel_driver(0)
ep_in = dev[0][(0,0)][0]
ep_out = dev[0][(0,0)][1]
# Make threads for the 6 axes
txThread = PWMThread(pos=TX_POS_PIN, neg=TX_NEG_PIN, event='tx')
tyThread = PWMThread(pos=TY_POS_PIN, neg=TY_NEG_PIN, event='ty')
tzThread = PWMThread(pos=TZ_POS_PIN, neg=TZ_NEG_PIN, event='tz')
rxThread = PWMThread(pos=RX_POS_PIN, neg=RX_NEG_PIN, event='rx')
ryThread = PWMThread(pos=RY_POS_PIN, neg=RY_NEG_PIN, event='ry')
rzThread = PWMThread(pos=RZ_POS_PIN, neg=RZ_NEG_PIN, event='rz')
print ('')
print ('Exit by pressing any button on the SpaceNavigator')
print ('')
run = True
while run:
try:
data = dev.read(ep_in.bEndpointAddress, ep_in.bLength, 0)
# raw data
# print data
# print it correctly T: x,y,z R: x,y,z
if data[0] == 1:
# translation packet
tx = data[1] + (data[2]*256)
ty = data[3] + (data[4]*256)
tz = data[5] + (data[6]*256)
if data[2] > 127:
tx -= 65536
if data[4] > 127:
ty -= 65536
if data[6] > 127:
tz -= 65536
print ("T: ", tx, ty, tz)
pub.sendMessage('tx', val=tx)
pub.sendMessage('ty', val=ty)
pub.sendMessage('tz', val=tz)
if data[0] == 2:
# rotation packet
rx = data[1] + (data[2]*256)
ry = data[3] + (data[4]*256)
rz = data[5] + (data[6]*256)
if data[2] > 127:
rx -= 65536
if data[4] > 127:
ry -= 65536
if data[6] > 127:
rz -= 65536
print ("R: ", rx, ry, rz)
pub.sendMessage('rx', val=rx)
pub.sendMessage('ry', val=ry)
pub.sendMessage('rz', val=rz)
if data[0] == 3 and data[1] == 0:
# button packet - exit on the release
run = False
except usb.core.USBError:
print("USBError")
# except:
# print("read failed")
# end while
# Cleanup GPIO pins
GPIO.cleanup()
usb.util.dispose_resources(dev)
if reattach:
dev.attach_kernel_driver(0)