-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathgripper.py
More file actions
executable file
·64 lines (50 loc) · 1.97 KB
/
gripper.py
File metadata and controls
executable file
·64 lines (50 loc) · 1.97 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
#!/usr/bin/env python3
# Software License Agreement (BSD License)
#
# Copyright (c) 2017, UFactory, Inc.
# All rights reserved.
#
# Author: Duke Fong <duke@ufactory.cc>
from time import sleep
from ...utils.log import *
class Gripper():
def __init__(self, ufc, node, iomap):
self.ports = {
'service': {'dir': 'in', 'type': 'service', 'callback': self.service_cb},
'cmd_sync': {'dir': 'out', 'type': 'service'},
}
self.logger = logging.getLogger('uf.' + node.replace('/', '.'))
ufc.node_init(node, self.ports, iomap)
def set_gripper(self, val):
if val == 'on':
self.ports['cmd_sync']['handle'].call('M2232 V1')
else:
self.ports['cmd_sync']['handle'].call('M2232 V0')
# TODO: modify the default timeout time with a service command
for _ in range(20):
ret = self.ports['cmd_sync']['handle'].call('P2232')
if val == 'on':
if ret == 'ok V2': # grabbed
return 'ok'
else:
if ret == 'ok V0': # stop
return 'ok'
sleep(0.5)
return 'err, timeout for {}, last ret: {}'.format(val, ret)
def service_cb(self, msg):
msg = msg.split(' ', 2)
if msg[1] == 'value':
if msg[0] == 'get':
ret = self.ports['cmd_sync']['handle'].call('P2232')
self.logger.debug('get value ret: %s' % ret)
if ret == 'ok V0':
return 'ok, stoped'
elif ret == 'ok V1':
return 'ok, working'
elif ret == 'ok V2':
return 'ok, grabbed'
else:
return 'err, unkown ret: %s' % ret
if msg[0] == 'set':
self.logger.debug('set value: %s' % msg[2])
return self.set_gripper(msg[2])