-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtask_teleop.py
196 lines (144 loc) · 5.5 KB
/
task_teleop.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
import argparse
import sys
from collections import OrderedDict
# PyGame: https://www.pygame.org/news
import pygame
# FRI Client: https://github.com/cmower/FRI-Client-SDK_Python
import pyfri as fri
pygame.init()
# NumPy: https://numpy.org/
import numpy as np
np.set_printoptions(precision=5, suppress=True, linewidth=1000)
# Local scripts
from ik import IK
def print_instructions():
print("\n")
print("-" * 65)
print("-- Control robot joints using LEFT/RIGHT direction keys. --")
print("-- Press keys x, y, z, r, p, a to enable a specific task axis. --")
print("-- The PyGame window must be in focus. --")
print("-" * 65, end="\n\n\n")
class Keyboard:
def __init__(self):
pygame.display.set_mode((300, 300))
self.max_task_velocity = 0.04
self.task_index = None
self.task_velocity = 0.0
self.key_to_dir_map = {
pygame.K_LEFT: 1.0,
pygame.K_RIGHT: -1.0,
}
self.key_task_map = OrderedDict()
self.key_task_map[pygame.K_x] = "x"
self.key_task_map[pygame.K_y] = "y"
self.key_task_map[pygame.K_z] = "z"
self.key_task_map[pygame.K_r] = "rx"
self.key_task_map[pygame.K_p] = "ry"
self.key_task_map[pygame.K_a] = "rz"
def __call__(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
# User closed pygame window -> shutdown
pygame.quit()
raise SystemExit
if event.type == pygame.KEYDOWN:
# Keydown event
if event.key == pygame.K_ESCAPE:
pygame.quit()
raise SystemExit
if event.key in self.key_task_map:
task_index = list(self.key_task_map.keys()).index(event.key)
task_label = self.key_task_map[event.key]
if self.task_index is None:
self.task_index = task_index
print(f"Turned ON task {task_label}")
else:
if task_index == self.task_index:
print(f"Turned OFF task {task_label}")
self.task_index = None
elif event.key in self.key_to_dir_map:
self.task_velocity += self.key_to_dir_map[event.key]
elif event.type == pygame.KEYUP:
# Keyup event
if event.key in self.key_to_dir_map:
self.task_velocity -= self.key_to_dir_map[event.key]
return self.task_index, self.max_task_velocity * self.task_velocity
class TeleopClient(fri.LBRClient):
def __init__(self, ik, keyboard):
super().__init__()
self.ik = ik
self.keyboard = keyboard
self.torques = np.zeros(fri.LBRState.NUMBER_OF_JOINTS)
def monitor(self):
pass
def onStateChange(self, old_state, new_state):
print(f"State changed from {old_state} to {new_state}")
if new_state == fri.ESessionState.MONITORING_READY:
self.q = None
self.torques = np.zeros(fri.LBRState.NUMBER_OF_JOINTS)
print_instructions()
def waitForCommand(self):
self.q = self.robotState().getIpoJointPosition()
self.robotCommand().setJointPosition(self.q.astype(np.float32))
if self.robotState().getClientCommandMode() == fri.EClientCommandMode.TORQUE:
self.robotCommand().setTorque(self.torques.astype(np.float32))
def command(self):
task_index, vgoal = self.keyboard()
if isinstance(task_index, int):
vg = np.zeros(len(self.keyboard.key_task_map))
vg[task_index] = vgoal
self.q = self.ik(self.q, vg, self.robotState().getSampleTime())
self.robotCommand().setJointPosition(self.q.astype(np.float32))
if self.robotState().getClientCommandMode() == fri.EClientCommandMode.TORQUE:
self.robotCommand().setTorque(self.torques.astype(np.float32))
def args_factory():
parser = argparse.ArgumentParser(description="LRBJointSineOverlay example.")
parser.add_argument(
"--hostname",
dest="hostname",
default=None,
help="The hostname used to communicate with the KUKA Sunrise Controller.",
)
parser.add_argument(
"--port",
dest="port",
type=int,
default=30200,
help="The port number used to communicate with the KUKA Sunrise Controller.",
)
parser.add_argument(
"--lbr-ver",
dest="lbr_ver",
type=int,
choices=[7, 14],
required=True,
help="The KUKA LBR Med version number.",
)
return parser.parse_args()
def main():
print("Running FRI Version:", fri.FRI_CLIENT_VERSION)
args = args_factory()
ik = IK(args.lbr_ver)
keyboard = Keyboard()
client = TeleopClient(ik, keyboard)
app = fri.ClientApplication(client)
success = app.connect(args.port, args.hostname)
if not success:
print("Connection to KUKA Sunrise controller failed.")
return 1
print("Connection to KUKA Sunrise controller established.")
try:
while success:
success = app.step()
if client.robotState().getSessionState() == fri.ESessionState.IDLE:
break
except KeyboardInterrupt:
pass
except SystemExit:
pass
finally:
app.disconnect()
print("Goodbye")
return 0
if __name__ == "__main__":
sys.exit(main())