-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhand_guide.py
135 lines (105 loc) · 3.56 KB
/
hand_guide.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
import argparse
import sys
import numpy as np
from admittance import AdmittanceController
import pyfri as fri
from pyfri.tools.filters import ExponentialStateFilter
from pyfri.tools.state_estimators import (
FRIExternalTorqueEstimator,
JointStateEstimator,
WrenchEstimatorTaskOffset,
)
if fri.FRI_CLIENT_VERSION_MAJOR == 1:
POSITION = fri.EClientCommandMode.POSITION
elif fri.FRI_CLIENT_VERSION_MAJOR == 2:
POSITION = fri.EClientCommandMode.JOINT_POSITION
class HandGuideClient(fri.LBRClient):
def __init__(self, lbr_ver):
super().__init__()
self.controller = AdmittanceController(lbr_ver)
self.joint_state_estimator = JointStateEstimator(self)
self.external_torque_estimator = FRIExternalTorqueEstimator(self)
self.wrench_estimator = WrenchEstimatorTaskOffset(
self,
self.joint_state_estimator,
self.external_torque_estimator,
self.controller.robot,
self.controller.ee_link,
)
self.wrench_filter = ExponentialStateFilter()
def command_position(self):
self.robotCommand().setJointPosition(self.q.astype(np.float32))
def monitor(self):
pass
def onStateChange(self, old_state, new_state):
print(f"State changed from {old_state} to {new_state}")
def waitForCommand(self):
if self.robotState().getClientCommandMode() != POSITION:
print(
f"[ERROR] hand guide example requires {POSITION.name} client command mode"
)
raise SystemExit
self.q = self.robotState().getIpoJointPosition()
self.command_position()
def command(self):
if not self.wrench_estimator.ready():
self.wrench_estimator.update()
self.robotCommand().setJointPosition(self.q.astype(np.float32))
return
# Get robot state
wr = self.wrench_estimator.get_wrench()
dt = self.robotState().getSampleTime()
# Filter wrench
wf = self.wrench_filter.filter(wr)
# Compute goal using admittance controller
self.q = self.controller(self.q, wf, dt)
# Command robot
self.command_position()
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()
client = HandGuideClient(args.lbr_ver)
app = fri.ClientApplication(client)
success = app.connect(args.port, args.hostname)
if not success:
print("Connection to KUKA Sunrise controller failed.")
return 1
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())