-
Notifications
You must be signed in to change notification settings - Fork 117
/
teleop_pr2.py
executable file
·146 lines (125 loc) · 3.49 KB
/
teleop_pr2.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
#!/usr/bin/env python
from __future__ import print_function
import select
import sys
import termios
import tty
from pybullet_tools.pr2_utils import PR2_GROUPS, DRAKE_PR2_URDF
from pybullet_tools.utils import add_data_path, connect, enable_gravity, load_model, \
joints_from_names, load_pybullet, \
velocity_control_joints, disconnect, enable_real_time, HideOutput
HELP_MSG = """
Reading from the keyboard and Publishing to Twist!
---------------------------
Moving around:
u i o
j k l
m , .
For Holonomic mode (strafing), hold down the shift key:
---------------------------
U I O
J K L
M < >
t : up (+z)
b : down (-z)
anything else : stop
q/z : increase/decrease max speeds by 10%
w/x : increase/decrease only linear speed by 10%
e/c : increase/decrease only angular speed by 10%
CTRL-C to quit
"""
MOVE_BINDINGS = {
'i': (1, 0, 0, 0),
'o': (1, 0, 0, -1),
'j': (0, 0, 0, 1),
'l': (0, 0, 0, -1),
'u': (1, 0, 0, 1),
',': (-1, 0, 0, 0),
'.': (-1, 0, 0, 1),
'm': (-1, 0, 0, -1),
'O': (1, -1, 0, 0),
'I': (1, 0, 0, 0),
'J': (0, 1, 0, 0),
'L': (0, -1, 0, 0),
'U': (1, 1, 0, 0),
'<': (-1, 0, 0, 0),
'>': (-1, -1, 0, 0),
'M': (-1, 1, 0, 0),
't': (0, 0, 1, 0),
'b': (0, 0, -1, 0),
}
SPEED_BINDINGS = {
'q': (1.1, 1.1),
'z': (.9, .9),
'w': (1.1, 1),
'x': (.9, 1),
'e': (1, 1.1),
'c': (1, .9),
}
ESCAPE = '\x03'
#####################################
def get_key(settings):
tty.setraw(sys.stdin.fileno())
select.select([sys.stdin], [], [], 0)
key = sys.stdin.read(1)
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
return key
def print_velocities(speed, turn):
print("Speed: {} | Turn: {} ".format(speed, turn))
#####################################
def run_simulate(pr2):
joints = joints_from_names(pr2, PR2_GROUPS['base'])
dx = dy = dz = dth = 1
speed, turn = 0.5, 1.0
while True:
velocities = [dx * speed, dy * speed, dth * turn]
velocity_control_joints(pr2, joints, velocities)
def run_thread(pr2):
joints = joints_from_names(pr2, PR2_GROUPS['base'])
dx = dy = dz = dth = 0
speed, turn = 0.5, 1.0
settings = termios.tcgetattr(sys.stdin)
try:
print(HELP_MSG)
print_velocities(speed, turn)
while True: # TODO: getKeyboardEvents
key = get_key(settings) # Waits until a key is read
if key in MOVE_BINDINGS:
dx, dy, dz, dth = MOVE_BINDINGS[key]
elif key in SPEED_BINDINGS:
mspeed, mturn = SPEED_BINDINGS[key]
speed *= mspeed
turn *= mturn
print_velocities(speed, turn)
else: # When it receives another key
dx = dy = dz = dth = 0
if key == ESCAPE:
break
# twist.linear.dz = dz * speed
velocities = [dx * speed, dy * speed, dth * turn]
velocity_control_joints(pr2, joints, velocities)
except Exception as e:
print(e)
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
#####################################
def main():
# https://github.com/ros-teleop/teleop_twist_keyboard
# http://openrave.org/docs/latest_stable/_modules/openravepy/misc/#SetViewerUserThread
connect(use_gui=True)
add_data_path()
load_pybullet("plane.urdf")
#load_pybullet("models/table_collision/table.urdf")
with HideOutput():
pr2 = load_model(DRAKE_PR2_URDF, fixed_base=True)
enable_gravity()
enable_real_time() # TODO: won't work as well on OS X due to simulation thread
#run_simulate(pr2)
run_thread(pr2)
# TODO: keep working on this
#userthread = threading.Thread(target=run_thread, args=[pr2])
#userthread.start()
#userthread.join()
disconnect()
if __name__=="__main__":
main()