-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpedal.py
executable file
·78 lines (60 loc) · 2.14 KB
/
pedal.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
#!/usr/bin/env python
from __future__ import print_function
import time
import fcntl
from math import pi
import rospy
from sensor_msgs.msg import *
from evdev import InputDevice, list_devices, categorize, ecodes, events
class Pedal(object):
def __init__(self):
self.n_pedals = rospy.get_param('~n_pedals',1)
self.pedal_devs = []
self.joy_msg = Joy(buttons=[0]*self.n_pedals)
def get_value(self, dev):
latest_value = None
while not rospy.is_shutdown():
event = dev.read_one()
if event is None:
break
if event.type == ecodes.EV_KEY:
print(categorize(event))
if event.value in [events.KeyEvent.key_down, events.KeyEvent.key_hold]:
latest_value = 1
else:
latest_value = 0
return latest_value
def spin(self):
devices = [InputDevice(fn) for fn in list_devices()]
print(devices)
for dev in devices:
if dev.info.vendor == 0x0c45 and dev.info.product == 0x7403:
print('Using pedal "'+dev.name+'" on '+dev.phys)
print('Info:\n\n{}'.format(dev.info))
try:
dev.grab()
except IOError as err:
print('Couldn\'t grab device, trying again.')
continue
self.pedal_devs.append(dev)
if len(self.pedal_devs) == self.n_pedals:
break
self.joy_pub = rospy.Publisher('joy', Joy, queue_size=50)
r = rospy.Rate(50)
while not rospy.is_shutdown():
for i,dev in enumerate(self.pedal_devs):
latest_value = self.get_value(dev)
if latest_value is not None:
self.joy_msg.buttons[i] = latest_value
self.joy_msg.header.stamp = rospy.Time.now()
self.joy_pub.publish(self.joy_msg)
r.sleep()
for dev in self.pedal_devs:
dev.close()
print('done')
def main():
rospy.init_node('pedal')
p = Pedal()
p.spin()
if __name__ == '__main__':
main()