-
Notifications
You must be signed in to change notification settings - Fork 1
/
controllers.py
executable file
·325 lines (244 loc) · 9.78 KB
/
controllers.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from __future__ import annotations
import copy
from typing import Tuple
from abc import ABC, abstractmethod
import numpy as np
import scipy
from spatialmath import SE3, SO3
from itmobotics_sim.utils.robot import RobotControllerType, EEState, JointState, Robot, Motion
class VectorController(ABC):
"""_summary_"""
def __init__(self):
pass
@abstractmethod
def u(self, err: np.ndarray):
pass
@abstractmethod
def reset(self):
pass
class MPIDController(VectorController):
"""_summary_
Args:
P (np.ndarray): P coefficient
I (np.ndarray): I coefficient
D (np.ndarray): D coefficient
dt (float): time step size
"""
def __init__(self, P: np.ndarray, I: np.ndarray, D: np.ndarray, dt: float):
super().__init__()
self.__P = P
self.__I = I
self.__D = D
self.__dt = dt
self.__integral_value = np.zeros(self.__I.shape[0])
self.__wind_up_max = np.ones(self.__I.shape[0]) * 0.1
self.__last_err = np.zeros(self.__I.shape[0])
def reset(self):
"""reset controller internal values"""
self.__integral_value = np.zeros(self.__I.shape[0])
self.__last_err = np.zeros(self.__I.shape[0])
def u(self, err: np.ndarray) -> float:
"""calculate control
Args:
err (np.ndarray): error to aim
Returns:
float:
"""
if err.shape[0] != self.__P.shape[0]:
raise (RuntimeError("Invalid error shape"))
nonlimit_integral = self.__I @ err * self.__dt + self.__integral_value
abs_integral_value = np.minimum(self.__wind_up_max, np.abs(nonlimit_integral))
self.__integral_value = np.multiply(abs_integral_value, np.sign(nonlimit_integral))
# print(self.__integral_value)
d_err = (err - self.__last_err) / (self.__dt + 1e-3)
u = self.__P @ err + self.__integral_value + self.__D @ d_err
self.__last_err = err
# print(u)
return u
@property
def P(self):
return self.__P
@property
def I(self):
return self.__I
@property
def D(self):
return self.__D
@P.setter
def P(self, P: np.ndarray):
assert (
P.shape[0] == self.__P.shape[0] and P.shape[1] == self.__P.shape[1]
), "Invalid input matrix size, expected {:d}x{:d}, but given {:d}x{:d}".format(
self.__P.shape[0], self.__P.shape[1], P.shape[0], P.shape[1]
)
self.__P = P
@I.setter
def I(self, I: np.ndarray):
assert (
I.shape[0] == self.__I.shape[0] and I.shape[1] == self.__I.shape[1]
), "Invalid input matrix size, expected {:d}x{:d}, but given {:d}x{:d}".format(
self.__I.shape[0], self.__I.shape[1], I.shape[0], I.shape[1]
)
self.__I = I
@D.setter
def D(self, D: np.ndarray):
assert (
D.shape[0] == self.__D.shape[0] and D.shape[1] == self.__D.shape[1]
), "Invalid input matrix size, expected {:d}x{:d}, but given {:d}x{:d}".format(
self.__D.shape[0], self.__D.shape[1], D.shape[0], D.shape[1]
)
self.__D = D
class ExternalController(ABC):
"""_summary_
Args:
rob (Robot): _description_
robot_controller_type (str): _description_
"""
def __init__(self, rob: Robot, robot_controller_type: str):
self.robot = rob
self.__robot_controller_type = robot_controller_type
self.__child_controller = None
def connect_controller(self, controller: ExternalController):
"""_summary_
Args:
controller (ExternalController): _description_
"""
self.__child_controller = controller
@abstractmethod
def calc_control(self, target_motion: Motion) -> bool:
pass
def send_control_to_robot(self, target_motion: Motion) -> bool:
"""_summary_
Args:
target_motion (Motion): _description_
Returns:
bool: _description_
"""
assert isinstance(target_motion, Motion), "Invalid type of target state, expected {:s}, but given {:s}".format(
str(Motion), str(type(target_motion))
)
ok = self.calc_control(target_motion)
if not ok:
return False
if not self.__child_controller is None:
return self.__child_controller.send_control_to_robot(target_motion)
return self.robot.set_control(target_motion, self.__robot_controller_type)
class SimpleController(ExternalController):
"""_summary_
Args:
ExternalController (_type_): _description_
"""
def __init__(self, rob: Robot, robot_controller_type: str):
super().__init__(rob, robot_controller_type)
def calc_control(self, target_motion: Motion) -> bool:
return True
class EEVelocityToJointVelocityController(ExternalController):
"""_summary_
Args:
robot (Robot): _description_
"""
def __init__(self, robot: Robot):
super().__init__(robot, RobotControllerType.JOINT_VELOCITIES)
def calc_control(self, target_motion: Motion) -> bool:
"""_summary_
Args:
target_motion (Motion): _description_
Returns:
bool: _description_
"""
target_motion.joint_state.joint_velocities = (
np.linalg.pinv(
self.robot.jacobian(
self.robot.joint_state.joint_positions,
target_motion.ee_state.ee_link,
target_motion.ee_state.ref_frame,
)
)
@ target_motion.ee_state.twist
)
return True
class JointTorquesController(SimpleController):
"""_summary_
Args:
robot (Robot): _description_
"""
def __init__(self, robot: Robot):
super().__init__(robot, RobotControllerType.JOINT_TORQUES)
class JointPositionsController(SimpleController):
"""_summary_
Args:
robot (Robot): _description_
"""
def __init__(self, robot: Robot):
super().__init__(robot, RobotControllerType.JOINT_POSITIONS)
class JointVelocitiesController(SimpleController):
"""_summary_
Args:
robot (Robot): _description_
"""
def __init__(self, robot: Robot):
super().__init__(robot, RobotControllerType.JOINT_VELOCITIES)
class EEPositionToEEVelocityController(ExternalController):
"""_summary_
Args:
robot (Robot): _description_
"""
def __init__(self, robot):
super().__init__(robot, RobotControllerType.TWIST)
self.__pid = MPIDController(10 * np.identity(6), 1e-4 * np.identity(6), 1e-1 * np.identity(6), 1e-3)
def calc_control(self, target_motion: Motion) -> bool:
assert isinstance(target_motion, Motion), "Invalid type of target state, expected {:s}, but given {:s}".format(
str(Motion), str(type(target_motion))
)
current_state = self.robot.ee_state(target_motion.ee_state.ee_link)
target_tf = target_motion.ee_state.tf
current_tf = current_state.tf
pose_err = target_tf.t - current_tf.t
orient_error = target_tf.R @ current_tf.R.T
twist_err = (SE3(*pose_err.tolist()) @ SE3(SO3(orient_error, check=False))).twist().A
target_twist = self.__pid.u(twist_err)
target_motion.ee_state.twist = target_twist
return True
class EEForceHybrideToEEVelocityController(ExternalController):
"""_summary_
Args:
robot (Robot): _description_
selected_axis (np.ndarray): _description_
stiffnes (np.ndarray): _description_
ref_basis (str, optional): _description_. Defaults to 'world'.
"""
def __init__(self, robot: Robot, selected_axis: np.ndarray, stiffnes: np.ndarray, ref_basis: str = "world"):
super().__init__(robot, RobotControllerType.TWIST)
self.__pid = MPIDController(10 * np.identity(6), 1e-4 * np.identity(6), 1e-1 * np.identity(6), 1e-3)
self.__ref_basis = ref_basis
self.__stiffnes = stiffnes
self.__T, self.__Y = EEForceHybrideToEEVelocityController.generate_square_selection_matrix(selected_axis)
def calc_control(self, target_motion: Motion) -> bool:
assert isinstance(target_motion, Motion), "Invalid type of target state, expected {:s}, but given {:s}".format(
str(Motion), str(type(target_motion))
)
basis_frame = self.robot.ee_state(self.__ref_basis)
control_basis = basis_frame.tf.R
control_move_block = scipy.linalg.block_diag(control_basis, np.identity(3))
control_force_block = scipy.linalg.block_diag(control_basis, control_basis)
current_state = self.robot.ee_state(target_motion.ee_state.ee_link)
target_tf = target_motion.ee_state.tf
current_tf = current_state.tf
pose_err = target_tf.t - control_basis.T @ current_tf.t
orient_error = target_tf.R @ current_tf.R.T
twist_err = (SE3(*pose_err.tolist()) @ SE3(SO3(orient_error, check=False))).twist().A
force_torque_err = target_motion.ee_state.force_torque - control_move_block.T @ current_state.force_torque
target_move_twist = control_move_block @ self.__T @ self.__pid.u(twist_err)
target_force_torque_twist = control_force_block @ self.__Y @ self.__stiffnes @ -force_torque_err
target_motion.ee_state.twist = target_move_twist + target_force_torque_twist
return True
def generate_square_selection_matrix(allow_moves: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""_summary_
Args:
allow_moves (np.ndarray): _description_
Returns:
Tuple[np.ndarray, np.ndarray]: _description_
"""
T_matrix = np.diag(allow_moves)
Y_matrix = np.identity(T_matrix.shape[0]) - T_matrix
return (T_matrix, Y_matrix)