-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjoint_teleop.py
177 lines (130 loc) · 5.03 KB
/
joint_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
import argparse
import sys
# FRI Client: https://github.com/cmower/FRI-Client-SDK_Python
import pyfri as fri
# PyGame: https://www.pygame.org/news
import pygame
pygame.init()
# NumPy: https://numpy.org/
import numpy as np
class Keyboard:
def __init__(self):
pygame.display.set_mode((300, 300))
self.max_joint_velocity = np.deg2rad(5)
self.joint_index = None
self.joint_velocity = 0.0
self.key_to_dir_map = {
pygame.K_LEFT: 1.0,
pygame.K_RIGHT: -1.0,
}
self.key_joint_map = [
pygame.K_1,
pygame.K_2,
pygame.K_3,
pygame.K_4,
pygame.K_5,
pygame.K_6,
pygame.K_7,
]
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_joint_map:
joint_index = self.key_joint_map.index(event.key)
if self.joint_index is None:
self.joint_index = joint_index
print(f"Turned ON joint {self.joint_index+1}")
else:
if joint_index == self.joint_index:
print(f"Turned OFF joint {self.joint_index+1}")
self.joint_index = None
elif event.key in self.key_to_dir_map:
self.joint_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.joint_velocity -= self.key_to_dir_map[event.key]
return self.joint_index, self.max_joint_velocity * self.joint_velocity
class TeleopClient(fri.LBRClient):
def __init__(self, keyboard):
super().__init__()
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("\n")
print("-----------------------------------------------------------")
print("-- Control robot joints using LEFT/RIGHT direction keys. --")
print("-- Press keys 1, ..., 7 to enable a specific joint. --")
print("-- The PyGame window must be in focus. --")
print(
"-----------------------------------------------------------",
end="\n\n\n",
)
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):
joint_index, vgoal = self.keyboard()
if isinstance(joint_index, int):
self.q[joint_index] += self.robotState().getSampleTime() * vgoal
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.",
)
return parser.parse_args()
def main():
print("Running FRI Version:", fri.FRI_CLIENT_VERSION)
args = args_factory()
keyboard = Keyboard()
client = TeleopClient(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())